Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
ESP8266Interface.h
00001 /* ESP8266 implementation of NetworkInterfaceAPI 00002 * Copyright (c) 2015 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #ifndef ESP8266_INTERFACE_H 00018 #define ESP8266_INTERFACE_H 00019 00020 #include "mbed.h" 00021 #include "ESP8266.h" 00022 00023 00024 #define ESP8266_SOCKET_COUNT 5 00025 00026 /** ESP8266Interface class 00027 * Implementation of the NetworkStack for the ESP8266 00028 */ 00029 class ESP8266Interface : public NetworkStack, public WiFiInterface 00030 { 00031 public: 00032 /** 00033 * @brief ESP8266Interface default constructor 00034 * Will use values defined in mbed_lib.json 00035 */ 00036 ESP8266Interface(); 00037 00038 /** ESP8266Interface lifetime 00039 * @param tx TX pin 00040 * @param rx RX pin 00041 * @param debug Enable debugging 00042 */ 00043 ESP8266Interface(PinName tx, PinName rx, bool debug = false); 00044 00045 /** Start the interface 00046 * 00047 * Attempts to connect to a WiFi network. Requires ssid and passphrase to be set. 00048 * If passphrase is invalid, NSAPI_ERROR_AUTH_ERROR is returned. 00049 * 00050 * @return 0 on success, negative error code on failure 00051 */ 00052 virtual int connect(); 00053 00054 /** Start the interface 00055 * 00056 * Attempts to connect to a WiFi network. 00057 * 00058 * @param ssid Name of the network to connect to 00059 * @param pass Security passphrase to connect to the network 00060 * @param security Type of encryption for connection (Default: NSAPI_SECURITY_NONE) 00061 * @param channel This parameter is not supported, setting it to anything else than 0 will result in NSAPI_ERROR_UNSUPPORTED 00062 * @return 0 on success, or error code on failure 00063 */ 00064 virtual int connect(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE , 00065 uint8_t channel = 0); 00066 00067 /** Set the WiFi network credentials 00068 * 00069 * @param ssid Name of the network to connect to 00070 * @param pass Security passphrase to connect to the network 00071 * @param security Type of encryption for connection 00072 * (defaults to NSAPI_SECURITY_NONE) 00073 * @return 0 on success, or error code on failure 00074 */ 00075 virtual int set_credentials(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE ); 00076 00077 /** Set the WiFi network channel - NOT SUPPORTED 00078 * 00079 * This function is not supported and will return NSAPI_ERROR_UNSUPPORTED 00080 * 00081 * @param channel Channel on which the connection is to be made, or 0 for any (Default: 0) 00082 * @return Not supported, returns NSAPI_ERROR_UNSUPPORTED 00083 */ 00084 virtual int set_channel(uint8_t channel); 00085 00086 /** Stop the interface 00087 * @return 0 on success, negative on failure 00088 */ 00089 virtual int disconnect(); 00090 00091 /** Get the internally stored IP address 00092 * @return IP address of the interface or null if not yet connected 00093 */ 00094 virtual const char *get_ip_address(); 00095 00096 /** Get the internally stored MAC address 00097 * @return MAC address of the interface 00098 */ 00099 virtual const char *get_mac_address(); 00100 00101 /** Get the local gateway 00102 * 00103 * @return Null-terminated representation of the local gateway 00104 * or null if no network mask has been recieved 00105 */ 00106 virtual const char *get_gateway(); 00107 00108 /** Get the local network mask 00109 * 00110 * @return Null-terminated representation of the local network mask 00111 * or null if no network mask has been recieved 00112 */ 00113 virtual const char *get_netmask(); 00114 00115 /** Gets the current radio signal strength for active connection 00116 * 00117 * @return Connection strength in dBm (negative value) 00118 */ 00119 virtual int8_t get_rssi(); 00120 00121 /** Scan for available networks 00122 * 00123 * This function will block. 00124 * 00125 * @param ap Pointer to allocated array to store discovered AP 00126 * @param count Size of allocated @a res array, or 0 to only count available AP 00127 * @param timeout Timeout in milliseconds; 0 for no timeout (Default: 0) 00128 * @return Number of entries in @a, or if @a count was 0 number of available networks, negative on error 00129 * see @a nsapi_error 00130 */ 00131 virtual int scan(WiFiAccessPoint *res, unsigned count); 00132 00133 /** Translates a hostname to an IP address with specific version 00134 * 00135 * The hostname may be either a domain name or an IP address. If the 00136 * hostname is an IP address, no network transactions will be performed. 00137 * 00138 * If no stack-specific DNS resolution is provided, the hostname 00139 * will be resolve using a UDP socket on the stack. 00140 * 00141 * @param address Destination for the host SocketAddress 00142 * @param host Hostname to resolve 00143 * @param version IP version of address to resolve, NSAPI_UNSPEC indicates 00144 * version is chosen by the stack (defaults to NSAPI_UNSPEC) 00145 * @return 0 on success, negative error code on failure 00146 */ 00147 using NetworkInterface::gethostbyname; 00148 00149 /** Add a domain name server to list of servers to query 00150 * 00151 * @param addr Destination for the host address 00152 * @return 0 on success, negative error code on failure 00153 */ 00154 using NetworkInterface::add_dns_server; 00155 00156 /** Set socket options 00157 * 00158 * The setsockopt allow an application to pass stack-specific hints 00159 * to the underlying stack. For unsupported options, 00160 * NSAPI_ERROR_UNSUPPORTED is returned and the socket is unmodified. 00161 * 00162 * @param handle Socket handle 00163 * @param level Stack-specific protocol level 00164 * @param optname Stack-specific option identifier 00165 * @param optval Option value 00166 * @param optlen Length of the option value 00167 * @return 0 on success, negative error code on failure 00168 */ 00169 virtual nsapi_error_t setsockopt(nsapi_socket_t handle, int level, 00170 int optname, const void *optval, unsigned optlen); 00171 00172 /** Get socket options 00173 * 00174 * getsockopt allows an application to retrieve stack-specific options 00175 * from the underlying stack using stack-specific level and option names, 00176 * or to request generic options using levels from nsapi_socket_level_t. 00177 * 00178 * For unsupported options, NSAPI_ERROR_UNSUPPORTED is returned 00179 * and the socket is unmodified. 00180 * 00181 * @param level Stack-specific protocol level or nsapi_socket_level_t 00182 * @param optname Level-specific option name 00183 * @param optval Destination for option value 00184 * @param optlen Length of the option value 00185 * @return 0 on success, negative error code on failure 00186 */ 00187 virtual nsapi_error_t getsockopt(nsapi_socket_t handle, int level, int optname, 00188 void *optval, unsigned *optlen); 00189 00190 /** Register callback for status reporting 00191 * 00192 * The specified status callback function will be called on status changes 00193 * on the network. The parameters on the callback are the event type and 00194 * event-type dependent reason parameter. 00195 * 00196 * In ESP8266 the callback will be called when processing OOB-messages via 00197 * AT-parser. Do NOT call any ESP8266Interface -functions or do extensive 00198 * processing in the callback. 00199 * 00200 * @param status_cb The callback for status changes 00201 */ 00202 virtual void attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb); 00203 00204 /** Get the connection status 00205 * 00206 * @return The connection status according to ConnectionStatusType 00207 */ 00208 virtual nsapi_connection_status_t get_connection_status() const; 00209 00210 protected: 00211 /** Open a socket 00212 * @param handle Handle in which to store new socket 00213 * @param proto Type of socket to open, NSAPI_TCP or NSAPI_UDP 00214 * @return 0 on success, negative on failure 00215 */ 00216 virtual int socket_open(void **handle, nsapi_protocol_t proto); 00217 00218 /** Close the socket 00219 * @param handle Socket handle 00220 * @return 0 on success, negative on failure 00221 * @note On failure, any memory associated with the socket must still 00222 * be cleaned up 00223 */ 00224 virtual int socket_close(void *handle); 00225 00226 /** Bind a server socket to a specific port 00227 * @param handle Socket handle 00228 * @param address Local address to listen for incoming connections on 00229 * @return 0 on success, negative on failure. 00230 */ 00231 virtual int socket_bind(void *handle, const SocketAddress &address); 00232 00233 /** Start listening for incoming connections 00234 * @param handle Socket handle 00235 * @param backlog Number of pending connections that can be queued up at any 00236 * one time [Default: 1] 00237 * @return 0 on success, negative on failure 00238 */ 00239 virtual int socket_listen(void *handle, int backlog); 00240 00241 /** Connects this TCP socket to the server 00242 * @param handle Socket handle 00243 * @param address SocketAddress to connect to 00244 * @return 0 on success, negative on failure 00245 */ 00246 virtual int socket_connect(void *handle, const SocketAddress &address); 00247 00248 /** Accept a new connection. 00249 * @param handle Handle in which to store new socket 00250 * @param server Socket handle to server to accept from 00251 * @return 0 on success, negative on failure 00252 * @note This call is not-blocking, if this call would block, must 00253 * immediately return NSAPI_ERROR_WOULD_WAIT 00254 */ 00255 virtual int socket_accept(void *handle, void **socket, SocketAddress *address); 00256 00257 /** Send data to the remote host 00258 * @param handle Socket handle 00259 * @param data The buffer to send to the host 00260 * @param size The length of the buffer to send 00261 * @return Number of written bytes on success, negative on failure 00262 * @note This call is not-blocking, if this call would block, must 00263 * immediately return NSAPI_ERROR_WOULD_WAIT 00264 */ 00265 virtual int socket_send(void *handle, const void *data, unsigned size); 00266 00267 /** Receive data from the remote host 00268 * @param handle Socket handle 00269 * @param data The buffer in which to store the data received from the host 00270 * @param size The maximum length of the buffer 00271 * @return Number of received bytes on success, negative on failure 00272 * @note This call is not-blocking, if this call would block, must 00273 * immediately return NSAPI_ERROR_WOULD_WAIT 00274 */ 00275 virtual int socket_recv(void *handle, void *data, unsigned size); 00276 00277 /** Send a packet to a remote endpoint 00278 * @param handle Socket handle 00279 * @param address The remote SocketAddress 00280 * @param data The packet to be sent 00281 * @param size The length of the packet to be sent 00282 * @return The number of written bytes on success, negative on failure 00283 * @note This call is not-blocking, if this call would block, must 00284 * immediately return NSAPI_ERROR_WOULD_WAIT 00285 */ 00286 virtual int socket_sendto(void *handle, const SocketAddress &address, const void *data, unsigned size); 00287 00288 /** Receive a packet from a remote endpoint 00289 * @param handle Socket handle 00290 * @param address Destination for the remote SocketAddress or null 00291 * @param buffer The buffer for storing the incoming packet data 00292 * If a packet is too long to fit in the supplied buffer, 00293 * excess bytes are discarded 00294 * @param size The length of the buffer 00295 * @return The number of received bytes on success, negative on failure 00296 * @note This call is not-blocking, if this call would block, must 00297 * immediately return NSAPI_ERROR_WOULD_WAIT 00298 */ 00299 virtual int socket_recvfrom(void *handle, SocketAddress *address, void *buffer, unsigned size); 00300 00301 /** Register a callback on state change of the socket 00302 * @param handle Socket handle 00303 * @param callback Function to call on state change 00304 * @param data Argument to pass to callback 00305 * @note Callback may be called in an interrupt context. 00306 */ 00307 virtual void socket_attach(void *handle, void (*callback)(void *), void *data); 00308 00309 /** Provide access to the NetworkStack object 00310 * 00311 * @return The underlying NetworkStack object 00312 */ 00313 virtual NetworkStack *get_stack() 00314 { 00315 return this; 00316 } 00317 00318 private: 00319 static const int ESP8266_SSID_MAX_LENGTH = 32; /* 32 is what 802.11 defines as longest possible name */ 00320 static const int ESP8266_PASSPHRASE_MAX_LENGTH = 63; /* The longest allowed passphrase */ 00321 static const int ESP8266_PASSPHRASE_MIN_LENGTH = 8; /* The shortest allowed passphrase */ 00322 00323 ESP8266 _esp; 00324 bool _ids[ESP8266_SOCKET_COUNT]; 00325 int _initialized; 00326 int _started; 00327 00328 char ap_ssid[ESP8266_SSID_MAX_LENGTH + 1]; /* 32 is what 802.11 defines as longest possible name; +1 for the \0 */ 00329 nsapi_security_t ap_sec; 00330 uint8_t ap_ch; 00331 char ap_pass[ESP8266_PASSPHRASE_MAX_LENGTH + 1]; 00332 uint16_t _local_ports[ESP8266_SOCKET_COUNT]; 00333 00334 bool _disable_default_softap(); 00335 void event(); 00336 bool _get_firmware_ok(); 00337 nsapi_error_t _init(void); 00338 nsapi_error_t _startup(const int8_t wifi_mode); 00339 00340 struct { 00341 void (*callback)(void *); 00342 void *data; 00343 } _cbs[ESP8266_SOCKET_COUNT]; 00344 }; 00345 00346 #endif
Generated on Tue Aug 9 2022 00:37:06 by
1.7.2