added to call unregisterReceiver for Android BT/BLE.
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / bt_le_adapter / android / caleclient.h
1 /* ****************************************************************
2  *
3  * Copyright 2014 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  ******************************************************************/
20
21 /**
22  * @file
23  * This file contains the APIs for BT LE communications.
24  */
25 #ifndef CA_LECLIENT_H_
26 #define CA_LECLIENT_H_
27
28 #include "cacommon.h"
29 #include "cathreadpool.h"
30 #include "uarraylist.h"
31 #include "jni.h"
32
33 #ifdef __cplusplus
34 extern "C"
35 {
36 #endif
37
38 static const uint16_t STATE_CHARACTER_SET = 2;
39 static const uint16_t STATE_CHARACTER_UNSET = 1;
40 static const uint16_t STATE_CHARACTER_NO_CHANGE = 0;
41
42 static const uint16_t STATE_SEND_NONE = 0;
43 static const uint16_t STATE_SEND_SUCCESS = 1;
44 static const uint16_t STATE_SEND_FAILED = 2;
45
46 static const jint STATE_CONNECTED = 2;
47 static const jint STATE_DISCONNECTED = 0;
48
49 typedef struct le_state_info
50 {
51     char address[CA_MACADDR_SIZE];
52     jint connectedState;
53     uint16_t notificationState;
54     uint16_t sendState;
55 } CALEState_t;
56
57 /**
58  * Callback to be notified on reception of any data from remote devices.
59  * @param[in]  address                MAC address of remote device.
60  * @param[in]  data                   Data received from remote device.
61  * @pre  Callback must be registered using CALESetCallback(CAPacketReceiveCallback callback)
62  */
63 typedef void (*CAPacketReceiveCallback)(const char *address, const uint8_t *data);
64
65 /**
66  * initialize JNI object.
67  */
68 void CALEClientJniInit();
69
70 /**
71  * set context of application.
72  */
73 void CALEClientJNISetContext();
74
75 /**
76  * create interface object and initialize the object.
77  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
78  */
79 CAResult_t CALEClientCreateJniInterfaceObject();
80
81 /**
82  * initialize client for BLE.
83  * @param[in]   handle                thread pool handle object.
84  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
85  */
86 CAResult_t CALEClientInitialize(ca_thread_pool_t handle);
87
88 /**
89  * terminate client for BLE.
90  */
91 void CALEClientTerminate();
92
93 /**
94  * destroy interface object and terminate the interface.
95  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
96  */
97 CAResult_t CALEClientDestroyJniInterface();
98
99 /**
100  * for destroy sending routine.
101  * @param[in]   env                   JNI interface pointer.
102  * @param[in]   gatt                  Gatt profile object.
103  */
104 void CALEClientSendFinish(JNIEnv *env, jobject gatt);
105
106 /**
107  * send data for unicast (interface).
108  * @param[in]   address               remote address.
109  * @param[in]   data                  data for transmission.
110  * @param[in]   dataLen               data length.
111  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
112  */
113 CAResult_t CALEClientSendUnicastMessage(const char *address, const uint8_t *data,
114                                         const uint32_t dataLen);
115
116 /**
117  * send data for multicast (interface).
118  * @param[in]   data                  data for transmission.
119  * @param[in]   dataLen               data length.
120  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
121  */
122 CAResult_t CALEClientSendMulticastMessage(const uint8_t *data, const uint32_t dataLen);
123
124 /**
125  * start unicast server.
126  * @param[in]   address               remote address.
127  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
128  */
129 CAResult_t CALEClientStartUnicastServer(const char *address);
130
131 /**
132  * start multicast server (start discovery).
133  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
134  */
135 CAResult_t CALEClientStartMulticastServer();
136
137 /**
138  * stop unicast server.
139  */
140 void CALEClientStopUnicastServer();
141
142 /**
143  * stop multicast server (stop discovery).
144  */
145 void CALEClientStopMulticastServer();
146
147 /**
148  * set this callback for receiving data packets from peer devices.
149  * @param[in]   callback              callback to be notified on reception of
150  *                                    unicast/multicast data packets.
151  */
152 void CALEClientSetCallback(CAPacketReceiveCallback callback);
153
154 /**
155  * send data for unicast (implement).
156  * @param[in]   address               remote address.
157  * @param[in]   data                  data for transmission.
158  * @param[in]   dataLen               data length.
159  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
160  */
161 CAResult_t CALEClientSendUnicastMessageImpl(const char *address, const uint8_t *data,
162                                             const uint32_t dataLen);
163
164 /**
165  * send data for multicast (implement).
166  * @param[in]   env                   JNI interface pointer.
167  * @param[in]   data                  data for transmission.
168  * @param[in]   dataLen               data length.
169  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
170  */
171 CAResult_t CALEClientSendMulticastMessageImpl(JNIEnv *env, const uint8_t *data,
172                                               const uint32_t dataLen);
173
174 /**
175  * check whether it is connected or not with remote address.
176  * @param[in]   address               remote address.
177  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
178  */
179 CAResult_t CALECheckSendState(const char* address);
180
181 /**
182  * send data to remote device.
183  * if it isn't connected yet. connect LE before try to send data.
184  * @param[in]   env                   JNI interface pointer.
185  * @param[in]   device                bluetooth device object.
186  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
187  */
188 CAResult_t CALEClientSendData(JNIEnv *env, jobject device);
189
190 /**
191  * get address from bluetooth gatt object.
192  * @param[in]   env                   JNI interface pointer.
193  * @param[in]   gatt                  Gatt profile object.
194  * @return  bluetooth address.
195  */
196 jstring CALEClientGetAddressFromGattObj(JNIEnv *env, jobject gatt);
197
198 /**
199  * get remote address from bluetooth socket object.
200  * @param[in]   env                   JNI interface pointer.
201  * @param[in]   bluetoothSocketObj    bluetooth socket.
202  * @return  bluetooth address.
203  */
204 jstring CALEClientGetRemoteAddress(JNIEnv *env, jobject bluetoothSocketObj);
205
206 /**
207  * close gatt.
208  * @param[in]   env                   JNI interface pointer.
209  * @param[in]   bluetoothGatt         gatt profile object.
210  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
211  */
212 CAResult_t CALEClientGattClose(JNIEnv *env, jobject bluetoothGatt);
213
214 /**
215  * start to scan whole bluetooth devices (interface).
216  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
217  */
218 CAResult_t CALEClientStartScan();
219
220 /**
221  * start to scan whole bluetooth devices (implement).
222  * @param[in]   env                   JNI interface pointer.
223  * @param[in]   callback              callback to receive device object by scanning.
224  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
225  */
226 CAResult_t CALEClientStartScanImpl(JNIEnv *env, jobject callback);
227
228 /**
229  * start to scan target bluetooth devices for service uuid (implement).
230  * @param[in]   env                   JNI interface pointer.
231  * @param[in]   uuids                 target UUID.
232  * @param[in]   callback              callback to receive device object by scanning.
233  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
234  */
235 CAResult_t CALEClientStartScanWithUUIDImpl(JNIEnv *env, jobjectArray uuids,
236                                            jobject callback);
237
238 /**
239  * get uuid object.
240  * @param[in]   env                   JNI interface pointer.
241  * @param[in]   uuid                  uuid.
242  * @return  uuid object.
243  */
244 jobject CALEClientGetUUIDObject(JNIEnv *env, const char *uuid);
245
246 /**
247  * stop scan (interface).
248  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
249  */
250 CAResult_t CALEClientStopScan();
251
252 /**
253  * set ble scanning flag.
254  * @param[in]   flag        scan flag.
255  */
256 void CALEClientSetScanFlag(bool flag);
257
258 /**
259  * stop scan (implement).
260  * @param[in]   env                   JNI interface pointer.
261  * @param[in]   callback              callback to receive device object by scanning.
262  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
263  */
264 CAResult_t CALEClientStopScanImpl(JNIEnv *env, jobject callback);
265
266 /**
267  * connect to gatt server hosted.
268  * @param[in]   env                   JNI interface pointer.
269  * @param[in]   bluetoothDevice       bluetooth Device object.
270  * @param[in]   autoconnect           whether to directly connect to the remote device(false) or
271  *                                     to automatically connect as soon as the remote device
272  *                                     becomes available.
273  * @param[in]   callback              callback for connection state change.
274  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
275  */
276 CAResult_t CALEClientConnect(JNIEnv *env, jobject bluetoothDevice, jboolean autoconnect,
277                              jobject callback);
278
279 /**
280  * disconnect to gatt server by a target device.
281  * @param[in]   env                   JNI interface pointer.
282  * @param[in]   bluetoothGatt         Gatt profile object.
283  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
284  */
285 CAResult_t CALEClientDisconnect(JNIEnv *env, jobject bluetoothGatt);
286
287 /**
288  * disconnect to gatt server by whole devices.
289  * @param[in]   env                   JNI interface pointer.
290  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
291  */
292 CAResult_t CALEClientDisconnectAll(JNIEnv *env);
293
294 /**
295  * start discovery server.
296  * @param[in]   env                   JNI interface pointer.
297  * @param[in]   bluetoothGatt         Gatt profile object.
298  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
299  */
300 CAResult_t CALEClientDiscoverServices(JNIEnv *env, jobject bluetoothGatt);
301
302 /**
303  * create GattCharacteristic and call CALEClientWriteCharacteristicImpl
304  * for request to write gatt characteristic.
305  * @param[in]   env                   JNI interface pointer.
306  * @param[in]   gatt                  Gatt profile object.
307  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
308  */
309 CAResult_t CALEClientWriteCharacteristic(JNIEnv *env, jobject gatt);
310
311 /**
312  * request to write gatt characteristic.
313  * @param[in]   env                   JNI interface pointer.
314  * @param[in]   bluetoothGatt         Gatt profile object.
315  * @param[in]   gattCharacteristic    characteristic object that contain data to send.
316  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
317  */
318 CAResult_t CALEClientWriteCharacteristicImpl(JNIEnv *env, jobject bluetoothGatt,
319                                              jobject gattCharacteristic);
320
321 /**
322  * request to read gatt characteristic.
323  * @param[in]   env                   JNI interface pointer.
324  * @param[in]   bluetoothGatt         Gatt profile object.
325  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
326  */
327 CAResult_t CALEClientReadCharacteristic(JNIEnv *env, jobject bluetoothGatt);
328
329 /**
330  * enable notification for a target device.
331  * @param[in]   env                   JNI interface pointer.
332  * @param[in]   bluetoothGatt         Gatt profile object.
333  * @param[in]   characteristic        Characteristic object.
334  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
335  */
336 CAResult_t CALEClientSetCharacteristicNotification(JNIEnv *env, jobject bluetoothGatt,
337                                                   jobject characteristic);
338
339 /**
340  * create gatt characteristic object.
341  * @param[in]   env                   JNI interface pointer.
342  * @param[in]   bluetoothGatt         Gatt profile object.
343  * @param[in]   data                  for make Characteristic with data.
344  * @return  Gatt Characteristic object.
345  */
346 jobject CALEClientCreateGattCharacteristic(JNIEnv *env, jobject bluetoothGatt, jbyteArray data);
347
348 /**
349  * get gatt service.
350  * @param[in]   env                   JNI interface pointer.
351  * @param[in]   bluetoothGatt         Gatt profile object.
352  * @param[in]   characterUUID         for make BluetoothGattCharacteristic object.
353  * @return  Gatt Service.
354  */
355 jobject CALEClientGetGattService(JNIEnv *env, jobject bluetoothGatt, jstring characterUUID);
356
357 /**
358  * get value from characteristic.
359  * @param[in]   env                   JNI interface pointer.
360  * @param[in]   characteristic        Characteristic object.
361  * @return  value in characteristic.
362  */
363 jbyteArray CALEClientGetValueFromCharacteristic(JNIEnv *env, jobject characteristic);
364
365 /**
366  * create UUID List.
367  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
368  */
369 CAResult_t CALEClientCreateUUIDList();
370
371 /**
372  * set UUID to descriptor.
373  * @param[in]   env                   JNI interface pointer.
374  * @param[in]   bluetoothGatt         Gatt profile object.
375  * @param[in]   characteristic        Characteristic object.
376  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
377  */
378 CAResult_t CALEClientSetUUIDToDescriptor(JNIEnv *env, jobject bluetoothGatt,
379                                          jobject characteristic);
380
381 /**
382  * add device object to scan device list.
383  * @param[in]   env                   JNI interface pointer.
384  * @param[in]   device                bluetooth device object.
385  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
386  */
387 CAResult_t CALEClientAddScanDeviceToList(JNIEnv *env, jobject device);
388
389 /**
390  * check whether the device exist in list or not.
391  * @param[in]   env                   JNI interface pointer.
392  * @param[in]   remoteAddress         remote address.
393  * @return  true or false.
394  */
395 bool CALEClientIsDeviceInScanDeviceList(JNIEnv *env, const char *remoteAddress);
396
397 /**
398  * remove all devices in scan device list.
399  * @param[in]   env                   JNI interface pointer.
400  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
401  */
402 CAResult_t CALEClientRemoveAllScanDevices(JNIEnv *env);
403
404 /**
405  * remove target device in scan device list.
406  * @param[in]   env                   JNI interface pointer.
407  * @param[in]   remoteAddress         remote address.
408  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
409  */
410 CAResult_t CALEClientRemoveDeviceInScanDeviceList(JNIEnv *env, jstring remoteAddress);
411
412 /**
413  * add gatt object to gatt object list.
414  * @param[in]   env                   JNI interface pointer.
415  * @param[in]   gatt                  Gatt profile object.
416  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
417  */
418 CAResult_t CALEClientAddGattobjToList(JNIEnv *env, jobject gatt);
419
420 /**
421  * check whether the gatt object exist in list or not.
422  * @param[in]   env                   JNI interface pointer.
423  * @param[in]   remoteAddress         remote address.
424  * @return  true or false.
425  */
426 bool CALEClientIsGattObjInList(JNIEnv *env, const char *remoteAddress);
427
428 /**
429  * get the gatt object.
430  * @param[in]   env                   JNI interface pointer.
431  * @param[in]   remoteAddress         remote address.
432  * @return  gatt object.
433  */
434 jobject CALEClientGetGattObjInList(JNIEnv *env, const char* remoteAddress);
435
436 /**
437  * remove all gatt objects in gatt object list.
438  * @param[in]   env                   JNI interface pointer.
439  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
440  */
441 CAResult_t CALEClientRemoveAllGattObjs(JNIEnv *env);
442
443 /**
444  * remove target device in gatt object list.
445  * @param[in]   env                   JNI interface pointer.
446  * @param[in]   gatt                  Gatt profile object.
447  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
448  */
449 CAResult_t CALEClientRemoveGattObj(JNIEnv *env, jobject gatt);
450
451 /**
452  * remove gatt object of target device for address in gatt object list.
453  * @param[in]   env                   JNI interface pointer.
454  * @param[in]   gatt                  Gatt profile object.
455  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
456  */
457 CAResult_t CALEClientRemoveGattObjForAddr(JNIEnv *env, jstring addr);
458
459 /**
460  * update new state information.
461  * @param[in]   address               remote address.
462  * @param[in]   connectedState        connection state.
463  * @param[in]   notificationState     whether characteristic notification already set or not.
464  * @param[in]   sendState             whether sending was success or not.
465  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
466  */
467 CAResult_t CALEClientUpdateDeviceState(const char* address, uint32_t connectedState,
468                                        uint16_t notificationState, uint16_t sendState);
469
470 /**
471  * add new state to state list.
472  * @param[in]   state                 new state.
473  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
474  */
475 CAResult_t CALEClientAddDeviceStateToList(CALEState_t* state);
476
477 /**
478  * check whether the remote address is existed or not.
479  * @param[in]   address               remote address.
480  * @return  true or false.
481  */
482 bool CALEClientIsDeviceInList(const char *remoteAddress);
483
484 /**
485  * remove all device states.
486  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
487  */
488 CAResult_t CALEClientRemoveAllDeviceState();
489
490 /**
491  * remove the device state for a remote device.
492  * @param[in]   remoteAddress         remote address.
493  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
494  */
495 CAResult_t CALEClientRemoveDeviceState(const char* remoteAddress);
496
497 /**
498  * get state information for a remote device.
499  * @param[in]   remoteAddress         remote address.
500  * @return  CALEState_t.
501  */
502 CALEState_t* CALEClientGetStateInfo(const char* remoteAddress);
503
504 /**
505  * check whether the remote address is connected or not.
506  * @param[in]   remoteAddress         remote address.
507  * @return  true or false.
508  */
509 bool CALEClientIsConnectedDevice(const char* remoteAddress);
510
511 /**
512  * check whether the remote address set CharacteristicNotification or not.
513  * @param[in]   remoteAddress         remote address.
514  * @return  true or false.
515  */
516 bool CALEClientIsSetCharacteristic(const char* remoteAddress);
517
518 /**
519  * create scan device list.
520  */
521 void CALEClientCreateDeviceList();
522
523 /**
524  * update the counter which data is sent to remote device.
525  * @param[in]   env                   JNI interface pointer.
526  */
527 void CALEClientUpdateSendCnt(JNIEnv *env);
528
529 /**
530  * initialize mutex.
531  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
532  */
533 CAResult_t CALEClientInitGattMutexVaraibles();
534
535 /**
536  * terminate mutex.
537  */
538 void CALEClientTerminateGattMutexVariables();
539
540 /**
541  * set send finish flag.
542  * @param[in]   flag        finish flag.
543  */
544 void CALEClientSetSendFinishFlag(bool flag);
545
546 #ifdef __cplusplus
547 } /* extern "C" */
548 #endif
549
550 #endif /* CA_LECLIENT_H_ */