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 * create GattCharacteristic and call CALEClientWriteCharacteristicImpl
303 * for request to write gatt characteristic.
304 * @param[in] env JNI interface pointer.
305 * @param[in] gatt Gatt profile object.
306 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
308 CAResult_t CALEClientWriteCharacteristic(JNIEnv *env, jobject gatt);
311 * request to write gatt characteristic.
312 * @param[in] env JNI interface pointer.
313 * @param[in] bluetoothGatt Gatt profile object.
314 * @param[in] gattCharacteristic characteristic object that contain data to send.
315 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
317 CAResult_t CALEClientWriteCharacteristicImpl(JNIEnv *env, jobject bluetoothGatt,
318 jobject gattCharacteristic);
321 * request to read gatt characteristic.
322 * @param[in] env JNI interface pointer.
323 * @param[in] bluetoothGatt Gatt profile object.
324 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
326 CAResult_t CALEClientReadCharacteristic(JNIEnv *env, jobject bluetoothGatt);
329 * enable notification for a target device.
330 * @param[in] env JNI interface pointer.
331 * @param[in] bluetoothGatt Gatt profile object.
332 * @param[in] characteristic Characteristic object.
333 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
335 CAResult_t CALEClientSetCharacteristicNotification(JNIEnv *env, jobject bluetoothGatt,
336 jobject characteristic);
339 * create gatt characteristic object.
340 * @param[in] env JNI interface pointer.
341 * @param[in] bluetoothGatt Gatt profile object.
342 * @param[in] data for make Characteristic with data.
343 * @return Gatt Characteristic object.
345 jobject CALEClientCreateGattCharacteristic(JNIEnv *env, jobject bluetoothGatt, jbyteArray data);
349 * @param[in] env JNI interface pointer.
350 * @param[in] bluetoothGatt Gatt profile object.
351 * @param[in] characterUUID for make BluetoothGattCharacteristic object.
352 * @return Gatt Service.
354 jobject CALEClientGetGattService(JNIEnv *env, jobject bluetoothGatt, jstring characterUUID);
357 * get value from characteristic.
358 * @param[in] env JNI interface pointer.
359 * @param[in] characteristic Characteristic object.
360 * @return value in characteristic.
362 jbyteArray CALEClientGetValueFromCharacteristic(JNIEnv *env, jobject characteristic);
366 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
368 CAResult_t CALEClientCreateUUIDList();
371 * set UUID to descriptor.
372 * @param[in] env JNI interface pointer.
373 * @param[in] bluetoothGatt Gatt profile object.
374 * @param[in] characteristic Characteristic object.
375 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
377 CAResult_t CALEClientSetUUIDToDescriptor(JNIEnv *env, jobject bluetoothGatt,
378 jobject characteristic);
381 * add device object to scan device list.
382 * @param[in] env JNI interface pointer.
383 * @param[in] device bluetooth device object.
384 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
386 CAResult_t CALEClientAddScanDeviceToList(JNIEnv *env, jobject device);
389 * check whether the device exist in list or not.
390 * @param[in] env JNI interface pointer.
391 * @param[in] remoteAddress remote address.
392 * @return true or false.
394 bool CALEClientIsDeviceInScanDeviceList(JNIEnv *env, const char *remoteAddress);
397 * remove all devices in scan device list.
398 * @param[in] env JNI interface pointer.
399 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
401 CAResult_t CALEClientRemoveAllScanDevices(JNIEnv *env);
404 * remove target device in scan device list.
405 * @param[in] env JNI interface pointer.
406 * @param[in] remoteAddress remote address.
407 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
409 CAResult_t CALEClientRemoveDeviceInScanDeviceList(JNIEnv *env, jstring remoteAddress);
412 * add gatt object to gatt object list.
413 * @param[in] env JNI interface pointer.
414 * @param[in] gatt Gatt profile object.
415 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
417 CAResult_t CALEClientAddGattobjToList(JNIEnv *env, jobject gatt);
420 * check whether the gatt object exist in list or not.
421 * @param[in] env JNI interface pointer.
422 * @param[in] remoteAddress remote address.
423 * @return true or false.
425 bool CALEClientIsGattObjInList(JNIEnv *env, const char *remoteAddress);
428 * get the gatt object.
429 * @param[in] env JNI interface pointer.
430 * @param[in] remoteAddress remote address.
431 * @return gatt object.
433 jobject CALEClientGetGattObjInList(JNIEnv *env, const char* remoteAddress);
436 * remove all gatt objects in gatt object list.
437 * @param[in] env JNI interface pointer.
438 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
440 CAResult_t CALEClientRemoveAllGattObjs(JNIEnv *env);
443 * remove target device in gatt object list.
444 * @param[in] env JNI interface pointer.
445 * @param[in] gatt Gatt profile object.
446 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
448 CAResult_t CALEClientRemoveGattObj(JNIEnv *env, jobject gatt);
451 * remove gatt object of target device for address 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 CALEClientRemoveGattObjForAddr(JNIEnv *env, jstring addr);
459 * update new state information.
460 * @param[in] address remote address.
461 * @param[in] connectedState connection state.
462 * @param[in] notificationState whether characteristic notification already set or not.
463 * @param[in] sendState whether sending was success or not.
464 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
466 CAResult_t CALEClientUpdateDeviceState(const char* address, uint32_t connectedState,
467 uint16_t notificationState, uint16_t sendState);
470 * add new state to state list.
471 * @param[in] state new state.
472 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
474 CAResult_t CALEClientAddDeviceStateToList(CALEState_t* state);
477 * check whether the remote address is existed or not.
478 * @param[in] address remote address.
479 * @return true or false.
481 bool CALEClientIsDeviceInList(const char *remoteAddress);
484 * remove all device states.
485 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
487 CAResult_t CALEClientRemoveAllDeviceState();
490 * remove the device state for a remote device.
491 * @param[in] remoteAddress remote address.
492 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
494 CAResult_t CALEClientRemoveDeviceState(const char* remoteAddress);
497 * get state information for a remote device.
498 * @param[in] remoteAddress remote address.
499 * @return CALEState_t.
501 CALEState_t* CALEClientGetStateInfo(const char* remoteAddress);
504 * check whether the remote address is connected or not.
505 * @param[in] remoteAddress remote address.
506 * @return true or false.
508 bool CALEClientIsConnectedDevice(const char* remoteAddress);
511 * check whether the remote address set CharacteristicNotification or not.
512 * @param[in] remoteAddress remote address.
513 * @return true or false.
515 bool CALEClientIsSetCharacteristic(const char* remoteAddress);
518 * create scan device list.
520 void CALEClientCreateDeviceList();
523 * update the counter which data is sent to remote device.
524 * @param[in] env JNI interface pointer.
526 void CALEClientUpdateSendCnt(JNIEnv *env);
530 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
532 CAResult_t CALEClientInitGattMutexVaraibles();
537 void CALEClientTerminateGattMutexVariables();
540 * set send finish flag.
541 * @param[in] flag finish flag.
543 void CALEClientSetSendFinishFlag(bool flag);
549 #endif /* CA_LECLIENT_H_ */