mayuresh bharmoria / Mbed OS mbed-os-example-wifi
Committer:
mayur098
Date:
Thu Jun 21 17:50:21 2018 +0000
Revision:
0:8f8e8f3cbd1c
first commit;

Who changed what in which revision?

UserRevisionLine numberNew 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_H
mayur098 0:8f8e8f3cbd1c 18 #define ESP32_INTERFACE_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 /** ESP32Interface class
mayur098 0:8f8e8f3cbd1c 24 * Implementation of the NetworkStack for the ESP32
mayur098 0:8f8e8f3cbd1c 25 */
mayur098 0:8f8e8f3cbd1c 26 class ESP32Interface : public ESP32Stack, public WiFiInterface
mayur098 0:8f8e8f3cbd1c 27 {
mayur098 0:8f8e8f3cbd1c 28 public:
mayur098 0:8f8e8f3cbd1c 29 /** ESP32Interface lifetime
mayur098 0:8f8e8f3cbd1c 30 * @param en EN pin
mayur098 0:8f8e8f3cbd1c 31 * @param io0 IO0 pin
mayur098 0:8f8e8f3cbd1c 32 * @param tx TX pin
mayur098 0:8f8e8f3cbd1c 33 * @param rx RX pin
mayur098 0:8f8e8f3cbd1c 34 * @param debug Enable debugging
mayur098 0:8f8e8f3cbd1c 35 * @param rts RTS pin
mayur098 0:8f8e8f3cbd1c 36 * @param cts CTS pin
mayur098 0:8f8e8f3cbd1c 37 * @param baudrate The baudrate of the serial port (default = 230400).
mayur098 0:8f8e8f3cbd1c 38 */
mayur098 0:8f8e8f3cbd1c 39 ESP32Interface(PinName en, PinName io0, PinName tx, PinName rx, bool debug = false,
mayur098 0:8f8e8f3cbd1c 40 PinName rts = NC, PinName cts = NC, int baudrate = 230400);
mayur098 0:8f8e8f3cbd1c 41
mayur098 0:8f8e8f3cbd1c 42 /** Start the interface
mayur098 0:8f8e8f3cbd1c 43 *
mayur098 0:8f8e8f3cbd1c 44 * Attempts to connect to a WiFi network. Requires ssid and passphrase to be set.
mayur098 0:8f8e8f3cbd1c 45 * If passphrase is invalid, NSAPI_ERROR_AUTH_ERROR is returned.
mayur098 0:8f8e8f3cbd1c 46 *
mayur098 0:8f8e8f3cbd1c 47 * @return 0 on success, negative error code on failure
mayur098 0:8f8e8f3cbd1c 48 */
mayur098 0:8f8e8f3cbd1c 49 virtual int connect();
mayur098 0:8f8e8f3cbd1c 50
mayur098 0:8f8e8f3cbd1c 51 /** Start the interface
mayur098 0:8f8e8f3cbd1c 52 *
mayur098 0:8f8e8f3cbd1c 53 * Attempts to connect to a WiFi network.
mayur098 0:8f8e8f3cbd1c 54 *
mayur098 0:8f8e8f3cbd1c 55 * @param ssid Name of the network to connect to
mayur098 0:8f8e8f3cbd1c 56 * @param pass Security passphrase to connect to the network
mayur098 0:8f8e8f3cbd1c 57 * @param security Type of encryption for connection (Default: NSAPI_SECURITY_NONE)
mayur098 0:8f8e8f3cbd1c 58 * @param channel This parameter is not supported, setting it to anything else than 0 will result in NSAPI_ERROR_UNSUPPORTED
mayur098 0:8f8e8f3cbd1c 59 * @return 0 on success, or error code on failure
mayur098 0:8f8e8f3cbd1c 60 */
mayur098 0:8f8e8f3cbd1c 61 virtual int connect(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE,
mayur098 0:8f8e8f3cbd1c 62 uint8_t channel = 0);
mayur098 0:8f8e8f3cbd1c 63
mayur098 0:8f8e8f3cbd1c 64 /** Set the WiFi network credentials
mayur098 0:8f8e8f3cbd1c 65 *
mayur098 0:8f8e8f3cbd1c 66 * @param ssid Name of the network to connect to
mayur098 0:8f8e8f3cbd1c 67 * @param pass Security passphrase to connect to the network
mayur098 0:8f8e8f3cbd1c 68 * @param security Type of encryption for connection
mayur098 0:8f8e8f3cbd1c 69 * (defaults to NSAPI_SECURITY_NONE)
mayur098 0:8f8e8f3cbd1c 70 * @return 0 on success, or error code on failure
mayur098 0:8f8e8f3cbd1c 71 */
mayur098 0:8f8e8f3cbd1c 72 virtual int set_credentials(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE);
mayur098 0:8f8e8f3cbd1c 73
mayur098 0:8f8e8f3cbd1c 74 /** Set the WiFi network channel - NOT SUPPORTED
mayur098 0:8f8e8f3cbd1c 75 *
mayur098 0:8f8e8f3cbd1c 76 * This function is not supported and will return NSAPI_ERROR_UNSUPPORTED
mayur098 0:8f8e8f3cbd1c 77 *
mayur098 0:8f8e8f3cbd1c 78 * @param channel Channel on which the connection is to be made, or 0 for any (Default: 0)
mayur098 0:8f8e8f3cbd1c 79 * @return Not supported, returns NSAPI_ERROR_UNSUPPORTED
mayur098 0:8f8e8f3cbd1c 80 */
mayur098 0:8f8e8f3cbd1c 81 virtual int set_channel(uint8_t channel);
mayur098 0:8f8e8f3cbd1c 82
mayur098 0:8f8e8f3cbd1c 83 /** Stop the interface
mayur098 0:8f8e8f3cbd1c 84 * @return 0 on success, negative on failure
mayur098 0:8f8e8f3cbd1c 85 */
mayur098 0:8f8e8f3cbd1c 86 virtual int disconnect();
mayur098 0:8f8e8f3cbd1c 87
mayur098 0:8f8e8f3cbd1c 88 /** Get the internally stored IP address
mayur098 0:8f8e8f3cbd1c 89 * @return IP address of the interface or null if not yet connected
mayur098 0:8f8e8f3cbd1c 90 */
mayur098 0:8f8e8f3cbd1c 91 virtual const char *get_ip_address();
mayur098 0:8f8e8f3cbd1c 92
mayur098 0:8f8e8f3cbd1c 93 /** Get the internally stored MAC address
mayur098 0:8f8e8f3cbd1c 94 * @return MAC address of the interface
mayur098 0:8f8e8f3cbd1c 95 */
mayur098 0:8f8e8f3cbd1c 96 virtual const char *get_mac_address();
mayur098 0:8f8e8f3cbd1c 97
mayur098 0:8f8e8f3cbd1c 98 /** Get the local gateway
mayur098 0:8f8e8f3cbd1c 99 *
mayur098 0:8f8e8f3cbd1c 100 * @return Null-terminated representation of the local gateway
mayur098 0:8f8e8f3cbd1c 101 * or null if no network mask has been recieved
mayur098 0:8f8e8f3cbd1c 102 */
mayur098 0:8f8e8f3cbd1c 103 virtual const char *get_gateway();
mayur098 0:8f8e8f3cbd1c 104
mayur098 0:8f8e8f3cbd1c 105 /** Get the local network mask
mayur098 0:8f8e8f3cbd1c 106 *
mayur098 0:8f8e8f3cbd1c 107 * @return Null-terminated representation of the local network mask
mayur098 0:8f8e8f3cbd1c 108 * or null if no network mask has been recieved
mayur098 0:8f8e8f3cbd1c 109 */
mayur098 0:8f8e8f3cbd1c 110 virtual const char *get_netmask();
mayur098 0:8f8e8f3cbd1c 111
mayur098 0:8f8e8f3cbd1c 112 /** Gets the current radio signal strength for active connection
mayur098 0:8f8e8f3cbd1c 113 *
mayur098 0:8f8e8f3cbd1c 114 * @return Connection strength in dBm (negative value)
mayur098 0:8f8e8f3cbd1c 115 */
mayur098 0:8f8e8f3cbd1c 116 virtual int8_t get_rssi();
mayur098 0:8f8e8f3cbd1c 117
mayur098 0:8f8e8f3cbd1c 118 /** Scan for available networks
mayur098 0:8f8e8f3cbd1c 119 *
mayur098 0:8f8e8f3cbd1c 120 * This function will block.
mayur098 0:8f8e8f3cbd1c 121 *
mayur098 0:8f8e8f3cbd1c 122 * @param ap Pointer to allocated array to store discovered AP
mayur098 0:8f8e8f3cbd1c 123 * @param count Size of allocated @a res array, or 0 to only count available AP
mayur098 0:8f8e8f3cbd1c 124 * @param timeout Timeout in milliseconds; 0 for no timeout (Default: 0)
mayur098 0:8f8e8f3cbd1c 125 * @return Number of entries in @a, or if @a count was 0 number of available networks, negative on error
mayur098 0:8f8e8f3cbd1c 126 * see @a nsapi_error
mayur098 0:8f8e8f3cbd1c 127 */
mayur098 0:8f8e8f3cbd1c 128 virtual int scan(WiFiAccessPoint *res, unsigned count);
mayur098 0:8f8e8f3cbd1c 129
mayur098 0:8f8e8f3cbd1c 130 /** Translates a hostname to an IP address with specific version
mayur098 0:8f8e8f3cbd1c 131 *
mayur098 0:8f8e8f3cbd1c 132 * The hostname may be either a domain name or an IP address. If the
mayur098 0:8f8e8f3cbd1c 133 * hostname is an IP address, no network transactions will be performed.
mayur098 0:8f8e8f3cbd1c 134 *
mayur098 0:8f8e8f3cbd1c 135 * If no stack-specific DNS resolution is provided, the hostname
mayur098 0:8f8e8f3cbd1c 136 * will be resolve using a UDP socket on the stack.
mayur098 0:8f8e8f3cbd1c 137 *
mayur098 0:8f8e8f3cbd1c 138 * @param address Destination for the host SocketAddress
mayur098 0:8f8e8f3cbd1c 139 * @param host Hostname to resolve
mayur098 0:8f8e8f3cbd1c 140 * @param version IP version of address to resolve, NSAPI_UNSPEC indicates
mayur098 0:8f8e8f3cbd1c 141 * version is chosen by the stack (defaults to NSAPI_UNSPEC)
mayur098 0:8f8e8f3cbd1c 142 * @return 0 on success, negative error code on failure
mayur098 0:8f8e8f3cbd1c 143 */
mayur098 0:8f8e8f3cbd1c 144 using NetworkInterface::gethostbyname;
mayur098 0:8f8e8f3cbd1c 145
mayur098 0:8f8e8f3cbd1c 146 /** Add a domain name server to list of servers to query
mayur098 0:8f8e8f3cbd1c 147 *
mayur098 0:8f8e8f3cbd1c 148 * @param addr Destination for the host address
mayur098 0:8f8e8f3cbd1c 149 * @return 0 on success, negative error code on failure
mayur098 0:8f8e8f3cbd1c 150 */
mayur098 0:8f8e8f3cbd1c 151 using NetworkInterface::add_dns_server;
mayur098 0:8f8e8f3cbd1c 152
mayur098 0:8f8e8f3cbd1c 153 /** Provide access to the NetworkStack object
mayur098 0:8f8e8f3cbd1c 154 *
mayur098 0:8f8e8f3cbd1c 155 * @return The underlying NetworkStack object
mayur098 0:8f8e8f3cbd1c 156 */
mayur098 0:8f8e8f3cbd1c 157 virtual NetworkStack *get_stack()
mayur098 0:8f8e8f3cbd1c 158 {
mayur098 0:8f8e8f3cbd1c 159 return this;
mayur098 0:8f8e8f3cbd1c 160 }
mayur098 0:8f8e8f3cbd1c 161
mayur098 0:8f8e8f3cbd1c 162 protected:
mayur098 0:8f8e8f3cbd1c 163 char ap_ssid[33]; /* 32 is what 802.11 defines as longest possible name; +1 for the \0 */
mayur098 0:8f8e8f3cbd1c 164 nsapi_security_t ap_sec;
mayur098 0:8f8e8f3cbd1c 165 uint8_t ap_ch;
mayur098 0:8f8e8f3cbd1c 166 char ap_pass[64]; /* The longest allowed passphrase */
mayur098 0:8f8e8f3cbd1c 167 };
mayur098 0:8f8e8f3cbd1c 168
mayur098 0:8f8e8f3cbd1c 169 #endif