From 766c7cd21c256359a4a10be73ee9f97c0b8d8d0d Mon Sep 17 00:00:00 2001 From: luc lebosse Date: Fri, 17 Apr 2015 17:13:40 +0800 Subject: [PATCH] Add missing function to change AP IP need to update ESP8266 files also - I pushed my code to ESP8266 github already to see if they implement it - or not --- esp8266/wifi.cpp | 2 +- .../libraries/ESP8266WiFi/src/ESP8266WiFi.cpp | 425 ++++++++++++++++++ .../libraries/ESP8266WiFi/src/ESP8266WiFi.h | 227 ++++++++++ 3 files changed, 653 insertions(+), 1 deletion(-) create mode 100644 modifiedFiles/esp8266com/esp8266/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp create mode 100644 modifiedFiles/esp8266com/esp8266/libraries/ESP8266WiFi/src/ESP8266WiFi.h diff --git a/esp8266/wifi.cpp b/esp8266/wifi.cpp index 931b5204..e6d08ecb 100644 --- a/esp8266/wifi.cpp +++ b/esp8266/wifi.cpp @@ -85,7 +85,7 @@ bool WIFI_CONFIG::Setup() //split in 4 parts split_ip (sbuf,ip); IPAddress subnet (ip[0],ip[1],ip[2],ip[3]); - WiFi.config(local_ip, gateway, subnet); + WiFi.configAP(local_ip, gateway, subnet); } return true; } diff --git a/modifiedFiles/esp8266com/esp8266/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp b/modifiedFiles/esp8266com/esp8266/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp new file mode 100644 index 00000000..9586d801 --- /dev/null +++ b/modifiedFiles/esp8266com/esp8266/libraries/ESP8266WiFi/src/ESP8266WiFi.cpp @@ -0,0 +1,425 @@ +/* + ESP8266WiFi.cpp - WiFi library for esp8266 + + Copyright (c) 2014 Ivan Grokhotkov. All rights reserved. + This file is part of the esp8266 core for Arduino environment. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include "ESP8266WiFi.h" +extern "C" { +#include "c_types.h" +#include "ets_sys.h" +#include "os_type.h" +#include "osapi.h" +#include "mem.h" +#include "user_interface.h" +#include "lwip/opt.h" +#include "lwip/err.h" +#include "lwip/dns.h" +} + + +extern "C" void esp_schedule(); +extern "C" void esp_yield(); + +ESP8266WiFiClass::ESP8266WiFiClass() +{ + +} + +void ESP8266WiFiClass::mode(WiFiMode m) +{ + ETS_UART_INTR_DISABLE(); + wifi_set_opmode(m); + ETS_UART_INTR_ENABLE(); +} + +int ESP8266WiFiClass::begin(const char* ssid) +{ + return begin(ssid, 0); +} + + +int ESP8266WiFiClass::begin(const char* ssid, const char *passphrase) +{ + if (wifi_get_opmode() == WIFI_AP) + { + // turn on AP+STA mode + mode(WIFI_AP_STA); + } + + struct station_config conf; + strcpy(reinterpret_cast(conf.ssid), ssid); + if (passphrase) + strcpy(reinterpret_cast(conf.password), passphrase); + else + *conf.password = 0; + + conf.bssid_set = 0; + + ETS_UART_INTR_DISABLE(); + wifi_station_set_config(&conf); + wifi_station_connect(); + ETS_UART_INTR_ENABLE(); + wifi_station_dhcpc_start(); + return status(); +} + +void ESP8266WiFiClass::config(IPAddress local_ip, IPAddress gateway, IPAddress subnet) +{ + struct ip_info info; + info.ip.addr = static_cast(local_ip); + info.gw.addr = static_cast(gateway); + info.netmask.addr = static_cast(subnet); + + wifi_station_dhcpc_stop(); + wifi_set_ip_info(STATION_IF, &info); +} + +void ESP8266WiFiClass::configAP(IPAddress local_ip, IPAddress gateway, IPAddress subnet) +{ + struct ip_info info; + info.ip.addr = static_cast(local_ip); + info.gw.addr = static_cast(gateway); + info.netmask.addr = static_cast(subnet); + wifi_softap_dhcps_stop(); + wifi_set_ip_info(SOFTAP_IF, &info); + wifi_softap_dhcps_start(); +} + +int ESP8266WiFiClass::disconnect() +{ + struct station_config conf; + *conf.ssid = 0; + *conf.password = 0; + ETS_UART_INTR_DISABLE(); + wifi_station_set_config(&conf); + wifi_station_disconnect(); + ETS_UART_INTR_ENABLE(); + return 0; +} + +void ESP8266WiFiClass::softAP(const char* ssid) +{ + softAP(ssid, 0); +} + + +void ESP8266WiFiClass::softAP(const char* ssid, const char* passphrase, int channel) +{ + if (wifi_get_opmode() == WIFI_STA) + { + // turn on AP+STA mode + mode(WIFI_AP_STA); + } + + struct softap_config conf; + wifi_softap_get_config(&conf); + strcpy(reinterpret_cast(conf.ssid), ssid); + conf.channel = channel; + conf.ssid_len = strlen(ssid); + conf.ssid_hidden = 0; + conf.max_connection = 4; + conf.beacon_interval = 100; + + if (!passphrase || strlen(passphrase) == 0) + { + conf.authmode = AUTH_OPEN; + *conf.password = 0; + } + else + { + conf.authmode = AUTH_WPA2_PSK; + strcpy(reinterpret_cast(conf.password), passphrase); + } + + ETS_UART_INTR_DISABLE(); + wifi_softap_set_config(&conf); + ETS_UART_INTR_ENABLE(); +} + + +uint8_t* ESP8266WiFiClass::macAddress(uint8_t* mac) +{ + wifi_get_macaddr(STATION_IF, mac); + return mac; +} + +uint8_t* ESP8266WiFiClass::softAPmacAddress(uint8_t* mac) +{ + wifi_get_macaddr(SOFTAP_IF, mac); + return mac; +} + +IPAddress ESP8266WiFiClass::localIP() +{ + struct ip_info ip; + wifi_get_ip_info(STATION_IF, &ip); + return IPAddress(ip.ip.addr); +} + +IPAddress ESP8266WiFiClass::softAPIP() +{ + struct ip_info ip; + wifi_get_ip_info(SOFTAP_IF, &ip); + return IPAddress(ip.ip.addr); +} + +IPAddress ESP8266WiFiClass::subnetMask() +{ + struct ip_info ip; + wifi_get_ip_info(STATION_IF, &ip); + return IPAddress(ip.netmask.addr); +} + +IPAddress ESP8266WiFiClass::gatewayIP() +{ + struct ip_info ip; + wifi_get_ip_info(STATION_IF, &ip); + return IPAddress(ip.gw.addr); +} + +char* ESP8266WiFiClass::SSID() +{ + static struct station_config conf; + wifi_station_get_config(&conf); + return reinterpret_cast(conf.ssid); +} + +// uint8_t* ESP8266WiFiClass::BSSID(uint8_t* bssid) +// { +// uint8_t* _bssid = WiFiDrv::getCurrentBSSID(); +// memcpy(bssid, _bssid, WL_MAC_ADDR_LENGTH); +// return bssid; +// } + +// int32_t ESP8266WiFiClass::RSSI() +// { +// return WiFiDrv::getCurrentRSSI(); +// } + +// uint8_t ESP8266WiFiClass::encryptionType() +// { +// return WiFiDrv::getCurrentEncryptionType(); +// } + +extern "C" +{ + typedef STAILQ_HEAD(, bss_info) bss_info_head_t; +} + +void ESP8266WiFiClass::_scanDone(void* result, int status) +{ + if (status != OK) + { + ESP8266WiFiClass::_scanCount = 0; + ESP8266WiFiClass::_scanResult = 0; + } + else + { + + int i = 0; + bss_info_head_t* head = reinterpret_cast(result); + + for (bss_info* it = STAILQ_FIRST(head); it; it = STAILQ_NEXT(it, next), ++i); + ESP8266WiFiClass::_scanCount = i; + if (i == 0) + { + ESP8266WiFiClass::_scanResult = 0; + } + else + { + bss_info* copied_info = new bss_info[i]; + i = 0; + for (bss_info* it = STAILQ_FIRST(head); it; it = STAILQ_NEXT(it, next), ++i) + { + memcpy(copied_info + i, it, sizeof(bss_info)); + } + + ESP8266WiFiClass::_scanResult = copied_info; + } + + } + esp_schedule(); +} + + +int8_t ESP8266WiFiClass::scanNetworks() +{ + if (wifi_get_opmode() == WIFI_AP) + { + mode(WIFI_AP_STA); + } + int status = wifi_station_get_connect_status(); + if (status != STATION_GOT_IP && status != STATION_IDLE) + { + disconnect(); + } + + if (ESP8266WiFiClass::_scanResult) + { + delete[] reinterpret_cast(ESP8266WiFiClass::_scanResult); + ESP8266WiFiClass::_scanResult = 0; + ESP8266WiFiClass::_scanCount = 0; + } + + struct scan_config config; + config.ssid = 0; + config.bssid = 0; + config.channel = 0; + config.show_hidden = 0; + wifi_station_scan(&config, reinterpret_cast(&ESP8266WiFiClass::_scanDone)); + esp_yield(); + return ESP8266WiFiClass::_scanCount; +} + +void * ESP8266WiFiClass::_getScanInfoByIndex(int i) +{ + if (!ESP8266WiFiClass::_scanResult || i > ESP8266WiFiClass::_scanCount) + { + return 0; + } + + return reinterpret_cast(ESP8266WiFiClass::_scanResult) + i; +} + +const char* ESP8266WiFiClass::SSID(uint8_t i) +{ + struct bss_info* it = reinterpret_cast(_getScanInfoByIndex(i)); + if (!it) + return 0; + + return reinterpret_cast(it->ssid); +} + +int32_t ESP8266WiFiClass::RSSI(uint8_t i) +{ + struct bss_info* it = reinterpret_cast(_getScanInfoByIndex(i)); + if (!it) + return 0; + + return it->rssi; +} + +uint8_t ESP8266WiFiClass::encryptionType(uint8_t i) +{ + struct bss_info* it = reinterpret_cast(_getScanInfoByIndex(i)); + if (!it) + return -1; + + int authmode = it->authmode; + if (authmode == AUTH_OPEN) + return ENC_TYPE_NONE; + if (authmode == AUTH_WEP) + return ENC_TYPE_WEP; + if (authmode == AUTH_WPA_PSK) + return ENC_TYPE_TKIP; + if (authmode == AUTH_WPA2_PSK) + return ENC_TYPE_CCMP; + if (authmode == AUTH_WPA_WPA2_PSK) + return ENC_TYPE_AUTO; + return -1; +} + +uint8_t ESP8266WiFiClass::status() +{ + int status = wifi_station_get_connect_status(); + + if (status == STATION_GOT_IP) + return WL_CONNECTED; + else if (status == STATION_NO_AP_FOUND) + return WL_NO_SSID_AVAIL; + else if (status == STATION_CONNECT_FAIL || status == STATION_WRONG_PASSWORD) + return WL_CONNECT_FAILED; + else if (status == STATION_IDLE) + return WL_IDLE_STATUS; + else + return WL_DISCONNECTED; +} + +void wifi_dns_found_callback(const char *name, ip_addr_t *ipaddr, void *callback_arg) +{ + if (ipaddr) + (*reinterpret_cast(callback_arg)) = ipaddr->addr; + esp_schedule(); // resume the hostByName function +} + +int ESP8266WiFiClass::hostByName(const char* aHostname, IPAddress& aResult) +{ + ip_addr_t addr; + aResult = static_cast(0); + err_t err = dns_gethostbyname(aHostname, &addr, &wifi_dns_found_callback, &aResult); + if (err == ERR_OK) + { + aResult = addr.addr; + } + else if (err == ERR_INPROGRESS) + { + esp_yield(); + // will return here when dns_found_callback fires + } + + return (aResult != 0) ? 1 : 0; +} + +void ESP8266WiFiClass::printDiag(Print& p) +{ + const char* modes[] = {"NULL", "STA", "AP", "STA+AP"}; + p.print("Mode: "); + p.println(modes[wifi_get_opmode()]); + + const char* phymodes[] = {"", "B", "G", "N"}; + p.print("PHY mode: "); + p.println(phymodes[(int) wifi_get_phy_mode()]); + + p.print("Channel: "); + p.println(wifi_get_channel()); + + p.print("AP id: "); + p.println(wifi_station_get_current_ap_id()); + + p.print("Status: "); + p.println(wifi_station_get_connect_status()); + + p.print("Auto connect: "); + p.println(wifi_station_get_auto_connect()); + + static struct station_config conf; + wifi_station_get_config(&conf); + + const char* ssid = reinterpret_cast(conf.ssid); + p.print("SSID ("); + p.print(strlen(ssid)); + p.print("): "); + p.println(ssid); + + const char* passphrase = reinterpret_cast(conf.password); + p.print("Passphrase ("); + p.print(strlen(passphrase)); + p.print("): "); + p.println(passphrase); + + p.print("BSSID set: "); + p.println(conf.bssid_set); + +} + +size_t ESP8266WiFiClass::_scanCount = 0; +void* ESP8266WiFiClass::_scanResult = 0; + + +ESP8266WiFiClass WiFi; diff --git a/modifiedFiles/esp8266com/esp8266/libraries/ESP8266WiFi/src/ESP8266WiFi.h b/modifiedFiles/esp8266com/esp8266/libraries/ESP8266WiFi/src/ESP8266WiFi.h new file mode 100644 index 00000000..1935b29f --- /dev/null +++ b/modifiedFiles/esp8266com/esp8266/libraries/ESP8266WiFi/src/ESP8266WiFi.h @@ -0,0 +1,227 @@ +/* + ESP8266WiFi.h - esp8266 Wifi support. + Based on WiFi.h from Ardiono WiFi shield library. + Copyright (c) 2011-2014 Arduino. All right reserved. + Modified by Ivan Grokhotkov, December 2014 + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef WiFi_h +#define WiFi_h + +#include + +extern "C" { + #include "include/wl_definitions.h" +} + +#include "IPAddress.h" +#include "WiFiClient.h" +#include "WiFiServer.h" + +enum WiFiMode { WIFI_STA = 1, WIFI_AP = 2, WIFI_AP_STA = 3 }; + +class ESP8266WiFiClass +{ +public: + + ESP8266WiFiClass(); + + void mode(WiFiMode); + + /* Start Wifi connection for OPEN networks + * + * param ssid: Pointer to the SSID string. + */ + int begin(const char* ssid); + + /* Start Wifi connection with passphrase + * the most secure supported mode will be automatically selected + * + * param ssid: Pointer to the SSID string. + * param passphrase: Passphrase. Valid characters in a passphrase + * must be between ASCII 32-126 (decimal). + */ + int begin(const char* ssid, const char *passphrase); + + + /* Set up an open access point + * + * param ssid: Pointer to the SSID string. + */ + void softAP(const char* ssid); + + + /* Set up a WPA2-secured access point + * + * param ssid: Pointer to the SSID string. + * param passphrase: Pointer to passphrase, 8 characters min. + * param channel: WiFi channel number, 1 - 13. + */ + void softAP(const char* ssid, const char* passphrase, int channel = 1); + + + /* Change Ip configuration settings disabling the dhcp client + * + * param local_ip: Static ip configuration + * param gateway: Static gateway configuration + * param subnet: Static Subnet mask + */ + void config(IPAddress local_ip, IPAddress gateway, IPAddress subnet); + + /* Change AP Ip configuration settings + * + * param local_ip: Static ip configuration + * param gateway: Static gateway configuration + * param subnet: Static Subnet mask + */ + void configAP(IPAddress local_ip, IPAddress gateway, IPAddress subnet); + + /* + * Disconnect from the network + * + * return: one value of wl_status_t enum + */ + int disconnect(void); + + /* + * Get the station interface MAC address. + * + * return: pointer to uint8_t array with length WL_MAC_ADDR_LENGTH + */ + uint8_t* macAddress(uint8_t* mac); + + /* + * Get the softAP interface MAC address. + * + * return: pointer to uint8_t array with length WL_MAC_ADDR_LENGTH + */ + uint8_t* softAPmacAddress(uint8_t* mac); + + /* + * Get the station interface IP address. + * + * return: Ip address value + */ + IPAddress localIP(); + + /* + * Get the softAP interface IP address. + * + * return: Ip address value + */ + IPAddress softAPIP(); + + /* + * Get the interface subnet mask address. + * + * return: subnet mask address value + */ + IPAddress subnetMask(); + + /* + * Get the gateway ip address. + * + * return: gateway ip address value + */ + IPAddress gatewayIP(); + + /* + * Return the current SSID associated with the network + * + * return: ssid string + */ + char* SSID(); + + /* + * Return the current network RSSI. Note: this is just a stub, there is no way to + * get the RSSI in the Espressif SDK yet. + * + * return: RSSI value (currently 0) + */ + + int32_t RSSI() { return 0; } + + /* + * Start scan WiFi networks available + * + * return: Number of discovered networks + */ + int8_t scanNetworks(); + + /* + * Return the SSID discovered during the network scan. + * + * param networkItem: specify from which network item want to get the information + * + * return: ssid string of the specified item on the networks scanned list + */ + const char* SSID(uint8_t networkItem); + + /* + * Return the encryption type of the networks discovered during the scanNetworks + * + * param networkItem: specify from which network item want to get the information + * + * return: encryption type (enum wl_enc_type) of the specified item on the networks scanned list + */ + uint8_t encryptionType(uint8_t networkItem); + + /* + * Return the RSSI of the networks discovered during the scanNetworks + * + * param networkItem: specify from which network item want to get the information + * + * return: signed value of RSSI of the specified item on the networks scanned list + */ + int32_t RSSI(uint8_t networkItem); + + /* + * Return Connection status. + * + * return: one of the value defined in wl_status_t + */ + uint8_t status(); + + /* + * Resolve the given hostname to an IP address. + * param aHostname: Name to be resolved + * param aResult: IPAddress structure to store the returned IP address + * result: 1 if aIPAddrString was successfully converted to an IP address, + * else error code + */ + int hostByName(const char* aHostname, IPAddress& aResult); + + /* + * Output WiFi settings to an object derived from Print interface (like Serial). + * + */ + void printDiag(Print& dest); + + friend class WiFiClient; + friend class WiFiServer; + +protected: + static void _scanDone(void* result, int status); + void * _getScanInfoByIndex(int i); + static size_t _scanCount; + static void* _scanResult; + +}; + +extern ESP8266WiFiClass WiFi; + +#endif