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 STATE_CHARACTER_SET = 2;
39 static const uint16_t STATE_CHARACTER_UNSET = 1;
40 static const uint16_t STATE_CHARACTER_NO_CHANGE = 0;
42 static const uint16_t STATE_SEND_NONE = 0;
43 static const uint16_t STATE_SEND_SUCCESS = 1;
44 static const uint16_t STATE_SEND_FAILED = 2;
46 static const jint STATE_CONNECTED = 2;
47 static const jint STATE_DISCONNECTED = 0;
49 typedef struct le_state_info
51 char address[CA_MACADDR_SIZE];
53 uint16_t notificationState;
58 * Callback to be notified on reception of any data from remote devices.
59 * @param[in] address MAC address of remote device.
60 * @param[in] data Data received from remote device.
61 * @pre Callback must be registered using CALESetCallback(CAPacketReceiveCallback callback)
63 typedef void (*CAPacketReceiveCallback)(const char *address, const uint8_t *data);
66 * initialize JNI object.
68 void CALEClientJniInit();
71 * set context of application.
73 void CALEClientJNISetContext();
76 * create interface object and initialize the object.
77 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
79 CAResult_t CALEClientCreateJniInterfaceObject();
82 * initialize client for BLE.
83 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
85 CAResult_t CALEClientInitialize();
88 * terminate client for BLE.
90 void CALEClientTerminate();
93 * destroy interface object and terminate the interface.
94 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
96 CAResult_t CALEClientDestroyJniInterface();
99 * for destroy sending routine.
100 * @param[in] env JNI interface pointer.
101 * @param[in] gatt Gatt profile object.
103 void CALEClientSendFinish(JNIEnv *env, jobject gatt);
106 * send data for unicast (interface).
107 * @param[in] address remote address.
108 * @param[in] data data for transmission.
109 * @param[in] dataLen data length.
110 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
112 CAResult_t CALEClientSendUnicastMessage(const char *address, const uint8_t *data,
113 const uint32_t dataLen);
116 * send data for multicast (interface).
117 * @param[in] data data for transmission.
118 * @param[in] dataLen data length.
119 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
121 CAResult_t CALEClientSendMulticastMessage(const uint8_t *data, const uint32_t dataLen);
124 * start unicast server.
125 * @param[in] address remote address.
126 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
128 CAResult_t CALEClientStartUnicastServer(const char *address);
131 * start multicast server (start discovery).
132 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
134 CAResult_t CALEClientStartMulticastServer();
137 * stop unicast server.
139 void CALEClientStopUnicastServer();
142 * stop multicast server (stop discovery).
144 void CALEClientStopMulticastServer();
147 * set this callback for receiving data packets from peer devices.
148 * @param[in] callback callback to be notified on reception of
149 * unicast/multicast data packets.
151 void CALEClientSetCallback(CAPacketReceiveCallback callback);
154 * waiting to get scanned device from BT Platform.
155 * if there is no scanned device in the list.
156 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
158 CAResult_t CALEClientIsThereScannedDevices();
161 * send data for unicast (implement).
162 * @param[in] address remote address.
163 * @param[in] data data for transmission.
164 * @param[in] dataLen data length.
165 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
167 CAResult_t CALEClientSendUnicastMessageImpl(const char *address, const uint8_t *data,
168 const uint32_t dataLen);
171 * send data for multicast (implement).
172 * @param[in] env JNI interface pointer.
173 * @param[in] data data for transmission.
174 * @param[in] dataLen data length.
175 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
177 CAResult_t CALEClientSendMulticastMessageImpl(JNIEnv *env, const uint8_t *data,
178 const uint32_t dataLen);
181 * check whether it is connected or not with remote address.
182 * @param[in] address remote address.
183 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
185 CAResult_t CALECheckSendState(const char* address);
188 * send data to remote device.
189 * if it isn't connected yet. connect LE before try to send data.
190 * @param[in] env JNI interface pointer.
191 * @param[in] device bluetooth device object.
192 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
194 CAResult_t CALEClientSendData(JNIEnv *env, jobject device);
197 * get address from bluetooth gatt object.
198 * @param[in] env JNI interface pointer.
199 * @param[in] gatt Gatt profile object.
200 * @return bluetooth address.
202 jstring CALEClientGetAddressFromGattObj(JNIEnv *env, jobject gatt);
205 * get remote address from bluetooth socket object.
206 * @param[in] env JNI interface pointer.
207 * @param[in] bluetoothSocketObj bluetooth socket.
208 * @return bluetooth address.
210 jstring CALEClientGetRemoteAddress(JNIEnv *env, jobject bluetoothSocketObj);
214 * @param[in] env JNI interface pointer.
215 * @param[in] bluetoothGatt gatt profile object.
216 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
218 CAResult_t CALEClientGattClose(JNIEnv *env, jobject bluetoothGatt);
221 * start to scan whole bluetooth devices (interface).
222 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
224 CAResult_t CALEClientStartScan();
227 * start to scan whole bluetooth devices (implement).
228 * @param[in] env JNI interface pointer.
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 CALEClientStartScanImpl(JNIEnv *env, jobject callback);
235 * start to scan target bluetooth devices for service uuid (implement).
236 * @param[in] env JNI interface pointer.
237 * @param[in] uuids target UUID.
238 * @param[in] callback callback to receive device object by scanning.
239 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
241 CAResult_t CALEClientStartScanWithUUIDImpl(JNIEnv *env, jobjectArray uuids,
246 * @param[in] env JNI interface pointer.
247 * @param[in] uuid uuid.
248 * @return uuid object.
250 jobject CALEClientGetUUIDObject(JNIEnv *env, const char *uuid);
253 * stop scan (interface).
254 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
256 CAResult_t CALEClientStopScan();
259 * set ble scanning flag.
260 * @param[in] flag scan flag.
262 void CALEClientSetScanFlag(bool flag);
265 * stop scan (implement).
266 * @param[in] env JNI interface pointer.
267 * @param[in] callback callback to receive device object by scanning.
268 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
270 CAResult_t CALEClientStopScanImpl(JNIEnv *env, jobject callback);
273 * connect to gatt server hosted.
274 * @param[in] env JNI interface pointer.
275 * @param[in] bluetoothDevice bluetooth Device object.
276 * @param[in] autoconnect whether to directly connect to the remote device(false) or
277 * to automatically connect as soon as the remote device
279 * @param[in] callback callback for connection state change.
280 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
282 CAResult_t CALEClientConnect(JNIEnv *env, jobject bluetoothDevice, jboolean autoconnect,
286 * disconnect to gatt server by a target device.
287 * @param[in] env JNI interface pointer.
288 * @param[in] bluetoothGatt Gatt profile object.
289 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
291 CAResult_t CALEClientDisconnect(JNIEnv *env, jobject bluetoothGatt);
294 * disconnect to gatt server by whole devices.
295 * @param[in] env JNI interface pointer.
296 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
298 CAResult_t CALEClientDisconnectAll(JNIEnv *env);
301 * start discovery server.
302 * @param[in] env JNI interface pointer.
303 * @param[in] bluetoothGatt Gatt profile object.
304 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
306 CAResult_t CALEClientDiscoverServices(JNIEnv *env, jobject bluetoothGatt);
309 * call CALESetValueAndWriteCharacteristic when connection is successful.
310 * @param[in] env JNI interface pointer.
311 * @param[in] gatt Gatt profile object.
312 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
314 CAResult_t CALEClientWriteCharacteristic(JNIEnv *env, jobject gatt);
317 * create GattCharacteristic and call CALEClientWriteCharacteristicImpl
318 * for request to write gatt characteristic.
319 * @param[in] env JNI interface pointer.
320 * @param[in] gatt Gatt profile object.
321 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
323 CAResult_t CALESetValueAndWriteCharacteristic(JNIEnv *env, jobject gatt);
326 * request to write gatt characteristic.
327 * @param[in] env JNI interface pointer.
328 * @param[in] bluetoothGatt Gatt profile object.
329 * @param[in] gattCharacteristic characteristic object that contain data to send.
330 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
332 CAResult_t CALEClientWriteCharacteristicImpl(JNIEnv *env, jobject bluetoothGatt,
333 jobject gattCharacteristic);
336 * request to read gatt characteristic.
337 * @param[in] env JNI interface pointer.
338 * @param[in] bluetoothGatt Gatt profile object.
339 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
341 CAResult_t CALEClientReadCharacteristic(JNIEnv *env, jobject bluetoothGatt);
344 * enable notification for a target device.
345 * @param[in] env JNI interface pointer.
346 * @param[in] bluetoothGatt Gatt profile object.
347 * @param[in] characteristic Characteristic object.
348 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
350 CAResult_t CALEClientSetCharacteristicNotification(JNIEnv *env, jobject bluetoothGatt,
351 jobject characteristic);
354 * create gatt characteristic object.
355 * @param[in] env JNI interface pointer.
356 * @param[in] bluetoothGatt Gatt profile object.
357 * @param[in] data for make Characteristic with data.
358 * @return Gatt Characteristic object.
360 jobject CALEClientCreateGattCharacteristic(JNIEnv *env, jobject bluetoothGatt, jbyteArray data);
364 * @param[in] env JNI interface pointer.
365 * @param[in] bluetoothGatt Gatt profile object.
366 * @param[in] characterUUID for make BluetoothGattCharacteristic object.
367 * @return Gatt Service.
369 jobject CALEClientGetGattService(JNIEnv *env, jobject bluetoothGatt, jstring characterUUID);
372 * get value from characteristic.
373 * @param[in] env JNI interface pointer.
374 * @param[in] characteristic Characteristic object.
375 * @return value in characteristic.
377 jbyteArray CALEClientGetValueFromCharacteristic(JNIEnv *env, jobject characteristic);
381 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
383 CAResult_t CALEClientCreateUUIDList();
386 * set UUID to descriptor.
387 * @param[in] env JNI interface pointer.
388 * @param[in] bluetoothGatt Gatt profile object.
389 * @param[in] characteristic Characteristic object.
390 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
392 CAResult_t CALEClientSetUUIDToDescriptor(JNIEnv *env, jobject bluetoothGatt,
393 jobject characteristic);
396 * add device object to scan device list.
397 * @param[in] env JNI interface pointer.
398 * @param[in] device bluetooth device object.
399 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
401 CAResult_t CALEClientAddScanDeviceToList(JNIEnv *env, jobject device);
404 * check whether the device exist in list or not.
405 * @param[in] env JNI interface pointer.
406 * @param[in] remoteAddress remote address.
407 * @return true or false.
409 bool CALEClientIsDeviceInScanDeviceList(JNIEnv *env, const char *remoteAddress);
412 * remove all devices in scan device list.
413 * @param[in] env JNI interface pointer.
414 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
416 CAResult_t CALEClientRemoveAllScanDevices(JNIEnv *env);
419 * remove target device in scan device list.
420 * @param[in] env JNI interface pointer.
421 * @param[in] remoteAddress remote address.
422 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
424 CAResult_t CALEClientRemoveDeviceInScanDeviceList(JNIEnv *env, jstring remoteAddress);
427 * add gatt object to gatt object list.
428 * @param[in] env JNI interface pointer.
429 * @param[in] gatt Gatt profile object.
430 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
432 CAResult_t CALEClientAddGattobjToList(JNIEnv *env, jobject gatt);
435 * check whether the gatt object exist in list or not.
436 * @param[in] env JNI interface pointer.
437 * @param[in] remoteAddress remote address.
438 * @return true or false.
440 bool CALEClientIsGattObjInList(JNIEnv *env, const char *remoteAddress);
443 * get the gatt object.
444 * @param[in] env JNI interface pointer.
445 * @param[in] remoteAddress remote address.
446 * @return gatt object.
448 jobject CALEClientGetGattObjInList(JNIEnv *env, const char* remoteAddress);
451 * remove all gatt objects in gatt object list.
452 * @param[in] env JNI interface pointer.
453 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
455 CAResult_t CALEClientRemoveAllGattObjs(JNIEnv *env);
458 * remove target device in gatt object list.
459 * @param[in] env JNI interface pointer.
460 * @param[in] gatt Gatt profile object.
461 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
463 CAResult_t CALEClientRemoveGattObj(JNIEnv *env, jobject gatt);
466 * remove gatt object of target device for address in gatt object list.
467 * @param[in] env JNI interface pointer.
468 * @param[in] gatt Gatt profile object.
469 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
471 CAResult_t CALEClientRemoveGattObjForAddr(JNIEnv *env, jstring addr);
474 * update new state information.
475 * @param[in] address remote address.
476 * @param[in] connectedState connection state.
477 * @param[in] notificationState whether characteristic notification already set or not.
478 * @param[in] sendState whether sending was success or not.
479 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
481 CAResult_t CALEClientUpdateDeviceState(const char* address, uint32_t connectedState,
482 uint16_t notificationState, uint16_t sendState);
485 * add new state to state list.
486 * @param[in] state new state.
487 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
489 CAResult_t CALEClientAddDeviceStateToList(CALEState_t* state);
492 * check whether the remote address is existed or not.
493 * @param[in] address remote address.
494 * @return true or false.
496 bool CALEClientIsDeviceInList(const char *remoteAddress);
499 * remove all device states.
500 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
502 CAResult_t CALEClientRemoveAllDeviceState();
505 * remove the device state for a remote device.
506 * @param[in] remoteAddress remote address.
507 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
509 CAResult_t CALEClientRemoveDeviceState(const char* remoteAddress);
512 * get state information for a remote device.
513 * @param[in] remoteAddress remote address.
514 * @return CALEState_t.
516 CALEState_t* CALEClientGetStateInfo(const char* remoteAddress);
519 * check whether the remote address is connected or not.
520 * @param[in] remoteAddress remote address.
521 * @return true or false.
523 bool CALEClientIsConnectedDevice(const char* remoteAddress);
526 * check whether the remote address set CharacteristicNotification or not.
527 * @param[in] remoteAddress remote address.
528 * @return true or false.
530 bool CALEClientIsSetCharacteristic(const char* remoteAddress);
533 * create scan device list.
535 void CALEClientCreateDeviceList();
538 * update the counter which data is sent to remote device.
539 * @param[in] env JNI interface pointer.
541 void CALEClientUpdateSendCnt(JNIEnv *env);
545 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
547 CAResult_t CALEClientInitGattMutexVaraibles();
552 void CALEClientTerminateGattMutexVariables();
555 * set send finish flag.
556 * @param[in] flag finish flag.
558 void CALEClientSetSendFinishFlag(bool flag);
564 #endif /* CA_LECLIENT_H_ */