![](/media/cache/profiles/5f55d0baa59f4bc1dc393149183f1492.jpg.50x50_q85.jpg)
wifi test
Dependencies: X_NUCLEO_IKS01A2 mbed-http
easy-connect/esp8266-driver/ESP8266Interface.h@0:24d3eb812fd4, 2018-09-05 (annotated)
- Committer:
- JMF
- Date:
- Wed Sep 05 14:28:24 2018 +0000
- Revision:
- 0:24d3eb812fd4
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JMF | 0:24d3eb812fd4 | 1 | /* ESP8266 implementation of NetworkInterfaceAPI |
JMF | 0:24d3eb812fd4 | 2 | * Copyright (c) 2015 ARM Limited |
JMF | 0:24d3eb812fd4 | 3 | * |
JMF | 0:24d3eb812fd4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
JMF | 0:24d3eb812fd4 | 5 | * you may not use this file except in compliance with the License. |
JMF | 0:24d3eb812fd4 | 6 | * You may obtain a copy of the License at |
JMF | 0:24d3eb812fd4 | 7 | * |
JMF | 0:24d3eb812fd4 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
JMF | 0:24d3eb812fd4 | 9 | * |
JMF | 0:24d3eb812fd4 | 10 | * Unless required by applicable law or agreed to in writing, software |
JMF | 0:24d3eb812fd4 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
JMF | 0:24d3eb812fd4 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
JMF | 0:24d3eb812fd4 | 13 | * See the License for the specific language governing permissions and |
JMF | 0:24d3eb812fd4 | 14 | * limitations under the License. |
JMF | 0:24d3eb812fd4 | 15 | */ |
JMF | 0:24d3eb812fd4 | 16 | |
JMF | 0:24d3eb812fd4 | 17 | #ifndef ESP8266_INTERFACE_H |
JMF | 0:24d3eb812fd4 | 18 | #define ESP8266_INTERFACE_H |
JMF | 0:24d3eb812fd4 | 19 | |
JMF | 0:24d3eb812fd4 | 20 | #include "mbed.h" |
JMF | 0:24d3eb812fd4 | 21 | #include "ESP8266.h" |
JMF | 0:24d3eb812fd4 | 22 | |
JMF | 0:24d3eb812fd4 | 23 | |
JMF | 0:24d3eb812fd4 | 24 | #define ESP8266_SOCKET_COUNT 5 |
JMF | 0:24d3eb812fd4 | 25 | |
JMF | 0:24d3eb812fd4 | 26 | /** ESP8266Interface class |
JMF | 0:24d3eb812fd4 | 27 | * Implementation of the NetworkStack for the ESP8266 |
JMF | 0:24d3eb812fd4 | 28 | */ |
JMF | 0:24d3eb812fd4 | 29 | class ESP8266Interface : public NetworkStack, public WiFiInterface |
JMF | 0:24d3eb812fd4 | 30 | { |
JMF | 0:24d3eb812fd4 | 31 | public: |
JMF | 0:24d3eb812fd4 | 32 | /** |
JMF | 0:24d3eb812fd4 | 33 | * @brief ESP8266Interface default constructor |
JMF | 0:24d3eb812fd4 | 34 | * Will use values defined in mbed_lib.json |
JMF | 0:24d3eb812fd4 | 35 | */ |
JMF | 0:24d3eb812fd4 | 36 | ESP8266Interface(); |
JMF | 0:24d3eb812fd4 | 37 | |
JMF | 0:24d3eb812fd4 | 38 | /** ESP8266Interface lifetime |
JMF | 0:24d3eb812fd4 | 39 | * @param tx TX pin |
JMF | 0:24d3eb812fd4 | 40 | * @param rx RX pin |
JMF | 0:24d3eb812fd4 | 41 | * @param debug Enable debugging |
JMF | 0:24d3eb812fd4 | 42 | */ |
JMF | 0:24d3eb812fd4 | 43 | ESP8266Interface(PinName tx, PinName rx, bool debug = false); |
JMF | 0:24d3eb812fd4 | 44 | |
JMF | 0:24d3eb812fd4 | 45 | /** Start the interface |
JMF | 0:24d3eb812fd4 | 46 | * |
JMF | 0:24d3eb812fd4 | 47 | * Attempts to connect to a WiFi network. Requires ssid and passphrase to be set. |
JMF | 0:24d3eb812fd4 | 48 | * If passphrase is invalid, NSAPI_ERROR_AUTH_ERROR is returned. |
JMF | 0:24d3eb812fd4 | 49 | * |
JMF | 0:24d3eb812fd4 | 50 | * @return 0 on success, negative error code on failure |
JMF | 0:24d3eb812fd4 | 51 | */ |
JMF | 0:24d3eb812fd4 | 52 | virtual int connect(); |
JMF | 0:24d3eb812fd4 | 53 | |
JMF | 0:24d3eb812fd4 | 54 | /** Start the interface |
JMF | 0:24d3eb812fd4 | 55 | * |
JMF | 0:24d3eb812fd4 | 56 | * Attempts to connect to a WiFi network. |
JMF | 0:24d3eb812fd4 | 57 | * |
JMF | 0:24d3eb812fd4 | 58 | * @param ssid Name of the network to connect to |
JMF | 0:24d3eb812fd4 | 59 | * @param pass Security passphrase to connect to the network |
JMF | 0:24d3eb812fd4 | 60 | * @param security Type of encryption for connection (Default: NSAPI_SECURITY_NONE) |
JMF | 0:24d3eb812fd4 | 61 | * @param channel This parameter is not supported, setting it to anything else than 0 will result in NSAPI_ERROR_UNSUPPORTED |
JMF | 0:24d3eb812fd4 | 62 | * @return 0 on success, or error code on failure |
JMF | 0:24d3eb812fd4 | 63 | */ |
JMF | 0:24d3eb812fd4 | 64 | virtual int connect(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE, |
JMF | 0:24d3eb812fd4 | 65 | uint8_t channel = 0); |
JMF | 0:24d3eb812fd4 | 66 | |
JMF | 0:24d3eb812fd4 | 67 | /** Set the WiFi network credentials |
JMF | 0:24d3eb812fd4 | 68 | * |
JMF | 0:24d3eb812fd4 | 69 | * @param ssid Name of the network to connect to |
JMF | 0:24d3eb812fd4 | 70 | * @param pass Security passphrase to connect to the network |
JMF | 0:24d3eb812fd4 | 71 | * @param security Type of encryption for connection |
JMF | 0:24d3eb812fd4 | 72 | * (defaults to NSAPI_SECURITY_NONE) |
JMF | 0:24d3eb812fd4 | 73 | * @return 0 on success, or error code on failure |
JMF | 0:24d3eb812fd4 | 74 | */ |
JMF | 0:24d3eb812fd4 | 75 | virtual int set_credentials(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE); |
JMF | 0:24d3eb812fd4 | 76 | |
JMF | 0:24d3eb812fd4 | 77 | /** Set the WiFi network channel - NOT SUPPORTED |
JMF | 0:24d3eb812fd4 | 78 | * |
JMF | 0:24d3eb812fd4 | 79 | * This function is not supported and will return NSAPI_ERROR_UNSUPPORTED |
JMF | 0:24d3eb812fd4 | 80 | * |
JMF | 0:24d3eb812fd4 | 81 | * @param channel Channel on which the connection is to be made, or 0 for any (Default: 0) |
JMF | 0:24d3eb812fd4 | 82 | * @return Not supported, returns NSAPI_ERROR_UNSUPPORTED |
JMF | 0:24d3eb812fd4 | 83 | */ |
JMF | 0:24d3eb812fd4 | 84 | virtual int set_channel(uint8_t channel); |
JMF | 0:24d3eb812fd4 | 85 | |
JMF | 0:24d3eb812fd4 | 86 | /** Stop the interface |
JMF | 0:24d3eb812fd4 | 87 | * @return 0 on success, negative on failure |
JMF | 0:24d3eb812fd4 | 88 | */ |
JMF | 0:24d3eb812fd4 | 89 | virtual int disconnect(); |
JMF | 0:24d3eb812fd4 | 90 | |
JMF | 0:24d3eb812fd4 | 91 | /** Get the internally stored IP address |
JMF | 0:24d3eb812fd4 | 92 | * @return IP address of the interface or null if not yet connected |
JMF | 0:24d3eb812fd4 | 93 | */ |
JMF | 0:24d3eb812fd4 | 94 | virtual const char *get_ip_address(); |
JMF | 0:24d3eb812fd4 | 95 | |
JMF | 0:24d3eb812fd4 | 96 | /** Get the internally stored MAC address |
JMF | 0:24d3eb812fd4 | 97 | * @return MAC address of the interface |
JMF | 0:24d3eb812fd4 | 98 | */ |
JMF | 0:24d3eb812fd4 | 99 | virtual const char *get_mac_address(); |
JMF | 0:24d3eb812fd4 | 100 | |
JMF | 0:24d3eb812fd4 | 101 | /** Get the local gateway |
JMF | 0:24d3eb812fd4 | 102 | * |
JMF | 0:24d3eb812fd4 | 103 | * @return Null-terminated representation of the local gateway |
JMF | 0:24d3eb812fd4 | 104 | * or null if no network mask has been recieved |
JMF | 0:24d3eb812fd4 | 105 | */ |
JMF | 0:24d3eb812fd4 | 106 | virtual const char *get_gateway(); |
JMF | 0:24d3eb812fd4 | 107 | |
JMF | 0:24d3eb812fd4 | 108 | /** Get the local network mask |
JMF | 0:24d3eb812fd4 | 109 | * |
JMF | 0:24d3eb812fd4 | 110 | * @return Null-terminated representation of the local network mask |
JMF | 0:24d3eb812fd4 | 111 | * or null if no network mask has been recieved |
JMF | 0:24d3eb812fd4 | 112 | */ |
JMF | 0:24d3eb812fd4 | 113 | virtual const char *get_netmask(); |
JMF | 0:24d3eb812fd4 | 114 | |
JMF | 0:24d3eb812fd4 | 115 | /** Gets the current radio signal strength for active connection |
JMF | 0:24d3eb812fd4 | 116 | * |
JMF | 0:24d3eb812fd4 | 117 | * @return Connection strength in dBm (negative value) |
JMF | 0:24d3eb812fd4 | 118 | */ |
JMF | 0:24d3eb812fd4 | 119 | virtual int8_t get_rssi(); |
JMF | 0:24d3eb812fd4 | 120 | |
JMF | 0:24d3eb812fd4 | 121 | /** Scan for available networks |
JMF | 0:24d3eb812fd4 | 122 | * |
JMF | 0:24d3eb812fd4 | 123 | * This function will block. |
JMF | 0:24d3eb812fd4 | 124 | * |
JMF | 0:24d3eb812fd4 | 125 | * @param ap Pointer to allocated array to store discovered AP |
JMF | 0:24d3eb812fd4 | 126 | * @param count Size of allocated @a res array, or 0 to only count available AP |
JMF | 0:24d3eb812fd4 | 127 | * @param timeout Timeout in milliseconds; 0 for no timeout (Default: 0) |
JMF | 0:24d3eb812fd4 | 128 | * @return Number of entries in @a, or if @a count was 0 number of available networks, negative on error |
JMF | 0:24d3eb812fd4 | 129 | * see @a nsapi_error |
JMF | 0:24d3eb812fd4 | 130 | */ |
JMF | 0:24d3eb812fd4 | 131 | virtual int scan(WiFiAccessPoint *res, unsigned count); |
JMF | 0:24d3eb812fd4 | 132 | |
JMF | 0:24d3eb812fd4 | 133 | /** Translates a hostname to an IP address with specific version |
JMF | 0:24d3eb812fd4 | 134 | * |
JMF | 0:24d3eb812fd4 | 135 | * The hostname may be either a domain name or an IP address. If the |
JMF | 0:24d3eb812fd4 | 136 | * hostname is an IP address, no network transactions will be performed. |
JMF | 0:24d3eb812fd4 | 137 | * |
JMF | 0:24d3eb812fd4 | 138 | * If no stack-specific DNS resolution is provided, the hostname |
JMF | 0:24d3eb812fd4 | 139 | * will be resolve using a UDP socket on the stack. |
JMF | 0:24d3eb812fd4 | 140 | * |
JMF | 0:24d3eb812fd4 | 141 | * @param address Destination for the host SocketAddress |
JMF | 0:24d3eb812fd4 | 142 | * @param host Hostname to resolve |
JMF | 0:24d3eb812fd4 | 143 | * @param version IP version of address to resolve, NSAPI_UNSPEC indicates |
JMF | 0:24d3eb812fd4 | 144 | * version is chosen by the stack (defaults to NSAPI_UNSPEC) |
JMF | 0:24d3eb812fd4 | 145 | * @return 0 on success, negative error code on failure |
JMF | 0:24d3eb812fd4 | 146 | */ |
JMF | 0:24d3eb812fd4 | 147 | using NetworkInterface::gethostbyname; |
JMF | 0:24d3eb812fd4 | 148 | |
JMF | 0:24d3eb812fd4 | 149 | /** Add a domain name server to list of servers to query |
JMF | 0:24d3eb812fd4 | 150 | * |
JMF | 0:24d3eb812fd4 | 151 | * @param addr Destination for the host address |
JMF | 0:24d3eb812fd4 | 152 | * @return 0 on success, negative error code on failure |
JMF | 0:24d3eb812fd4 | 153 | */ |
JMF | 0:24d3eb812fd4 | 154 | using NetworkInterface::add_dns_server; |
JMF | 0:24d3eb812fd4 | 155 | |
JMF | 0:24d3eb812fd4 | 156 | /** Set socket options |
JMF | 0:24d3eb812fd4 | 157 | * |
JMF | 0:24d3eb812fd4 | 158 | * The setsockopt allow an application to pass stack-specific hints |
JMF | 0:24d3eb812fd4 | 159 | * to the underlying stack. For unsupported options, |
JMF | 0:24d3eb812fd4 | 160 | * NSAPI_ERROR_UNSUPPORTED is returned and the socket is unmodified. |
JMF | 0:24d3eb812fd4 | 161 | * |
JMF | 0:24d3eb812fd4 | 162 | * @param handle Socket handle |
JMF | 0:24d3eb812fd4 | 163 | * @param level Stack-specific protocol level |
JMF | 0:24d3eb812fd4 | 164 | * @param optname Stack-specific option identifier |
JMF | 0:24d3eb812fd4 | 165 | * @param optval Option value |
JMF | 0:24d3eb812fd4 | 166 | * @param optlen Length of the option value |
JMF | 0:24d3eb812fd4 | 167 | * @return 0 on success, negative error code on failure |
JMF | 0:24d3eb812fd4 | 168 | */ |
JMF | 0:24d3eb812fd4 | 169 | virtual nsapi_error_t setsockopt(nsapi_socket_t handle, int level, |
JMF | 0:24d3eb812fd4 | 170 | int optname, const void *optval, unsigned optlen); |
JMF | 0:24d3eb812fd4 | 171 | |
JMF | 0:24d3eb812fd4 | 172 | /** Get socket options |
JMF | 0:24d3eb812fd4 | 173 | * |
JMF | 0:24d3eb812fd4 | 174 | * getsockopt allows an application to retrieve stack-specific options |
JMF | 0:24d3eb812fd4 | 175 | * from the underlying stack using stack-specific level and option names, |
JMF | 0:24d3eb812fd4 | 176 | * or to request generic options using levels from nsapi_socket_level_t. |
JMF | 0:24d3eb812fd4 | 177 | * |
JMF | 0:24d3eb812fd4 | 178 | * For unsupported options, NSAPI_ERROR_UNSUPPORTED is returned |
JMF | 0:24d3eb812fd4 | 179 | * and the socket is unmodified. |
JMF | 0:24d3eb812fd4 | 180 | * |
JMF | 0:24d3eb812fd4 | 181 | * @param level Stack-specific protocol level or nsapi_socket_level_t |
JMF | 0:24d3eb812fd4 | 182 | * @param optname Level-specific option name |
JMF | 0:24d3eb812fd4 | 183 | * @param optval Destination for option value |
JMF | 0:24d3eb812fd4 | 184 | * @param optlen Length of the option value |
JMF | 0:24d3eb812fd4 | 185 | * @return 0 on success, negative error code on failure |
JMF | 0:24d3eb812fd4 | 186 | */ |
JMF | 0:24d3eb812fd4 | 187 | virtual nsapi_error_t getsockopt(nsapi_socket_t handle, int level, int optname, |
JMF | 0:24d3eb812fd4 | 188 | void *optval, unsigned *optlen); |
JMF | 0:24d3eb812fd4 | 189 | |
JMF | 0:24d3eb812fd4 | 190 | /** Register callback for status reporting |
JMF | 0:24d3eb812fd4 | 191 | * |
JMF | 0:24d3eb812fd4 | 192 | * The specified status callback function will be called on status changes |
JMF | 0:24d3eb812fd4 | 193 | * on the network. The parameters on the callback are the event type and |
JMF | 0:24d3eb812fd4 | 194 | * event-type dependent reason parameter. |
JMF | 0:24d3eb812fd4 | 195 | * |
JMF | 0:24d3eb812fd4 | 196 | * In ESP8266 the callback will be called when processing OOB-messages via |
JMF | 0:24d3eb812fd4 | 197 | * AT-parser. Do NOT call any ESP8266Interface -functions or do extensive |
JMF | 0:24d3eb812fd4 | 198 | * processing in the callback. |
JMF | 0:24d3eb812fd4 | 199 | * |
JMF | 0:24d3eb812fd4 | 200 | * @param status_cb The callback for status changes |
JMF | 0:24d3eb812fd4 | 201 | */ |
JMF | 0:24d3eb812fd4 | 202 | virtual void attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb); |
JMF | 0:24d3eb812fd4 | 203 | |
JMF | 0:24d3eb812fd4 | 204 | /** Get the connection status |
JMF | 0:24d3eb812fd4 | 205 | * |
JMF | 0:24d3eb812fd4 | 206 | * @return The connection status according to ConnectionStatusType |
JMF | 0:24d3eb812fd4 | 207 | */ |
JMF | 0:24d3eb812fd4 | 208 | virtual nsapi_connection_status_t get_connection_status() const; |
JMF | 0:24d3eb812fd4 | 209 | |
JMF | 0:24d3eb812fd4 | 210 | protected: |
JMF | 0:24d3eb812fd4 | 211 | /** Open a socket |
JMF | 0:24d3eb812fd4 | 212 | * @param handle Handle in which to store new socket |
JMF | 0:24d3eb812fd4 | 213 | * @param proto Type of socket to open, NSAPI_TCP or NSAPI_UDP |
JMF | 0:24d3eb812fd4 | 214 | * @return 0 on success, negative on failure |
JMF | 0:24d3eb812fd4 | 215 | */ |
JMF | 0:24d3eb812fd4 | 216 | virtual int socket_open(void **handle, nsapi_protocol_t proto); |
JMF | 0:24d3eb812fd4 | 217 | |
JMF | 0:24d3eb812fd4 | 218 | /** Close the socket |
JMF | 0:24d3eb812fd4 | 219 | * @param handle Socket handle |
JMF | 0:24d3eb812fd4 | 220 | * @return 0 on success, negative on failure |
JMF | 0:24d3eb812fd4 | 221 | * @note On failure, any memory associated with the socket must still |
JMF | 0:24d3eb812fd4 | 222 | * be cleaned up |
JMF | 0:24d3eb812fd4 | 223 | */ |
JMF | 0:24d3eb812fd4 | 224 | virtual int socket_close(void *handle); |
JMF | 0:24d3eb812fd4 | 225 | |
JMF | 0:24d3eb812fd4 | 226 | /** Bind a server socket to a specific port |
JMF | 0:24d3eb812fd4 | 227 | * @param handle Socket handle |
JMF | 0:24d3eb812fd4 | 228 | * @param address Local address to listen for incoming connections on |
JMF | 0:24d3eb812fd4 | 229 | * @return 0 on success, negative on failure. |
JMF | 0:24d3eb812fd4 | 230 | */ |
JMF | 0:24d3eb812fd4 | 231 | virtual int socket_bind(void *handle, const SocketAddress &address); |
JMF | 0:24d3eb812fd4 | 232 | |
JMF | 0:24d3eb812fd4 | 233 | /** Start listening for incoming connections |
JMF | 0:24d3eb812fd4 | 234 | * @param handle Socket handle |
JMF | 0:24d3eb812fd4 | 235 | * @param backlog Number of pending connections that can be queued up at any |
JMF | 0:24d3eb812fd4 | 236 | * one time [Default: 1] |
JMF | 0:24d3eb812fd4 | 237 | * @return 0 on success, negative on failure |
JMF | 0:24d3eb812fd4 | 238 | */ |
JMF | 0:24d3eb812fd4 | 239 | virtual int socket_listen(void *handle, int backlog); |
JMF | 0:24d3eb812fd4 | 240 | |
JMF | 0:24d3eb812fd4 | 241 | /** Connects this TCP socket to the server |
JMF | 0:24d3eb812fd4 | 242 | * @param handle Socket handle |
JMF | 0:24d3eb812fd4 | 243 | * @param address SocketAddress to connect to |
JMF | 0:24d3eb812fd4 | 244 | * @return 0 on success, negative on failure |
JMF | 0:24d3eb812fd4 | 245 | */ |
JMF | 0:24d3eb812fd4 | 246 | virtual int socket_connect(void *handle, const SocketAddress &address); |
JMF | 0:24d3eb812fd4 | 247 | |
JMF | 0:24d3eb812fd4 | 248 | /** Accept a new connection. |
JMF | 0:24d3eb812fd4 | 249 | * @param handle Handle in which to store new socket |
JMF | 0:24d3eb812fd4 | 250 | * @param server Socket handle to server to accept from |
JMF | 0:24d3eb812fd4 | 251 | * @return 0 on success, negative on failure |
JMF | 0:24d3eb812fd4 | 252 | * @note This call is not-blocking, if this call would block, must |
JMF | 0:24d3eb812fd4 | 253 | * immediately return NSAPI_ERROR_WOULD_WAIT |
JMF | 0:24d3eb812fd4 | 254 | */ |
JMF | 0:24d3eb812fd4 | 255 | virtual int socket_accept(void *handle, void **socket, SocketAddress *address); |
JMF | 0:24d3eb812fd4 | 256 | |
JMF | 0:24d3eb812fd4 | 257 | /** Send data to the remote host |
JMF | 0:24d3eb812fd4 | 258 | * @param handle Socket handle |
JMF | 0:24d3eb812fd4 | 259 | * @param data The buffer to send to the host |
JMF | 0:24d3eb812fd4 | 260 | * @param size The length of the buffer to send |
JMF | 0:24d3eb812fd4 | 261 | * @return Number of written bytes on success, negative on failure |
JMF | 0:24d3eb812fd4 | 262 | * @note This call is not-blocking, if this call would block, must |
JMF | 0:24d3eb812fd4 | 263 | * immediately return NSAPI_ERROR_WOULD_WAIT |
JMF | 0:24d3eb812fd4 | 264 | */ |
JMF | 0:24d3eb812fd4 | 265 | virtual int socket_send(void *handle, const void *data, unsigned size); |
JMF | 0:24d3eb812fd4 | 266 | |
JMF | 0:24d3eb812fd4 | 267 | /** Receive data from the remote host |
JMF | 0:24d3eb812fd4 | 268 | * @param handle Socket handle |
JMF | 0:24d3eb812fd4 | 269 | * @param data The buffer in which to store the data received from the host |
JMF | 0:24d3eb812fd4 | 270 | * @param size The maximum length of the buffer |
JMF | 0:24d3eb812fd4 | 271 | * @return Number of received bytes on success, negative on failure |
JMF | 0:24d3eb812fd4 | 272 | * @note This call is not-blocking, if this call would block, must |
JMF | 0:24d3eb812fd4 | 273 | * immediately return NSAPI_ERROR_WOULD_WAIT |
JMF | 0:24d3eb812fd4 | 274 | */ |
JMF | 0:24d3eb812fd4 | 275 | virtual int socket_recv(void *handle, void *data, unsigned size); |
JMF | 0:24d3eb812fd4 | 276 | |
JMF | 0:24d3eb812fd4 | 277 | /** Send a packet to a remote endpoint |
JMF | 0:24d3eb812fd4 | 278 | * @param handle Socket handle |
JMF | 0:24d3eb812fd4 | 279 | * @param address The remote SocketAddress |
JMF | 0:24d3eb812fd4 | 280 | * @param data The packet to be sent |
JMF | 0:24d3eb812fd4 | 281 | * @param size The length of the packet to be sent |
JMF | 0:24d3eb812fd4 | 282 | * @return The number of written bytes on success, negative on failure |
JMF | 0:24d3eb812fd4 | 283 | * @note This call is not-blocking, if this call would block, must |
JMF | 0:24d3eb812fd4 | 284 | * immediately return NSAPI_ERROR_WOULD_WAIT |
JMF | 0:24d3eb812fd4 | 285 | */ |
JMF | 0:24d3eb812fd4 | 286 | virtual int socket_sendto(void *handle, const SocketAddress &address, const void *data, unsigned size); |
JMF | 0:24d3eb812fd4 | 287 | |
JMF | 0:24d3eb812fd4 | 288 | /** Receive a packet from a remote endpoint |
JMF | 0:24d3eb812fd4 | 289 | * @param handle Socket handle |
JMF | 0:24d3eb812fd4 | 290 | * @param address Destination for the remote SocketAddress or null |
JMF | 0:24d3eb812fd4 | 291 | * @param buffer The buffer for storing the incoming packet data |
JMF | 0:24d3eb812fd4 | 292 | * If a packet is too long to fit in the supplied buffer, |
JMF | 0:24d3eb812fd4 | 293 | * excess bytes are discarded |
JMF | 0:24d3eb812fd4 | 294 | * @param size The length of the buffer |
JMF | 0:24d3eb812fd4 | 295 | * @return The number of received bytes on success, negative on failure |
JMF | 0:24d3eb812fd4 | 296 | * @note This call is not-blocking, if this call would block, must |
JMF | 0:24d3eb812fd4 | 297 | * immediately return NSAPI_ERROR_WOULD_WAIT |
JMF | 0:24d3eb812fd4 | 298 | */ |
JMF | 0:24d3eb812fd4 | 299 | virtual int socket_recvfrom(void *handle, SocketAddress *address, void *buffer, unsigned size); |
JMF | 0:24d3eb812fd4 | 300 | |
JMF | 0:24d3eb812fd4 | 301 | /** Register a callback on state change of the socket |
JMF | 0:24d3eb812fd4 | 302 | * @param handle Socket handle |
JMF | 0:24d3eb812fd4 | 303 | * @param callback Function to call on state change |
JMF | 0:24d3eb812fd4 | 304 | * @param data Argument to pass to callback |
JMF | 0:24d3eb812fd4 | 305 | * @note Callback may be called in an interrupt context. |
JMF | 0:24d3eb812fd4 | 306 | */ |
JMF | 0:24d3eb812fd4 | 307 | virtual void socket_attach(void *handle, void (*callback)(void *), void *data); |
JMF | 0:24d3eb812fd4 | 308 | |
JMF | 0:24d3eb812fd4 | 309 | /** Provide access to the NetworkStack object |
JMF | 0:24d3eb812fd4 | 310 | * |
JMF | 0:24d3eb812fd4 | 311 | * @return The underlying NetworkStack object |
JMF | 0:24d3eb812fd4 | 312 | */ |
JMF | 0:24d3eb812fd4 | 313 | virtual NetworkStack *get_stack() |
JMF | 0:24d3eb812fd4 | 314 | { |
JMF | 0:24d3eb812fd4 | 315 | return this; |
JMF | 0:24d3eb812fd4 | 316 | } |
JMF | 0:24d3eb812fd4 | 317 | |
JMF | 0:24d3eb812fd4 | 318 | private: |
JMF | 0:24d3eb812fd4 | 319 | static const int ESP8266_SSID_MAX_LENGTH = 32; /* 32 is what 802.11 defines as longest possible name */ |
JMF | 0:24d3eb812fd4 | 320 | static const int ESP8266_PASSPHRASE_MAX_LENGTH = 63; /* The longest allowed passphrase */ |
JMF | 0:24d3eb812fd4 | 321 | static const int ESP8266_PASSPHRASE_MIN_LENGTH = 8; /* The shortest allowed passphrase */ |
JMF | 0:24d3eb812fd4 | 322 | |
JMF | 0:24d3eb812fd4 | 323 | ESP8266 _esp; |
JMF | 0:24d3eb812fd4 | 324 | bool _ids[ESP8266_SOCKET_COUNT]; |
JMF | 0:24d3eb812fd4 | 325 | int _initialized; |
JMF | 0:24d3eb812fd4 | 326 | int _started; |
JMF | 0:24d3eb812fd4 | 327 | |
JMF | 0:24d3eb812fd4 | 328 | char ap_ssid[ESP8266_SSID_MAX_LENGTH + 1]; /* 32 is what 802.11 defines as longest possible name; +1 for the \0 */ |
JMF | 0:24d3eb812fd4 | 329 | nsapi_security_t ap_sec; |
JMF | 0:24d3eb812fd4 | 330 | uint8_t ap_ch; |
JMF | 0:24d3eb812fd4 | 331 | char ap_pass[ESP8266_PASSPHRASE_MAX_LENGTH + 1]; |
JMF | 0:24d3eb812fd4 | 332 | uint16_t _local_ports[ESP8266_SOCKET_COUNT]; |
JMF | 0:24d3eb812fd4 | 333 | |
JMF | 0:24d3eb812fd4 | 334 | bool _disable_default_softap(); |
JMF | 0:24d3eb812fd4 | 335 | void event(); |
JMF | 0:24d3eb812fd4 | 336 | bool _get_firmware_ok(); |
JMF | 0:24d3eb812fd4 | 337 | nsapi_error_t _init(void); |
JMF | 0:24d3eb812fd4 | 338 | nsapi_error_t _startup(const int8_t wifi_mode); |
JMF | 0:24d3eb812fd4 | 339 | |
JMF | 0:24d3eb812fd4 | 340 | struct { |
JMF | 0:24d3eb812fd4 | 341 | void (*callback)(void *); |
JMF | 0:24d3eb812fd4 | 342 | void *data; |
JMF | 0:24d3eb812fd4 | 343 | } _cbs[ESP8266_SOCKET_COUNT]; |
JMF | 0:24d3eb812fd4 | 344 | }; |
JMF | 0:24d3eb812fd4 | 345 | |
JMF | 0:24d3eb812fd4 | 346 | #endif |