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