The driver for the ESP32 WiFi module

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ESP32InterfaceAP.h Source File

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