Color Oled(SSD1331) connect to STMicroelectronics Nucleo-F466

Dependencies:   ssd1331

Committer:
kadonotakashi
Date:
Thu Oct 11 02:27:46 2018 +0000
Revision:
3:f3764f852aa8
Parent:
0:8fdf9a60065b
Nucreo 446 + SSD1331 test version;

Who changed what in which revision?

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