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 * @brief 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 uint32_t STATE_CONNECTED = 2;
47 static const uint32_t STATE_DISCONNECTED = 0;
49 typedef struct le_state_info
51 char address[CA_MACADDR_SIZE];
52 uint32_t connectedState;
53 uint16_t notificationState;
58 * @brief Callback to be notified on reception of any data from remote devices.
59 * @param address [IN] MAC address of remote device.
60 * @param data [IN] Data received from remote device.
62 * @pre Callback must be registered using CALESetCallback(CAPacketReceiveCallback callback)
64 typedef void (*CAPacketReceiveCallback)(const char *address, const char *data);
67 * @brief initialize JNI object
70 void CALEClientJniInit();
73 * @brief set context of application
76 void CALEClientJNISetContext();
79 * @brief 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 * @brief initialize client for BLE
86 * @param handle [IN] thread pool handle object
87 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
89 CAResult_t CALEClientInitialize(ca_thread_pool_t handle);
92 * @brief terminate client for BLE
95 void CALEClientTerminate();
98 * @brief for destroy sending routine
99 * @param env [IN] JNI interface pointer
100 * @param gatt [IN] Gatt profile object
103 void CALEClientSendFinish(JNIEnv *env, jobject gatt);
106 * @brief send data for unicast (interface)
107 * @param address [IN] remote address
108 * @param data [IN] data for transmission
109 * @param dataLen [IN] 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 char *data,
113 const uint32_t dataLen);
116 * @brief send data for multicast (interface)
117 * @param data [IN] data for transmission
118 * @param dataLen [IN] data length
119 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
121 CAResult_t CALEClientSendMulticastMessage(const char *data, const uint32_t dataLen);
124 * @brief start unicast server
125 * @param address [IN] 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 * @brief 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 * @brief stop unicast server
140 void CALEClientStopUnicastServer();
143 * @brief stop multicast server (stop discovery)
146 void CALEClientStopMulticastServer();
149 * @brief set this callback for receiving data packets from peer devices.
150 * @param callback [IN] callback to be notified on reception of
151 * unicast/multicast data packets.
154 void CALEClientSetCallback(CAPacketReceiveCallback callback);
157 * @brief get local address (interface)
158 * @param address [OUT] local address
159 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
161 CAResult_t CALEClientGetInterfaceInfo(char **address);
164 * @brief get local address (implement)
165 * @param address [OUT] local address
166 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
168 CAResult_t CALEClientGetLocalAddress(char** address);
171 * @brief send data for unicast (implement)
172 * @param address [IN] remote address
173 * @param data [IN] data for transmission
174 * @param dataLen [IN] data length
175 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
177 CAResult_t CALEClientSendUnicastMessageImpl(const char *address, const char *data,
178 const uint32_t dataLen);
181 * @brief send data for multicast (implement)
182 * @param env [IN] JNI interface pointer
183 * @param data [IN] data for transmission
184 * @param dataLen [IN] data length
185 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
187 CAResult_t CALEClientSendMulticastMessageImpl(JNIEnv *env, const char *data,
188 const uint32_t dataLen);
191 * @brief check whether it is connected or not with remote address.
192 * @param address [IN] remote address
193 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
195 CAResult_t CALECheckSendState(const char* address);
198 * @brief send data to remote device.
199 * if it isn't connected yet. connect LE before try to send data
200 * @param env [IN] JNI interface pointer
201 * @param device [IN] bluetooth device object
202 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
204 CAResult_t CALEClientSendData(JNIEnv *env, jobject device);
207 * @brief get address from bluetooth gatt object
208 * @param env [IN] JNI interface pointer
209 * @param gatt [IN] Gatt profile object
210 * @return bluetooth address
212 jstring CALEClientGetAddressFromGattObj(JNIEnv *env, jobject gatt);
215 * @brief get remote address from bluetooth socket object
216 * @param env [IN] JNI interface pointer
217 * @param bluetoothSocketObj [IN] bluetooth socket
218 * @return bluetooth address
220 jstring CALEClientGetRemoteAddress(JNIEnv *env, jobject bluetoothSocketObj);
224 * @param env [IN] JNI interface pointer
225 * @param bluetoothGatt [IN] gatt profile object
226 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
228 CAResult_t CALEClientGattClose(JNIEnv *env, jobject bluetoothGatt);
231 * @brief start to scan whole bluetooth devices (interface)
232 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
234 CAResult_t CALEClientStartScan();
237 * @brief start to scan whole bluetooth devices (implement)
238 * @param env [IN] JNI interface pointer
239 * @param callback [IN] callback to receive device object by scanning
240 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
242 CAResult_t CALEClientStartScanImpl(JNIEnv *env, jobject callback);
245 * @brief start to scan target bluetooth devices for service uuid (implement)
246 * @param env [IN] JNI interface pointer
247 * @param uuids [IN] target UUID
248 * @param callback [IN] callback to receive device object by scanning
249 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
251 CAResult_t CALEClientStartScanWithUUIDImpl(JNIEnv *env, jobjectArray uuids,
255 * @brief get uuid object
256 * @param env [IN] JNI interface pointer
257 * @param uuid [IN] uuid
258 * @return uuid object
260 jobject CALEClientGetUUIDObject(JNIEnv *env, const char *uuid);
263 * @brief stop scan (interface)
264 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
266 CAResult_t CALEClientStopScan();
269 * @brief stop scan (implement)
270 * @param env [IN] JNI interface pointer
271 * @param callback [IN] callback to receive device object by scanning
272 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
274 CAResult_t CALEClientStopScanImpl(JNIEnv *env, jobject callback);
277 * @brief connect to gatt server hosted
278 * @param env [IN] JNI interface pointer
279 * @param bluetoothDevice [IN] bluetooth Device object
280 * @param autoconnect [IN] whether to directly connect to the remote device(false) or
281 * to automatically connect as soon as the remote device
283 * @param callback [IN] callback for connection state change
284 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
286 CAResult_t CALEClientConnect(JNIEnv *env, jobject bluetoothDevice, jboolean autoconnect,
290 * @brief disconnect to gatt server by a target device
291 * @param env [IN] JNI interface pointer
292 * @param bluetoothGatt [IN] Gatt profile object
293 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
295 CAResult_t CALEClientDisconnect(JNIEnv *env, jobject bluetoothGatt);
298 * @brief disconnect to gatt server by whole devices
299 * @param env [IN] JNI interface pointer
300 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
302 CAResult_t CALEClientDisconnectAll(JNIEnv *env);
305 * @brief start discovery server
306 * @param env [IN] JNI interface pointer
307 * @param bluetoothGatt [IN] Gatt profile object
308 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
310 CAResult_t CALEClientDiscoverServices(JNIEnv *env, jobject bluetoothGatt);
313 * @brief create GattCharacteristic and call CALEClientWriteCharacteristicImpl
314 * for request to write gatt characteristic
315 * @param env [IN] JNI interface pointer
316 * @param gatt [IN] Gatt profile object
317 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
319 CAResult_t CALEClientWriteCharacteristic(JNIEnv *env, jobject gatt);
322 * @brief request to write gatt characteristic
323 * @param env [IN] JNI interface pointer
324 * @param bluetoothGatt [IN] Gatt profile object
325 * @param gattCharacteristic [IN] characteristic object that contain data to send
326 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
328 CAResult_t CALEClientWriteCharacteristicImpl(JNIEnv *env, jobject bluetoothGatt,
329 jobject gattCharacteristic);
332 * @brief request to read gatt characteristic
333 * @param env [IN] JNI interface pointer
334 * @param bluetoothGatt [IN] Gatt profile object
335 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
337 CAResult_t CALEClientReadCharacteristic(JNIEnv *env, jobject bluetoothGatt);
340 * @brief enable notification for a target device
341 * @param env [IN] JNI interface pointer
342 * @param bluetoothGatt [IN] Gatt profile object
343 * @param characteristic [IN] Characteristic object
344 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
346 CAResult_t CALEClientSetCharacteristicNotification(JNIEnv *env, jobject bluetoothGatt,
347 jobject characteristic);
350 * @brief create gatt characteristic object
351 * @param env [IN] JNI interface pointer
352 * @param bluetoothGatt [IN] Gatt profile object
353 * @param data [IN] for make Characteristic with data
354 * @return Gatt Characteristic object
356 jobject CALEClientCreateGattCharacteristic(JNIEnv *env, jobject bluetoothGatt, jbyteArray data);
359 * @brief get gatt service
360 * @param env [IN] JNI interface pointer
361 * @param bluetoothGatt [IN] Gatt profile object
362 * @param characterUUID [IN] for make BluetoothGattCharacteristic object
363 * @return Gatt Service
365 jobject CALEClientGetGattService(JNIEnv *env, jobject bluetoothGatt, jstring characterUUID);
368 * @brief get value from characteristic
369 * @param env [IN] JNI interface pointer
370 * @param characteristic [IN] Characteristic object
371 * @return value in characteristic
373 jbyteArray CALEClientGetValueFromCharacteristic(JNIEnv *env, jobject characteristic);
376 * @brief create UUID List
377 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
379 CAResult_t CALEClientCreateUUIDList();
382 * @brief set UUID to descriptor
383 * @param env [IN] JNI interface pointer
384 * @param bluetoothGatt [IN] Gatt profile object
385 * @param characteristic [IN] Characteristic object
386 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
388 CAResult_t CALEClientSetUUIDToDescriptor(JNIEnv *env, jobject bluetoothGatt,
389 jobject characteristic);
392 * BluetoothDevice List
395 * @brief add device object to scan device list
396 * @param env [IN] JNI interface pointer
397 * @param device [IN] bluetooth device object
398 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
400 CAResult_t CALEClientAddScanDeviceToList(JNIEnv *env, jobject device);
403 * @brief check whether the device exist in list or not
404 * @param env [IN] JNI interface pointer
405 * @param remoteAddress [IN] remote address
406 * @return true or false
408 bool CALEClientIsDeviceInScanDeviceList(JNIEnv *env, const char *remoteAddress);
411 * @brief remove all devices in scan device list
412 * @param env [IN] JNI interface pointer
413 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
415 CAResult_t CALEClientRemoveAllScanDevices(JNIEnv *env);
418 * @brief remove target device in scan device list
419 * @param env [IN] JNI interface pointer
420 * @param remoteAddress [IN] remote address
421 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
423 CAResult_t CALEClientRemoveDeviceInScanDeviceList(JNIEnv *env, jstring remoteAddress);
430 * @brief add gatt object to gatt object list
431 * @param env [IN] JNI interface pointer
432 * @param gatt [IN] Gatt profile object
433 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
435 CAResult_t CALEClientAddGattobjToList(JNIEnv *env, jobject gatt);
438 * @brief check whether the gatt object exist in list or not
439 * @param env [IN] JNI interface pointer
440 * @param remoteAddress [IN] remote address
441 * @return true or false
443 bool CALEClientIsGattObjInList(JNIEnv *env, const char *remoteAddress);
446 * @brief get the gatt object
447 * @param env [IN] JNI interface pointer
448 * @param remoteAddress [IN] remote address
449 * @return gatt object
451 jobject CALEClientGetGattObjInList(JNIEnv *env, const char* remoteAddress);
454 * @brief remove all gatt objects in gatt object list
455 * @param env [IN] JNI interface pointer
456 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
458 CAResult_t CALEClientRemoveAllGattObjs(JNIEnv *env);
461 * @brief remove target device in gatt object list
462 * @param env [IN] JNI interface pointer
463 * @param gatt [IN] Gatt profile object
464 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
466 CAResult_t CALEClientRemoveGattObj(JNIEnv *env, jobject gatt);
469 * @brief remove gatt object of target device for address in gatt object list
470 * @param env [IN] JNI interface pointer
471 * @param gatt [IN] Gatt profile object
472 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
474 CAResult_t CALEClientRemoveGattObjForAddr(JNIEnv *env, jstring addr);
481 * @brief update new state information
482 * @param address [IN] remote address
483 * @param connectedState [IN] connection state
484 * @param notificationState [IN] whether characteristic notification already set or not
485 * @param sendState [IN] whether sending was success or not
486 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
488 CAResult_t CALEClientUpdateDeviceState(const char* address, uint32_t connectedState,
489 uint16_t notificationState, uint16_t sendState);
492 * @brief add new state to state list
493 * @param state [IN] new state
494 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
496 CAResult_t CALEClientAddDeviceStateToList(CALEState_t* state);
499 * @brief check whether the remote address is existed or not.
500 * @param address [IN] remote address
501 * @return true or false
503 bool CALEClientIsDeviceInList(const char *remoteAddress);
506 * @brief remove all device states
507 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
509 CAResult_t CALEClientRemoveAllDeviceState();
512 * @brief remove the device state for a remote device
513 * @param remoteAddress [IN] remote address
514 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
516 CAResult_t CALEClientRemoveDeviceState(const char* remoteAddress);
519 * @brief get state information for a remote device
520 * @param remoteAddress [IN] remote address
521 * @return CALEState_t
523 CALEState_t* CALEClientGetStateInfo(const char* remoteAddress);
526 * @brief check whether the remote address is connected or not.
527 * @param remoteAddress [IN] remote address
528 * @return true or false
530 bool CALEClientIsConnectedDevice(const char* remoteAddress);
533 * @brief check whether the remote address set CharacteristicNotification or not
534 * @param remoteAddress [IN] remote address
535 * @return true or false
537 bool CALEClientIsSetCharacteristic(const char* remoteAddress);
540 * @brief create scan device list
543 void CALEClientCreateDeviceList();
546 * @brief Reordering for device state list
547 * @param index [IN] index of device list that want to reordering
548 * @param list [IN] the list to reorder
549 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
551 CAResult_t CALEClientReorderingList(uint32_t index, u_arraylist_t *list);
554 * @brief update the counter which data is sent to remote device
555 * @param env [IN] JNI interface pointer
558 void CALEClientUpdateSendCnt(JNIEnv *env);
561 * @brief initialize mutex
562 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h)
564 CAResult_t CALEClientInitGattMutexVaraibles();
567 * @brief terminate mutex
570 void CALEClientTerminateGattMutexVariables();
573 * @brief set send finish flag
574 * @param flag [IN] flag
577 void CALEClientSetSendFinishFlag(bool flag);
580 * @brief set the flag whether WriteCharacteristicCB is called
581 * @param flag [IN] flag
584 void CALEClientSetWriteCharacteristicCBFlag(bool flag);
587 * @brief set the flag whether Send Request is called
588 * @param flag [IN] flag
591 void CALEClientSetTheSendRequestFlag(bool flag);