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