Merge branch 'master' into resource-manipulation
[platform/upstream/iotivity.git] / resource / csdk / connectivity / inc / caleadapter.h
1 /* ****************************************************************
2  *
3  * Copyright 2014 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  *
24  * This file contains the APIs for LE adapters to be implemented.
25  */
26
27 #ifndef CA_LEADAPTER_H_
28 #define CA_LEADAPTER_H_
29
30 #include "cacommon.h"
31 #include "caadapterinterface.h"
32 #include "cathreadpool.h" /* for thread pool */
33
34 /**
35  * BLE Interface APIs.
36  */
37 #ifdef __cplusplus
38 extern "C"
39 {
40 #endif
41
42 /**
43  * @struct CALEData_t
44  * @brief Stores the information of the Data to be sent from the queues.
45  *        This structure will be pushed to the sender/receiver queue for processing.
46  */
47 typedef struct
48 {
49     CAEndpoint_t
50     *remoteEndpoint;   /**< Remote endpoint contains the inforamtion of remote device */
51     void *data;        /**< Data to be transmitted over LE tranport */
52     uint32_t dataLen;  /**< Length of the data being transmitted */
53 } CALEData_t;
54
55 /**
56  * @brief  Initialize LE connectivity interface.
57  * @param  registerCallback [IN] Callback to register LE interfaces to Connectivity
58  *                               Abstraction Layer
59  * @param  reqRespCallback  [IN] Callback to notify request and response messages from server(s)
60  *                               started at Connectivity Abstraction Layer.
61  * @param  netCallback      [IN] Callback to notify the network additions to Connectivity
62  *                               Abstraction Layer.
63  * @param  errorCallback    [IN] errorCallback to notify error to connectivity common logic
64  *                               layer from adapter
65  * @param  handle           [IN] Threadpool Handle
66  * @return #CA_STATUS_OK or Appropriate error code
67  */
68 CAResult_t CAInitializeLE(CARegisterConnectivityCallback registerCallback,
69                           CANetworkPacketReceivedCallback reqRespCallback,
70                           CANetworkChangeCallback netCallback,
71                           CAErrorHandleCallback errorCallback, ca_thread_pool_t handle);
72
73 /**
74  * @brief Starting LE connectivity adapters.
75  *        As its peer to peer it doesnot require to start any servers
76  * @return  #CA_STATUS_OK or Appropriate error code
77  */
78 CAResult_t CAStartLE();
79
80 /**
81  * @brief Starting listening server for receiving multicast search requests
82  * Transport Specific Behavior:
83  *   LE  Starts GATT Server with prefixed UUID and Characteristics as per OIC Specification.
84  * @return  #CA_STATUS_OK or Appropriate error code
85  */
86 CAResult_t CAStartLEListeningServer();
87
88 /**
89  * @brief for starting discovery servers for receiving multicast advertisements
90  * Transport Specific Behavior:
91  *   LE  Starts GATT Server with prefixed UUID and Characteristics as per OIC Specification.
92  * @return  #CA_STATUS_OK or Appropriate error code
93  */
94 CAResult_t CAStartLEDiscoveryServer();
95
96 /**
97  * @brief Sends data to the endpoint using the adapter connectivity.
98  * @param   endpoint  [IN]  Remote Endpoint information (like ipaddress , port, reference uri \
99  *                          and connectivity type) to which the unicast data has to be sent.
100  * @param   data      [IN]  Data which required to be sent.
101  * @param   dataLen   [IN]  Size of data to be sent.
102  * @return  The number of bytes sent on the network. Returns -1 on error.
103  * @remarks  dataLen must be > 0.
104  */
105 int32_t CASendLEUnicastData(const CAEndpoint_t *endpoint, const void *data,
106                              uint32_t dataLen);
107
108 /**
109  * @brief Sends Multicast data to the endpoint using the LE connectivity.
110  * @param   endpoint    [IN]  Remote Endpoint information to which the unicast data has to be sent.
111  * @param   data        [IN]  Data which required to be sent.
112  * @param   dataLen     [IN]  Size of data to be sent.
113  * @return  The number of bytes sent on the network. Returns -1 on error.
114  * @remarks  dataLen must be > 0.
115  */
116 int32_t CASendLEMulticastData(const CAEndpoint_t *endpoint, const void *data, uint32_t dataLen);
117
118 /**
119  * @brief Starts notification server on EDR adapters.
120  * @return  #CA_STATUS_OK or Appropriate error code
121  */
122 CAResult_t CAStartLENotifyServer();
123
124 /**
125  * @brief   Send notification information.
126  * @param   endpoint  [IN]    Remote Endpoint information (like ipaddress , port, reference uri \
127  *                            and connectivity type) to which the unicast data has to be sent.
128  * @param   data      [IN]    Data which required to be sent.
129  * @param   dataLen   [IN]    Size of data to be sent.
130  * @return  The number of bytes sent on the network. Returns 0 on error.
131  * @remarks dataLen must be > 0.
132  */
133 uint32_t CASendLENotification(const CAEndpoint_t *endpoint, const void *data,
134                               uint32_t dataLen);
135
136 /**
137  * @brief  Get LE Connectivity network information
138  * @param  info        [OUT]   Local connectivity information structures
139  * @param  size        [OUT]   Number of local connectivity structures.
140  * @return #CA_STATUS_OK or Appropriate error code
141  */
142 CAResult_t CAGetLEInterfaceInformation(CAEndpoint_t **info, uint32_t *size);
143
144 /**
145  * @brief Read Synchronous API callback.
146  * @return  #CA_STATUS_OK or Appropriate error code
147  */
148 CAResult_t CAReadLEData();
149
150 /**
151  * @brief Stopping the adapters and close socket connections
152  *   LE Stops all GATT servers and GATT Clients.
153  * @return  #CA_STATUS_OK or Appropriate error code
154  */
155 CAResult_t CAStopLE();
156
157 /**
158  * @brief Terminate the LE connectivity adapter.
159  * Configuration information will be deleted from further use
160  */
161 void CATerminateLE();
162
163 /**
164  * @brief  This function will receive the data from the GattServer and add the data to
165  *         the Server receiver queue.
166  * @param  remoteAddress [IN] Remote address of the device from where data is received.
167  * @param  serviceUUID   [IN] Uuid of the OIC service running on the remote device
168  * @param  data          [IN] Actual data recevied from the remote device.
169  * @param  dataLength    [IN] Length of the data received from the remote device.
170  * @param  sentLength    [IN] Length of the data sent from the remote device.
171  * @return #CA_STATUS_OK or Appropriate error code
172  * @retval #CA_STATUS_OK  Successful
173  * @retval #CA_STATUS_INVALID_PARAM  Invalid input argumets
174  * @retval #CA_STATUS_FAILED Operation failed
175  *
176  */
177 CAResult_t CABLEServerReceivedData(const char *remoteAddress, const char *serviceUUID,
178                                    const void *data, uint32_t dataLength, uint32_t *sentLength);
179
180 /**
181  * @brief  This function will receive the data from the GattClient and add the data into the
182  *         Client receiver queue.
183  * @param  remoteAddress [IN] Remote address of the device from where data is received.
184  * @param  serviceUUID   [IN] Uuid of the OIC service running on the remote device
185  * @param  data          [IN] Actual data recevied from the remote device.
186  * @param  dataLength    [IN] Length of the data received from the remote device.
187  * @param  sentLength    [IN] Length of the data sent from the remote device.
188  * @return #CA_STATUS_OK or Appropriate error code
189  * @retval #CA_STATUS_OK  Successful
190  * @retval #CA_STATUS_INVALID_PARAM  Invalid input argumets
191  * @retval #CA_STATUS_FAILED Operation failed
192  */
193 CAResult_t CABLEClientReceivedData(const char *remoteAddress, const char *serviceUUID,
194                                    const void *data, uint32_t dataLength, uint32_t *sentLength);
195
196 /**
197  * @brief  This function is used to set the NetworkPacket received callback to CA layer from
198  *         adapter layer.
199  * @param  callback [IN] callback handle sent from the upper layer.
200  * @return NONE
201  */
202 void CASetBLEReqRespAdapterCallback(CANetworkPacketReceivedCallback callback);
203
204 /**
205  * @brief  This function will push the data from CA layer to the Sender processor queue.
206  *
207  * @param  remoteEndpoint [IN] Remote endpoint information of the server.
208  * @param  data           [IN] Data to be transmitted from LE.
209  * @param  dataLen        [IN] length of the Data being transmitted.
210  *
211  * @return #CA_STATUS_OK or Appropriate error code
212  * @retval #CA_STATUS_OK  Successful
213  * @retval #CA_STATUS_INVALID_PARAM  Invalid input argumets
214  * @retval #CA_STATUS_FAILED Operation failed
215  */
216 CAResult_t CABLEServerSendData(const CAEndpoint_t *remoteEndpoint,
217                                const void *data, uint32_t dataLen);
218
219 /**
220  * @brief  This function will push the data from CA layer to the Sender processor queue.
221  *
222  * @param  remoteEndpoint [IN] Remote endpoint information of the server.
223  * @param  data           [IN] Data to be transmitted from LE.
224  * @param  dataLen        [IN] length of the Data being transmitted.
225  *
226  * @return #CA_STATUS_OK or Appropriate error code
227  * @retval #CA_STATUS_OK  Successful
228  * @retval #CA_STATUS_INVALID_PARAM  Invalid input argumets
229  * @retval #CA_STATUS_FAILED Operation failed
230  */
231 CAResult_t CABLEClientSendData(const CAEndpoint_t *remoteEndpoint,
232                                const void *data,  uint32_t dataLen);
233
234 /**
235  * @brief  This function will be associated with the sender queue for GattServer.This function will
236  *         fragment the data to the MTU of the transport and send the data in fragments to the
237  *         adapters. The function will be blocked untill all data is sent out from the adapter.
238  *
239  * @param threadData [IN] Data pushed to the queue which contains the info about RemoteEndpoint
240  *                        and Data.
241  *
242  * @return  NONE
243  */
244 void CABLEServerSendDataThread(void *threadData);
245
246 /**
247  * @brief  This function will be associated with the sender queue for GattClient.This function will
248  *         fragment the data to the MTU of the transport and send the data in fragments to the
249  *         adapters. The function will be blocked untill all data is sent out from the adapter.
250  *
251  * @param  threadData [IN] Data pushed to the queue which contains the info about RemoteEndpoint
252  *                         and Data.
253  *
254  * @return NONE
255  */
256 void CABLEClientSendDataThread(void *threadData);
257
258 /**
259  * @brief  This function will be associated with the receiver queue of GattServer. This function
260  *         will defragment the data received and will send the data UP to the CA layer only after
261  *         it collects all the data from the adapter layer. Adapter Header will provide the
262  *         length of the data sent from the server.
263  *
264  * @param  threadData [IN] Data pushed to the queue which contains the info about RemoteEndpoint
265  *                         and Data.
266  *
267  * @return  NONE
268  */
269 void CABLEServerDataReceiverHandler(void *threadData);
270
271 /**
272  * @brief  This function will be associated with the receiver queue of GattClient. This function
273  *         will defragment the data received and will send the data UP to the CA layer only after
274  *         it collects all the data from the adapter layer. Adapter Header will provide the length
275  *         of the data sent from the server.
276  *
277  * @param  threadData [IN] Data pushed to the queue which contains the info about RemoteEndpoint
278  *                         and Data.
279  * @return NONE
280  */
281 void CABLEClientDataReceiverHandler(void *threadData);
282
283 /**
284  * @brief  This function is used to Initalize both GattServer and GattClient queues. All four
285  *         queues will be initialized with this function invocations.
286  * @return  NONE
287  */
288 void CAInitBleQueues();
289
290 /**
291  * @brief  This function will stop all queues created for GattServer and GattClient. All
292  *         four queues will be be stopped with this function invocations.
293  * @return  NONE
294  */
295 void CAStopBleQueues();
296
297 /**
298  * @brief  This function will terminate all queues created for GattServer and GattClient. All
299  *         four queues will be be terminated with this function invocations.
300  * @return  NONE
301  */
302 void CATerminateBleQueues();
303
304 /**
305  * @brief  This function will initalize the Receiver and Sender queues for GattServer. This
306  *         function will inturn call the functions CAInitBleServerReceiverQueue() and
307  *         CAInitBleServerSenderQueue() to initialize the queues.
308  * @return #CA_STATUS_OK or Appropriate error code
309  * @retval #CA_STATUS_OK  Successful
310  * @retval #CA_STATUS_INVALID_PARAM  Invalid input argumets
311  * @retval #CA_STATUS_FAILED Operation failed
312  */
313 CAResult_t CAInitBleServerQueues();
314
315 /**
316  * @brief  This function will initalize the Receiver and Sender queues for GattClient. This
317  *         function will inturn call the functions CAInitBleClientReceiverQueue() and
318  *         CAInitBleClientSenderQueue() to initialize the queues.
319  *
320  * @return #CA_STATUS_OK or Appropriate error code
321  * @retval #CA_STATUS_OK  Successful
322  * @retval #CA_STATUS_INVALID_PARAM  Invalid input argumets
323  * @retval #CA_STATUS_FAILED Operation failed
324  *
325  */
326 CAResult_t CAInitBleClientQueues();
327
328 /**
329  * @brief  This function will initalize the Receiver queue for GattServer. This will initialize
330  *         the queue to process the function CABLEServerSendDataThread() when ever the task is
331  *         added to this queue.
332  *
333  * @return #CA_STATUS_OK or Appropriate error code
334  * @retval #CA_STATUS_OK  Successful
335  * @retval #CA_STATUS_INVALID_PARAM  Invalid input argumets
336  * @retval #CA_STATUS_FAILED Operation failed
337  */
338 CAResult_t CAInitBleServerSenderQueue();
339
340 /**
341  * @brief  This function will initalize the Receiver queue for GattClient. This will initialize
342  *         the queue to process the function CABLEClientSendDataThread() when ever the task is
343  *         added to this queue.
344  *
345  * @return #CA_STATUS_OK or Appropriate error code
346  * @retval #CA_STATUS_OK  Successful
347  * @retval #CA_STATUS_INVALID_PARAM  Invalid input argumets
348  * @retval #CA_STATUS_FAILED Operation failed
349  */
350 CAResult_t CAInitBleClientSenderQueue();
351
352 /**
353  * @brief  This function will initalize the Receiver queue for GattServer. This will initialize
354  *         the queue to process the function CABLEServerDataReceiverHandler() when ever the task
355  *         is added to this queue.
356  *
357  * @return #CA_STATUS_OK or Appropriate error code
358  * @retval #CA_STATUS_OK  Successful
359  * @retval #CA_STATUS_INVALID_PARAM  Invalid input argumets
360  * @retval #CA_STATUS_FAILED Operation failed
361  *
362  */
363 CAResult_t CAInitBleServerReceiverQueue();
364
365 /**
366  * @brief  This function will initalize the Receiver queue for GattClient. This will initialize
367  *         the queue to process the function CABLEClientDataReceiverHandler() when ever the task
368  *         is added to this queue.
369  *
370  * @return #CA_STATUS_OK or Appropriate error code
371  * @retval #CA_STATUS_OK  Successful
372  * @retval #CA_STATUS_INVALID_PARAM  Invalid input argumets
373  * @retval #CA_STATUS_FAILED Operation failed
374  */
375 CAResult_t CAInitBleClientReceiverQueue();
376
377 /**
378  * @brief  This function will create the Data required to send it in the queue.
379  *
380  * @param  remoteEndpoint [IN] Remote endpoint information of the server.
381  * @param  data           [IN] Data to be transmitted from LE.
382  * @param  dataLength     [IN] length of the Data being transmitted.
383  *
384  * @return #CA_STATUS_OK or Appropriate error code
385  * @retval #CA_STATUS_OK  Successful
386  * @retval #CA_STATUS_INVALID_PARAM  Invalid input argumets
387  * @retval #CA_STATUS_FAILED Operation failed
388  */
389 CALEData_t *CACreateBLEData(const CAEndpoint_t *remoteEndpoint, const void *data,
390                            uint32_t dataLength);
391
392 /**
393  * @brief Used to free the BLE information stored in the sender/receiver queues.
394  * @param bleData [IN] Structure contains the information of a particular data segment.
395  * @return NONE
396  */
397 void CAFreeBLEData(CALEData_t *bleData);
398
399 /**
400  * @brief This will be used to notify device status changes to the LE adapter layer
401  * @param  adapter_state [IN] State of the adapter
402  * @return NONE
403  */
404 typedef void (*CALEDeviceStateChangedCallback)(CAAdapterState_t adapter_state);
405
406 /**
407  * @brief This will be used to notify that network packet recieved from GATTClient to adapter layer.
408  * @param  remoteAddress  [IN] Remote endpoint Address
409  * @param  serviceUUID    [IN] Service UUID
410  * @param  data           [IN] Data received
411  * @param  dataLength     [IN] Length of the data received
412  * @param  sentLength     [IN] Length of the data sent
413  * @return #CA_STATUS_OK or Appropriate error code
414  * @retval #CA_STATUS_OK  Successful
415  * @retval #CA_STATUS_INVALID_PARAM  Invalid input argumets
416  * @retval #CA_STATUS_FAILED Operation failed
417  */
418 typedef CAResult_t (*CABLEClientDataReceivedCallback)(const char *remoteAddress,
419                                                       const char *serviceUUID, const void *data,
420                                                       uint32_t dataLength, uint32_t *sentLength);
421
422 /**
423  * @brief This will be used to notify that network packet recieved from GATTServer to adapter layer.
424  * @param  remoteAddress  [IN] Remote endpoint Address
425  * @param  serviceUUID    [IN] Service UUID
426  * @param  data           [IN] Data received
427  * @param  dataLength     [IN] Length of the data received
428  * @param  sentLength     [IN] Length of the data sent
429  * @return #CA_STATUS_OK or Appropriate error code
430  * @retval #CA_STATUS_OK  Successful
431  * @retval #CA_STATUS_INVALID_PARAM  Invalid input argumets
432  * @retval #CA_STATUS_FAILED Operation failed
433  */
434 typedef CAResult_t (*CABLEServerDataReceivedCallback)(const char *remoteAddress,
435                                                       const char *serviceUUID, const void *data,
436                                                       uint32_t dataLength, uint32_t *sentLength);
437
438 #ifdef __cplusplus
439 } /* extern "C" */
440 #endif
441
442 #endif /* CA_LEADAPTER_H_ */
443