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.
esp32-driver/ESP32InterfaceAP.h@0:8f8e8f3cbd1c, 2018-06-21 (annotated)
- Committer:
- mayur098
- Date:
- Thu Jun 21 17:50:21 2018 +0000
- Revision:
- 0:8f8e8f3cbd1c
first commit;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mayur098 | 0:8f8e8f3cbd1c | 1 | /* ESP32 implementation of NetworkInterfaceAPI |
mayur098 | 0:8f8e8f3cbd1c | 2 | * Copyright (c) 2017 Renesas Electronics Corporation |
mayur098 | 0:8f8e8f3cbd1c | 3 | * |
mayur098 | 0:8f8e8f3cbd1c | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mayur098 | 0:8f8e8f3cbd1c | 5 | * you may not use this file except in compliance with the License. |
mayur098 | 0:8f8e8f3cbd1c | 6 | * You may obtain a copy of the License at |
mayur098 | 0:8f8e8f3cbd1c | 7 | * |
mayur098 | 0:8f8e8f3cbd1c | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mayur098 | 0:8f8e8f3cbd1c | 9 | * |
mayur098 | 0:8f8e8f3cbd1c | 10 | * Unless required by applicable law or agreed to in writing, software |
mayur098 | 0:8f8e8f3cbd1c | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
mayur098 | 0:8f8e8f3cbd1c | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mayur098 | 0:8f8e8f3cbd1c | 13 | * See the License for the specific language governing permissions and |
mayur098 | 0:8f8e8f3cbd1c | 14 | * limitations under the License. |
mayur098 | 0:8f8e8f3cbd1c | 15 | */ |
mayur098 | 0:8f8e8f3cbd1c | 16 | |
mayur098 | 0:8f8e8f3cbd1c | 17 | #ifndef ESP32_INTERFACE_AP_H |
mayur098 | 0:8f8e8f3cbd1c | 18 | #define ESP32_INTERFACE_AP_H |
mayur098 | 0:8f8e8f3cbd1c | 19 | |
mayur098 | 0:8f8e8f3cbd1c | 20 | #include "mbed.h" |
mayur098 | 0:8f8e8f3cbd1c | 21 | #include "ESP32Stack.h" |
mayur098 | 0:8f8e8f3cbd1c | 22 | |
mayur098 | 0:8f8e8f3cbd1c | 23 | |
mayur098 | 0:8f8e8f3cbd1c | 24 | /** ESP32Interface class |
mayur098 | 0:8f8e8f3cbd1c | 25 | * Implementation of the NetworkStack for the ESP32 |
mayur098 | 0:8f8e8f3cbd1c | 26 | */ |
mayur098 | 0:8f8e8f3cbd1c | 27 | class ESP32InterfaceAP : public ESP32Stack, public WiFiInterface |
mayur098 | 0:8f8e8f3cbd1c | 28 | { |
mayur098 | 0:8f8e8f3cbd1c | 29 | public: |
mayur098 | 0:8f8e8f3cbd1c | 30 | /** ESP32InterfaceAP lifetime |
mayur098 | 0:8f8e8f3cbd1c | 31 | * @param en EN pin |
mayur098 | 0:8f8e8f3cbd1c | 32 | * @param io0 IO0 pin |
mayur098 | 0:8f8e8f3cbd1c | 33 | * @param tx TX pin |
mayur098 | 0:8f8e8f3cbd1c | 34 | * @param rx RX pin |
mayur098 | 0:8f8e8f3cbd1c | 35 | * @param debug Enable debugging |
mayur098 | 0:8f8e8f3cbd1c | 36 | * @param rts RTS pin |
mayur098 | 0:8f8e8f3cbd1c | 37 | * @param cts CTS pin |
mayur098 | 0:8f8e8f3cbd1c | 38 | * @param baudrate The baudrate of the serial port (default = 230400). |
mayur098 | 0:8f8e8f3cbd1c | 39 | */ |
mayur098 | 0:8f8e8f3cbd1c | 40 | ESP32InterfaceAP(PinName en, PinName io0, PinName tx, PinName rx, bool debug = false, |
mayur098 | 0:8f8e8f3cbd1c | 41 | PinName rts = NC, PinName cts = NC, int baudrate = 230400); |
mayur098 | 0:8f8e8f3cbd1c | 42 | |
mayur098 | 0:8f8e8f3cbd1c | 43 | /** Start the interface |
mayur098 | 0:8f8e8f3cbd1c | 44 | * |
mayur098 | 0:8f8e8f3cbd1c | 45 | * Attempts to connect to a WiFi network. Requires ssid and passphrase to be set. |
mayur098 | 0:8f8e8f3cbd1c | 46 | * If passphrase is invalid, NSAPI_ERROR_AUTH_ERROR is returned. |
mayur098 | 0:8f8e8f3cbd1c | 47 | * |
mayur098 | 0:8f8e8f3cbd1c | 48 | * @return 0 on success, negative error code on failure |
mayur098 | 0:8f8e8f3cbd1c | 49 | */ |
mayur098 | 0:8f8e8f3cbd1c | 50 | virtual int connect(); |
mayur098 | 0:8f8e8f3cbd1c | 51 | |
mayur098 | 0:8f8e8f3cbd1c | 52 | /** Start the interface |
mayur098 | 0:8f8e8f3cbd1c | 53 | * |
mayur098 | 0:8f8e8f3cbd1c | 54 | * Attempts to connect to a WiFi network. |
mayur098 | 0:8f8e8f3cbd1c | 55 | * |
mayur098 | 0:8f8e8f3cbd1c | 56 | * @param ssid Name of the network to connect to |
mayur098 | 0:8f8e8f3cbd1c | 57 | * @param pass Security passphrase to connect to the network |
mayur098 | 0:8f8e8f3cbd1c | 58 | * @param security Type of encryption for connection (Default: NSAPI_SECURITY_NONE) |
mayur098 | 0:8f8e8f3cbd1c | 59 | * @param channel This parameter is not supported, setting it to anything else than 0 will result in NSAPI_ERROR_UNSUPPORTED |
mayur098 | 0:8f8e8f3cbd1c | 60 | * @return 0 on success, or error code on failure |
mayur098 | 0:8f8e8f3cbd1c | 61 | */ |
mayur098 | 0:8f8e8f3cbd1c | 62 | virtual int connect(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE, |
mayur098 | 0:8f8e8f3cbd1c | 63 | uint8_t channel = 0); |
mayur098 | 0:8f8e8f3cbd1c | 64 | |
mayur098 | 0:8f8e8f3cbd1c | 65 | /** Set the WiFi network credentials |
mayur098 | 0:8f8e8f3cbd1c | 66 | * |
mayur098 | 0:8f8e8f3cbd1c | 67 | * @param ssid Name of the network to connect to |
mayur098 | 0:8f8e8f3cbd1c | 68 | * @param pass Security passphrase to connect to the network |
mayur098 | 0:8f8e8f3cbd1c | 69 | * @param security Type of encryption for connection |
mayur098 | 0:8f8e8f3cbd1c | 70 | * (defaults to NSAPI_SECURITY_NONE) |
mayur098 | 0:8f8e8f3cbd1c | 71 | * @return 0 on success, or error code on failure |
mayur098 | 0:8f8e8f3cbd1c | 72 | */ |
mayur098 | 0:8f8e8f3cbd1c | 73 | virtual int set_credentials(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE); |
mayur098 | 0:8f8e8f3cbd1c | 74 | |
mayur098 | 0:8f8e8f3cbd1c | 75 | /** Set the WiFi network channel - NOT SUPPORTED |
mayur098 | 0:8f8e8f3cbd1c | 76 | * |
mayur098 | 0:8f8e8f3cbd1c | 77 | * This function is not supported and will return NSAPI_ERROR_UNSUPPORTED |
mayur098 | 0:8f8e8f3cbd1c | 78 | * |
mayur098 | 0:8f8e8f3cbd1c | 79 | * @param channel Channel on which the connection is to be made, or 0 for any (Default: 0) |
mayur098 | 0:8f8e8f3cbd1c | 80 | * @return Not supported, returns NSAPI_ERROR_UNSUPPORTED |
mayur098 | 0:8f8e8f3cbd1c | 81 | */ |
mayur098 | 0:8f8e8f3cbd1c | 82 | virtual int set_channel(uint8_t channel); |
mayur098 | 0:8f8e8f3cbd1c | 83 | |
mayur098 | 0:8f8e8f3cbd1c | 84 | /** Stop the interface |
mayur098 | 0:8f8e8f3cbd1c | 85 | * @return 0 on success, negative on failure |
mayur098 | 0:8f8e8f3cbd1c | 86 | */ |
mayur098 | 0:8f8e8f3cbd1c | 87 | virtual int disconnect(); |
mayur098 | 0:8f8e8f3cbd1c | 88 | |
mayur098 | 0:8f8e8f3cbd1c | 89 | /** Get the internally stored IP address |
mayur098 | 0:8f8e8f3cbd1c | 90 | * @return IP address of the interface or null if not yet connected |
mayur098 | 0:8f8e8f3cbd1c | 91 | */ |
mayur098 | 0:8f8e8f3cbd1c | 92 | virtual const char *get_ip_address(); |
mayur098 | 0:8f8e8f3cbd1c | 93 | |
mayur098 | 0:8f8e8f3cbd1c | 94 | /** Get the internally stored MAC address |
mayur098 | 0:8f8e8f3cbd1c | 95 | * @return MAC address of the interface |
mayur098 | 0:8f8e8f3cbd1c | 96 | */ |
mayur098 | 0:8f8e8f3cbd1c | 97 | virtual const char *get_mac_address(); |
mayur098 | 0:8f8e8f3cbd1c | 98 | |
mayur098 | 0:8f8e8f3cbd1c | 99 | /** Get the local gateway |
mayur098 | 0:8f8e8f3cbd1c | 100 | * |
mayur098 | 0:8f8e8f3cbd1c | 101 | * @return Null-terminated representation of the local gateway |
mayur098 | 0:8f8e8f3cbd1c | 102 | * or null if no network mask has been recieved |
mayur098 | 0:8f8e8f3cbd1c | 103 | */ |
mayur098 | 0:8f8e8f3cbd1c | 104 | virtual const char *get_gateway(); |
mayur098 | 0:8f8e8f3cbd1c | 105 | |
mayur098 | 0:8f8e8f3cbd1c | 106 | /** Get the local network mask |
mayur098 | 0:8f8e8f3cbd1c | 107 | * |
mayur098 | 0:8f8e8f3cbd1c | 108 | * @return Null-terminated representation of the local network mask |
mayur098 | 0:8f8e8f3cbd1c | 109 | * or null if no network mask has been recieved |
mayur098 | 0:8f8e8f3cbd1c | 110 | */ |
mayur098 | 0:8f8e8f3cbd1c | 111 | virtual const char *get_netmask(); |
mayur098 | 0:8f8e8f3cbd1c | 112 | |
mayur098 | 0:8f8e8f3cbd1c | 113 | /** Gets the current radio signal strength for active connection |
mayur098 | 0:8f8e8f3cbd1c | 114 | * |
mayur098 | 0:8f8e8f3cbd1c | 115 | * @return Connection strength in dBm (negative value) |
mayur098 | 0:8f8e8f3cbd1c | 116 | */ |
mayur098 | 0:8f8e8f3cbd1c | 117 | virtual int8_t get_rssi(); |
mayur098 | 0:8f8e8f3cbd1c | 118 | |
mayur098 | 0:8f8e8f3cbd1c | 119 | /** Scan for available networks |
mayur098 | 0:8f8e8f3cbd1c | 120 | * |
mayur098 | 0:8f8e8f3cbd1c | 121 | * This function will block. |
mayur098 | 0:8f8e8f3cbd1c | 122 | * |
mayur098 | 0:8f8e8f3cbd1c | 123 | * @param ap Pointer to allocated array to store discovered AP |
mayur098 | 0:8f8e8f3cbd1c | 124 | * @param count Size of allocated @a res array, or 0 to only count available AP |
mayur098 | 0:8f8e8f3cbd1c | 125 | * @param timeout Timeout in milliseconds; 0 for no timeout (Default: 0) |
mayur098 | 0:8f8e8f3cbd1c | 126 | * @return Number of entries in @a, or if @a count was 0 number of available networks, negative on error |
mayur098 | 0:8f8e8f3cbd1c | 127 | * see @a nsapi_error |
mayur098 | 0:8f8e8f3cbd1c | 128 | */ |
mayur098 | 0:8f8e8f3cbd1c | 129 | virtual int scan(WiFiAccessPoint *res, unsigned count); |
mayur098 | 0:8f8e8f3cbd1c | 130 | |
mayur098 | 0:8f8e8f3cbd1c | 131 | /** Translates a hostname to an IP address with specific version |
mayur098 | 0:8f8e8f3cbd1c | 132 | * |
mayur098 | 0:8f8e8f3cbd1c | 133 | * The hostname may be either a domain name or an IP address. If the |
mayur098 | 0:8f8e8f3cbd1c | 134 | * hostname is an IP address, no network transactions will be performed. |
mayur098 | 0:8f8e8f3cbd1c | 135 | * |
mayur098 | 0:8f8e8f3cbd1c | 136 | * If no stack-specific DNS resolution is provided, the hostname |
mayur098 | 0:8f8e8f3cbd1c | 137 | * will be resolve using a UDP socket on the stack. |
mayur098 | 0:8f8e8f3cbd1c | 138 | * |
mayur098 | 0:8f8e8f3cbd1c | 139 | * @param address Destination for the host SocketAddress |
mayur098 | 0:8f8e8f3cbd1c | 140 | * @param host Hostname to resolve |
mayur098 | 0:8f8e8f3cbd1c | 141 | * @param version IP version of address to resolve, NSAPI_UNSPEC indicates |
mayur098 | 0:8f8e8f3cbd1c | 142 | * version is chosen by the stack (defaults to NSAPI_UNSPEC) |
mayur098 | 0:8f8e8f3cbd1c | 143 | * @return 0 on success, negative error code on failure |
mayur098 | 0:8f8e8f3cbd1c | 144 | */ |
mayur098 | 0:8f8e8f3cbd1c | 145 | using NetworkInterface::gethostbyname; |
mayur098 | 0:8f8e8f3cbd1c | 146 | |
mayur098 | 0:8f8e8f3cbd1c | 147 | /** Add a domain name server to list of servers to query |
mayur098 | 0:8f8e8f3cbd1c | 148 | * |
mayur098 | 0:8f8e8f3cbd1c | 149 | * @param addr Destination for the host address |
mayur098 | 0:8f8e8f3cbd1c | 150 | * @return 0 on success, negative error code on failure |
mayur098 | 0:8f8e8f3cbd1c | 151 | */ |
mayur098 | 0:8f8e8f3cbd1c | 152 | using NetworkInterface::add_dns_server; |
mayur098 | 0:8f8e8f3cbd1c | 153 | |
mayur098 | 0:8f8e8f3cbd1c | 154 | /** Provide access to the NetworkStack object |
mayur098 | 0:8f8e8f3cbd1c | 155 | * |
mayur098 | 0:8f8e8f3cbd1c | 156 | * @return The underlying NetworkStack object |
mayur098 | 0:8f8e8f3cbd1c | 157 | */ |
mayur098 | 0:8f8e8f3cbd1c | 158 | virtual NetworkStack *get_stack() |
mayur098 | 0:8f8e8f3cbd1c | 159 | { |
mayur098 | 0:8f8e8f3cbd1c | 160 | return this; |
mayur098 | 0:8f8e8f3cbd1c | 161 | } |
mayur098 | 0:8f8e8f3cbd1c | 162 | |
mayur098 | 0:8f8e8f3cbd1c | 163 | private: |
mayur098 | 0:8f8e8f3cbd1c | 164 | char own_ssid[33]; /* 32 is what 802.11 defines as longest possible name; +1 for the \0 */ |
mayur098 | 0:8f8e8f3cbd1c | 165 | nsapi_security_t own_sec; |
mayur098 | 0:8f8e8f3cbd1c | 166 | char own_pass[64]; /* The longest allowed passphrase */ |
mayur098 | 0:8f8e8f3cbd1c | 167 | uint8_t own_ch; |
mayur098 | 0:8f8e8f3cbd1c | 168 | }; |
mayur098 | 0:8f8e8f3cbd1c | 169 | |
mayur098 | 0:8f8e8f3cbd1c | 170 | #endif |