1 /* *****************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 ******************************************************************/
23 * This file provides APIs TCP client/server/network monitor modules.
26 #ifndef CA_TCP_INTERFACE_H_
27 #define CA_TCP_INTERFACE_H_
32 #include "catcpadapter.h"
33 #include "cathreadpool.h"
34 #include "uarraylist.h"
42 * Callback to be notified on reception of any data from remote OIC devices.
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().
50 typedef CAResult_t (*CATCPPacketReceivedCallback)(const CASecureEndpoint_t *endpoint,
55 * Callback to notify error in the TCP adapter.
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().
63 typedef void (*CATCPErrorHandleCallback)(const CAEndpoint_t *endpoint, const void *data,
64 size_t dataLength, CAResult_t result);
67 * Callback to notify connection information in the TCP adapter.
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().
74 typedef void (*CATCPConnectionHandleCallback)(const CAEndpoint_t *endpoint, bool isConnected,
78 * set error callback to notify error in TCP adapter.
80 * @param[in] errorHandleCallback Callback function to notify the error
83 void CATCPSetErrorHandler(CATCPErrorHandleCallback errorHandleCallback);
87 CAResult_t CATCPStartServer();
90 * Pull the Received Data.
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.
100 void CAGetTCPHeaderDetails(unsigned char *recvBuffer, coap_transport_t *transport,
104 * Get total length from CoAP over TCP header.
106 * @param[in] recvBuffer received header data.
107 * @param[in] size length of buffer.
108 * @return total data length
110 size_t CAGetTotalLengthFromPacketHeader(const unsigned char *recvBuffer, size_t size);
115 * set keepalive callback to notify connection information in TCP adapter.
117 * @param[in] keepaliveHandler Callback function to notify the connection information.
118 * in the TCP adapter.
120 void CATCPSetKeepAliveCallback(CAKeepAliveConnectionCallback keepaliveHandler);
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.
132 CAResult_t CATCPStartServer(const ca_thread_pool_t threadPool);
139 void CATCPStopServer();
142 * Set this callback for receiving data packets from peer devices.
144 * @param[in] callback Callback to be notified on reception of unicast data packets.
146 void CATCPSetPacketReceiveCallback(CATCPPacketReceivedCallback callback);
149 * Set this callback for receiving the changed connection information from peer devices.
151 * @param[in] connHandler Callback to be notified when connection state changes.
153 void CATCPSetConnectionChangedCallback(CATCPConnectionHandleCallback connHandler);
156 * API to send unicast TCP data.
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.
163 ssize_t CATCPSendData(CAEndpoint_t *endpoint, const void *data, size_t dataLength);
166 * Get a list of CAInterface_t items.
168 * @return List of CAInterface_t items.
170 u_arraylist_t *CATCPGetInterfaceInformation(int desiredIndex);
173 * Connect to TCP Server.
175 * @param[in] endpoint remote endpoint information.
176 * @return Created socket file descriptor.
178 CASocketFd_t CAConnectTCPSession(const CAEndpoint_t *endpoint);
181 * Disconnect from TCP Server.
183 * @param[in] index current session index in list.
184 * @return ::CA_STATUS_OK or Appropriate error code.
186 CAResult_t CADisconnectTCPSession(size_t index);
189 * Disconnect all connection from TCP Server.
191 void CATCPDisconnectAll();
194 * Get TCP connection information from list.
196 * @param[in] endpoint remote endpoint information.
197 * @param[out] index index of array list.
198 * @return TCP Session Information structure.
200 CATCPSessionInfo_t *CAGetTCPSessionInfoFromEndpoint(const CAEndpoint_t *endpoint,
204 * Get total length from CoAP over TCP header.
206 * @param[in] recvBuffer received header data.
207 * @return total data length
209 size_t CAGetTotalLengthFromHeader(const unsigned char *recvBuffer);
212 * Get session information from file descriptor index.
214 * @param[in] fd file descriptor.
215 * @param[out] index index of array list
216 * @return TCP Server Information structure.
218 CATCPSessionInfo_t *CAGetSessionInfoFromFD(int fd, size_t *index);
221 * Get socket file descriptor from remote device information.
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.
228 CASocketFd_t CAGetSocketFDFromEndpoint(const CAEndpoint_t *endpoint);
231 * Find the session with endpoint info and remove it from list.
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.
238 CAResult_t CASearchAndDeleteTCPSession(const CAEndpoint_t *endpoint);
242 * Abort TCP sessions which are in progress of connecting with servers.
244 void CATCPCloseInProgressConnections();
247 * Get total payload length from CoAP over TCP header.
249 * @param[in] data Data to be send.
250 * @param[in] dlen Total data length.
251 * @return Payload length
253 size_t CACheckPayloadLengthFromHeader(const void *data, size_t dlen);
256 * Construct CoAP header and payload from buffer
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
263 CAResult_t CAConstructCoAP(CATCPSessionInfo_t *svritem, unsigned char **data,
267 * Clean socket state data
269 * @param[in/out] svritem - socket state data
271 void CACleanData(CATCPSessionInfo_t *svritem);
274 * Create a mutex object.
276 * @return ::CA_STATUS_OK or Appropriate error code.
278 CAResult_t CATCPCreateMutex();
281 * Close a mutex object.
283 void CATCPDestroyMutex();
286 * Initialize a condition variable.
288 * @return ::CA_STATUS_OK or Appropriate error code.
290 CAResult_t CATCPCreateCond();
293 * Destroy condition variable state.
295 void CATCPDestroyCond();
301 #endif /* CA_TCP_INTERFACE_H_ */