1 /* ****************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 ******************************************************************/
23 * This file contains the APIs for TCP Adapter.
25 #ifndef CA_TCP_ADAPTER_H_
26 #define CA_TCP_ADAPTER_H_
29 #include "caadapterinterface.h"
30 #include "cathreadpool.h"
31 #include "cainterface.h"
39 // TLS data max size (rfc5246: TLSCiphertext max (2^14+2048+5))
40 #define TLS_DATA_MAX_SIZE 18437
42 typedef enum CAProtocol
50 * TCP Connection State.
57 } CATCPConnectionState_t;
60 * TCP Session Information for IPv4 TCP transport
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. */
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
93 CAResult_t CAInitializeTCP(CARegisterConnectivityCallback registerCallback,
94 CANetworkPacketReceivedCallback networkPacketCallback,
95 CAAdapterChangeCallback netCallback,
96 CAConnectionChangeCallback connCallback,
97 CAErrorHandleCallback errorCallback, ca_thread_pool_t handle);
100 * Start TCP Interface adapter.
101 * @return ::CA_STATUS_OK or Appropriate error code.
103 CAResult_t CAStartTCP();
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,
113 CAResult_t CATCPDisconnectSession(const CAEndpoint_t *endpoint);
116 * Enable or disable skip closing TCP servers
117 * when network interface is down.
119 * @param[in] state true to skip closing TCP servers,
122 void CATCPSkipCloseOnInterfaceDown(bool state);
125 * Start listening server for receiving connect requests.
126 * Transport Specific Behavior:
127 * TCP Starts 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.
131 CAResult_t CAStartTCPListeningServer();
134 * Stops listening server from receiving connect requests.
135 * Transport Specific Behavior:
136 * TCP Stops Listening Server on a particular interface and prefixed port
137 * number and as per OIC Specification.
138 * @return ::CA_STATUS_OK or Appropriate error code.
140 CAResult_t CAStopTCPListeningServer();
143 * Start discovery servers for receiving advertisements.
144 * Transport Specific Behavior:
145 * TCP Starts Discovery server on a particular interface and prefixed port
146 * number as per OIC Specification.
147 * @return ::CA_STATUS_OK or Appropriate error code.
149 CAResult_t CAStartTCPDiscoveryServer();
152 * Sends data to the endpoint using the adapter connectivity.
153 * @param[in] endpoint Remote Endpoint information (like ipaddress,
154 * port, reference uri and transport type) to
155 * which the unicast data has to be sent.
156 * @param[in] data Data which is required to be sent.
157 * @param[in] dataLen Size of data to be sent.
158 * @param[in] dataType Data type which is REQUEST or RESPONSE.
159 * @note dataLen must be > 0.
160 * @return The number of bytes sent on the network, or -1 upon error.
162 int32_t CASendTCPUnicastData(const CAEndpoint_t *endpoint,
163 const void *data, uint32_t dataLen,
164 CADataType_t dataType);
167 * Send Multicast data to the endpoint using the TCP connectivity.
168 * @param[in] endpoint Remote Endpoint information (like ipaddress,
170 * @param[in] data Data which is required to be sent.
171 * @param[in] dataLen Size of data to be sent.
172 * @param[in] dataType Data type which is REQUEST or RESPONSE.
173 * @note dataLen must be > 0.
174 * @return The number of bytes sent on the network, or -1 upon error.
176 int32_t CASendTCPMulticastData(const CAEndpoint_t *endpoint,
177 const void *data, uint32_t dataLen,
178 CADataType_t dataType);
181 * Get TCP Connectivity network information.
182 * @param[out] info Local connectivity information structures.
183 * @note info is allocated in this API and should be freed by the caller.
184 * @param[out] size Number of local connectivity structures.
185 * @return ::CA_STATUS_OK or Appropriate error code.
187 CAResult_t CAGetTCPInterfaceInformation(CAEndpoint_t **info, uint32_t *size);
190 * Read Synchronous API callback.
191 * @return ::CA_STATUS_OK or Appropriate error code.
193 CAResult_t CAReadTCPData();
196 * Stops Unicast, servers and close the sockets.
197 * @return ::CA_STATUS_OK or Appropriate error code.
199 CAResult_t CAStopTCP();
202 * Terminate the TCP connectivity adapter.
203 * Configuration information will be deleted from further use.
205 void CATerminateTCP();
209 * Set connection status changes callback to process KeepAlive.
210 * connection informations are delivered these callbacks.
211 * @param[in] ConnHandler Connection status changes callback.
213 void CATCPSetKeepAliveCallbacks(CAKeepAliveConnectionCallback ConnHandler);
220 #endif // CA_TCP_ADAPTER_H_