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 * set ble scanning flag.
248 * @param[in] flag scan flag.
250 void CALEClientSetScanFlag(bool flag);
253 * stop scan (implement).
254 * @param[in] env JNI interface pointer.
255 * @param[in] callback callback to receive device object by scanning.
256 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
258 CAResult_t CALEClientStopScanImpl(JNIEnv *env, jobject callback);
261 * connect to gatt server hosted.
262 * @param[in] env JNI interface pointer.
263 * @param[in] bluetoothDevice bluetooth Device object.
264 * @param[in] autoconnect whether to directly connect to the remote device(false) or
265 * to automatically connect as soon as the remote device
267 * @param[in] callback callback for connection state change.
268 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
270 CAResult_t CALEClientConnect(JNIEnv *env, jobject bluetoothDevice, jboolean autoconnect,
274 * disconnect to gatt server by a target device.
275 * @param[in] env JNI interface pointer.
276 * @param[in] bluetoothGatt Gatt profile object.
277 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
279 CAResult_t CALEClientDisconnect(JNIEnv *env, jobject bluetoothGatt);
282 * disconnect to gatt server by whole devices.
283 * @param[in] env JNI interface pointer.
284 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
286 CAResult_t CALEClientDisconnectAll(JNIEnv *env);
289 * start discovery server.
290 * @param[in] env JNI interface pointer.
291 * @param[in] bluetoothGatt Gatt profile object.
292 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
294 CAResult_t CALEClientDiscoverServices(JNIEnv *env, jobject bluetoothGatt);
297 * create GattCharacteristic and call CALEClientWriteCharacteristicImpl
298 * for request to write gatt characteristic.
299 * @param[in] env JNI interface pointer.
300 * @param[in] gatt Gatt profile object.
301 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
303 CAResult_t CALEClientWriteCharacteristic(JNIEnv *env, jobject gatt);
306 * request to write gatt characteristic.
307 * @param[in] env JNI interface pointer.
308 * @param[in] bluetoothGatt Gatt profile object.
309 * @param[in] gattCharacteristic characteristic object that contain data to send.
310 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
312 CAResult_t CALEClientWriteCharacteristicImpl(JNIEnv *env, jobject bluetoothGatt,
313 jobject gattCharacteristic);
316 * request to read gatt characteristic.
317 * @param[in] env JNI interface pointer.
318 * @param[in] bluetoothGatt Gatt profile object.
319 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
321 CAResult_t CALEClientReadCharacteristic(JNIEnv *env, jobject bluetoothGatt);
324 * enable notification for a target device.
325 * @param[in] env JNI interface pointer.
326 * @param[in] bluetoothGatt Gatt profile object.
327 * @param[in] characteristic Characteristic object.
328 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
330 CAResult_t CALEClientSetCharacteristicNotification(JNIEnv *env, jobject bluetoothGatt,
331 jobject characteristic);
334 * create gatt characteristic object.
335 * @param[in] env JNI interface pointer.
336 * @param[in] bluetoothGatt Gatt profile object.
337 * @param[in] data for make Characteristic with data.
338 * @return Gatt Characteristic object.
340 jobject CALEClientCreateGattCharacteristic(JNIEnv *env, jobject bluetoothGatt, jbyteArray data);
344 * @param[in] env JNI interface pointer.
345 * @param[in] bluetoothGatt Gatt profile object.
346 * @param[in] characterUUID for make BluetoothGattCharacteristic object.
347 * @return Gatt Service.
349 jobject CALEClientGetGattService(JNIEnv *env, jobject bluetoothGatt, jstring characterUUID);
352 * get value from characteristic.
353 * @param[in] env JNI interface pointer.
354 * @param[in] characteristic Characteristic object.
355 * @return value in characteristic.
357 jbyteArray CALEClientGetValueFromCharacteristic(JNIEnv *env, jobject characteristic);
361 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
363 CAResult_t CALEClientCreateUUIDList();
366 * set UUID to descriptor.
367 * @param[in] env JNI interface pointer.
368 * @param[in] bluetoothGatt Gatt profile object.
369 * @param[in] characteristic Characteristic object.
370 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
372 CAResult_t CALEClientSetUUIDToDescriptor(JNIEnv *env, jobject bluetoothGatt,
373 jobject characteristic);
376 * add device object to scan device list.
377 * @param[in] env JNI interface pointer.
378 * @param[in] device bluetooth device object.
379 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
381 CAResult_t CALEClientAddScanDeviceToList(JNIEnv *env, jobject device);
384 * check whether the device exist in list or not.
385 * @param[in] env JNI interface pointer.
386 * @param[in] remoteAddress remote address.
387 * @return true or false.
389 bool CALEClientIsDeviceInScanDeviceList(JNIEnv *env, const char *remoteAddress);
392 * remove all devices in scan device list.
393 * @param[in] env JNI interface pointer.
394 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
396 CAResult_t CALEClientRemoveAllScanDevices(JNIEnv *env);
399 * remove target device in scan device list.
400 * @param[in] env JNI interface pointer.
401 * @param[in] remoteAddress remote address.
402 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
404 CAResult_t CALEClientRemoveDeviceInScanDeviceList(JNIEnv *env, jstring remoteAddress);
407 * add gatt object to gatt object list.
408 * @param[in] env JNI interface pointer.
409 * @param[in] gatt Gatt profile object.
410 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
412 CAResult_t CALEClientAddGattobjToList(JNIEnv *env, jobject gatt);
415 * check whether the gatt object exist in list or not.
416 * @param[in] env JNI interface pointer.
417 * @param[in] remoteAddress remote address.
418 * @return true or false.
420 bool CALEClientIsGattObjInList(JNIEnv *env, const char *remoteAddress);
423 * get the gatt object.
424 * @param[in] env JNI interface pointer.
425 * @param[in] remoteAddress remote address.
426 * @return gatt object.
428 jobject CALEClientGetGattObjInList(JNIEnv *env, const char* remoteAddress);
431 * remove all gatt objects in gatt object list.
432 * @param[in] env JNI interface pointer.
433 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
435 CAResult_t CALEClientRemoveAllGattObjs(JNIEnv *env);
438 * remove target device in gatt object list.
439 * @param[in] env JNI interface pointer.
440 * @param[in] gatt Gatt profile object.
441 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
443 CAResult_t CALEClientRemoveGattObj(JNIEnv *env, jobject gatt);
446 * remove gatt object of target device for address in gatt object list.
447 * @param[in] env JNI interface pointer.
448 * @param[in] gatt Gatt profile object.
449 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
451 CAResult_t CALEClientRemoveGattObjForAddr(JNIEnv *env, jstring addr);
454 * update new state information.
455 * @param[in] address remote address.
456 * @param[in] connectedState connection state.
457 * @param[in] notificationState whether characteristic notification already set or not.
458 * @param[in] sendState whether sending was success or not.
459 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
461 CAResult_t CALEClientUpdateDeviceState(const char* address, uint32_t connectedState,
462 uint16_t notificationState, uint16_t sendState);
465 * add new state to state list.
466 * @param[in] state new state.
467 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
469 CAResult_t CALEClientAddDeviceStateToList(CALEState_t* state);
472 * check whether the remote address is existed or not.
473 * @param[in] address remote address.
474 * @return true or false.
476 bool CALEClientIsDeviceInList(const char *remoteAddress);
479 * remove all device states.
480 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
482 CAResult_t CALEClientRemoveAllDeviceState();
485 * remove the device state for a remote device.
486 * @param[in] remoteAddress remote address.
487 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
489 CAResult_t CALEClientRemoveDeviceState(const char* remoteAddress);
492 * get state information for a remote device.
493 * @param[in] remoteAddress remote address.
494 * @return CALEState_t.
496 CALEState_t* CALEClientGetStateInfo(const char* remoteAddress);
499 * check whether the remote address is connected or not.
500 * @param[in] remoteAddress remote address.
501 * @return true or false.
503 bool CALEClientIsConnectedDevice(const char* remoteAddress);
506 * check whether the remote address set CharacteristicNotification or not.
507 * @param[in] remoteAddress remote address.
508 * @return true or false.
510 bool CALEClientIsSetCharacteristic(const char* remoteAddress);
513 * create scan device list.
515 void CALEClientCreateDeviceList();
518 * update the counter which data is sent to remote device.
519 * @param[in] env JNI interface pointer.
521 void CALEClientUpdateSendCnt(JNIEnv *env);
525 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
527 CAResult_t CALEClientInitGattMutexVaraibles();
532 void CALEClientTerminateGattMutexVariables();
535 * set send finish flag.
536 * @param[in] flag finish flag.
538 void CALEClientSetSendFinishFlag(bool flag);
544 #endif /* CA_LECLIENT_H_ */