mbed-os for GR-LYCHEE

Dependents:   mbed-os-example-blinky-gr-lychee GR-Boads_Camera_sample GR-Boards_Audio_Recoder GR-Boads_Camera_DisplayApp ... more

Committer:
dkato
Date:
Fri Feb 02 05:42:23 2018 +0000
Revision:
0:f782d9c66c49
mbed-os for GR-LYCHEE

Who changed what in which revision?

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