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 char *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 * @param[in] handle thread pool handle object.
84 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
86 CAResult_t CALEClientInitialize(ca_thread_pool_t handle);
89 * terminate client for BLE.
91 void CALEClientTerminate();
94 * for destroy sending routine.
95 * @param[in] env JNI interface pointer.
96 * @param[in] gatt Gatt profile object.
98 void CALEClientSendFinish(JNIEnv *env, jobject gatt);
101 * send data for unicast (interface).
102 * @param[in] address remote address.
103 * @param[in] data data for transmission.
104 * @param[in] dataLen data length.
105 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
107 CAResult_t CALEClientSendUnicastMessage(const char *address, const char *data,
108 const uint32_t dataLen);
111 * send data for multicast (interface).
112 * @param[in] data data for transmission.
113 * @param[in] dataLen data length.
114 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
116 CAResult_t CALEClientSendMulticastMessage(const char *data, const uint32_t dataLen);
119 * start unicast server.
120 * @param[in] address remote address.
121 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
123 CAResult_t CALEClientStartUnicastServer(const char *address);
126 * start multicast server (start discovery).
127 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
129 CAResult_t CALEClientStartMulticastServer();
132 * stop unicast server.
134 void CALEClientStopUnicastServer();
137 * stop multicast server (stop discovery).
139 void CALEClientStopMulticastServer();
142 * set this callback for receiving data packets from peer devices.
143 * @param[in] callback callback to be notified on reception of
144 * unicast/multicast data packets.
146 void CALEClientSetCallback(CAPacketReceiveCallback callback);
149 * get local address (implement).
150 * @param[out] address local address.
151 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
153 CAResult_t CALEClientGetLocalAddress(char** address);
156 * send data for unicast (implement).
157 * @param[in] address remote address.
158 * @param[in] data data for transmission.
159 * @param[in] dataLen data length.
160 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
162 CAResult_t CALEClientSendUnicastMessageImpl(const char *address, const char *data,
163 const uint32_t dataLen);
166 * send data for multicast (implement).
167 * @param[in] env JNI interface pointer.
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 CALEClientSendMulticastMessageImpl(JNIEnv *env, const char *data,
173 const uint32_t dataLen);
176 * check whether it is connected or not with remote address.
177 * @param[in] address remote address.
178 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
180 CAResult_t CALECheckSendState(const char* address);
183 * send data to remote device.
184 * if it isn't connected yet. connect LE before try to send data.
185 * @param[in] env JNI interface pointer.
186 * @param[in] device bluetooth device object.
187 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
189 CAResult_t CALEClientSendData(JNIEnv *env, jobject device);
192 * get address from bluetooth gatt object.
193 * @param[in] env JNI interface pointer.
194 * @param[in] gatt Gatt profile object.
195 * @return bluetooth address.
197 jstring CALEClientGetAddressFromGattObj(JNIEnv *env, jobject gatt);
200 * get remote address from bluetooth socket object.
201 * @param[in] env JNI interface pointer.
202 * @param[in] bluetoothSocketObj bluetooth socket.
203 * @return bluetooth address.
205 jstring CALEClientGetRemoteAddress(JNIEnv *env, jobject bluetoothSocketObj);
209 * @param[in] env JNI interface pointer.
210 * @param[in] bluetoothGatt gatt profile object.
211 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
213 CAResult_t CALEClientGattClose(JNIEnv *env, jobject bluetoothGatt);
216 * start to scan whole bluetooth devices (interface).
217 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
219 CAResult_t CALEClientStartScan();
222 * start to scan whole bluetooth devices (implement).
223 * @param[in] env JNI interface pointer.
224 * @param[in] callback callback to receive device object by scanning.
225 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
227 CAResult_t CALEClientStartScanImpl(JNIEnv *env, jobject callback);
230 * start to scan target bluetooth devices for service uuid (implement).
231 * @param[in] env JNI interface pointer.
232 * @param[in] uuids target UUID.
233 * @param[in] callback callback to receive device object by scanning.
234 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
236 CAResult_t CALEClientStartScanWithUUIDImpl(JNIEnv *env, jobjectArray uuids,
241 * @param[in] env JNI interface pointer.
242 * @param[in] uuid uuid.
243 * @return uuid object.
245 jobject CALEClientGetUUIDObject(JNIEnv *env, const char *uuid);
248 * stop scan (interface).
249 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
251 CAResult_t CALEClientStopScan();
254 * stop scan (implement).
255 * @param[in] env JNI interface pointer.
256 * @param[in] callback callback to receive device object by scanning.
257 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
259 CAResult_t CALEClientStopScanImpl(JNIEnv *env, jobject callback);
262 * connect to gatt server hosted.
263 * @param[in] env JNI interface pointer.
264 * @param[in] bluetoothDevice bluetooth Device object.
265 * @param[in] autoconnect whether to directly connect to the remote device(false) or
266 * to automatically connect as soon as the remote device
268 * @param[in] callback callback for connection state change.
269 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
271 CAResult_t CALEClientConnect(JNIEnv *env, jobject bluetoothDevice, jboolean autoconnect,
275 * disconnect to gatt server by a target device.
276 * @param[in] env JNI interface pointer.
277 * @param[in] bluetoothGatt Gatt profile object.
278 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
280 CAResult_t CALEClientDisconnect(JNIEnv *env, jobject bluetoothGatt);
283 * disconnect to gatt server by whole devices.
284 * @param[in] env JNI interface pointer.
285 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
287 CAResult_t CALEClientDisconnectAll(JNIEnv *env);
290 * start discovery server.
291 * @param[in] env JNI interface pointer.
292 * @param[in] bluetoothGatt Gatt profile object.
293 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
295 CAResult_t CALEClientDiscoverServices(JNIEnv *env, jobject bluetoothGatt);
298 * create GattCharacteristic and call CALEClientWriteCharacteristicImpl
299 * for request to write gatt characteristic.
300 * @param[in] env JNI interface pointer.
301 * @param[in] gatt Gatt profile object.
302 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
304 CAResult_t CALEClientWriteCharacteristic(JNIEnv *env, jobject gatt);
307 * request to write gatt characteristic.
308 * @param[in] env JNI interface pointer.
309 * @param[in] bluetoothGatt Gatt profile object.
310 * @param[in] gattCharacteristic characteristic object that contain data to send.
311 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
313 CAResult_t CALEClientWriteCharacteristicImpl(JNIEnv *env, jobject bluetoothGatt,
314 jobject gattCharacteristic);
317 * request to read gatt characteristic.
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 CALEClientReadCharacteristic(JNIEnv *env, jobject bluetoothGatt);
325 * enable notification for a target device.
326 * @param[in] env JNI interface pointer.
327 * @param[in] bluetoothGatt Gatt profile object.
328 * @param[in] characteristic Characteristic object.
329 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
331 CAResult_t CALEClientSetCharacteristicNotification(JNIEnv *env, jobject bluetoothGatt,
332 jobject characteristic);
335 * create gatt characteristic object.
336 * @param[in] env JNI interface pointer.
337 * @param[in] bluetoothGatt Gatt profile object.
338 * @param[in] data for make Characteristic with data.
339 * @return Gatt Characteristic object.
341 jobject CALEClientCreateGattCharacteristic(JNIEnv *env, jobject bluetoothGatt, jbyteArray data);
345 * @param[in] env JNI interface pointer.
346 * @param[in] bluetoothGatt Gatt profile object.
347 * @param[in] characterUUID for make BluetoothGattCharacteristic object.
348 * @return Gatt Service.
350 jobject CALEClientGetGattService(JNIEnv *env, jobject bluetoothGatt, jstring characterUUID);
353 * get value from characteristic.
354 * @param[in] env JNI interface pointer.
355 * @param[in] characteristic Characteristic object.
356 * @return value in characteristic.
358 jbyteArray CALEClientGetValueFromCharacteristic(JNIEnv *env, jobject characteristic);
362 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
364 CAResult_t CALEClientCreateUUIDList();
367 * set UUID to descriptor.
368 * @param[in] env JNI interface pointer.
369 * @param[in] bluetoothGatt Gatt profile object.
370 * @param[in] characteristic Characteristic object.
371 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
373 CAResult_t CALEClientSetUUIDToDescriptor(JNIEnv *env, jobject bluetoothGatt,
374 jobject characteristic);
377 * add device object to scan device list.
378 * @param[in] env JNI interface pointer.
379 * @param[in] device bluetooth device object.
380 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
382 CAResult_t CALEClientAddScanDeviceToList(JNIEnv *env, jobject device);
385 * check whether the device exist in list or not.
386 * @param[in] env JNI interface pointer.
387 * @param[in] remoteAddress remote address.
388 * @return true or false.
390 bool CALEClientIsDeviceInScanDeviceList(JNIEnv *env, const char *remoteAddress);
393 * remove all devices in scan device list.
394 * @param[in] env JNI interface pointer.
395 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
397 CAResult_t CALEClientRemoveAllScanDevices(JNIEnv *env);
400 * remove target device in scan device list.
401 * @param[in] env JNI interface pointer.
402 * @param[in] remoteAddress remote address.
403 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
405 CAResult_t CALEClientRemoveDeviceInScanDeviceList(JNIEnv *env, jstring remoteAddress);
408 * add gatt object to gatt object list.
409 * @param[in] env JNI interface pointer.
410 * @param[in] gatt Gatt profile object.
411 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
413 CAResult_t CALEClientAddGattobjToList(JNIEnv *env, jobject gatt);
416 * check whether the gatt object exist in list or not.
417 * @param[in] env JNI interface pointer.
418 * @param[in] remoteAddress remote address.
419 * @return true or false.
421 bool CALEClientIsGattObjInList(JNIEnv *env, const char *remoteAddress);
424 * get the gatt object.
425 * @param[in] env JNI interface pointer.
426 * @param[in] remoteAddress remote address.
427 * @return gatt object.
429 jobject CALEClientGetGattObjInList(JNIEnv *env, const char* remoteAddress);
432 * remove all gatt objects in gatt object 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 CALEClientRemoveAllGattObjs(JNIEnv *env);
439 * remove target device in gatt object list.
440 * @param[in] env JNI interface pointer.
441 * @param[in] gatt Gatt profile object.
442 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
444 CAResult_t CALEClientRemoveGattObj(JNIEnv *env, jobject gatt);
447 * remove gatt object of target device for address in 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 CALEClientRemoveGattObjForAddr(JNIEnv *env, jstring addr);
455 * update new state information.
456 * @param[in] address remote address.
457 * @param[in] connectedState connection state.
458 * @param[in] notificationState whether characteristic notification already set or not.
459 * @param[in] sendState whether sending was success or not.
460 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
462 CAResult_t CALEClientUpdateDeviceState(const char* address, uint32_t connectedState,
463 uint16_t notificationState, uint16_t sendState);
466 * add new state to state list.
467 * @param[in] state new state.
468 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
470 CAResult_t CALEClientAddDeviceStateToList(CALEState_t* state);
473 * check whether the remote address is existed or not.
474 * @param[in] address remote address.
475 * @return true or false.
477 bool CALEClientIsDeviceInList(const char *remoteAddress);
480 * remove all device states.
481 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
483 CAResult_t CALEClientRemoveAllDeviceState();
486 * remove the device state for a remote device.
487 * @param[in] remoteAddress remote address.
488 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
490 CAResult_t CALEClientRemoveDeviceState(const char* remoteAddress);
493 * get state information for a remote device.
494 * @param[in] remoteAddress remote address.
495 * @return CALEState_t.
497 CALEState_t* CALEClientGetStateInfo(const char* remoteAddress);
500 * check whether the remote address is connected or not.
501 * @param[in] remoteAddress remote address.
502 * @return true or false.
504 bool CALEClientIsConnectedDevice(const char* remoteAddress);
507 * check whether the remote address set CharacteristicNotification or not.
508 * @param[in] remoteAddress remote address.
509 * @return true or false.
511 bool CALEClientIsSetCharacteristic(const char* remoteAddress);
514 * create scan device list.
516 void CALEClientCreateDeviceList();
519 * Reordering for device state list.
520 * @param[in] index index of device list that want to reordering.
521 * @param[in] list the list to reorder.
522 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
524 CAResult_t CALEClientReorderingList(uint32_t index, u_arraylist_t *list);
527 * update the counter which data is sent to remote device.
528 * @param[in] env JNI interface pointer.
530 void CALEClientUpdateSendCnt(JNIEnv *env);
534 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
536 CAResult_t CALEClientInitGattMutexVaraibles();
541 void CALEClientTerminateGattMutexVariables();
544 * set send finish flag.
545 * @param[in] flag finish flag.
547 void CALEClientSetSendFinishFlag(bool flag);
553 #endif /* CA_LECLIENT_H_ */