replace : iotivity -> iotivity-sec
[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 /**
114  * set keepalive callback to notify connection information in TCP adapter.
115  *
116  * @param[in]  keepaliveHandler Callback function to notify the connection information.
117  * in the TCP adapter.
118  */
119 void CATCPSetKeepAliveCallback(CAKeepAliveConnectionCallback keepaliveHandler);
120
121 /**
122  * Start TCP server.
123  *
124  * @param   threadPool   Thread pool for managing Unicast server threads.
125  * @return ::CA_STATUS_OK or Appropriate error code.
126  * @retval ::CA_STATUS_OK  Successful.
127  * @retval ::CA_STATUS_INVALID_PARAM Invalid input data.
128  * @retval ::CA_STATUS_FAILED Initialization failed.
129  */
130 CAResult_t CATCPStartServer(const ca_thread_pool_t threadPool);
131
132 #endif
133
134 /**
135  * Stop TCP server.
136  */
137 void CATCPStopServer();
138
139 /**
140  * Set this callback for receiving data packets from peer devices.
141  *
142  * @param[in]  callback    Callback to be notified on reception of unicast data packets.
143  */
144 void CATCPSetPacketReceiveCallback(CATCPPacketReceivedCallback callback);
145
146 /**
147  * Set this callback for receiving the changed connection information from peer devices.
148  *
149  * @param[in]  connHandler    Callback to be notified when connection state changes.
150  */
151 void CATCPSetConnectionChangedCallback(CATCPConnectionHandleCallback connHandler);
152
153 /**
154  * API to send unicast TCP data.
155  *
156  * @param[in]  endpoint          complete network address to send to.
157  * @param[in]  data              Data to be send.
158  * @param[in]  dataLength        Length of data in bytes.
159  * @return  Sent data length or -1 on error.
160  */
161 ssize_t CATCPSendData(CAEndpoint_t *endpoint, const void *data, size_t dataLength);
162
163 /**
164  * Get a list of CAInterface_t items.
165  *
166  * @return  List of CAInterface_t items.
167  */
168 u_arraylist_t *CATCPGetInterfaceInformation(int desiredIndex);
169
170 /**
171  * Connect to TCP Server.
172  *
173  * @param[in]   endpoint    remote endpoint information.
174  * @return  Created socket file descriptor.
175  */
176 CASocketFd_t CAConnectTCPSession(const CAEndpoint_t *endpoint);
177
178 /**
179  * Disconnect from TCP Server.
180  *
181  * @param[in]   index       current session index in list.
182  * @return  ::CA_STATUS_OK or Appropriate error code.
183  */
184 CAResult_t CADisconnectTCPSession(size_t index);
185
186 /**
187  * Disconnect all connection from TCP Server.
188  */
189 void CATCPDisconnectAll();
190
191 /**
192  * Get TCP connection information from list.
193  *
194  * @param[in]   endpoint    remote endpoint information.
195  * @param[out]  index   index of array list.
196  * @return  TCP Session Information structure.
197  */
198 CATCPSessionInfo_t *CAGetTCPSessionInfoFromEndpoint(const CAEndpoint_t *endpoint,
199                                                     size_t *index);
200
201 /**
202  * Get total length from CoAP over TCP header.
203  *
204  * @param[in]   recvBuffer    received header data.
205  * @return  total data length
206  */
207 size_t CAGetTotalLengthFromHeader(const unsigned char *recvBuffer);
208
209 /**
210  * Get session information from file descriptor index.
211  *
212  * @param[in]   fd      file descriptor.
213  * @param[out]  index   index of array list
214  * @return  TCP Server Information structure.
215  */
216 CATCPSessionInfo_t *CAGetSessionInfoFromFD(int fd, size_t *index);
217
218 /**
219  * Get socket file descriptor from remote device information.
220  *
221  * @param[in]   endpoint    Remote Endpoint information (such as ipaddress,
222  *                          port, reference uri and transport type) to
223  *                          which the unicast data has to be sent.
224  * @return  Created socket file descriptor.
225  */
226 CASocketFd_t CAGetSocketFDFromEndpoint(const CAEndpoint_t *endpoint);
227
228 /**
229  * Find the session with endpoint info and remove it from list.
230  *
231  * @param[in]   endpoint    Remote Endpoint information (such as ipaddress,
232  *                          port, reference uri and transport type) to
233  *                          which the unicast data has to be sent.
234  * @return  ::CA_STATUS_OK or Appropriate error code.
235  */
236 CAResult_t CASearchAndDeleteTCPSession(const CAEndpoint_t *endpoint);
237
238 /**
239  * Get total payload length from CoAP over TCP header.
240  *
241  * @param[in]   data    Data to be send.
242  * @param[in]   dlen    Total data length.
243  * @return  Payload length
244  */
245 size_t CACheckPayloadLengthFromHeader(const void *data, size_t dlen);
246
247 /**
248  * Construct CoAP header and payload from buffer
249  *
250  * @param[in] svritem - used socket, buffer, current received message length and protocol
251  * @param[in/out]  data  - data buffer, this value is updated as data is copied to svritem
252  * @param[in/out]  dataLength  - length of data, this value decreased as data is copied to svritem
253  * @return             - CA_STATUS_OK or appropriate error code
254  */
255 CAResult_t CAConstructCoAP(CATCPSessionInfo_t *svritem, unsigned char **data,
256                           size_t *dataLength);
257
258 /**
259  * Clean socket state data
260  *
261  * @param[in/out] svritem - socket state data
262  */
263 void CACleanData(CATCPSessionInfo_t *svritem);
264
265 /**
266  * Create a mutex object.
267  *
268  * @return  ::CA_STATUS_OK or Appropriate error code.
269  */
270 CAResult_t CATCPCreateMutex();
271
272 /**
273  * Close a mutex object.
274  */
275 void CATCPDestroyMutex();
276
277 /**
278  * Initialize a condition variable.
279  *
280  * @return  ::CA_STATUS_OK or Appropriate error code.
281  */
282 CAResult_t CATCPCreateCond();
283
284 /**
285  * Destroy condition variable state.
286  */
287 void CATCPDestroyCond();
288
289 #ifdef __cplusplus
290 }
291 #endif
292
293 #endif /* CA_TCP_INTERFACE_H_ */
294