Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
NetworkStack.h
00001 00002 /* NetworkStack 00003 * Copyright (c) 2015 ARM Limited 00004 * 00005 * Licensed under the Apache License, Version 2.0 (the "License"); 00006 * you may not use this file except in compliance with the License. 00007 * You may obtain a copy of the License at 00008 * 00009 * http://www.apache.org/licenses/LICENSE-2.0 00010 * 00011 * Unless required by applicable law or agreed to in writing, software 00012 * distributed under the License is distributed on an "AS IS" BASIS, 00013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00014 * See the License for the specific language governing permissions and 00015 * limitations under the License. 00016 */ 00017 00018 #ifndef NETWORK_STACK_H 00019 #define NETWORK_STACK_H 00020 00021 #include "nsapi_types.h" 00022 #include "netsocket/SocketAddress.h" 00023 #include "netsocket/NetworkInterface.h" 00024 #include "DNS.h" 00025 00026 // Predeclared classes 00027 class OnboardNetworkStack; 00028 00029 /** NetworkStack class 00030 * 00031 * Common interface that is shared between hardware that 00032 * can connect to a network over IP. By implementing the 00033 * NetworkStack, a network stack can be used as a target 00034 * for instantiating network sockets. 00035 * @addtogroup netsocket 00036 */ 00037 class NetworkStack: public DNS 00038 { 00039 public: 00040 virtual ~NetworkStack() {}; 00041 00042 /** Get the local IP address 00043 * 00044 * @return Null-terminated representation of the local IP address 00045 * or null if not yet connected 00046 */ 00047 virtual const char *get_ip_address(); 00048 00049 /** Translates a hostname to an IP address with specific version 00050 * 00051 * The hostname may be either a domain name or an IP address. If the 00052 * hostname is an IP address, no network transactions will be performed. 00053 * 00054 * If no stack-specific DNS resolution is provided, the hostname 00055 * will be resolve using a UDP socket on the stack. 00056 * 00057 * @param host Hostname to resolve 00058 * @param address Destination for the host SocketAddress 00059 * @param version IP version of address to resolve, NSAPI_UNSPEC indicates 00060 * version is chosen by the stack (defaults to NSAPI_UNSPEC) 00061 * @return 0 on success, negative error code on failure 00062 */ 00063 virtual nsapi_error_t gethostbyname(const char *host, 00064 SocketAddress *address, nsapi_version_t version = NSAPI_UNSPEC ); 00065 00066 /** Hostname translation callback (asynchronous) 00067 * 00068 * Callback will be called after DNS resolution completes or a failure occurs. 00069 * 00070 * Callback should not take more than 10ms to execute, otherwise it might 00071 * prevent underlying thread processing. A portable user of the callback 00072 * should not make calls to network operations due to stack size limitations. 00073 * The callback should not perform expensive operations such as socket recv/send 00074 * calls or blocking operations. 00075 * 00076 * @param status 0 on success, negative error code on failure 00077 * @param address On success, destination for the host SocketAddress 00078 */ 00079 typedef mbed::Callback<void (nsapi_error_t result, SocketAddress *address)> hostbyname_cb_t; 00080 00081 /** Translates a hostname to an IP address (asynchronous) 00082 * 00083 * The hostname may be either a domain name or an IP address. If the 00084 * hostname is an IP address, no network transactions will be performed. 00085 * 00086 * If no stack-specific DNS resolution is provided, the hostname 00087 * will be resolve using a UDP socket on the stack. 00088 * 00089 * Call is non-blocking. Result of the DNS operation is returned by the callback. 00090 * If this function returns failure, callback will not be called. In case result 00091 * is success (IP address was found from DNS cache), callback will be called 00092 * before function returns. 00093 * 00094 * @param host Hostname to resolve 00095 * @param callback Callback that is called for result 00096 * @param version IP version of address to resolve, NSAPI_UNSPEC indicates 00097 * version is chosen by the stack (defaults to NSAPI_UNSPEC) 00098 * @return 0 on immediate success, 00099 * negative error code on immediate failure or 00100 * a positive unique id that represents the hostname translation operation 00101 * and can be passed to cancel 00102 */ 00103 virtual nsapi_value_or_error_t gethostbyname_async(const char *host, hostbyname_cb_t callback, 00104 nsapi_version_t version = NSAPI_UNSPEC ); 00105 00106 /** Cancels asynchronous hostname translation 00107 * 00108 * When translation is cancelled, callback will not be called. 00109 * 00110 * @param id Unique id of the hostname translation operation 00111 * @return 0 on success, negative error code on failure 00112 */ 00113 virtual nsapi_error_t gethostbyname_async_cancel(int id); 00114 00115 /** Add a domain name server to list of servers to query 00116 * 00117 * @param address Destination for the host address 00118 * @return 0 on success, negative error code on failure 00119 */ 00120 virtual nsapi_error_t add_dns_server(const SocketAddress &address); 00121 00122 /** Get a domain name server from a list of servers to query 00123 * 00124 * Returns a DNS server address for a index. If returns error no more 00125 * DNS servers to read. 00126 * 00127 * @param index Index of the DNS server, starts from zero 00128 * @param address Destination for the host address 00129 * @return 0 on success, negative error code on failure 00130 */ 00131 virtual nsapi_error_t get_dns_server(int index, SocketAddress *address); 00132 00133 /* Set stack options 00134 * 00135 * setstackopt allows an application to pass stack-specific options 00136 * to the underlying stack using stack-specific level and option names, 00137 * or to request generic options using levels from nsapi_stack_level_t. 00138 * 00139 * For unsupported options, NSAPI_ERROR_UNSUPPORTED is returned 00140 * and the stack is unmodified. 00141 * 00142 * @param level Stack-specific protocol level or nsapi_stack_level_t 00143 * @param optname Level-specific option name 00144 * @param optval Option value 00145 * @param optlen Length of the option value 00146 * @return 0 on success, negative error code on failure 00147 */ 00148 virtual nsapi_error_t setstackopt(int level, int optname, const void *optval, unsigned optlen); 00149 00150 /* Get stack options 00151 * 00152 * getstackopt allows an application to retrieve stack-specific options 00153 * to the underlying stack using stack-specific level and option names, 00154 * or to request generic options using levels from nsapi_stack_level_t. 00155 * 00156 * @param level Stack-specific protocol level or nsapi_stack_level_t 00157 * @param optname Level-specific option name 00158 * @param optval Destination for option value 00159 * @param optlen Length of the option value 00160 * @return 0 on success, negative error code on failure 00161 */ 00162 virtual nsapi_error_t getstackopt(int level, int optname, void *optval, unsigned *optlen); 00163 00164 /** Dynamic downcast to a OnboardNetworkStack */ 00165 virtual OnboardNetworkStack *onboardNetworkStack() { return 0; } 00166 00167 protected: 00168 friend class Socket; 00169 friend class UDPSocket; 00170 friend class TCPSocket; 00171 friend class TCPServer; 00172 00173 /** Opens a socket 00174 * 00175 * Creates a network socket and stores it in the specified handle. 00176 * The handle must be passed to following calls on the socket. 00177 * 00178 * A stack may have a finite number of sockets, in this case 00179 * NSAPI_ERROR_NO_SOCKET is returned if no socket is available. 00180 * 00181 * @param handle Destination for the handle to a newly created socket 00182 * @param proto Protocol of socket to open, NSAPI_TCP or NSAPI_UDP 00183 * @return 0 on success, negative error code on failure 00184 */ 00185 virtual nsapi_error_t socket_open(nsapi_socket_t *handle, nsapi_protocol_t proto) = 0; 00186 00187 /** Close the socket 00188 * 00189 * Closes any open connection and deallocates any memory associated 00190 * with the socket. 00191 * 00192 * @param handle Socket handle 00193 * @return 0 on success, negative error code on failure 00194 */ 00195 virtual nsapi_error_t socket_close(nsapi_socket_t handle) = 0; 00196 00197 /** Bind a specific address to a socket 00198 * 00199 * Binding a socket specifies the address and port on which to receive 00200 * data. If the IP address is zeroed, only the port is bound. 00201 * 00202 * @param handle Socket handle 00203 * @param address Local address to bind 00204 * @return 0 on success, negative error code on failure. 00205 */ 00206 virtual nsapi_error_t socket_bind(nsapi_socket_t handle, const SocketAddress &address) = 0; 00207 00208 /** Listen for connections on a TCP socket 00209 * 00210 * Marks the socket as a passive socket that can be used to accept 00211 * incoming connections. 00212 * 00213 * @param handle Socket handle 00214 * @param backlog Number of pending connections that can be queued 00215 * simultaneously 00216 * @return 0 on success, negative error code on failure 00217 */ 00218 virtual nsapi_error_t socket_listen(nsapi_socket_t handle, int backlog) = 0; 00219 00220 /** Connects TCP socket to a remote host 00221 * 00222 * Initiates a connection to a remote server specified by the 00223 * indicated address. 00224 * 00225 * @param handle Socket handle 00226 * @param address The SocketAddress of the remote host 00227 * @return 0 on success, negative error code on failure 00228 */ 00229 virtual nsapi_error_t socket_connect(nsapi_socket_t handle, const SocketAddress &address) = 0; 00230 00231 /** Accepts a connection on a TCP socket 00232 * 00233 * The server socket must be bound and set to listen for connections. 00234 * On a new connection, creates a network socket and stores it in the 00235 * specified handle. The handle must be passed to following calls on 00236 * the socket. 00237 * 00238 * A stack may have a finite number of sockets, in this case 00239 * NSAPI_ERROR_NO_SOCKET is returned if no socket is available. 00240 * 00241 * This call is non-blocking. If accept would block, 00242 * NSAPI_ERROR_WOULD_BLOCK is returned immediately. 00243 * 00244 * @param server Socket handle to server to accept from 00245 * @param handle Destination for a handle to the newly created socket 00246 * @param address Destination for the remote address or NULL 00247 * @return 0 on success, negative error code on failure 00248 */ 00249 virtual nsapi_error_t socket_accept(nsapi_socket_t server, 00250 nsapi_socket_t *handle, SocketAddress *address=0) = 0; 00251 00252 /** Send data over a TCP socket 00253 * 00254 * The socket must be connected to a remote host. Returns the number of 00255 * bytes sent from the buffer. 00256 * 00257 * This call is non-blocking. If send would block, 00258 * NSAPI_ERROR_WOULD_BLOCK is returned immediately. 00259 * 00260 * @param handle Socket handle 00261 * @param data Buffer of data to send to the host 00262 * @param size Size of the buffer in bytes 00263 * @return Number of sent bytes on success, negative error 00264 * code on failure 00265 */ 00266 virtual nsapi_size_or_error_t socket_send(nsapi_socket_t handle, 00267 const void *data, nsapi_size_t size) = 0; 00268 00269 /** Receive data over a TCP socket 00270 * 00271 * The socket must be connected to a remote host. Returns the number of 00272 * bytes received into the buffer. 00273 * 00274 * This call is non-blocking. If recv would block, 00275 * NSAPI_ERROR_WOULD_BLOCK is returned immediately. 00276 * 00277 * @param handle Socket handle 00278 * @param data Destination buffer for data received from the host 00279 * @param size Size of the buffer in bytes 00280 * @return Number of received bytes on success, negative error 00281 * code on failure 00282 */ 00283 virtual nsapi_size_or_error_t socket_recv(nsapi_socket_t handle, 00284 void *data, nsapi_size_t size) = 0; 00285 00286 /** Send a packet over a UDP socket 00287 * 00288 * Sends data to the specified address. Returns the number of bytes 00289 * sent from the buffer. 00290 * 00291 * This call is non-blocking. If sendto would block, 00292 * NSAPI_ERROR_WOULD_BLOCK is returned immediately. 00293 * 00294 * @param handle Socket handle 00295 * @param address The SocketAddress of the remote host 00296 * @param data Buffer of data to send to the host 00297 * @param size Size of the buffer in bytes 00298 * @return Number of sent bytes on success, negative error 00299 * code on failure 00300 */ 00301 virtual nsapi_size_or_error_t socket_sendto(nsapi_socket_t handle, const SocketAddress &address, 00302 const void *data, nsapi_size_t size) = 0; 00303 00304 /** Receive a packet over a UDP socket 00305 * 00306 * Receives data and stores the source address in address if address 00307 * is not NULL. Returns the number of bytes received into the buffer. 00308 * 00309 * This call is non-blocking. If recvfrom would block, 00310 * NSAPI_ERROR_WOULD_BLOCK is returned immediately. 00311 * 00312 * @param handle Socket handle 00313 * @param address Destination for the source address or NULL 00314 * @param buffer Destination buffer for data received from the host 00315 * @param size Size of the buffer in bytes 00316 * @return Number of received bytes on success, negative error 00317 * code on failure 00318 */ 00319 virtual nsapi_size_or_error_t socket_recvfrom(nsapi_socket_t handle, SocketAddress *address, 00320 void *buffer, nsapi_size_t size) = 0; 00321 00322 /** Register a callback on state change of the socket 00323 * 00324 * The specified callback will be called on state changes such as when 00325 * the socket can recv/send/accept successfully and on when an error 00326 * occurs. The callback may also be called spuriously without reason. 00327 * 00328 * The callback may be called in an interrupt context and should not 00329 * perform expensive operations such as recv/send calls. 00330 * 00331 * @param handle Socket handle 00332 * @param callback Function to call on state change 00333 * @param data Argument to pass to callback 00334 */ 00335 virtual void socket_attach(nsapi_socket_t handle, void (*callback)(void *), void *data) = 0; 00336 00337 /* Set stack-specific socket options 00338 * 00339 * The setsockopt allow an application to pass stack-specific hints 00340 * to the underlying stack. For unsupported options, 00341 * NSAPI_ERROR_UNSUPPORTED is returned and the socket is unmodified. 00342 * 00343 * @param handle Socket handle 00344 * @param level Stack-specific protocol level 00345 * @param optname Stack-specific option identifier 00346 * @param optval Option value 00347 * @param optlen Length of the option value 00348 * @return 0 on success, negative error code on failure 00349 */ 00350 virtual nsapi_error_t setsockopt(nsapi_socket_t handle, int level, 00351 int optname, const void *optval, unsigned optlen); 00352 00353 /* Get stack-specific socket options 00354 * 00355 * The getstackopt allow an application to retrieve stack-specific hints 00356 * from the underlying stack. For unsupported options, 00357 * NSAPI_ERROR_UNSUPPORTED is returned and optval is unmodified. 00358 * 00359 * @param handle Socket handle 00360 * @param level Stack-specific protocol level 00361 * @param optname Stack-specific option identifier 00362 * @param optval Destination for option value 00363 * @param optlen Length of the option value 00364 * @return 0 on success, negative error code on failure 00365 */ 00366 virtual nsapi_error_t getsockopt(nsapi_socket_t handle, int level, 00367 int optname, void *optval, unsigned *optlen); 00368 00369 private: 00370 00371 /** Call in callback 00372 * 00373 * Callback is used to call the call in method of the network stack. 00374 */ 00375 typedef mbed::Callback<nsapi_error_t (int delay_ms, mbed::Callback<void()> user_cb)> call_in_callback_cb_t; 00376 00377 /** Get a call in callback 00378 * 00379 * Get a call in callback from the network stack context. 00380 * 00381 * Callback should not take more than 10ms to execute, otherwise it might 00382 * prevent underlying thread processing. A portable user of the callback 00383 * should not make calls to network operations due to stack size limitations. 00384 * The callback should not perform expensive operations such as socket recv/send 00385 * calls or blocking operations. 00386 * 00387 * @return Call in callback 00388 */ 00389 virtual call_in_callback_cb_t get_call_in_callback(); 00390 00391 /** Call a callback after a delay 00392 * 00393 * Call a callback from the network stack context after a delay. If function 00394 * returns error callback will not be called. 00395 * 00396 * @param delay Delay in milliseconds 00397 * @param func Callback to be called 00398 * @return 0 on success, negative error code on failure 00399 */ 00400 virtual nsapi_error_t call_in(int delay, mbed::Callback<void()> func); 00401 }; 00402 00403 /** Convert a raw nsapi_stack_t object into a C++ NetworkStack object 00404 * 00405 * @param stack Reference to an object that can be converted to a stack 00406 * - A raw nsapi_stack_t object 00407 * - A reference to a network stack 00408 * - A reference to a network interface 00409 * @return Reference to the underlying network stack 00410 */ 00411 NetworkStack *nsapi_create_stack(nsapi_stack_t *stack); 00412 NetworkStack *nsapi_create_stack(NetworkStack *stack); 00413 00414 template <typename IF> 00415 NetworkStack *nsapi_create_stack(IF *iface) 00416 { 00417 return nsapi_create_stack(static_cast<NetworkInterface *>(iface)->get_stack()); 00418 } 00419 00420 00421 #endif
Generated on Tue Jul 12 2022 12:45:36 by
1.7.2