RTC auf true
features/netsocket/NetworkStack.h@0:38ceb79fef03, 2018-11-28 (annotated)
- Committer:
- kevman
- Date:
- Wed Nov 28 15:10:15 2018 +0000
- Revision:
- 0:38ceb79fef03
RTC modified
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kevman | 0:38ceb79fef03 | 1 | |
kevman | 0:38ceb79fef03 | 2 | /* NetworkStack |
kevman | 0:38ceb79fef03 | 3 | * Copyright (c) 2015 ARM Limited |
kevman | 0:38ceb79fef03 | 4 | * |
kevman | 0:38ceb79fef03 | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
kevman | 0:38ceb79fef03 | 6 | * you may not use this file except in compliance with the License. |
kevman | 0:38ceb79fef03 | 7 | * You may obtain a copy of the License at |
kevman | 0:38ceb79fef03 | 8 | * |
kevman | 0:38ceb79fef03 | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
kevman | 0:38ceb79fef03 | 10 | * |
kevman | 0:38ceb79fef03 | 11 | * Unless required by applicable law or agreed to in writing, software |
kevman | 0:38ceb79fef03 | 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
kevman | 0:38ceb79fef03 | 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
kevman | 0:38ceb79fef03 | 14 | * See the License for the specific language governing permissions and |
kevman | 0:38ceb79fef03 | 15 | * limitations under the License. |
kevman | 0:38ceb79fef03 | 16 | */ |
kevman | 0:38ceb79fef03 | 17 | |
kevman | 0:38ceb79fef03 | 18 | #ifndef NETWORK_STACK_H |
kevman | 0:38ceb79fef03 | 19 | #define NETWORK_STACK_H |
kevman | 0:38ceb79fef03 | 20 | |
kevman | 0:38ceb79fef03 | 21 | #include "nsapi_types.h" |
kevman | 0:38ceb79fef03 | 22 | #include "netsocket/SocketAddress.h" |
kevman | 0:38ceb79fef03 | 23 | #include "netsocket/NetworkInterface.h" |
kevman | 0:38ceb79fef03 | 24 | #include "DNS.h" |
kevman | 0:38ceb79fef03 | 25 | |
kevman | 0:38ceb79fef03 | 26 | // Predeclared classes |
kevman | 0:38ceb79fef03 | 27 | class OnboardNetworkStack; |
kevman | 0:38ceb79fef03 | 28 | |
kevman | 0:38ceb79fef03 | 29 | /** NetworkStack class |
kevman | 0:38ceb79fef03 | 30 | * |
kevman | 0:38ceb79fef03 | 31 | * Common interface that is shared between hardware that |
kevman | 0:38ceb79fef03 | 32 | * can connect to a network over IP. By implementing the |
kevman | 0:38ceb79fef03 | 33 | * NetworkStack, a network stack can be used as a target |
kevman | 0:38ceb79fef03 | 34 | * for instantiating network sockets. |
kevman | 0:38ceb79fef03 | 35 | * @addtogroup netsocket |
kevman | 0:38ceb79fef03 | 36 | */ |
kevman | 0:38ceb79fef03 | 37 | class NetworkStack: public DNS { |
kevman | 0:38ceb79fef03 | 38 | public: |
kevman | 0:38ceb79fef03 | 39 | virtual ~NetworkStack() {}; |
kevman | 0:38ceb79fef03 | 40 | |
kevman | 0:38ceb79fef03 | 41 | /** Get the local IP address |
kevman | 0:38ceb79fef03 | 42 | * |
kevman | 0:38ceb79fef03 | 43 | * @return Null-terminated representation of the local IP address |
kevman | 0:38ceb79fef03 | 44 | * or null if not yet connected |
kevman | 0:38ceb79fef03 | 45 | */ |
kevman | 0:38ceb79fef03 | 46 | virtual const char *get_ip_address(); |
kevman | 0:38ceb79fef03 | 47 | |
kevman | 0:38ceb79fef03 | 48 | /** Translates a hostname to an IP address with specific version |
kevman | 0:38ceb79fef03 | 49 | * |
kevman | 0:38ceb79fef03 | 50 | * The hostname may be either a domain name or an IP address. If the |
kevman | 0:38ceb79fef03 | 51 | * hostname is an IP address, no network transactions will be performed. |
kevman | 0:38ceb79fef03 | 52 | * |
kevman | 0:38ceb79fef03 | 53 | * If no stack-specific DNS resolution is provided, the hostname |
kevman | 0:38ceb79fef03 | 54 | * will be resolve using a UDP socket on the stack. |
kevman | 0:38ceb79fef03 | 55 | * |
kevman | 0:38ceb79fef03 | 56 | * @param host Hostname to resolve |
kevman | 0:38ceb79fef03 | 57 | * @param address Pointer to a SocketAddress to store the result. |
kevman | 0:38ceb79fef03 | 58 | * @param version IP version of address to resolve, NSAPI_UNSPEC indicates |
kevman | 0:38ceb79fef03 | 59 | * version is chosen by the stack (defaults to NSAPI_UNSPEC) |
kevman | 0:38ceb79fef03 | 60 | * @return NSAPI_ERROR_OK on success, negative error code on failure |
kevman | 0:38ceb79fef03 | 61 | */ |
kevman | 0:38ceb79fef03 | 62 | virtual nsapi_error_t gethostbyname(const char *host, |
kevman | 0:38ceb79fef03 | 63 | SocketAddress *address, nsapi_version_t version = NSAPI_UNSPEC); |
kevman | 0:38ceb79fef03 | 64 | |
kevman | 0:38ceb79fef03 | 65 | /** Hostname translation callback (asynchronous) |
kevman | 0:38ceb79fef03 | 66 | * |
kevman | 0:38ceb79fef03 | 67 | * Callback will be called after DNS resolution completes or a failure occurs. |
kevman | 0:38ceb79fef03 | 68 | * |
kevman | 0:38ceb79fef03 | 69 | * Callback should not take more than 10ms to execute, otherwise it might |
kevman | 0:38ceb79fef03 | 70 | * prevent underlying thread processing. A portable user of the callback |
kevman | 0:38ceb79fef03 | 71 | * should not make calls to network operations due to stack size limitations. |
kevman | 0:38ceb79fef03 | 72 | * The callback should not perform expensive operations such as socket recv/send |
kevman | 0:38ceb79fef03 | 73 | * calls or blocking operations. |
kevman | 0:38ceb79fef03 | 74 | * |
kevman | 0:38ceb79fef03 | 75 | * @param status NSAPI_ERROR_OK on success, negative error code on failure |
kevman | 0:38ceb79fef03 | 76 | * @param address On success, destination for the host SocketAddress |
kevman | 0:38ceb79fef03 | 77 | */ |
kevman | 0:38ceb79fef03 | 78 | typedef mbed::Callback<void (nsapi_error_t result, SocketAddress *address)> hostbyname_cb_t; |
kevman | 0:38ceb79fef03 | 79 | |
kevman | 0:38ceb79fef03 | 80 | /** Translates a hostname to an IP address (asynchronous) |
kevman | 0:38ceb79fef03 | 81 | * |
kevman | 0:38ceb79fef03 | 82 | * The hostname may be either a domain name or an IP address. If the |
kevman | 0:38ceb79fef03 | 83 | * hostname is an IP address, no network transactions will be performed. |
kevman | 0:38ceb79fef03 | 84 | * |
kevman | 0:38ceb79fef03 | 85 | * If no stack-specific DNS resolution is provided, the hostname |
kevman | 0:38ceb79fef03 | 86 | * will be resolve using a UDP socket on the stack. |
kevman | 0:38ceb79fef03 | 87 | * |
kevman | 0:38ceb79fef03 | 88 | * Call is non-blocking. Result of the DNS operation is returned by the callback. |
kevman | 0:38ceb79fef03 | 89 | * If this function returns failure, callback will not be called. In case result |
kevman | 0:38ceb79fef03 | 90 | * is success (IP address was found from DNS cache), callback will be called |
kevman | 0:38ceb79fef03 | 91 | * before function returns. |
kevman | 0:38ceb79fef03 | 92 | * |
kevman | 0:38ceb79fef03 | 93 | * @param host Hostname to resolve |
kevman | 0:38ceb79fef03 | 94 | * @param callback Callback that is called for result |
kevman | 0:38ceb79fef03 | 95 | * @param version IP version of address to resolve, NSAPI_UNSPEC indicates |
kevman | 0:38ceb79fef03 | 96 | * version is chosen by the stack (defaults to NSAPI_UNSPEC) |
kevman | 0:38ceb79fef03 | 97 | * @return 0 on immediate success, |
kevman | 0:38ceb79fef03 | 98 | * negative error code on immediate failure or |
kevman | 0:38ceb79fef03 | 99 | * a positive unique id that represents the hostname translation operation |
kevman | 0:38ceb79fef03 | 100 | * and can be passed to cancel |
kevman | 0:38ceb79fef03 | 101 | */ |
kevman | 0:38ceb79fef03 | 102 | virtual nsapi_value_or_error_t gethostbyname_async(const char *host, hostbyname_cb_t callback, |
kevman | 0:38ceb79fef03 | 103 | nsapi_version_t version = NSAPI_UNSPEC); |
kevman | 0:38ceb79fef03 | 104 | |
kevman | 0:38ceb79fef03 | 105 | /** Cancels asynchronous hostname translation |
kevman | 0:38ceb79fef03 | 106 | * |
kevman | 0:38ceb79fef03 | 107 | * When translation is cancelled, callback will not be called. |
kevman | 0:38ceb79fef03 | 108 | * |
kevman | 0:38ceb79fef03 | 109 | * @param id Unique id of the hostname translation operation |
kevman | 0:38ceb79fef03 | 110 | * @return NSAPI_ERROR_OK on success, negative error code on failure |
kevman | 0:38ceb79fef03 | 111 | */ |
kevman | 0:38ceb79fef03 | 112 | virtual nsapi_error_t gethostbyname_async_cancel(int id); |
kevman | 0:38ceb79fef03 | 113 | |
kevman | 0:38ceb79fef03 | 114 | /** Add a domain name server to list of servers to query |
kevman | 0:38ceb79fef03 | 115 | * |
kevman | 0:38ceb79fef03 | 116 | * @param address Destination for the host address |
kevman | 0:38ceb79fef03 | 117 | * @return NSAPI_ERROR_OK on success, negative error code on failure |
kevman | 0:38ceb79fef03 | 118 | */ |
kevman | 0:38ceb79fef03 | 119 | virtual nsapi_error_t add_dns_server(const SocketAddress &address); |
kevman | 0:38ceb79fef03 | 120 | |
kevman | 0:38ceb79fef03 | 121 | /** Get a domain name server from a list of servers to query |
kevman | 0:38ceb79fef03 | 122 | * |
kevman | 0:38ceb79fef03 | 123 | * Returns a DNS server address for a index. If returns error no more |
kevman | 0:38ceb79fef03 | 124 | * DNS servers to read. |
kevman | 0:38ceb79fef03 | 125 | * |
kevman | 0:38ceb79fef03 | 126 | * @param index Index of the DNS server, starts from zero |
kevman | 0:38ceb79fef03 | 127 | * @param address Destination for the host address |
kevman | 0:38ceb79fef03 | 128 | * @return NSAPI_ERROR_OK on success, negative error code on failure |
kevman | 0:38ceb79fef03 | 129 | */ |
kevman | 0:38ceb79fef03 | 130 | virtual nsapi_error_t get_dns_server(int index, SocketAddress *address); |
kevman | 0:38ceb79fef03 | 131 | |
kevman | 0:38ceb79fef03 | 132 | /* Set stack options |
kevman | 0:38ceb79fef03 | 133 | * |
kevman | 0:38ceb79fef03 | 134 | * setstackopt allows an application to pass stack-specific options |
kevman | 0:38ceb79fef03 | 135 | * to the underlying stack using stack-specific level and option names, |
kevman | 0:38ceb79fef03 | 136 | * or to request generic options using levels from nsapi_stack_level_t. |
kevman | 0:38ceb79fef03 | 137 | * |
kevman | 0:38ceb79fef03 | 138 | * For unsupported options, NSAPI_ERROR_UNSUPPORTED is returned |
kevman | 0:38ceb79fef03 | 139 | * and the stack is unmodified. |
kevman | 0:38ceb79fef03 | 140 | * |
kevman | 0:38ceb79fef03 | 141 | * @param level Stack-specific protocol level or nsapi_stack_level_t |
kevman | 0:38ceb79fef03 | 142 | * @param optname Level-specific option name |
kevman | 0:38ceb79fef03 | 143 | * @param optval Option value |
kevman | 0:38ceb79fef03 | 144 | * @param optlen Length of the option value |
kevman | 0:38ceb79fef03 | 145 | * @return NSAPI_ERROR_OK on success, negative error code on failure |
kevman | 0:38ceb79fef03 | 146 | */ |
kevman | 0:38ceb79fef03 | 147 | virtual nsapi_error_t setstackopt(int level, int optname, const void *optval, unsigned optlen); |
kevman | 0:38ceb79fef03 | 148 | |
kevman | 0:38ceb79fef03 | 149 | /* Get stack options |
kevman | 0:38ceb79fef03 | 150 | * |
kevman | 0:38ceb79fef03 | 151 | * getstackopt allows an application to retrieve stack-specific options |
kevman | 0:38ceb79fef03 | 152 | * to the underlying stack using stack-specific level and option names, |
kevman | 0:38ceb79fef03 | 153 | * or to request generic options using levels from nsapi_stack_level_t. |
kevman | 0:38ceb79fef03 | 154 | * |
kevman | 0:38ceb79fef03 | 155 | * @param level Stack-specific protocol level or nsapi_stack_level_t |
kevman | 0:38ceb79fef03 | 156 | * @param optname Level-specific option name |
kevman | 0:38ceb79fef03 | 157 | * @param optval Destination for option value |
kevman | 0:38ceb79fef03 | 158 | * @param optlen Length of the option value |
kevman | 0:38ceb79fef03 | 159 | * @return NSAPI_ERROR_OK on success, negative error code on failure |
kevman | 0:38ceb79fef03 | 160 | */ |
kevman | 0:38ceb79fef03 | 161 | virtual nsapi_error_t getstackopt(int level, int optname, void *optval, unsigned *optlen); |
kevman | 0:38ceb79fef03 | 162 | |
kevman | 0:38ceb79fef03 | 163 | /** Dynamic downcast to a OnboardNetworkStack */ |
kevman | 0:38ceb79fef03 | 164 | virtual OnboardNetworkStack *onboardNetworkStack() |
kevman | 0:38ceb79fef03 | 165 | { |
kevman | 0:38ceb79fef03 | 166 | return 0; |
kevman | 0:38ceb79fef03 | 167 | } |
kevman | 0:38ceb79fef03 | 168 | |
kevman | 0:38ceb79fef03 | 169 | protected: |
kevman | 0:38ceb79fef03 | 170 | friend class InternetSocket; |
kevman | 0:38ceb79fef03 | 171 | friend class UDPSocket; |
kevman | 0:38ceb79fef03 | 172 | friend class TCPSocket; |
kevman | 0:38ceb79fef03 | 173 | friend class TCPServer; |
kevman | 0:38ceb79fef03 | 174 | |
kevman | 0:38ceb79fef03 | 175 | /** Opens a socket |
kevman | 0:38ceb79fef03 | 176 | * |
kevman | 0:38ceb79fef03 | 177 | * Creates a network socket and stores it in the specified handle. |
kevman | 0:38ceb79fef03 | 178 | * The handle must be passed to following calls on the socket. |
kevman | 0:38ceb79fef03 | 179 | * |
kevman | 0:38ceb79fef03 | 180 | * A stack may have a finite number of sockets, in this case |
kevman | 0:38ceb79fef03 | 181 | * NSAPI_ERROR_NO_SOCKET is returned if no socket is available. |
kevman | 0:38ceb79fef03 | 182 | * |
kevman | 0:38ceb79fef03 | 183 | * @param handle Destination for the handle to a newly created socket |
kevman | 0:38ceb79fef03 | 184 | * @param proto Protocol of socket to open, NSAPI_TCP or NSAPI_UDP |
kevman | 0:38ceb79fef03 | 185 | * @return NSAPI_ERROR_OK on success, negative error code on failure |
kevman | 0:38ceb79fef03 | 186 | */ |
kevman | 0:38ceb79fef03 | 187 | virtual nsapi_error_t socket_open(nsapi_socket_t *handle, nsapi_protocol_t proto) = 0; |
kevman | 0:38ceb79fef03 | 188 | |
kevman | 0:38ceb79fef03 | 189 | /** Close the socket |
kevman | 0:38ceb79fef03 | 190 | * |
kevman | 0:38ceb79fef03 | 191 | * Closes any open connection and deallocates any memory associated |
kevman | 0:38ceb79fef03 | 192 | * with the socket. |
kevman | 0:38ceb79fef03 | 193 | * |
kevman | 0:38ceb79fef03 | 194 | * @param handle Socket handle |
kevman | 0:38ceb79fef03 | 195 | * @return NSAPI_ERROR_OK on success, negative error code on failure |
kevman | 0:38ceb79fef03 | 196 | */ |
kevman | 0:38ceb79fef03 | 197 | virtual nsapi_error_t socket_close(nsapi_socket_t handle) = 0; |
kevman | 0:38ceb79fef03 | 198 | |
kevman | 0:38ceb79fef03 | 199 | /** Bind a specific address to a socket |
kevman | 0:38ceb79fef03 | 200 | * |
kevman | 0:38ceb79fef03 | 201 | * Binding a socket specifies the address and port on which to receive |
kevman | 0:38ceb79fef03 | 202 | * data. If the IP address is zeroed, only the port is bound. |
kevman | 0:38ceb79fef03 | 203 | * |
kevman | 0:38ceb79fef03 | 204 | * @param handle Socket handle |
kevman | 0:38ceb79fef03 | 205 | * @param address Local address to bind |
kevman | 0:38ceb79fef03 | 206 | * @return NSAPI_ERROR_OK on success, negative error code on failure. |
kevman | 0:38ceb79fef03 | 207 | */ |
kevman | 0:38ceb79fef03 | 208 | virtual nsapi_error_t socket_bind(nsapi_socket_t handle, const SocketAddress &address) = 0; |
kevman | 0:38ceb79fef03 | 209 | |
kevman | 0:38ceb79fef03 | 210 | /** Listen for connections on a TCP socket |
kevman | 0:38ceb79fef03 | 211 | * |
kevman | 0:38ceb79fef03 | 212 | * Marks the socket as a passive socket that can be used to accept |
kevman | 0:38ceb79fef03 | 213 | * incoming connections. |
kevman | 0:38ceb79fef03 | 214 | * |
kevman | 0:38ceb79fef03 | 215 | * @param handle Socket handle |
kevman | 0:38ceb79fef03 | 216 | * @param backlog Number of pending connections that can be queued |
kevman | 0:38ceb79fef03 | 217 | * simultaneously |
kevman | 0:38ceb79fef03 | 218 | * @return NSAPI_ERROR_OK on success, negative error code on failure |
kevman | 0:38ceb79fef03 | 219 | */ |
kevman | 0:38ceb79fef03 | 220 | virtual nsapi_error_t socket_listen(nsapi_socket_t handle, int backlog) = 0; |
kevman | 0:38ceb79fef03 | 221 | |
kevman | 0:38ceb79fef03 | 222 | /** Connects TCP socket to a remote host |
kevman | 0:38ceb79fef03 | 223 | * |
kevman | 0:38ceb79fef03 | 224 | * Initiates a connection to a remote server specified by the |
kevman | 0:38ceb79fef03 | 225 | * indicated address. |
kevman | 0:38ceb79fef03 | 226 | * |
kevman | 0:38ceb79fef03 | 227 | * @param handle Socket handle |
kevman | 0:38ceb79fef03 | 228 | * @param address The SocketAddress of the remote host |
kevman | 0:38ceb79fef03 | 229 | * @return NSAPI_ERROR_OK on success, negative error code on failure |
kevman | 0:38ceb79fef03 | 230 | */ |
kevman | 0:38ceb79fef03 | 231 | virtual nsapi_error_t socket_connect(nsapi_socket_t handle, const SocketAddress &address) = 0; |
kevman | 0:38ceb79fef03 | 232 | |
kevman | 0:38ceb79fef03 | 233 | /** Accepts a connection on a TCP socket |
kevman | 0:38ceb79fef03 | 234 | * |
kevman | 0:38ceb79fef03 | 235 | * The server socket must be bound and set to listen for connections. |
kevman | 0:38ceb79fef03 | 236 | * On a new connection, creates a network socket and stores it in the |
kevman | 0:38ceb79fef03 | 237 | * specified handle. The handle must be passed to following calls on |
kevman | 0:38ceb79fef03 | 238 | * the socket. |
kevman | 0:38ceb79fef03 | 239 | * |
kevman | 0:38ceb79fef03 | 240 | * A stack may have a finite number of sockets, in this case |
kevman | 0:38ceb79fef03 | 241 | * NSAPI_ERROR_NO_SOCKET is returned if no socket is available. |
kevman | 0:38ceb79fef03 | 242 | * |
kevman | 0:38ceb79fef03 | 243 | * This call is non-blocking. If accept would block, |
kevman | 0:38ceb79fef03 | 244 | * NSAPI_ERROR_WOULD_BLOCK is returned immediately. |
kevman | 0:38ceb79fef03 | 245 | * |
kevman | 0:38ceb79fef03 | 246 | * @param server Socket handle to server to accept from |
kevman | 0:38ceb79fef03 | 247 | * @param handle Destination for a handle to the newly created socket |
kevman | 0:38ceb79fef03 | 248 | * @param address Destination for the remote address or NULL |
kevman | 0:38ceb79fef03 | 249 | * @return NSAPI_ERROR_OK on success, negative error code on failure |
kevman | 0:38ceb79fef03 | 250 | */ |
kevman | 0:38ceb79fef03 | 251 | virtual nsapi_error_t socket_accept(nsapi_socket_t server, |
kevman | 0:38ceb79fef03 | 252 | nsapi_socket_t *handle, SocketAddress *address = 0) = 0; |
kevman | 0:38ceb79fef03 | 253 | |
kevman | 0:38ceb79fef03 | 254 | /** Send data over a TCP socket |
kevman | 0:38ceb79fef03 | 255 | * |
kevman | 0:38ceb79fef03 | 256 | * The socket must be connected to a remote host. Returns the number of |
kevman | 0:38ceb79fef03 | 257 | * bytes sent from the buffer. |
kevman | 0:38ceb79fef03 | 258 | * |
kevman | 0:38ceb79fef03 | 259 | * This call is non-blocking. If send would block, |
kevman | 0:38ceb79fef03 | 260 | * NSAPI_ERROR_WOULD_BLOCK is returned immediately. |
kevman | 0:38ceb79fef03 | 261 | * |
kevman | 0:38ceb79fef03 | 262 | * @param handle Socket handle |
kevman | 0:38ceb79fef03 | 263 | * @param data Buffer of data to send to the host |
kevman | 0:38ceb79fef03 | 264 | * @param size Size of the buffer in bytes |
kevman | 0:38ceb79fef03 | 265 | * @return Number of sent bytes on success, negative error |
kevman | 0:38ceb79fef03 | 266 | * code on failure |
kevman | 0:38ceb79fef03 | 267 | */ |
kevman | 0:38ceb79fef03 | 268 | virtual nsapi_size_or_error_t socket_send(nsapi_socket_t handle, |
kevman | 0:38ceb79fef03 | 269 | const void *data, nsapi_size_t size) = 0; |
kevman | 0:38ceb79fef03 | 270 | |
kevman | 0:38ceb79fef03 | 271 | /** Receive data over a TCP socket |
kevman | 0:38ceb79fef03 | 272 | * |
kevman | 0:38ceb79fef03 | 273 | * The socket must be connected to a remote host. Returns the number of |
kevman | 0:38ceb79fef03 | 274 | * bytes received into the buffer. |
kevman | 0:38ceb79fef03 | 275 | * |
kevman | 0:38ceb79fef03 | 276 | * This call is non-blocking. If recv would block, |
kevman | 0:38ceb79fef03 | 277 | * NSAPI_ERROR_WOULD_BLOCK is returned immediately. |
kevman | 0:38ceb79fef03 | 278 | * |
kevman | 0:38ceb79fef03 | 279 | * @param handle Socket handle |
kevman | 0:38ceb79fef03 | 280 | * @param data Destination buffer for data received from the host |
kevman | 0:38ceb79fef03 | 281 | * @param size Size of the buffer in bytes |
kevman | 0:38ceb79fef03 | 282 | * @return Number of received bytes on success, negative error |
kevman | 0:38ceb79fef03 | 283 | * code on failure |
kevman | 0:38ceb79fef03 | 284 | */ |
kevman | 0:38ceb79fef03 | 285 | virtual nsapi_size_or_error_t socket_recv(nsapi_socket_t handle, |
kevman | 0:38ceb79fef03 | 286 | void *data, nsapi_size_t size) = 0; |
kevman | 0:38ceb79fef03 | 287 | |
kevman | 0:38ceb79fef03 | 288 | /** Send a packet over a UDP socket |
kevman | 0:38ceb79fef03 | 289 | * |
kevman | 0:38ceb79fef03 | 290 | * Sends data to the specified address. Returns the number of bytes |
kevman | 0:38ceb79fef03 | 291 | * sent from the buffer. |
kevman | 0:38ceb79fef03 | 292 | * |
kevman | 0:38ceb79fef03 | 293 | * This call is non-blocking. If sendto would block, |
kevman | 0:38ceb79fef03 | 294 | * NSAPI_ERROR_WOULD_BLOCK is returned immediately. |
kevman | 0:38ceb79fef03 | 295 | * |
kevman | 0:38ceb79fef03 | 296 | * @param handle Socket handle |
kevman | 0:38ceb79fef03 | 297 | * @param address The SocketAddress of the remote host |
kevman | 0:38ceb79fef03 | 298 | * @param data Buffer of data to send to the host |
kevman | 0:38ceb79fef03 | 299 | * @param size Size of the buffer in bytes |
kevman | 0:38ceb79fef03 | 300 | * @return Number of sent bytes on success, negative error |
kevman | 0:38ceb79fef03 | 301 | * code on failure |
kevman | 0:38ceb79fef03 | 302 | */ |
kevman | 0:38ceb79fef03 | 303 | virtual nsapi_size_or_error_t socket_sendto(nsapi_socket_t handle, const SocketAddress &address, |
kevman | 0:38ceb79fef03 | 304 | const void *data, nsapi_size_t size) = 0; |
kevman | 0:38ceb79fef03 | 305 | |
kevman | 0:38ceb79fef03 | 306 | /** Receive a packet over a UDP socket |
kevman | 0:38ceb79fef03 | 307 | * |
kevman | 0:38ceb79fef03 | 308 | * Receives data and stores the source address in address if address |
kevman | 0:38ceb79fef03 | 309 | * is not NULL. Returns the number of bytes received into the buffer. |
kevman | 0:38ceb79fef03 | 310 | * |
kevman | 0:38ceb79fef03 | 311 | * This call is non-blocking. If recvfrom would block, |
kevman | 0:38ceb79fef03 | 312 | * NSAPI_ERROR_WOULD_BLOCK is returned immediately. |
kevman | 0:38ceb79fef03 | 313 | * |
kevman | 0:38ceb79fef03 | 314 | * @param handle Socket handle |
kevman | 0:38ceb79fef03 | 315 | * @param address Destination for the source address or NULL |
kevman | 0:38ceb79fef03 | 316 | * @param buffer Destination buffer for data received from the host |
kevman | 0:38ceb79fef03 | 317 | * @param size Size of the buffer in bytes |
kevman | 0:38ceb79fef03 | 318 | * @return Number of received bytes on success, negative error |
kevman | 0:38ceb79fef03 | 319 | * code on failure |
kevman | 0:38ceb79fef03 | 320 | */ |
kevman | 0:38ceb79fef03 | 321 | virtual nsapi_size_or_error_t socket_recvfrom(nsapi_socket_t handle, SocketAddress *address, |
kevman | 0:38ceb79fef03 | 322 | void *buffer, nsapi_size_t size) = 0; |
kevman | 0:38ceb79fef03 | 323 | |
kevman | 0:38ceb79fef03 | 324 | /** Register a callback on state change of the socket |
kevman | 0:38ceb79fef03 | 325 | * |
kevman | 0:38ceb79fef03 | 326 | * The specified callback will be called on state changes such as when |
kevman | 0:38ceb79fef03 | 327 | * the socket can recv/send/accept successfully and on when an error |
kevman | 0:38ceb79fef03 | 328 | * occurs. The callback may also be called spuriously without reason. |
kevman | 0:38ceb79fef03 | 329 | * |
kevman | 0:38ceb79fef03 | 330 | * The callback may be called in an interrupt context and should not |
kevman | 0:38ceb79fef03 | 331 | * perform expensive operations such as recv/send calls. |
kevman | 0:38ceb79fef03 | 332 | * |
kevman | 0:38ceb79fef03 | 333 | * @param handle Socket handle |
kevman | 0:38ceb79fef03 | 334 | * @param callback Function to call on state change |
kevman | 0:38ceb79fef03 | 335 | * @param data Argument to pass to callback |
kevman | 0:38ceb79fef03 | 336 | */ |
kevman | 0:38ceb79fef03 | 337 | virtual void socket_attach(nsapi_socket_t handle, void (*callback)(void *), void *data) = 0; |
kevman | 0:38ceb79fef03 | 338 | |
kevman | 0:38ceb79fef03 | 339 | /** Set stack-specific socket options. |
kevman | 0:38ceb79fef03 | 340 | * |
kevman | 0:38ceb79fef03 | 341 | * The setsockopt allow an application to pass stack-specific hints |
kevman | 0:38ceb79fef03 | 342 | * to the underlying stack. For unsupported options, |
kevman | 0:38ceb79fef03 | 343 | * NSAPI_ERROR_UNSUPPORTED is returned and the socket is unmodified. |
kevman | 0:38ceb79fef03 | 344 | * |
kevman | 0:38ceb79fef03 | 345 | * @param handle Socket handle. |
kevman | 0:38ceb79fef03 | 346 | * @param level Stack-specific protocol level. |
kevman | 0:38ceb79fef03 | 347 | * @param optname Stack-specific option identifier. |
kevman | 0:38ceb79fef03 | 348 | * @param optval Option value. |
kevman | 0:38ceb79fef03 | 349 | * @param optlen Length of the option value. |
kevman | 0:38ceb79fef03 | 350 | * @return NSAPI_ERROR_OK on success, negative error code on failure. |
kevman | 0:38ceb79fef03 | 351 | */ |
kevman | 0:38ceb79fef03 | 352 | virtual nsapi_error_t setsockopt(nsapi_socket_t handle, int level, |
kevman | 0:38ceb79fef03 | 353 | int optname, const void *optval, unsigned optlen); |
kevman | 0:38ceb79fef03 | 354 | |
kevman | 0:38ceb79fef03 | 355 | /** Get stack-specific socket options. |
kevman | 0:38ceb79fef03 | 356 | * |
kevman | 0:38ceb79fef03 | 357 | * The getstackopt allow an application to retrieve stack-specific hints |
kevman | 0:38ceb79fef03 | 358 | * from the underlying stack. For unsupported options, |
kevman | 0:38ceb79fef03 | 359 | * NSAPI_ERROR_UNSUPPORTED is returned and optval is unmodified. |
kevman | 0:38ceb79fef03 | 360 | * |
kevman | 0:38ceb79fef03 | 361 | * @param handle Socket handle. |
kevman | 0:38ceb79fef03 | 362 | * @param level Stack-specific protocol level. |
kevman | 0:38ceb79fef03 | 363 | * @param optname Stack-specific option identifier. |
kevman | 0:38ceb79fef03 | 364 | * @param optval Destination for option value. |
kevman | 0:38ceb79fef03 | 365 | * @param optlen Length of the option value. |
kevman | 0:38ceb79fef03 | 366 | * @return NSAPI_ERROR_OK on success, negative error code on failure. |
kevman | 0:38ceb79fef03 | 367 | */ |
kevman | 0:38ceb79fef03 | 368 | virtual nsapi_error_t getsockopt(nsapi_socket_t handle, int level, |
kevman | 0:38ceb79fef03 | 369 | int optname, void *optval, unsigned *optlen); |
kevman | 0:38ceb79fef03 | 370 | |
kevman | 0:38ceb79fef03 | 371 | private: |
kevman | 0:38ceb79fef03 | 372 | |
kevman | 0:38ceb79fef03 | 373 | /** Call in callback |
kevman | 0:38ceb79fef03 | 374 | * |
kevman | 0:38ceb79fef03 | 375 | * Callback is used to call the call in method of the network stack. |
kevman | 0:38ceb79fef03 | 376 | */ |
kevman | 0:38ceb79fef03 | 377 | typedef mbed::Callback<nsapi_error_t (int delay_ms, mbed::Callback<void()> user_cb)> call_in_callback_cb_t; |
kevman | 0:38ceb79fef03 | 378 | |
kevman | 0:38ceb79fef03 | 379 | /** Get a call in callback |
kevman | 0:38ceb79fef03 | 380 | * |
kevman | 0:38ceb79fef03 | 381 | * Get a call in callback from the network stack context. |
kevman | 0:38ceb79fef03 | 382 | * |
kevman | 0:38ceb79fef03 | 383 | * Callback should not take more than 10ms to execute, otherwise it might |
kevman | 0:38ceb79fef03 | 384 | * prevent underlying thread processing. A portable user of the callback |
kevman | 0:38ceb79fef03 | 385 | * should not make calls to network operations due to stack size limitations. |
kevman | 0:38ceb79fef03 | 386 | * The callback should not perform expensive operations such as socket recv/send |
kevman | 0:38ceb79fef03 | 387 | * calls or blocking operations. |
kevman | 0:38ceb79fef03 | 388 | * |
kevman | 0:38ceb79fef03 | 389 | * @return Call in callback |
kevman | 0:38ceb79fef03 | 390 | */ |
kevman | 0:38ceb79fef03 | 391 | virtual call_in_callback_cb_t get_call_in_callback(); |
kevman | 0:38ceb79fef03 | 392 | |
kevman | 0:38ceb79fef03 | 393 | /** Call a callback after a delay |
kevman | 0:38ceb79fef03 | 394 | * |
kevman | 0:38ceb79fef03 | 395 | * Call a callback from the network stack context after a delay. If function |
kevman | 0:38ceb79fef03 | 396 | * returns error callback will not be called. |
kevman | 0:38ceb79fef03 | 397 | * |
kevman | 0:38ceb79fef03 | 398 | * @param delay Delay in milliseconds |
kevman | 0:38ceb79fef03 | 399 | * @param func Callback to be called |
kevman | 0:38ceb79fef03 | 400 | * @return NSAPI_ERROR_OK on success, negative error code on failure |
kevman | 0:38ceb79fef03 | 401 | */ |
kevman | 0:38ceb79fef03 | 402 | virtual nsapi_error_t call_in(int delay, mbed::Callback<void()> func); |
kevman | 0:38ceb79fef03 | 403 | }; |
kevman | 0:38ceb79fef03 | 404 | |
kevman | 0:38ceb79fef03 | 405 | /** Convert a raw nsapi_stack_t object into a C++ NetworkStack object |
kevman | 0:38ceb79fef03 | 406 | * |
kevman | 0:38ceb79fef03 | 407 | * @param stack Reference to an object that can be converted to a stack |
kevman | 0:38ceb79fef03 | 408 | * - A raw nsapi_stack_t object |
kevman | 0:38ceb79fef03 | 409 | * - A reference to a network stack |
kevman | 0:38ceb79fef03 | 410 | * - A reference to a network interface |
kevman | 0:38ceb79fef03 | 411 | * @return Reference to the underlying network stack |
kevman | 0:38ceb79fef03 | 412 | */ |
kevman | 0:38ceb79fef03 | 413 | NetworkStack *nsapi_create_stack(nsapi_stack_t *stack); |
kevman | 0:38ceb79fef03 | 414 | NetworkStack *nsapi_create_stack(NetworkStack *stack); |
kevman | 0:38ceb79fef03 | 415 | |
kevman | 0:38ceb79fef03 | 416 | template <typename IF> |
kevman | 0:38ceb79fef03 | 417 | NetworkStack *nsapi_create_stack(IF *iface) |
kevman | 0:38ceb79fef03 | 418 | { |
kevman | 0:38ceb79fef03 | 419 | return nsapi_create_stack(static_cast<NetworkInterface *>(iface)->get_stack()); |
kevman | 0:38ceb79fef03 | 420 | } |
kevman | 0:38ceb79fef03 | 421 | |
kevman | 0:38ceb79fef03 | 422 | |
kevman | 0:38ceb79fef03 | 423 | #endif |