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);
114 * set keepalive callback to notify connection information in TCP adapter.
116 * @param[in] keepaliveHandler Callback function to notify the connection information.
117 * in the TCP adapter.
119 void CATCPSetKeepAliveCallback(CAKeepAliveConnectionCallback keepaliveHandler);
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.
130 CAResult_t CATCPStartServer(const ca_thread_pool_t threadPool);
137 void CATCPStopServer();
140 * Set this callback for receiving data packets from peer devices.
142 * @param[in] callback Callback to be notified on reception of unicast data packets.
144 void CATCPSetPacketReceiveCallback(CATCPPacketReceivedCallback callback);
147 * Set this callback for receiving the changed connection information from peer devices.
149 * @param[in] connHandler Callback to be notified when connection state changes.
151 void CATCPSetConnectionChangedCallback(CATCPConnectionHandleCallback connHandler);
154 * API to send unicast TCP data.
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.
161 ssize_t CATCPSendData(CAEndpoint_t *endpoint, const void *data, size_t dataLength);
164 * Get a list of CAInterface_t items.
166 * @return List of CAInterface_t items.
168 u_arraylist_t *CATCPGetInterfaceInformation(int desiredIndex);
171 * Connect to TCP Server.
173 * @param[in] endpoint remote endpoint information.
174 * @return Created socket file descriptor.
176 CASocketFd_t CAConnectTCPSession(const CAEndpoint_t *endpoint);
179 * Disconnect from TCP Server.
181 * @param[in] index current session index in list.
182 * @return ::CA_STATUS_OK or Appropriate error code.
184 CAResult_t CADisconnectTCPSession(size_t index);
187 * Disconnect all connection from TCP Server.
189 void CATCPDisconnectAll();
192 * Get TCP connection information from list.
194 * @param[in] endpoint remote endpoint information.
195 * @param[out] index index of array list.
196 * @return TCP Session Information structure.
198 CATCPSessionInfo_t *CAGetTCPSessionInfoFromEndpoint(const CAEndpoint_t *endpoint,
202 * Get total length from CoAP over TCP header.
204 * @param[in] recvBuffer received header data.
205 * @return total data length
207 size_t CAGetTotalLengthFromHeader(const unsigned char *recvBuffer);
210 * Get session information from file descriptor index.
212 * @param[in] fd file descriptor.
213 * @param[out] index index of array list
214 * @return TCP Server Information structure.
216 CATCPSessionInfo_t *CAGetSessionInfoFromFD(int fd, size_t *index);
219 * Get socket file descriptor from remote device information.
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.
226 CASocketFd_t CAGetSocketFDFromEndpoint(const CAEndpoint_t *endpoint);
229 * Find the session with endpoint info and remove it from list.
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.
236 CAResult_t CASearchAndDeleteTCPSession(const CAEndpoint_t *endpoint);
239 * Get total payload length from CoAP over TCP header.
241 * @param[in] data Data to be send.
242 * @param[in] dlen Total data length.
243 * @return Payload length
245 size_t CACheckPayloadLengthFromHeader(const void *data, size_t dlen);
248 * Construct CoAP header and payload from buffer
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
255 CAResult_t CAConstructCoAP(CATCPSessionInfo_t *svritem, unsigned char **data,
259 * Clean socket state data
261 * @param[in/out] svritem - socket state data
263 void CACleanData(CATCPSessionInfo_t *svritem);
266 * Create a mutex object.
268 * @return ::CA_STATUS_OK or Appropriate error code.
270 CAResult_t CATCPCreateMutex();
273 * Close a mutex object.
275 void CATCPDestroyMutex();
278 * Initialize a condition variable.
280 * @return ::CA_STATUS_OK or Appropriate error code.
282 CAResult_t CATCPCreateCond();
285 * Destroy condition variable state.
287 void CATCPDestroyCond();
293 #endif /* CA_TCP_INTERFACE_H_ */