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