Update snapshot(2018-01-31)
[platform/upstream/iotivity.git] / resource / csdk / connectivity / inc / catcpadapter.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 contains the APIs for TCP Adapter.
24  */
25 #ifndef CA_TCP_ADAPTER_H_
26 #define CA_TCP_ADAPTER_H_
27
28 #include "cacommon.h"
29 #include "caadapterinterface.h"
30 #include "cathreadpool.h"
31 #include "cainterface.h"
32 #include <coap/pdu.h>
33
34 #ifdef __cplusplus
35 extern "C"
36 {
37 #endif
38
39 // TLS data max size (rfc5246: TLSCiphertext max (2^14+2048+5))
40 #define TLS_DATA_MAX_SIZE 18437
41
42 typedef enum CAProtocol
43 {
44     UNKNOWN = 0,
45     TLS,
46     COAP
47 } CAProtocol_t;
48
49 /**
50  * TCP Connection State.
51  */
52 typedef enum
53 {
54     CONNECTING = 0,
55     CONNECTED,
56     DISCONNECTED
57 } CATCPConnectionState_t;
58
59 /**
60  * TCP Session Information for IPv4 TCP transport
61  */
62 typedef struct
63 {
64     CASecureEndpoint_t sep;             /**< secure endpoint information */
65     int fd;                             /**< file descriptor info */
66     unsigned char* data;                /**< received data from remote device */
67     size_t len;                         /**< received data length */
68     size_t totalLen;                    /**< total coap data length required to receive */
69     size_t bufLen;                      /**< Buffer length. Buffer will be grown dynamically with respect to data received. */
70     unsigned char *tlsdata;             /**< tls data(rfc5246: TLSCiphertext max (2^14+2048+5)) */
71     size_t tlsLen;                      /**< received tls data length */
72     CAProtocol_t protocol;              /**< application-level protocol */
73     CATCPConnectionState_t state;       /**< current tcp session state */
74     bool isClient;                      /**< Host Mode of Operation. */
75 } CATCPSessionInfo_t;
76
77 /**
78  * API to initialize TCP Interface.
79  * @param[in] registerCallback      Callback to register TCP interfaces to
80  *                                  Connectivity Abstraction Layer.
81  * @param[in] networkPacketCallback Callback to notify request and
82  *                                  response messages from server(s)
83  *                                  started at Connectivity Abstraction Layer.
84  * @param[in] netCallback           Callback to notify the adapter changes
85  *                                  to Connectivity Abstraction Layer.
86  * @param[in] connCallback          Callback to notify the connection changes
87  *                                  to Connectivity Abstraction Layer.
88  * @param[in] errorCallback         Callback to notify the network errors to
89  *                                  Connectivity Abstraction Layer.
90  * @param[in] handle                Threadpool Handle.
91  * @return  ::CA_STATUS_OK or Appropriate error code
92  */
93 CAResult_t CAInitializeTCP(CARegisterConnectivityCallback registerCallback,
94                            CANetworkPacketReceivedCallback networkPacketCallback,
95                            CAAdapterChangeCallback netCallback,
96                            CAConnectionChangeCallback connCallback,
97                            CAErrorHandleCallback errorCallback, ca_thread_pool_t handle);
98
99 /**
100  * Start TCP Interface adapter.
101  * @return  ::CA_STATUS_OK or Appropriate error code.
102  */
103 CAResult_t CAStartTCP();
104
105 /**
106  * Disconnect TCP session.
107  * When there is no transmission for a long time.
108  * Some carrier Vendor is blocking data.
109  * Thur, TCP Session is cleaned through this function.
110  * @param[in]   endpoint       Remote Endpoint information (like ipaddress,
111  *                             port)
112  */
113 CAResult_t CATCPDisconnectSession(const CAEndpoint_t *endpoint);
114
115 /**
116  * Start listening server for receiving connect requests.
117  * Transport Specific Behavior:
118  * TCP Starts Listening Server on a particular interface and prefixed port
119  * number and as per OIC Specification.
120  * @return  ::CA_STATUS_OK or Appropriate error code.
121  */
122 CAResult_t CAStartTCPListeningServer();
123
124 /**
125  * Stops listening server from receiving connect requests.
126  * Transport Specific Behavior:
127  * TCP Stops Listening Server on a particular interface and prefixed port
128  * number and as per OIC Specification.
129  * @return  ::CA_STATUS_OK or Appropriate error code.
130  */
131 CAResult_t CAStopTCPListeningServer();
132
133 /**
134  * Start discovery servers for receiving advertisements.
135  * Transport Specific Behavior:
136  * TCP Starts Discovery server on a particular interface and prefixed port
137  * number as per OIC Specification.
138  * @return  ::CA_STATUS_OK or Appropriate error code.
139  */
140 CAResult_t CAStartTCPDiscoveryServer();
141
142 /**
143  * Sends data to the endpoint using the adapter connectivity.
144  * @param[in]   endpoint       Remote Endpoint information (like ipaddress,
145  *                              port, reference uri and transport type) to
146  *                              which the unicast data has to be sent.
147  * @param[in]   data           Data which is required to be sent.
148  * @param[in]   dataLen        Size of data to be sent.
149  * @param[in]   dataType       Data type which is REQUEST or RESPONSE.
150  * @note  dataLen must be > 0.
151  * @return  The number of bytes sent on the network, or -1 upon error.
152  */
153 int32_t CASendTCPUnicastData(const CAEndpoint_t *endpoint,
154                              const void *data, uint32_t dataLen,
155                              CADataType_t dataType);
156
157 /**
158  * Send Multicast data to the endpoint using the TCP connectivity.
159  * @param[in]   endpoint       Remote Endpoint information (like ipaddress,
160  *                              port)
161  * @param[in]   data           Data which is required to be sent.
162  * @param[in]   dataLen        Size of data to be sent.
163  * @param[in]   dataType       Data type which is REQUEST or RESPONSE.
164  * @note  dataLen must be > 0.
165  * @return  The number of bytes sent on the network, or -1 upon error.
166  */
167 int32_t CASendTCPMulticastData(const CAEndpoint_t *endpoint,
168                                const void *data, uint32_t dataLen,
169                                CADataType_t dataType);
170
171 /**
172  * Get TCP Connectivity network information.
173  * @param[out]   info        Local connectivity information structures.
174  * @note info is allocated in this API and should be freed by the caller.
175  * @param[out]   size        Number of local connectivity structures.
176  * @return  ::CA_STATUS_OK or Appropriate error code.
177  */
178 CAResult_t CAGetTCPInterfaceInformation(CAEndpoint_t **info, uint32_t *size);
179
180 /**
181  * Read Synchronous API callback.
182  * @return  ::CA_STATUS_OK or Appropriate error code.
183  */
184 CAResult_t CAReadTCPData();
185
186 /**
187  * Stops Unicast, servers and close the sockets.
188  * @return  ::CA_STATUS_OK or Appropriate error code.
189  */
190 CAResult_t CAStopTCP();
191
192 /**
193  * Terminate the TCP connectivity adapter.
194  * Configuration information will be deleted from further use.
195  */
196 void CATerminateTCP();
197
198 #ifdef TCP_ADAPTER
199 /**
200  * Set connection status changes callback to process KeepAlive.
201  * connection informations are delivered these callbacks.
202  * @param[in]   ConnHandler     Connection status changes callback.
203  */
204 void CATCPSetKeepAliveCallbacks(CAKeepAliveConnectionCallback ConnHandler);
205 #endif
206
207 #ifdef __cplusplus
208 } /* extern "C" */
209 #endif
210
211 #endif  // CA_TCP_ADAPTER_H_