2 * IoT Platform V1.1.0
\r
3 * Copyright (C) 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
24 * @file iot_network.h
\r
25 * @brief Abstraction of network functions used by libraries in this SDK.
\r
28 #ifndef IOT_NETWORK_H_
\r
29 #define IOT_NETWORK_H_
\r
31 /* The config header is always included first. */
\r
32 #include "iot_config.h"
\r
34 /* Standard includes. */
\r
39 * @ingroup platform_datatypes_enums
\r
40 * @brief Return codes for [network functions](@ref platform_network_functions).
\r
42 typedef enum IotNetworkError
\r
44 IOT_NETWORK_SUCCESS = 0, /**< Function successfully completed. */
\r
45 IOT_NETWORK_FAILURE, /**< Generic failure not covered by other values. */
\r
46 IOT_NETWORK_BAD_PARAMETER, /**< At least one parameter was invalid. */
\r
47 IOT_NETWORK_NO_MEMORY, /**< Memory allocation failed. */
\r
48 IOT_NETWORK_SYSTEM_ERROR /**< An error occurred when calling a system API. */
\r
49 } IotNetworkError_t;
\r
52 * @page platform_network_functions Networking
\r
53 * @brief Functions of the network abstraction component.
\r
55 * The network abstraction component does not declare functions, but uses function
\r
56 * pointers instead. This allows multiple network stacks to be used at the same time.
\r
57 * Libraries that require the network will request an #IotNetworkInterface_t
\r
58 * parameter. The members of the #IotNetworkInterface_t will be called whenever
\r
59 * the library interacts with the network.
\r
61 * The following function pointers are associated with an #IotNetworkInterface_t.
\r
62 * Together, they represent a network stack.
\r
63 * - @functionname{platform_network_function_create}
\r
64 * - @functionname{platform_network_function_setreceivecallback}
\r
65 * - @functionname{platform_network_function_send}
\r
66 * - @functionname{platform_network_function_receive}
\r
67 * - @functionname{platform_network_function_close}
\r
68 * - @functionname{platform_network_function_destroy}
\r
69 * - @functionname{platform_network_function_receivecallback}
\r
73 * @functionpage{IotNetworkInterface_t::create,platform_network,create}
\r
74 * @functionpage{IotNetworkInterface_t::setReceiveCallback,platform_network,setreceivecallback}
\r
75 * @functionpage{IotNetworkInterface_t::send,platform_network,send}
\r
76 * @functionpage{IotNetworkInterface_t::receive,platform_network,receive}
\r
77 * @functionpage{IotNetworkInterface_t::close,platform_network,close}
\r
78 * @functionpage{IotNetworkInterface_t::destroy,platform_network,destroy}
\r
79 * @functionpage{IotNetworkReceiveCallback_t,platform_network,receivecallback}
\r
83 * @brief Provide an asynchronous notification of incoming network data.
\r
85 * A function with this signature may be set with @ref platform_network_function_setreceivecallback
\r
86 * to be invoked when data is available on the network.
\r
88 * @param[in] pConnection The connection on which data is available, defined by
\r
89 * the network stack.
\r
90 * @param[in] pContext The third argument passed to @ref platform_network_function_setreceivecallback.
\r
92 /* @[declare_platform_network_receivecallback] */
\r
93 typedef void ( * IotNetworkReceiveCallback_t )( IotNetworkConnection_t pConnection,
\r
95 /* @[declare_platform_network_receivecallback] */
\r
98 * @ingroup platform_datatypes_paramstructs
\r
99 * @brief Represents the functions of a network stack.
\r
101 * Functions that match these signatures should be implemented against a system's
\r
102 * network stack. See the `platform` directory for existing implementations.
\r
104 typedef struct IotNetworkInterface
\r
107 * @brief Create a new network connection.
\r
109 * This function allocates resources and establishes a new network connection.
\r
110 * @param[in] pServerInfo Represents information needed to set up the
\r
111 * new connection, defined by the network stack.
\r
112 * @param[in] pCredentialInfo Represents information needed to secure the
\r
113 * new connection, defined by the network stack.
\r
114 * @param[out] pConnection Set to represent a new connection, defined by the
\r
117 * @return Any #IotNetworkError_t, as defined by the network stack.
\r
119 /* @[declare_platform_network_create] */
\r
120 IotNetworkError_t ( * create )( IotNetworkServerInfo_t pServerInfo,
\r
121 IotNetworkCredentials_t pCredentialInfo,
\r
122 IotNetworkConnection_t * pConnection );
\r
123 /* @[declare_platform_network_create] */
\r
126 * @brief Register an @ref platform_network_function_receivecallback.
\r
128 * Sets an @ref platform_network_function_receivecallback to be called
\r
129 * asynchronously when data arrives on the network. The network stack
\r
130 * should invoke this function "as if" it were the thread routine of a
\r
133 * Each network connection may only have one receive callback at any time.
\r
134 * @ref platform_network_function_close is expected to remove any active
\r
135 * receive callbacks.
\r
137 * @param[in] pConnection The connection to associate with the receive callback.
\r
138 * @param[in] receiveCallback The function to invoke for incoming network data.
\r
139 * @param[in] pContext A value to pass as the first parameter to the receive callback.
\r
141 * @return Any #IotNetworkError_t, as defined by the network stack.
\r
143 * @see platform_network_function_receivecallback
\r
145 /* @[declare_platform_network_setreceivecallback] */
\r
146 IotNetworkError_t ( * setReceiveCallback )( IotNetworkConnection_t pConnection,
\r
147 IotNetworkReceiveCallback_t receiveCallback,
\r
149 /* @[declare_platform_network_setreceivecallback] */
\r
152 * @brief Send data over a return connection.
\r
154 * Attempts to transmit `messageLength` bytes of `pMessage` across the
\r
155 * connection represented by `pConnection`. Returns the number of bytes
\r
156 * actually sent, `0` on failure.
\r
158 * @param[in] pConnection The connection used to send data, defined by the
\r
160 * @param[in] pMessage The message to send.
\r
161 * @param[in] messageLength The length of `pMessage`.
\r
163 * @return The number of bytes successfully sent, `0` on failure.
\r
165 /* @[declare_platform_network_send] */
\r
166 size_t ( * send )( IotNetworkConnection_t pConnection,
\r
167 const uint8_t * pMessage,
\r
168 size_t messageLength );
\r
169 /* @[declare_platform_network_send] */
\r
172 * @brief Block and wait for incoming network data.
\r
174 * Wait for a message of size `bytesRequested` to arrive on the network and
\r
175 * place it in `pBuffer`.
\r
177 * @param[in] pConnection The connection to wait on, defined by the network
\r
179 * @param[out] pBuffer Where to place the incoming network data. This buffer
\r
180 * must be at least `bytesRequested` in size.
\r
181 * @param[in] bytesRequested How many bytes to wait for. `pBuffer` must be at
\r
184 * @return The number of bytes successfully received. This should be
\r
185 * `bytesRequested` when successful. Any other value may indicate an error.
\r
187 /* @[declare_platform_network_receive] */
\r
188 size_t ( * receive )( IotNetworkConnection_t pConnection,
\r
190 size_t bytesRequested );
\r
192 /* @[declare_platform_network_receive] */
\r
195 * @brief Read incoming data available in the network buffers.
\r
197 * Reads bytes available in the network buffers into `pBuffer`.
\r
198 * - If there is less data available than requested, it will return
\r
199 * the available number of bytes.
\r
200 * - If there is more data available than requested, it will fill the
\r
202 * - If there is no data available, it will return 0.
\r
204 * @param[in] pConnection The connection to receive data on, defined by
\r
205 * the network stack.
\r
206 * @param[out] pBuffer The buffer to place the incoming network data.
\r
207 * @param[in] bufferSize The size of `pBuffer`.
\r
209 * @return The number of bytes successfully received.
\r
211 /* @[declare_platform_network_receiveupto] */
\r
212 size_t ( * receiveUpto )( IotNetworkConnection_t pConnection,
\r
214 size_t bufferSize );
\r
215 /* @[declare_platform_network_receiveupto] */
\r
219 * @brief Close a network connection.
\r
221 * This function closes the connection, but does not release the resources
\r
222 * used by the connection. This allows calls to other networking functions
\r
223 * to return an error and handle a closed connection without the risk of
\r
224 * crashing. Once it can be guaranteed that `pConnection` will no longer be
\r
225 * used, the connection can be destroyed with @ref platform_network_function_destroy.
\r
227 * In addition to closing the connection, this function should also remove
\r
228 * any active [receive callback](@ref platform_network_function_receivecallback).
\r
230 * @param[in] pConnection The network connection to close, defined by the
\r
233 * @return Any #IotNetworkError_t, as defined by the network stack.
\r
235 * @note It must be safe to call this function on an already-closed connection.
\r
237 /* @[declare_platform_network_close] */
\r
238 IotNetworkError_t ( * close )( IotNetworkConnection_t pConnection );
\r
239 /* @[declare_platform_network_close] */
\r
242 * @brief Free resources used by a network connection.
\r
244 * This function releases the resources of a closed connection. It should be
\r
245 * called after @ref platform_network_function_close.
\r
247 * @param[in] pConnection The network connection to destroy, defined by
\r
248 * the network stack.
\r
250 * @return Any #IotNetworkError_t, as defined by the network stack.
\r
252 * @attention No function should be called on the network connection after
\r
253 * calling this function. This function must be safe to call from a
\r
254 * [receive callback](@ref platform_network_function_receivecallback).
\r
256 /* @[declare_platform_network_destroy] */
\r
257 IotNetworkError_t ( * destroy )( IotNetworkConnection_t pConnection );
\r
258 /* @[declare_platform_network_destroy] */
\r
259 } IotNetworkInterface_t;
\r
262 * @ingroup platform_datatypes_paramstructs
\r
263 * @brief Information on the remote server for connection setup.
\r
265 * May be passed to #IotNetworkInterface_t.create as `pConnectionInfo`. This
\r
266 * structure contains commonly-used parameters, but may be replaced with an
\r
269 struct IotNetworkServerInfo
\r
271 const char * pHostName; /**< @brief Server host name. Must be NULL-terminated. */
\r
272 uint16_t port; /**< @brief Server port in host-order. */
\r
276 * @ingroup platform_datatypes_paramstructs
\r
277 * @brief Contains the credentials necessary for connection setup.
\r
279 * May be passed to #IotNetworkInterface_t.create as `pCredentialInfo`. This
\r
280 * structure contains commonly-used parameters, but may be replaced with an
\r
283 struct IotNetworkCredentials
\r
286 * @brief Set this to a non-NULL value to use ALPN.
\r
288 * This string must be NULL-terminated.
\r
291 * (https://aws.amazon.com/blogs/iot/mqtt-with-tls-client-authentication-on-port-443-why-it-is-useful-and-how-it-works/)
\r
292 * for more information.
\r
294 const char * pAlpnProtos;
\r
297 * @brief Set this to a non-zero value to use TLS max fragment length
\r
298 * negotiation (TLS MFLN).
\r
300 * @note The network stack may have a minimum value for this parameter and
\r
301 * may return an error if this parameter is too small.
\r
303 size_t maxFragmentLength;
\r
306 * @brief Disable server name indication (SNI) for a TLS session.
\r
310 const char * pRootCa; /**< @brief String representing a trusted server root certificate. */
\r
311 size_t rootCaSize; /**< @brief Size associated with #IotNetworkCredentials.pRootCa. */
\r
312 const char * pClientCert; /**< @brief String representing the client certificate. */
\r
313 size_t clientCertSize; /**< @brief Size associated with #IotNetworkCredentials.pClientCert. */
\r
314 const char * pPrivateKey; /**< @brief String representing the client certificate's private key. */
\r
315 size_t privateKeySize; /**< @brief Size associated with #IotNetworkCredentials.pPrivateKey. */
\r
318 #endif /* ifndef IOT_NETWORK_H_ */
\r