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_SEND_NONE = 1;
41 static const uint16_t STATE_SEND_SUCCESS = 2;
42 static const uint16_t STATE_SEND_FAIL = 3;
43 static const uint16_t STATE_SENDING = 4;
45 typedef struct le_state_info
47 char address[CA_MACADDR_SIZE];
48 uint16_t connectedState;
50 jboolean autoConnectFlag;
51 jboolean isDescriptorFound;
59 BLE_SCAN_ENABLE = 0, /**< BLE scan is working */
60 BLE_SCAN_DISABLE /**< BLE scan is not working */
64 * Callback to be notified on reception of any data from remote devices.
65 * @param[in] address MAC address of remote device.
66 * @param[in] data Data received from remote device.
67 * @pre Callback must be registered using CALESetCallback(CAPacketReceiveCallback callback)
69 typedef void (*CAPacketReceiveCallback)(const char *address, const uint8_t *data);
72 * initialize JNI object.
74 void CALEClientJniInit();
77 * set context of application.
79 void CALEClientJNISetContext();
82 * create interface object and initialize the object.
83 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
85 CAResult_t CALEClientCreateJniInterfaceObject();
88 * initialize client for BLE.
89 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
91 CAResult_t CALEClientInitialize();
94 * terminate client for BLE.
96 void CALEClientTerminate();
99 * destroy interface object and terminate the interface.
100 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
102 CAResult_t CALEClientDestroyJniInterface();
105 * for destroy sending routine.
106 * @param[in] env JNI interface pointer.
107 * @param[in] gatt Gatt profile object.
109 void CALEClientSendFinish(JNIEnv *env, jobject gatt);
112 * send data for unicast (interface).
113 * @param[in] address remote address.
114 * @param[in] data data for transmission.
115 * @param[in] dataLen data length.
116 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
118 CAResult_t CALEClientSendUnicastMessage(const char *address, const uint8_t *data,
119 const uint32_t dataLen);
122 * send data for multicast (interface).
123 * @param[in] data data for transmission.
124 * @param[in] dataLen data length.
125 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
127 CAResult_t CALEClientSendMulticastMessage(const uint8_t *data, const uint32_t dataLen);
130 * start unicast server.
131 * @param[in] address remote address.
132 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
134 CAResult_t CALEClientStartUnicastServer(const char *address);
137 * start multicast server (start discovery).
138 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
140 CAResult_t CALEClientStartMulticastServer();
143 * stop unicast server.
145 void CALEClientStopUnicastServer();
148 * stop multicast server (stop discovery).
150 void CALEClientStopMulticastServer();
153 * set this callback for receiving data packets from peer devices.
154 * @param[in] callback callback to be notified on reception of
155 * unicast/multicast data packets.
157 void CALEClientSetCallback(CAPacketReceiveCallback callback);
160 * waiting to get scanned device from BT Platform.
161 * if there is no scanned device in the list.
162 * @param[in] env JNI interface pointer.
163 * @param[in] address LE address.
164 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
166 CAResult_t CALEClientIsThereScannedDevices(JNIEnv *env, const char* address);
169 * send data for unicast (implement).
170 * @param[in] address remote address.
171 * @param[in] data data for transmission.
172 * @param[in] dataLen data length.
173 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
175 CAResult_t CALEClientSendUnicastMessageImpl(const char *address, const uint8_t *data,
176 const uint32_t dataLen);
179 * send data for multicast (implement).
180 * @param[in] env JNI interface pointer.
181 * @param[in] data data for transmission.
182 * @param[in] dataLen data length.
183 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
185 CAResult_t CALEClientSendMulticastMessageImpl(JNIEnv *env, const uint8_t *data,
186 const uint32_t dataLen);
189 * send data to remote device.
190 * if it isn't connected yet. connect LE before try to send data.
191 * @param[in] env JNI interface pointer.
192 * @param[in] device bluetooth device object.
193 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
195 CAResult_t CALEClientSendData(JNIEnv *env, jobject device);
198 * get address from bluetooth gatt object.
199 * @param[in] env JNI interface pointer.
200 * @param[in] gatt Gatt profile object.
201 * @return bluetooth address.
203 jstring CALEClientGetAddressFromGattObj(JNIEnv *env, jobject gatt);
206 * get remote address from bluetooth socket object.
207 * @param[in] env JNI interface pointer.
208 * @param[in] bluetoothSocketObj bluetooth socket.
209 * @return bluetooth address.
211 jstring CALEClientGetRemoteAddress(JNIEnv *env, jobject bluetoothSocketObj);
215 * @param[in] env JNI interface pointer.
216 * @param[in] bluetoothGatt gatt profile object.
217 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
219 CAResult_t CALEClientGattClose(JNIEnv *env, jobject bluetoothGatt);
222 * start to scan whole bluetooth devices (interface).
223 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
225 CAResult_t CALEClientStartScan();
228 * start to scan whole bluetooth devices (implement).
229 * @param[in] env JNI interface pointer.
230 * @param[in] callback callback to receive device object by scanning.
231 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
233 CAResult_t CALEClientStartScanImpl(JNIEnv *env, jobject callback);
236 * start to scan target bluetooth devices for service uuid (implement).
237 * @param[in] env JNI interface pointer.
238 * @param[in] uuids target UUID.
239 * @param[in] callback callback to receive device object by scanning.
240 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
242 CAResult_t CALEClientStartScanWithUUIDImpl(JNIEnv *env, jobjectArray uuids,
247 * @param[in] env JNI interface pointer.
248 * @param[in] uuid uuid.
249 * @return uuid object.
251 jobject CALEClientGetUUIDObject(JNIEnv *env, const char *uuid);
254 * stop scan (interface).
255 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
257 CAResult_t CALEClientStopScan();
260 * stop scan (implement).
261 * @param[in] env JNI interface pointer.
262 * @param[in] callback callback to receive device object by scanning.
263 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
265 CAResult_t CALEClientStopScanImpl(JNIEnv *env, jobject callback);
268 * set flag into State List.
269 * @param[in] env JNI interface pointer.
270 * @param[in] jni_address remote address.
271 * @param[in] state_idx state index.
272 * @param[in] flag auto connect flag.
274 CAResult_t CALEClientSetFlagToState(JNIEnv *env, jstring jni_address,
275 jint state_idx, jboolean flag);
278 * get flag from State List.
279 * @param[in] env JNI interface pointer.
280 * @param[in] jni_address remote address.
281 * @param[in] state_idx state index.
282 * @return current flag;
284 jboolean CALEClientGetFlagFromState(JNIEnv *env, jstring jni_address, jint state_idx);
287 * connect to gatt server hosted.
288 * @param[in] env JNI interface pointer.
289 * @param[in] bluetoothDevice bluetooth Device object.
290 * @param[in] autoconnect whether to directly connect to the remote device(false) or
291 * to automatically connect as soon as the remote device
293 * @return gatt object
295 jobject CALEClientConnect(JNIEnv *env, jobject bluetoothDevice, jboolean autoconnect);
298 * disconnect to gatt server by a target device.
299 * @param[in] env JNI interface pointer.
300 * @param[in] bluetoothGatt Gatt profile object.
301 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
303 CAResult_t CALEClientDisconnect(JNIEnv *env, jobject bluetoothGatt);
306 * disconnect to gatt server by whole devices.
307 * @param[in] env JNI interface pointer.
308 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
310 CAResult_t CALEClientDisconnectAll(JNIEnv *env);
313 * disconnect to gatt server by selected address.
314 * @param[in] env JNI interface pointer.
315 * @param[in] remoteAddress remote address.
316 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
318 CAResult_t CALEClientDisconnectforAddress(JNIEnv *env, jstring remoteAddress);
321 * start discovery server.
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).
326 CAResult_t CALEClientDiscoverServices(JNIEnv *env, jobject bluetoothGatt);
329 * call CALESetValueAndWriteCharacteristic when connection is successful.
330 * @param[in] env JNI interface pointer.
331 * @param[in] gatt Gatt profile object.
332 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
334 CAResult_t CALEClientWriteCharacteristic(JNIEnv *env, jobject gatt);
337 * create GattCharacteristic and call CALEClientWriteCharacteristicImpl
338 * for request to write gatt characteristic.
339 * @param[in] env JNI interface pointer.
340 * @param[in] gatt Gatt profile object.
341 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
343 CAResult_t CALESetValueAndWriteCharacteristic(JNIEnv *env, jobject gatt);
346 * request to write gatt characteristic.
347 * @param[in] env JNI interface pointer.
348 * @param[in] bluetoothGatt Gatt profile object.
349 * @param[in] gattCharacteristic characteristic object that contain data to send.
350 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
352 CAResult_t CALEClientWriteCharacteristicImpl(JNIEnv *env, jobject bluetoothGatt,
353 jobject gattCharacteristic);
356 * request to read gatt characteristic.
357 * @param[in] env JNI interface pointer.
358 * @param[in] bluetoothGatt Gatt profile object.
359 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
361 CAResult_t CALEClientReadCharacteristic(JNIEnv *env, jobject bluetoothGatt);
364 * enable notification for a target device.
365 * @param[in] env JNI interface pointer.
366 * @param[in] bluetoothGatt Gatt profile object.
367 * @param[in] characteristic Characteristic object.
368 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
370 CAResult_t CALEClientSetCharacteristicNotification(JNIEnv *env, jobject bluetoothGatt,
371 jobject characteristic);
374 * create gatt characteristic object.
375 * @param[in] env JNI interface pointer.
376 * @param[in] bluetoothGatt Gatt profile object.
377 * @param[in] data for make Characteristic with data.
378 * @return Gatt Characteristic object.
380 jobject CALEClientCreateGattCharacteristic(JNIEnv *env, jobject bluetoothGatt, jbyteArray data);
384 * @param[in] env JNI interface pointer.
385 * @param[in] bluetoothGatt Gatt profile object.
386 * @param[in] characterUUID for make BluetoothGattCharacteristic object.
387 * @return Gatt Service.
389 jobject CALEClientGetGattService(JNIEnv *env, jobject bluetoothGatt, jstring characterUUID);
392 * get value from characteristic.
393 * @param[in] env JNI interface pointer.
394 * @param[in] characteristic Characteristic object.
395 * @return value in characteristic.
397 jbyteArray CALEClientGetValueFromCharacteristic(JNIEnv *env, jobject characteristic);
401 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
403 CAResult_t CALEClientCreateUUIDList();
406 * set UUID to descriptor.
407 * @param[in] env JNI interface pointer.
408 * @param[in] bluetoothGatt Gatt profile object.
409 * @param[in] characteristic Characteristic object.
410 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
412 CAResult_t CALEClientSetUUIDToDescriptor(JNIEnv *env, jobject bluetoothGatt,
413 jobject characteristic);
416 * add device object to scan device list.
417 * @param[in] env JNI interface pointer.
418 * @param[in] device bluetooth device object.
419 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
421 CAResult_t CALEClientAddScanDeviceToList(JNIEnv *env, jobject device);
424 * check whether the device exist in list or not.
425 * @param[in] env JNI interface pointer.
426 * @param[in] remoteAddress remote address.
427 * @return true or false.
429 bool CALEClientIsDeviceInScanDeviceList(JNIEnv *env, const char *remoteAddress);
432 * remove all devices in scan device list.
433 * @param[in] env JNI interface pointer.
434 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
436 CAResult_t CALEClientRemoveAllScanDevices(JNIEnv *env);
439 * remove target device in scan device list.
440 * @param[in] env JNI interface pointer.
441 * @param[in] remoteAddress remote address.
442 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
444 CAResult_t CALEClientRemoveDeviceInScanDeviceList(JNIEnv *env, jstring remoteAddress);
447 * add gatt object to 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).
452 CAResult_t CALEClientAddGattobjToList(JNIEnv *env, jobject gatt);
455 * check whether the gatt object exist in list or not.
456 * @param[in] env JNI interface pointer.
457 * @param[in] remoteAddress remote address.
458 * @return true or false.
460 bool CALEClientIsGattObjInList(JNIEnv *env, const char *remoteAddress);
463 * get the gatt object.
464 * @param[in] env JNI interface pointer.
465 * @param[in] remoteAddress remote address.
466 * @return gatt object.
468 jobject CALEClientGetGattObjInList(JNIEnv *env, const char* remoteAddress);
471 * remove all gatt objects in gatt object list.
472 * @param[in] env JNI interface pointer.
473 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
475 CAResult_t CALEClientRemoveAllGattObjs(JNIEnv *env);
478 * remove target device in gatt object list.
479 * @param[in] env JNI interface pointer.
480 * @param[in] gatt Gatt profile object.
481 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
483 CAResult_t CALEClientRemoveGattObj(JNIEnv *env, jobject gatt);
486 * remove gatt object of target device for address in gatt object list.
487 * @param[in] env JNI interface pointer.
488 * @param[in] gatt Gatt profile object.
489 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
491 CAResult_t CALEClientRemoveGattObjForAddr(JNIEnv *env, jstring addr);
494 * get ble address from Bluetooth device.
495 * @param[in] env JNI interface pointer.
496 * @param[in] bluetoothDevice Bluetooth device.
497 * @return ble address.
499 jstring CALEClientGetLEAddressFromBTDevice(JNIEnv *env, jobject bluetoothDevice);
502 * update new state information.
503 * @param[in] address remote address.
504 * @param[in] state_type state type.
505 * @param[in] target_state state index to update.
506 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
508 CAResult_t CALEClientUpdateDeviceState(const char* address, uint16_t state_type,
509 uint16_t target_state);
512 * check whether the remote address is existed or not.
513 * @param[in] address remote address.
514 * @return true or false.
516 bool CALEClientIsDeviceInList(const char *remoteAddress);
519 * remove all device states.
520 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
522 CAResult_t CALEClientRemoveAllDeviceState();
525 * Reset values of device state for all of devices.
526 * this method has to be invoked when BT adapter is disabled.
527 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
529 CAResult_t CALEClientResetDeviceStateForAll();
532 * remove the device state for a remote device.
533 * @param[in] remoteAddress remote address.
534 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
536 CAResult_t CALEClientRemoveDeviceState(const char* remoteAddress);
539 * get state information for a remote device.
540 * @param[in] remoteAddress remote address.
541 * @return CALEState_t.
543 CALEState_t* CALEClientGetStateInfo(const char* remoteAddress);
546 * check whether the remote address has same state with target state.
547 * @param[in] remoteAddress remote address.
548 * @param[in] state_type state_type.
549 * @param[in] target_state state index to check.
550 * @return true or false.
552 bool CALEClientIsValidState(const char* remoteAddress, uint16_t state_type,
553 uint16_t target_state);
556 * create scan device list.
558 void CALEClientCreateDeviceList();
561 * update the counter which data is sent to remote device.
562 * @param[in] env JNI interface pointer.
564 void CALEClientUpdateSendCnt(JNIEnv *env);
568 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
570 CAResult_t CALEClientInitGattMutexVaraibles();
575 void CALEClientTerminateGattMutexVariables();
578 * set send finish flag.
579 * @param[in] flag finish flag.
581 void CALEClientSetSendFinishFlag(bool flag);
584 * close the connection of the profile proxy to the Service.
585 * @param[in] env JNI interface pointer.
586 * @param[in] gatt gatt profile object.
587 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
589 CAResult_t CALEClientCloseProfileProxy(JNIEnv *env, jobject gatt);
592 * connect to GATT Server hosted by this device.
593 * @param[in] env JNI interface pointer.
594 * @param[in] bluetoothDevice bluetooth device object.
595 * @param[in] autoconnect connect as soon as the device becomes avaiable(true).
596 * @return gatt profile object
598 jobject CALEClientGattConnect(JNIEnv *env, jobject bluetoothDevice, jboolean autoconnect);
601 * connect to GATT Server hosted by this device directly.
602 * @param[in] env JNI interface pointer.
603 * @param[in] bluetoothDevice bluetooth device object.
604 * @param[in] autoconnect connect as soon as the device becomes avaiable(true).
605 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
607 CAResult_t CALEClientDirectConnect(JNIEnv *env, jobject bluetoothDevice, jboolean autoconnect);
610 * set new interval time and working count.
611 * @param[in] intervalTime interval time(Seconds).
612 * @param[in] workingCount working count for selected interval time.
613 * @param[in] nextScanningStep set next scanning state.
615 void CALEClientSetScanInterval(int32_t intervalTime, int32_t workingCount,
616 CALEScanState_t nextScanningStep);
619 * restart scanning with new interval time and working count.
620 * @param[in] intervalTime interval time(Seconds).
621 * @param[in] workingCount working count for selected interval time.
622 * @param[in] nextScanningStep set next scanning state.
624 void CALERestartScanWithInterval(int32_t intervalTime, int32_t workingCount,
625 CALEScanState_t nextScanningStep);
628 * start LE scanning logic with interval time and working count.
629 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
631 CAResult_t CALEClientStartScanWithInterval();
634 * stop LE scanning logic with interval time and cycle.
636 void CALEClientStopScanWithInterval();
642 #endif /* CA_LECLIENT_H_ */