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_
28 #include "calestate.h"
30 #include "cathreadpool.h"
31 #include "uarraylist.h"
44 BLE_SCAN_ENABLE = 0, /**< BLE scan is working */
45 BLE_SCAN_DISABLE, /**< BLE scan is not working */
46 BLE_SCAN_NONE /**< Initialize State */
50 * Callback to be notified on reception of any data from remote devices.
51 * @param[in] address MAC address of remote device.
52 * @param[in] data Data received from remote device.
53 * @pre Callback must be registered using CALESetCallback(CAPacketReceiveCallback callback)
55 typedef void (*CAPacketReceiveCallback)(const char *address, const uint8_t *data);
58 * initialize JNI object.
60 void CALEClientJniInit();
63 * set context of application.
65 void CALEClientJNISetContext();
68 * create interface object and initialize the object.
69 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
71 CAResult_t CALEClientCreateJniInterfaceObject();
74 * initialize client for BLE.
75 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
77 CAResult_t CALEClientInitialize();
80 * terminate client for BLE.
82 void CALEClientTerminate();
85 * destroy interface object and terminate the interface.
86 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
88 CAResult_t CALEClientDestroyJniInterface();
91 * for destroy sending routine.
92 * @param[in] env JNI interface pointer.
93 * @param[in] gatt Gatt profile object.
95 void CALEClientSendFinish(JNIEnv *env, jobject gatt);
98 * send negotiation message after gatt connection is done.
99 * @param[in] address remote address.
100 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
102 CAResult_t CALEClientSendNegotiationMessage(const char* address);
105 * send data for unicast (interface).
106 * @param[in] address remote address.
107 * @param[in] data data for transmission.
108 * @param[in] dataLen data length.
109 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
111 CAResult_t CALEClientSendUnicastMessage(const char *address, const uint8_t *data,
112 const uint32_t dataLen);
115 * send data for multicast (interface).
116 * @param[in] data data for transmission.
117 * @param[in] dataLen data length.
118 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
120 CAResult_t CALEClientSendMulticastMessage(const uint8_t *data, const uint32_t dataLen);
123 * start unicast server.
124 * @param[in] address remote address.
125 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
127 CAResult_t CALEClientStartUnicastServer(const char *address);
130 * start multicast server (start discovery).
131 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
133 CAResult_t CALEClientStartMulticastServer();
136 * stop unicast server.
138 void CALEClientStopUnicastServer();
141 * stop multicast server (stop discovery).
143 void CALEClientStopMulticastServer();
146 * set this callback for receiving data packets from peer devices.
147 * @param[in] callback callback to be notified on reception of
148 * unicast/multicast data packets.
150 void CALEClientSetCallback(CAPacketReceiveCallback callback);
153 * waiting to get scanned device from BT Platform.
154 * if there is no scanned device in the list.
155 * @param[in] env JNI interface pointer.
156 * @param[in] address LE address.
157 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
159 CAResult_t CALEClientIsThereScannedDevices(JNIEnv *env, const char* address);
162 * send data for unicast (implement).
163 * @param[in] address remote address.
164 * @param[in] data data for transmission.
165 * @param[in] dataLen data length.
166 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
168 CAResult_t CALEClientSendUnicastMessageImpl(const char *address, const uint8_t *data,
169 const uint32_t dataLen);
172 * send data for multicast (implement).
173 * @param[in] env JNI interface pointer.
174 * @param[in] data data for transmission.
175 * @param[in] dataLen data length.
176 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
178 CAResult_t CALEClientSendMulticastMessageImpl(JNIEnv *env, const uint8_t *data,
179 const uint32_t dataLen);
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 whole bluetooth devices for android API level 21 (implement).
230 * @param[in] env JNI interface pointer.
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 CALEClientStartScanImplForV21(JNIEnv *env, jobject callback);
237 * start to scan target bluetooth devices for service uuid (implement).
238 * @param[in] env JNI interface pointer.
239 * @param[in] uuids target UUID.
240 * @param[in] callback callback to receive device object by scanning.
241 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
243 CAResult_t CALEClientStartScanWithUUIDImpl(JNIEnv *env, jobjectArray uuids,
247 * start to scan target bluetooth devices for service uuid for android API level 21 (implement).
248 * @param[in] env JNI interface pointer.
249 * @param[in] uuids target UUID.
250 * @param[in] callback callback to receive device object by scanning.
251 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
253 CAResult_t CALEClientStartScanWithUUIDImplForV21(JNIEnv *env, jobjectArray uuids,
258 * @param[in] env JNI interface pointer.
259 * @param[in] uuid uuid.
260 * @return uuid object.
262 jobject CALEClientGetUUIDObject(JNIEnv *env, const char *uuid);
265 * stop scan (interface).
266 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
268 CAResult_t CALEClientStopScan();
271 * stop scan (implement).
272 * @param[in] env JNI interface pointer.
273 * @param[in] callback callback to receive device object by scanning.
274 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
276 CAResult_t CALEClientStopScanImpl(JNIEnv *env, jobject callback);
279 * stop scan for android API level 21(implement).
280 * @param[in] env JNI interface pointer.
281 * @param[in] callback callback to receive device object by scanning.
282 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
284 CAResult_t CALEClientStopScanImplForV21(JNIEnv *env, jobject callback);
287 * connect to gatt server hosted.
288 * @param[in] env JNI interface pointer.
289 * @param[in] bluetoothDevice bluetooth Device object.
290 * @param[in] autoconnect whether to directly connect to the remote device(false) or
291 * to automatically connect as soon as the remote device
293 * @return gatt object
295 jobject CALEClientConnect(JNIEnv *env, jobject bluetoothDevice, jboolean autoconnect);
298 * disconnect to gatt server by a target device.
299 * @param[in] env JNI interface pointer.
300 * @param[in] bluetoothGatt Gatt profile object.
301 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
303 CAResult_t CALEClientDisconnect(JNIEnv *env, jobject bluetoothGatt);
306 * disconnect to gatt server by whole devices.
307 * @param[in] env JNI interface pointer.
308 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
310 CAResult_t CALEClientDisconnectAll(JNIEnv *env);
313 * disconnect to gatt server by selected address.
314 * @param[in] env JNI interface pointer.
315 * @param[in] remoteAddress remote address.
316 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
318 CAResult_t CALEClientDisconnectforAddress(JNIEnv *env, jstring remoteAddress);
321 * request MTU size negotiation to server.
322 * @param[in] env JNI interface pointer.
323 * @param[in] bluetoothGatt gatt object.
324 * @param[in] size MTU size.
325 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
327 CAResult_t CALEClientRequestMTU(JNIEnv *env, jobject bluetoothGatt, jint size);
329 * start discovery server.
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 CALEClientDiscoverServices(JNIEnv *env, jobject bluetoothGatt);
337 * call CALESetValueAndWriteCharacteristic when connection is successful.
338 * @param[in] env JNI interface pointer.
339 * @param[in] gatt Gatt profile object.
340 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
342 CAResult_t CALEClientWriteCharacteristic(JNIEnv *env, jobject gatt);
345 * create GattCharacteristic and call CALEClientWriteCharacteristicImpl
346 * for request to write gatt characteristic.
347 * @param[in] env JNI interface pointer.
348 * @param[in] gatt Gatt profile object.
349 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
351 CAResult_t CALESetValueAndWriteCharacteristic(JNIEnv *env, jobject gatt);
354 * request to write gatt characteristic.
355 * @param[in] env JNI interface pointer.
356 * @param[in] bluetoothGatt Gatt profile object.
357 * @param[in] gattCharacteristic characteristic object that contain data to send.
358 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
360 CAResult_t CALEClientWriteCharacteristicImpl(JNIEnv *env, jobject bluetoothGatt,
361 jobject gattCharacteristic);
364 * request to read gatt characteristic.
365 * @param[in] env JNI interface pointer.
366 * @param[in] bluetoothGatt Gatt profile object.
367 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
369 CAResult_t CALEClientReadCharacteristic(JNIEnv *env, jobject bluetoothGatt);
372 * enable notification for a target device.
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 CALEClientSetCharacteristicNotification(JNIEnv *env, jobject bluetoothGatt,
379 jobject characteristic);
382 * create gatt characteristic object.
383 * @param[in] env JNI interface pointer.
384 * @param[in] bluetoothGatt Gatt profile object.
385 * @param[in] data for make Characteristic with data.
386 * @return Gatt Characteristic object.
388 jobject CALEClientCreateGattCharacteristic(JNIEnv *env, jobject bluetoothGatt, jbyteArray data);
392 * @param[in] env JNI interface pointer.
393 * @param[in] bluetoothGatt Gatt profile object.
394 * @param[in] characterUUID for make BluetoothGattCharacteristic object.
395 * @return Gatt Service.
397 jobject CALEClientGetGattService(JNIEnv *env, jobject bluetoothGatt, jstring characterUUID);
400 * get value from characteristic.
401 * @param[in] env JNI interface pointer.
402 * @param[in] characteristic Characteristic object.
403 * @return value in characteristic.
405 jbyteArray CALEClientGetValueFromCharacteristic(JNIEnv *env, jobject characteristic);
409 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
411 CAResult_t CALEClientCreateUUIDList();
414 * set UUID to descriptor.
415 * @param[in] env JNI interface pointer.
416 * @param[in] bluetoothGatt Gatt profile object.
417 * @param[in] characteristic Characteristic object.
418 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
420 CAResult_t CALEClientSetUUIDToDescriptor(JNIEnv *env, jobject bluetoothGatt,
421 jobject characteristic);
424 * add device object to scan device list.
425 * @param[in] env JNI interface pointer.
426 * @param[in] device bluetooth device object.
427 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
429 CAResult_t CALEClientAddScanDeviceToList(JNIEnv *env, jobject device);
432 * check whether the device exist in list or not.
433 * @param[in] env JNI interface pointer.
434 * @param[in] remoteAddress remote address.
435 * @return true or false.
437 bool CALEClientIsDeviceInScanDeviceList(JNIEnv *env, const char *remoteAddress);
440 * remove all devices in scan device list.
441 * @param[in] env JNI interface pointer.
442 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
444 CAResult_t CALEClientRemoveAllScanDevices(JNIEnv *env);
447 * remove target device in scan device list.
448 * @param[in] env JNI interface pointer.
449 * @param[in] remoteAddress remote address.
450 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
452 CAResult_t CALEClientRemoveDeviceInScanDeviceList(JNIEnv *env, jstring remoteAddress);
455 * add gatt object to gatt object list.
456 * @param[in] env JNI interface pointer.
457 * @param[in] gatt Gatt profile object.
458 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
460 CAResult_t CALEClientAddGattobjToList(JNIEnv *env, jobject gatt);
463 * check whether the gatt object exist in list or not.
464 * @param[in] env JNI interface pointer.
465 * @param[in] remoteAddress remote address.
466 * @return true or false.
468 bool CALEClientIsGattObjInList(JNIEnv *env, const char *remoteAddress);
471 * get the gatt object.
472 * @param[in] env JNI interface pointer.
473 * @param[in] remoteAddress remote address.
474 * @return gatt object.
476 jobject CALEClientGetGattObjInList(JNIEnv *env, const char* remoteAddress);
479 * remove all gatt objects in gatt object list.
480 * @param[in] env JNI interface pointer.
481 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
483 CAResult_t CALEClientRemoveAllGattObjs(JNIEnv *env);
486 * remove target device in gatt object list.
487 * @param[in] env JNI interface pointer.
488 * @param[in] gatt Gatt profile object.
489 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
491 CAResult_t CALEClientRemoveGattObj(JNIEnv *env, jobject gatt);
494 * remove gatt object of target device for address in gatt object list.
495 * @param[in] env JNI interface pointer.
496 * @param[in] gatt Gatt profile object.
497 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
499 CAResult_t CALEClientRemoveGattObjForAddr(JNIEnv *env, jstring addr);
502 * get ble address from Bluetooth device.
503 * @param[in] env JNI interface pointer.
504 * @param[in] bluetoothDevice Bluetooth device.
505 * @return ble address.
507 jstring CALEClientGetLEAddressFromBTDevice(JNIEnv *env, jobject bluetoothDevice);
510 * update new state information by Bluetooth device object.
511 * @param[in] env JNI interface pointer.
512 * @param[in] device Bluetooth device.
513 * @param[in] state_type state type.
514 * @param[in] target_state state index to update.
515 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
517 CAResult_t CALEClientUpdateDeviceStateWithBtDevice(JNIEnv *env,
520 uint16_t target_state);
523 * set flag into State List.
524 * @param[in] env JNI interface pointer.
525 * @param[in] jni_address remote address.
526 * @param[in] state_idx state index.
527 * @param[in] flag auto connect flag.
529 CAResult_t CALEClientSetFlagToState(JNIEnv *env, jstring jni_address,
530 jint state_idx, jboolean flag);
533 * get flag from State List.
534 * @param[in] env JNI interface pointer.
535 * @param[in] jni_address remote address.
536 * @param[in] state_idx state index.
537 * @return current flag;
539 jboolean CALEClientGetFlagFromState(JNIEnv *env, jstring jni_address, jint state_idx);
543 * @param[in] address the address of the remote device.
544 * @return mtu size negotiated from remote device.
546 uint16_t CALEClientGetMtuSize(const char* address);
549 * Reset values of device state for all of devices.
550 * this method has to be invoked when BT adapter is disabled.
551 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
553 CAResult_t CALEClientResetDeviceStateForAll();
556 * create scan device list.
558 void CALEClientCreateDeviceList();
561 * update the counter which data is sent to remote device.
562 * @param[in] env JNI interface pointer.
564 void CALEClientUpdateSendCnt(JNIEnv *env);
568 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
570 CAResult_t CALEClientInitGattMutexVaraibles();
575 void CALEClientTerminateGattMutexVariables();
578 * set send finish flag.
579 * @param[in] flag finish flag.
581 void CALEClientSetSendFinishFlag(bool flag);
584 * close the connection of the profile proxy to the Service.
585 * @param[in] env JNI interface pointer.
586 * @param[in] gatt gatt profile object.
587 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
589 CAResult_t CALEClientCloseProfileProxy(JNIEnv *env, jobject gatt);
592 * connect to GATT Server hosted by this device.
593 * @param[in] env JNI interface pointer.
594 * @param[in] bluetoothDevice bluetooth device object.
595 * @param[in] autoconnect connect as soon as the device becomes avaiable(true).
596 * @return gatt profile object
598 jobject CALEClientGattConnect(JNIEnv *env, jobject bluetoothDevice, jboolean autoconnect);
601 * connect to GATT Server hosted by this device directly.
602 * @param[in] env JNI interface pointer.
603 * @param[in] bluetoothDevice bluetooth device object.
604 * @param[in] autoconnect connect as soon as the device becomes avaiable(true).
605 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
607 CAResult_t CALEClientDirectConnect(JNIEnv *env, jobject bluetoothDevice, jboolean autoconnect);
610 * check connection status.
611 * @param[in] address the address of the remote device.
612 * @return true or false.
614 bool CALEClientIsConnected(const char* address);
617 * set new interval time and working count.
618 * @param[in] intervalTime interval time(Seconds).
619 * @param[in] workingCount working count for selected interval time.
620 * @param[in] nextScanningStep set next scanning state.
622 void CALEClientSetScanInterval(int32_t intervalTime, int32_t workingCount,
623 CALEScanState_t nextScanningStep);
626 * restart scanning with new interval time and working count.
627 * @param[in] intervalTime interval time(Seconds).
628 * @param[in] workingCount working count for selected interval time.
629 * @param[in] nextScanningStep set next scanning state.
631 void CALERestartScanWithInterval(int32_t intervalTime, int32_t workingCount,
632 CALEScanState_t nextScanningStep);
635 * start LE scanning logic with interval time and working count.
636 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
638 CAResult_t CALEClientStartScanWithInterval();
641 * stop LE scanning logic with interval time and cycle.
643 void CALEClientStopScanWithInterval();
649 #endif /* CA_LECLIENT_H_ */