1 /* ****************************************************************
3 * Copyright 2017 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"
32 #include "calenwmonitor.h"
33 #include "caleutils.h"
40 static const uint16_t GATT_ERROR = 133;
42 static const uint16_t STATE_CHARACTER_SET = 2;
43 static const uint16_t STATE_CHARACTER_UNSET = 1;
44 static const uint16_t STATE_CHARACTER_NO_CHANGE = 0;
46 static const uint16_t STATE_SEND_NONE = 0;
47 static const uint16_t STATE_SEND_SUCCESS = 1;
48 static const uint16_t STATE_SEND_FAILED = 2;
50 typedef struct le_state_info
52 char address[CA_MACADDR_SIZE];
54 uint16_t notificationState;
60 * Callback to be notified on reception of any data from remote devices.
61 * @param[in] address MAC address of remote device.
62 * @param[in] data Data received from remote device.
63 * @pre Callback must be registered using CALESetCallback(CAPacketReceiveCallback callback)
65 typedef void (*CAPacketReceiveCallback)(const char *address,
68 typedef void (*CAManagerAdapterStateChangeCallback)(CBCentralManager *central);
69 typedef void (*CAManagerConnectionCallback)(CBPeripheral *peripheral, const char *remote_address,
70 bool connected, NSError *error);
71 typedef void (*CAManagerServiceDiscoveredCallback)(CBPeripheral *peripheral,
72 const char *remote_address, NSError *error);
75 * initialize client for BLE.
76 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
78 CAResult_t CALEClientInitialize();
81 * terminate client for BLE.
83 void CALEClientTerminate();
87 * for destroy sending routine.
88 * @param[in] peripheral remotePeripheral object
90 void CALEClientSendFinish(CBPeripheral *peripheral);
93 * send data for unicast (interface).
94 * @param[in] address remote address.
95 * @param[in] data data for transmission.
96 * @param[in] dataLen data length.
97 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
99 CAResult_t CALEClientSendUnicastMessage(const char *address,
101 const uint32_t dataLen);
104 * send data for multicast (interface).
105 * @param[in] data data for transmission.
106 * @param[in] dataLen data length.
107 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
109 CAResult_t CALEClientSendMulticastMessage(const uint8_t *data, const uint32_t dataLen);
112 * start unicast server.
113 * @param[in] address remote address.
114 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
116 CAResult_t CALEClientStartUnicastServer(const char *address);
119 * start multicast server (start discovery).
120 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
122 CAResult_t CALEClientStartMulticastServer();
125 * stop unicast server.
127 void CALEClientStopUnicastServer();
130 * stop multicast server (stop discovery).
132 void CALEClientStopMulticastServer();
135 * set this callback for receiving data packets from peer devices.
136 * @param[in] callback callback to be notified on reception of
137 * unicast/multicast data packets.
139 void CALEClientSetCallback(CAPacketReceiveCallback callback);
141 void CALEClientSetCAManagerCallback(CAManagerAdapterStateChangeCallback adapterStateChangeCB,
142 CAManagerConnectionCallback connectionCB,
143 CAManagerServiceDiscoveredCallback serviceDiscoverdCB);
146 * waiting to get scanned device from BT Platform.
147 * if there is no scanned device in the list.
148 * @param[in] address LE address.
149 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
151 CAResult_t CALEClientIsThereScannedDevices(const char* address);
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,
162 const uint32_t dataLen);
165 * send data for multicast (implement).
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(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] peripheral remote peripheral object
184 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
186 CAResult_t CALEClientSendData(CBPeripheral *peripheral);
189 * get address from bluetooth gatt object.
190 * @param[in] peripheral remote peripheral Object
191 * @return bluetooth address.
193 NSString *CALEClientGetAddressFromGattObj(CBPeripheral *peripheral);
196 * get remote address from bluetooth socket object.
197 * @param[in] peripheral remote peripheral Object
198 * @return bluetooth address.
200 NSString *CALEClientGetRemoteAddress(CBPeripheral *peripheral);
204 * @param[in] peripheral remote peripheral Object
205 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
207 CAResult_t CALEClientGattClose(CBPeripheral *peripheral);
210 * start to scan whole bluetooth devices (interface).
211 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
213 CAResult_t CALEClientStartScan();
216 * stop scan (interface).
217 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
219 CAResult_t CALEClientStopScan();
222 * set ble scanning flag.
223 * @param[in] flag scan flag.
225 void CALEClientSetScanFlag(bool flag);
228 * set auto connect flag for connectGatt API.
229 * @param[in] address remote address.
230 * @param[in] flag auto connect flag.
232 CAResult_t CALEClientSetAutoConnectFlag(const char *remoteAddress, bool flag);
235 * get auto connect flag.
236 * @param[in] address remote address.
237 * @return current auto connect flag;
239 bool CALEClientGetAutoConnectFlag(const char *address);
242 * connect to gatt server hosted.
243 * @param[in] bluetoothDevice bluetooth Device object.
244 * @param[in] autoconnect whether to directly connect to the remote device(false) or
245 * to automatically connect as soon as the remote device
249 CAResult_t CALEClientConnect(CBPeripheral *peripheral, bool autoconnect);
252 * disconnect to gatt server by a target device.
253 * @param[in] peripheral remote peripheral Object
254 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
256 CAResult_t CALEClientDisconnect(CBPeripheral *peripheral);
259 * cancel to gatt connecting by a target device.
260 * @param[in] peripheral remote peripheral Object
261 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
263 CAResult_t CALEClientConnectCancel(CBPeripheral *peripheral);
266 * set retreived peripheral to scan device list.
267 * @param[in] peripheral remote peripheral Object
268 * @return ::remote_address.
272 * disconnect to gatt server by whole devices.
273 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
275 CAResult_t CALEClientDisconnectAll();
278 * disconnect to gatt server by selected address.
279 * @param[in] remoteAddress remote address.
280 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
282 CAResult_t CALEClientDisconnectforAddress(const char *remoteAddress);
285 * start discovery server.
286 * @param[in] peripheral remote peripheral Objectyy
287 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
289 CAResult_t CALEClientDiscoverServices(CBPeripheral *peripheral);
292 * call CALESetValueAndWriteCharacteristic when connection is successful.
293 * @param[in] peripheral remote peripheral Objectyy
294 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
296 CAResult_t CALEClientWriteCharacteristic(CBPeripheral *peripheral);
300 * create GattCharacteristic and call CALEClientWriteCharacteristicImpl
301 * for request to write gatt characteristic.
302 * @param[in] peripheral remote peripheral Objectyy
303 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
307 * request to write gatt characteristic.
308 * @param[in] peripheral remote peripheral Objectyy
309 * @param[in] characteristic 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(CBPeripheral *peripheral,
313 CBCharacteristic *characteristic);
316 * enable notification for a target device.
317 * @param[in] peripheral remote peripheral Objectyy
318 * @param[in] characteristic characteristic object that contain data to send.
319 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
321 CAResult_t CALEClientSetCharacteristicNotification(CBPeripheral *peripheral,
322 CBCharacteristic *characteristic);
325 * create gatt characteristic object.
326 * @param[in] peripheral remote peripheral Objectyy
327 * @return CBCharacteristic handler
329 CBCharacteristic *CALEClientCreateGattCharacteristic(CBPeripheral *peripheral);
332 * add device object to scan device list.
333 * @param[in] device bluetooth device object.
334 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
336 CAResult_t CALEClientAddScanDeviceToList(CBPeripheral *peripheral);
339 * check whether the device exist in list or not.
340 * @param[in] remoteAddress remote address.
341 * @return true or false.
343 bool CALEClientIsDeviceInScanDeviceList(const char *remoteAddress);
346 * remove all devices in scan device list.
347 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
349 CAResult_t CALEClientRemoveAllScanDevices();
352 * remove target device in scan device list.
353 * @param[in] remoteAddress remote address.
354 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
356 CAResult_t CALEClientRemoveDeviceInScanDeviceList(NSString *remoteAddress);
359 * add gatt object to gatt object list.
360 * @param[in] gatt Gatt profile object.
361 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
363 CAResult_t CALEClientAddGattobjToList(CBPeripheral *peripheral);
366 * check whether the gatt object exist in list or not.
367 * @param[in] remoteAddress remote address.
368 * @return true or false.
370 bool CALEClientIsGattObjInList(const char *remoteAddress);
373 * get the gatt object.
374 * @param[in] remoteAddress remote address.
375 * @return gatt object.
377 CBPeripheral *CALEClientGetGattObjInList(const char* remoteAddress);
380 * remove all gatt objects in gatt object list.
381 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
383 CAResult_t CALEClientRemoveAllGattObjs();
386 * remove target device in gatt object list.
387 * @param[in] gatt Gatt profile object.
388 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
390 CAResult_t CALEClientRemoveGattObj(CBPeripheral *peripheral);
393 * remove gatt object of target device for address in gatt object list.
394 * @param[in] gatt Gatt profile object.
395 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
397 CAResult_t CALEClientRemoveGattObjForAddr(const char* addr);
400 * get ble address from Bluetooth device.
401 * @param[in] bluetoothDevice Bluetooth device.
402 * @return ble address.
404 NSString *CALEClientGetLEAddressFromBTDevice(CBPeripheral *peripheral);
407 * update new state information.
408 * @param[in] address remote address.
409 * @param[in] connectedState connection state.
410 * @param[in] notificationState whether characteristic notification already set or not.
411 * @param[in] sendState whether sending was success or not.
412 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
414 CAResult_t CALEClientUpdateDeviceState(const char* address,
415 uint32_t connectedState,
416 uint16_t notificationState,
420 * add new state to state list.
421 * @param[in] state new state.
422 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
424 CAResult_t CALEClientAddDeviceStateToList(CALEState_t* state);
427 * check whether the remote address is existed or not.
428 * @param[in] address remote address.
429 * @return true or false.
431 bool CALEClientIsDeviceInList(const char *remoteAddress);
434 * remove all device states.
435 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
437 CAResult_t CALEClientRemoveAllDeviceState();
440 * Reset values of device state for all of devices.
441 * this method has to be invoked when BT adapter is disabled.
442 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
444 CAResult_t CALEClientResetDeviceStateForAll();
447 * remove the device state for a remote device.
448 * @param[in] remoteAddress remote address.
449 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
451 CAResult_t CALEClientRemoveDeviceState(const char* remoteAddress);
454 * get state information for a remote device.
455 * @param[in] remoteAddress remote address.
456 * @return CALEState_t.
458 CALEState_t* CALEClientGetStateInfo(const char* remoteAddress);
461 * check whether the remote address is connected or not.
462 * @param[in] remoteAddress remote address.
463 * @return true or false.
465 bool CALEClientIsConnectedDevice(const char* remoteAddress);
468 * check whether the remote address set CharacteristicNotification or not.
469 * @param[in] remoteAddress remote address.
470 * @return true or false.
472 bool CALEClientIsSetCharacteristic(const char* remoteAddress);
475 * create scan device list.
477 void CALEClientCreateDeviceList();
480 * update the counter which data is sent to remote device.
482 void CALEClientUpdateSendCnt();
486 * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
488 CAResult_t CALEClientInitGattMutexVaraibles();
493 void CALEClientTerminateGattMutexVariables();
496 * set send finish flag.
497 * @param[in] flag finish flag.
499 void CALEClientSetSendFinishFlag(bool flag);
501 static bool isEnableBtAdapter = false;
502 bool CALEClientIsEnableBTAdapter();
503 void CALEClientSetFlagBTAdapter(bool state);
509 #endif /* CA_LECLIENT_H_ */