Merge "Merge branch 'master' into notification-service" into notification-service
[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 "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 Session Information for IPv4 TCP transport
49  */
50 typedef struct
51 {
52     CASecureEndpoint_t sep;             /**< secure endpoint information */
53     int fd;                             /**< file descriptor info */
54     unsigned char* data;                /**< received data from remote device */
55     size_t len;                         /**< received data length */
56     size_t totalLen;                    /**< total data length required to receive */
57     CAProtocol_t protocol;              /**< application-level protocol */
58 } CATCPSessionInfo_t;
59
60 /**
61  * API to initialize TCP Interface.
62  * @param[in] registerCallback      Callback to register TCP interfaces to
63  *                                  Connectivity Abstraction Layer.
64  * @param[in] networkPacketCallback Callback to notify request and
65  *                                  response messages from server(s)
66  *                                  started at Connectivity Abstraction Layer.
67  * @param[in] netCallback           Callback to notify the adapter changes
68  *                                  to Connectivity Abstraction Layer.
69  * @param[in] connCallback          Callback to notify the connection changes
70  *                                  to Connectivity Abstraction Layer.
71  * @param[in] errorCallback         Callback to notify the network errors to
72  *                                  Connectivity Abstraction Layer.
73  * @param[in] handle                Threadpool Handle.
74  * @return  ::CA_STATUS_OK or Appropriate error code
75  */
76 CAResult_t CAInitializeTCP(CARegisterConnectivityCallback registerCallback,
77                            CANetworkPacketReceivedCallback networkPacketCallback,
78                            CAAdapterChangeCallback netCallback,
79                            CAConnectionChangeCallback connCallback,
80                            CAErrorHandleCallback errorCallback, ca_thread_pool_t handle);
81
82 /**
83  * Start TCP Interface adapter.
84  * @return  ::CA_STATUS_OK or Appropriate error code.
85  */
86 CAResult_t CAStartTCP();
87
88 /**
89  * Start listening server for receiving connect requests.
90  * Transport Specific Behavior:
91  * TCP Starts Listening Server on a particular interface and prefixed port
92  * number and as per OIC Specification.
93  * @return  ::CA_STATUS_OK or Appropriate error code.
94  */
95 CAResult_t CAStartTCPListeningServer();
96
97 /**
98  * Stops listening server from receiving connect requests.
99  * Transport Specific Behavior:
100  * TCP Stops Listening Server on a particular interface and prefixed port
101  * number and as per OIC Specification.
102  * @return  ::CA_STATUS_OK or Appropriate error code.
103  */
104 CAResult_t CAStopTCPListeningServer();
105
106 /**
107  * Start discovery servers for receiving advertisements.
108  * Transport Specific Behavior:
109  * TCP Starts Discovery server on a particular interface and prefixed port
110  * number as per OIC Specification.
111  * @return  ::CA_STATUS_OK or Appropriate error code.
112  */
113 CAResult_t CAStartTCPDiscoveryServer();
114
115 /**
116  * Sends data to the endpoint using the adapter connectivity.
117  * @param[in]   endpoint       Remote Endpoint information (like ipaddress,
118  *                              port, reference uri and transport type) to
119  *                              which the unicast data has to be sent.
120  * @param[in]   data           Data which is required to be sent.
121  * @param[in]   dataLen        Size of data to be sent.
122  * @param[in]   dataType       Data type which is REQUEST or RESPONSE.
123  * @note  dataLen must be > 0.
124  * @return  The number of bytes sent on the network, or -1 upon error.
125  */
126 int32_t CASendTCPUnicastData(const CAEndpoint_t *endpoint,
127                              const void *data, uint32_t dataLen,
128                              CADataType_t dataType);
129
130 /**
131  * Send Multicast data to the endpoint using the TCP connectivity.
132  * @param[in]   endpoint       Remote Endpoint information (like ipaddress,
133  *                              port)
134  * @param[in]   data           Data which is required to be sent.
135  * @param[in]   dataLen        Size of data to be sent.
136  * @param[in]   dataType       Data type which is REQUEST or RESPONSE.
137  * @note  dataLen must be > 0.
138  * @return  The number of bytes sent on the network, or -1 upon error.
139  */
140 int32_t CASendTCPMulticastData(const CAEndpoint_t *endpoint,
141                                const void *data, uint32_t dataLen,
142                                CADataType_t dataType);
143
144 /**
145  * Get TCP Connectivity network information.
146  * @param[out]   info        Local connectivity information structures.
147  * @note info is allocated in this API and should be freed by the caller.
148  * @param[out]   size        Number of local connectivity structures.
149  * @return  ::CA_STATUS_OK or Appropriate error code.
150  */
151 CAResult_t CAGetTCPInterfaceInformation(CAEndpoint_t **info, uint32_t *size);
152
153 /**
154  * Read Synchronous API callback.
155  * @return  ::CA_STATUS_OK or Appropriate error code.
156  */
157 CAResult_t CAReadTCPData();
158
159 /**
160  * Stops Unicast, servers and close the sockets.
161  * @return  ::CA_STATUS_OK or Appropriate error code.
162  */
163 CAResult_t CAStopTCP();
164
165 /**
166  * Terminate the TCP connectivity adapter.
167  * Configuration information will be deleted from further use.
168  */
169 void CATerminateTCP();
170
171 /**
172  * Set connection status changes callback to process KeepAlive.
173  * connection informations are delivered these callbacks.
174  * @param[in]   ConnHandler     Connection status changes callback.
175  */
176 void CATCPSetKeepAliveCallbacks(CAKeepAliveConnectionCallback ConnHandler);
177
178 #ifdef __cplusplus
179 } /* extern "C" */
180 #endif
181
182 #endif  // CA_TCP_ADAPTER_H_