Added COAP over TCP feature for Arduino for Transport 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                                             uint32_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                                          uint32_t dataLength, CAResult_t result);
64
65 /**
66   * Callback to notify connection information in the TCP adapter.
67   *
68   * @param[in]  addr    connected device address.
69   * @param[in]  port    connected port info.
70   * @param[in]  isConnected    Whether keepalive message needs to be sent.
71   * @see  Callback must be registered using CATCPSetKeepAliveCallback().
72  */
73 typedef void (*CATCPConnectionHandleCallback)(const char *addr, uint16_t port, bool isConnected);
74
75 /**
76  * set error callback to notify error in TCP adapter.
77  *
78  * @param[in]  errorHandleCallback Callback function to notify the error
79  * in the TCP adapter.
80  */
81 void CATCPSetErrorHandler(CATCPErrorHandleCallback errorHandleCallback);
82
83 #ifdef SINGLE_THREAD
84
85 CAResult_t CATCPStartServer();
86
87 /**
88  * Pull the Received Data.
89  */
90 void CATCPPullData();
91
92 /**
93  * Get TCP Header Details.
94  * @param[in]    recvBuffer   index of array list.
95  * @param[out]   transport    TCP Server address.
96  * @param[out]   headerlen    TCP Server port.
97  */
98 void CAGetTCPHeaderDetails(unsigned char *recvBuffer, coap_transport_type *transport,
99                            size_t *headerlen);
100
101 /**
102  * Get total length from CoAP over TCP header.
103  *
104  * @param[in]   recvBuffer    received header data.
105  * @param[in]   size          length of buffer.
106  * @return  total data length
107  */
108 size_t CAGetTotalLengthFromPacketHeader(const unsigned char *recvBuffer, size_t size);
109
110 #else
111 /**
112  * set keepalive callback to notify connection information in TCP adapter.
113  *
114  * @param[in]  keepaliveHandler Callback function to notify the connection information.
115  * in the TCP adapter.
116  */
117 void CATCPSetKeepAliveCallback(CAKeepAliveConnectionCallback keepaliveHandler);
118
119 /**
120  * Start TCP server.
121  *
122  * @param   threadPool   Thread pool for managing Unicast server threads.
123  * @return ::CA_STATUS_OK or Appropriate error code.
124  * @retval ::CA_STATUS_OK  Successful.
125  * @retval ::CA_STATUS_INVALID_PARAM Invalid input data.
126  * @retval ::CA_STATUS_FAILED Initialization failed.
127  */
128 CAResult_t CATCPStartServer(const ca_thread_pool_t threadPool);
129
130 #endif
131
132 /**
133  * Stop TCP server.
134  */
135 void CATCPStopServer();
136
137 /**
138  * Set this callback for receiving data packets from peer devices.
139  *
140  * @param[in]  callback    Callback to be notified on reception of unicast data packets.
141  */
142 void CATCPSetPacketReceiveCallback(CATCPPacketReceivedCallback callback);
143
144 /**
145  * API to send unicast TCP data.
146  *
147  * @param[in]  endpoint          complete network address to send to.
148  * @param[in]  data              Data to be send.
149  * @param[in]  dataLength        Length of data in bytes.
150  * @param[in]  isMulticast       Whether data needs to be sent to multicast ip.
151  */
152 void CATCPSendData(CAEndpoint_t *endpoint, const void *data, uint32_t dataLength,
153                    bool isMulticast);
154
155 /**
156  * Get a list of CAInterface_t items.
157  *
158  * @return  List of CAInterface_t items.
159  */
160 u_arraylist_t *CATCPGetInterfaceInformation(int desiredIndex);
161
162 /**
163  * Connect to TCP Server.
164  *
165  * @param[in]   endpoint    remote endpoint information.
166  * @return  TCP Session Information structure.
167  */
168 CATCPSessionInfo_t *CAConnectTCPSession(const CAEndpoint_t *endpoint);
169
170 /**
171  * Disconnect from TCP Server.
172  *
173  * @param[in]   svritem     TCP session information.
174  * @param[in]   index       current session index in list.
175  * @return  ::CA_STATUS_OK or Appropriate error code.
176  */
177 CAResult_t CADisconnectTCPSession(CATCPSessionInfo_t *svritem, size_t index);
178
179 /**
180  * Disconnect all connection from TCP Server.
181  */
182 void CATCPDisconnectAll();
183
184 /**
185  * Get TCP connection information from list.
186  *
187  * @param[in]   endpoint    remote endpoint information.
188  * @param[out]  index   index of array list.
189  * @return  TCP Session Information structure.
190  */
191 CATCPSessionInfo_t *CAGetTCPSessionInfoFromEndpoint(const CAEndpoint_t *endpoint,
192                                                     size_t *index);
193
194 /**
195  * Get total length from CoAP over TCP header.
196  *
197  * @param[in]   recvBuffer    received header data.
198  * @return  total data length
199  */
200 size_t CAGetTotalLengthFromHeader(const unsigned char *recvBuffer);
201
202 /**
203  * Get session information from file descriptor index.
204  *
205  * @param[in]   fd      file descriptor.
206  * @param[out]  index   index of array list
207  * @return  TCP Server Information structure.
208  */
209 CATCPSessionInfo_t *CAGetSessionInfoFromFD(int fd, size_t *index);
210
211 #ifdef __cplusplus
212 }
213 #endif
214
215 #endif /* CA_TCP_INTERFACE_H_ */