Marco Zecchini
/
Example_RTOS
Rtos API example
mbed-os/features/netsocket/NetworkStack.h@0:9fca2b23d0ba, 2019-02-23 (annotated)
- Committer:
- marcozecchini
- Date:
- Sat Feb 23 12:13:36 2019 +0000
- Revision:
- 0:9fca2b23d0ba
final commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
marcozecchini | 0:9fca2b23d0ba | 1 | |
marcozecchini | 0:9fca2b23d0ba | 2 | /* NetworkStack |
marcozecchini | 0:9fca2b23d0ba | 3 | * Copyright (c) 2015 ARM Limited |
marcozecchini | 0:9fca2b23d0ba | 4 | * |
marcozecchini | 0:9fca2b23d0ba | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
marcozecchini | 0:9fca2b23d0ba | 6 | * you may not use this file except in compliance with the License. |
marcozecchini | 0:9fca2b23d0ba | 7 | * You may obtain a copy of the License at |
marcozecchini | 0:9fca2b23d0ba | 8 | * |
marcozecchini | 0:9fca2b23d0ba | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
marcozecchini | 0:9fca2b23d0ba | 10 | * |
marcozecchini | 0:9fca2b23d0ba | 11 | * Unless required by applicable law or agreed to in writing, software |
marcozecchini | 0:9fca2b23d0ba | 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
marcozecchini | 0:9fca2b23d0ba | 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
marcozecchini | 0:9fca2b23d0ba | 14 | * See the License for the specific language governing permissions and |
marcozecchini | 0:9fca2b23d0ba | 15 | * limitations under the License. |
marcozecchini | 0:9fca2b23d0ba | 16 | */ |
marcozecchini | 0:9fca2b23d0ba | 17 | |
marcozecchini | 0:9fca2b23d0ba | 18 | #ifndef NETWORK_STACK_H |
marcozecchini | 0:9fca2b23d0ba | 19 | #define NETWORK_STACK_H |
marcozecchini | 0:9fca2b23d0ba | 20 | |
marcozecchini | 0:9fca2b23d0ba | 21 | #include "nsapi_types.h" |
marcozecchini | 0:9fca2b23d0ba | 22 | #include "netsocket/SocketAddress.h" |
marcozecchini | 0:9fca2b23d0ba | 23 | #include "netsocket/NetworkInterface.h" |
marcozecchini | 0:9fca2b23d0ba | 24 | |
marcozecchini | 0:9fca2b23d0ba | 25 | |
marcozecchini | 0:9fca2b23d0ba | 26 | /** NetworkStack class |
marcozecchini | 0:9fca2b23d0ba | 27 | * |
marcozecchini | 0:9fca2b23d0ba | 28 | * Common interface that is shared between hardware that |
marcozecchini | 0:9fca2b23d0ba | 29 | * can connect to a network over IP. By implementing the |
marcozecchini | 0:9fca2b23d0ba | 30 | * NetworkStack, a network stack can be used as a target |
marcozecchini | 0:9fca2b23d0ba | 31 | * for instantiating network sockets. |
marcozecchini | 0:9fca2b23d0ba | 32 | * @addtogroup netsocket |
marcozecchini | 0:9fca2b23d0ba | 33 | */ |
marcozecchini | 0:9fca2b23d0ba | 34 | class NetworkStack |
marcozecchini | 0:9fca2b23d0ba | 35 | { |
marcozecchini | 0:9fca2b23d0ba | 36 | public: |
marcozecchini | 0:9fca2b23d0ba | 37 | virtual ~NetworkStack() {}; |
marcozecchini | 0:9fca2b23d0ba | 38 | |
marcozecchini | 0:9fca2b23d0ba | 39 | /** Get the local IP address |
marcozecchini | 0:9fca2b23d0ba | 40 | * |
marcozecchini | 0:9fca2b23d0ba | 41 | * @return Null-terminated representation of the local IP address |
marcozecchini | 0:9fca2b23d0ba | 42 | * or null if not yet connected |
marcozecchini | 0:9fca2b23d0ba | 43 | */ |
marcozecchini | 0:9fca2b23d0ba | 44 | virtual const char *get_ip_address() = 0; |
marcozecchini | 0:9fca2b23d0ba | 45 | |
marcozecchini | 0:9fca2b23d0ba | 46 | /** Translates a hostname to an IP address with specific version |
marcozecchini | 0:9fca2b23d0ba | 47 | * |
marcozecchini | 0:9fca2b23d0ba | 48 | * The hostname may be either a domain name or an IP address. If the |
marcozecchini | 0:9fca2b23d0ba | 49 | * hostname is an IP address, no network transactions will be performed. |
marcozecchini | 0:9fca2b23d0ba | 50 | * |
marcozecchini | 0:9fca2b23d0ba | 51 | * If no stack-specific DNS resolution is provided, the hostname |
marcozecchini | 0:9fca2b23d0ba | 52 | * will be resolve using a UDP socket on the stack. |
marcozecchini | 0:9fca2b23d0ba | 53 | * |
marcozecchini | 0:9fca2b23d0ba | 54 | * @param host Hostname to resolve |
marcozecchini | 0:9fca2b23d0ba | 55 | * @param address Destination for the host SocketAddress |
marcozecchini | 0:9fca2b23d0ba | 56 | * @param version IP version of address to resolve, NSAPI_UNSPEC indicates |
marcozecchini | 0:9fca2b23d0ba | 57 | * version is chosen by the stack (defaults to NSAPI_UNSPEC) |
marcozecchini | 0:9fca2b23d0ba | 58 | * @return 0 on success, negative error code on failure |
marcozecchini | 0:9fca2b23d0ba | 59 | */ |
marcozecchini | 0:9fca2b23d0ba | 60 | virtual nsapi_error_t gethostbyname(const char *host, |
marcozecchini | 0:9fca2b23d0ba | 61 | SocketAddress *address, nsapi_version_t version = NSAPI_UNSPEC); |
marcozecchini | 0:9fca2b23d0ba | 62 | |
marcozecchini | 0:9fca2b23d0ba | 63 | /** Add a domain name server to list of servers to query |
marcozecchini | 0:9fca2b23d0ba | 64 | * |
marcozecchini | 0:9fca2b23d0ba | 65 | * @param address Destination for the host address |
marcozecchini | 0:9fca2b23d0ba | 66 | * @return 0 on success, negative error code on failure |
marcozecchini | 0:9fca2b23d0ba | 67 | */ |
marcozecchini | 0:9fca2b23d0ba | 68 | virtual nsapi_error_t add_dns_server(const SocketAddress &address); |
marcozecchini | 0:9fca2b23d0ba | 69 | |
marcozecchini | 0:9fca2b23d0ba | 70 | /* Set stack options |
marcozecchini | 0:9fca2b23d0ba | 71 | * |
marcozecchini | 0:9fca2b23d0ba | 72 | * setstackopt allows an application to pass stack-specific options |
marcozecchini | 0:9fca2b23d0ba | 73 | * to the underlying stack using stack-specific level and option names, |
marcozecchini | 0:9fca2b23d0ba | 74 | * or to request generic options using levels from nsapi_stack_level_t. |
marcozecchini | 0:9fca2b23d0ba | 75 | * |
marcozecchini | 0:9fca2b23d0ba | 76 | * For unsupported options, NSAPI_ERROR_UNSUPPORTED is returned |
marcozecchini | 0:9fca2b23d0ba | 77 | * and the stack is unmodified. |
marcozecchini | 0:9fca2b23d0ba | 78 | * |
marcozecchini | 0:9fca2b23d0ba | 79 | * @param level Stack-specific protocol level or nsapi_stack_level_t |
marcozecchini | 0:9fca2b23d0ba | 80 | * @param optname Level-specific option name |
marcozecchini | 0:9fca2b23d0ba | 81 | * @param optval Option value |
marcozecchini | 0:9fca2b23d0ba | 82 | * @param optlen Length of the option value |
marcozecchini | 0:9fca2b23d0ba | 83 | * @return 0 on success, negative error code on failure |
marcozecchini | 0:9fca2b23d0ba | 84 | */ |
marcozecchini | 0:9fca2b23d0ba | 85 | virtual nsapi_error_t setstackopt(int level, int optname, const void *optval, unsigned optlen); |
marcozecchini | 0:9fca2b23d0ba | 86 | |
marcozecchini | 0:9fca2b23d0ba | 87 | /* Get stack options |
marcozecchini | 0:9fca2b23d0ba | 88 | * |
marcozecchini | 0:9fca2b23d0ba | 89 | * getstackopt allows an application to retrieve stack-specific options |
marcozecchini | 0:9fca2b23d0ba | 90 | * to the underlying stack using stack-specific level and option names, |
marcozecchini | 0:9fca2b23d0ba | 91 | * or to request generic options using levels from nsapi_stack_level_t. |
marcozecchini | 0:9fca2b23d0ba | 92 | * |
marcozecchini | 0:9fca2b23d0ba | 93 | * @param level Stack-specific protocol level or nsapi_stack_level_t |
marcozecchini | 0:9fca2b23d0ba | 94 | * @param optname Level-specific option name |
marcozecchini | 0:9fca2b23d0ba | 95 | * @param optval Destination for option value |
marcozecchini | 0:9fca2b23d0ba | 96 | * @param optlen Length of the option value |
marcozecchini | 0:9fca2b23d0ba | 97 | * @return 0 on success, negative error code on failure |
marcozecchini | 0:9fca2b23d0ba | 98 | */ |
marcozecchini | 0:9fca2b23d0ba | 99 | virtual nsapi_error_t getstackopt(int level, int optname, void *optval, unsigned *optlen); |
marcozecchini | 0:9fca2b23d0ba | 100 | |
marcozecchini | 0:9fca2b23d0ba | 101 | protected: |
marcozecchini | 0:9fca2b23d0ba | 102 | friend class Socket; |
marcozecchini | 0:9fca2b23d0ba | 103 | friend class UDPSocket; |
marcozecchini | 0:9fca2b23d0ba | 104 | friend class TCPSocket; |
marcozecchini | 0:9fca2b23d0ba | 105 | friend class TCPServer; |
marcozecchini | 0:9fca2b23d0ba | 106 | |
marcozecchini | 0:9fca2b23d0ba | 107 | /** Opens a socket |
marcozecchini | 0:9fca2b23d0ba | 108 | * |
marcozecchini | 0:9fca2b23d0ba | 109 | * Creates a network socket and stores it in the specified handle. |
marcozecchini | 0:9fca2b23d0ba | 110 | * The handle must be passed to following calls on the socket. |
marcozecchini | 0:9fca2b23d0ba | 111 | * |
marcozecchini | 0:9fca2b23d0ba | 112 | * A stack may have a finite number of sockets, in this case |
marcozecchini | 0:9fca2b23d0ba | 113 | * NSAPI_ERROR_NO_SOCKET is returned if no socket is available. |
marcozecchini | 0:9fca2b23d0ba | 114 | * |
marcozecchini | 0:9fca2b23d0ba | 115 | * @param handle Destination for the handle to a newly created socket |
marcozecchini | 0:9fca2b23d0ba | 116 | * @param proto Protocol of socket to open, NSAPI_TCP or NSAPI_UDP |
marcozecchini | 0:9fca2b23d0ba | 117 | * @return 0 on success, negative error code on failure |
marcozecchini | 0:9fca2b23d0ba | 118 | */ |
marcozecchini | 0:9fca2b23d0ba | 119 | virtual nsapi_error_t socket_open(nsapi_socket_t *handle, nsapi_protocol_t proto) = 0; |
marcozecchini | 0:9fca2b23d0ba | 120 | |
marcozecchini | 0:9fca2b23d0ba | 121 | /** Close the socket |
marcozecchini | 0:9fca2b23d0ba | 122 | * |
marcozecchini | 0:9fca2b23d0ba | 123 | * Closes any open connection and deallocates any memory associated |
marcozecchini | 0:9fca2b23d0ba | 124 | * with the socket. |
marcozecchini | 0:9fca2b23d0ba | 125 | * |
marcozecchini | 0:9fca2b23d0ba | 126 | * @param handle Socket handle |
marcozecchini | 0:9fca2b23d0ba | 127 | * @return 0 on success, negative error code on failure |
marcozecchini | 0:9fca2b23d0ba | 128 | */ |
marcozecchini | 0:9fca2b23d0ba | 129 | virtual nsapi_error_t socket_close(nsapi_socket_t handle) = 0; |
marcozecchini | 0:9fca2b23d0ba | 130 | |
marcozecchini | 0:9fca2b23d0ba | 131 | /** Bind a specific address to a socket |
marcozecchini | 0:9fca2b23d0ba | 132 | * |
marcozecchini | 0:9fca2b23d0ba | 133 | * Binding a socket specifies the address and port on which to recieve |
marcozecchini | 0:9fca2b23d0ba | 134 | * data. If the IP address is zeroed, only the port is bound. |
marcozecchini | 0:9fca2b23d0ba | 135 | * |
marcozecchini | 0:9fca2b23d0ba | 136 | * @param handle Socket handle |
marcozecchini | 0:9fca2b23d0ba | 137 | * @param address Local address to bind |
marcozecchini | 0:9fca2b23d0ba | 138 | * @return 0 on success, negative error code on failure. |
marcozecchini | 0:9fca2b23d0ba | 139 | */ |
marcozecchini | 0:9fca2b23d0ba | 140 | virtual nsapi_error_t socket_bind(nsapi_socket_t handle, const SocketAddress &address) = 0; |
marcozecchini | 0:9fca2b23d0ba | 141 | |
marcozecchini | 0:9fca2b23d0ba | 142 | /** Listen for connections on a TCP socket |
marcozecchini | 0:9fca2b23d0ba | 143 | * |
marcozecchini | 0:9fca2b23d0ba | 144 | * Marks the socket as a passive socket that can be used to accept |
marcozecchini | 0:9fca2b23d0ba | 145 | * incoming connections. |
marcozecchini | 0:9fca2b23d0ba | 146 | * |
marcozecchini | 0:9fca2b23d0ba | 147 | * @param handle Socket handle |
marcozecchini | 0:9fca2b23d0ba | 148 | * @param backlog Number of pending connections that can be queued |
marcozecchini | 0:9fca2b23d0ba | 149 | * simultaneously |
marcozecchini | 0:9fca2b23d0ba | 150 | * @return 0 on success, negative error code on failure |
marcozecchini | 0:9fca2b23d0ba | 151 | */ |
marcozecchini | 0:9fca2b23d0ba | 152 | virtual nsapi_error_t socket_listen(nsapi_socket_t handle, int backlog) = 0; |
marcozecchini | 0:9fca2b23d0ba | 153 | |
marcozecchini | 0:9fca2b23d0ba | 154 | /** Connects TCP socket to a remote host |
marcozecchini | 0:9fca2b23d0ba | 155 | * |
marcozecchini | 0:9fca2b23d0ba | 156 | * Initiates a connection to a remote server specified by the |
marcozecchini | 0:9fca2b23d0ba | 157 | * indicated address. |
marcozecchini | 0:9fca2b23d0ba | 158 | * |
marcozecchini | 0:9fca2b23d0ba | 159 | * @param handle Socket handle |
marcozecchini | 0:9fca2b23d0ba | 160 | * @param address The SocketAddress of the remote host |
marcozecchini | 0:9fca2b23d0ba | 161 | * @return 0 on success, negative error code on failure |
marcozecchini | 0:9fca2b23d0ba | 162 | */ |
marcozecchini | 0:9fca2b23d0ba | 163 | virtual nsapi_error_t socket_connect(nsapi_socket_t handle, const SocketAddress &address) = 0; |
marcozecchini | 0:9fca2b23d0ba | 164 | |
marcozecchini | 0:9fca2b23d0ba | 165 | /** Accepts a connection on a TCP socket |
marcozecchini | 0:9fca2b23d0ba | 166 | * |
marcozecchini | 0:9fca2b23d0ba | 167 | * The server socket must be bound and set to listen for connections. |
marcozecchini | 0:9fca2b23d0ba | 168 | * On a new connection, creates a network socket and stores it in the |
marcozecchini | 0:9fca2b23d0ba | 169 | * specified handle. The handle must be passed to following calls on |
marcozecchini | 0:9fca2b23d0ba | 170 | * the socket. |
marcozecchini | 0:9fca2b23d0ba | 171 | * |
marcozecchini | 0:9fca2b23d0ba | 172 | * A stack may have a finite number of sockets, in this case |
marcozecchini | 0:9fca2b23d0ba | 173 | * NSAPI_ERROR_NO_SOCKET is returned if no socket is available. |
marcozecchini | 0:9fca2b23d0ba | 174 | * |
marcozecchini | 0:9fca2b23d0ba | 175 | * This call is non-blocking. If accept would block, |
marcozecchini | 0:9fca2b23d0ba | 176 | * NSAPI_ERROR_WOULD_BLOCK is returned immediately. |
marcozecchini | 0:9fca2b23d0ba | 177 | * |
marcozecchini | 0:9fca2b23d0ba | 178 | * @param server Socket handle to server to accept from |
marcozecchini | 0:9fca2b23d0ba | 179 | * @param handle Destination for a handle to the newly created socket |
marcozecchini | 0:9fca2b23d0ba | 180 | * @param address Destination for the remote address or NULL |
marcozecchini | 0:9fca2b23d0ba | 181 | * @return 0 on success, negative error code on failure |
marcozecchini | 0:9fca2b23d0ba | 182 | */ |
marcozecchini | 0:9fca2b23d0ba | 183 | virtual nsapi_error_t socket_accept(nsapi_socket_t server, |
marcozecchini | 0:9fca2b23d0ba | 184 | nsapi_socket_t *handle, SocketAddress *address=0) = 0; |
marcozecchini | 0:9fca2b23d0ba | 185 | |
marcozecchini | 0:9fca2b23d0ba | 186 | /** Send data over a TCP socket |
marcozecchini | 0:9fca2b23d0ba | 187 | * |
marcozecchini | 0:9fca2b23d0ba | 188 | * The socket must be connected to a remote host. Returns the number of |
marcozecchini | 0:9fca2b23d0ba | 189 | * bytes sent from the buffer. |
marcozecchini | 0:9fca2b23d0ba | 190 | * |
marcozecchini | 0:9fca2b23d0ba | 191 | * This call is non-blocking. If send would block, |
marcozecchini | 0:9fca2b23d0ba | 192 | * NSAPI_ERROR_WOULD_BLOCK is returned immediately. |
marcozecchini | 0:9fca2b23d0ba | 193 | * |
marcozecchini | 0:9fca2b23d0ba | 194 | * @param handle Socket handle |
marcozecchini | 0:9fca2b23d0ba | 195 | * @param data Buffer of data to send to the host |
marcozecchini | 0:9fca2b23d0ba | 196 | * @param size Size of the buffer in bytes |
marcozecchini | 0:9fca2b23d0ba | 197 | * @return Number of sent bytes on success, negative error |
marcozecchini | 0:9fca2b23d0ba | 198 | * code on failure |
marcozecchini | 0:9fca2b23d0ba | 199 | */ |
marcozecchini | 0:9fca2b23d0ba | 200 | virtual nsapi_size_or_error_t socket_send(nsapi_socket_t handle, |
marcozecchini | 0:9fca2b23d0ba | 201 | const void *data, nsapi_size_t size) = 0; |
marcozecchini | 0:9fca2b23d0ba | 202 | |
marcozecchini | 0:9fca2b23d0ba | 203 | /** Receive data over a TCP socket |
marcozecchini | 0:9fca2b23d0ba | 204 | * |
marcozecchini | 0:9fca2b23d0ba | 205 | * The socket must be connected to a remote host. Returns the number of |
marcozecchini | 0:9fca2b23d0ba | 206 | * bytes received into the buffer. |
marcozecchini | 0:9fca2b23d0ba | 207 | * |
marcozecchini | 0:9fca2b23d0ba | 208 | * This call is non-blocking. If recv would block, |
marcozecchini | 0:9fca2b23d0ba | 209 | * NSAPI_ERROR_WOULD_BLOCK is returned immediately. |
marcozecchini | 0:9fca2b23d0ba | 210 | * |
marcozecchini | 0:9fca2b23d0ba | 211 | * @param handle Socket handle |
marcozecchini | 0:9fca2b23d0ba | 212 | * @param data Destination buffer for data received from the host |
marcozecchini | 0:9fca2b23d0ba | 213 | * @param size Size of the buffer in bytes |
marcozecchini | 0:9fca2b23d0ba | 214 | * @return Number of received bytes on success, negative error |
marcozecchini | 0:9fca2b23d0ba | 215 | * code on failure |
marcozecchini | 0:9fca2b23d0ba | 216 | */ |
marcozecchini | 0:9fca2b23d0ba | 217 | virtual nsapi_size_or_error_t socket_recv(nsapi_socket_t handle, |
marcozecchini | 0:9fca2b23d0ba | 218 | void *data, nsapi_size_t size) = 0; |
marcozecchini | 0:9fca2b23d0ba | 219 | |
marcozecchini | 0:9fca2b23d0ba | 220 | /** Send a packet over a UDP socket |
marcozecchini | 0:9fca2b23d0ba | 221 | * |
marcozecchini | 0:9fca2b23d0ba | 222 | * Sends data to the specified address. Returns the number of bytes |
marcozecchini | 0:9fca2b23d0ba | 223 | * sent from the buffer. |
marcozecchini | 0:9fca2b23d0ba | 224 | * |
marcozecchini | 0:9fca2b23d0ba | 225 | * This call is non-blocking. If sendto would block, |
marcozecchini | 0:9fca2b23d0ba | 226 | * NSAPI_ERROR_WOULD_BLOCK is returned immediately. |
marcozecchini | 0:9fca2b23d0ba | 227 | * |
marcozecchini | 0:9fca2b23d0ba | 228 | * @param handle Socket handle |
marcozecchini | 0:9fca2b23d0ba | 229 | * @param address The SocketAddress of the remote host |
marcozecchini | 0:9fca2b23d0ba | 230 | * @param data Buffer of data to send to the host |
marcozecchini | 0:9fca2b23d0ba | 231 | * @param size Size of the buffer in bytes |
marcozecchini | 0:9fca2b23d0ba | 232 | * @return Number of sent bytes on success, negative error |
marcozecchini | 0:9fca2b23d0ba | 233 | * code on failure |
marcozecchini | 0:9fca2b23d0ba | 234 | */ |
marcozecchini | 0:9fca2b23d0ba | 235 | virtual nsapi_size_or_error_t socket_sendto(nsapi_socket_t handle, const SocketAddress &address, |
marcozecchini | 0:9fca2b23d0ba | 236 | const void *data, nsapi_size_t size) = 0; |
marcozecchini | 0:9fca2b23d0ba | 237 | |
marcozecchini | 0:9fca2b23d0ba | 238 | /** Receive a packet over a UDP socket |
marcozecchini | 0:9fca2b23d0ba | 239 | * |
marcozecchini | 0:9fca2b23d0ba | 240 | * Receives data and stores the source address in address if address |
marcozecchini | 0:9fca2b23d0ba | 241 | * is not NULL. Returns the number of bytes received into the buffer. |
marcozecchini | 0:9fca2b23d0ba | 242 | * |
marcozecchini | 0:9fca2b23d0ba | 243 | * This call is non-blocking. If recvfrom would block, |
marcozecchini | 0:9fca2b23d0ba | 244 | * NSAPI_ERROR_WOULD_BLOCK is returned immediately. |
marcozecchini | 0:9fca2b23d0ba | 245 | * |
marcozecchini | 0:9fca2b23d0ba | 246 | * @param handle Socket handle |
marcozecchini | 0:9fca2b23d0ba | 247 | * @param address Destination for the source address or NULL |
marcozecchini | 0:9fca2b23d0ba | 248 | * @param buffer Destination buffer for data received from the host |
marcozecchini | 0:9fca2b23d0ba | 249 | * @param size Size of the buffer in bytes |
marcozecchini | 0:9fca2b23d0ba | 250 | * @return Number of received bytes on success, negative error |
marcozecchini | 0:9fca2b23d0ba | 251 | * code on failure |
marcozecchini | 0:9fca2b23d0ba | 252 | */ |
marcozecchini | 0:9fca2b23d0ba | 253 | virtual nsapi_size_or_error_t socket_recvfrom(nsapi_socket_t handle, SocketAddress *address, |
marcozecchini | 0:9fca2b23d0ba | 254 | void *buffer, nsapi_size_t size) = 0; |
marcozecchini | 0:9fca2b23d0ba | 255 | |
marcozecchini | 0:9fca2b23d0ba | 256 | /** Register a callback on state change of the socket |
marcozecchini | 0:9fca2b23d0ba | 257 | * |
marcozecchini | 0:9fca2b23d0ba | 258 | * The specified callback will be called on state changes such as when |
marcozecchini | 0:9fca2b23d0ba | 259 | * the socket can recv/send/accept successfully and on when an error |
marcozecchini | 0:9fca2b23d0ba | 260 | * occurs. The callback may also be called spuriously without reason. |
marcozecchini | 0:9fca2b23d0ba | 261 | * |
marcozecchini | 0:9fca2b23d0ba | 262 | * The callback may be called in an interrupt context and should not |
marcozecchini | 0:9fca2b23d0ba | 263 | * perform expensive operations such as recv/send calls. |
marcozecchini | 0:9fca2b23d0ba | 264 | * |
marcozecchini | 0:9fca2b23d0ba | 265 | * @param handle Socket handle |
marcozecchini | 0:9fca2b23d0ba | 266 | * @param callback Function to call on state change |
marcozecchini | 0:9fca2b23d0ba | 267 | * @param data Argument to pass to callback |
marcozecchini | 0:9fca2b23d0ba | 268 | */ |
marcozecchini | 0:9fca2b23d0ba | 269 | virtual void socket_attach(nsapi_socket_t handle, void (*callback)(void *), void *data) = 0; |
marcozecchini | 0:9fca2b23d0ba | 270 | |
marcozecchini | 0:9fca2b23d0ba | 271 | /* Set stack-specific socket options |
marcozecchini | 0:9fca2b23d0ba | 272 | * |
marcozecchini | 0:9fca2b23d0ba | 273 | * The setsockopt allow an application to pass stack-specific hints |
marcozecchini | 0:9fca2b23d0ba | 274 | * to the underlying stack. For unsupported options, |
marcozecchini | 0:9fca2b23d0ba | 275 | * NSAPI_ERROR_UNSUPPORTED is returned and the socket is unmodified. |
marcozecchini | 0:9fca2b23d0ba | 276 | * |
marcozecchini | 0:9fca2b23d0ba | 277 | * @param handle Socket handle |
marcozecchini | 0:9fca2b23d0ba | 278 | * @param level Stack-specific protocol level |
marcozecchini | 0:9fca2b23d0ba | 279 | * @param optname Stack-specific option identifier |
marcozecchini | 0:9fca2b23d0ba | 280 | * @param optval Option value |
marcozecchini | 0:9fca2b23d0ba | 281 | * @param optlen Length of the option value |
marcozecchini | 0:9fca2b23d0ba | 282 | * @return 0 on success, negative error code on failure |
marcozecchini | 0:9fca2b23d0ba | 283 | */ |
marcozecchini | 0:9fca2b23d0ba | 284 | virtual nsapi_error_t setsockopt(nsapi_socket_t handle, int level, |
marcozecchini | 0:9fca2b23d0ba | 285 | int optname, const void *optval, unsigned optlen); |
marcozecchini | 0:9fca2b23d0ba | 286 | |
marcozecchini | 0:9fca2b23d0ba | 287 | /* Get stack-specific socket options |
marcozecchini | 0:9fca2b23d0ba | 288 | * |
marcozecchini | 0:9fca2b23d0ba | 289 | * The getstackopt allow an application to retrieve stack-specific hints |
marcozecchini | 0:9fca2b23d0ba | 290 | * from the underlying stack. For unsupported options, |
marcozecchini | 0:9fca2b23d0ba | 291 | * NSAPI_ERROR_UNSUPPORTED is returned and optval is unmodified. |
marcozecchini | 0:9fca2b23d0ba | 292 | * |
marcozecchini | 0:9fca2b23d0ba | 293 | * @param handle Socket handle |
marcozecchini | 0:9fca2b23d0ba | 294 | * @param level Stack-specific protocol level |
marcozecchini | 0:9fca2b23d0ba | 295 | * @param optname Stack-specific option identifier |
marcozecchini | 0:9fca2b23d0ba | 296 | * @param optval Destination for option value |
marcozecchini | 0:9fca2b23d0ba | 297 | * @param optlen Length of the option value |
marcozecchini | 0:9fca2b23d0ba | 298 | * @return 0 on success, negative error code on failure |
marcozecchini | 0:9fca2b23d0ba | 299 | */ |
marcozecchini | 0:9fca2b23d0ba | 300 | virtual nsapi_error_t getsockopt(nsapi_socket_t handle, int level, |
marcozecchini | 0:9fca2b23d0ba | 301 | int optname, void *optval, unsigned *optlen); |
marcozecchini | 0:9fca2b23d0ba | 302 | }; |
marcozecchini | 0:9fca2b23d0ba | 303 | |
marcozecchini | 0:9fca2b23d0ba | 304 | |
marcozecchini | 0:9fca2b23d0ba | 305 | /** Convert a raw nsapi_stack_t object into a C++ NetworkStack object |
marcozecchini | 0:9fca2b23d0ba | 306 | * |
marcozecchini | 0:9fca2b23d0ba | 307 | * @param stack Reference to an object that can be converted to a stack |
marcozecchini | 0:9fca2b23d0ba | 308 | * - A raw nsapi_stack_t object |
marcozecchini | 0:9fca2b23d0ba | 309 | * - A reference to a network stack |
marcozecchini | 0:9fca2b23d0ba | 310 | * - A reference to a network interface |
marcozecchini | 0:9fca2b23d0ba | 311 | * @return Reference to the underlying network stack |
marcozecchini | 0:9fca2b23d0ba | 312 | */ |
marcozecchini | 0:9fca2b23d0ba | 313 | NetworkStack *nsapi_create_stack(nsapi_stack_t *stack); |
marcozecchini | 0:9fca2b23d0ba | 314 | NetworkStack *nsapi_create_stack(NetworkStack *stack); |
marcozecchini | 0:9fca2b23d0ba | 315 | |
marcozecchini | 0:9fca2b23d0ba | 316 | template <typename IF> |
marcozecchini | 0:9fca2b23d0ba | 317 | NetworkStack *nsapi_create_stack(IF *iface) |
marcozecchini | 0:9fca2b23d0ba | 318 | { |
marcozecchini | 0:9fca2b23d0ba | 319 | return nsapi_create_stack(static_cast<NetworkInterface *>(iface)->get_stack()); |
marcozecchini | 0:9fca2b23d0ba | 320 | } |
marcozecchini | 0:9fca2b23d0ba | 321 | |
marcozecchini | 0:9fca2b23d0ba | 322 | |
marcozecchini | 0:9fca2b23d0ba | 323 | #endif |