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 * destroy interface object and terminate the interface.
95 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
97 CAResult_t CALEClientDestroyJniInterface();
100 * for destroy sending routine.
101 * @param[in] env JNI interface pointer.
102 * @param[in] gatt Gatt profile object.
104 void CALEClientSendFinish(JNIEnv *env, jobject gatt);
107 * send data for unicast (interface).
108 * @param[in] address remote address.
109 * @param[in] data data for transmission.
110 * @param[in] dataLen data length.
111 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
113 CAResult_t CALEClientSendUnicastMessage(const char *address, const uint8_t *data,
114 const uint32_t dataLen);
117 * send data for multicast (interface).
118 * @param[in] data data for transmission.
119 * @param[in] dataLen data length.
120 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
122 CAResult_t CALEClientSendMulticastMessage(const uint8_t *data, const uint32_t dataLen);
125 * start unicast server.
126 * @param[in] address remote address.
127 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
129 CAResult_t CALEClientStartUnicastServer(const char *address);
132 * start multicast server (start discovery).
133 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
135 CAResult_t CALEClientStartMulticastServer();
138 * stop unicast server.
140 void CALEClientStopUnicastServer();
143 * stop multicast server (stop discovery).
145 void CALEClientStopMulticastServer();
148 * set this callback for receiving data packets from peer devices.
149 * @param[in] callback callback to be notified on reception of
150 * unicast/multicast data packets.
152 void CALEClientSetCallback(CAPacketReceiveCallback callback);
155 * send data for unicast (implement).
156 * @param[in] address remote address.
157 * @param[in] data data for transmission.
158 * @param[in] dataLen data length.
159 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
161 CAResult_t CALEClientSendUnicastMessageImpl(const char *address, const uint8_t *data,
162 const uint32_t dataLen);
165 * send data for multicast (implement).
166 * @param[in] env JNI interface pointer.
167 * @param[in] data data for transmission.
168 * @param[in] dataLen data length.
169 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
171 CAResult_t CALEClientSendMulticastMessageImpl(JNIEnv *env, const uint8_t *data,
172 const uint32_t dataLen);
175 * check whether it is connected or not with remote address.
176 * @param[in] address remote address.
177 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
179 CAResult_t CALECheckSendState(const char* address);
182 * send data to remote device.
183 * if it isn't connected yet. connect LE before try to send data.
184 * @param[in] env JNI interface pointer.
185 * @param[in] device bluetooth device object.
186 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
188 CAResult_t CALEClientSendData(JNIEnv *env, jobject device);
191 * get address from bluetooth gatt object.
192 * @param[in] env JNI interface pointer.
193 * @param[in] gatt Gatt profile object.
194 * @return bluetooth address.
196 jstring CALEClientGetAddressFromGattObj(JNIEnv *env, jobject gatt);
199 * get remote address from bluetooth socket object.
200 * @param[in] env JNI interface pointer.
201 * @param[in] bluetoothSocketObj bluetooth socket.
202 * @return bluetooth address.
204 jstring CALEClientGetRemoteAddress(JNIEnv *env, jobject bluetoothSocketObj);
208 * @param[in] env JNI interface pointer.
209 * @param[in] bluetoothGatt gatt profile object.
210 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
212 CAResult_t CALEClientGattClose(JNIEnv *env, jobject bluetoothGatt);
215 * start to scan whole bluetooth devices (interface).
216 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
218 CAResult_t CALEClientStartScan();
221 * start to scan whole bluetooth devices (implement).
222 * @param[in] env JNI interface pointer.
223 * @param[in] callback callback to receive device object by scanning.
224 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
226 CAResult_t CALEClientStartScanImpl(JNIEnv *env, jobject callback);
229 * start to scan target bluetooth devices for service uuid (implement).
230 * @param[in] env JNI interface pointer.
231 * @param[in] uuids target UUID.
232 * @param[in] callback callback to receive device object by scanning.
233 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
235 CAResult_t CALEClientStartScanWithUUIDImpl(JNIEnv *env, jobjectArray uuids,
240 * @param[in] env JNI interface pointer.
241 * @param[in] uuid uuid.
242 * @return uuid object.
244 jobject CALEClientGetUUIDObject(JNIEnv *env, const char *uuid);
247 * stop scan (interface).
248 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
250 CAResult_t CALEClientStopScan();
253 * set ble scanning flag.
254 * @param[in] flag scan flag.
256 void CALEClientSetScanFlag(bool flag);
259 * stop scan (implement).
260 * @param[in] env JNI interface pointer.
261 * @param[in] callback callback to receive device object by scanning.
262 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
264 CAResult_t CALEClientStopScanImpl(JNIEnv *env, jobject callback);
267 * connect to gatt server hosted.
268 * @param[in] env JNI interface pointer.
269 * @param[in] bluetoothDevice bluetooth Device object.
270 * @param[in] autoconnect whether to directly connect to the remote device(false) or
271 * to automatically connect as soon as the remote device
273 * @param[in] callback callback for connection state change.
274 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
276 CAResult_t CALEClientConnect(JNIEnv *env, jobject bluetoothDevice, jboolean autoconnect,
280 * disconnect to gatt server by a target device.
281 * @param[in] env JNI interface pointer.
282 * @param[in] bluetoothGatt Gatt profile object.
283 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
285 CAResult_t CALEClientDisconnect(JNIEnv *env, jobject bluetoothGatt);
288 * disconnect to gatt server by whole devices.
289 * @param[in] env JNI interface pointer.
290 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
292 CAResult_t CALEClientDisconnectAll(JNIEnv *env);
295 * start discovery server.
296 * @param[in] env JNI interface pointer.
297 * @param[in] bluetoothGatt Gatt profile object.
298 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
300 CAResult_t CALEClientDiscoverServices(JNIEnv *env, jobject bluetoothGatt);
303 * create GattCharacteristic and call CALEClientWriteCharacteristicImpl
304 * for request to write gatt characteristic.
305 * @param[in] env JNI interface pointer.
306 * @param[in] gatt Gatt profile object.
307 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
309 CAResult_t CALEClientWriteCharacteristic(JNIEnv *env, jobject gatt);
312 * request to write gatt characteristic.
313 * @param[in] env JNI interface pointer.
314 * @param[in] bluetoothGatt Gatt profile object.
315 * @param[in] gattCharacteristic characteristic object that contain data to send.
316 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
318 CAResult_t CALEClientWriteCharacteristicImpl(JNIEnv *env, jobject bluetoothGatt,
319 jobject gattCharacteristic);
322 * request to read gatt characteristic.
323 * @param[in] env JNI interface pointer.
324 * @param[in] bluetoothGatt Gatt profile object.
325 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
327 CAResult_t CALEClientReadCharacteristic(JNIEnv *env, jobject bluetoothGatt);
330 * enable notification for a target device.
331 * @param[in] env JNI interface pointer.
332 * @param[in] bluetoothGatt Gatt profile object.
333 * @param[in] characteristic Characteristic object.
334 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
336 CAResult_t CALEClientSetCharacteristicNotification(JNIEnv *env, jobject bluetoothGatt,
337 jobject characteristic);
340 * create gatt characteristic object.
341 * @param[in] env JNI interface pointer.
342 * @param[in] bluetoothGatt Gatt profile object.
343 * @param[in] data for make Characteristic with data.
344 * @return Gatt Characteristic object.
346 jobject CALEClientCreateGattCharacteristic(JNIEnv *env, jobject bluetoothGatt, jbyteArray data);
350 * @param[in] env JNI interface pointer.
351 * @param[in] bluetoothGatt Gatt profile object.
352 * @param[in] characterUUID for make BluetoothGattCharacteristic object.
353 * @return Gatt Service.
355 jobject CALEClientGetGattService(JNIEnv *env, jobject bluetoothGatt, jstring characterUUID);
358 * get value from characteristic.
359 * @param[in] env JNI interface pointer.
360 * @param[in] characteristic Characteristic object.
361 * @return value in characteristic.
363 jbyteArray CALEClientGetValueFromCharacteristic(JNIEnv *env, jobject characteristic);
367 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
369 CAResult_t CALEClientCreateUUIDList();
372 * set UUID to descriptor.
373 * @param[in] env JNI interface pointer.
374 * @param[in] bluetoothGatt Gatt profile object.
375 * @param[in] characteristic Characteristic object.
376 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
378 CAResult_t CALEClientSetUUIDToDescriptor(JNIEnv *env, jobject bluetoothGatt,
379 jobject characteristic);
382 * add device object to scan device list.
383 * @param[in] env JNI interface pointer.
384 * @param[in] device bluetooth device object.
385 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
387 CAResult_t CALEClientAddScanDeviceToList(JNIEnv *env, jobject device);
390 * check whether the device exist in list or not.
391 * @param[in] env JNI interface pointer.
392 * @param[in] remoteAddress remote address.
393 * @return true or false.
395 bool CALEClientIsDeviceInScanDeviceList(JNIEnv *env, const char *remoteAddress);
398 * remove all devices in scan device list.
399 * @param[in] env JNI interface pointer.
400 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
402 CAResult_t CALEClientRemoveAllScanDevices(JNIEnv *env);
405 * remove target device in scan device list.
406 * @param[in] env JNI interface pointer.
407 * @param[in] remoteAddress remote address.
408 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
410 CAResult_t CALEClientRemoveDeviceInScanDeviceList(JNIEnv *env, jstring remoteAddress);
413 * add gatt object to gatt object list.
414 * @param[in] env JNI interface pointer.
415 * @param[in] gatt Gatt profile object.
416 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
418 CAResult_t CALEClientAddGattobjToList(JNIEnv *env, jobject gatt);
421 * check whether the gatt object exist in list or not.
422 * @param[in] env JNI interface pointer.
423 * @param[in] remoteAddress remote address.
424 * @return true or false.
426 bool CALEClientIsGattObjInList(JNIEnv *env, const char *remoteAddress);
429 * get the gatt object.
430 * @param[in] env JNI interface pointer.
431 * @param[in] remoteAddress remote address.
432 * @return gatt object.
434 jobject CALEClientGetGattObjInList(JNIEnv *env, const char* remoteAddress);
437 * remove all gatt objects in gatt object list.
438 * @param[in] env JNI interface pointer.
439 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
441 CAResult_t CALEClientRemoveAllGattObjs(JNIEnv *env);
444 * remove target device in gatt object list.
445 * @param[in] env JNI interface pointer.
446 * @param[in] gatt Gatt profile object.
447 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
449 CAResult_t CALEClientRemoveGattObj(JNIEnv *env, jobject gatt);
452 * remove gatt object of target device for address in gatt object list.
453 * @param[in] env JNI interface pointer.
454 * @param[in] gatt Gatt profile object.
455 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
457 CAResult_t CALEClientRemoveGattObjForAddr(JNIEnv *env, jstring addr);
460 * update new state information.
461 * @param[in] address remote address.
462 * @param[in] connectedState connection state.
463 * @param[in] notificationState whether characteristic notification already set or not.
464 * @param[in] sendState whether sending was success or not.
465 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
467 CAResult_t CALEClientUpdateDeviceState(const char* address, uint32_t connectedState,
468 uint16_t notificationState, uint16_t sendState);
471 * add new state to state list.
472 * @param[in] state new state.
473 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
475 CAResult_t CALEClientAddDeviceStateToList(CALEState_t* state);
478 * check whether the remote address is existed or not.
479 * @param[in] address remote address.
480 * @return true or false.
482 bool CALEClientIsDeviceInList(const char *remoteAddress);
485 * remove all device states.
486 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
488 CAResult_t CALEClientRemoveAllDeviceState();
491 * remove the device state for a remote device.
492 * @param[in] remoteAddress remote address.
493 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
495 CAResult_t CALEClientRemoveDeviceState(const char* remoteAddress);
498 * get state information for a remote device.
499 * @param[in] remoteAddress remote address.
500 * @return CALEState_t.
502 CALEState_t* CALEClientGetStateInfo(const char* remoteAddress);
505 * check whether the remote address is connected or not.
506 * @param[in] remoteAddress remote address.
507 * @return true or false.
509 bool CALEClientIsConnectedDevice(const char* remoteAddress);
512 * check whether the remote address set CharacteristicNotification or not.
513 * @param[in] remoteAddress remote address.
514 * @return true or false.
516 bool CALEClientIsSetCharacteristic(const char* remoteAddress);
519 * create scan device list.
521 void CALEClientCreateDeviceList();
524 * update the counter which data is sent to remote device.
525 * @param[in] env JNI interface pointer.
527 void CALEClientUpdateSendCnt(JNIEnv *env);
531 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
533 CAResult_t CALEClientInitGattMutexVaraibles();
538 void CALEClientTerminateGattMutexVariables();
541 * set send finish flag.
542 * @param[in] flag finish flag.
544 void CALEClientSetSendFinishFlag(bool flag);
550 #endif /* CA_LECLIENT_H_ */