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 * @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 uint8_t *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 uint8_t *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 * send data for unicast (implement).
150 * @param[in] address remote address.
151 * @param[in] data data for transmission.
152 * @param[in] dataLen data length.
153 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
155 CAResult_t CALEClientSendUnicastMessageImpl(const char *address, const uint8_t *data,
156 const uint32_t dataLen);
159 * send data for multicast (implement).
160 * @param[in] env JNI interface pointer.
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 CALEClientSendMulticastMessageImpl(JNIEnv *env, const uint8_t *data,
166 const uint32_t dataLen);
169 * check whether it is connected or not with remote address.
170 * @param[in] address remote address.
171 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
173 CAResult_t CALECheckSendState(const char* address);
176 * send data to remote device.
177 * if it isn't connected yet. connect LE before try to send data.
178 * @param[in] env JNI interface pointer.
179 * @param[in] device bluetooth device object.
180 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
182 CAResult_t CALEClientSendData(JNIEnv *env, jobject device);
185 * get address from bluetooth gatt object.
186 * @param[in] env JNI interface pointer.
187 * @param[in] gatt Gatt profile object.
188 * @return bluetooth address.
190 jstring CALEClientGetAddressFromGattObj(JNIEnv *env, jobject gatt);
193 * get remote address from bluetooth socket object.
194 * @param[in] env JNI interface pointer.
195 * @param[in] bluetoothSocketObj bluetooth socket.
196 * @return bluetooth address.
198 jstring CALEClientGetRemoteAddress(JNIEnv *env, jobject bluetoothSocketObj);
202 * @param[in] env JNI interface pointer.
203 * @param[in] bluetoothGatt gatt profile object.
204 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
206 CAResult_t CALEClientGattClose(JNIEnv *env, jobject bluetoothGatt);
209 * start to scan whole bluetooth devices (interface).
210 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
212 CAResult_t CALEClientStartScan();
215 * start to scan whole bluetooth devices (implement).
216 * @param[in] env JNI interface pointer.
217 * @param[in] callback callback to receive device object by scanning.
218 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
220 CAResult_t CALEClientStartScanImpl(JNIEnv *env, jobject callback);
223 * start to scan target bluetooth devices for service uuid (implement).
224 * @param[in] env JNI interface pointer.
225 * @param[in] uuids target UUID.
226 * @param[in] callback callback to receive device object by scanning.
227 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
229 CAResult_t CALEClientStartScanWithUUIDImpl(JNIEnv *env, jobjectArray uuids,
234 * @param[in] env JNI interface pointer.
235 * @param[in] uuid uuid.
236 * @return uuid object.
238 jobject CALEClientGetUUIDObject(JNIEnv *env, const char *uuid);
241 * stop scan (interface).
242 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
244 CAResult_t CALEClientStopScan();
247 * stop scan (implement).
248 * @param[in] env JNI interface pointer.
249 * @param[in] callback callback to receive device object by scanning.
250 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
252 CAResult_t CALEClientStopScanImpl(JNIEnv *env, jobject callback);
255 * connect to gatt server hosted.
256 * @param[in] env JNI interface pointer.
257 * @param[in] bluetoothDevice bluetooth Device object.
258 * @param[in] autoconnect whether to directly connect to the remote device(false) or
259 * to automatically connect as soon as the remote device
261 * @param[in] callback callback for connection state change.
262 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
264 CAResult_t CALEClientConnect(JNIEnv *env, jobject bluetoothDevice, jboolean autoconnect,
268 * disconnect to gatt server by a target device.
269 * @param[in] env JNI interface pointer.
270 * @param[in] bluetoothGatt Gatt profile object.
271 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
273 CAResult_t CALEClientDisconnect(JNIEnv *env, jobject bluetoothGatt);
276 * disconnect to gatt server by whole devices.
277 * @param[in] env JNI interface pointer.
278 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
280 CAResult_t CALEClientDisconnectAll(JNIEnv *env);
283 * start discovery server.
284 * @param[in] env JNI interface pointer.
285 * @param[in] bluetoothGatt Gatt profile object.
286 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
288 CAResult_t CALEClientDiscoverServices(JNIEnv *env, jobject bluetoothGatt);
291 * create GattCharacteristic and call CALEClientWriteCharacteristicImpl
292 * for request to write gatt characteristic.
293 * @param[in] env JNI interface pointer.
294 * @param[in] gatt Gatt profile object.
295 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
297 CAResult_t CALEClientWriteCharacteristic(JNIEnv *env, jobject gatt);
300 * request to write gatt characteristic.
301 * @param[in] env JNI interface pointer.
302 * @param[in] bluetoothGatt Gatt profile object.
303 * @param[in] gattCharacteristic characteristic object that contain data to send.
304 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
306 CAResult_t CALEClientWriteCharacteristicImpl(JNIEnv *env, jobject bluetoothGatt,
307 jobject gattCharacteristic);
310 * request to read gatt characteristic.
311 * @param[in] env JNI interface pointer.
312 * @param[in] bluetoothGatt Gatt profile object.
313 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
315 CAResult_t CALEClientReadCharacteristic(JNIEnv *env, jobject bluetoothGatt);
318 * enable notification for a target device.
319 * @param[in] env JNI interface pointer.
320 * @param[in] bluetoothGatt Gatt profile object.
321 * @param[in] characteristic Characteristic object.
322 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
324 CAResult_t CALEClientSetCharacteristicNotification(JNIEnv *env, jobject bluetoothGatt,
325 jobject characteristic);
328 * create gatt characteristic object.
329 * @param[in] env JNI interface pointer.
330 * @param[in] bluetoothGatt Gatt profile object.
331 * @param[in] data for make Characteristic with data.
332 * @return Gatt Characteristic object.
334 jobject CALEClientCreateGattCharacteristic(JNIEnv *env, jobject bluetoothGatt, jbyteArray data);
338 * @param[in] env JNI interface pointer.
339 * @param[in] bluetoothGatt Gatt profile object.
340 * @param[in] characterUUID for make BluetoothGattCharacteristic object.
341 * @return Gatt Service.
343 jobject CALEClientGetGattService(JNIEnv *env, jobject bluetoothGatt, jstring characterUUID);
346 * get value from characteristic.
347 * @param[in] env JNI interface pointer.
348 * @param[in] characteristic Characteristic object.
349 * @return value in characteristic.
351 jbyteArray CALEClientGetValueFromCharacteristic(JNIEnv *env, jobject characteristic);
355 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
357 CAResult_t CALEClientCreateUUIDList();
360 * set UUID to descriptor.
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 CALEClientSetUUIDToDescriptor(JNIEnv *env, jobject bluetoothGatt,
367 jobject characteristic);
370 * add device object to scan device list.
371 * @param[in] env JNI interface pointer.
372 * @param[in] device bluetooth device object.
373 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
375 CAResult_t CALEClientAddScanDeviceToList(JNIEnv *env, jobject device);
378 * check whether the device exist in list or not.
379 * @param[in] env JNI interface pointer.
380 * @param[in] remoteAddress remote address.
381 * @return true or false.
383 bool CALEClientIsDeviceInScanDeviceList(JNIEnv *env, const char *remoteAddress);
386 * remove all devices in scan device list.
387 * @param[in] env JNI interface pointer.
388 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
390 CAResult_t CALEClientRemoveAllScanDevices(JNIEnv *env);
393 * remove target device in scan device list.
394 * @param[in] env JNI interface pointer.
395 * @param[in] remoteAddress remote address.
396 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
398 CAResult_t CALEClientRemoveDeviceInScanDeviceList(JNIEnv *env, jstring remoteAddress);
401 * add gatt object to gatt object list.
402 * @param[in] env JNI interface pointer.
403 * @param[in] gatt Gatt profile object.
404 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
406 CAResult_t CALEClientAddGattobjToList(JNIEnv *env, jobject gatt);
409 * check whether the gatt object exist in list or not.
410 * @param[in] env JNI interface pointer.
411 * @param[in] remoteAddress remote address.
412 * @return true or false.
414 bool CALEClientIsGattObjInList(JNIEnv *env, const char *remoteAddress);
417 * get the gatt object.
418 * @param[in] env JNI interface pointer.
419 * @param[in] remoteAddress remote address.
420 * @return gatt object.
422 jobject CALEClientGetGattObjInList(JNIEnv *env, const char* remoteAddress);
425 * remove all gatt objects in gatt object list.
426 * @param[in] env JNI interface pointer.
427 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
429 CAResult_t CALEClientRemoveAllGattObjs(JNIEnv *env);
432 * remove target device in gatt object list.
433 * @param[in] env JNI interface pointer.
434 * @param[in] gatt Gatt profile object.
435 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
437 CAResult_t CALEClientRemoveGattObj(JNIEnv *env, jobject gatt);
440 * remove gatt object of target device for address in gatt object list.
441 * @param[in] env JNI interface pointer.
442 * @param[in] gatt Gatt profile object.
443 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
445 CAResult_t CALEClientRemoveGattObjForAddr(JNIEnv *env, jstring addr);
448 * update new state information.
449 * @param[in] address remote address.
450 * @param[in] connectedState connection state.
451 * @param[in] notificationState whether characteristic notification already set or not.
452 * @param[in] sendState whether sending was success or not.
453 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
455 CAResult_t CALEClientUpdateDeviceState(const char* address, uint32_t connectedState,
456 uint16_t notificationState, uint16_t sendState);
459 * add new state to state list.
460 * @param[in] state new state.
461 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
463 CAResult_t CALEClientAddDeviceStateToList(CALEState_t* state);
466 * check whether the remote address is existed or not.
467 * @param[in] address remote address.
468 * @return true or false.
470 bool CALEClientIsDeviceInList(const char *remoteAddress);
473 * remove all device states.
474 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
476 CAResult_t CALEClientRemoveAllDeviceState();
479 * remove the device state for a remote device.
480 * @param[in] remoteAddress remote address.
481 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
483 CAResult_t CALEClientRemoveDeviceState(const char* remoteAddress);
486 * get state information for a remote device.
487 * @param[in] remoteAddress remote address.
488 * @return CALEState_t.
490 CALEState_t* CALEClientGetStateInfo(const char* remoteAddress);
493 * check whether the remote address is connected or not.
494 * @param[in] remoteAddress remote address.
495 * @return true or false.
497 bool CALEClientIsConnectedDevice(const char* remoteAddress);
500 * check whether the remote address set CharacteristicNotification or not.
501 * @param[in] remoteAddress remote address.
502 * @return true or false.
504 bool CALEClientIsSetCharacteristic(const char* remoteAddress);
507 * create scan device list.
509 void CALEClientCreateDeviceList();
512 * update the counter which data is sent to remote device.
513 * @param[in] env JNI interface pointer.
515 void CALEClientUpdateSendCnt(JNIEnv *env);
519 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
521 CAResult_t CALEClientInitGattMutexVaraibles();
526 void CALEClientTerminateGattMutexVariables();
529 * set send finish flag.
530 * @param[in] flag finish flag.
532 void CALEClientSetSendFinishFlag(bool flag);
538 #endif /* CA_LECLIENT_H_ */