Simon Cooksey / mbed-os
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers NetworkStack.h Source File

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