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