Imported Upstream version 1.1.1
[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 GATT_ERROR = 133;
39
40 static const uint16_t STATE_SEND_NONE = 1;
41 static const uint16_t STATE_SEND_SUCCESS = 2;
42 static const uint16_t STATE_SEND_FAIL = 3;
43 static const uint16_t STATE_SENDING = 4;
44
45 typedef struct le_state_info
46 {
47     char address[CA_MACADDR_SIZE];
48     uint16_t connectedState;
49     uint16_t sendState;
50     jboolean autoConnectFlag;
51 } CALEState_t;
52
53 /**
54  * Callback to be notified on reception of any data from remote devices.
55  * @param[in]  address                MAC address of remote device.
56  * @param[in]  data                   Data received from remote device.
57  * @pre  Callback must be registered using CALESetCallback(CAPacketReceiveCallback callback)
58  */
59 typedef void (*CAPacketReceiveCallback)(const char *address, const uint8_t *data);
60
61 /**
62  * initialize JNI object.
63  */
64 void CALEClientJniInit();
65
66 /**
67  * set context of application.
68  */
69 void CALEClientJNISetContext();
70
71 /**
72  * create interface object and initialize the object.
73  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
74  */
75 CAResult_t CALEClientCreateJniInterfaceObject();
76
77 /**
78  * initialize client for BLE.
79  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
80  */
81 CAResult_t CALEClientInitialize();
82
83 /**
84  * terminate client for BLE.
85  */
86 void CALEClientTerminate();
87
88 /**
89  * destroy interface object and terminate the interface.
90  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
91  */
92 CAResult_t CALEClientDestroyJniInterface();
93
94 /**
95  * for destroy sending routine.
96  * @param[in]   env                   JNI interface pointer.
97  * @param[in]   gatt                  Gatt profile object.
98  */
99 void CALEClientSendFinish(JNIEnv *env, jobject gatt);
100
101 /**
102  * send data for unicast (interface).
103  * @param[in]   address               remote address.
104  * @param[in]   data                  data for transmission.
105  * @param[in]   dataLen               data length.
106  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
107  */
108 CAResult_t CALEClientSendUnicastMessage(const char *address, const uint8_t *data,
109                                         const uint32_t dataLen);
110
111 /**
112  * send data for multicast (interface).
113  * @param[in]   data                  data for transmission.
114  * @param[in]   dataLen               data length.
115  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
116  */
117 CAResult_t CALEClientSendMulticastMessage(const uint8_t *data, const uint32_t dataLen);
118
119 /**
120  * start unicast server.
121  * @param[in]   address               remote address.
122  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
123  */
124 CAResult_t CALEClientStartUnicastServer(const char *address);
125
126 /**
127  * start multicast server (start discovery).
128  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
129  */
130 CAResult_t CALEClientStartMulticastServer();
131
132 /**
133  * stop unicast server.
134  */
135 void CALEClientStopUnicastServer();
136
137 /**
138  * stop multicast server (stop discovery).
139  */
140 void CALEClientStopMulticastServer();
141
142 /**
143  * set this callback for receiving data packets from peer devices.
144  * @param[in]   callback              callback to be notified on reception of
145  *                                    unicast/multicast data packets.
146  */
147 void CALEClientSetCallback(CAPacketReceiveCallback callback);
148
149 /**
150  * waiting to get scanned device from BT Platform.
151  * if there is no scanned device in the list.
152  * @param[in]   env                   JNI interface pointer.
153  * @param[in]   address               LE address.
154  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
155  */
156 CAResult_t CALEClientIsThereScannedDevices(JNIEnv *env, const char* address);
157
158 /**
159  * send data for unicast (implement).
160  * @param[in]   address               remote address.
161  * @param[in]   data                  data for transmission.
162  * @param[in]   dataLen               data length.
163  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
164  */
165 CAResult_t CALEClientSendUnicastMessageImpl(const char *address, const uint8_t *data,
166                                             const uint32_t dataLen);
167
168 /**
169  * send data for multicast (implement).
170  * @param[in]   env                   JNI interface pointer.
171  * @param[in]   data                  data for transmission.
172  * @param[in]   dataLen               data length.
173  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
174  */
175 CAResult_t CALEClientSendMulticastMessageImpl(JNIEnv *env, const uint8_t *data,
176                                               const uint32_t dataLen);
177
178 /**
179  * send data to remote device.
180  * if it isn't connected yet. connect LE before try to send data.
181  * @param[in]   env                   JNI interface pointer.
182  * @param[in]   device                bluetooth device object.
183  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
184  */
185 CAResult_t CALEClientSendData(JNIEnv *env, jobject device);
186
187 /**
188  * get address from bluetooth gatt object.
189  * @param[in]   env                   JNI interface pointer.
190  * @param[in]   gatt                  Gatt profile object.
191  * @return  bluetooth address.
192  */
193 jstring CALEClientGetAddressFromGattObj(JNIEnv *env, jobject gatt);
194
195 /**
196  * get remote address from bluetooth socket object.
197  * @param[in]   env                   JNI interface pointer.
198  * @param[in]   bluetoothSocketObj    bluetooth socket.
199  * @return  bluetooth address.
200  */
201 jstring CALEClientGetRemoteAddress(JNIEnv *env, jobject bluetoothSocketObj);
202
203 /**
204  * close gatt.
205  * @param[in]   env                   JNI interface pointer.
206  * @param[in]   bluetoothGatt         gatt profile object.
207  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
208  */
209 CAResult_t CALEClientGattClose(JNIEnv *env, jobject bluetoothGatt);
210
211 /**
212  * start to scan whole bluetooth devices (interface).
213  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
214  */
215 CAResult_t CALEClientStartScan();
216
217 /**
218  * start to scan whole bluetooth devices (implement).
219  * @param[in]   env                   JNI interface pointer.
220  * @param[in]   callback              callback to receive device object by scanning.
221  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
222  */
223 CAResult_t CALEClientStartScanImpl(JNIEnv *env, jobject callback);
224
225 /**
226  * start to scan target bluetooth devices for service uuid (implement).
227  * @param[in]   env                   JNI interface pointer.
228  * @param[in]   uuids                 target UUID.
229  * @param[in]   callback              callback to receive device object by scanning.
230  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
231  */
232 CAResult_t CALEClientStartScanWithUUIDImpl(JNIEnv *env, jobjectArray uuids,
233                                            jobject callback);
234
235 /**
236  * get uuid object.
237  * @param[in]   env                   JNI interface pointer.
238  * @param[in]   uuid                  uuid.
239  * @return  uuid object.
240  */
241 jobject CALEClientGetUUIDObject(JNIEnv *env, const char *uuid);
242
243 /**
244  * stop scan (interface).
245  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
246  */
247 CAResult_t CALEClientStopScan();
248
249 /**
250  * set ble scanning flag.
251  * @param[in]   flag        scan flag.
252  */
253 void CALEClientSetScanFlag(bool flag);
254
255 /**
256  * stop scan (implement).
257  * @param[in]   env                   JNI interface pointer.
258  * @param[in]   callback              callback to receive device object by scanning.
259  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
260  */
261 CAResult_t CALEClientStopScanImpl(JNIEnv *env, jobject callback);
262
263 /**
264  * set flag into State List.
265  * @param[in]   env                   JNI interface pointer.
266  * @param[in]   jni_address           remote address.
267  * @param[in]   state_idx             state index.
268  * @param[in]   flag                  auto connect flag.
269  */
270 CAResult_t CALEClientSetFlagToState(JNIEnv *env, jstring jni_address,
271                                     jint state_idx, jboolean flag);
272
273 /**
274  * get flag from State List.
275  * @param[in]   env                   JNI interface pointer.
276  * @param[in]   jni_address           remote address.
277  * @param[in]   state_idx             state index.
278  * @return  current flag;
279  */
280 jboolean CALEClientGetFlagFromState(JNIEnv *env, jstring jni_address, jint state_idx);
281
282 /**
283  * connect to gatt server hosted.
284  * @param[in]   env                   JNI interface pointer.
285  * @param[in]   bluetoothDevice       bluetooth Device object.
286  * @param[in]   autoconnect           whether to directly connect to the remote device(false) or
287  *                                     to automatically connect as soon as the remote device
288  *                                     becomes available.
289  * @return  gatt object
290  */
291 jobject CALEClientConnect(JNIEnv *env, jobject bluetoothDevice, jboolean autoconnect);
292
293 /**
294  * disconnect to gatt server by a target device.
295  * @param[in]   env                   JNI interface pointer.
296  * @param[in]   bluetoothGatt         Gatt profile object.
297  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
298  */
299 CAResult_t CALEClientDisconnect(JNIEnv *env, jobject bluetoothGatt);
300
301 /**
302  * disconnect to gatt server by whole devices.
303  * @param[in]   env                   JNI interface pointer.
304  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
305  */
306 CAResult_t CALEClientDisconnectAll(JNIEnv *env);
307
308 /**
309  * disconnect to gatt server by selected address.
310  * @param[in]   env                   JNI interface pointer.
311  * @param[in]   remoteAddress         remote address.
312  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
313  */
314 CAResult_t CALEClientDisconnectforAddress(JNIEnv *env, jstring remoteAddress);
315
316 /**
317  * start discovery server.
318  * @param[in]   env                   JNI interface pointer.
319  * @param[in]   bluetoothGatt         Gatt profile object.
320  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
321  */
322 CAResult_t CALEClientDiscoverServices(JNIEnv *env, jobject bluetoothGatt);
323
324 /**
325  * call CALESetValueAndWriteCharacteristic when connection is successful.
326  * @param[in]   env                   JNI interface pointer.
327  * @param[in]   gatt                  Gatt profile object.
328  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
329  */
330 CAResult_t CALEClientWriteCharacteristic(JNIEnv *env, jobject gatt);
331
332 /**
333  * create GattCharacteristic and call CALEClientWriteCharacteristicImpl
334  * for request to write gatt characteristic.
335  * @param[in]   env                   JNI interface pointer.
336  * @param[in]   gatt                  Gatt profile object.
337  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
338  */
339 CAResult_t CALESetValueAndWriteCharacteristic(JNIEnv *env, jobject gatt);
340
341 /**
342  * request to write gatt characteristic.
343  * @param[in]   env                   JNI interface pointer.
344  * @param[in]   bluetoothGatt         Gatt profile object.
345  * @param[in]   gattCharacteristic    characteristic object that contain data to send.
346  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
347  */
348 CAResult_t CALEClientWriteCharacteristicImpl(JNIEnv *env, jobject bluetoothGatt,
349                                              jobject gattCharacteristic);
350
351 /**
352  * request to read gatt characteristic.
353  * @param[in]   env                   JNI interface pointer.
354  * @param[in]   bluetoothGatt         Gatt profile object.
355  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
356  */
357 CAResult_t CALEClientReadCharacteristic(JNIEnv *env, jobject bluetoothGatt);
358
359 /**
360  * enable notification for a target device.
361  * @param[in]   env                   JNI interface pointer.
362  * @param[in]   bluetoothGatt         Gatt profile object.
363  * @param[in]   characteristic        Characteristic object.
364  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
365  */
366 CAResult_t CALEClientSetCharacteristicNotification(JNIEnv *env, jobject bluetoothGatt,
367                                                   jobject characteristic);
368
369 /**
370  * create gatt characteristic object.
371  * @param[in]   env                   JNI interface pointer.
372  * @param[in]   bluetoothGatt         Gatt profile object.
373  * @param[in]   data                  for make Characteristic with data.
374  * @return  Gatt Characteristic object.
375  */
376 jobject CALEClientCreateGattCharacteristic(JNIEnv *env, jobject bluetoothGatt, jbyteArray data);
377
378 /**
379  * get gatt service.
380  * @param[in]   env                   JNI interface pointer.
381  * @param[in]   bluetoothGatt         Gatt profile object.
382  * @param[in]   characterUUID         for make BluetoothGattCharacteristic object.
383  * @return  Gatt Service.
384  */
385 jobject CALEClientGetGattService(JNIEnv *env, jobject bluetoothGatt, jstring characterUUID);
386
387 /**
388  * get value from characteristic.
389  * @param[in]   env                   JNI interface pointer.
390  * @param[in]   characteristic        Characteristic object.
391  * @return  value in characteristic.
392  */
393 jbyteArray CALEClientGetValueFromCharacteristic(JNIEnv *env, jobject characteristic);
394
395 /**
396  * create UUID List.
397  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
398  */
399 CAResult_t CALEClientCreateUUIDList();
400
401 /**
402  * set UUID to descriptor.
403  * @param[in]   env                   JNI interface pointer.
404  * @param[in]   bluetoothGatt         Gatt profile object.
405  * @param[in]   characteristic        Characteristic object.
406  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
407  */
408 CAResult_t CALEClientSetUUIDToDescriptor(JNIEnv *env, jobject bluetoothGatt,
409                                          jobject characteristic);
410
411 /**
412  * add device object to scan device list.
413  * @param[in]   env                   JNI interface pointer.
414  * @param[in]   device                bluetooth device object.
415  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
416  */
417 CAResult_t CALEClientAddScanDeviceToList(JNIEnv *env, jobject device);
418
419 /**
420  * check whether the device exist in list or not.
421  * @param[in]   env                   JNI interface pointer.
422  * @param[in]   remoteAddress         remote address.
423  * @return  true or false.
424  */
425 bool CALEClientIsDeviceInScanDeviceList(JNIEnv *env, const char *remoteAddress);
426
427 /**
428  * remove all devices in scan device list.
429  * @param[in]   env                   JNI interface pointer.
430  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
431  */
432 CAResult_t CALEClientRemoveAllScanDevices(JNIEnv *env);
433
434 /**
435  * remove target device in scan device list.
436  * @param[in]   env                   JNI interface pointer.
437  * @param[in]   remoteAddress         remote address.
438  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
439  */
440 CAResult_t CALEClientRemoveDeviceInScanDeviceList(JNIEnv *env, jstring remoteAddress);
441
442 /**
443  * add gatt object to gatt object list.
444  * @param[in]   env                   JNI interface pointer.
445  * @param[in]   gatt                  Gatt profile object.
446  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
447  */
448 CAResult_t CALEClientAddGattobjToList(JNIEnv *env, jobject gatt);
449
450 /**
451  * check whether the gatt object exist in list or not.
452  * @param[in]   env                   JNI interface pointer.
453  * @param[in]   remoteAddress         remote address.
454  * @return  true or false.
455  */
456 bool CALEClientIsGattObjInList(JNIEnv *env, const char *remoteAddress);
457
458 /**
459  * get the gatt object.
460  * @param[in]   env                   JNI interface pointer.
461  * @param[in]   remoteAddress         remote address.
462  * @return  gatt object.
463  */
464 jobject CALEClientGetGattObjInList(JNIEnv *env, const char* remoteAddress);
465
466 /**
467  * remove all gatt objects in gatt object list.
468  * @param[in]   env                   JNI interface pointer.
469  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
470  */
471 CAResult_t CALEClientRemoveAllGattObjs(JNIEnv *env);
472
473 /**
474  * remove target device in gatt object list.
475  * @param[in]   env                   JNI interface pointer.
476  * @param[in]   gatt                  Gatt profile object.
477  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
478  */
479 CAResult_t CALEClientRemoveGattObj(JNIEnv *env, jobject gatt);
480
481 /**
482  * remove gatt object of target device for address in gatt object list.
483  * @param[in]   env                   JNI interface pointer.
484  * @param[in]   gatt                  Gatt profile object.
485  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
486  */
487 CAResult_t CALEClientRemoveGattObjForAddr(JNIEnv *env, jstring addr);
488
489 /**
490  * get ble address from Bluetooth device.
491  * @param[in]   env                   JNI interface pointer.
492  * @param[in]   bluetoothDevice       Bluetooth device.
493  * @return  ble address.
494  */
495 jstring CALEClientGetLEAddressFromBTDevice(JNIEnv *env, jobject bluetoothDevice);
496
497 /**
498  * update new state information.
499  * @param[in]   address               remote address.
500  * @param[in]   state_type            state type.
501  * @param[in]   target_state          state index to update.
502  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
503  */
504 CAResult_t CALEClientUpdateDeviceState(const char* address, uint16_t state_type,
505                                        uint16_t target_state);
506
507 /**
508  * check whether the remote address is existed or not.
509  * @param[in]   address               remote address.
510  * @return  true or false.
511  */
512 bool CALEClientIsDeviceInList(const char *remoteAddress);
513
514 /**
515  * remove all device states.
516  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
517  */
518 CAResult_t CALEClientRemoveAllDeviceState();
519
520 /**
521  * Reset values of device state for all of devices.
522  * this method has to be invoked when BT adapter is disabled.
523  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
524  */
525 CAResult_t CALEClientResetDeviceStateForAll();
526
527 /**
528  * remove the device state for a remote device.
529  * @param[in]   remoteAddress         remote address.
530  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
531  */
532 CAResult_t CALEClientRemoveDeviceState(const char* remoteAddress);
533
534 /**
535  * get state information for a remote device.
536  * @param[in]   remoteAddress         remote address.
537  * @return  CALEState_t.
538  */
539 CALEState_t* CALEClientGetStateInfo(const char* remoteAddress);
540
541 /**
542  * check whether the remote address has same state with target state.
543  * @param[in]   remoteAddress         remote address.
544  * @param[in]   state_type            state_type.
545  * @param[in]   target_state          state index to check.
546  * @return  true or false.
547  */
548 bool CALEClientIsValidState(const char* remoteAddress, uint16_t state_type,
549                             uint16_t target_state);
550
551 /**
552  * create scan device list.
553  */
554 void CALEClientCreateDeviceList();
555
556 /**
557  * update the counter which data is sent to remote device.
558  * @param[in]   env                   JNI interface pointer.
559  */
560 void CALEClientUpdateSendCnt(JNIEnv *env);
561
562 /**
563  * initialize mutex.
564  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
565  */
566 CAResult_t CALEClientInitGattMutexVaraibles();
567
568 /**
569  * terminate mutex.
570  */
571 void CALEClientTerminateGattMutexVariables();
572
573 /**
574  * set send finish flag.
575  * @param[in]   flag        finish flag.
576  */
577 void CALEClientSetSendFinishFlag(bool flag);
578
579 /**
580  * close the connection of the profile proxy to the Service.
581  * @param[in]   env                   JNI interface pointer.
582  * @param[in]   gatt                  gatt profile object.
583  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
584  */
585 CAResult_t CALEClientCloseProfileProxy(JNIEnv *env, jobject gatt);
586
587 /**
588  * connect to GATT Server hosted by this device.
589  * @param[in]   env                   JNI interface pointer.
590  * @param[in]   bluetoothDevice       bluetooth device object.
591  * @param[in]   autoconnect           connect as soon as the device becomes avaiable(true).
592  * @return  gatt profile object
593  */
594 jobject CALEClientGattConnect(JNIEnv *env, jobject bluetoothDevice, jboolean autoconnect);
595
596 /**
597  * connect to GATT Server hosted by this device directly.
598  * @param[in]   env                   JNI interface pointer.
599  * @param[in]   bluetoothDevice       bluetooth device object.
600  * @param[in]   autoconnect           connect as soon as the device becomes avaiable(true).
601  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
602  */
603 CAResult_t CALEClientDirectConnect(JNIEnv *env, jobject bluetoothDevice, jboolean autoconnect);
604
605 #ifdef __cplusplus
606 } /* extern "C" */
607 #endif
608
609 #endif /* CA_LECLIENT_H_ */