[IOT-946] To support Resource Discovery on unicast for Android 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  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
84  */
85 CAResult_t CALEClientInitialize();
86
87 /**
88  * terminate client for BLE.
89  */
90 void CALEClientTerminate();
91
92 /**
93  * destroy interface object and terminate the interface.
94  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
95  */
96 CAResult_t CALEClientDestroyJniInterface();
97
98 /**
99  * for destroy sending routine.
100  * @param[in]   env                   JNI interface pointer.
101  * @param[in]   gatt                  Gatt profile object.
102  */
103 void CALEClientSendFinish(JNIEnv *env, jobject gatt);
104
105 /**
106  * send data for unicast (interface).
107  * @param[in]   address               remote address.
108  * @param[in]   data                  data for transmission.
109  * @param[in]   dataLen               data length.
110  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
111  */
112 CAResult_t CALEClientSendUnicastMessage(const char *address, const uint8_t *data,
113                                         const uint32_t dataLen);
114
115 /**
116  * send data for multicast (interface).
117  * @param[in]   data                  data for transmission.
118  * @param[in]   dataLen               data length.
119  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
120  */
121 CAResult_t CALEClientSendMulticastMessage(const uint8_t *data, const uint32_t dataLen);
122
123 /**
124  * start unicast server.
125  * @param[in]   address               remote address.
126  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
127  */
128 CAResult_t CALEClientStartUnicastServer(const char *address);
129
130 /**
131  * start multicast server (start discovery).
132  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
133  */
134 CAResult_t CALEClientStartMulticastServer();
135
136 /**
137  * stop unicast server.
138  */
139 void CALEClientStopUnicastServer();
140
141 /**
142  * stop multicast server (stop discovery).
143  */
144 void CALEClientStopMulticastServer();
145
146 /**
147  * set this callback for receiving data packets from peer devices.
148  * @param[in]   callback              callback to be notified on reception of
149  *                                    unicast/multicast data packets.
150  */
151 void CALEClientSetCallback(CAPacketReceiveCallback callback);
152
153 /**
154  * waiting to get scanned device from BT Platform.
155  * if there is no scanned device in the list.
156  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
157  */
158 CAResult_t CALEClientIsThereScannedDevices();
159
160 /**
161  * send data for unicast (implement).
162  * @param[in]   address               remote address.
163  * @param[in]   data                  data for transmission.
164  * @param[in]   dataLen               data length.
165  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
166  */
167 CAResult_t CALEClientSendUnicastMessageImpl(const char *address, const uint8_t *data,
168                                             const uint32_t dataLen);
169
170 /**
171  * send data for multicast (implement).
172  * @param[in]   env                   JNI interface pointer.
173  * @param[in]   data                  data for transmission.
174  * @param[in]   dataLen               data length.
175  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
176  */
177 CAResult_t CALEClientSendMulticastMessageImpl(JNIEnv *env, const uint8_t *data,
178                                               const uint32_t dataLen);
179
180 /**
181  * check whether it is connected or not with remote address.
182  * @param[in]   address               remote address.
183  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
184  */
185 CAResult_t CALECheckSendState(const char* address);
186
187 /**
188  * send data to remote device.
189  * if it isn't connected yet. connect LE before try to send data.
190  * @param[in]   env                   JNI interface pointer.
191  * @param[in]   device                bluetooth device object.
192  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
193  */
194 CAResult_t CALEClientSendData(JNIEnv *env, jobject device);
195
196 /**
197  * get address from bluetooth gatt object.
198  * @param[in]   env                   JNI interface pointer.
199  * @param[in]   gatt                  Gatt profile object.
200  * @return  bluetooth address.
201  */
202 jstring CALEClientGetAddressFromGattObj(JNIEnv *env, jobject gatt);
203
204 /**
205  * get remote address from bluetooth socket object.
206  * @param[in]   env                   JNI interface pointer.
207  * @param[in]   bluetoothSocketObj    bluetooth socket.
208  * @return  bluetooth address.
209  */
210 jstring CALEClientGetRemoteAddress(JNIEnv *env, jobject bluetoothSocketObj);
211
212 /**
213  * close gatt.
214  * @param[in]   env                   JNI interface pointer.
215  * @param[in]   bluetoothGatt         gatt profile object.
216  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
217  */
218 CAResult_t CALEClientGattClose(JNIEnv *env, jobject bluetoothGatt);
219
220 /**
221  * start to scan whole bluetooth devices (interface).
222  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
223  */
224 CAResult_t CALEClientStartScan();
225
226 /**
227  * start to scan whole bluetooth devices (implement).
228  * @param[in]   env                   JNI interface pointer.
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 CALEClientStartScanImpl(JNIEnv *env, jobject callback);
233
234 /**
235  * start to scan target bluetooth devices for service uuid (implement).
236  * @param[in]   env                   JNI interface pointer.
237  * @param[in]   uuids                 target UUID.
238  * @param[in]   callback              callback to receive device object by scanning.
239  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
240  */
241 CAResult_t CALEClientStartScanWithUUIDImpl(JNIEnv *env, jobjectArray uuids,
242                                            jobject callback);
243
244 /**
245  * get uuid object.
246  * @param[in]   env                   JNI interface pointer.
247  * @param[in]   uuid                  uuid.
248  * @return  uuid object.
249  */
250 jobject CALEClientGetUUIDObject(JNIEnv *env, const char *uuid);
251
252 /**
253  * stop scan (interface).
254  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
255  */
256 CAResult_t CALEClientStopScan();
257
258 /**
259  * set ble scanning flag.
260  * @param[in]   flag        scan flag.
261  */
262 void CALEClientSetScanFlag(bool flag);
263
264 /**
265  * stop scan (implement).
266  * @param[in]   env                   JNI interface pointer.
267  * @param[in]   callback              callback to receive device object by scanning.
268  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
269  */
270 CAResult_t CALEClientStopScanImpl(JNIEnv *env, jobject callback);
271
272 /**
273  * connect to gatt server hosted.
274  * @param[in]   env                   JNI interface pointer.
275  * @param[in]   bluetoothDevice       bluetooth Device object.
276  * @param[in]   autoconnect           whether to directly connect to the remote device(false) or
277  *                                     to automatically connect as soon as the remote device
278  *                                     becomes available.
279  * @param[in]   callback              callback for connection state change.
280  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
281  */
282 CAResult_t CALEClientConnect(JNIEnv *env, jobject bluetoothDevice, jboolean autoconnect,
283                              jobject callback);
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  * start discovery server.
302  * @param[in]   env                   JNI interface pointer.
303  * @param[in]   bluetoothGatt         Gatt profile object.
304  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
305  */
306 CAResult_t CALEClientDiscoverServices(JNIEnv *env, jobject bluetoothGatt);
307
308 /**
309  * call CALESetValueAndWriteCharacteristic when connection is successful.
310  * @param[in]   env                   JNI interface pointer.
311  * @param[in]   gatt                  Gatt profile object.
312  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
313  */
314 CAResult_t CALEClientWriteCharacteristic(JNIEnv *env, jobject gatt);
315
316 /**
317  * create GattCharacteristic and call CALEClientWriteCharacteristicImpl
318  * for request to write gatt characteristic.
319  * @param[in]   env                   JNI interface pointer.
320  * @param[in]   gatt                  Gatt profile object.
321  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
322  */
323 CAResult_t CALESetValueAndWriteCharacteristic(JNIEnv *env, jobject gatt);
324
325 /**
326  * request to write gatt characteristic.
327  * @param[in]   env                   JNI interface pointer.
328  * @param[in]   bluetoothGatt         Gatt profile object.
329  * @param[in]   gattCharacteristic    characteristic object that contain data to send.
330  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
331  */
332 CAResult_t CALEClientWriteCharacteristicImpl(JNIEnv *env, jobject bluetoothGatt,
333                                              jobject gattCharacteristic);
334
335 /**
336  * request to read gatt characteristic.
337  * @param[in]   env                   JNI interface pointer.
338  * @param[in]   bluetoothGatt         Gatt profile object.
339  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
340  */
341 CAResult_t CALEClientReadCharacteristic(JNIEnv *env, jobject bluetoothGatt);
342
343 /**
344  * enable notification for a target device.
345  * @param[in]   env                   JNI interface pointer.
346  * @param[in]   bluetoothGatt         Gatt profile object.
347  * @param[in]   characteristic        Characteristic object.
348  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
349  */
350 CAResult_t CALEClientSetCharacteristicNotification(JNIEnv *env, jobject bluetoothGatt,
351                                                   jobject characteristic);
352
353 /**
354  * create gatt characteristic object.
355  * @param[in]   env                   JNI interface pointer.
356  * @param[in]   bluetoothGatt         Gatt profile object.
357  * @param[in]   data                  for make Characteristic with data.
358  * @return  Gatt Characteristic object.
359  */
360 jobject CALEClientCreateGattCharacteristic(JNIEnv *env, jobject bluetoothGatt, jbyteArray data);
361
362 /**
363  * get gatt service.
364  * @param[in]   env                   JNI interface pointer.
365  * @param[in]   bluetoothGatt         Gatt profile object.
366  * @param[in]   characterUUID         for make BluetoothGattCharacteristic object.
367  * @return  Gatt Service.
368  */
369 jobject CALEClientGetGattService(JNIEnv *env, jobject bluetoothGatt, jstring characterUUID);
370
371 /**
372  * get value from characteristic.
373  * @param[in]   env                   JNI interface pointer.
374  * @param[in]   characteristic        Characteristic object.
375  * @return  value in characteristic.
376  */
377 jbyteArray CALEClientGetValueFromCharacteristic(JNIEnv *env, jobject characteristic);
378
379 /**
380  * create UUID List.
381  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
382  */
383 CAResult_t CALEClientCreateUUIDList();
384
385 /**
386  * set UUID to descriptor.
387  * @param[in]   env                   JNI interface pointer.
388  * @param[in]   bluetoothGatt         Gatt profile object.
389  * @param[in]   characteristic        Characteristic object.
390  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
391  */
392 CAResult_t CALEClientSetUUIDToDescriptor(JNIEnv *env, jobject bluetoothGatt,
393                                          jobject characteristic);
394
395 /**
396  * add device object to scan device list.
397  * @param[in]   env                   JNI interface pointer.
398  * @param[in]   device                bluetooth device object.
399  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
400  */
401 CAResult_t CALEClientAddScanDeviceToList(JNIEnv *env, jobject device);
402
403 /**
404  * check whether the device exist in list or not.
405  * @param[in]   env                   JNI interface pointer.
406  * @param[in]   remoteAddress         remote address.
407  * @return  true or false.
408  */
409 bool CALEClientIsDeviceInScanDeviceList(JNIEnv *env, const char *remoteAddress);
410
411 /**
412  * remove all devices in scan device list.
413  * @param[in]   env                   JNI interface pointer.
414  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
415  */
416 CAResult_t CALEClientRemoveAllScanDevices(JNIEnv *env);
417
418 /**
419  * remove target device in scan device list.
420  * @param[in]   env                   JNI interface pointer.
421  * @param[in]   remoteAddress         remote address.
422  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
423  */
424 CAResult_t CALEClientRemoveDeviceInScanDeviceList(JNIEnv *env, jstring remoteAddress);
425
426 /**
427  * add gatt object to gatt object list.
428  * @param[in]   env                   JNI interface pointer.
429  * @param[in]   gatt                  Gatt profile object.
430  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
431  */
432 CAResult_t CALEClientAddGattobjToList(JNIEnv *env, jobject gatt);
433
434 /**
435  * check whether the gatt object exist in list or not.
436  * @param[in]   env                   JNI interface pointer.
437  * @param[in]   remoteAddress         remote address.
438  * @return  true or false.
439  */
440 bool CALEClientIsGattObjInList(JNIEnv *env, const char *remoteAddress);
441
442 /**
443  * get the gatt object.
444  * @param[in]   env                   JNI interface pointer.
445  * @param[in]   remoteAddress         remote address.
446  * @return  gatt object.
447  */
448 jobject CALEClientGetGattObjInList(JNIEnv *env, const char* remoteAddress);
449
450 /**
451  * remove all gatt objects in gatt object list.
452  * @param[in]   env                   JNI interface pointer.
453  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
454  */
455 CAResult_t CALEClientRemoveAllGattObjs(JNIEnv *env);
456
457 /**
458  * remove target device in gatt object list.
459  * @param[in]   env                   JNI interface pointer.
460  * @param[in]   gatt                  Gatt profile object.
461  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
462  */
463 CAResult_t CALEClientRemoveGattObj(JNIEnv *env, jobject gatt);
464
465 /**
466  * remove gatt object of target device for address 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 CALEClientRemoveGattObjForAddr(JNIEnv *env, jstring addr);
472
473 /**
474  * update new state information.
475  * @param[in]   address               remote address.
476  * @param[in]   connectedState        connection state.
477  * @param[in]   notificationState     whether characteristic notification already set or not.
478  * @param[in]   sendState             whether sending was success or not.
479  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
480  */
481 CAResult_t CALEClientUpdateDeviceState(const char* address, uint32_t connectedState,
482                                        uint16_t notificationState, uint16_t sendState);
483
484 /**
485  * add new state to state list.
486  * @param[in]   state                 new state.
487  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
488  */
489 CAResult_t CALEClientAddDeviceStateToList(CALEState_t* state);
490
491 /**
492  * check whether the remote address is existed or not.
493  * @param[in]   address               remote address.
494  * @return  true or false.
495  */
496 bool CALEClientIsDeviceInList(const char *remoteAddress);
497
498 /**
499  * remove all device states.
500  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
501  */
502 CAResult_t CALEClientRemoveAllDeviceState();
503
504 /**
505  * remove the device state for a remote device.
506  * @param[in]   remoteAddress         remote address.
507  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
508  */
509 CAResult_t CALEClientRemoveDeviceState(const char* remoteAddress);
510
511 /**
512  * get state information for a remote device.
513  * @param[in]   remoteAddress         remote address.
514  * @return  CALEState_t.
515  */
516 CALEState_t* CALEClientGetStateInfo(const char* remoteAddress);
517
518 /**
519  * check whether the remote address is connected or not.
520  * @param[in]   remoteAddress         remote address.
521  * @return  true or false.
522  */
523 bool CALEClientIsConnectedDevice(const char* remoteAddress);
524
525 /**
526  * check whether the remote address set CharacteristicNotification or not.
527  * @param[in]   remoteAddress         remote address.
528  * @return  true or false.
529  */
530 bool CALEClientIsSetCharacteristic(const char* remoteAddress);
531
532 /**
533  * create scan device list.
534  */
535 void CALEClientCreateDeviceList();
536
537 /**
538  * update the counter which data is sent to remote device.
539  * @param[in]   env                   JNI interface pointer.
540  */
541 void CALEClientUpdateSendCnt(JNIEnv *env);
542
543 /**
544  * initialize mutex.
545  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
546  */
547 CAResult_t CALEClientInitGattMutexVaraibles();
548
549 /**
550  * terminate mutex.
551  */
552 void CALEClientTerminateGattMutexVariables();
553
554 /**
555  * set send finish flag.
556  * @param[in]   flag        finish flag.
557  */
558 void CALEClientSetSendFinishFlag(bool flag);
559
560 #ifdef __cplusplus
561 } /* extern "C" */
562 #endif
563
564 #endif /* CA_LECLIENT_H_ */