Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Android] Implement pairing with code API #26324

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ object ChipClient {
if (!this::androidPlatform.isInitialized && context != null) {
//force ChipDeviceController load jni
ChipDeviceController.loadJni()
androidPlatform = AndroidChipPlatform(AndroidBleManager(), PreferencesKeyValueStoreManager(context), PreferencesConfigurationManager(context), NsdManagerServiceResolver(context), NsdManagerServiceBrowser(context), ChipMdnsCallbackImpl(), DiagnosticDataProviderImpl(context))
androidPlatform = AndroidChipPlatform(AndroidBleManager(context), PreferencesKeyValueStoreManager(context), PreferencesConfigurationManager(context), NsdManagerServiceResolver(context), NsdManagerServiceBrowser(context), ChipMdnsCallbackImpl(), DiagnosticDataProviderImpl(context))
joonhaengHeo marked this conversation as resolved.
Show resolved Hide resolved
}

return androidPlatform
Expand Down
36 changes: 36 additions & 0 deletions src/controller/java/CHIPDeviceController-JNI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,42 @@ JNI_METHOD(void, pairDeviceWithAddress)
}
}

JNI_METHOD(void, pairDeviceWithCode)
joonhaengHeo marked this conversation as resolved.
Show resolved Hide resolved
(JNIEnv * env, jobject self, jlong handle, jlong deviceId, jstring setUpCode, jbyteArray csrNonce, jobject networkCredentials)
{
chip::DeviceLayer::StackLock lock;
CHIP_ERROR err = CHIP_NO_ERROR;
AndroidDeviceControllerWrapper * wrapper = AndroidDeviceControllerWrapper::FromJNIHandle(handle);

ChipLogProgress(Controller, "pairDeviceWithCode() called");

JniUtfString setUpCodeJniString(env, setUpCode);

CommissioningParameters commissioningParams = wrapper->GetCommissioningParameters();
if (csrNonce != nullptr)
{
JniByteArray jniCsrNonce(env, csrNonce);
commissioningParams.SetCSRNonce(jniCsrNonce.byteSpan());
}

if (networkCredentials != nullptr)
{
wrapper->ApplyNetworkCredentials(commissioningParams, networkCredentials);
}

if (wrapper->GetDeviceAttestationDelegateBridge() != nullptr)
{
commissioningParams.SetDeviceAttestationDelegate(wrapper->GetDeviceAttestationDelegateBridge());
}
err = wrapper->Controller()->PairDevice(deviceId, setUpCodeJniString.c_str(), commissioningParams, DiscoveryType::kAll);

if (err != CHIP_NO_ERROR)
{
ChipLogError(Controller, "Failed to pair the device.");
JniReferences::GetInstance().ThrowError(env, sChipDeviceControllerExceptionCls, err);
}
}

JNI_METHOD(void, establishPaseConnection)(JNIEnv * env, jobject self, jlong handle, jlong deviceId, jint connObj, jlong pinCode)
{
chip::DeviceLayer::StackLock lock;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,14 @@ public void pairDeviceWithAddress(
deviceControllerPtr, deviceId, address, port, discriminator, pinCode, csrNonce);
}

public void pairDeviceWithCode(
joonhaengHeo marked this conversation as resolved.
Show resolved Hide resolved
long deviceId,
String setupCode,
@Nullable byte[] csrNonce,
@Nullable NetworkCredentials networkCredentials) {
pairDeviceWithCode(deviceControllerPtr, deviceId, setupCode, csrNonce, networkCredentials);
}

public void establishPaseConnection(long deviceId, int connId, long setupPincode) {
if (connectionId == 0) {
connectionId = connId;
Expand Down Expand Up @@ -996,6 +1004,13 @@ private native void pairDeviceWithAddress(
long pinCode,
@Nullable byte[] csrNonce);

private native void pairDeviceWithCode(
long deviceControllerPtr,
long deviceId,
String setupCode,
@Nullable byte[] csrNonce,
@Nullable NetworkCredentials networkCredentials);

private native void establishPaseConnection(
long deviceControllerPtr, long deviceId, int connId, long setupPincode);

Expand Down
5 changes: 5 additions & 0 deletions src/platform/android/AndroidChipPlatform-JNI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

#include "AndroidChipPlatform-JNI.h"
#include "BLEManagerImpl.h"
#include "BleConnectCallback-JNI.h"
#include "CommissionableDataProviderImpl.h"
#include "DiagnosticDataProviderImpl.h"
#include "DnssdImpl.h"
Expand Down Expand Up @@ -84,6 +85,9 @@ CHIP_ERROR AndroidChipPlatformJNI_OnLoad(JavaVM * jvm, void * reserved)
SuccessOrExit(err);
ChipLogProgress(DeviceLayer, "Java class references loaded.");

err = BleConnectCallbackJNI_OnLoad(jvm, reserved);
SuccessOrExit(err);

chip::InitializeTracing();

exit:
Expand All @@ -99,6 +103,7 @@ CHIP_ERROR AndroidChipPlatformJNI_OnLoad(JavaVM * jvm, void * reserved)
void AndroidChipPlatformJNI_OnUnload(JavaVM * jvm, void * reserved)
{
ChipLogProgress(DeviceLayer, "AndroidChipPlatform JNI_OnUnload() called");
BleConnectCallbackJNI_OnUnload(jvm, reserved);
chip::Platform::MemoryShutdown();
}

Expand Down
22 changes: 20 additions & 2 deletions src/platform/android/BLEManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ void BLEManagerImpl::InitializeWithObject(jobject manager)
env->ExceptionClear();
}

mOnNewConnectionMethod = env->GetMethodID(BLEManagerClass, "onNewConnection", "(I)V");
mOnNewConnectionMethod = env->GetMethodID(BLEManagerClass, "onNewConnection", "(IZJJ)V");
if (mOnNewConnectionMethod == nullptr)
{
ChipLogError(DeviceLayer, "Failed to access BLEManager 'onNewConnection' method");
Expand Down Expand Up @@ -186,16 +186,19 @@ bool BLEManagerImpl::_IsAdvertising()

CHIP_ERROR BLEManagerImpl::_SetAdvertisingMode(BLEAdvertisingMode mode)
{
ChipLogDetail(DeviceLayer, "%s, %u", __FUNCTION__, static_cast<uint8_t>(mode));
return CHIP_ERROR_NOT_IMPLEMENTED;
}

CHIP_ERROR BLEManagerImpl::_GetDeviceName(char * buf, size_t bufSize)
{
ChipLogDetail(DeviceLayer, "%s, %s", __FUNCTION__, buf);
return CHIP_ERROR_NOT_IMPLEMENTED;
}

CHIP_ERROR BLEManagerImpl::_SetDeviceName(const char * deviceName)
{
ChipLogDetail(DeviceLayer, "%s, %s", __FUNCTION__, deviceName);
return CHIP_ERROR_NOT_IMPLEMENTED;
}

Expand Down Expand Up @@ -449,6 +452,18 @@ void BLEManagerImpl::NotifyChipConnectionClosed(BLE_CONNECTION_OBJECT conId)

// ===== start implement virtual methods on BleConnectionDelegate.

void BLEManagerImpl::OnConnectSuccess(void * appState, BLE_CONNECTION_OBJECT connObj)
{
chip::DeviceLayer::StackLock lock;
BleConnectionDelegate::OnConnectionComplete(appState, connObj);
}

void BLEManagerImpl::OnConnectFailed(void * appState, CHIP_ERROR err)
{
chip::DeviceLayer::StackLock lock;
BleConnectionDelegate::OnConnectionError(appState, err);
}

void BLEManagerImpl::NewConnection(BleLayer * bleLayer, void * appState, const SetupDiscriminator & connDiscriminator)
{
chip::DeviceLayer::StackUnlock unlock;
Expand All @@ -474,7 +489,10 @@ void BLEManagerImpl::NewConnection(BleLayer * bleLayer, void * appState, const S
{
discriminator = connDiscriminator.GetLongValue();
}
env->CallVoidMethod(mBLEManagerObject, mOnNewConnectionMethod, static_cast<jint>(discriminator));

env->CallVoidMethod(mBLEManagerObject, mOnNewConnectionMethod, static_cast<jint>(discriminator),
static_cast<jboolean>(connDiscriminator.IsShortDiscriminator()), reinterpret_cast<jlong>(this),
reinterpret_cast<jlong>(appState));
VerifyOrExit(!env->ExceptionCheck(), err = CHIP_JNI_ERROR_EXCEPTION_THROWN);

exit:
Expand Down
3 changes: 3 additions & 0 deletions src/platform/android/BLEManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ class BLEManagerImpl final : public BLEManager,

void InitializeWithObject(jobject managerObject);

void OnConnectSuccess(void * appState, BLE_CONNECTION_OBJECT connObj);
void OnConnectFailed(void * appState, CHIP_ERROR err);

private:
// ===== Members that implement the BLEManager internal interface.

Expand Down
2 changes: 2 additions & 0 deletions src/platform/android/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ static_library("android") {
"AndroidConfig.h",
"BLEManagerImpl.cpp",
"BLEManagerImpl.h",
"BleConnectCallback-JNI.cpp",
"BlePlatformConfig.h",
"CHIPDevicePlatformEvent.h",
"CHIPP256KeypairBridge.cpp",
Expand Down Expand Up @@ -81,6 +82,7 @@ android_library("java") {
"java/chip/platform/AndroidChipPlatform.java",
"java/chip/platform/AndroidChipPlatformException.java",
"java/chip/platform/BleCallback.java",
"java/chip/platform/BleConnectCallback.java",
"java/chip/platform/BleManager.java",
"java/chip/platform/ChipMdnsCallback.java",
"java/chip/platform/ChipMdnsCallbackImpl.java",
Expand Down
54 changes: 54 additions & 0 deletions src/platform/android/BleConnectCallback-JNI.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (c) 2023 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

#include <jni.h>
#include <lib/core/CHIPError.h>
#include <platform/ConnectivityManager.h>

#include "BLEManagerImpl.h"

using namespace chip;
using namespace chip::DeviceLayer::Internal;

#define JNI_METHOD(RETURN, METHOD_NAME) extern "C" JNIEXPORT RETURN JNICALL Java_chip_platform_BleConnectCallback_##METHOD_NAME

CHIP_ERROR BleConnectCallbackJNI_OnLoad(JavaVM * jvm, void * reserved)
{
ChipLogProgress(DeviceLayer, "BleConnectCallbackJNI_OnLoad");
return CHIP_NO_ERROR;
}

void BleConnectCallbackJNI_OnUnload(JavaVM * jvm, void * reserved) {}

JNI_METHOD(void, onConnectSuccess)(JNIEnv * env, jobject self, jlong managerImplPtr, jlong appStatePtr, jint connId)
{
#if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE
BLEManagerImpl * impl = reinterpret_cast<BLEManagerImpl *>(managerImplPtr);
void * appState = reinterpret_cast<void *>(appStatePtr);
impl->OnConnectSuccess(appState, reinterpret_cast<BLE_CONNECTION_OBJECT>(connId));
#endif
}

JNI_METHOD(void, onConnectFailed)(JNIEnv * env, jobject self, jlong managerImplPtr, jlong appStatePtr)
{
#if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE
BLEManagerImpl * impl = reinterpret_cast<BLEManagerImpl *>(managerImplPtr);
void * appState = reinterpret_cast<void *>(appStatePtr);
impl->OnConnectFailed(appState, BLE_ERROR_NO_CONNECTION_RECEIVED_CALLBACK);
#endif
}
29 changes: 29 additions & 0 deletions src/platform/android/BleConnectCallback-JNI.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2020-2021 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

/**
* @file
* Implementation of JNI bridge for CHIP Device Controller for Android apps
*
*/

#pragma once

CHIP_ERROR BleConnectCallbackJNI_OnLoad(JavaVM * jvm, void * reserved);

void BleConnectCallbackJNI_OnUnload(JavaVM * jvm, void * reserved);
Loading