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