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;
55 * Callback to be notified on reception of any data from remote devices.
56 * @param[in] address MAC address of remote device.
57 * @param[in] data Data received from remote device.
58 * @pre Callback must be registered using CALESetCallback(CAPacketReceiveCallback callback)
60 typedef void (*CAPacketReceiveCallback)(const char *address, const uint8_t *data);
63 * initialize JNI object.
65 void CALEClientJniInit();
68 * set context of application.
70 void CALEClientJNISetContext();
73 * create interface object and initialize the object.
74 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
76 CAResult_t CALEClientCreateJniInterfaceObject();
79 * initialize client for BLE.
80 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
82 CAResult_t CALEClientInitialize();
85 * terminate client for BLE.
87 void CALEClientTerminate();
90 * destroy interface object and terminate the interface.
91 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
93 CAResult_t CALEClientDestroyJniInterface();
96 * for destroy sending routine.
97 * @param[in] env JNI interface pointer.
98 * @param[in] gatt Gatt profile object.
100 void CALEClientSendFinish(JNIEnv *env, jobject gatt);
103 * send data for unicast (interface).
104 * @param[in] address remote address.
105 * @param[in] data data for transmission.
106 * @param[in] dataLen data length.
107 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
109 CAResult_t CALEClientSendUnicastMessage(const char *address, const uint8_t *data,
110 const uint32_t dataLen);
113 * send data for multicast (interface).
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 CALEClientSendMulticastMessage(const uint8_t *data, const uint32_t dataLen);
121 * start unicast server.
122 * @param[in] address remote address.
123 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
125 CAResult_t CALEClientStartUnicastServer(const char *address);
128 * start multicast server (start discovery).
129 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
131 CAResult_t CALEClientStartMulticastServer();
134 * stop unicast server.
136 void CALEClientStopUnicastServer();
139 * stop multicast server (stop discovery).
141 void CALEClientStopMulticastServer();
144 * set this callback for receiving data packets from peer devices.
145 * @param[in] callback callback to be notified on reception of
146 * unicast/multicast data packets.
148 void CALEClientSetCallback(CAPacketReceiveCallback callback);
151 * waiting to get scanned device from BT Platform.
152 * if there is no scanned device in the list.
153 * @param[in] env JNI interface pointer.
154 * @param[in] address LE address.
155 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
157 CAResult_t CALEClientIsThereScannedDevices(JNIEnv *env, const char* address);
160 * send data for unicast (implement).
161 * @param[in] address remote address.
162 * @param[in] data data for transmission.
163 * @param[in] dataLen data length.
164 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
166 CAResult_t CALEClientSendUnicastMessageImpl(const char *address, const uint8_t *data,
167 const uint32_t dataLen);
170 * send data for multicast (implement).
171 * @param[in] env JNI interface pointer.
172 * @param[in] data data for transmission.
173 * @param[in] dataLen data length.
174 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
176 CAResult_t CALEClientSendMulticastMessageImpl(JNIEnv *env, const uint8_t *data,
177 const uint32_t dataLen);
180 * send data to remote device.
181 * if it isn't connected yet. connect LE before try to send data.
182 * @param[in] env JNI interface pointer.
183 * @param[in] device bluetooth device object.
184 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
186 CAResult_t CALEClientSendData(JNIEnv *env, jobject device);
189 * get address from bluetooth gatt object.
190 * @param[in] env JNI interface pointer.
191 * @param[in] gatt Gatt profile object.
192 * @return bluetooth address.
194 jstring CALEClientGetAddressFromGattObj(JNIEnv *env, jobject gatt);
197 * get remote address from bluetooth socket object.
198 * @param[in] env JNI interface pointer.
199 * @param[in] bluetoothSocketObj bluetooth socket.
200 * @return bluetooth address.
202 jstring CALEClientGetRemoteAddress(JNIEnv *env, jobject bluetoothSocketObj);
206 * @param[in] env JNI interface pointer.
207 * @param[in] bluetoothGatt gatt profile object.
208 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
210 CAResult_t CALEClientGattClose(JNIEnv *env, jobject bluetoothGatt);
213 * start to scan whole bluetooth devices (interface).
214 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
216 CAResult_t CALEClientStartScan();
219 * start to scan whole bluetooth devices (implement).
220 * @param[in] env JNI interface pointer.
221 * @param[in] callback callback to receive device object by scanning.
222 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
224 CAResult_t CALEClientStartScanImpl(JNIEnv *env, jobject callback);
227 * start to scan target bluetooth devices for service uuid (implement).
228 * @param[in] env JNI interface pointer.
229 * @param[in] uuids target UUID.
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 CALEClientStartScanWithUUIDImpl(JNIEnv *env, jobjectArray uuids,
238 * @param[in] env JNI interface pointer.
239 * @param[in] uuid uuid.
240 * @return uuid object.
242 jobject CALEClientGetUUIDObject(JNIEnv *env, const char *uuid);
245 * stop scan (interface).
246 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
248 CAResult_t CALEClientStopScan();
251 * set ble scanning flag.
252 * @param[in] flag scan flag.
254 void CALEClientSetScanFlag(bool flag);
257 * stop scan (implement).
258 * @param[in] env JNI interface pointer.
259 * @param[in] callback callback to receive device object by scanning.
260 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
262 CAResult_t CALEClientStopScanImpl(JNIEnv *env, jobject callback);
265 * set flag into State List.
266 * @param[in] env JNI interface pointer.
267 * @param[in] jni_address remote address.
268 * @param[in] state_idx state index.
269 * @param[in] flag auto connect flag.
271 CAResult_t CALEClientSetFlagToState(JNIEnv *env, jstring jni_address,
272 jint state_idx, jboolean flag);
275 * get flag from State List.
276 * @param[in] env JNI interface pointer.
277 * @param[in] jni_address remote address.
278 * @param[in] state_idx state index.
279 * @return current flag;
281 jboolean CALEClientGetFlagFromState(JNIEnv *env, jstring jni_address, jint state_idx);
284 * connect to gatt server hosted.
285 * @param[in] env JNI interface pointer.
286 * @param[in] bluetoothDevice bluetooth Device object.
287 * @param[in] autoconnect whether to directly connect to the remote device(false) or
288 * to automatically connect as soon as the remote device
290 * @return gatt object
292 jobject CALEClientConnect(JNIEnv *env, jobject bluetoothDevice, jboolean autoconnect);
295 * disconnect to gatt server by a target device.
296 * @param[in] env JNI interface pointer.
297 * @param[in] bluetoothGatt Gatt profile object.
298 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
300 CAResult_t CALEClientDisconnect(JNIEnv *env, jobject bluetoothGatt);
303 * disconnect to gatt server by whole devices.
304 * @param[in] env JNI interface pointer.
305 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
307 CAResult_t CALEClientDisconnectAll(JNIEnv *env);
310 * disconnect to gatt server by selected address.
311 * @param[in] env JNI interface pointer.
312 * @param[in] remoteAddress remote address.
313 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
315 CAResult_t CALEClientDisconnectforAddress(JNIEnv *env, jstring remoteAddress);
318 * start discovery server.
319 * @param[in] env JNI interface pointer.
320 * @param[in] bluetoothGatt Gatt profile object.
321 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
323 CAResult_t CALEClientDiscoverServices(JNIEnv *env, jobject bluetoothGatt);
326 * call CALESetValueAndWriteCharacteristic when connection is successful.
327 * @param[in] env JNI interface pointer.
328 * @param[in] gatt Gatt profile object.
329 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
331 CAResult_t CALEClientWriteCharacteristic(JNIEnv *env, jobject gatt);
334 * create GattCharacteristic and call CALEClientWriteCharacteristicImpl
335 * for request to write gatt characteristic.
336 * @param[in] env JNI interface pointer.
337 * @param[in] gatt Gatt profile object.
338 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
340 CAResult_t CALESetValueAndWriteCharacteristic(JNIEnv *env, jobject gatt);
343 * request to write gatt characteristic.
344 * @param[in] env JNI interface pointer.
345 * @param[in] bluetoothGatt Gatt profile object.
346 * @param[in] gattCharacteristic characteristic object that contain data to send.
347 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
349 CAResult_t CALEClientWriteCharacteristicImpl(JNIEnv *env, jobject bluetoothGatt,
350 jobject gattCharacteristic);
353 * request to read gatt characteristic.
354 * @param[in] env JNI interface pointer.
355 * @param[in] bluetoothGatt Gatt profile object.
356 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
358 CAResult_t CALEClientReadCharacteristic(JNIEnv *env, jobject bluetoothGatt);
361 * enable notification for a target device.
362 * @param[in] env JNI interface pointer.
363 * @param[in] bluetoothGatt Gatt profile object.
364 * @param[in] characteristic Characteristic object.
365 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
367 CAResult_t CALEClientSetCharacteristicNotification(JNIEnv *env, jobject bluetoothGatt,
368 jobject characteristic);
371 * create gatt characteristic object.
372 * @param[in] env JNI interface pointer.
373 * @param[in] bluetoothGatt Gatt profile object.
374 * @param[in] data for make Characteristic with data.
375 * @return Gatt Characteristic object.
377 jobject CALEClientCreateGattCharacteristic(JNIEnv *env, jobject bluetoothGatt, jbyteArray data);
381 * @param[in] env JNI interface pointer.
382 * @param[in] bluetoothGatt Gatt profile object.
383 * @param[in] characterUUID for make BluetoothGattCharacteristic object.
384 * @return Gatt Service.
386 jobject CALEClientGetGattService(JNIEnv *env, jobject bluetoothGatt, jstring characterUUID);
389 * get value from characteristic.
390 * @param[in] env JNI interface pointer.
391 * @param[in] characteristic Characteristic object.
392 * @return value in characteristic.
394 jbyteArray CALEClientGetValueFromCharacteristic(JNIEnv *env, jobject characteristic);
398 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
400 CAResult_t CALEClientCreateUUIDList();
403 * set UUID to descriptor.
404 * @param[in] env JNI interface pointer.
405 * @param[in] bluetoothGatt Gatt profile object.
406 * @param[in] characteristic Characteristic object.
407 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
409 CAResult_t CALEClientSetUUIDToDescriptor(JNIEnv *env, jobject bluetoothGatt,
410 jobject characteristic);
413 * add device object to scan device list.
414 * @param[in] env JNI interface pointer.
415 * @param[in] device bluetooth device object.
416 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
418 CAResult_t CALEClientAddScanDeviceToList(JNIEnv *env, jobject device);
421 * check whether the device exist in list or not.
422 * @param[in] env JNI interface pointer.
423 * @param[in] remoteAddress remote address.
424 * @return true or false.
426 bool CALEClientIsDeviceInScanDeviceList(JNIEnv *env, const char *remoteAddress);
429 * remove all devices in scan device list.
430 * @param[in] env JNI interface pointer.
431 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
433 CAResult_t CALEClientRemoveAllScanDevices(JNIEnv *env);
436 * remove target device in scan device list.
437 * @param[in] env JNI interface pointer.
438 * @param[in] remoteAddress remote address.
439 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
441 CAResult_t CALEClientRemoveDeviceInScanDeviceList(JNIEnv *env, jstring remoteAddress);
444 * add gatt object to gatt object list.
445 * @param[in] env JNI interface pointer.
446 * @param[in] gatt Gatt profile object.
447 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
449 CAResult_t CALEClientAddGattobjToList(JNIEnv *env, jobject gatt);
452 * check whether the gatt object exist in list or not.
453 * @param[in] env JNI interface pointer.
454 * @param[in] remoteAddress remote address.
455 * @return true or false.
457 bool CALEClientIsGattObjInList(JNIEnv *env, const char *remoteAddress);
460 * get the gatt object.
461 * @param[in] env JNI interface pointer.
462 * @param[in] remoteAddress remote address.
463 * @return gatt object.
465 jobject CALEClientGetGattObjInList(JNIEnv *env, const char* remoteAddress);
468 * remove all gatt objects in gatt object list.
469 * @param[in] env JNI interface pointer.
470 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
472 CAResult_t CALEClientRemoveAllGattObjs(JNIEnv *env);
475 * remove target device in gatt object list.
476 * @param[in] env JNI interface pointer.
477 * @param[in] gatt Gatt profile object.
478 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
480 CAResult_t CALEClientRemoveGattObj(JNIEnv *env, jobject gatt);
483 * remove gatt object of target device for address in gatt object list.
484 * @param[in] env JNI interface pointer.
485 * @param[in] gatt Gatt profile object.
486 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
488 CAResult_t CALEClientRemoveGattObjForAddr(JNIEnv *env, jstring addr);
491 * get ble address from Bluetooth device.
492 * @param[in] env JNI interface pointer.
493 * @param[in] bluetoothDevice Bluetooth device.
494 * @return ble address.
496 jstring CALEClientGetLEAddressFromBTDevice(JNIEnv *env, jobject bluetoothDevice);
499 * update new state information.
500 * @param[in] address remote address.
501 * @param[in] state_type state type.
502 * @param[in] target_state state index to update.
503 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
505 CAResult_t CALEClientUpdateDeviceState(const char* address, uint16_t state_type,
506 uint16_t target_state);
509 * check whether the remote address is existed or not.
510 * @param[in] address remote address.
511 * @return true or false.
513 bool CALEClientIsDeviceInList(const char *remoteAddress);
516 * remove all device states.
517 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
519 CAResult_t CALEClientRemoveAllDeviceState();
522 * Reset values of device state for all of devices.
523 * this method has to be invoked when BT adapter is disabled.
524 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
526 CAResult_t CALEClientResetDeviceStateForAll();
529 * remove the device state for a remote device.
530 * @param[in] remoteAddress remote address.
531 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
533 CAResult_t CALEClientRemoveDeviceState(const char* remoteAddress);
536 * get state information for a remote device.
537 * @param[in] remoteAddress remote address.
538 * @return CALEState_t.
540 CALEState_t* CALEClientGetStateInfo(const char* remoteAddress);
543 * check whether the remote address has same state with target state.
544 * @param[in] remoteAddress remote address.
545 * @param[in] state_type state_type.
546 * @param[in] target_state state index to check.
547 * @return true or false.
549 bool CALEClientIsValidState(const char* remoteAddress, uint16_t state_type,
550 uint16_t target_state);
553 * create scan device list.
555 void CALEClientCreateDeviceList();
558 * update the counter which data is sent to remote device.
559 * @param[in] env JNI interface pointer.
561 void CALEClientUpdateSendCnt(JNIEnv *env);
565 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
567 CAResult_t CALEClientInitGattMutexVaraibles();
572 void CALEClientTerminateGattMutexVariables();
575 * set send finish flag.
576 * @param[in] flag finish flag.
578 void CALEClientSetSendFinishFlag(bool flag);
581 * close the connection of the profile proxy to the Service.
582 * @param[in] env JNI interface pointer.
583 * @param[in] gatt gatt profile object.
584 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
586 CAResult_t CALEClientCloseProfileProxy(JNIEnv *env, jobject gatt);
589 * connect to GATT Server hosted by this device.
590 * @param[in] env JNI interface pointer.
591 * @param[in] bluetoothDevice bluetooth device object.
592 * @param[in] autoconnect connect as soon as the device becomes avaiable(true).
593 * @return gatt profile object
595 jobject CALEClientGattConnect(JNIEnv *env, jobject bluetoothDevice, jboolean autoconnect);
601 #endif /* CA_LECLIENT_H_ */