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;
60 * Callback to be notified on reception of any data from remote devices.
61 * @param[in] address MAC address of remote device.
62 * @param[in] data Data received from remote device.
63 * @pre Callback must be registered using CALESetCallback(CAPacketReceiveCallback callback)
65 typedef void (*CAPacketReceiveCallback)(const char *address, const uint8_t *data);
68 * initialize JNI object.
70 void CALEClientJniInit();
73 * set context of application.
75 void CALEClientJNISetContext();
78 * create interface object and initialize the object.
79 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
81 CAResult_t CALEClientCreateJniInterfaceObject();
84 * initialize client for BLE.
85 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
87 CAResult_t CALEClientInitialize();
90 * terminate client for BLE.
92 void CALEClientTerminate();
95 * destroy interface object and terminate the interface.
96 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
98 CAResult_t CALEClientDestroyJniInterface();
101 * for destroy sending routine.
102 * @param[in] env JNI interface pointer.
103 * @param[in] gatt Gatt profile object.
105 void CALEClientSendFinish(JNIEnv *env, jobject gatt);
108 * send data for unicast (interface).
109 * @param[in] address remote address.
110 * @param[in] data data for transmission.
111 * @param[in] dataLen data length.
112 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
114 CAResult_t CALEClientSendUnicastMessage(const char *address, const uint8_t *data,
115 const uint32_t dataLen);
118 * send data for multicast (interface).
119 * @param[in] data data for transmission.
120 * @param[in] dataLen data length.
121 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
123 CAResult_t CALEClientSendMulticastMessage(const uint8_t *data, const uint32_t dataLen);
126 * start unicast server.
127 * @param[in] address remote address.
128 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
130 CAResult_t CALEClientStartUnicastServer(const char *address);
133 * start multicast server (start discovery).
134 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
136 CAResult_t CALEClientStartMulticastServer();
139 * stop unicast server.
141 void CALEClientStopUnicastServer();
144 * stop multicast server (stop discovery).
146 void CALEClientStopMulticastServer();
149 * set this callback for receiving data packets from peer devices.
150 * @param[in] callback callback to be notified on reception of
151 * unicast/multicast data packets.
153 void CALEClientSetCallback(CAPacketReceiveCallback callback);
156 * waiting to get scanned device from BT Platform.
157 * if there is no scanned device in the list.
158 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
160 CAResult_t CALEClientIsThereScannedDevices();
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] flag auto connect flag.
278 void CALEClientSetAutoConnectFlag(jboolean flag);
281 * get auto connect flag.
282 * @return current auto connect flag;
284 jboolean CALEClientGetAutoConnectFlag();
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] connectedState connection state.
505 * @param[in] notificationState whether characteristic notification already set or not.
506 * @param[in] sendState whether sending was success or not.
507 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
509 CAResult_t CALEClientUpdateDeviceState(const char* address, uint32_t connectedState,
510 uint16_t notificationState, uint16_t sendState);
513 * add new state to state list.
514 * @param[in] state new state.
515 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
517 CAResult_t CALEClientAddDeviceStateToList(CALEState_t* state);
520 * check whether the remote address is existed or not.
521 * @param[in] address remote address.
522 * @return true or false.
524 bool CALEClientIsDeviceInList(const char *remoteAddress);
527 * remove all device states.
528 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
530 CAResult_t CALEClientRemoveAllDeviceState();
533 * remove the device state for a remote device.
534 * @param[in] remoteAddress remote address.
535 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
537 CAResult_t CALEClientRemoveDeviceState(const char* remoteAddress);
540 * get state information for a remote device.
541 * @param[in] remoteAddress remote address.
542 * @return CALEState_t.
544 CALEState_t* CALEClientGetStateInfo(const char* remoteAddress);
547 * check whether the remote address is connected or not.
548 * @param[in] remoteAddress remote address.
549 * @return true or false.
551 bool CALEClientIsConnectedDevice(const char* remoteAddress);
554 * check whether the remote address set CharacteristicNotification or not.
555 * @param[in] remoteAddress remote address.
556 * @return true or false.
558 bool CALEClientIsSetCharacteristic(const char* remoteAddress);
561 * create scan device list.
563 void CALEClientCreateDeviceList();
566 * update the counter which data is sent to remote device.
567 * @param[in] env JNI interface pointer.
569 void CALEClientUpdateSendCnt(JNIEnv *env);
573 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
575 CAResult_t CALEClientInitGattMutexVaraibles();
580 void CALEClientTerminateGattMutexVariables();
583 * set send finish flag.
584 * @param[in] flag finish flag.
586 void CALEClientSetSendFinishFlag(bool flag);
589 * close the connection of the profile proxy to the Service.
590 * @param[in] env JNI interface pointer.
591 * @param[in] gatt gatt profile object.
592 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
594 CAResult_t CALEClientCloseProfileProxy(JNIEnv *env, jobject gatt);
597 * connect to GATT Server hosted by this device.
598 * @param[in] env JNI interface pointer.
599 * @param[in] bluetoothDevice bluetooth device object.
600 * @param[in] autoconnect connect as soon as the device becomes avaiable(true).
601 * @return gatt profile object
603 jobject CALEClientGattConnect(JNIEnv *env, jobject bluetoothDevice, jboolean autoconnect);
609 #endif /* CA_LECLIENT_H_ */