1 /* ****************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 ******************************************************************/
23 * This file contains the APIs for BT LE communications.
25 #ifndef CA_LECLIENT_H_
26 #define CA_LECLIENT_H_
29 #include "cathreadpool.h"
30 #include "uarraylist.h"
38 static const uint16_t GATT_ERROR = 133;
40 static const uint16_t STATE_CHARACTER_SET = 2;
41 static const uint16_t STATE_CHARACTER_UNSET = 1;
42 static const uint16_t STATE_CHARACTER_NO_CHANGE = 0;
44 static const uint16_t STATE_SEND_NONE = 0;
45 static const uint16_t STATE_SEND_SUCCESS = 1;
46 static const uint16_t STATE_SEND_FAILED = 2;
48 typedef struct le_state_info
50 char address[CA_MACADDR_SIZE];
52 uint16_t notificationState;
54 jboolean autoConnectFlag;
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)
63 typedef void (*CAPacketReceiveCallback)(const char *address, const uint8_t *data);
66 * initialize JNI object.
68 void CALEClientJniInit();
71 * set context of application.
73 void CALEClientJNISetContext();
76 * create interface object and initialize the object.
77 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
79 CAResult_t CALEClientCreateJniInterfaceObject();
82 * initialize client for BLE.
83 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
85 CAResult_t CALEClientInitialize();
88 * terminate client for BLE.
90 void CALEClientTerminate();
93 * destroy interface object and terminate the interface.
94 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
96 CAResult_t CALEClientDestroyJniInterface();
99 * for destroy sending routine.
100 * @param[in] env JNI interface pointer.
101 * @param[in] gatt Gatt profile object.
103 void CALEClientSendFinish(JNIEnv *env, jobject gatt);
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).
112 CAResult_t CALEClientSendUnicastMessage(const char *address, const uint8_t *data,
113 const uint32_t dataLen);
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).
121 CAResult_t CALEClientSendMulticastMessage(const uint8_t *data, const uint32_t dataLen);
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).
128 CAResult_t CALEClientStartUnicastServer(const char *address);
131 * start multicast server (start discovery).
132 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
134 CAResult_t CALEClientStartMulticastServer();
137 * stop unicast server.
139 void CALEClientStopUnicastServer();
142 * stop multicast server (stop discovery).
144 void CALEClientStopMulticastServer();
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.
151 void CALEClientSetCallback(CAPacketReceiveCallback callback);
154 * waiting to get scanned device from BT Platform.
155 * if there is no scanned device in the list.
156 * @param[in] env JNI interface pointer.
157 * @param[in] address LE address.
158 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
160 CAResult_t CALEClientIsThereScannedDevices(JNIEnv *env, const char* address);
163 * send data for unicast (implement).
164 * @param[in] address remote address.
165 * @param[in] data data for transmission.
166 * @param[in] dataLen data length.
167 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
169 CAResult_t CALEClientSendUnicastMessageImpl(const char *address, const uint8_t *data,
170 const uint32_t dataLen);
173 * send data for multicast (implement).
174 * @param[in] env JNI interface pointer.
175 * @param[in] data data for transmission.
176 * @param[in] dataLen data length.
177 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
179 CAResult_t CALEClientSendMulticastMessageImpl(JNIEnv *env, const uint8_t *data,
180 const uint32_t dataLen);
183 * check whether it is connected or not with remote address.
184 * @param[in] address remote address.
185 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
187 CAResult_t CALECheckSendState(const char* address);
190 * send data to remote device.
191 * if it isn't connected yet. connect LE before try to send data.
192 * @param[in] env JNI interface pointer.
193 * @param[in] device bluetooth device object.
194 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
196 CAResult_t CALEClientSendData(JNIEnv *env, jobject device);
199 * get address from bluetooth gatt object.
200 * @param[in] env JNI interface pointer.
201 * @param[in] gatt Gatt profile object.
202 * @return bluetooth address.
204 jstring CALEClientGetAddressFromGattObj(JNIEnv *env, jobject gatt);
207 * get remote address from bluetooth socket object.
208 * @param[in] env JNI interface pointer.
209 * @param[in] bluetoothSocketObj bluetooth socket.
210 * @return bluetooth address.
212 jstring CALEClientGetRemoteAddress(JNIEnv *env, jobject bluetoothSocketObj);
216 * @param[in] env JNI interface pointer.
217 * @param[in] bluetoothGatt gatt profile object.
218 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
220 CAResult_t CALEClientGattClose(JNIEnv *env, jobject bluetoothGatt);
223 * start to scan whole bluetooth devices (interface).
224 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
226 CAResult_t CALEClientStartScan();
229 * start to scan whole bluetooth devices (implement).
230 * @param[in] env JNI interface pointer.
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).
234 CAResult_t CALEClientStartScanImpl(JNIEnv *env, jobject callback);
237 * start to scan target bluetooth devices for service uuid (implement).
238 * @param[in] env JNI interface pointer.
239 * @param[in] uuids target UUID.
240 * @param[in] callback callback to receive device object by scanning.
241 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
243 CAResult_t CALEClientStartScanWithUUIDImpl(JNIEnv *env, jobjectArray uuids,
248 * @param[in] env JNI interface pointer.
249 * @param[in] uuid uuid.
250 * @return uuid object.
252 jobject CALEClientGetUUIDObject(JNIEnv *env, const char *uuid);
255 * stop scan (interface).
256 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
258 CAResult_t CALEClientStopScan();
261 * set ble scanning flag.
262 * @param[in] flag scan flag.
264 void CALEClientSetScanFlag(bool flag);
267 * stop scan (implement).
268 * @param[in] env JNI interface pointer.
269 * @param[in] callback callback to receive device object by scanning.
270 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
272 CAResult_t CALEClientStopScanImpl(JNIEnv *env, jobject callback);
275 * set auto connect flag for connectGatt API.
276 * @param[in] env JNI interface pointer.
277 * @param[in] jni_address remote address.
278 * @param[in] flag auto connect flag.
280 CAResult_t CALEClientSetAutoConnectFlag(JNIEnv *env, jstring jni_address, jboolean flag);
283 * get auto connect flag.
284 * @param[in] env JNI interface pointer.
285 * @param[in] jni_address remote address.
286 * @return current auto connect flag;
288 jboolean CALEClientGetAutoConnectFlag(JNIEnv *env, jstring jni_address);
291 * connect to gatt server hosted.
292 * @param[in] env JNI interface pointer.
293 * @param[in] bluetoothDevice bluetooth Device object.
294 * @param[in] autoconnect whether to directly connect to the remote device(false) or
295 * to automatically connect as soon as the remote device
297 * @return gatt object
299 jobject CALEClientConnect(JNIEnv *env, jobject bluetoothDevice, jboolean autoconnect);
302 * disconnect to gatt server by a target device.
303 * @param[in] env JNI interface pointer.
304 * @param[in] bluetoothGatt Gatt profile object.
305 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
307 CAResult_t CALEClientDisconnect(JNIEnv *env, jobject bluetoothGatt);
310 * disconnect to gatt server by whole devices.
311 * @param[in] env JNI interface pointer.
312 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
314 CAResult_t CALEClientDisconnectAll(JNIEnv *env);
317 * disconnect to gatt server by selected address.
318 * @param[in] env JNI interface pointer.
319 * @param[in] remoteAddress remote address.
320 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
322 CAResult_t CALEClientDisconnectforAddress(JNIEnv *env, jstring remoteAddress);
325 * start discovery server.
326 * @param[in] env JNI interface pointer.
327 * @param[in] bluetoothGatt Gatt profile object.
328 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
330 CAResult_t CALEClientDiscoverServices(JNIEnv *env, jobject bluetoothGatt);
333 * call CALESetValueAndWriteCharacteristic when connection is successful.
334 * @param[in] env JNI interface pointer.
335 * @param[in] gatt Gatt profile object.
336 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
338 CAResult_t CALEClientWriteCharacteristic(JNIEnv *env, jobject gatt);
341 * create GattCharacteristic and call CALEClientWriteCharacteristicImpl
342 * for request to write gatt characteristic.
343 * @param[in] env JNI interface pointer.
344 * @param[in] gatt Gatt profile object.
345 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
347 CAResult_t CALESetValueAndWriteCharacteristic(JNIEnv *env, jobject gatt);
350 * request to write gatt characteristic.
351 * @param[in] env JNI interface pointer.
352 * @param[in] bluetoothGatt Gatt profile object.
353 * @param[in] gattCharacteristic characteristic object that contain data to send.
354 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
356 CAResult_t CALEClientWriteCharacteristicImpl(JNIEnv *env, jobject bluetoothGatt,
357 jobject gattCharacteristic);
360 * request to read gatt characteristic.
361 * @param[in] env JNI interface pointer.
362 * @param[in] bluetoothGatt Gatt profile object.
363 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
365 CAResult_t CALEClientReadCharacteristic(JNIEnv *env, jobject bluetoothGatt);
368 * enable notification for a target device.
369 * @param[in] env JNI interface pointer.
370 * @param[in] bluetoothGatt Gatt profile object.
371 * @param[in] characteristic Characteristic object.
372 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
374 CAResult_t CALEClientSetCharacteristicNotification(JNIEnv *env, jobject bluetoothGatt,
375 jobject characteristic);
378 * create gatt characteristic object.
379 * @param[in] env JNI interface pointer.
380 * @param[in] bluetoothGatt Gatt profile object.
381 * @param[in] data for make Characteristic with data.
382 * @return Gatt Characteristic object.
384 jobject CALEClientCreateGattCharacteristic(JNIEnv *env, jobject bluetoothGatt, jbyteArray data);
388 * @param[in] env JNI interface pointer.
389 * @param[in] bluetoothGatt Gatt profile object.
390 * @param[in] characterUUID for make BluetoothGattCharacteristic object.
391 * @return Gatt Service.
393 jobject CALEClientGetGattService(JNIEnv *env, jobject bluetoothGatt, jstring characterUUID);
396 * get value from characteristic.
397 * @param[in] env JNI interface pointer.
398 * @param[in] characteristic Characteristic object.
399 * @return value in characteristic.
401 jbyteArray CALEClientGetValueFromCharacteristic(JNIEnv *env, jobject characteristic);
405 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
407 CAResult_t CALEClientCreateUUIDList();
410 * set UUID to descriptor.
411 * @param[in] env JNI interface pointer.
412 * @param[in] bluetoothGatt Gatt profile object.
413 * @param[in] characteristic Characteristic object.
414 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
416 CAResult_t CALEClientSetUUIDToDescriptor(JNIEnv *env, jobject bluetoothGatt,
417 jobject characteristic);
420 * add device object to scan device list.
421 * @param[in] env JNI interface pointer.
422 * @param[in] device bluetooth device object.
423 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
425 CAResult_t CALEClientAddScanDeviceToList(JNIEnv *env, jobject device);
428 * check whether the device exist in list or not.
429 * @param[in] env JNI interface pointer.
430 * @param[in] remoteAddress remote address.
431 * @return true or false.
433 bool CALEClientIsDeviceInScanDeviceList(JNIEnv *env, const char *remoteAddress);
436 * remove all devices in scan device list.
437 * @param[in] env JNI interface pointer.
438 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
440 CAResult_t CALEClientRemoveAllScanDevices(JNIEnv *env);
443 * remove target device in scan device list.
444 * @param[in] env JNI interface pointer.
445 * @param[in] remoteAddress remote address.
446 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
448 CAResult_t CALEClientRemoveDeviceInScanDeviceList(JNIEnv *env, jstring remoteAddress);
451 * add gatt object to 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).
456 CAResult_t CALEClientAddGattobjToList(JNIEnv *env, jobject gatt);
459 * check whether the gatt object exist in list or not.
460 * @param[in] env JNI interface pointer.
461 * @param[in] remoteAddress remote address.
462 * @return true or false.
464 bool CALEClientIsGattObjInList(JNIEnv *env, const char *remoteAddress);
467 * get the gatt object.
468 * @param[in] env JNI interface pointer.
469 * @param[in] remoteAddress remote address.
470 * @return gatt object.
472 jobject CALEClientGetGattObjInList(JNIEnv *env, const char* remoteAddress);
475 * remove all gatt objects in gatt object list.
476 * @param[in] env JNI interface pointer.
477 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
479 CAResult_t CALEClientRemoveAllGattObjs(JNIEnv *env);
482 * remove target device in gatt object list.
483 * @param[in] env JNI interface pointer.
484 * @param[in] gatt Gatt profile object.
485 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
487 CAResult_t CALEClientRemoveGattObj(JNIEnv *env, jobject gatt);
490 * remove gatt object of target device for address in gatt object list.
491 * @param[in] env JNI interface pointer.
492 * @param[in] gatt Gatt profile object.
493 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
495 CAResult_t CALEClientRemoveGattObjForAddr(JNIEnv *env, jstring addr);
498 * get ble address from Bluetooth device.
499 * @param[in] env JNI interface pointer.
500 * @param[in] bluetoothDevice Bluetooth device.
501 * @return ble address.
503 jstring CALEClientGetLEAddressFromBTDevice(JNIEnv *env, jobject bluetoothDevice);
506 * update new state information.
507 * @param[in] address remote address.
508 * @param[in] connectedState connection state.
509 * @param[in] notificationState whether characteristic notification already set or not.
510 * @param[in] sendState whether sending was success or not.
511 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
513 CAResult_t CALEClientUpdateDeviceState(const char* address, uint32_t connectedState,
514 uint16_t notificationState, uint16_t sendState);
517 * add new state to state list.
518 * @param[in] state new state.
519 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
521 CAResult_t CALEClientAddDeviceStateToList(CALEState_t* state);
524 * check whether the remote address is existed or not.
525 * @param[in] address remote address.
526 * @return true or false.
528 bool CALEClientIsDeviceInList(const char *remoteAddress);
531 * remove all device states.
532 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
534 CAResult_t CALEClientRemoveAllDeviceState();
537 * Reset values of device state for all of devices.
538 * this method has to be invoked when BT adapter is disabled.
539 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
541 CAResult_t CALEClientResetDeviceStateForAll();
544 * remove the device state for a remote device.
545 * @param[in] remoteAddress remote address.
546 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
548 CAResult_t CALEClientRemoveDeviceState(const char* remoteAddress);
551 * get state information for a remote device.
552 * @param[in] remoteAddress remote address.
553 * @return CALEState_t.
555 CALEState_t* CALEClientGetStateInfo(const char* remoteAddress);
558 * check whether the remote address is connected or not.
559 * @param[in] remoteAddress remote address.
560 * @return true or false.
562 bool CALEClientIsConnectedDevice(const char* remoteAddress);
565 * check whether the remote address set CharacteristicNotification or not.
566 * @param[in] remoteAddress remote address.
567 * @return true or false.
569 bool CALEClientIsSetCharacteristic(const char* remoteAddress);
572 * create scan device list.
574 void CALEClientCreateDeviceList();
577 * update the counter which data is sent to remote device.
578 * @param[in] env JNI interface pointer.
580 void CALEClientUpdateSendCnt(JNIEnv *env);
584 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
586 CAResult_t CALEClientInitGattMutexVaraibles();
591 void CALEClientTerminateGattMutexVariables();
594 * set send finish flag.
595 * @param[in] flag finish flag.
597 void CALEClientSetSendFinishFlag(bool flag);
600 * close the connection of the profile proxy to the Service.
601 * @param[in] env JNI interface pointer.
602 * @param[in] gatt gatt profile object.
603 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
605 CAResult_t CALEClientCloseProfileProxy(JNIEnv *env, jobject gatt);
608 * connect to GATT Server hosted by this device.
609 * @param[in] env JNI interface pointer.
610 * @param[in] bluetoothDevice bluetooth device object.
611 * @param[in] autoconnect connect as soon as the device becomes avaiable(true).
612 * @return gatt profile object
614 jobject CALEClientGattConnect(JNIEnv *env, jobject bluetoothDevice, jboolean autoconnect);
620 #endif /* CA_LECLIENT_H_ */