Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / controller / java / AndroidDeviceControllerWrapper.h
1 /*
2  *   Copyright (c) 2020 Project CHIP Authors
3  *   All rights reserved.
4  *
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
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  */
18 #pragma once
19
20 #include <memory>
21
22 #include <jni.h>
23
24 #include <controller/CHIPDeviceController.h>
25 #include <platform/internal/DeviceNetworkInfo.h>
26
27 /**
28  * This class contains all relevant information for the JNI view of CHIPDeviceController
29  * to handle all controller-related processing.
30  *
31  * Generally it contains the DeviceController class itself, plus any related delegates/callbacks.
32  */
33 class AndroidDeviceControllerWrapper : public chip::Controller::DevicePairingDelegate,
34                                        public chip::Controller::DeviceStatusDelegate,
35                                        public chip::PersistentStorageDelegate
36 {
37 public:
38     ~AndroidDeviceControllerWrapper();
39
40     chip::Controller::DeviceCommissioner * Controller() { return mController.get(); }
41     void SetJavaObjectRef(JavaVM * vm, jobject obj);
42
43     void SendNetworkCredentials(const char * ssid, const char * password);
44     void SendThreadCredentials(const chip::DeviceLayer::Internal::DeviceNetworkInfo & threadData);
45
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;
53
54     // DeviceStatusDelegate implementation
55     void OnMessage(chip::System::PacketBufferHandle msg) override;
56     void OnStatusChange(void) override;
57
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;
63
64     jlong ToJNIHandle()
65     {
66         static_assert(sizeof(jlong) >= sizeof(void *), "Need to store a pointer in a java handle");
67         return reinterpret_cast<jlong>(this);
68     }
69
70     jobject JavaObjectRef() { return mJavaObjectRef; }
71
72     static AndroidDeviceControllerWrapper * FromJNIHandle(jlong handle)
73     {
74         return reinterpret_cast<AndroidDeviceControllerWrapper *>(handle);
75     }
76
77     static AndroidDeviceControllerWrapper * AllocateNew(JavaVM * vm, jobject deviceControllerObj, chip::NodeId nodeId,
78                                                         chip::System::Layer * systemLayer, chip::Inet::InetLayer * inetLayer,
79                                                         CHIP_ERROR * errInfoOnFailure);
80
81 private:
82     using ChipDeviceControllerPtr = std::unique_ptr<chip::Controller::DeviceCommissioner>;
83
84     ChipDeviceControllerPtr mController;
85     chip::RendezvousDeviceCredentialsDelegate * mCredentialsDelegate = nullptr;
86     chip::PersistentStorageResultDelegate * mStorageResultDelegate   = nullptr;
87
88     JavaVM * mJavaVM       = nullptr;
89     jobject mJavaObjectRef = nullptr;
90
91     JNIEnv * GetJavaEnv();
92
93     jclass GetPersistentStorageClass() { return GetJavaEnv()->FindClass("chip/devicecontroller/PersistentStorage"); }
94
95     AndroidDeviceControllerWrapper(ChipDeviceControllerPtr controller) : mController(std::move(controller)) {}
96 };