my customized lib
esp8266-driver/ESP8266Interface.h@0:8094b249013c, 2017-11-26 (annotated)
- Committer:
- DuyLionTran
- Date:
- Sun Nov 26 15:08:14 2017 +0000
- Revision:
- 0:8094b249013c
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
DuyLionTran | 0:8094b249013c | 1 | /* ESP8266 implementation of NetworkInterfaceAPI |
DuyLionTran | 0:8094b249013c | 2 | * Copyright (c) 2015 ARM Limited |
DuyLionTran | 0:8094b249013c | 3 | * |
DuyLionTran | 0:8094b249013c | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
DuyLionTran | 0:8094b249013c | 5 | * you may not use this file except in compliance with the License. |
DuyLionTran | 0:8094b249013c | 6 | * You may obtain a copy of the License at |
DuyLionTran | 0:8094b249013c | 7 | * |
DuyLionTran | 0:8094b249013c | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
DuyLionTran | 0:8094b249013c | 9 | * |
DuyLionTran | 0:8094b249013c | 10 | * Unless required by applicable law or agreed to in writing, software |
DuyLionTran | 0:8094b249013c | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
DuyLionTran | 0:8094b249013c | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
DuyLionTran | 0:8094b249013c | 13 | * See the License for the specific language governing permissions and |
DuyLionTran | 0:8094b249013c | 14 | * limitations under the License. |
DuyLionTran | 0:8094b249013c | 15 | */ |
DuyLionTran | 0:8094b249013c | 16 | |
DuyLionTran | 0:8094b249013c | 17 | #ifndef ESP8266_INTERFACE_H |
DuyLionTran | 0:8094b249013c | 18 | #define ESP8266_INTERFACE_H |
DuyLionTran | 0:8094b249013c | 19 | |
DuyLionTran | 0:8094b249013c | 20 | #include "mbed.h" |
DuyLionTran | 0:8094b249013c | 21 | #include "ESP8266.h" |
DuyLionTran | 0:8094b249013c | 22 | |
DuyLionTran | 0:8094b249013c | 23 | |
DuyLionTran | 0:8094b249013c | 24 | #define ESP8266_SOCKET_COUNT 5 |
DuyLionTran | 0:8094b249013c | 25 | |
DuyLionTran | 0:8094b249013c | 26 | /** ESP8266Interface class |
DuyLionTran | 0:8094b249013c | 27 | * Implementation of the NetworkStack for the ESP8266 |
DuyLionTran | 0:8094b249013c | 28 | */ |
DuyLionTran | 0:8094b249013c | 29 | class ESP8266Interface : public NetworkStack, public WiFiInterface |
DuyLionTran | 0:8094b249013c | 30 | { |
DuyLionTran | 0:8094b249013c | 31 | public: |
DuyLionTran | 0:8094b249013c | 32 | /** ESP8266Interface lifetime |
DuyLionTran | 0:8094b249013c | 33 | * @param tx TX pin |
DuyLionTran | 0:8094b249013c | 34 | * @param rx RX pin |
DuyLionTran | 0:8094b249013c | 35 | * @param debug Enable debugging |
DuyLionTran | 0:8094b249013c | 36 | */ |
DuyLionTran | 0:8094b249013c | 37 | ESP8266Interface(PinName tx, PinName rx, bool debug = false); |
DuyLionTran | 0:8094b249013c | 38 | |
DuyLionTran | 0:8094b249013c | 39 | /** Start the interface |
DuyLionTran | 0:8094b249013c | 40 | * |
DuyLionTran | 0:8094b249013c | 41 | * Attempts to connect to a WiFi network. Requires ssid and passphrase to be set. |
DuyLionTran | 0:8094b249013c | 42 | * If passphrase is invalid, NSAPI_ERROR_AUTH_ERROR is returned. |
DuyLionTran | 0:8094b249013c | 43 | * |
DuyLionTran | 0:8094b249013c | 44 | * @return 0 on success, negative error code on failure |
DuyLionTran | 0:8094b249013c | 45 | */ |
DuyLionTran | 0:8094b249013c | 46 | virtual int connect(); |
DuyLionTran | 0:8094b249013c | 47 | |
DuyLionTran | 0:8094b249013c | 48 | /** Start the interface |
DuyLionTran | 0:8094b249013c | 49 | * |
DuyLionTran | 0:8094b249013c | 50 | * Attempts to connect to a WiFi network. |
DuyLionTran | 0:8094b249013c | 51 | * |
DuyLionTran | 0:8094b249013c | 52 | * @param ssid Name of the network to connect to |
DuyLionTran | 0:8094b249013c | 53 | * @param pass Security passphrase to connect to the network |
DuyLionTran | 0:8094b249013c | 54 | * @param security Type of encryption for connection (Default: NSAPI_SECURITY_NONE) |
DuyLionTran | 0:8094b249013c | 55 | * @param channel This parameter is not supported, setting it to anything else than 0 will result in NSAPI_ERROR_UNSUPPORTED |
DuyLionTran | 0:8094b249013c | 56 | * @return 0 on success, or error code on failure |
DuyLionTran | 0:8094b249013c | 57 | */ |
DuyLionTran | 0:8094b249013c | 58 | virtual int connect(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE, |
DuyLionTran | 0:8094b249013c | 59 | uint8_t channel = 0); |
DuyLionTran | 0:8094b249013c | 60 | |
DuyLionTran | 0:8094b249013c | 61 | /** Set the WiFi network credentials |
DuyLionTran | 0:8094b249013c | 62 | * |
DuyLionTran | 0:8094b249013c | 63 | * @param ssid Name of the network to connect to |
DuyLionTran | 0:8094b249013c | 64 | * @param pass Security passphrase to connect to the network |
DuyLionTran | 0:8094b249013c | 65 | * @param security Type of encryption for connection |
DuyLionTran | 0:8094b249013c | 66 | * (defaults to NSAPI_SECURITY_NONE) |
DuyLionTran | 0:8094b249013c | 67 | * @return 0 on success, or error code on failure |
DuyLionTran | 0:8094b249013c | 68 | */ |
DuyLionTran | 0:8094b249013c | 69 | virtual int set_credentials(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE); |
DuyLionTran | 0:8094b249013c | 70 | |
DuyLionTran | 0:8094b249013c | 71 | /** Set the WiFi network channel - NOT SUPPORTED |
DuyLionTran | 0:8094b249013c | 72 | * |
DuyLionTran | 0:8094b249013c | 73 | * This function is not supported and will return NSAPI_ERROR_UNSUPPORTED |
DuyLionTran | 0:8094b249013c | 74 | * |
DuyLionTran | 0:8094b249013c | 75 | * @param channel Channel on which the connection is to be made, or 0 for any (Default: 0) |
DuyLionTran | 0:8094b249013c | 76 | * @return Not supported, returns NSAPI_ERROR_UNSUPPORTED |
DuyLionTran | 0:8094b249013c | 77 | */ |
DuyLionTran | 0:8094b249013c | 78 | virtual int set_channel(uint8_t channel); |
DuyLionTran | 0:8094b249013c | 79 | |
DuyLionTran | 0:8094b249013c | 80 | /** Stop the interface |
DuyLionTran | 0:8094b249013c | 81 | * @return 0 on success, negative on failure |
DuyLionTran | 0:8094b249013c | 82 | */ |
DuyLionTran | 0:8094b249013c | 83 | virtual int disconnect(); |
DuyLionTran | 0:8094b249013c | 84 | |
DuyLionTran | 0:8094b249013c | 85 | /** Get the internally stored IP address |
DuyLionTran | 0:8094b249013c | 86 | * @return IP address of the interface or null if not yet connected |
DuyLionTran | 0:8094b249013c | 87 | */ |
DuyLionTran | 0:8094b249013c | 88 | virtual const char *get_ip_address(); |
DuyLionTran | 0:8094b249013c | 89 | |
DuyLionTran | 0:8094b249013c | 90 | /** Get the internally stored MAC address |
DuyLionTran | 0:8094b249013c | 91 | * @return MAC address of the interface |
DuyLionTran | 0:8094b249013c | 92 | */ |
DuyLionTran | 0:8094b249013c | 93 | virtual const char *get_mac_address(); |
DuyLionTran | 0:8094b249013c | 94 | |
DuyLionTran | 0:8094b249013c | 95 | /** Get the local gateway |
DuyLionTran | 0:8094b249013c | 96 | * |
DuyLionTran | 0:8094b249013c | 97 | * @return Null-terminated representation of the local gateway |
DuyLionTran | 0:8094b249013c | 98 | * or null if no network mask has been recieved |
DuyLionTran | 0:8094b249013c | 99 | */ |
DuyLionTran | 0:8094b249013c | 100 | virtual const char *get_gateway(); |
DuyLionTran | 0:8094b249013c | 101 | |
DuyLionTran | 0:8094b249013c | 102 | /** Get the local network mask |
DuyLionTran | 0:8094b249013c | 103 | * |
DuyLionTran | 0:8094b249013c | 104 | * @return Null-terminated representation of the local network mask |
DuyLionTran | 0:8094b249013c | 105 | * or null if no network mask has been recieved |
DuyLionTran | 0:8094b249013c | 106 | */ |
DuyLionTran | 0:8094b249013c | 107 | virtual const char *get_netmask(); |
DuyLionTran | 0:8094b249013c | 108 | |
DuyLionTran | 0:8094b249013c | 109 | /** Gets the current radio signal strength for active connection |
DuyLionTran | 0:8094b249013c | 110 | * |
DuyLionTran | 0:8094b249013c | 111 | * @return Connection strength in dBm (negative value) |
DuyLionTran | 0:8094b249013c | 112 | */ |
DuyLionTran | 0:8094b249013c | 113 | virtual int8_t get_rssi(); |
DuyLionTran | 0:8094b249013c | 114 | |
DuyLionTran | 0:8094b249013c | 115 | /** Scan for available networks |
DuyLionTran | 0:8094b249013c | 116 | * |
DuyLionTran | 0:8094b249013c | 117 | * This function will block. |
DuyLionTran | 0:8094b249013c | 118 | * |
DuyLionTran | 0:8094b249013c | 119 | * @param ap Pointer to allocated array to store discovered AP |
DuyLionTran | 0:8094b249013c | 120 | * @param count Size of allocated @a res array, or 0 to only count available AP |
DuyLionTran | 0:8094b249013c | 121 | * @param timeout Timeout in milliseconds; 0 for no timeout (Default: 0) |
DuyLionTran | 0:8094b249013c | 122 | * @return Number of entries in @a, or if @a count was 0 number of available networks, negative on error |
DuyLionTran | 0:8094b249013c | 123 | * see @a nsapi_error |
DuyLionTran | 0:8094b249013c | 124 | */ |
DuyLionTran | 0:8094b249013c | 125 | virtual int scan(WiFiAccessPoint *res, unsigned count); |
DuyLionTran | 0:8094b249013c | 126 | |
DuyLionTran | 0:8094b249013c | 127 | /** Translates a hostname to an IP address with specific version |
DuyLionTran | 0:8094b249013c | 128 | * |
DuyLionTran | 0:8094b249013c | 129 | * The hostname may be either a domain name or an IP address. If the |
DuyLionTran | 0:8094b249013c | 130 | * hostname is an IP address, no network transactions will be performed. |
DuyLionTran | 0:8094b249013c | 131 | * |
DuyLionTran | 0:8094b249013c | 132 | * If no stack-specific DNS resolution is provided, the hostname |
DuyLionTran | 0:8094b249013c | 133 | * will be resolve using a UDP socket on the stack. |
DuyLionTran | 0:8094b249013c | 134 | * |
DuyLionTran | 0:8094b249013c | 135 | * @param address Destination for the host SocketAddress |
DuyLionTran | 0:8094b249013c | 136 | * @param host Hostname to resolve |
DuyLionTran | 0:8094b249013c | 137 | * @param version IP version of address to resolve, NSAPI_UNSPEC indicates |
DuyLionTran | 0:8094b249013c | 138 | * version is chosen by the stack (defaults to NSAPI_UNSPEC) |
DuyLionTran | 0:8094b249013c | 139 | * @return 0 on success, negative error code on failure |
DuyLionTran | 0:8094b249013c | 140 | */ |
DuyLionTran | 0:8094b249013c | 141 | using NetworkInterface::gethostbyname; |
DuyLionTran | 0:8094b249013c | 142 | |
DuyLionTran | 0:8094b249013c | 143 | /** Add a domain name server to list of servers to query |
DuyLionTran | 0:8094b249013c | 144 | * |
DuyLionTran | 0:8094b249013c | 145 | * @param addr Destination for the host address |
DuyLionTran | 0:8094b249013c | 146 | * @return 0 on success, negative error code on failure |
DuyLionTran | 0:8094b249013c | 147 | */ |
DuyLionTran | 0:8094b249013c | 148 | using NetworkInterface::add_dns_server; |
DuyLionTran | 0:8094b249013c | 149 | |
DuyLionTran | 0:8094b249013c | 150 | protected: |
DuyLionTran | 0:8094b249013c | 151 | /** Open a socket |
DuyLionTran | 0:8094b249013c | 152 | * @param handle Handle in which to store new socket |
DuyLionTran | 0:8094b249013c | 153 | * @param proto Type of socket to open, NSAPI_TCP or NSAPI_UDP |
DuyLionTran | 0:8094b249013c | 154 | * @return 0 on success, negative on failure |
DuyLionTran | 0:8094b249013c | 155 | */ |
DuyLionTran | 0:8094b249013c | 156 | virtual int socket_open(void **handle, nsapi_protocol_t proto); |
DuyLionTran | 0:8094b249013c | 157 | |
DuyLionTran | 0:8094b249013c | 158 | /** Close the socket |
DuyLionTran | 0:8094b249013c | 159 | * @param handle Socket handle |
DuyLionTran | 0:8094b249013c | 160 | * @return 0 on success, negative on failure |
DuyLionTran | 0:8094b249013c | 161 | * @note On failure, any memory associated with the socket must still |
DuyLionTran | 0:8094b249013c | 162 | * be cleaned up |
DuyLionTran | 0:8094b249013c | 163 | */ |
DuyLionTran | 0:8094b249013c | 164 | virtual int socket_close(void *handle); |
DuyLionTran | 0:8094b249013c | 165 | |
DuyLionTran | 0:8094b249013c | 166 | /** Bind a server socket to a specific port |
DuyLionTran | 0:8094b249013c | 167 | * @param handle Socket handle |
DuyLionTran | 0:8094b249013c | 168 | * @param address Local address to listen for incoming connections on |
DuyLionTran | 0:8094b249013c | 169 | * @return 0 on success, negative on failure. |
DuyLionTran | 0:8094b249013c | 170 | */ |
DuyLionTran | 0:8094b249013c | 171 | virtual int socket_bind(void *handle, const SocketAddress &address); |
DuyLionTran | 0:8094b249013c | 172 | |
DuyLionTran | 0:8094b249013c | 173 | /** Start listening for incoming connections |
DuyLionTran | 0:8094b249013c | 174 | * @param handle Socket handle |
DuyLionTran | 0:8094b249013c | 175 | * @param backlog Number of pending connections that can be queued up at any |
DuyLionTran | 0:8094b249013c | 176 | * one time [Default: 1] |
DuyLionTran | 0:8094b249013c | 177 | * @return 0 on success, negative on failure |
DuyLionTran | 0:8094b249013c | 178 | */ |
DuyLionTran | 0:8094b249013c | 179 | virtual int socket_listen(void *handle, int backlog); |
DuyLionTran | 0:8094b249013c | 180 | |
DuyLionTran | 0:8094b249013c | 181 | /** Connects this TCP socket to the server |
DuyLionTran | 0:8094b249013c | 182 | * @param handle Socket handle |
DuyLionTran | 0:8094b249013c | 183 | * @param address SocketAddress to connect to |
DuyLionTran | 0:8094b249013c | 184 | * @return 0 on success, negative on failure |
DuyLionTran | 0:8094b249013c | 185 | */ |
DuyLionTran | 0:8094b249013c | 186 | virtual int socket_connect(void *handle, const SocketAddress &address); |
DuyLionTran | 0:8094b249013c | 187 | |
DuyLionTran | 0:8094b249013c | 188 | /** Accept a new connection. |
DuyLionTran | 0:8094b249013c | 189 | * @param handle Handle in which to store new socket |
DuyLionTran | 0:8094b249013c | 190 | * @param server Socket handle to server to accept from |
DuyLionTran | 0:8094b249013c | 191 | * @return 0 on success, negative on failure |
DuyLionTran | 0:8094b249013c | 192 | * @note This call is not-blocking, if this call would block, must |
DuyLionTran | 0:8094b249013c | 193 | * immediately return NSAPI_ERROR_WOULD_WAIT |
DuyLionTran | 0:8094b249013c | 194 | */ |
DuyLionTran | 0:8094b249013c | 195 | virtual int socket_accept(void *handle, void **socket, SocketAddress *address); |
DuyLionTran | 0:8094b249013c | 196 | |
DuyLionTran | 0:8094b249013c | 197 | /** Send data to the remote host |
DuyLionTran | 0:8094b249013c | 198 | * @param handle Socket handle |
DuyLionTran | 0:8094b249013c | 199 | * @param data The buffer to send to the host |
DuyLionTran | 0:8094b249013c | 200 | * @param size The length of the buffer to send |
DuyLionTran | 0:8094b249013c | 201 | * @return Number of written bytes on success, negative on failure |
DuyLionTran | 0:8094b249013c | 202 | * @note This call is not-blocking, if this call would block, must |
DuyLionTran | 0:8094b249013c | 203 | * immediately return NSAPI_ERROR_WOULD_WAIT |
DuyLionTran | 0:8094b249013c | 204 | */ |
DuyLionTran | 0:8094b249013c | 205 | virtual int socket_send(void *handle, const void *data, unsigned size); |
DuyLionTran | 0:8094b249013c | 206 | |
DuyLionTran | 0:8094b249013c | 207 | /** Receive data from the remote host |
DuyLionTran | 0:8094b249013c | 208 | * @param handle Socket handle |
DuyLionTran | 0:8094b249013c | 209 | * @param data The buffer in which to store the data received from the host |
DuyLionTran | 0:8094b249013c | 210 | * @param size The maximum length of the buffer |
DuyLionTran | 0:8094b249013c | 211 | * @return Number of received bytes on success, negative on failure |
DuyLionTran | 0:8094b249013c | 212 | * @note This call is not-blocking, if this call would block, must |
DuyLionTran | 0:8094b249013c | 213 | * immediately return NSAPI_ERROR_WOULD_WAIT |
DuyLionTran | 0:8094b249013c | 214 | */ |
DuyLionTran | 0:8094b249013c | 215 | virtual int socket_recv(void *handle, void *data, unsigned size); |
DuyLionTran | 0:8094b249013c | 216 | |
DuyLionTran | 0:8094b249013c | 217 | /** Send a packet to a remote endpoint |
DuyLionTran | 0:8094b249013c | 218 | * @param handle Socket handle |
DuyLionTran | 0:8094b249013c | 219 | * @param address The remote SocketAddress |
DuyLionTran | 0:8094b249013c | 220 | * @param data The packet to be sent |
DuyLionTran | 0:8094b249013c | 221 | * @param size The length of the packet to be sent |
DuyLionTran | 0:8094b249013c | 222 | * @return The number of written bytes on success, negative on failure |
DuyLionTran | 0:8094b249013c | 223 | * @note This call is not-blocking, if this call would block, must |
DuyLionTran | 0:8094b249013c | 224 | * immediately return NSAPI_ERROR_WOULD_WAIT |
DuyLionTran | 0:8094b249013c | 225 | */ |
DuyLionTran | 0:8094b249013c | 226 | virtual int socket_sendto(void *handle, const SocketAddress &address, const void *data, unsigned size); |
DuyLionTran | 0:8094b249013c | 227 | |
DuyLionTran | 0:8094b249013c | 228 | /** Receive a packet from a remote endpoint |
DuyLionTran | 0:8094b249013c | 229 | * @param handle Socket handle |
DuyLionTran | 0:8094b249013c | 230 | * @param address Destination for the remote SocketAddress or null |
DuyLionTran | 0:8094b249013c | 231 | * @param buffer The buffer for storing the incoming packet data |
DuyLionTran | 0:8094b249013c | 232 | * If a packet is too long to fit in the supplied buffer, |
DuyLionTran | 0:8094b249013c | 233 | * excess bytes are discarded |
DuyLionTran | 0:8094b249013c | 234 | * @param size The length of the buffer |
DuyLionTran | 0:8094b249013c | 235 | * @return The number of received bytes on success, negative on failure |
DuyLionTran | 0:8094b249013c | 236 | * @note This call is not-blocking, if this call would block, must |
DuyLionTran | 0:8094b249013c | 237 | * immediately return NSAPI_ERROR_WOULD_WAIT |
DuyLionTran | 0:8094b249013c | 238 | */ |
DuyLionTran | 0:8094b249013c | 239 | virtual int socket_recvfrom(void *handle, SocketAddress *address, void *buffer, unsigned size); |
DuyLionTran | 0:8094b249013c | 240 | |
DuyLionTran | 0:8094b249013c | 241 | /** Register a callback on state change of the socket |
DuyLionTran | 0:8094b249013c | 242 | * @param handle Socket handle |
DuyLionTran | 0:8094b249013c | 243 | * @param callback Function to call on state change |
DuyLionTran | 0:8094b249013c | 244 | * @param data Argument to pass to callback |
DuyLionTran | 0:8094b249013c | 245 | * @note Callback may be called in an interrupt context. |
DuyLionTran | 0:8094b249013c | 246 | */ |
DuyLionTran | 0:8094b249013c | 247 | virtual void socket_attach(void *handle, void (*callback)(void *), void *data); |
DuyLionTran | 0:8094b249013c | 248 | |
DuyLionTran | 0:8094b249013c | 249 | /** Provide access to the NetworkStack object |
DuyLionTran | 0:8094b249013c | 250 | * |
DuyLionTran | 0:8094b249013c | 251 | * @return The underlying NetworkStack object |
DuyLionTran | 0:8094b249013c | 252 | */ |
DuyLionTran | 0:8094b249013c | 253 | virtual NetworkStack *get_stack() |
DuyLionTran | 0:8094b249013c | 254 | { |
DuyLionTran | 0:8094b249013c | 255 | return this; |
DuyLionTran | 0:8094b249013c | 256 | } |
DuyLionTran | 0:8094b249013c | 257 | |
DuyLionTran | 0:8094b249013c | 258 | private: |
DuyLionTran | 0:8094b249013c | 259 | ESP8266 _esp; |
DuyLionTran | 0:8094b249013c | 260 | bool _ids[ESP8266_SOCKET_COUNT]; |
DuyLionTran | 0:8094b249013c | 261 | |
DuyLionTran | 0:8094b249013c | 262 | char ap_ssid[33]; /* 32 is what 802.11 defines as longest possible name; +1 for the \0 */ |
DuyLionTran | 0:8094b249013c | 263 | nsapi_security_t ap_sec; |
DuyLionTran | 0:8094b249013c | 264 | uint8_t ap_ch; |
DuyLionTran | 0:8094b249013c | 265 | char ap_pass[64]; /* The longest allowed passphrase */ |
DuyLionTran | 0:8094b249013c | 266 | |
DuyLionTran | 0:8094b249013c | 267 | void event(); |
DuyLionTran | 0:8094b249013c | 268 | |
DuyLionTran | 0:8094b249013c | 269 | struct { |
DuyLionTran | 0:8094b249013c | 270 | void (*callback)(void *); |
DuyLionTran | 0:8094b249013c | 271 | void *data; |
DuyLionTran | 0:8094b249013c | 272 | } _cbs[ESP8266_SOCKET_COUNT]; |
DuyLionTran | 0:8094b249013c | 273 | }; |
DuyLionTran | 0:8094b249013c | 274 | |
DuyLionTran | 0:8094b249013c | 275 | #endif |