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 static const jint STATE_CONNECTED = 2;
49 static const jint STATE_DISCONNECTED = 0;
51 typedef struct le_state_info
53 char address[CA_MACADDR_SIZE];
55 uint16_t notificationState;
57 jboolean autoConnectFlag;
61 * Callback to be notified on reception of any data from remote devices.
62 * @param[in] address MAC address of remote device.
63 * @param[in] data Data received from remote device.
64 * @pre Callback must be registered using CALESetCallback(CAPacketReceiveCallback callback)
66 typedef void (*CAPacketReceiveCallback)(const char *address, const uint8_t *data);
69 * initialize JNI object.
71 void CALEClientJniInit();
74 * set context of application.
76 void CALEClientJNISetContext();
79 * create interface object and initialize the object.
80 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
82 CAResult_t CALEClientCreateJniInterfaceObject();
85 * initialize client for BLE.
86 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
88 CAResult_t CALEClientInitialize();
91 * terminate client for BLE.
93 void CALEClientTerminate();
96 * destroy interface object and terminate the interface.
97 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
99 CAResult_t CALEClientDestroyJniInterface();
102 * for destroy sending routine.
103 * @param[in] env JNI interface pointer.
104 * @param[in] gatt Gatt profile object.
106 void CALEClientSendFinish(JNIEnv *env, jobject gatt);
109 * send data for unicast (interface).
110 * @param[in] address remote address.
111 * @param[in] data data for transmission.
112 * @param[in] dataLen data length.
113 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
115 CAResult_t CALEClientSendUnicastMessage(const char *address, const uint8_t *data,
116 const uint32_t dataLen);
119 * send data for multicast (interface).
120 * @param[in] data data for transmission.
121 * @param[in] dataLen data length.
122 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
124 CAResult_t CALEClientSendMulticastMessage(const uint8_t *data, const uint32_t dataLen);
127 * start unicast server.
128 * @param[in] address remote address.
129 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
131 CAResult_t CALEClientStartUnicastServer(const char *address);
134 * start multicast server (start discovery).
135 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
137 CAResult_t CALEClientStartMulticastServer();
140 * stop unicast server.
142 void CALEClientStopUnicastServer();
145 * stop multicast server (stop discovery).
147 void CALEClientStopMulticastServer();
150 * set this callback for receiving data packets from peer devices.
151 * @param[in] callback callback to be notified on reception of
152 * unicast/multicast data packets.
154 void CALEClientSetCallback(CAPacketReceiveCallback callback);
157 * waiting to get scanned device from BT Platform.
158 * if there is no scanned device in the list.
159 * @param[in] env JNI interface pointer.
160 * @param[in] address LE address.
161 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
163 CAResult_t CALEClientIsThereScannedDevices(JNIEnv *env, const char* address);
166 * send data for unicast (implement).
167 * @param[in] address remote address.
168 * @param[in] data data for transmission.
169 * @param[in] dataLen data length.
170 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
172 CAResult_t CALEClientSendUnicastMessageImpl(const char *address, const uint8_t *data,
173 const uint32_t dataLen);
176 * send data for multicast (implement).
177 * @param[in] env JNI interface pointer.
178 * @param[in] data data for transmission.
179 * @param[in] dataLen data length.
180 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
182 CAResult_t CALEClientSendMulticastMessageImpl(JNIEnv *env, const uint8_t *data,
183 const uint32_t dataLen);
186 * check whether it is connected or not with remote address.
187 * @param[in] address remote address.
188 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
190 CAResult_t CALECheckSendState(const char* address);
193 * send data to remote device.
194 * if it isn't connected yet. connect LE before try to send data.
195 * @param[in] env JNI interface pointer.
196 * @param[in] device bluetooth device object.
197 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
199 CAResult_t CALEClientSendData(JNIEnv *env, jobject device);
202 * get address from bluetooth gatt object.
203 * @param[in] env JNI interface pointer.
204 * @param[in] gatt Gatt profile object.
205 * @return bluetooth address.
207 jstring CALEClientGetAddressFromGattObj(JNIEnv *env, jobject gatt);
210 * get remote address from bluetooth socket object.
211 * @param[in] env JNI interface pointer.
212 * @param[in] bluetoothSocketObj bluetooth socket.
213 * @return bluetooth address.
215 jstring CALEClientGetRemoteAddress(JNIEnv *env, jobject bluetoothSocketObj);
219 * @param[in] env JNI interface pointer.
220 * @param[in] bluetoothGatt gatt profile object.
221 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
223 CAResult_t CALEClientGattClose(JNIEnv *env, jobject bluetoothGatt);
226 * start to scan whole bluetooth devices (interface).
227 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
229 CAResult_t CALEClientStartScan();
232 * start to scan whole bluetooth devices (implement).
233 * @param[in] env JNI interface pointer.
234 * @param[in] callback callback to receive device object by scanning.
235 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
237 CAResult_t CALEClientStartScanImpl(JNIEnv *env, jobject callback);
240 * start to scan target bluetooth devices for service uuid (implement).
241 * @param[in] env JNI interface pointer.
242 * @param[in] uuids target UUID.
243 * @param[in] callback callback to receive device object by scanning.
244 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
246 CAResult_t CALEClientStartScanWithUUIDImpl(JNIEnv *env, jobjectArray uuids,
251 * @param[in] env JNI interface pointer.
252 * @param[in] uuid uuid.
253 * @return uuid object.
255 jobject CALEClientGetUUIDObject(JNIEnv *env, const char *uuid);
258 * stop scan (interface).
259 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
261 CAResult_t CALEClientStopScan();
264 * set ble scanning flag.
265 * @param[in] flag scan flag.
267 void CALEClientSetScanFlag(bool flag);
270 * stop scan (implement).
271 * @param[in] env JNI interface pointer.
272 * @param[in] callback callback to receive device object by scanning.
273 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
275 CAResult_t CALEClientStopScanImpl(JNIEnv *env, jobject callback);
278 * set auto connect flag for connectGatt API.
279 * @param[in] env JNI interface pointer.
280 * @param[in] jni_address remote address.
281 * @param[in] flag auto connect flag.
283 CAResult_t CALEClientSetAutoConnectFlag(JNIEnv *env, jstring jni_address, jboolean flag);
286 * get auto connect flag.
287 * @param[in] env JNI interface pointer.
288 * @param[in] jni_address remote address.
289 * @return current auto connect flag;
291 jboolean CALEClientGetAutoConnectFlag(JNIEnv *env, jstring jni_address);
294 * connect to gatt server hosted.
295 * @param[in] env JNI interface pointer.
296 * @param[in] bluetoothDevice bluetooth Device object.
297 * @param[in] autoconnect whether to directly connect to the remote device(false) or
298 * to automatically connect as soon as the remote device
300 * @return gatt object
302 jobject CALEClientConnect(JNIEnv *env, jobject bluetoothDevice, jboolean autoconnect);
305 * disconnect to gatt server by a target device.
306 * @param[in] env JNI interface pointer.
307 * @param[in] bluetoothGatt Gatt profile object.
308 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
310 CAResult_t CALEClientDisconnect(JNIEnv *env, jobject bluetoothGatt);
313 * disconnect to gatt server by whole devices.
314 * @param[in] env JNI interface pointer.
315 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
317 CAResult_t CALEClientDisconnectAll(JNIEnv *env);
320 * disconnect to gatt server by selected address.
321 * @param[in] env JNI interface pointer.
322 * @param[in] remoteAddress remote address.
323 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
325 CAResult_t CALEClientDisconnectforAddress(JNIEnv *env, jstring remoteAddress);
328 * start discovery server.
329 * @param[in] env JNI interface pointer.
330 * @param[in] bluetoothGatt Gatt profile object.
331 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
333 CAResult_t CALEClientDiscoverServices(JNIEnv *env, jobject bluetoothGatt);
336 * call CALESetValueAndWriteCharacteristic when connection is successful.
337 * @param[in] env JNI interface pointer.
338 * @param[in] gatt Gatt profile object.
339 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
341 CAResult_t CALEClientWriteCharacteristic(JNIEnv *env, jobject gatt);
344 * create GattCharacteristic and call CALEClientWriteCharacteristicImpl
345 * for request to write gatt characteristic.
346 * @param[in] env JNI interface pointer.
347 * @param[in] gatt Gatt profile object.
348 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
350 CAResult_t CALESetValueAndWriteCharacteristic(JNIEnv *env, jobject gatt);
353 * request to write gatt characteristic.
354 * @param[in] env JNI interface pointer.
355 * @param[in] bluetoothGatt Gatt profile object.
356 * @param[in] gattCharacteristic characteristic object that contain data to send.
357 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
359 CAResult_t CALEClientWriteCharacteristicImpl(JNIEnv *env, jobject bluetoothGatt,
360 jobject gattCharacteristic);
363 * request to read gatt characteristic.
364 * @param[in] env JNI interface pointer.
365 * @param[in] bluetoothGatt Gatt profile object.
366 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
368 CAResult_t CALEClientReadCharacteristic(JNIEnv *env, jobject bluetoothGatt);
371 * enable notification for a target device.
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).
377 CAResult_t CALEClientSetCharacteristicNotification(JNIEnv *env, jobject bluetoothGatt,
378 jobject characteristic);
381 * create gatt characteristic object.
382 * @param[in] env JNI interface pointer.
383 * @param[in] bluetoothGatt Gatt profile object.
384 * @param[in] data for make Characteristic with data.
385 * @return Gatt Characteristic object.
387 jobject CALEClientCreateGattCharacteristic(JNIEnv *env, jobject bluetoothGatt, jbyteArray data);
391 * @param[in] env JNI interface pointer.
392 * @param[in] bluetoothGatt Gatt profile object.
393 * @param[in] characterUUID for make BluetoothGattCharacteristic object.
394 * @return Gatt Service.
396 jobject CALEClientGetGattService(JNIEnv *env, jobject bluetoothGatt, jstring characterUUID);
399 * get value from characteristic.
400 * @param[in] env JNI interface pointer.
401 * @param[in] characteristic Characteristic object.
402 * @return value in characteristic.
404 jbyteArray CALEClientGetValueFromCharacteristic(JNIEnv *env, jobject characteristic);
408 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
410 CAResult_t CALEClientCreateUUIDList();
413 * set UUID to descriptor.
414 * @param[in] env JNI interface pointer.
415 * @param[in] bluetoothGatt Gatt profile object.
416 * @param[in] characteristic Characteristic object.
417 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
419 CAResult_t CALEClientSetUUIDToDescriptor(JNIEnv *env, jobject bluetoothGatt,
420 jobject characteristic);
423 * add device object to scan device list.
424 * @param[in] env JNI interface pointer.
425 * @param[in] device bluetooth device object.
426 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
428 CAResult_t CALEClientAddScanDeviceToList(JNIEnv *env, jobject device);
431 * check whether the device exist in list or not.
432 * @param[in] env JNI interface pointer.
433 * @param[in] remoteAddress remote address.
434 * @return true or false.
436 bool CALEClientIsDeviceInScanDeviceList(JNIEnv *env, const char *remoteAddress);
439 * remove all devices in scan device list.
440 * @param[in] env JNI interface pointer.
441 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
443 CAResult_t CALEClientRemoveAllScanDevices(JNIEnv *env);
446 * remove target device in scan device list.
447 * @param[in] env JNI interface pointer.
448 * @param[in] remoteAddress remote address.
449 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
451 CAResult_t CALEClientRemoveDeviceInScanDeviceList(JNIEnv *env, jstring remoteAddress);
454 * add gatt object to gatt object list.
455 * @param[in] env JNI interface pointer.
456 * @param[in] gatt Gatt profile object.
457 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
459 CAResult_t CALEClientAddGattobjToList(JNIEnv *env, jobject gatt);
462 * check whether the gatt object exist in list or not.
463 * @param[in] env JNI interface pointer.
464 * @param[in] remoteAddress remote address.
465 * @return true or false.
467 bool CALEClientIsGattObjInList(JNIEnv *env, const char *remoteAddress);
470 * get the gatt object.
471 * @param[in] env JNI interface pointer.
472 * @param[in] remoteAddress remote address.
473 * @return gatt object.
475 jobject CALEClientGetGattObjInList(JNIEnv *env, const char* remoteAddress);
478 * remove all gatt objects in gatt object list.
479 * @param[in] env JNI interface pointer.
480 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
482 CAResult_t CALEClientRemoveAllGattObjs(JNIEnv *env);
485 * remove target device in gatt object list.
486 * @param[in] env JNI interface pointer.
487 * @param[in] gatt Gatt profile object.
488 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
490 CAResult_t CALEClientRemoveGattObj(JNIEnv *env, jobject gatt);
493 * remove gatt object of target device for address in gatt object list.
494 * @param[in] env JNI interface pointer.
495 * @param[in] gatt Gatt profile object.
496 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
498 CAResult_t CALEClientRemoveGattObjForAddr(JNIEnv *env, jstring addr);
501 * get ble address from Bluetooth device.
502 * @param[in] env JNI interface pointer.
503 * @param[in] bluetoothDevice Bluetooth device.
504 * @return ble address.
506 jstring CALEClientGetLEAddressFromBTDevice(JNIEnv *env, jobject bluetoothDevice);
509 * update new state information.
510 * @param[in] address remote address.
511 * @param[in] connectedState connection state.
512 * @param[in] notificationState whether characteristic notification already set or not.
513 * @param[in] sendState whether sending was success or not.
514 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
516 CAResult_t CALEClientUpdateDeviceState(const char* address, uint32_t connectedState,
517 uint16_t notificationState, uint16_t sendState);
520 * add new state to state list.
521 * @param[in] state new state.
522 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
524 CAResult_t CALEClientAddDeviceStateToList(CALEState_t* state);
527 * check whether the remote address is existed or not.
528 * @param[in] address remote address.
529 * @return true or false.
531 bool CALEClientIsDeviceInList(const char *remoteAddress);
534 * remove all device states.
535 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
537 CAResult_t CALEClientRemoveAllDeviceState();
540 * Reset values of device state for all of devices.
541 * this method has to be invoked when BT adapter is disabled.
542 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
544 CAResult_t CALEClientResetDeviceStateForAll();
547 * remove the device state for a remote device.
548 * @param[in] remoteAddress remote address.
549 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
551 CAResult_t CALEClientRemoveDeviceState(const char* remoteAddress);
554 * get state information for a remote device.
555 * @param[in] remoteAddress remote address.
556 * @return CALEState_t.
558 CALEState_t* CALEClientGetStateInfo(const char* remoteAddress);
561 * check whether the remote address is connected or not.
562 * @param[in] remoteAddress remote address.
563 * @return true or false.
565 bool CALEClientIsConnectedDevice(const char* remoteAddress);
568 * check whether the remote address set CharacteristicNotification or not.
569 * @param[in] remoteAddress remote address.
570 * @return true or false.
572 bool CALEClientIsSetCharacteristic(const char* remoteAddress);
575 * create scan device list.
577 void CALEClientCreateDeviceList();
580 * update the counter which data is sent to remote device.
581 * @param[in] env JNI interface pointer.
583 void CALEClientUpdateSendCnt(JNIEnv *env);
587 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
589 CAResult_t CALEClientInitGattMutexVaraibles();
594 void CALEClientTerminateGattMutexVariables();
597 * set send finish flag.
598 * @param[in] flag finish flag.
600 void CALEClientSetSendFinishFlag(bool flag);
603 * close the connection of the profile proxy to the Service.
604 * @param[in] env JNI interface pointer.
605 * @param[in] gatt gatt profile object.
606 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
608 CAResult_t CALEClientCloseProfileProxy(JNIEnv *env, jobject gatt);
611 * connect to GATT Server hosted by this device.
612 * @param[in] env JNI interface pointer.
613 * @param[in] bluetoothDevice bluetooth device object.
614 * @param[in] autoconnect connect as soon as the device becomes avaiable(true).
615 * @return gatt profile object
617 jobject CALEClientGattConnect(JNIEnv *env, jobject bluetoothDevice, jboolean autoconnect);
623 #endif /* CA_LECLIENT_H_ */