mbed os with nrf51 internal bandgap enabled to read battery level

Dependents:   BLE_file_test BLE_Blink ExternalEncoder

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

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