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: mbed-TFT-example-NCS36510 mbed-Accelerometer-example-NCS36510 mbed-Accelerometer-example-NCS36510
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 options 00072 * 00073 * setstackopt allows an application to pass stack-specific options 00074 * to the underlying stack using stack-specific level and option names, 00075 * or to request generic options using levels from nsapi_stack_level_t. 00076 * 00077 * For unsupported options, NSAPI_ERROR_UNSUPPORTED is returned 00078 * and the stack is unmodified. 00079 * 00080 * @param level Stack-specific protocol level or nsapi_stack_level_t 00081 * @param optname Level-specific option name 00082 * @param optval Option value 00083 * @param optlen Length of the option value 00084 * @return 0 on success, negative error code on failure 00085 */ 00086 virtual nsapi_error_t setstackopt(int level, int optname, const void *optval, unsigned optlen); 00087 00088 /* Get stack options 00089 * 00090 * getstackopt allows an application to retrieve stack-specific options 00091 * to the underlying stack using stack-specific level and option names, 00092 * or to request generic options using levels from nsapi_stack_level_t. 00093 * 00094 * @param level Stack-specific protocol level or nsapi_stack_level_t 00095 * @param optname Level-specific option name 00096 * @param optval Destination for option value 00097 * @param optlen Length of the option value 00098 * @return 0 on success, negative error code on failure 00099 */ 00100 virtual nsapi_error_t getstackopt(int level, int optname, void *optval, unsigned *optlen); 00101 00102 protected: 00103 friend class Socket; 00104 friend class UDPSocket; 00105 friend class TCPSocket; 00106 friend class TCPServer; 00107 00108 /** Opens a socket 00109 * 00110 * Creates a network socket and stores it in the specified handle. 00111 * The handle must be passed to following calls on the socket. 00112 * 00113 * A stack may have a finite number of sockets, in this case 00114 * NSAPI_ERROR_NO_SOCKET is returned if no socket is available. 00115 * 00116 * @param handle Destination for the handle to a newly created socket 00117 * @param proto Protocol of socket to open, NSAPI_TCP or NSAPI_UDP 00118 * @return 0 on success, negative error code on failure 00119 */ 00120 virtual nsapi_error_t socket_open(nsapi_socket_t *handle, nsapi_protocol_t proto) = 0; 00121 00122 /** Close the socket 00123 * 00124 * Closes any open connection and deallocates any memory associated 00125 * with the socket. 00126 * 00127 * @param handle Socket handle 00128 * @return 0 on success, negative error code on failure 00129 */ 00130 virtual nsapi_error_t socket_close(nsapi_socket_t handle) = 0; 00131 00132 /** Bind a specific address to a socket 00133 * 00134 * Binding a socket specifies the address and port on which to recieve 00135 * data. If the IP address is zeroed, only the port is bound. 00136 * 00137 * @param handle Socket handle 00138 * @param address Local address to bind 00139 * @return 0 on success, negative error code on failure. 00140 */ 00141 virtual nsapi_error_t socket_bind(nsapi_socket_t handle, const SocketAddress &address) = 0; 00142 00143 /** Listen for connections on a TCP socket 00144 * 00145 * Marks the socket as a passive socket that can be used to accept 00146 * incoming connections. 00147 * 00148 * @param handle Socket handle 00149 * @param backlog Number of pending connections that can be queued 00150 * simultaneously 00151 * @return 0 on success, negative error code on failure 00152 */ 00153 virtual nsapi_error_t socket_listen(nsapi_socket_t handle, int backlog) = 0; 00154 00155 /** Connects TCP socket to a remote host 00156 * 00157 * Initiates a connection to a remote server specified by the 00158 * indicated address. 00159 * 00160 * @param handle Socket handle 00161 * @param address The SocketAddress of the remote host 00162 * @return 0 on success, negative error code on failure 00163 */ 00164 virtual nsapi_error_t socket_connect(nsapi_socket_t handle, const SocketAddress &address) = 0; 00165 00166 /** Accepts a connection on a TCP socket 00167 * 00168 * The server socket must be bound and set to listen for connections. 00169 * On a new connection, creates a network socket and stores it in the 00170 * specified handle. The handle must be passed to following calls on 00171 * the socket. 00172 * 00173 * A stack may have a finite number of sockets, in this case 00174 * NSAPI_ERROR_NO_SOCKET is returned if no socket is available. 00175 * 00176 * This call is non-blocking. If accept would block, 00177 * NSAPI_ERROR_WOULD_BLOCK is returned immediately. 00178 * 00179 * @param server Socket handle to server to accept from 00180 * @param handle Destination for a handle to the newly created socket 00181 * @param address Destination for the remote address or NULL 00182 * @return 0 on success, negative error code on failure 00183 */ 00184 virtual nsapi_error_t socket_accept(nsapi_socket_t server, 00185 nsapi_socket_t *handle, SocketAddress *address=0) = 0; 00186 00187 /** Send data over a TCP socket 00188 * 00189 * The socket must be connected to a remote host. Returns the number of 00190 * bytes sent from the buffer. 00191 * 00192 * This call is non-blocking. If send would block, 00193 * NSAPI_ERROR_WOULD_BLOCK is returned immediately. 00194 * 00195 * @param handle Socket handle 00196 * @param data Buffer of data to send to the host 00197 * @param size Size of the buffer in bytes 00198 * @return Number of sent bytes on success, negative error 00199 * code on failure 00200 */ 00201 virtual nsapi_size_or_error_t socket_send(nsapi_socket_t handle, 00202 const void *data, nsapi_size_t size) = 0; 00203 00204 /** Receive data over a TCP socket 00205 * 00206 * The socket must be connected to a remote host. Returns the number of 00207 * bytes received into the buffer. 00208 * 00209 * This call is non-blocking. If recv would block, 00210 * NSAPI_ERROR_WOULD_BLOCK is returned immediately. 00211 * 00212 * @param handle Socket handle 00213 * @param data Destination buffer for data received from the host 00214 * @param size Size of the buffer in bytes 00215 * @return Number of received bytes on success, negative error 00216 * code on failure 00217 */ 00218 virtual nsapi_size_or_error_t socket_recv(nsapi_socket_t handle, 00219 void *data, nsapi_size_t size) = 0; 00220 00221 /** Send a packet over a UDP socket 00222 * 00223 * Sends data to the specified address. Returns the number of bytes 00224 * sent from the buffer. 00225 * 00226 * This call is non-blocking. If sendto would block, 00227 * NSAPI_ERROR_WOULD_BLOCK is returned immediately. 00228 * 00229 * @param handle Socket handle 00230 * @param address The SocketAddress of the remote host 00231 * @param data Buffer of data to send to the host 00232 * @param size Size of the buffer in bytes 00233 * @return Number of sent bytes on success, negative error 00234 * code on failure 00235 */ 00236 virtual nsapi_size_or_error_t socket_sendto(nsapi_socket_t handle, const SocketAddress &address, 00237 const void *data, nsapi_size_t size) = 0; 00238 00239 /** Receive a packet over a UDP socket 00240 * 00241 * Receives data and stores the source address in address if address 00242 * is not NULL. Returns the number of bytes received into the buffer. 00243 * 00244 * This call is non-blocking. If recvfrom would block, 00245 * NSAPI_ERROR_WOULD_BLOCK is returned immediately. 00246 * 00247 * @param handle Socket handle 00248 * @param address Destination for the source address or NULL 00249 * @param data Destination buffer for data received from the host 00250 * @param size Size of the buffer in bytes 00251 * @return Number of received bytes on success, negative error 00252 * code on failure 00253 */ 00254 virtual nsapi_size_or_error_t socket_recvfrom(nsapi_socket_t handle, SocketAddress *address, 00255 void *buffer, nsapi_size_t size) = 0; 00256 00257 /** Register a callback on state change of the socket 00258 * 00259 * The specified callback will be called on state changes such as when 00260 * the socket can recv/send/accept successfully and on when an error 00261 * occurs. The callback may also be called spuriously without reason. 00262 * 00263 * The callback may be called in an interrupt context and should not 00264 * perform expensive operations such as recv/send calls. 00265 * 00266 * @param handle Socket handle 00267 * @param callback Function to call on state change 00268 * @param data Argument to pass to callback 00269 */ 00270 virtual void socket_attach(nsapi_socket_t handle, void (*callback)(void *), void *data) = 0; 00271 00272 /* Set stack-specific socket options 00273 * 00274 * The setsockopt allow an application to pass stack-specific hints 00275 * to the underlying stack. For unsupported options, 00276 * NSAPI_ERROR_UNSUPPORTED is returned and the socket is unmodified. 00277 * 00278 * @param handle Socket handle 00279 * @param level Stack-specific protocol level 00280 * @param optname Stack-specific option identifier 00281 * @param optval Option value 00282 * @param optlen Length of the option value 00283 * @return 0 on success, negative error code on failure 00284 */ 00285 virtual nsapi_error_t setsockopt(nsapi_socket_t handle, int level, 00286 int optname, const void *optval, unsigned optlen); 00287 00288 /* Get stack-specific socket options 00289 * 00290 * The getstackopt allow an application to retrieve stack-specific hints 00291 * from the underlying stack. For unsupported options, 00292 * NSAPI_ERROR_UNSUPPORTED is returned and optval is unmodified. 00293 * 00294 * @param handle Socket handle 00295 * @param level Stack-specific protocol level 00296 * @param optname Stack-specific option identifier 00297 * @param optval Destination for option value 00298 * @param optlen Length of the option value 00299 * @return 0 on success, negative error code on failure 00300 */ 00301 virtual nsapi_error_t getsockopt(nsapi_socket_t handle, int level, 00302 int optname, void *optval, unsigned *optlen); 00303 }; 00304 00305 00306 /** Convert a raw nsapi_stack_t object into a C++ NetworkStack object 00307 * 00308 * @param stack Reference to an object that can be converted to a stack 00309 * - A raw nsapi_stack_t object 00310 * - A reference to a network stack 00311 * - A reference to a network interface 00312 * @return Reference to the underlying network stack 00313 */ 00314 NetworkStack *nsapi_create_stack(nsapi_stack_t *stack); 00315 NetworkStack *nsapi_create_stack(NetworkStack *stack); 00316 00317 template <typename IF> 00318 NetworkStack *nsapi_create_stack(IF *iface) 00319 { 00320 return nsapi_create_stack(static_cast<NetworkInterface *>(iface)->get_stack()); 00321 } 00322 00323 00324 #endif 00325 00326 /** @}*/
Generated on Tue Jul 12 2022 11:02:48 by
