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;
54 * Callback to be notified on reception of any data from remote devices.
55 * @param[in] address MAC address of remote device.
56 * @param[in] data Data received from remote device.
57 * @pre Callback must be registered using CALESetCallback(CAPacketReceiveCallback callback)
59 typedef void (*CAPacketReceiveCallback)(const char *address, const uint8_t *data);
62 * initialize JNI object.
64 void CALEClientJniInit();
67 * set context of application.
69 void CALEClientJNISetContext();
72 * create interface object and initialize the object.
73 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
75 CAResult_t CALEClientCreateJniInterfaceObject();
78 * initialize client for BLE.
79 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
81 CAResult_t CALEClientInitialize();
84 * terminate client for BLE.
86 void CALEClientTerminate();
89 * destroy interface object and terminate the interface.
90 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
92 CAResult_t CALEClientDestroyJniInterface();
95 * for destroy sending routine.
96 * @param[in] env JNI interface pointer.
97 * @param[in] gatt Gatt profile object.
99 void CALEClientSendFinish(JNIEnv *env, jobject gatt);
102 * send data for unicast (interface).
103 * @param[in] address remote address.
104 * @param[in] data data for transmission.
105 * @param[in] dataLen data length.
106 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
108 CAResult_t CALEClientSendUnicastMessage(const char *address, const uint8_t *data,
109 const uint32_t dataLen);
112 * send data for multicast (interface).
113 * @param[in] data data for transmission.
114 * @param[in] dataLen data length.
115 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
117 CAResult_t CALEClientSendMulticastMessage(const uint8_t *data, const uint32_t dataLen);
120 * start unicast server.
121 * @param[in] address remote address.
122 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
124 CAResult_t CALEClientStartUnicastServer(const char *address);
127 * start multicast server (start discovery).
128 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
130 CAResult_t CALEClientStartMulticastServer();
133 * stop unicast server.
135 void CALEClientStopUnicastServer();
138 * stop multicast server (stop discovery).
140 void CALEClientStopMulticastServer();
143 * set this callback for receiving data packets from peer devices.
144 * @param[in] callback callback to be notified on reception of
145 * unicast/multicast data packets.
147 void CALEClientSetCallback(CAPacketReceiveCallback callback);
150 * waiting to get scanned device from BT Platform.
151 * if there is no scanned device in the list.
152 * @param[in] env JNI interface pointer.
153 * @param[in] address LE address.
154 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
156 CAResult_t CALEClientIsThereScannedDevices(JNIEnv *env, const char* address);
159 * send data for unicast (implement).
160 * @param[in] address remote address.
161 * @param[in] data data for transmission.
162 * @param[in] dataLen data length.
163 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
165 CAResult_t CALEClientSendUnicastMessageImpl(const char *address, const uint8_t *data,
166 const uint32_t dataLen);
169 * send data for multicast (implement).
170 * @param[in] env JNI interface pointer.
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 CALEClientSendMulticastMessageImpl(JNIEnv *env, const uint8_t *data,
176 const uint32_t dataLen);
179 * send data to remote device.
180 * if it isn't connected yet. connect LE before try to send data.
181 * @param[in] env JNI interface pointer.
182 * @param[in] device bluetooth device object.
183 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
185 CAResult_t CALEClientSendData(JNIEnv *env, jobject device);
188 * get address from bluetooth gatt object.
189 * @param[in] env JNI interface pointer.
190 * @param[in] gatt Gatt profile object.
191 * @return bluetooth address.
193 jstring CALEClientGetAddressFromGattObj(JNIEnv *env, jobject gatt);
196 * get remote address from bluetooth socket object.
197 * @param[in] env JNI interface pointer.
198 * @param[in] bluetoothSocketObj bluetooth socket.
199 * @return bluetooth address.
201 jstring CALEClientGetRemoteAddress(JNIEnv *env, jobject bluetoothSocketObj);
205 * @param[in] env JNI interface pointer.
206 * @param[in] bluetoothGatt gatt profile object.
207 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
209 CAResult_t CALEClientGattClose(JNIEnv *env, jobject bluetoothGatt);
212 * start to scan whole bluetooth devices (interface).
213 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
215 CAResult_t CALEClientStartScan();
218 * start to scan whole bluetooth devices (implement).
219 * @param[in] env JNI interface pointer.
220 * @param[in] callback callback to receive device object by scanning.
221 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
223 CAResult_t CALEClientStartScanImpl(JNIEnv *env, jobject callback);
226 * start to scan target bluetooth devices for service uuid (implement).
227 * @param[in] env JNI interface pointer.
228 * @param[in] uuids target UUID.
229 * @param[in] callback callback to receive device object by scanning.
230 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
232 CAResult_t CALEClientStartScanWithUUIDImpl(JNIEnv *env, jobjectArray uuids,
237 * @param[in] env JNI interface pointer.
238 * @param[in] uuid uuid.
239 * @return uuid object.
241 jobject CALEClientGetUUIDObject(JNIEnv *env, const char *uuid);
244 * stop scan (interface).
245 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
247 CAResult_t CALEClientStopScan();
250 * set ble scanning flag.
251 * @param[in] flag scan flag.
253 void CALEClientSetScanFlag(bool flag);
256 * stop scan (implement).
257 * @param[in] env JNI interface pointer.
258 * @param[in] callback callback to receive device object by scanning.
259 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
261 CAResult_t CALEClientStopScanImpl(JNIEnv *env, jobject callback);
264 * set flag into State List.
265 * @param[in] env JNI interface pointer.
266 * @param[in] jni_address remote address.
267 * @param[in] state_idx state index.
268 * @param[in] flag auto connect flag.
270 CAResult_t CALEClientSetFlagToState(JNIEnv *env, jstring jni_address,
271 jint state_idx, jboolean flag);
274 * get flag from State List.
275 * @param[in] env JNI interface pointer.
276 * @param[in] jni_address remote address.
277 * @param[in] state_idx state index.
278 * @return current flag;
280 jboolean CALEClientGetFlagFromState(JNIEnv *env, jstring jni_address, jint state_idx);
283 * connect to gatt server hosted.
284 * @param[in] env JNI interface pointer.
285 * @param[in] bluetoothDevice bluetooth Device object.
286 * @param[in] autoconnect whether to directly connect to the remote device(false) or
287 * to automatically connect as soon as the remote device
289 * @return gatt object
291 jobject CALEClientConnect(JNIEnv *env, jobject bluetoothDevice, jboolean autoconnect);
294 * disconnect to gatt server by a target device.
295 * @param[in] env JNI interface pointer.
296 * @param[in] bluetoothGatt Gatt profile object.
297 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
299 CAResult_t CALEClientDisconnect(JNIEnv *env, jobject bluetoothGatt);
302 * disconnect to gatt server by whole devices.
303 * @param[in] env JNI interface pointer.
304 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
306 CAResult_t CALEClientDisconnectAll(JNIEnv *env);
309 * disconnect to gatt server by selected address.
310 * @param[in] env JNI interface pointer.
311 * @param[in] remoteAddress remote address.
312 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
314 CAResult_t CALEClientDisconnectforAddress(JNIEnv *env, jstring remoteAddress);
317 * start discovery server.
318 * @param[in] env JNI interface pointer.
319 * @param[in] bluetoothGatt Gatt profile object.
320 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
322 CAResult_t CALEClientDiscoverServices(JNIEnv *env, jobject bluetoothGatt);
325 * call CALESetValueAndWriteCharacteristic when connection is successful.
326 * @param[in] env JNI interface pointer.
327 * @param[in] gatt Gatt profile object.
328 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
330 CAResult_t CALEClientWriteCharacteristic(JNIEnv *env, jobject gatt);
333 * create GattCharacteristic and call CALEClientWriteCharacteristicImpl
334 * for request to write gatt characteristic.
335 * @param[in] env JNI interface pointer.
336 * @param[in] gatt Gatt profile object.
337 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
339 CAResult_t CALESetValueAndWriteCharacteristic(JNIEnv *env, jobject gatt);
342 * request to write gatt characteristic.
343 * @param[in] env JNI interface pointer.
344 * @param[in] bluetoothGatt Gatt profile object.
345 * @param[in] gattCharacteristic characteristic object that contain data to send.
346 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
348 CAResult_t CALEClientWriteCharacteristicImpl(JNIEnv *env, jobject bluetoothGatt,
349 jobject gattCharacteristic);
352 * request to read gatt characteristic.
353 * @param[in] env JNI interface pointer.
354 * @param[in] bluetoothGatt Gatt profile object.
355 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
357 CAResult_t CALEClientReadCharacteristic(JNIEnv *env, jobject bluetoothGatt);
360 * enable notification for a target device.
361 * @param[in] env JNI interface pointer.
362 * @param[in] bluetoothGatt Gatt profile object.
363 * @param[in] characteristic Characteristic object.
364 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
366 CAResult_t CALEClientSetCharacteristicNotification(JNIEnv *env, jobject bluetoothGatt,
367 jobject characteristic);
370 * create gatt characteristic object.
371 * @param[in] env JNI interface pointer.
372 * @param[in] bluetoothGatt Gatt profile object.
373 * @param[in] data for make Characteristic with data.
374 * @return Gatt Characteristic object.
376 jobject CALEClientCreateGattCharacteristic(JNIEnv *env, jobject bluetoothGatt, jbyteArray data);
380 * @param[in] env JNI interface pointer.
381 * @param[in] bluetoothGatt Gatt profile object.
382 * @param[in] characterUUID for make BluetoothGattCharacteristic object.
383 * @return Gatt Service.
385 jobject CALEClientGetGattService(JNIEnv *env, jobject bluetoothGatt, jstring characterUUID);
388 * get value from characteristic.
389 * @param[in] env JNI interface pointer.
390 * @param[in] characteristic Characteristic object.
391 * @return value in characteristic.
393 jbyteArray CALEClientGetValueFromCharacteristic(JNIEnv *env, jobject characteristic);
397 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
399 CAResult_t CALEClientCreateUUIDList();
402 * set UUID to descriptor.
403 * @param[in] env JNI interface pointer.
404 * @param[in] bluetoothGatt Gatt profile object.
405 * @param[in] characteristic Characteristic object.
406 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
408 CAResult_t CALEClientSetUUIDToDescriptor(JNIEnv *env, jobject bluetoothGatt,
409 jobject characteristic);
412 * add device object to scan device list.
413 * @param[in] env JNI interface pointer.
414 * @param[in] device bluetooth device object.
415 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
417 CAResult_t CALEClientAddScanDeviceToList(JNIEnv *env, jobject device);
420 * check whether the device exist in list or not.
421 * @param[in] env JNI interface pointer.
422 * @param[in] remoteAddress remote address.
423 * @return true or false.
425 bool CALEClientIsDeviceInScanDeviceList(JNIEnv *env, const char *remoteAddress);
428 * remove all devices in scan device list.
429 * @param[in] env JNI interface pointer.
430 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
432 CAResult_t CALEClientRemoveAllScanDevices(JNIEnv *env);
435 * remove target device in scan device list.
436 * @param[in] env JNI interface pointer.
437 * @param[in] remoteAddress remote address.
438 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
440 CAResult_t CALEClientRemoveDeviceInScanDeviceList(JNIEnv *env, jstring remoteAddress);
443 * add gatt object to gatt object list.
444 * @param[in] env JNI interface pointer.
445 * @param[in] gatt Gatt profile object.
446 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
448 CAResult_t CALEClientAddGattobjToList(JNIEnv *env, jobject gatt);
451 * check whether the gatt object exist in list or not.
452 * @param[in] env JNI interface pointer.
453 * @param[in] remoteAddress remote address.
454 * @return true or false.
456 bool CALEClientIsGattObjInList(JNIEnv *env, const char *remoteAddress);
459 * get the gatt object.
460 * @param[in] env JNI interface pointer.
461 * @param[in] remoteAddress remote address.
462 * @return gatt object.
464 jobject CALEClientGetGattObjInList(JNIEnv *env, const char* remoteAddress);
467 * remove all gatt objects in gatt object list.
468 * @param[in] env JNI interface pointer.
469 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
471 CAResult_t CALEClientRemoveAllGattObjs(JNIEnv *env);
474 * remove target device in gatt object list.
475 * @param[in] env JNI interface pointer.
476 * @param[in] gatt Gatt profile object.
477 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
479 CAResult_t CALEClientRemoveGattObj(JNIEnv *env, jobject gatt);
482 * remove gatt object of target device for address 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 CALEClientRemoveGattObjForAddr(JNIEnv *env, jstring addr);
490 * get ble address from Bluetooth device.
491 * @param[in] env JNI interface pointer.
492 * @param[in] bluetoothDevice Bluetooth device.
493 * @return ble address.
495 jstring CALEClientGetLEAddressFromBTDevice(JNIEnv *env, jobject bluetoothDevice);
498 * update new state information.
499 * @param[in] address remote address.
500 * @param[in] state_type state type.
501 * @param[in] target_state state index to update.
502 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
504 CAResult_t CALEClientUpdateDeviceState(const char* address, uint16_t state_type,
505 uint16_t target_state);
508 * check whether the remote address is existed or not.
509 * @param[in] address remote address.
510 * @return true or false.
512 bool CALEClientIsDeviceInList(const char *remoteAddress);
515 * remove all device states.
516 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
518 CAResult_t CALEClientRemoveAllDeviceState();
521 * Reset values of device state for all of devices.
522 * this method has to be invoked when BT adapter is disabled.
523 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
525 CAResult_t CALEClientResetDeviceStateForAll();
528 * remove the device state for a remote device.
529 * @param[in] remoteAddress remote address.
530 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
532 CAResult_t CALEClientRemoveDeviceState(const char* remoteAddress);
535 * get state information for a remote device.
536 * @param[in] remoteAddress remote address.
537 * @return CALEState_t.
539 CALEState_t* CALEClientGetStateInfo(const char* remoteAddress);
542 * check whether the remote address has same state with target state.
543 * @param[in] remoteAddress remote address.
544 * @param[in] state_type state_type.
545 * @param[in] target_state state index to check.
546 * @return true or false.
548 bool CALEClientIsValidState(const char* remoteAddress, uint16_t state_type,
549 uint16_t target_state);
552 * create scan device list.
554 void CALEClientCreateDeviceList();
557 * update the counter which data is sent to remote device.
558 * @param[in] env JNI interface pointer.
560 void CALEClientUpdateSendCnt(JNIEnv *env);
564 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
566 CAResult_t CALEClientInitGattMutexVaraibles();
571 void CALEClientTerminateGattMutexVariables();
574 * set send finish flag.
575 * @param[in] flag finish flag.
577 void CALEClientSetSendFinishFlag(bool flag);
580 * close the connection of the profile proxy to the Service.
581 * @param[in] env JNI interface pointer.
582 * @param[in] gatt gatt profile object.
583 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
585 CAResult_t CALEClientCloseProfileProxy(JNIEnv *env, jobject gatt);
588 * connect to GATT Server hosted by this device.
589 * @param[in] env JNI interface pointer.
590 * @param[in] bluetoothDevice bluetooth device object.
591 * @param[in] autoconnect connect as soon as the device becomes avaiable(true).
592 * @return gatt profile object
594 jobject CALEClientGattConnect(JNIEnv *env, jobject bluetoothDevice, jboolean autoconnect);
597 * connect to GATT Server hosted by this device directly.
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 ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
603 CAResult_t CALEClientDirectConnect(JNIEnv *env, jobject bluetoothDevice, jboolean autoconnect);
609 #endif /* CA_LECLIENT_H_ */