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