2 * Copyright (c) 2020 Project CHIP Authors
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
24 #include <controller/CHIPDeviceController.h>
25 #include <platform/internal/DeviceNetworkInfo.h>
28 * This class contains all relevant information for the JNI view of CHIPDeviceController
29 * to handle all controller-related processing.
31 * Generally it contains the DeviceController class itself, plus any related delegates/callbacks.
33 class AndroidDeviceControllerWrapper : public chip::Controller::DevicePairingDelegate,
34 public chip::Controller::DeviceStatusDelegate,
35 public chip::PersistentStorageDelegate
38 ~AndroidDeviceControllerWrapper();
40 chip::Controller::DeviceCommissioner * Controller() { return mController.get(); }
41 void SetJavaObjectRef(JavaVM * vm, jobject obj);
43 void SendNetworkCredentials(const char * ssid, const char * password);
44 void SendThreadCredentials(const chip::DeviceLayer::Internal::DeviceNetworkInfo & threadData);
46 // DevicePairingDelegate implementation
47 void OnNetworkCredentialsRequested(chip::RendezvousDeviceCredentialsDelegate * callback) override;
48 void OnOperationalCredentialsRequested(const char * csr, size_t csr_length,
49 chip::RendezvousDeviceCredentialsDelegate * callback) override;
50 void OnStatusUpdate(chip::RendezvousSessionDelegate::Status status) override;
51 void OnPairingComplete(CHIP_ERROR error) override;
52 void OnPairingDeleted(CHIP_ERROR error) override;
54 // DeviceStatusDelegate implementation
55 void OnMessage(chip::System::PacketBufferHandle msg) override;
56 void OnStatusChange(void) override;
58 // PersistentStorageDelegate implementation
59 void SetStorageDelegate(chip::PersistentStorageResultDelegate * delegate) override;
60 CHIP_ERROR SyncGetKeyValue(const char * key, char * value, uint16_t & size) override;
61 void AsyncSetKeyValue(const char * key, const char * value) override;
62 void AsyncDeleteKeyValue(const char * key) override;
66 static_assert(sizeof(jlong) >= sizeof(void *), "Need to store a pointer in a java handle");
67 return reinterpret_cast<jlong>(this);
70 jobject JavaObjectRef() { return mJavaObjectRef; }
72 static AndroidDeviceControllerWrapper * FromJNIHandle(jlong handle)
74 return reinterpret_cast<AndroidDeviceControllerWrapper *>(handle);
77 static AndroidDeviceControllerWrapper * AllocateNew(JavaVM * vm, jobject deviceControllerObj, chip::NodeId nodeId,
78 chip::System::Layer * systemLayer, chip::Inet::InetLayer * inetLayer,
79 CHIP_ERROR * errInfoOnFailure);
82 using ChipDeviceControllerPtr = std::unique_ptr<chip::Controller::DeviceCommissioner>;
84 ChipDeviceControllerPtr mController;
85 chip::RendezvousDeviceCredentialsDelegate * mCredentialsDelegate = nullptr;
86 chip::PersistentStorageResultDelegate * mStorageResultDelegate = nullptr;
88 JavaVM * mJavaVM = nullptr;
89 jobject mJavaObjectRef = nullptr;
91 JNIEnv * GetJavaEnv();
93 jclass GetPersistentStorageClass() { return GetJavaEnv()->FindClass("chip/devicecontroller/PersistentStorage"); }
95 AndroidDeviceControllerWrapper(ChipDeviceControllerPtr controller) : mController(std::move(controller)) {}