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 * send data for unicast (implement).
155 * @param[in] address remote address.
156 * @param[in] data data for transmission.
157 * @param[in] dataLen data length.
158 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
160 CAResult_t CALEClientSendUnicastMessageImpl(const char *address, const uint8_t *data,
161 const uint32_t dataLen);
164 * send data for multicast (implement).
165 * @param[in] env JNI interface pointer.
166 * @param[in] data data for transmission.
167 * @param[in] dataLen data length.
168 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
170 CAResult_t CALEClientSendMulticastMessageImpl(JNIEnv *env, const uint8_t *data,
171 const uint32_t dataLen);
174 * check whether it is connected or not with remote address.
175 * @param[in] address remote address.
176 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
178 CAResult_t CALECheckSendState(const char* address);
181 * send data to remote device.
182 * if it isn't connected yet. connect LE before try to send data.
183 * @param[in] env JNI interface pointer.
184 * @param[in] device bluetooth device object.
185 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
187 CAResult_t CALEClientSendData(JNIEnv *env, jobject device);
190 * get address from bluetooth gatt object.
191 * @param[in] env JNI interface pointer.
192 * @param[in] gatt Gatt profile object.
193 * @return bluetooth address.
195 jstring CALEClientGetAddressFromGattObj(JNIEnv *env, jobject gatt);
198 * get remote address from bluetooth socket object.
199 * @param[in] env JNI interface pointer.
200 * @param[in] bluetoothSocketObj bluetooth socket.
201 * @return bluetooth address.
203 jstring CALEClientGetRemoteAddress(JNIEnv *env, jobject bluetoothSocketObj);
207 * @param[in] env JNI interface pointer.
208 * @param[in] bluetoothGatt gatt profile object.
209 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
211 CAResult_t CALEClientGattClose(JNIEnv *env, jobject bluetoothGatt);
214 * start to scan whole bluetooth devices (interface).
215 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
217 CAResult_t CALEClientStartScan();
220 * start to scan whole bluetooth devices (implement).
221 * @param[in] env JNI interface pointer.
222 * @param[in] callback callback to receive device object by scanning.
223 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
225 CAResult_t CALEClientStartScanImpl(JNIEnv *env, jobject callback);
228 * start to scan target bluetooth devices for service uuid (implement).
229 * @param[in] env JNI interface pointer.
230 * @param[in] uuids target UUID.
231 * @param[in] callback callback to receive device object by scanning.
232 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
234 CAResult_t CALEClientStartScanWithUUIDImpl(JNIEnv *env, jobjectArray uuids,
239 * @param[in] env JNI interface pointer.
240 * @param[in] uuid uuid.
241 * @return uuid object.
243 jobject CALEClientGetUUIDObject(JNIEnv *env, const char *uuid);
246 * stop scan (interface).
247 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
249 CAResult_t CALEClientStopScan();
252 * set ble scanning flag.
253 * @param[in] flag scan flag.
255 void CALEClientSetScanFlag(bool flag);
258 * stop scan (implement).
259 * @param[in] env JNI interface pointer.
260 * @param[in] callback callback to receive device object by scanning.
261 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
263 CAResult_t CALEClientStopScanImpl(JNIEnv *env, jobject callback);
266 * connect to gatt server hosted.
267 * @param[in] env JNI interface pointer.
268 * @param[in] bluetoothDevice bluetooth Device object.
269 * @param[in] autoconnect whether to directly connect to the remote device(false) or
270 * to automatically connect as soon as the remote device
272 * @param[in] callback callback for connection state change.
273 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
275 CAResult_t CALEClientConnect(JNIEnv *env, jobject bluetoothDevice, jboolean autoconnect,
279 * disconnect to gatt server by a target device.
280 * @param[in] env JNI interface pointer.
281 * @param[in] bluetoothGatt Gatt profile object.
282 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
284 CAResult_t CALEClientDisconnect(JNIEnv *env, jobject bluetoothGatt);
287 * disconnect to gatt server by whole devices.
288 * @param[in] env JNI interface pointer.
289 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
291 CAResult_t CALEClientDisconnectAll(JNIEnv *env);
294 * start discovery server.
295 * @param[in] env JNI interface pointer.
296 * @param[in] bluetoothGatt Gatt profile object.
297 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
299 CAResult_t CALEClientDiscoverServices(JNIEnv *env, jobject bluetoothGatt);
302 * call CALESetValueAndWriteCharacteristic when connection is successful.
303 * @param[in] env JNI interface pointer.
304 * @param[in] gatt Gatt profile object.
305 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
307 CAResult_t CALEClientWriteCharacteristic(JNIEnv *env, jobject gatt);
310 * create GattCharacteristic and call CALEClientWriteCharacteristicImpl
311 * for request to write gatt characteristic.
312 * @param[in] env JNI interface pointer.
313 * @param[in] gatt Gatt profile object.
314 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
316 CAResult_t CALESetValueAndWriteCharacteristic(JNIEnv *env, jobject gatt);
319 * request to write gatt characteristic.
320 * @param[in] env JNI interface pointer.
321 * @param[in] bluetoothGatt Gatt profile object.
322 * @param[in] gattCharacteristic characteristic object that contain data to send.
323 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
325 CAResult_t CALEClientWriteCharacteristicImpl(JNIEnv *env, jobject bluetoothGatt,
326 jobject gattCharacteristic);
329 * request to read gatt characteristic.
330 * @param[in] env JNI interface pointer.
331 * @param[in] bluetoothGatt Gatt profile object.
332 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
334 CAResult_t CALEClientReadCharacteristic(JNIEnv *env, jobject bluetoothGatt);
337 * enable notification for a target device.
338 * @param[in] env JNI interface pointer.
339 * @param[in] bluetoothGatt Gatt profile object.
340 * @param[in] characteristic Characteristic object.
341 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
343 CAResult_t CALEClientSetCharacteristicNotification(JNIEnv *env, jobject bluetoothGatt,
344 jobject characteristic);
347 * create gatt characteristic object.
348 * @param[in] env JNI interface pointer.
349 * @param[in] bluetoothGatt Gatt profile object.
350 * @param[in] data for make Characteristic with data.
351 * @return Gatt Characteristic object.
353 jobject CALEClientCreateGattCharacteristic(JNIEnv *env, jobject bluetoothGatt, jbyteArray data);
357 * @param[in] env JNI interface pointer.
358 * @param[in] bluetoothGatt Gatt profile object.
359 * @param[in] characterUUID for make BluetoothGattCharacteristic object.
360 * @return Gatt Service.
362 jobject CALEClientGetGattService(JNIEnv *env, jobject bluetoothGatt, jstring characterUUID);
365 * get value from characteristic.
366 * @param[in] env JNI interface pointer.
367 * @param[in] characteristic Characteristic object.
368 * @return value in characteristic.
370 jbyteArray CALEClientGetValueFromCharacteristic(JNIEnv *env, jobject characteristic);
374 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
376 CAResult_t CALEClientCreateUUIDList();
379 * set UUID to descriptor.
380 * @param[in] env JNI interface pointer.
381 * @param[in] bluetoothGatt Gatt profile object.
382 * @param[in] characteristic Characteristic object.
383 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
385 CAResult_t CALEClientSetUUIDToDescriptor(JNIEnv *env, jobject bluetoothGatt,
386 jobject characteristic);
389 * add device object to scan device list.
390 * @param[in] env JNI interface pointer.
391 * @param[in] device bluetooth device object.
392 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
394 CAResult_t CALEClientAddScanDeviceToList(JNIEnv *env, jobject device);
397 * check whether the device exist in list or not.
398 * @param[in] env JNI interface pointer.
399 * @param[in] remoteAddress remote address.
400 * @return true or false.
402 bool CALEClientIsDeviceInScanDeviceList(JNIEnv *env, const char *remoteAddress);
405 * remove all devices in scan device list.
406 * @param[in] env JNI interface pointer.
407 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
409 CAResult_t CALEClientRemoveAllScanDevices(JNIEnv *env);
412 * remove target device in scan device list.
413 * @param[in] env JNI interface pointer.
414 * @param[in] remoteAddress remote address.
415 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
417 CAResult_t CALEClientRemoveDeviceInScanDeviceList(JNIEnv *env, jstring remoteAddress);
420 * add gatt object to gatt object list.
421 * @param[in] env JNI interface pointer.
422 * @param[in] gatt Gatt profile object.
423 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
425 CAResult_t CALEClientAddGattobjToList(JNIEnv *env, jobject gatt);
428 * check whether the gatt object exist in list or not.
429 * @param[in] env JNI interface pointer.
430 * @param[in] remoteAddress remote address.
431 * @return true or false.
433 bool CALEClientIsGattObjInList(JNIEnv *env, const char *remoteAddress);
436 * get the gatt object.
437 * @param[in] env JNI interface pointer.
438 * @param[in] remoteAddress remote address.
439 * @return gatt object.
441 jobject CALEClientGetGattObjInList(JNIEnv *env, const char* remoteAddress);
444 * remove all gatt objects in gatt object list.
445 * @param[in] env JNI interface pointer.
446 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
448 CAResult_t CALEClientRemoveAllGattObjs(JNIEnv *env);
451 * remove target device in gatt object list.
452 * @param[in] env JNI interface pointer.
453 * @param[in] gatt Gatt profile object.
454 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
456 CAResult_t CALEClientRemoveGattObj(JNIEnv *env, jobject gatt);
459 * remove gatt object of target device for address in gatt object list.
460 * @param[in] env JNI interface pointer.
461 * @param[in] gatt Gatt profile object.
462 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
464 CAResult_t CALEClientRemoveGattObjForAddr(JNIEnv *env, jstring addr);
467 * update new state information.
468 * @param[in] address remote address.
469 * @param[in] connectedState connection state.
470 * @param[in] notificationState whether characteristic notification already set or not.
471 * @param[in] sendState whether sending was success or not.
472 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
474 CAResult_t CALEClientUpdateDeviceState(const char* address, uint32_t connectedState,
475 uint16_t notificationState, uint16_t sendState);
478 * add new state to state list.
479 * @param[in] state new state.
480 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
482 CAResult_t CALEClientAddDeviceStateToList(CALEState_t* state);
485 * check whether the remote address is existed or not.
486 * @param[in] address remote address.
487 * @return true or false.
489 bool CALEClientIsDeviceInList(const char *remoteAddress);
492 * remove all device states.
493 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
495 CAResult_t CALEClientRemoveAllDeviceState();
498 * remove the device state for a remote device.
499 * @param[in] remoteAddress remote address.
500 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
502 CAResult_t CALEClientRemoveDeviceState(const char* remoteAddress);
505 * get state information for a remote device.
506 * @param[in] remoteAddress remote address.
507 * @return CALEState_t.
509 CALEState_t* CALEClientGetStateInfo(const char* remoteAddress);
512 * check whether the remote address is connected or not.
513 * @param[in] remoteAddress remote address.
514 * @return true or false.
516 bool CALEClientIsConnectedDevice(const char* remoteAddress);
519 * check whether the remote address set CharacteristicNotification or not.
520 * @param[in] remoteAddress remote address.
521 * @return true or false.
523 bool CALEClientIsSetCharacteristic(const char* remoteAddress);
526 * create scan device list.
528 void CALEClientCreateDeviceList();
531 * update the counter which data is sent to remote device.
532 * @param[in] env JNI interface pointer.
534 void CALEClientUpdateSendCnt(JNIEnv *env);
538 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
540 CAResult_t CALEClientInitGattMutexVaraibles();
545 void CALEClientTerminateGattMutexVariables();
548 * set send finish flag.
549 * @param[in] flag finish flag.
551 void CALEClientSetSendFinishFlag(bool flag);
557 #endif /* CA_LECLIENT_H_ */