Merge branch 'remote-access2'
[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  * set ble scanning flag.
248  * @param[in]   flag        scan flag.
249  */
250 void CALEClientSetScanFlag(bool flag);
251
252 /**
253  * stop scan (implement).
254  * @param[in]   env                   JNI interface pointer.
255  * @param[in]   callback              callback to receive device object by scanning.
256  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
257  */
258 CAResult_t CALEClientStopScanImpl(JNIEnv *env, jobject callback);
259
260 /**
261  * connect to gatt server hosted.
262  * @param[in]   env                   JNI interface pointer.
263  * @param[in]   bluetoothDevice       bluetooth Device object.
264  * @param[in]   autoconnect           whether to directly connect to the remote device(false) or
265  *                                     to automatically connect as soon as the remote device
266  *                                     becomes available.
267  * @param[in]   callback              callback for connection state change.
268  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
269  */
270 CAResult_t CALEClientConnect(JNIEnv *env, jobject bluetoothDevice, jboolean autoconnect,
271                              jobject callback);
272
273 /**
274  * disconnect to gatt server by a target device.
275  * @param[in]   env                   JNI interface pointer.
276  * @param[in]   bluetoothGatt         Gatt profile object.
277  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
278  */
279 CAResult_t CALEClientDisconnect(JNIEnv *env, jobject bluetoothGatt);
280
281 /**
282  * disconnect to gatt server by whole devices.
283  * @param[in]   env                   JNI interface pointer.
284  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
285  */
286 CAResult_t CALEClientDisconnectAll(JNIEnv *env);
287
288 /**
289  * start discovery server.
290  * @param[in]   env                   JNI interface pointer.
291  * @param[in]   bluetoothGatt         Gatt profile object.
292  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
293  */
294 CAResult_t CALEClientDiscoverServices(JNIEnv *env, jobject bluetoothGatt);
295
296 /**
297  * create GattCharacteristic and call CALEClientWriteCharacteristicImpl
298  * for request to write gatt characteristic.
299  * @param[in]   env                   JNI interface pointer.
300  * @param[in]   gatt                  Gatt profile object.
301  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
302  */
303 CAResult_t CALEClientWriteCharacteristic(JNIEnv *env, jobject gatt);
304
305 /**
306  * request to write gatt characteristic.
307  * @param[in]   env                   JNI interface pointer.
308  * @param[in]   bluetoothGatt         Gatt profile object.
309  * @param[in]   gattCharacteristic    characteristic object that contain data to send.
310  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
311  */
312 CAResult_t CALEClientWriteCharacteristicImpl(JNIEnv *env, jobject bluetoothGatt,
313                                              jobject gattCharacteristic);
314
315 /**
316  * request to read gatt characteristic.
317  * @param[in]   env                   JNI interface pointer.
318  * @param[in]   bluetoothGatt         Gatt profile object.
319  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
320  */
321 CAResult_t CALEClientReadCharacteristic(JNIEnv *env, jobject bluetoothGatt);
322
323 /**
324  * enable notification for a target device.
325  * @param[in]   env                   JNI interface pointer.
326  * @param[in]   bluetoothGatt         Gatt profile object.
327  * @param[in]   characteristic        Characteristic object.
328  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
329  */
330 CAResult_t CALEClientSetCharacteristicNotification(JNIEnv *env, jobject bluetoothGatt,
331                                                   jobject characteristic);
332
333 /**
334  * create gatt characteristic object.
335  * @param[in]   env                   JNI interface pointer.
336  * @param[in]   bluetoothGatt         Gatt profile object.
337  * @param[in]   data                  for make Characteristic with data.
338  * @return  Gatt Characteristic object.
339  */
340 jobject CALEClientCreateGattCharacteristic(JNIEnv *env, jobject bluetoothGatt, jbyteArray data);
341
342 /**
343  * get gatt service.
344  * @param[in]   env                   JNI interface pointer.
345  * @param[in]   bluetoothGatt         Gatt profile object.
346  * @param[in]   characterUUID         for make BluetoothGattCharacteristic object.
347  * @return  Gatt Service.
348  */
349 jobject CALEClientGetGattService(JNIEnv *env, jobject bluetoothGatt, jstring characterUUID);
350
351 /**
352  * get value from characteristic.
353  * @param[in]   env                   JNI interface pointer.
354  * @param[in]   characteristic        Characteristic object.
355  * @return  value in characteristic.
356  */
357 jbyteArray CALEClientGetValueFromCharacteristic(JNIEnv *env, jobject characteristic);
358
359 /**
360  * create UUID List.
361  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
362  */
363 CAResult_t CALEClientCreateUUIDList();
364
365 /**
366  * set UUID to descriptor.
367  * @param[in]   env                   JNI interface pointer.
368  * @param[in]   bluetoothGatt         Gatt profile object.
369  * @param[in]   characteristic        Characteristic object.
370  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
371  */
372 CAResult_t CALEClientSetUUIDToDescriptor(JNIEnv *env, jobject bluetoothGatt,
373                                          jobject characteristic);
374
375 /**
376  * add device object to scan device list.
377  * @param[in]   env                   JNI interface pointer.
378  * @param[in]   device                bluetooth device object.
379  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
380  */
381 CAResult_t CALEClientAddScanDeviceToList(JNIEnv *env, jobject device);
382
383 /**
384  * check whether the device exist in list or not.
385  * @param[in]   env                   JNI interface pointer.
386  * @param[in]   remoteAddress         remote address.
387  * @return  true or false.
388  */
389 bool CALEClientIsDeviceInScanDeviceList(JNIEnv *env, const char *remoteAddress);
390
391 /**
392  * remove all devices in scan device list.
393  * @param[in]   env                   JNI interface pointer.
394  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
395  */
396 CAResult_t CALEClientRemoveAllScanDevices(JNIEnv *env);
397
398 /**
399  * remove target device in scan device list.
400  * @param[in]   env                   JNI interface pointer.
401  * @param[in]   remoteAddress         remote address.
402  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
403  */
404 CAResult_t CALEClientRemoveDeviceInScanDeviceList(JNIEnv *env, jstring remoteAddress);
405
406 /**
407  * add gatt object to gatt object list.
408  * @param[in]   env                   JNI interface pointer.
409  * @param[in]   gatt                  Gatt profile object.
410  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
411  */
412 CAResult_t CALEClientAddGattobjToList(JNIEnv *env, jobject gatt);
413
414 /**
415  * check whether the gatt object exist in list or not.
416  * @param[in]   env                   JNI interface pointer.
417  * @param[in]   remoteAddress         remote address.
418  * @return  true or false.
419  */
420 bool CALEClientIsGattObjInList(JNIEnv *env, const char *remoteAddress);
421
422 /**
423  * get the gatt object.
424  * @param[in]   env                   JNI interface pointer.
425  * @param[in]   remoteAddress         remote address.
426  * @return  gatt object.
427  */
428 jobject CALEClientGetGattObjInList(JNIEnv *env, const char* remoteAddress);
429
430 /**
431  * remove all gatt objects in gatt object list.
432  * @param[in]   env                   JNI interface pointer.
433  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
434  */
435 CAResult_t CALEClientRemoveAllGattObjs(JNIEnv *env);
436
437 /**
438  * remove target device in gatt object list.
439  * @param[in]   env                   JNI interface pointer.
440  * @param[in]   gatt                  Gatt profile object.
441  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
442  */
443 CAResult_t CALEClientRemoveGattObj(JNIEnv *env, jobject gatt);
444
445 /**
446  * remove gatt object of target device for address in gatt object list.
447  * @param[in]   env                   JNI interface pointer.
448  * @param[in]   gatt                  Gatt profile object.
449  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
450  */
451 CAResult_t CALEClientRemoveGattObjForAddr(JNIEnv *env, jstring addr);
452
453 /**
454  * update new state information.
455  * @param[in]   address               remote address.
456  * @param[in]   connectedState        connection state.
457  * @param[in]   notificationState     whether characteristic notification already set or not.
458  * @param[in]   sendState             whether sending was success or not.
459  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
460  */
461 CAResult_t CALEClientUpdateDeviceState(const char* address, uint32_t connectedState,
462                                        uint16_t notificationState, uint16_t sendState);
463
464 /**
465  * add new state to state list.
466  * @param[in]   state                 new state.
467  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
468  */
469 CAResult_t CALEClientAddDeviceStateToList(CALEState_t* state);
470
471 /**
472  * check whether the remote address is existed or not.
473  * @param[in]   address               remote address.
474  * @return  true or false.
475  */
476 bool CALEClientIsDeviceInList(const char *remoteAddress);
477
478 /**
479  * remove all device states.
480  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
481  */
482 CAResult_t CALEClientRemoveAllDeviceState();
483
484 /**
485  * remove the device state for a remote device.
486  * @param[in]   remoteAddress         remote address.
487  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
488  */
489 CAResult_t CALEClientRemoveDeviceState(const char* remoteAddress);
490
491 /**
492  * get state information for a remote device.
493  * @param[in]   remoteAddress         remote address.
494  * @return  CALEState_t.
495  */
496 CALEState_t* CALEClientGetStateInfo(const char* remoteAddress);
497
498 /**
499  * check whether the remote address is connected or not.
500  * @param[in]   remoteAddress         remote address.
501  * @return  true or false.
502  */
503 bool CALEClientIsConnectedDevice(const char* remoteAddress);
504
505 /**
506  * check whether the remote address set CharacteristicNotification or not.
507  * @param[in]   remoteAddress         remote address.
508  * @return  true or false.
509  */
510 bool CALEClientIsSetCharacteristic(const char* remoteAddress);
511
512 /**
513  * create scan device list.
514  */
515 void CALEClientCreateDeviceList();
516
517 /**
518  * update the counter which data is sent to remote device.
519  * @param[in]   env                   JNI interface pointer.
520  */
521 void CALEClientUpdateSendCnt(JNIEnv *env);
522
523 /**
524  * initialize mutex.
525  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
526  */
527 CAResult_t CALEClientInitGattMutexVaraibles();
528
529 /**
530  * terminate mutex.
531  */
532 void CALEClientTerminateGattMutexVariables();
533
534 /**
535  * set send finish flag.
536  * @param[in]   flag        finish flag.
537  */
538 void CALEClientSetSendFinishFlag(bool flag);
539
540 #ifdef __cplusplus
541 } /* extern "C" */
542 #endif
543
544 #endif /* CA_LECLIENT_H_ */