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.
Dependents: blinky_max32630fthr
NetworkStack.h
00001 00002 /** \addtogroup netsocket */ 00003 /** @{*/ 00004 /* NetworkStack 00005 * Copyright (c) 2015 ARM Limited 00006 * 00007 * Licensed under the Apache License, Version 2.0 (the "License"); 00008 * you may not use this file except in compliance with the License. 00009 * You may obtain a copy of the License at 00010 * 00011 * http://www.apache.org/licenses/LICENSE-2.0 00012 * 00013 * Unless required by applicable law or agreed to in writing, software 00014 * distributed under the License is distributed on an "AS IS" BASIS, 00015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00016 * See the License for the specific language governing permissions and 00017 * limitations under the License. 00018 */ 00019 00020 #ifndef NETWORK_STACK_H 00021 #define NETWORK_STACK_H 00022 00023 #include "nsapi_types.h" 00024 #include "netsocket/SocketAddress.h" 00025 #include "netsocket/NetworkInterface.h" 00026 00027 00028 /** NetworkStack class 00029 * 00030 * Common interface that is shared between hardware that 00031 * can connect to a network over IP. By implementing the 00032 * NetworkStack, a network stack can be used as a target 00033 * for instantiating network sockets. 00034 */ 00035 class NetworkStack 00036 { 00037 public: 00038 virtual ~NetworkStack() {}; 00039 00040 /** Get the local IP address 00041 * 00042 * @return Null-terminated representation of the local IP address 00043 * or null if not yet connected 00044 */ 00045 virtual const char *get_ip_address() = 0; 00046 00047 /** Translates a hostname to an IP address with specific version 00048 * 00049 * The hostname may be either a domain name or an IP address. If the 00050 * hostname is an IP address, no network transactions will be performed. 00051 * 00052 * If no stack-specific DNS resolution is provided, the hostname 00053 * will be resolve using a UDP socket on the stack. 00054 * 00055 * @param host Hostname to resolve 00056 * @param address Destination for the host SocketAddress 00057 * @param version IP version of address to resolve, NSAPI_UNSPEC indicates 00058 * version is chosen by the stack (defaults to NSAPI_UNSPEC) 00059 * @return 0 on success, negative error code on failure 00060 */ 00061 virtual nsapi_error_t gethostbyname(const char *host, 00062 SocketAddress *address, nsapi_version_t version = NSAPI_UNSPEC ); 00063 00064 /** Add a domain name server to list of servers to query 00065 * 00066 * @param addr Destination for the host address 00067 * @return 0 on success, negative error code on failure 00068 */ 00069 virtual nsapi_error_t add_dns_server(const SocketAddress &address); 00070 00071 /* Set stack-specific stack options 00072 * 00073 * The setstackopt allow an application to pass stack-specific hints 00074 * to the underlying stack. For unsupported options, 00075 * NSAPI_ERROR_UNSUPPORTED is returned and the stack is unmodified. 00076 * 00077 * @param level Stack-specific protocol level 00078 * @param optname Stack-specific option identifier 00079 * @param optval Option value 00080 * @param optlen Length of the option value 00081 * @return 0 on success, negative error code on failure 00082 */ 00083 virtual nsapi_error_t setstackopt(int level, int optname, const void *optval, unsigned optlen); 00084 00085 /* Get stack-specific stack options 00086 * 00087 * The getstackopt allow an application to retrieve stack-specific hints 00088 * from the underlying stack. For unsupported options, 00089 * NSAPI_ERROR_UNSUPPORTED is returned and optval is unmodified. 00090 * 00091 * @param level Stack-specific protocol level 00092 * @param optname Stack-specific option identifier 00093 * @param optval Destination for option value 00094 * @param optlen Length of the option value 00095 * @return 0 on success, negative error code on failure 00096 */ 00097 virtual nsapi_error_t getstackopt(int level, int optname, void *optval, unsigned *optlen); 00098 00099 protected: 00100 friend class Socket; 00101 friend class UDPSocket; 00102 friend class TCPSocket; 00103 friend class TCPServer; 00104 00105 /** Opens a socket 00106 * 00107 * Creates a network socket and stores it in the specified handle. 00108 * The handle must be passed to following calls on the socket. 00109 * 00110 * A stack may have a finite number of sockets, in this case 00111 * NSAPI_ERROR_NO_SOCKET is returned if no socket is available. 00112 * 00113 * @param handle Destination for the handle to a newly created socket 00114 * @param proto Protocol of socket to open, NSAPI_TCP or NSAPI_UDP 00115 * @return 0 on success, negative error code on failure 00116 */ 00117 virtual nsapi_error_t socket_open(nsapi_socket_t *handle, nsapi_protocol_t proto) = 0; 00118 00119 /** Close the socket 00120 * 00121 * Closes any open connection and deallocates any memory associated 00122 * with the socket. 00123 * 00124 * @param handle Socket handle 00125 * @return 0 on success, negative error code on failure 00126 */ 00127 virtual nsapi_error_t socket_close(nsapi_socket_t handle) = 0; 00128 00129 /** Bind a specific address to a socket 00130 * 00131 * Binding a socket specifies the address and port on which to recieve 00132 * data. If the IP address is zeroed, only the port is bound. 00133 * 00134 * @param handle Socket handle 00135 * @param address Local address to bind 00136 * @return 0 on success, negative error code on failure. 00137 */ 00138 virtual nsapi_error_t socket_bind(nsapi_socket_t handle, const SocketAddress &address) = 0; 00139 00140 /** Listen for connections on a TCP socket 00141 * 00142 * Marks the socket as a passive socket that can be used to accept 00143 * incoming connections. 00144 * 00145 * @param handle Socket handle 00146 * @param backlog Number of pending connections that can be queued 00147 * simultaneously 00148 * @return 0 on success, negative error code on failure 00149 */ 00150 virtual nsapi_error_t socket_listen(nsapi_socket_t handle, int backlog) = 0; 00151 00152 /** Connects TCP socket to a remote host 00153 * 00154 * Initiates a connection to a remote server specified by the 00155 * indicated address. 00156 * 00157 * @param handle Socket handle 00158 * @param address The SocketAddress of the remote host 00159 * @return 0 on success, negative error code on failure 00160 */ 00161 virtual nsapi_error_t socket_connect(nsapi_socket_t handle, const SocketAddress &address) = 0; 00162 00163 /** Accepts a connection on a TCP socket 00164 * 00165 * The server socket must be bound and set to listen for connections. 00166 * On a new connection, creates a network socket and stores it in the 00167 * specified handle. The handle must be passed to following calls on 00168 * the socket. 00169 * 00170 * A stack may have a finite number of sockets, in this case 00171 * NSAPI_ERROR_NO_SOCKET is returned if no socket is available. 00172 * 00173 * This call is non-blocking. If accept would block, 00174 * NSAPI_ERROR_WOULD_BLOCK is returned immediately. 00175 * 00176 * @param server Socket handle to server to accept from 00177 * @param handle Destination for a handle to the newly created socket 00178 * @param address Destination for the remote address or NULL 00179 * @return 0 on success, negative error code on failure 00180 */ 00181 virtual nsapi_error_t socket_accept(nsapi_socket_t server, 00182 nsapi_socket_t *handle, SocketAddress *address=0) = 0; 00183 00184 /** Send data over a TCP socket 00185 * 00186 * The socket must be connected to a remote host. Returns the number of 00187 * bytes sent from the buffer. 00188 * 00189 * This call is non-blocking. If send would block, 00190 * NSAPI_ERROR_WOULD_BLOCK is returned immediately. 00191 * 00192 * @param handle Socket handle 00193 * @param data Buffer of data to send to the host 00194 * @param size Size of the buffer in bytes 00195 * @return Number of sent bytes on success, negative error 00196 * code on failure 00197 */ 00198 virtual nsapi_size_or_error_t socket_send(nsapi_socket_t handle, 00199 const void *data, nsapi_size_t size) = 0; 00200 00201 /** Receive data over a TCP socket 00202 * 00203 * The socket must be connected to a remote host. Returns the number of 00204 * bytes received into the buffer. 00205 * 00206 * This call is non-blocking. If recv would block, 00207 * NSAPI_ERROR_WOULD_BLOCK is returned immediately. 00208 * 00209 * @param handle Socket handle 00210 * @param data Destination buffer for data received from the host 00211 * @param size Size of the buffer in bytes 00212 * @return Number of received bytes on success, negative error 00213 * code on failure 00214 */ 00215 virtual nsapi_size_or_error_t socket_recv(nsapi_socket_t handle, 00216 void *data, nsapi_size_t size) = 0; 00217 00218 /** Send a packet over a UDP socket 00219 * 00220 * Sends data to the specified address. Returns the number of bytes 00221 * sent from the buffer. 00222 * 00223 * This call is non-blocking. If sendto would block, 00224 * NSAPI_ERROR_WOULD_BLOCK is returned immediately. 00225 * 00226 * @param handle Socket handle 00227 * @param address The SocketAddress of the remote host 00228 * @param data Buffer of data to send to the host 00229 * @param size Size of the buffer in bytes 00230 * @return Number of sent bytes on success, negative error 00231 * code on failure 00232 */ 00233 virtual nsapi_size_or_error_t socket_sendto(nsapi_socket_t handle, const SocketAddress &address, 00234 const void *data, nsapi_size_t size) = 0; 00235 00236 /** Receive a packet over a UDP socket 00237 * 00238 * Receives data and stores the source address in address if address 00239 * is not NULL. Returns the number of bytes received into the buffer. 00240 * 00241 * This call is non-blocking. If recvfrom would block, 00242 * NSAPI_ERROR_WOULD_BLOCK is returned immediately. 00243 * 00244 * @param handle Socket handle 00245 * @param address Destination for the source address or NULL 00246 * @param data Destination buffer for data received from the host 00247 * @param size Size of the buffer in bytes 00248 * @return Number of received bytes on success, negative error 00249 * code on failure 00250 */ 00251 virtual nsapi_size_or_error_t socket_recvfrom(nsapi_socket_t handle, SocketAddress *address, 00252 void *buffer, nsapi_size_t size) = 0; 00253 00254 /** Register a callback on state change of the socket 00255 * 00256 * The specified callback will be called on state changes such as when 00257 * the socket can recv/send/accept successfully and on when an error 00258 * occurs. The callback may also be called spuriously without reason. 00259 * 00260 * The callback may be called in an interrupt context and should not 00261 * perform expensive operations such as recv/send calls. 00262 * 00263 * @param handle Socket handle 00264 * @param callback Function to call on state change 00265 * @param data Argument to pass to callback 00266 */ 00267 virtual void socket_attach(nsapi_socket_t handle, void (*callback)(void *), void *data) = 0; 00268 00269 /* Set stack-specific socket options 00270 * 00271 * The setsockopt allow an application to pass stack-specific hints 00272 * to the underlying stack. For unsupported options, 00273 * NSAPI_ERROR_UNSUPPORTED is returned and the socket is unmodified. 00274 * 00275 * @param handle Socket handle 00276 * @param level Stack-specific protocol level 00277 * @param optname Stack-specific option identifier 00278 * @param optval Option value 00279 * @param optlen Length of the option value 00280 * @return 0 on success, negative error code on failure 00281 */ 00282 virtual nsapi_error_t setsockopt(nsapi_socket_t handle, int level, 00283 int optname, const void *optval, unsigned optlen); 00284 00285 /* Get stack-specific socket options 00286 * 00287 * The getstackopt allow an application to retrieve stack-specific hints 00288 * from the underlying stack. For unsupported options, 00289 * NSAPI_ERROR_UNSUPPORTED is returned and optval is unmodified. 00290 * 00291 * @param handle Socket handle 00292 * @param level Stack-specific protocol level 00293 * @param optname Stack-specific option identifier 00294 * @param optval Destination for option value 00295 * @param optlen Length of the option value 00296 * @return 0 on success, negative error code on failure 00297 */ 00298 virtual nsapi_error_t getsockopt(nsapi_socket_t handle, int level, 00299 int optname, void *optval, unsigned *optlen); 00300 }; 00301 00302 00303 /** Convert a raw nsapi_stack_t object into a C++ NetworkStack object 00304 * 00305 * @param stack Reference to an object that can be converted to a stack 00306 * - A raw nsapi_stack_t object 00307 * - A reference to a network stack 00308 * - A reference to a network interface 00309 * @return Reference to the underlying network stack 00310 */ 00311 NetworkStack *nsapi_create_stack(nsapi_stack_t *stack); 00312 NetworkStack *nsapi_create_stack(NetworkStack *stack); 00313 00314 template <typename IF> 00315 NetworkStack *nsapi_create_stack(IF *iface) 00316 { 00317 return nsapi_create_stack(static_cast<NetworkInterface *>(iface)->get_stack()); 00318 } 00319 00320 00321 #endif 00322 00323 /** @}*/
Generated on Tue Jul 12 2022 14:21:16 by
1.7.2