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.
ESP32Interface.h
00001 /* ESP32 implementation of NetworkInterfaceAPI 00002 * Copyright (c) 2017 Renesas Electronics Corporation 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 ESP32_INTERFACE_H 00018 #define ESP32_INTERFACE_H 00019 00020 #include "mbed.h" 00021 #include "ESP32Stack.h" 00022 00023 /** ESP32Interface class 00024 * Implementation of the NetworkStack for the ESP32 00025 */ 00026 class ESP32Interface : public ESP32Stack, public WiFiInterface 00027 { 00028 public: 00029 /** ESP32Interface lifetime 00030 * @param en EN pin (If not used this pin, please set "NC") 00031 * @param io0 IO0 pin (If not used this pin, please set "NC") 00032 * @param tx TX pin 00033 * @param rx RX pin 00034 * @param debug Enable debugging 00035 * @param rts RTS pin 00036 * @param cts CTS pin 00037 * @param baudrate The baudrate of the serial port (default = 230400). 00038 */ 00039 ESP32Interface(PinName en, PinName io0, PinName tx, PinName rx, bool debug = false, 00040 PinName rts = NC, PinName cts = NC, int baudrate = 230400); 00041 00042 /** ESP32Interface lifetime 00043 * @param tx TX pin 00044 * @param rx RX pin 00045 * @param debug Enable debugging 00046 */ 00047 ESP32Interface(PinName tx, PinName rx, bool debug = false); 00048 00049 /** Set a static IP address 00050 * 00051 * Configures this network interface to use a static IP address. 00052 * Implicitly disables DHCP, which can be enabled in set_dhcp. 00053 * Requires that the network is disconnected. 00054 * 00055 * @param ip_address Null-terminated representation of the local IP address 00056 * @param netmask Null-terminated representation of the local network mask 00057 * @param gateway Null-terminated representation of the local gateway 00058 * @return 0 on success, negative error code on failure 00059 */ 00060 virtual nsapi_error_t set_network( 00061 const char *ip_address, const char *netmask, const char *gateway); 00062 00063 /** Enable or disable DHCP on the network 00064 * 00065 * Enables DHCP on connecting the network. Defaults to enabled unless 00066 * a static IP address has been assigned. Requires that the network is 00067 * disconnected. 00068 * 00069 * @param dhcp True to enable DHCP 00070 * @return 0 on success, negative error code on failure 00071 */ 00072 virtual nsapi_error_t set_dhcp(bool dhcp); 00073 00074 /** Start the interface 00075 * 00076 * Attempts to connect to a WiFi network. Requires ssid and passphrase to be set. 00077 * If passphrase is invalid, NSAPI_ERROR_AUTH_ERROR is returned. 00078 * 00079 * @return 0 on success, negative error code on failure 00080 */ 00081 virtual int connect(); 00082 00083 /** Start the interface 00084 * 00085 * Attempts to connect to a WiFi network. 00086 * 00087 * @param ssid Name of the network to connect to 00088 * @param pass Security passphrase to connect to the network 00089 * @param security Type of encryption for connection (Default: NSAPI_SECURITY_NONE) 00090 * @param channel This parameter is not supported, setting it to anything else than 0 will result in NSAPI_ERROR_UNSUPPORTED 00091 * @return 0 on success, or error code on failure 00092 */ 00093 virtual int connect(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE, 00094 uint8_t channel = 0); 00095 00096 /** Set the WiFi network credentials 00097 * 00098 * @param ssid Name of the network to connect to 00099 * @param pass Security passphrase to connect to the network 00100 * @param security Type of encryption for connection 00101 * (defaults to NSAPI_SECURITY_NONE) 00102 * @return 0 on success, or error code on failure 00103 */ 00104 virtual int set_credentials(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE); 00105 00106 /** Set the WiFi network channel - NOT SUPPORTED 00107 * 00108 * This function is not supported and will return NSAPI_ERROR_UNSUPPORTED 00109 * 00110 * @param channel Channel on which the connection is to be made, or 0 for any (Default: 0) 00111 * @return Not supported, returns NSAPI_ERROR_UNSUPPORTED 00112 */ 00113 virtual int set_channel(uint8_t channel); 00114 00115 /** Stop the interface 00116 * @return 0 on success, negative on failure 00117 */ 00118 virtual int disconnect(); 00119 00120 /** Get the internally stored IP address 00121 * @return IP address of the interface or null if not yet connected 00122 */ 00123 virtual const char *get_ip_address(); 00124 00125 /** Get the internally stored MAC address 00126 * @return MAC address of the interface 00127 */ 00128 virtual const char *get_mac_address(); 00129 00130 /** Get the local gateway 00131 * 00132 * @return Null-terminated representation of the local gateway 00133 * or null if no network mask has been recieved 00134 */ 00135 virtual const char *get_gateway(); 00136 00137 /** Get the local network mask 00138 * 00139 * @return Null-terminated representation of the local network mask 00140 * or null if no network mask has been recieved 00141 */ 00142 virtual const char *get_netmask(); 00143 00144 /** Gets the current radio signal strength for active connection 00145 * 00146 * @return Connection strength in dBm (negative value) 00147 */ 00148 virtual int8_t get_rssi(); 00149 00150 /** Scan for available networks 00151 * 00152 * This function will block. 00153 * 00154 * @param ap Pointer to allocated array to store discovered AP 00155 * @param count Size of allocated @a res array, or 0 to only count available AP 00156 * @param timeout Timeout in milliseconds; 0 for no timeout (Default: 0) 00157 * @return Number of entries in @a, or if @a count was 0 number of available networks, negative on error 00158 * see @a nsapi_error 00159 */ 00160 virtual int scan(WiFiAccessPoint *res, unsigned count); 00161 00162 /** Translates a hostname to an IP address with specific version 00163 * 00164 * The hostname may be either a domain name or an IP address. If the 00165 * hostname is an IP address, no network transactions will be performed. 00166 * 00167 * If no stack-specific DNS resolution is provided, the hostname 00168 * will be resolve using a UDP socket on the stack. 00169 * 00170 * @param address Destination for the host SocketAddress 00171 * @param host Hostname to resolve 00172 * @param version IP version of address to resolve, NSAPI_UNSPEC indicates 00173 * version is chosen by the stack (defaults to NSAPI_UNSPEC) 00174 * @return 0 on success, negative error code on failure 00175 */ 00176 using NetworkInterface::gethostbyname; 00177 00178 /** Add a domain name server to list of servers to query 00179 * 00180 * @param addr Destination for the host address 00181 * @return 0 on success, negative error code on failure 00182 */ 00183 using NetworkInterface::add_dns_server; 00184 00185 /** Register callback for status reporting 00186 * 00187 * The specified status callback function will be called on status changes 00188 * on the network. The parameters on the callback are the event type and 00189 * event-type dependent reason parameter. 00190 * 00191 * In ESP32 the callback will be called when processing OOB-messages via 00192 * AT-parser. Do NOT call any ESP8266Interface -functions or do extensive 00193 * processing in the callback. 00194 * 00195 * @param status_cb The callback for status changes 00196 */ 00197 virtual void attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb); 00198 00199 /** Get the connection status 00200 * 00201 * @return The connection status according to ConnectionStatusType 00202 */ 00203 virtual nsapi_connection_status_t get_connection_status() const; 00204 00205 /** Provide access to the NetworkStack object 00206 * 00207 * @return The underlying NetworkStack object 00208 */ 00209 virtual NetworkStack *get_stack() 00210 { 00211 return this; 00212 } 00213 00214 private: 00215 bool _dhcp; 00216 char _ap_ssid[33]; /* 32 is what 802.11 defines as longest possible name; +1 for the \0 */ 00217 char _ap_pass[64]; /* The longest allowed passphrase */ 00218 nsapi_security_t _ap_sec; 00219 char _ip_address[NSAPI_IPv6_SIZE]; 00220 char _netmask[NSAPI_IPv4_SIZE]; 00221 char _gateway[NSAPI_IPv4_SIZE]; 00222 nsapi_connection_status_t _connection_status; 00223 Callback<void(nsapi_event_t, intptr_t)> _connection_status_cb; 00224 00225 void set_connection_status(nsapi_connection_status_t connection_status); 00226 void wifi_status_cb(int8_t wifi_status); 00227 }; 00228 00229 #endif
Generated on Wed Jul 13 2022 08:56:28 by
1.7.2