NetworkSocketAPI

Dependencies:   DnsQuery

Dependents:   HelloWizFi250Interface

Fork of NetworkSocketAPI by NetworkSocketAPI

Committer:
geky
Date:
Tue Apr 05 19:21:41 2016 +0000
Revision:
84:d317e056fd20
Parent:
81:1600369a29dd
Child:
87:94e2cf3a06be
Added thunk for Socket class

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Christopher Haster 78:0914f9b9b24b 1 /* Socket
Christopher Haster 76:bbe51641f405 2 * Copyright (c) 2015 ARM Limited
Christopher Haster 76:bbe51641f405 3 *
Christopher Haster 76:bbe51641f405 4 * Licensed under the Apache License, Version 2.0 (the "License");
Christopher Haster 76:bbe51641f405 5 * you may not use this file except in compliance with the License.
Christopher Haster 76:bbe51641f405 6 * You may obtain a copy of the License at
Christopher Haster 76:bbe51641f405 7 *
Christopher Haster 76:bbe51641f405 8 * http://www.apache.org/licenses/LICENSE-2.0
Christopher Haster 76:bbe51641f405 9 *
Christopher Haster 76:bbe51641f405 10 * Unless required by applicable law or agreed to in writing, software
Christopher Haster 76:bbe51641f405 11 * distributed under the License is distributed on an "AS IS" BASIS,
Christopher Haster 76:bbe51641f405 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Christopher Haster 76:bbe51641f405 13 * See the License for the specific language governing permissions and
Christopher Haster 76:bbe51641f405 14 * limitations under the License.
Christopher Haster 76:bbe51641f405 15 */
Christopher Haster 76:bbe51641f405 16
Christopher Haster 76:bbe51641f405 17 #ifndef NETWORK_INTERFACE_H
Christopher Haster 76:bbe51641f405 18 #define NETWORK_INTERFACE_H
Christopher Haster 76:bbe51641f405 19
Christopher Haster 81:1600369a29dd 20 #ifndef MBED_OPERATORS
Christopher Haster 81:1600369a29dd 21 #define MBED_OPERATORS
Christopher Haster 81:1600369a29dd 22 #endif
Christopher Haster 79:43a7e8c0d6cc 23 #include "FunctionPointer.h"
Christopher Haster 80:9c6673c93082 24 #include "SocketAddress.h"
Christopher Haster 79:43a7e8c0d6cc 25
Christopher Haster 77:b66a6984ed2d 26 /**
Christopher Haster 77:b66a6984ed2d 27 * @enum ns_error_t
Christopher Haster 77:b66a6984ed2d 28 * @brief enum of standardized error codes
Christopher Haster 77:b66a6984ed2d 29 */
Christopher Haster 77:b66a6984ed2d 30 enum ns_error_t {
Christopher Haster 77:b66a6984ed2d 31 NSAPI_ERROR_WOULD_BLOCK = -3001, /*!< no data is not available but call is non-blocking */
Christopher Haster 77:b66a6984ed2d 32 NSAPI_ERROR_UNSUPPORTED = -3002, /*!< unsupported configuration */
Christopher Haster 77:b66a6984ed2d 33 NSAPI_ERROR_NO_CONNECTION = -3003, /*!< not connected to a network */
Christopher Haster 77:b66a6984ed2d 34 NSAPI_ERROR_NO_SOCKET = -3004, /*!< socket not available for use */
Christopher Haster 77:b66a6984ed2d 35 NSAPI_ERROR_NO_ADDRESS = -3005, /*!< IP address is not known */
Christopher Haster 77:b66a6984ed2d 36 NSAPI_ERROR_NO_MEMORY = -3006, /*!< memory resource not available */
Christopher Haster 77:b66a6984ed2d 37 NSAPI_ERROR_DNS_FAILURE = -3007, /*!< DNS failed to complete successfully */
Christopher Haster 77:b66a6984ed2d 38 NSAPI_ERROR_DHCP_FAILURE = -3008, /*!< DHCP failed to complete successfully */
Christopher Haster 77:b66a6984ed2d 39 NSAPI_ERROR_AUTH_FAILURE = -3009, /*!< connection to access point faield */
Christopher Haster 77:b66a6984ed2d 40 NSAPI_ERROR_DEVICE_ERROR = -3010, /*!< failure interfacing with the network procesor */
Christopher Haster 77:b66a6984ed2d 41 };
Christopher Haster 77:b66a6984ed2d 42
Christopher Haster 77:b66a6984ed2d 43 /**
Christopher Haster 77:b66a6984ed2d 44 * @enum ns_opt_t
Christopher Haster 77:b66a6984ed2d 45 * @brief enum of available options
Christopher Haster 77:b66a6984ed2d 46 */
Christopher Haster 77:b66a6984ed2d 47 enum ns_opt_t {
Christopher Haster 77:b66a6984ed2d 48 };
Christopher Haster 77:b66a6984ed2d 49
Christopher Haster 76:bbe51641f405 50 /** NetworkInterface class
Christopher Haster 76:bbe51641f405 51 * Common interface that is shared between all hardware that
Christopher Haster 76:bbe51641f405 52 * can connect to a network over IP.
Christopher Haster 76:bbe51641f405 53 */
Christopher Haster 76:bbe51641f405 54 class NetworkInterface
Christopher Haster 76:bbe51641f405 55 {
Christopher Haster 76:bbe51641f405 56 public:
Christopher Haster 76:bbe51641f405 57 virtual ~NetworkInterface() {};
Christopher Haster 76:bbe51641f405 58
Christopher Haster 76:bbe51641f405 59 /** Get the internally stored IP address
Christopher Haster 76:bbe51641f405 60 /return IP address of the interface or null if not yet connected
Christopher Haster 76:bbe51641f405 61 */
Christopher Haster 76:bbe51641f405 62 virtual const char *get_ip_address() = 0;
Christopher Haster 76:bbe51641f405 63
Christopher Haster 76:bbe51641f405 64 /** Get the internally stored MAC address
Christopher Haster 76:bbe51641f405 65 /return MAC address of the interface
Christopher Haster 76:bbe51641f405 66 */
Christopher Haster 76:bbe51641f405 67 virtual const char *get_mac_address() = 0;
Christopher Haster 76:bbe51641f405 68
Christopher Haster 76:bbe51641f405 69 /** Get the current status of the interface
Christopher Haster 76:bbe51641f405 70 /return true if connected
Christopher Haster 76:bbe51641f405 71 */
Christopher Haster 76:bbe51641f405 72 virtual bool is_connected() {
Christopher Haster 76:bbe51641f405 73 return get_ip_address() != NULL;
Christopher Haster 76:bbe51641f405 74 }
Christopher Haster 76:bbe51641f405 75
Christopher Haster 76:bbe51641f405 76 /** Looks up the specified host's IP address
Christopher Haster 76:bbe51641f405 77 /param name Hostname to lookup
Christopher Haster 80:9c6673c93082 78 /param dest Destination for IP address, must have space for SocketAddress::IP_SIZE
Christopher Haster 80:9c6673c93082 79 /return 0 on success, negative on failure
Christopher Haster 76:bbe51641f405 80 */
Christopher Haster 80:9c6673c93082 81 virtual int gethostbyname(const char *name, char *dest);
Christopher Haster 76:bbe51641f405 82
Christopher Haster 76:bbe51641f405 83 protected:
Christopher Haster 79:43a7e8c0d6cc 84 friend class Socket;
Christopher Haster 79:43a7e8c0d6cc 85 friend class UDPSocket;
Christopher Haster 79:43a7e8c0d6cc 86 friend class TCPSocket;
Christopher Haster 79:43a7e8c0d6cc 87 friend class TCPServer;
Christopher Haster 79:43a7e8c0d6cc 88
Christopher Haster 76:bbe51641f405 89 /** Enum of socket protocols
Christopher Haster 76:bbe51641f405 90 /enum protocol_t
Christopher Haster 76:bbe51641f405 91 */
Christopher Haster 76:bbe51641f405 92 enum protocol_t {
Christopher Haster 76:bbe51641f405 93 TCP, /*!< Socket is of TCP type */
Christopher Haster 76:bbe51641f405 94 UDP, /*!< Socket is of UDP type */
Christopher Haster 76:bbe51641f405 95 };
Christopher Haster 76:bbe51641f405 96
Christopher Haster 76:bbe51641f405 97 /** Create a socket
Christopher Haster 76:bbe51641f405 98 /param proto The type of socket to open, TCP or UDP
Christopher Haster 76:bbe51641f405 99 /return The alocated socket or null on failure
Christopher Haster 76:bbe51641f405 100 */
Christopher Haster 76:bbe51641f405 101 virtual void *socket_create(protocol_t proto) = 0;
Christopher Haster 76:bbe51641f405 102
Christopher Haster 76:bbe51641f405 103 /** Destroy a socket
Christopher Haster 76:bbe51641f405 104 /param socket Previously allocated socket
Christopher Haster 76:bbe51641f405 105 */
Christopher Haster 76:bbe51641f405 106 virtual void socket_destroy(void *handle) = 0;
Christopher Haster 76:bbe51641f405 107
Christopher Haster 76:bbe51641f405 108 /** Set socket options
Christopher Haster 76:bbe51641f405 109 \param handle Socket handle
Christopher Haster 76:bbe51641f405 110 \param optname Option ID
Christopher Haster 76:bbe51641f405 111 \param optval Option value
Christopher Haster 76:bbe51641f405 112 \param optlen Length of the option value
Christopher Haster 76:bbe51641f405 113 \return 0 on success, negative on failure
Christopher Haster 76:bbe51641f405 114 */
Christopher Haster 76:bbe51641f405 115 virtual int socket_set_option(void *handle, int optname, const void *optval, unsigned int optlen) = 0;
Christopher Haster 76:bbe51641f405 116
Christopher Haster 76:bbe51641f405 117 /** Get socket options
Christopher Haster 76:bbe51641f405 118 \param handle Socket handle
Christopher Haster 76:bbe51641f405 119 \param optname Option ID
Christopher Haster 76:bbe51641f405 120 \param optval Buffer pointer where to write the option value
Christopher Haster 76:bbe51641f405 121 \param optlen Length of the option value
Christopher Haster 76:bbe51641f405 122 \return 0 on success, negative on failure
Christopher Haster 76:bbe51641f405 123 */
Christopher Haster 76:bbe51641f405 124 virtual int socket_get_option(void *handle, int optname, void *optval, unsigned int *optlen) = 0;
Christopher Haster 76:bbe51641f405 125
Christopher Haster 76:bbe51641f405 126 /** Bind a server socket to a specific port
Christopher Haster 76:bbe51641f405 127 \param handle Socket handle
Christopher Haster 76:bbe51641f405 128 \param port The port to listen for incoming connections on
Christopher Haster 76:bbe51641f405 129 \return 0 on success, negative on failure.
Christopher Haster 76:bbe51641f405 130 */
Christopher Haster 76:bbe51641f405 131 virtual int socket_bind(void *handle, int port) = 0;
Christopher Haster 76:bbe51641f405 132
Christopher Haster 76:bbe51641f405 133 /** Start listening for incoming connections
Christopher Haster 76:bbe51641f405 134 \param handle Socket handle
Christopher Haster 76:bbe51641f405 135 \param backlog Number of pending connections that can be queued up at any
Christopher Haster 76:bbe51641f405 136 one time [Default: 1]
Christopher Haster 76:bbe51641f405 137 \return 0 on success, negative on failure
Christopher Haster 76:bbe51641f405 138 */
Christopher Haster 80:9c6673c93082 139 virtual int socket_listen(void *handle, int backlog) = 0;
Christopher Haster 76:bbe51641f405 140
Christopher Haster 76:bbe51641f405 141 /** Connects this TCP socket to the server
Christopher Haster 76:bbe51641f405 142 \param handle Socket handle
Christopher Haster 76:bbe51641f405 143 \param address SocketAddress to connect to
Christopher Haster 76:bbe51641f405 144 \return 0 on success, negative on failure
Christopher Haster 76:bbe51641f405 145 */
Christopher Haster 80:9c6673c93082 146 virtual int socket_connect(void *handle, const SocketAddress &address) = 0;
Christopher Haster 81:1600369a29dd 147
Christopher Haster 76:bbe51641f405 148 /** Check if the socket is connected
Christopher Haster 76:bbe51641f405 149 \param handle Socket handle
Christopher Haster 76:bbe51641f405 150 \return true if connected, false otherwise
Christopher Haster 76:bbe51641f405 151 */
Christopher Haster 80:9c6673c93082 152 virtual bool socket_is_connected(void *handle) = 0;
Christopher Haster 76:bbe51641f405 153
Christopher Haster 81:1600369a29dd 154 /** Accept a new connection.
Christopher Haster 81:1600369a29dd 155 \param handle Socket handle
Christopher Haster 81:1600369a29dd 156 \param socket A TCPSocket instance that will handle the incoming connection.
Christopher Haster 81:1600369a29dd 157 \return 0 on success, negative on failure.
Christopher Haster 81:1600369a29dd 158 \note This call is not-blocking, if this call would block, must
Christopher Haster 81:1600369a29dd 159 immediately return NSAPI_ERROR_WOULD_WAIT
Christopher Haster 81:1600369a29dd 160 */
Christopher Haster 81:1600369a29dd 161 virtual int socket_accept(void *handle, void **connection) = 0;
Christopher Haster 81:1600369a29dd 162
Christopher Haster 76:bbe51641f405 163 /** Send data to the remote host
Christopher Haster 76:bbe51641f405 164 \param handle Socket handle
Christopher Haster 76:bbe51641f405 165 \param data The buffer to send to the host
Christopher Haster 76:bbe51641f405 166 \param size The length of the buffer to send
Christopher Haster 76:bbe51641f405 167 \return Number of written bytes on success, negative on failure
Christopher Haster 81:1600369a29dd 168 \note This call is not-blocking, if this call would block, must
Christopher Haster 81:1600369a29dd 169 immediately return NSAPI_ERROR_WOULD_WAIT
Christopher Haster 76:bbe51641f405 170 */
Christopher Haster 76:bbe51641f405 171 virtual int socket_send(void *handle, const void *data, unsigned size) = 0;
Christopher Haster 76:bbe51641f405 172
Christopher Haster 76:bbe51641f405 173 /** Receive data from the remote host
Christopher Haster 76:bbe51641f405 174 \param handle Socket handle
Christopher Haster 76:bbe51641f405 175 \param data The buffer in which to store the data received from the host
Christopher Haster 76:bbe51641f405 176 \param size The maximum length of the buffer
Christopher Haster 76:bbe51641f405 177 \return Number of received bytes on success, negative on failure
Christopher Haster 81:1600369a29dd 178 \note This call is not-blocking, if this call would block, must
Christopher Haster 81:1600369a29dd 179 immediately return NSAPI_ERROR_WOULD_WAIT
Christopher Haster 76:bbe51641f405 180 */
Christopher Haster 76:bbe51641f405 181 virtual int socket_recv(void *handle, void *data, unsigned size) = 0;
Christopher Haster 76:bbe51641f405 182
Christopher Haster 76:bbe51641f405 183 /** Send a packet to a remote endpoint
Christopher Haster 76:bbe51641f405 184 \param handle Socket handle
Christopher Haster 76:bbe51641f405 185 \param address The remote SocketAddress
Christopher Haster 76:bbe51641f405 186 \param data The packet to be sent
Christopher Haster 76:bbe51641f405 187 \param size The length of the packet to be sent
Christopher Haster 76:bbe51641f405 188 \return the number of written bytes on success, negative on failure
Christopher Haster 81:1600369a29dd 189 \note This call is not-blocking, if this call would block, must
Christopher Haster 81:1600369a29dd 190 immediately return NSAPI_ERROR_WOULD_WAIT
Christopher Haster 76:bbe51641f405 191 */
Christopher Haster 80:9c6673c93082 192 virtual int socket_sendto(void *handle, const SocketAddress &address, const void *data, unsigned size) = 0;
Christopher Haster 76:bbe51641f405 193
Christopher Haster 76:bbe51641f405 194 /** Receive a packet from a remote endpoint
Christopher Haster 76:bbe51641f405 195 \param handle Socket handle
Christopher Haster 76:bbe51641f405 196 \param address Destination for the remote SocketAddress or null
Christopher Haster 76:bbe51641f405 197 \param buffer The buffer for storing the incoming packet data
Christopher Haster 76:bbe51641f405 198 If a packet is too long to fit in the supplied buffer,
Christopher Haster 76:bbe51641f405 199 excess bytes are discarded
Christopher Haster 76:bbe51641f405 200 \param size The length of the buffer
Christopher Haster 76:bbe51641f405 201 \return the number of received bytes on success, negative on failure
Christopher Haster 81:1600369a29dd 202 \note This call is not-blocking, if this call would block, must
Christopher Haster 81:1600369a29dd 203 immediately return NSAPI_ERROR_WOULD_WAIT
Christopher Haster 76:bbe51641f405 204 */
Christopher Haster 76:bbe51641f405 205 virtual int socket_recvfrom(void *handle, SocketAddress *address, void *buffer, unsigned size) = 0;
Christopher Haster 76:bbe51641f405 206
Christopher Haster 76:bbe51641f405 207 /** Close the socket
Christopher Haster 76:bbe51641f405 208 \param handle Socket handle
Christopher Haster 76:bbe51641f405 209 \param shutdown free the left-over data in message queues
Christopher Haster 76:bbe51641f405 210 */
Christopher Haster 76:bbe51641f405 211 virtual int socket_close(void *handle, bool shutdown) = 0;
Christopher Haster 76:bbe51641f405 212
Christopher Haster 76:bbe51641f405 213 /** Register a callback on when a new connection is ready
Christopher Haster 76:bbe51641f405 214 \param handle Socket handle
Christopher Haster 76:bbe51641f405 215 \param callback Function to call when accept will succeed, may be called in
Christopher Haster 76:bbe51641f405 216 interrupt context.
Christopher Haster 81:1600369a29dd 217 \param id Argument to pass to callback
Christopher Haster 76:bbe51641f405 218 */
Christopher Haster 81:1600369a29dd 219 virtual void socket_attach_accept(void *handle, void (*callback)(void *), void *id) = 0;
Christopher Haster 76:bbe51641f405 220
Christopher Haster 76:bbe51641f405 221 /** Register a callback on when send is ready
Christopher Haster 76:bbe51641f405 222 \param handle Socket handle
Christopher Haster 81:1600369a29dd 223 \param callback Function to call when accept will succeed, may be called in
Christopher Haster 76:bbe51641f405 224 interrupt context.
Christopher Haster 81:1600369a29dd 225 \param id Argument to pass to callback
Christopher Haster 76:bbe51641f405 226 */
Christopher Haster 81:1600369a29dd 227 virtual void socket_attach_send(void *handle, void (*callback)(void *), void *id) = 0;
Christopher Haster 76:bbe51641f405 228
Christopher Haster 76:bbe51641f405 229 /** Register a callback on when recv is ready
Christopher Haster 76:bbe51641f405 230 \param handle Socket handle
Christopher Haster 81:1600369a29dd 231 \param callback Function to call when accept will succeed, may be called in
Christopher Haster 76:bbe51641f405 232 interrupt context.
Christopher Haster 81:1600369a29dd 233 \param id Argument to pass to callback
Christopher Haster 76:bbe51641f405 234 */
Christopher Haster 81:1600369a29dd 235 virtual void socket_attach_recv(void *handle, void (*callback)(void *), void *id) = 0;
Christopher Haster 76:bbe51641f405 236 };
Christopher Haster 76:bbe51641f405 237
Christopher Haster 76:bbe51641f405 238 #endif