NetworkSocketAPI
Dependents: HelloWizFi250Interface
Fork of NetworkSocketAPI by
NetworkInterface.h@99:f51358e506c1, 2016-04-19 (annotated)
- Committer:
- Christopher Haster
- Date:
- Tue Apr 19 18:25:12 2016 -0500
- Revision:
- 99:f51358e506c1
- Parent:
- 96:656011e49d9f
- Child:
- 101:9caa935c3baa
Revised stack specific configurations
Adds the following functions for direct configuration of interface
- (set|get)stackopt
- (set|get)sockopt
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 |
96:656011e49d9f | 25 | * @enum nsapi_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 socket protocols |
Christopher Haster |
89:b1d417383c0d | 41 | * @enum protocol_t |
Christopher Haster |
89:b1d417383c0d | 42 | */ |
Christopher Haster |
89:b1d417383c0d | 43 | enum nsapi_protocol_t { |
Christopher Haster |
89:b1d417383c0d | 44 | NSAPI_TCP, /*!< Socket is of TCP type */ |
Christopher Haster |
89:b1d417383c0d | 45 | NSAPI_UDP, /*!< Socket is of UDP type */ |
Christopher Haster |
89:b1d417383c0d | 46 | }; |
Christopher Haster |
89:b1d417383c0d | 47 | |
Christopher Haster |
94:644df37bb05b | 48 | /** Maximum size of MAC address representation |
Christopher Haster |
94:644df37bb05b | 49 | */ |
Christopher Haster |
94:644df37bb05b | 50 | #define NSAPI_MAC_SIZE 18 |
Christopher Haster |
94:644df37bb05b | 51 | |
Christopher Haster |
94:644df37bb05b | 52 | /** Maximum number of bytes for MAC address |
Christopher Haster |
94:644df37bb05b | 53 | */ |
Christopher Haster |
94:644df37bb05b | 54 | #define NSAPI_MAC_BYTES 6 |
Christopher Haster |
94:644df37bb05b | 55 | |
Christopher Haster |
94:644df37bb05b | 56 | |
Christopher Haster |
89:b1d417383c0d | 57 | /** NetworkInterface class |
Christopher Haster |
89:b1d417383c0d | 58 | * Common interface that is shared between all hardware that |
Christopher Haster |
89:b1d417383c0d | 59 | * can connect to a network over IP. |
Christopher Haster |
89:b1d417383c0d | 60 | */ |
Christopher Haster |
89:b1d417383c0d | 61 | class NetworkInterface |
Christopher Haster |
89:b1d417383c0d | 62 | { |
Christopher Haster |
89:b1d417383c0d | 63 | public: |
Christopher Haster |
89:b1d417383c0d | 64 | virtual ~NetworkInterface() {}; |
Christopher Haster |
89:b1d417383c0d | 65 | |
Christopher Haster |
89:b1d417383c0d | 66 | /** Get the internally stored IP address |
Christopher Haster |
89:b1d417383c0d | 67 | * @return IP address of the interface or null if not yet connected |
Christopher Haster |
89:b1d417383c0d | 68 | */ |
Christopher Haster |
89:b1d417383c0d | 69 | virtual const char *get_ip_address() = 0; |
Christopher Haster |
89:b1d417383c0d | 70 | |
Christopher Haster |
89:b1d417383c0d | 71 | /** Get the internally stored MAC address |
Christopher Haster |
89:b1d417383c0d | 72 | * @return MAC address of the interface |
Christopher Haster |
89:b1d417383c0d | 73 | */ |
Christopher Haster |
89:b1d417383c0d | 74 | virtual const char *get_mac_address() = 0; |
Christopher Haster |
89:b1d417383c0d | 75 | |
Christopher Haster |
89:b1d417383c0d | 76 | /** Get the current status of the interface |
Christopher Haster |
89:b1d417383c0d | 77 | * @return true if connected |
Christopher Haster |
89:b1d417383c0d | 78 | */ |
Christopher Haster |
89:b1d417383c0d | 79 | virtual bool is_connected() { |
Christopher Haster |
89:b1d417383c0d | 80 | return get_ip_address() != NULL; |
Christopher Haster |
89:b1d417383c0d | 81 | } |
Christopher Haster |
89:b1d417383c0d | 82 | |
Christopher Haster |
89:b1d417383c0d | 83 | /** Looks up the specified host's IP address |
Christopher Haster |
95:b3c679f20d13 | 84 | * @param address Destination for the host SocketAddress |
Christopher Haster |
89:b1d417383c0d | 85 | * @param name Hostname to lookup |
Christopher Haster |
89:b1d417383c0d | 86 | * @return 0 on success, negative on failure |
Christopher Haster |
89:b1d417383c0d | 87 | */ |
Christopher Haster |
95:b3c679f20d13 | 88 | virtual int gethostbyname(SocketAddress *address, const char *name); |
Christopher Haster |
89:b1d417383c0d | 89 | |
Christopher Haster |
99:f51358e506c1 | 90 | /* Set stack options |
Christopher Haster |
99:f51358e506c1 | 91 | * @param level Option level |
Christopher Haster |
99:f51358e506c1 | 92 | * @param optname Option identifier |
Christopher Haster |
99:f51358e506c1 | 93 | * @param optval Option value |
Christopher Haster |
99:f51358e506c1 | 94 | * @param optlen Length of the option value |
Christopher Haster |
99:f51358e506c1 | 95 | * @return 0 on success, negative on failure |
Christopher Haster |
99:f51358e506c1 | 96 | */ |
Christopher Haster |
99:f51358e506c1 | 97 | virtual int setstackopt(int level, int optname, const void *optval, unsigned optlen); |
Christopher Haster |
99:f51358e506c1 | 98 | |
Christopher Haster |
99:f51358e506c1 | 99 | /* Get stack options |
Christopher Haster |
99:f51358e506c1 | 100 | * @param level Option level |
Christopher Haster |
99:f51358e506c1 | 101 | * @param optname Option identifier |
Christopher Haster |
99:f51358e506c1 | 102 | * @param optval Buffer where to write option value |
Christopher Haster |
99:f51358e506c1 | 103 | * @param optlen Length of the option value |
Christopher Haster |
99:f51358e506c1 | 104 | * @return 0 on success, negative on failure |
Christopher Haster |
99:f51358e506c1 | 105 | */ |
Christopher Haster |
99:f51358e506c1 | 106 | virtual int getstackopt(int level, int optname, void *optval, unsigned *optlen); |
Christopher Haster |
99:f51358e506c1 | 107 | |
Christopher Haster |
89:b1d417383c0d | 108 | protected: |
Christopher Haster |
89:b1d417383c0d | 109 | friend class Socket; |
Christopher Haster |
89:b1d417383c0d | 110 | friend class UDPSocket; |
Christopher Haster |
89:b1d417383c0d | 111 | friend class TCPSocket; |
Christopher Haster |
89:b1d417383c0d | 112 | friend class TCPServer; |
Christopher Haster |
89:b1d417383c0d | 113 | |
Christopher Haster |
93:65a9f84862f0 | 114 | /** Open a socket |
Christopher Haster |
93:65a9f84862f0 | 115 | * @param handle Handle in which to store new socket |
Christopher Haster |
93:65a9f84862f0 | 116 | * @param proto Type of socket to open, NSAPI_TCP or NSAPI_UDP |
Christopher Haster |
93:65a9f84862f0 | 117 | * @return 0 on success, negative on failure |
Christopher Haster |
89:b1d417383c0d | 118 | */ |
Christopher Haster |
93:65a9f84862f0 | 119 | virtual int socket_open(void **handle, nsapi_protocol_t proto) = 0; |
Christopher Haster |
89:b1d417383c0d | 120 | |
Christopher Haster |
93:65a9f84862f0 | 121 | /** Close the socket |
Christopher Haster |
93:65a9f84862f0 | 122 | * @param handle Socket handle |
Christopher Haster |
93:65a9f84862f0 | 123 | * @return 0 on success, negative on failure |
Christopher Haster |
93:65a9f84862f0 | 124 | * @note On failure, any memory associated with the socket must still |
Christopher Haster |
93:65a9f84862f0 | 125 | * be cleaned up |
Christopher Haster |
89:b1d417383c0d | 126 | */ |
Christopher Haster |
93:65a9f84862f0 | 127 | virtual int socket_close(void *handle) = 0; |
Christopher Haster |
89:b1d417383c0d | 128 | |
Christopher Haster |
89:b1d417383c0d | 129 | /** Bind a server socket to a specific port |
Christopher Haster |
89:b1d417383c0d | 130 | * @param handle Socket handle |
Christopher Haster |
96:656011e49d9f | 131 | * @param address Local address to listen for incoming connections on |
Christopher Haster |
89:b1d417383c0d | 132 | * @return 0 on success, negative on failure. |
Christopher Haster |
89:b1d417383c0d | 133 | */ |
Christopher Haster |
96:656011e49d9f | 134 | virtual int socket_bind(void *handle, const SocketAddress &address) = 0; |
Christopher Haster |
89:b1d417383c0d | 135 | |
Christopher Haster |
89:b1d417383c0d | 136 | /** Start listening for incoming connections |
Christopher Haster |
89:b1d417383c0d | 137 | * @param handle Socket handle |
Christopher Haster |
89:b1d417383c0d | 138 | * @param backlog Number of pending connections that can be queued up at any |
Christopher Haster |
89:b1d417383c0d | 139 | * one time [Default: 1] |
Christopher Haster |
89:b1d417383c0d | 140 | * @return 0 on success, negative on failure |
Christopher Haster |
89:b1d417383c0d | 141 | */ |
Christopher Haster |
89:b1d417383c0d | 142 | virtual int socket_listen(void *handle, int backlog) = 0; |
Christopher Haster |
89:b1d417383c0d | 143 | |
Christopher Haster |
89:b1d417383c0d | 144 | /** Connects this TCP socket to the server |
Christopher Haster |
89:b1d417383c0d | 145 | * @param handle Socket handle |
Christopher Haster |
89:b1d417383c0d | 146 | * @param address SocketAddress to connect to |
Christopher Haster |
89:b1d417383c0d | 147 | * @return 0 on success, negative on failure |
Christopher Haster |
89:b1d417383c0d | 148 | */ |
Christopher Haster |
89:b1d417383c0d | 149 | virtual int socket_connect(void *handle, const SocketAddress &address) = 0; |
Christopher Haster |
89:b1d417383c0d | 150 | |
Christopher Haster |
89:b1d417383c0d | 151 | /** Check if the socket is connected |
Christopher Haster |
89:b1d417383c0d | 152 | * @param handle Socket handle |
Christopher Haster |
89:b1d417383c0d | 153 | * @return true if connected, false otherwise |
Christopher Haster |
89:b1d417383c0d | 154 | */ |
Christopher Haster |
89:b1d417383c0d | 155 | virtual bool socket_is_connected(void *handle) = 0; |
Christopher Haster |
89:b1d417383c0d | 156 | |
Christopher Haster |
89:b1d417383c0d | 157 | /** Accept a new connection. |
Christopher Haster |
93:65a9f84862f0 | 158 | * @param handle Handle in which to store new socket |
Christopher Haster |
93:65a9f84862f0 | 159 | * @param server Socket handle to server to accept from |
Christopher Haster |
93:65a9f84862f0 | 160 | * @return 0 on success, negative on failure |
Christopher Haster |
89:b1d417383c0d | 161 | * @note This call is not-blocking, if this call would block, must |
Christopher Haster |
89:b1d417383c0d | 162 | * immediately return NSAPI_ERROR_WOULD_WAIT |
Christopher Haster |
89:b1d417383c0d | 163 | */ |
Christopher Haster |
93:65a9f84862f0 | 164 | virtual int socket_accept(void **handle, void *server) = 0; |
Christopher Haster |
89:b1d417383c0d | 165 | |
Christopher Haster |
89:b1d417383c0d | 166 | /** Send data to the remote host |
Christopher Haster |
89:b1d417383c0d | 167 | * @param handle Socket handle |
Christopher Haster |
89:b1d417383c0d | 168 | * @param data The buffer to send to the host |
Christopher Haster |
89:b1d417383c0d | 169 | * @param size The length of the buffer to send |
Christopher Haster |
89:b1d417383c0d | 170 | * @return Number of written bytes on success, negative on failure |
Christopher Haster |
89:b1d417383c0d | 171 | * @note This call is not-blocking, if this call would block, must |
Christopher Haster |
89:b1d417383c0d | 172 | * immediately return NSAPI_ERROR_WOULD_WAIT |
Christopher Haster |
89:b1d417383c0d | 173 | */ |
Christopher Haster |
89:b1d417383c0d | 174 | virtual int socket_send(void *handle, const void *data, unsigned size) = 0; |
Christopher Haster |
89:b1d417383c0d | 175 | |
Christopher Haster |
89:b1d417383c0d | 176 | /** Receive data from the remote host |
Christopher Haster |
89:b1d417383c0d | 177 | * @param handle Socket handle |
Christopher Haster |
89:b1d417383c0d | 178 | * @param data The buffer in which to store the data received from the host |
Christopher Haster |
89:b1d417383c0d | 179 | * @param size The maximum length of the buffer |
Christopher Haster |
89:b1d417383c0d | 180 | * @return Number of received bytes on success, negative on failure |
Christopher Haster |
89:b1d417383c0d | 181 | * @note This call is not-blocking, if this call would block, must |
Christopher Haster |
89:b1d417383c0d | 182 | * immediately return NSAPI_ERROR_WOULD_WAIT |
Christopher Haster |
89:b1d417383c0d | 183 | */ |
Christopher Haster |
89:b1d417383c0d | 184 | virtual int socket_recv(void *handle, void *data, unsigned size) = 0; |
Christopher Haster |
89:b1d417383c0d | 185 | |
Christopher Haster |
89:b1d417383c0d | 186 | /** Send a packet to a remote endpoint |
Christopher Haster |
89:b1d417383c0d | 187 | * @param handle Socket handle |
Christopher Haster |
89:b1d417383c0d | 188 | * @param address The remote SocketAddress |
Christopher Haster |
89:b1d417383c0d | 189 | * @param data The packet to be sent |
Christopher Haster |
89:b1d417383c0d | 190 | * @param size The length of the packet to be sent |
Christopher Haster |
89:b1d417383c0d | 191 | * @return the number of written bytes on success, negative on failure |
Christopher Haster |
89:b1d417383c0d | 192 | * @note This call is not-blocking, if this call would block, must |
Christopher Haster |
89:b1d417383c0d | 193 | * immediately return NSAPI_ERROR_WOULD_WAIT |
Christopher Haster |
89:b1d417383c0d | 194 | */ |
Christopher Haster |
89:b1d417383c0d | 195 | virtual int socket_sendto(void *handle, const SocketAddress &address, const void *data, unsigned size) = 0; |
Christopher Haster |
89:b1d417383c0d | 196 | |
Christopher Haster |
89:b1d417383c0d | 197 | /** Receive a packet from a remote endpoint |
Christopher Haster |
89:b1d417383c0d | 198 | * @param handle Socket handle |
Christopher Haster |
89:b1d417383c0d | 199 | * @param address Destination for the remote SocketAddress or null |
Christopher Haster |
89:b1d417383c0d | 200 | * @param buffer The buffer for storing the incoming packet data |
Christopher Haster |
89:b1d417383c0d | 201 | * If a packet is too long to fit in the supplied buffer, |
Christopher Haster |
89:b1d417383c0d | 202 | * excess bytes are discarded |
Christopher Haster |
89:b1d417383c0d | 203 | * @param size The length of the buffer |
Christopher Haster |
89:b1d417383c0d | 204 | * @return the number of received bytes on success, negative on failure |
Christopher Haster |
89:b1d417383c0d | 205 | * @note This call is not-blocking, if this call would block, must |
Christopher Haster |
89:b1d417383c0d | 206 | * immediately return NSAPI_ERROR_WOULD_WAIT |
Christopher Haster |
89:b1d417383c0d | 207 | */ |
Christopher Haster |
89:b1d417383c0d | 208 | virtual int socket_recvfrom(void *handle, SocketAddress *address, void *buffer, unsigned size) = 0; |
Christopher Haster |
89:b1d417383c0d | 209 | |
Christopher Haster |
92:dd5f19874adf | 210 | /** Register a callback on state change of the socket |
Christopher Haster |
89:b1d417383c0d | 211 | * @param handle Socket handle |
Christopher Haster |
92:dd5f19874adf | 212 | * @param callback Function to call on state change |
Christopher Haster |
92:dd5f19874adf | 213 | * @param data Argument to pass to callback |
Christopher Haster |
92:dd5f19874adf | 214 | * @note Callback may be called in an interrupt context. |
Christopher Haster |
89:b1d417383c0d | 215 | */ |
Christopher Haster |
92:dd5f19874adf | 216 | virtual void socket_attach(void *handle, void (*callback)(void *), void *data) = 0; |
Christopher Haster |
99:f51358e506c1 | 217 | |
Christopher Haster |
99:f51358e506c1 | 218 | /* Set socket options |
Christopher Haster |
99:f51358e506c1 | 219 | * @param handle Socket handle |
Christopher Haster |
99:f51358e506c1 | 220 | * @param level Option level |
Christopher Haster |
99:f51358e506c1 | 221 | * @param optname Option identifier |
Christopher Haster |
99:f51358e506c1 | 222 | * @param optval Option value |
Christopher Haster |
99:f51358e506c1 | 223 | * @param optlen Length of the option value |
Christopher Haster |
99:f51358e506c1 | 224 | * @return 0 on success, negative on failure |
Christopher Haster |
99:f51358e506c1 | 225 | */ |
Christopher Haster |
99:f51358e506c1 | 226 | virtual int setsockopt(void *handle, int level, int optname, const void *optval, unsigned optlen); |
Christopher Haster |
99:f51358e506c1 | 227 | |
Christopher Haster |
99:f51358e506c1 | 228 | /* Get socket options |
Christopher Haster |
99:f51358e506c1 | 229 | * @param handle Socket handle |
Christopher Haster |
99:f51358e506c1 | 230 | * @param level Option level |
Christopher Haster |
99:f51358e506c1 | 231 | * @param optname Option identifier |
Christopher Haster |
99:f51358e506c1 | 232 | * @param optval Buffer where to write option value |
Christopher Haster |
99:f51358e506c1 | 233 | * @param optlen Length of the option value |
Christopher Haster |
99:f51358e506c1 | 234 | * @return 0 on success, negative on failure |
Christopher Haster |
99:f51358e506c1 | 235 | */ |
Christopher Haster |
99:f51358e506c1 | 236 | virtual int getsockopt(void *handle, int level, int optname, void *optval, unsigned *optlen); |
Christopher Haster |
89:b1d417383c0d | 237 | }; |
Christopher Haster |
89:b1d417383c0d | 238 | |
Christopher Haster |
89:b1d417383c0d | 239 | #endif |