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