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