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