Update snapshot(2018-02-07)
[platform/upstream/iotivity.git] / resource / csdk / connectivity / inc / catcpinterface.h
1 /* *****************************************************************
2  *
3  * Copyright 2015 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 provides APIs TCP client/server/network monitor modules.
24  */
25
26 #ifndef CA_TCP_INTERFACE_H_
27 #define CA_TCP_INTERFACE_H_
28
29 #include <stdbool.h>
30
31 #include "cacommon.h"
32 #include "catcpadapter.h"
33 #include "cathreadpool.h"
34 #include "uarraylist.h"
35
36 #ifdef __cplusplus
37 extern "C"
38 {
39 #endif
40
41 /**
42  * Callback to be notified on reception of any data from remote OIC devices.
43  *
44  * @param[in]  endpoint      network endpoint description.
45  * @param[in]  data          Data received from remote OIC device.
46  * @param[in]  dataLength    Length of data in bytes.
47  * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
48  * @pre  Callback must be registered using CAIPSetPacketReceiveCallback().
49  */
50 typedef CAResult_t (*CATCPPacketReceivedCallback)(const CASecureEndpoint_t *endpoint,
51                                                   const void *data,
52                                                   size_t dataLength);
53
54 /**
55   * Callback to notify error in the TCP adapter.
56   *
57   * @param[in]  endpoint      network endpoint description.
58   * @param[in]  data          Data sent/received.
59   * @param[in]  dataLength    Length of data in bytes.
60   * @param[in]  result        result of request from R.I.
61   * @pre  Callback must be registered using CAIPSetPacketReceiveCallback().
62  */
63 typedef void (*CATCPErrorHandleCallback)(const CAEndpoint_t *endpoint, const void *data,
64                                          size_t dataLength, CAResult_t result);
65
66 /**
67   * Callback to notify connection information in the TCP adapter.
68   *
69   * @param[in]  endpoint        network endpoint description.
70   * @param[in]  isConnected     Whether keepalive message needs to be sent.
71   * @param[in]  isClient        Host Mode of Operation.
72   * @see  Callback must be registered using CATCPSetKeepAliveCallback().
73  */
74 typedef void (*CATCPConnectionHandleCallback)(const CAEndpoint_t *endpoint, bool isConnected,
75                                               bool isClient);
76
77 /**
78  * set error callback to notify error in TCP adapter.
79  *
80  * @param[in]  errorHandleCallback Callback function to notify the error
81  * in the TCP adapter.
82  */
83 void CATCPSetErrorHandler(CATCPErrorHandleCallback errorHandleCallback);
84
85 #ifdef SINGLE_THREAD
86
87 CAResult_t CATCPStartServer();
88
89 /**
90  * Pull the Received Data.
91  */
92 void CATCPPullData();
93
94 /**
95  * Get TCP Header Details.
96  * @param[in]    recvBuffer   index of array list.
97  * @param[out]   transport    TCP Server address.
98  * @param[out]   headerlen    TCP Server port.
99  */
100 void CAGetTCPHeaderDetails(unsigned char *recvBuffer, coap_transport_t *transport,
101                            size_t *headerlen);
102
103 /**
104  * Get total length from CoAP over TCP header.
105  *
106  * @param[in]   recvBuffer    received header data.
107  * @param[in]   size          length of buffer.
108  * @return  total data length
109  */
110 size_t CAGetTotalLengthFromPacketHeader(const unsigned char *recvBuffer, size_t size);
111
112 #else
113 #ifdef TCP_ADAPTER
114 /**
115  * set keepalive callback to notify connection information in TCP adapter.
116  *
117  * @param[in]  keepaliveHandler Callback function to notify the connection information.
118  * in the TCP adapter.
119  */
120 void CATCPSetKeepAliveCallback(CAKeepAliveConnectionCallback keepaliveHandler);
121 #endif
122
123 /**
124  * Start TCP server.
125  *
126  * @param   threadPool   Thread pool for managing Unicast server threads.
127  * @return ::CA_STATUS_OK or Appropriate error code.
128  * @retval ::CA_STATUS_OK  Successful.
129  * @retval ::CA_STATUS_INVALID_PARAM Invalid input data.
130  * @retval ::CA_STATUS_FAILED Initialization failed.
131  */
132 CAResult_t CATCPStartServer(const ca_thread_pool_t threadPool);
133
134 #endif
135
136 /**
137  * Stop TCP server.
138  */
139 void CATCPStopServer();
140
141 /**
142  * Set this callback for receiving data packets from peer devices.
143  *
144  * @param[in]  callback    Callback to be notified on reception of unicast data packets.
145  */
146 void CATCPSetPacketReceiveCallback(CATCPPacketReceivedCallback callback);
147
148 /**
149  * Set this callback for receiving the changed connection information from peer devices.
150  *
151  * @param[in]  connHandler    Callback to be notified when connection state changes.
152  */
153 void CATCPSetConnectionChangedCallback(CATCPConnectionHandleCallback connHandler);
154
155 /**
156  * API to send unicast TCP data.
157  *
158  * @param[in]  endpoint          complete network address to send to.
159  * @param[in]  data              Data to be send.
160  * @param[in]  dataLength        Length of data in bytes.
161  * @return  Sent data length or -1 on error.
162  */
163 ssize_t CATCPSendData(CAEndpoint_t *endpoint, const void *data, size_t dataLength);
164
165 /**
166  * Get a list of CAInterface_t items.
167  *
168  * @return  List of CAInterface_t items.
169  */
170 u_arraylist_t *CATCPGetInterfaceInformation(int desiredIndex);
171
172 /**
173  * Connect to TCP Server.
174  *
175  * @param[in]   endpoint    remote endpoint information.
176  * @return  Created socket file descriptor.
177  */
178 CASocketFd_t CAConnectTCPSession(const CAEndpoint_t *endpoint);
179
180 /**
181  * Disconnect from TCP Server.
182  *
183  * @param[in]   index       current session index in list.
184  * @return  ::CA_STATUS_OK or Appropriate error code.
185  */
186 CAResult_t CADisconnectTCPSession(size_t index);
187
188 /**
189  * Disconnect all connection from TCP Server.
190  */
191 void CATCPDisconnectAll();
192
193 /**
194  * Get TCP connection information from list.
195  *
196  * @param[in]   endpoint    remote endpoint information.
197  * @param[out]  index   index of array list.
198  * @return  TCP Session Information structure.
199  */
200 CATCPSessionInfo_t *CAGetTCPSessionInfoFromEndpoint(const CAEndpoint_t *endpoint,
201                                                     size_t *index);
202
203 /**
204  * Get total length from CoAP over TCP header.
205  *
206  * @param[in]   recvBuffer    received header data.
207  * @return  total data length
208  */
209 size_t CAGetTotalLengthFromHeader(const unsigned char *recvBuffer);
210
211 /**
212  * Get session information from file descriptor index.
213  *
214  * @param[in]   fd      file descriptor.
215  * @param[out]  index   index of array list
216  * @return  TCP Server Information structure.
217  */
218 CATCPSessionInfo_t *CAGetSessionInfoFromFD(int fd, size_t *index);
219
220 /**
221  * Get socket file descriptor from remote device information.
222  *
223  * @param[in]   endpoint    Remote Endpoint information (such as ipaddress,
224  *                          port, reference uri and transport type) to
225  *                          which the unicast data has to be sent.
226  * @return  Created socket file descriptor.
227  */
228 CASocketFd_t CAGetSocketFDFromEndpoint(const CAEndpoint_t *endpoint);
229
230 /**
231  * Find the session with endpoint info and remove it from list.
232  *
233  * @param[in]   endpoint    Remote Endpoint information (such as ipaddress,
234  *                          port, reference uri and transport type) to
235  *                          which the unicast data has to be sent.
236  * @return  ::CA_STATUS_OK or Appropriate error code.
237  */
238 CAResult_t CASearchAndDeleteTCPSession(const CAEndpoint_t *endpoint);
239
240
241 /**
242  * Abort TCP sessions which are in progress of connecting with servers.
243  */
244 void CATCPCloseInProgressConnections();
245
246 /**
247  * Get total payload length from CoAP over TCP header.
248  *
249  * @param[in]   data    Data to be send.
250  * @param[in]   dlen    Total data length.
251  * @return  Payload length
252  */
253 size_t CACheckPayloadLengthFromHeader(const void *data, size_t dlen);
254
255 /**
256  * Construct CoAP header and payload from buffer
257  *
258  * @param[in] svritem - used socket, buffer, current received message length and protocol
259  * @param[in/out]  data  - data buffer, this value is updated as data is copied to svritem
260  * @param[in/out]  dataLength  - length of data, this value decreased as data is copied to svritem
261  * @return             - CA_STATUS_OK or appropriate error code
262  */
263 CAResult_t CAConstructCoAP(CATCPSessionInfo_t *svritem, unsigned char **data,
264                           size_t *dataLength);
265
266 /**
267  * Clean socket state data
268  *
269  * @param[in/out] svritem - socket state data
270  */
271 void CACleanData(CATCPSessionInfo_t *svritem);
272
273 /**
274  * Create a mutex object.
275  *
276  * @return  ::CA_STATUS_OK or Appropriate error code.
277  */
278 CAResult_t CATCPCreateMutex();
279
280 /**
281  * Close a mutex object.
282  */
283 void CATCPDestroyMutex();
284
285 /**
286  * Create a mutex object for send data.
287  *
288  * @return  ::CA_STATUS_OK or Appropriate error code.
289  */
290 CAResult_t CATCPCreateSendMutex();
291
292 /**
293  * Close a mutex object for send data.
294  */
295 void CATCPDestroySendMutex();
296
297
298 /**
299  * Initialize a condition variable.
300  *
301  * @return  ::CA_STATUS_OK or Appropriate error code.
302  */
303 CAResult_t CATCPCreateCond();
304
305 /**
306  * Destroy condition variable state.
307  */
308 void CATCPDestroyCond();
309
310 /**
311  * Initialize a condition variable for send data.
312  *
313  * @return  ::CA_STATUS_OK or Appropriate error code.
314  */
315 CAResult_t CATCPCreateSendCond();
316
317 /**
318  * Destroy condition variable state for send data.
319  */
320 void CATCPDestroySendCond();
321
322 #ifdef __cplusplus
323 }
324 #endif
325
326 #endif /* CA_TCP_INTERFACE_H_ */
327