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 * @pre Callback must be registered using CAIPSetPacketReceiveCallback().
49 typedef void (*CATCPPacketReceivedCallback)(const CASecureEndpoint_t *endpoint,
54 * Callback to notify error in the TCP adapter.
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().
62 typedef void (*CATCPErrorHandleCallback)(const CAEndpoint_t *endpoint, const void *data,
63 uint32_t dataLength, CAResult_t result);
66 * Callback to notify connection information in the TCP adapter.
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().
72 typedef void (*CATCPConnectionHandleCallback)(const CAEndpoint_t *endpoint, bool isConnected);
75 * set error callback to notify error in TCP adapter.
77 * @param[in] errorHandleCallback Callback function to notify the error
80 void CATCPSetErrorHandler(CATCPErrorHandleCallback errorHandleCallback);
84 CAResult_t CATCPStartServer();
87 * Pull the Received Data.
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.
97 void CAGetTCPHeaderDetails(unsigned char *recvBuffer, coap_transport_type *transport,
101 * Get total length from CoAP over TCP header.
103 * @param[in] recvBuffer received header data.
104 * @param[in] size length of buffer.
105 * @return total data length
107 size_t CAGetTotalLengthFromPacketHeader(const unsigned char *recvBuffer, size_t size);
111 * set keepalive callback to notify connection information in TCP adapter.
113 * @param[in] keepaliveHandler Callback function to notify the connection information.
114 * in the TCP adapter.
116 void CATCPSetKeepAliveCallback(CAKeepAliveConnectionCallback keepaliveHandler);
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.
127 CAResult_t CATCPStartServer(const ca_thread_pool_t threadPool);
134 void CATCPStopServer();
137 * Set this callback for receiving data packets from peer devices.
139 * @param[in] callback Callback to be notified on reception of unicast data packets.
141 void CATCPSetPacketReceiveCallback(CATCPPacketReceivedCallback callback);
144 * Set this callback for receiving the changed connection information from peer devices.
146 * @param[in] connHandler Callback to be notified when connection state changes.
148 void CATCPSetConnectionChangedCallback(CATCPConnectionHandleCallback connHandler);
151 * API to send unicast TCP data.
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 * @param[in] isMulticast Whether data needs to be sent to multicast ip.
158 void CATCPSendData(CAEndpoint_t *endpoint, const void *data, uint32_t dataLength,
162 * Get a list of CAInterface_t items.
164 * @return List of CAInterface_t items.
166 u_arraylist_t *CATCPGetInterfaceInformation(int desiredIndex);
169 * Connect to TCP Server.
171 * @param[in] endpoint remote endpoint information.
172 * @return TCP Session Information structure.
174 CATCPSessionInfo_t *CAConnectTCPSession(const CAEndpoint_t *endpoint);
177 * Disconnect from TCP Server.
179 * @param[in] svritem TCP session information.
180 * @param[in] index current session index in list.
181 * @return ::CA_STATUS_OK or Appropriate error code.
183 CAResult_t CADisconnectTCPSession(CATCPSessionInfo_t *svritem, size_t index);
186 * Disconnect all connection from TCP Server.
188 void CATCPDisconnectAll();
191 * Get TCP connection information from list.
193 * @param[in] endpoint remote endpoint information.
194 * @param[out] index index of array list.
195 * @return TCP Session Information structure.
197 CATCPSessionInfo_t *CAGetTCPSessionInfoFromEndpoint(const CAEndpoint_t *endpoint,
201 * Get total length from CoAP over TCP header.
203 * @param[in] recvBuffer received header data.
204 * @return total data length
206 size_t CAGetTotalLengthFromHeader(const unsigned char *recvBuffer);
209 * Get session information from file descriptor index.
211 * @param[in] fd file descriptor.
212 * @param[out] index index of array list
213 * @return TCP Server Information structure.
215 CATCPSessionInfo_t *CAGetSessionInfoFromFD(int fd, size_t *index);
221 #endif /* CA_TCP_INTERFACE_H_ */