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 * Start listening server for receiving connect requests.
117 * Transport Specific Behavior:
118 * TCP Starts Listening Server on a particular interface and prefixed port
119 * number and as per OIC Specification.
120 * @return ::CA_STATUS_OK or Appropriate error code.
122 CAResult_t CAStartTCPListeningServer();
125 * Stops listening server from receiving connect requests.
126 * Transport Specific Behavior:
127 * TCP Stops 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 CAStopTCPListeningServer();
134 * Start discovery servers for receiving advertisements.
135 * Transport Specific Behavior:
136 * TCP Starts Discovery server on a particular interface and prefixed port
137 * number as per OIC Specification.
138 * @return ::CA_STATUS_OK or Appropriate error code.
140 CAResult_t CAStartTCPDiscoveryServer();
143 * Sends data to the endpoint using the adapter connectivity.
144 * @param[in] endpoint Remote Endpoint information (like ipaddress,
145 * port, reference uri and transport type) to
146 * which the unicast data has to be sent.
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.
153 int32_t CASendTCPUnicastData(const CAEndpoint_t *endpoint,
154 const void *data, uint32_t dataLen,
155 CADataType_t dataType);
158 * Send Multicast data to the endpoint using the TCP connectivity.
159 * @param[in] endpoint Remote Endpoint information (like ipaddress,
161 * @param[in] data Data which is required to be sent.
162 * @param[in] dataLen Size of data to be sent.
163 * @param[in] dataType Data type which is REQUEST or RESPONSE.
164 * @note dataLen must be > 0.
165 * @return The number of bytes sent on the network, or -1 upon error.
167 int32_t CASendTCPMulticastData(const CAEndpoint_t *endpoint,
168 const void *data, uint32_t dataLen,
169 CADataType_t dataType);
172 * Get TCP Connectivity network information.
173 * @param[out] info Local connectivity information structures.
174 * @note info is allocated in this API and should be freed by the caller.
175 * @param[out] size Number of local connectivity structures.
176 * @return ::CA_STATUS_OK or Appropriate error code.
178 CAResult_t CAGetTCPInterfaceInformation(CAEndpoint_t **info, uint32_t *size);
181 * Read Synchronous API callback.
182 * @return ::CA_STATUS_OK or Appropriate error code.
184 CAResult_t CAReadTCPData();
187 * Stops Unicast, servers and close the sockets.
188 * @return ::CA_STATUS_OK or Appropriate error code.
190 CAResult_t CAStopTCP();
193 * Terminate the TCP connectivity adapter.
194 * Configuration information will be deleted from further use.
196 void CATerminateTCP();
200 * Set connection status changes callback to process KeepAlive.
201 * connection informations are delivered these callbacks.
202 * @param[in] ConnHandler Connection status changes callback.
204 void CATCPSetKeepAliveCallbacks(CAKeepAliveConnectionCallback ConnHandler);
211 #endif // CA_TCP_ADAPTER_H_