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