[IOT-1548] Fix to transfer a large size of data on CoAPs over TCP
[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  * @pre  Callback must be registered using CAIPSetPacketReceiveCallback().
48  */
49 typedef void (*CATCPPacketReceivedCallback)(const CASecureEndpoint_t *endpoint,
50                                             const void *data,
51                                             size_t dataLength);
52
53 /**
54   * Callback to notify error in the TCP adapter.
55   *
56   * @param[in]  endpoint      network endpoint description.
57   * @param[in]  data          Data sent/received.
58   * @param[in]  dataLength    Length of data in bytes.
59   * @param[in]  result        result of request from R.I.
60   * @pre  Callback must be registered using CAIPSetPacketReceiveCallback().
61  */
62 typedef void (*CATCPErrorHandleCallback)(const CAEndpoint_t *endpoint, const void *data,
63                                          size_t dataLength, CAResult_t result);
64
65 /**
66   * Callback to notify connection information in the TCP adapter.
67   *
68   * @param[in]  endpoint        network endpoint description.
69   * @param[in]  isConnected     Whether keepalive message needs to be sent.
70   * @see  Callback must be registered using CATCPSetKeepAliveCallback().
71  */
72 typedef void (*CATCPConnectionHandleCallback)(const CAEndpoint_t *endpoint, bool isConnected);
73
74 /**
75  * set error callback to notify error in TCP adapter.
76  *
77  * @param[in]  errorHandleCallback Callback function to notify the error
78  * in the TCP adapter.
79  */
80 void CATCPSetErrorHandler(CATCPErrorHandleCallback errorHandleCallback);
81
82 #ifdef SINGLE_THREAD
83
84 CAResult_t CATCPStartServer();
85
86 /**
87  * Pull the Received Data.
88  */
89 void CATCPPullData();
90
91 /**
92  * Get TCP Header Details.
93  * @param[in]    recvBuffer   index of array list.
94  * @param[out]   transport    TCP Server address.
95  * @param[out]   headerlen    TCP Server port.
96  */
97 void CAGetTCPHeaderDetails(unsigned char *recvBuffer, coap_transport_t *transport,
98                            size_t *headerlen);
99
100 /**
101  * Get total length from CoAP over TCP header.
102  *
103  * @param[in]   recvBuffer    received header data.
104  * @param[in]   size          length of buffer.
105  * @return  total data length
106  */
107 size_t CAGetTotalLengthFromPacketHeader(const unsigned char *recvBuffer, size_t size);
108
109 #else
110 /**
111  * set keepalive callback to notify connection information in TCP adapter.
112  *
113  * @param[in]  keepaliveHandler Callback function to notify the connection information.
114  * in the TCP adapter.
115  */
116 void CATCPSetKeepAliveCallback(CAKeepAliveConnectionCallback keepaliveHandler);
117
118 /**
119  * Start TCP server.
120  *
121  * @param   threadPool   Thread pool for managing Unicast server threads.
122  * @return ::CA_STATUS_OK or Appropriate error code.
123  * @retval ::CA_STATUS_OK  Successful.
124  * @retval ::CA_STATUS_INVALID_PARAM Invalid input data.
125  * @retval ::CA_STATUS_FAILED Initialization failed.
126  */
127 CAResult_t CATCPStartServer(const ca_thread_pool_t threadPool);
128
129 #endif
130
131 /**
132  * Stop TCP server.
133  */
134 void CATCPStopServer();
135
136 /**
137  * Set this callback for receiving data packets from peer devices.
138  *
139  * @param[in]  callback    Callback to be notified on reception of unicast data packets.
140  */
141 void CATCPSetPacketReceiveCallback(CATCPPacketReceivedCallback callback);
142
143 /**
144  * Set this callback for receiving the changed connection information from peer devices.
145  *
146  * @param[in]  connHandler    Callback to be notified when connection state changes.
147  */
148 void CATCPSetConnectionChangedCallback(CATCPConnectionHandleCallback connHandler);
149
150 /**
151  * API to send unicast TCP data.
152  *
153  * @param[in]  endpoint          complete network address to send to.
154  * @param[in]  data              Data to be send.
155  * @param[in]  dataLength        Length of data in bytes.
156  * @return  Sent data length or -1 on error.
157  */
158 ssize_t CATCPSendData(CAEndpoint_t *endpoint, const void *data, size_t dataLength);
159
160 /**
161  * Get a list of CAInterface_t items.
162  *
163  * @return  List of CAInterface_t items.
164  */
165 u_arraylist_t *CATCPGetInterfaceInformation(int desiredIndex);
166
167 /**
168  * Connect to TCP Server.
169  *
170  * @param[in]   endpoint    remote endpoint information.
171  * @return  Created socket file descriptor.
172  */
173 CASocketFd_t CAConnectTCPSession(const CAEndpoint_t *endpoint);
174
175 /**
176  * Disconnect from TCP Server.
177  *
178  * @param[in]   index       current session index in list.
179  * @return  ::CA_STATUS_OK or Appropriate error code.
180  */
181 CAResult_t CADisconnectTCPSession(size_t index);
182
183 /**
184  * Disconnect all connection from TCP Server.
185  */
186 void CATCPDisconnectAll();
187
188 /**
189  * Get TCP connection information from list.
190  *
191  * @param[in]   endpoint    remote endpoint information.
192  * @param[out]  index   index of array list.
193  * @return  TCP Session Information structure.
194  */
195 CATCPSessionInfo_t *CAGetTCPSessionInfoFromEndpoint(const CAEndpoint_t *endpoint,
196                                                     size_t *index);
197
198 /**
199  * Get total length from CoAP over TCP header.
200  *
201  * @param[in]   recvBuffer    received header data.
202  * @return  total data length
203  */
204 size_t CAGetTotalLengthFromHeader(const unsigned char *recvBuffer);
205
206 /**
207  * Get session information from file descriptor index.
208  *
209  * @param[in]   fd      file descriptor.
210  * @param[out]  index   index of array list
211  * @return  TCP Server Information structure.
212  */
213 CATCPSessionInfo_t *CAGetSessionInfoFromFD(int fd, size_t *index);
214
215 /**
216  * Get socket file descriptor from remote device information.
217  *
218  * @param[in]   endpoint    Remote Endpoint information (such as ipaddress,
219  *                          port, reference uri and transport type) to
220  *                          which the unicast data has to be sent.
221  * @return  Created socket file descriptor.
222  */
223 CASocketFd_t CAGetSocketFDFromEndpoint(const CAEndpoint_t *endpoint);
224
225 /**
226  * Find the session with endpoint info and remove it from list.
227  *
228  * @param[in]   endpoint    Remote Endpoint information (such as ipaddress,
229  *                          port, reference uri and transport type) to
230  *                          which the unicast data has to be sent.
231  * @return  ::CA_STATUS_OK or Appropriate error code.
232  */
233 CAResult_t CASearchAndDeleteTCPSession(const CAEndpoint_t *endpoint);
234
235 /**
236  * Get total payload length from CoAP over TCP header.
237  *
238  * @param[in]   data    Data to be send.
239  * @param[in]   dlen    Total data length.
240  * @return  Payload length
241  */
242 size_t CACheckPayloadLengthFromHeader(const void *data, size_t dlen);
243
244 /**
245  * Construct CoAP header and payload from buffer
246  *
247  * @param[in] svritem - used socket, buffer, current received message length and protocol
248  * @param[in/out]  data  - data buffer, this value is updated as data is copied to svritem
249  * @param[in/out]  dataLength  - length of data, this value decreased as data is copied to svritem
250  * @return             - CA_STATUS_OK or appropriate error code
251  */
252 CAResult_t CAConstructCoAP(CATCPSessionInfo_t *svritem, unsigned char **data,
253                           size_t *dataLength);
254
255 /**
256  * Clean socket state data
257  *
258  * @param[in/out] svritem - socket state data
259  */
260 void CACleanData(CATCPSessionInfo_t *svritem);
261
262 #ifdef __cplusplus
263 }
264 #endif
265
266 #endif /* CA_TCP_INTERFACE_H_ */
267