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