Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / controller / java / src / chip / devicecontroller / ChipDeviceController.java
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 package chip.devicecontroller;
19
20 import android.bluetooth.BluetoothGatt;
21 import android.bluetooth.BluetoothGattCallback;
22 import android.util.Log;
23
24 /** Controller to interact with the CHIP device. */
25 public class ChipDeviceController {
26   private static final String TAG = ChipDeviceController.class.getSimpleName();
27
28   private long deviceControllerPtr;
29   private int connectionId;
30   private BluetoothGatt bleGatt;
31   private CompletionListener completionListener;
32
33   public ChipDeviceController() {
34     deviceControllerPtr = newDeviceController();
35   }
36
37   public void setCompletionListener(CompletionListener listener) {
38     completionListener = listener;
39   }
40
41   public BluetoothGatt getBluetoothGatt() {
42     return bleGatt;
43   }
44
45   public BluetoothGattCallback getCallback() {
46     return AndroidChipStack.getInstance().getCallback();
47   }
48
49   public void pairDevice(BluetoothGatt bleServer, long deviceId, long setupPincode) {
50     if (connectionId == 0) {
51       bleGatt = bleServer;
52
53       connectionId = AndroidChipStack.getInstance().addConnection(this);
54       if (connectionId == 0) {
55         Log.e(TAG, "Failed to add Bluetooth connection.");
56         completionListener.onError(new Exception("Failed to add Bluetooth connection."));
57         return;
58       }
59
60       Log.d(TAG, "Bluetooth connection added with ID: " + connectionId);
61       Log.d(TAG, "Pairing device with ID: " + deviceId);
62       pairDevice(deviceControllerPtr, deviceId, connectionId, setupPincode);
63     } else {
64       Log.e(TAG, "Bluetooth connection already in use.");
65       completionListener.onError(new Exception("Bluetooth connection already in use."));
66     }
67   }
68
69   public void unpairDevice(long deviceId) {
70     unpairDevice(deviceControllerPtr, deviceId);
71   }
72
73   public void pairTestDeviceWithoutSecurity(String ipAddress) {
74     pairTestDeviceWithoutSecurity(deviceControllerPtr, ipAddress);
75   }
76
77   public void pairDevice(long deviceId, int connectionId, long pinCode) {
78     pairDevice(deviceControllerPtr, deviceId, connectionId, pinCode);
79   }
80
81   public void sendWiFiCredentials(String ssid, String password) {
82     sendWiFiCredentials(deviceControllerPtr, ssid, password);
83   }
84
85   public void sendThreadCredentials(int channel, int panId, byte[] xpanId, byte[] masterKey) {
86     sendThreadCredentials(deviceControllerPtr, channel, panId, xpanId, masterKey);
87   }
88
89   public boolean disconnectDevice(long deviceId) {
90     return disconnectDevice(deviceControllerPtr, deviceId);
91   }
92
93   public void onConnectDeviceComplete() {
94     completionListener.onConnectDeviceComplete();
95   }
96
97   public void onSendMessageComplete(String message) {
98     completionListener.onSendMessageComplete(message);
99   }
100
101   public void onNetworkCredentialsRequested() {
102     if (completionListener != null) {
103       completionListener.onNetworkCredentialsRequested();
104     }
105   }
106
107   public void onOperationalCredentialsRequested(byte[] csr) {
108     if (completionListener != null) {
109       completionListener.onOperationalCredentialsRequested(csr);
110     }
111   }
112
113   public void onStatusUpdate(int status) {
114     if (completionListener != null) {
115       completionListener.onStatusUpdate(status);
116     }
117   }
118
119   public void onPairingComplete(int errorCode) {
120     if (completionListener != null) {
121       completionListener.onPairingComplete(errorCode);
122     }
123   }
124
125   public void onPairingDeleted(int errorCode) {
126     if (completionListener != null) {
127       completionListener.onPairingDeleted(errorCode);
128     }
129   }
130
131   public void onNotifyChipConnectionClosed(int connId) {
132     // Clear connection state.
133     AndroidChipStack.getInstance().removeConnection(connId);
134     connectionId = 0;
135     bleGatt = null;
136
137     Log.d(TAG, "Calling onNotifyChipConnectionClosed()");
138     completionListener.onNotifyChipConnectionClosed();
139   }
140
141   public void onCloseBleComplete(int connId) {
142     if (releaseBluetoothGatt(connId)) {
143       Log.d(TAG, "Calling onCloseBleComplete()");
144       completionListener.onCloseBleComplete();
145     } else {
146       Log.d(TAG, "Skipped calling onCloseBleComplete(). Connection has already been closed.");
147     }
148   }
149
150   public void onError(Throwable error) {
151     completionListener.onError(error);
152   }
153
154   public void close() {
155     releaseBluetoothGatt(connectionId);
156   }
157
158   private boolean releaseBluetoothGatt(int connId) {
159     if (connectionId == 0) {
160       return false;
161     }
162
163     Log.d(TAG, "Closing GATT and removing connection for " + connId);
164
165     // Close gatt
166     bleGatt.close();
167
168     // Clear connection state.
169     AndroidChipStack.getInstance().removeConnection(connId);
170     connectionId = 0;
171     bleGatt = null;
172     return true;
173   }
174
175   public String getIpAddress(long deviceId) {
176     return getIpAddress(deviceControllerPtr, deviceId);
177   }
178
179   public void sendMessage(long deviceId, String message) {
180     sendMessage(deviceControllerPtr, deviceId, message);
181   }
182
183   public void sendCommand(long deviceId, ChipCommandType command, int value) {
184     sendCommand(deviceControllerPtr, deviceId, command, value);
185   }
186
187   public boolean openPairingWindow(long deviceId, int duration) {
188     return openPairingWindow(deviceControllerPtr, deviceId, duration);
189   }
190
191   public boolean isActive(long deviceId) {
192     return isActive(deviceControllerPtr, deviceId);
193   }
194
195   private native long newDeviceController();
196
197   private native void pairDevice(
198       long deviceControllerPtr, long deviceId, int connectionId, long pinCode);
199
200   private native void unpairDevice(long deviceControllerPtr, long deviceId);
201
202   private native void pairTestDeviceWithoutSecurity(long deviceControllerPtr, String ipAddress);
203
204   private native void sendWiFiCredentials(long deviceControllerPtr, String ssid, String password);
205
206   private native void sendThreadCredentials(
207       long deviceControllerPtr, int channel, int panId, byte[] xpanId, byte[] masterKey);
208
209   private native boolean disconnectDevice(long deviceControllerPtr, long deviceId);
210
211   private native void deleteDeviceController(long deviceControllerPtr);
212
213   private native String getIpAddress(long deviceControllerPtr, long deviceId);
214
215   private native void sendMessage(long deviceControllerPtr, long deviceId, String message);
216
217   private native void sendCommand(
218       long deviceControllerPtr, long deviceId, ChipCommandType command, int value);
219
220   private native boolean openPairingWindow(long deviceControllerPtr, long deviceId, int duration);
221
222   private native boolean isActive(long deviceControllerPtr, long deviceId);
223
224   static {
225     System.loadLibrary("CHIPController");
226   }
227
228   @SuppressWarnings("deprecation")
229   protected void finalize() throws Throwable {
230     super.finalize();
231
232     if (deviceControllerPtr != 0) {
233       deleteDeviceController(deviceControllerPtr);
234       deviceControllerPtr = 0;
235     }
236   }
237
238   /** Interface to listen for callbacks from CHIPDeviceController. */
239   public interface CompletionListener {
240
241     /** Notifies the completion of "ConnectDevice" command. */
242     void onConnectDeviceComplete();
243
244     /** Notifies the completion of "SendMessage" echo command. */
245     void onSendMessageComplete(String message);
246
247     /** Notifies that the device is ready to receive Wi-Fi network credentials. */
248     void onNetworkCredentialsRequested();
249
250     /** Notifies that the device is ready to receive operational credentials. */
251     void onOperationalCredentialsRequested(byte[] csr);
252
253     /** Notifies the pairing status. */
254     void onStatusUpdate(int status);
255
256     /** Notifies the completion of pairing. */
257     void onPairingComplete(int errorCode);
258
259     /** Notifies the deletion of pairing session. */
260     void onPairingDeleted(int errorCode);
261
262     /** Notifies that the Chip connection has been closed. */
263     void onNotifyChipConnectionClosed();
264
265     /** Notifies the completion of the "close BLE connection" command. */
266     void onCloseBleComplete();
267
268     /** Notifies the listener of the error. */
269     void onError(Throwable error);
270   }
271 }