replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / bt_le_adapter / ios / caleclient.h
1 /* ****************************************************************
2  *
3  * Copyright 2017 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  ******************************************************************/
20
21 /**
22  * @file
23  * This file contains the APIs for BT LE communications.
24  */
25 #ifndef CA_LECLIENT_H_
26 #define CA_LECLIENT_H_
27
28 #include "cacommon.h"
29 #include "cathreadpool.h"
30 #include "uarraylist.h"
31
32 #include "calenwmonitor.h"
33 #include "caleutils.h"
34
35 #ifdef __cplusplus
36 extern "C"
37 {
38 #endif
39
40 static const uint16_t GATT_ERROR = 133;
41
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;
45
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;
49
50 typedef struct le_state_info
51 {
52     char address[CA_MACADDR_SIZE];
53     int connectedState;
54     uint16_t notificationState;
55     uint16_t sendState;
56     bool autoConnectFlag;
57 } CALEState_t;
58
59 /**
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)
64  */
65 typedef void (*CAPacketReceiveCallback)(const char *address,
66                                         const uint8_t *data);
67
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);
73
74 /**
75  * initialize client for BLE.
76  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
77  */
78 CAResult_t CALEClientInitialize();
79
80 /**
81  * terminate client for BLE.
82  */
83 void CALEClientTerminate();
84
85
86 /**
87  * for destroy sending routine.
88  * @param[in]   peripheral    remotePeripheral object
89  */
90 void CALEClientSendFinish(CBPeripheral *peripheral);
91
92 /**
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).
98  */
99 CAResult_t CALEClientSendUnicastMessage(const char *address,
100                                         const uint8_t *data,
101                                         const uint32_t dataLen);
102
103 /**
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).
108  */
109 CAResult_t CALEClientSendMulticastMessage(const uint8_t *data, const uint32_t dataLen);
110
111 /**
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).
115  */
116 CAResult_t CALEClientStartUnicastServer(const char *address);
117
118 /**
119  * start multicast server (start discovery).
120  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
121  */
122 CAResult_t CALEClientStartMulticastServer();
123
124 /**
125  * stop unicast server.
126  */
127 void CALEClientStopUnicastServer();
128
129 /**
130  * stop multicast server (stop discovery).
131  */
132 void CALEClientStopMulticastServer();
133
134 /**
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.
138  */
139 void CALEClientSetCallback(CAPacketReceiveCallback callback);
140
141 void CALEClientSetCAManagerCallback(CAManagerAdapterStateChangeCallback adapterStateChangeCB,
142                                     CAManagerConnectionCallback connectionCB,
143                                     CAManagerServiceDiscoveredCallback serviceDiscoverdCB);
144
145 /**
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).
150  */
151 CAResult_t CALEClientIsThereScannedDevices(const char* address);
152
153 /**
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).
159  */
160 CAResult_t CALEClientSendUnicastMessageImpl(const char *address,
161                                             const uint8_t *data,
162                                             const uint32_t dataLen);
163
164 /**
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).
169  */
170 CAResult_t CALEClientSendMulticastMessageImpl(const uint8_t *data,
171                                               const uint32_t dataLen);
172
173 /**
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).
177  */
178 CAResult_t CALECheckSendState(const char* address);
179
180 /**
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).
185  */
186 CAResult_t CALEClientSendData(CBPeripheral *peripheral);
187
188 /**
189  * get address from bluetooth gatt object.
190  * @param[in]   peripheral        remote peripheral Object
191  * @return  bluetooth address.
192  */
193 NSString *CALEClientGetAddressFromGattObj(CBPeripheral *peripheral);
194
195 /**
196  * get remote address from bluetooth socket object.
197  * @param[in]   peripheral        remote peripheral Object
198  * @return  bluetooth address.
199  */
200 NSString *CALEClientGetRemoteAddress(CBPeripheral *peripheral);
201
202 /**
203  * close gatt.
204  * @param[in]   peripheral        remote peripheral Object
205  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
206  */
207 CAResult_t CALEClientGattClose(CBPeripheral *peripheral);
208
209 /**
210  * start to scan whole bluetooth devices (interface).
211  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
212  */
213 CAResult_t CALEClientStartScan();
214
215 /**
216  * stop scan (interface).
217  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
218  */
219 CAResult_t CALEClientStopScan();
220
221 /**
222  * set ble scanning flag.
223  * @param[in]   flag        scan flag.
224  */
225 void CALEClientSetScanFlag(bool flag);
226
227 /**
228  * set auto connect flag for connectGatt API.
229  * @param[in]   address           remote address.
230  * @param[in]   flag              auto connect flag.
231  */
232 CAResult_t CALEClientSetAutoConnectFlag(const char *remoteAddress, bool flag);
233
234 /**
235  * get auto connect flag.
236  * @param[in]   address           remote address.
237  * @return  current auto connect flag;
238  */
239 bool CALEClientGetAutoConnectFlag(const char *address);
240
241 /**
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
246  *                                     becomes available.
247  * @return  void
248  */
249 CAResult_t CALEClientConnect(CBPeripheral *peripheral, bool autoconnect);
250
251 /**
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).
255  */
256 CAResult_t CALEClientDisconnect(CBPeripheral *peripheral);
257
258 /**
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).
262  */
263 CAResult_t CALEClientConnectCancel(CBPeripheral *peripheral);
264
265 /**
266  * set retreived peripheral to scan device list.
267  * @param[in]   peripheral        remote peripheral Object
268  * @return  ::remote_address.
269  */
270
271 /**
272  * disconnect to gatt server by whole devices.
273  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
274  */
275 CAResult_t CALEClientDisconnectAll();
276
277 /**
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).
281  */
282 CAResult_t CALEClientDisconnectforAddress(const char *remoteAddress);
283
284 /**
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).
288  */
289 CAResult_t CALEClientDiscoverServices(CBPeripheral *peripheral);
290
291 /**
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).
295  */
296 CAResult_t CALEClientWriteCharacteristic(CBPeripheral *peripheral);
297
298
299 /**
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).
304  */
305
306 /**
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).
311  */
312 CAResult_t CALEClientWriteCharacteristicImpl(CBPeripheral *peripheral,
313                                              CBCharacteristic *characteristic);
314
315 /**
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).
320  */
321 CAResult_t CALEClientSetCharacteristicNotification(CBPeripheral *peripheral,
322                                                    CBCharacteristic *characteristic);
323
324 /**
325  * create gatt characteristic object.
326  * @param[in]   peripheral        remote peripheral Objectyy
327  * @return  CBCharacteristic handler
328  */
329 CBCharacteristic *CALEClientCreateGattCharacteristic(CBPeripheral *peripheral);
330
331 /**
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).
335  */
336 CAResult_t CALEClientAddScanDeviceToList(CBPeripheral *peripheral);
337
338 /**
339  * check whether the device exist in list or not.
340  * @param[in]   remoteAddress         remote address.
341  * @return  true or false.
342  */
343 bool CALEClientIsDeviceInScanDeviceList(const char *remoteAddress);
344
345 /**
346  * remove all devices in scan device list.
347  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
348  */
349 CAResult_t CALEClientRemoveAllScanDevices();
350
351 /**
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).
355  */
356 CAResult_t CALEClientRemoveDeviceInScanDeviceList(NSString *remoteAddress);
357
358 /**
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).
362  */
363 CAResult_t CALEClientAddGattobjToList(CBPeripheral *peripheral);
364
365 /**
366  * check whether the gatt object exist in list or not.
367  * @param[in]   remoteAddress         remote address.
368  * @return  true or false.
369  */
370 bool CALEClientIsGattObjInList(const char *remoteAddress);
371
372 /**
373  * get the gatt object.
374  * @param[in]   remoteAddress         remote address.
375  * @return  gatt object.
376  */
377 CBPeripheral *CALEClientGetGattObjInList(const char* remoteAddress);
378
379 /**
380  * remove all gatt objects in gatt object list.
381  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
382  */
383 CAResult_t CALEClientRemoveAllGattObjs();
384
385 /**
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).
389  */
390 CAResult_t CALEClientRemoveGattObj(CBPeripheral *peripheral);
391
392 /**
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).
396  */
397 CAResult_t CALEClientRemoveGattObjForAddr(const char* addr);
398
399 /**
400  * get ble address from Bluetooth device.
401  * @param[in]   bluetoothDevice       Bluetooth device.
402  * @return  ble address.
403  */
404 NSString *CALEClientGetLEAddressFromBTDevice(CBPeripheral *peripheral);
405
406 /**
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).
413  */
414 CAResult_t CALEClientUpdateDeviceState(const char* address,
415                                     uint32_t connectedState,
416                                     uint16_t notificationState,
417                                     uint16_t sendState);
418
419 /**
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).
423  */
424 CAResult_t CALEClientAddDeviceStateToList(CALEState_t* state);
425
426 /**
427  * check whether the remote address is existed or not.
428  * @param[in]   address               remote address.
429  * @return  true or false.
430  */
431 bool CALEClientIsDeviceInList(const char *remoteAddress);
432
433 /**
434  * remove all device states.
435  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
436  */
437 CAResult_t CALEClientRemoveAllDeviceState();
438
439 /**
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).
443  */
444 CAResult_t CALEClientResetDeviceStateForAll();
445
446 /**
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).
450  */
451 CAResult_t CALEClientRemoveDeviceState(const char* remoteAddress);
452
453 /**
454  * get state information for a remote device.
455  * @param[in]   remoteAddress         remote address.
456  * @return  CALEState_t.
457  */
458 CALEState_t* CALEClientGetStateInfo(const char* remoteAddress);
459
460 /**
461  * check whether the remote address is connected or not.
462  * @param[in]   remoteAddress         remote address.
463  * @return  true or false.
464  */
465 bool CALEClientIsConnectedDevice(const char* remoteAddress);
466
467 /**
468  * check whether the remote address set CharacteristicNotification or not.
469  * @param[in]   remoteAddress         remote address.
470  * @return  true or false.
471  */
472 bool CALEClientIsSetCharacteristic(const char* remoteAddress);
473
474 /**
475  * create scan device list.
476  */
477 void CALEClientCreateDeviceList();
478
479 /**
480  * update the counter which data is sent to remote device.
481  */
482 void CALEClientUpdateSendCnt();
483
484 /**
485  * initialize mutex.
486  * @return  ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
487  */
488 CAResult_t CALEClientInitGattMutexVaraibles();
489
490 /**
491  * terminate mutex.
492  */
493 void CALEClientTerminateGattMutexVariables();
494
495 /**
496  * set send finish flag.
497  * @param[in]   flag        finish flag.
498  */
499 void CALEClientSetSendFinishFlag(bool flag);
500
501 static bool isEnableBtAdapter = false;
502 bool CALEClientIsEnableBTAdapter();
503 void CALEClientSetFlagBTAdapter(bool state);
504
505 #ifdef __cplusplus
506 } /* extern "C" */
507 #endif
508
509 #endif /* CA_LECLIENT_H_ */