|
|
|
@ -1,4 +1,10 @@
|
|
|
|
|
/*
|
|
|
|
|
* V6.0 - 20.11.2018- Verze i pro ESP32
|
|
|
|
|
*
|
|
|
|
|
* V5.9 - 18.7.2018 - Retezce, ukladane do EEPROM jsou primitivnim zpusobem sifrovany pro ztizeni odhaleni SSID a hesla pri dumpu pameti
|
|
|
|
|
*
|
|
|
|
|
* V5.8 - 5.7.2018 - Opraveno spatne zobrazovani sily signalu v prehledu WiFi siti (spatne barvy)
|
|
|
|
|
*
|
|
|
|
|
* V5.7 - 4.7.2018 - BugFix - staticka konfigurace ip adresy se neuplatnila - vypada to, jako kdyby SDK ukladalo SSID a Password, ale uz neuklada
|
|
|
|
|
* konfiguraci ip adres, takze je treba je vzdycky nastavit znovu
|
|
|
|
|
*
|
|
|
|
@ -63,9 +69,13 @@
|
|
|
|
|
* Prejit na pripojovani pomoci vnitrnich mechanizmu SDK ESP - zvysi se tim rychlost pripojeni kvuli bateriovym zarizenim
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "WiFiConfig.h"
|
|
|
|
|
#include <ESP8266WebServer.h>
|
|
|
|
|
#include <DNSServer.h>
|
|
|
|
|
#if defined(ESP8266)
|
|
|
|
|
#include <ESP8266WebServer.h>
|
|
|
|
|
#else
|
|
|
|
|
#include <WebServer.h>
|
|
|
|
|
#endif
|
|
|
|
|
#include "WiFiConfig.h"
|
|
|
|
|
#include <EEPROM.h>
|
|
|
|
|
#include <Ticker.h>
|
|
|
|
|
#include "embHTML.h"
|
|
|
|
@ -76,9 +86,15 @@
|
|
|
|
|
#define DEBUG_MSG(...)
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
#if defined(ESP8266)
|
|
|
|
|
extern "C" {
|
|
|
|
|
#include "user_interface.h"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#define ESP_getChipId() (ESP.getChipId())
|
|
|
|
|
#else
|
|
|
|
|
#include <esp_wifi.h>
|
|
|
|
|
#define ESP_getChipId() ((uint32_t)ESP.getEfuseMac())
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#define DNS_PORT 53
|
|
|
|
|
|
|
|
|
@ -99,8 +115,12 @@ enum
|
|
|
|
|
|
|
|
|
|
static int configBase; // musi byt trvale ulozene, aby fungovaly metody pro ziskani retezcu z EEPROM
|
|
|
|
|
|
|
|
|
|
static std::unique_ptr<DNSServer> dnsServer;
|
|
|
|
|
static std::unique_ptr<ESP8266WebServer> server;
|
|
|
|
|
static std::unique_ptr<DNSServer> dnsServer;
|
|
|
|
|
#if defined(ESP8266)
|
|
|
|
|
static std::unique_ptr<ESP8266WebServer> server;
|
|
|
|
|
#else
|
|
|
|
|
static std::unique_ptr<WebServer> server;
|
|
|
|
|
#endif
|
|
|
|
|
static std::unique_ptr<Ticker> rsttick;
|
|
|
|
|
|
|
|
|
|
const char VALUE[] PROGMEM = "value='";
|
|
|
|
@ -137,11 +157,11 @@ uint8_t * getOurMAC(uint8_t *mac)
|
|
|
|
|
return WiFi.softAPmacAddress(mac);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint32_t getEEPROMuint32(int start)
|
|
|
|
|
uint32_t getEEPROMuint32(unsigned int start)
|
|
|
|
|
{
|
|
|
|
|
uint32_t result = 0;
|
|
|
|
|
|
|
|
|
|
for (int i=0; i<4; ++i)
|
|
|
|
|
for (uint32_t i=0; i<4; ++i)
|
|
|
|
|
{
|
|
|
|
|
result <<= 8;
|
|
|
|
|
result += EEPROM.read(start);
|
|
|
|
@ -150,36 +170,36 @@ uint32_t getEEPROMuint32(int start)
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void setEEPROMuint32(int start, uint32_t val)
|
|
|
|
|
void setEEPROMuint32(unsigned int start, uint32_t val)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
for (int i=0; i<4; ++i)
|
|
|
|
|
for (unsigned int i=0; i<4; ++i)
|
|
|
|
|
{
|
|
|
|
|
EEPROM.write(start + 3 - i, (uint8_t)val);
|
|
|
|
|
val >>= 8;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String getEEPROMString(int start, int len)
|
|
|
|
|
String getEEPROMString(unsigned int start, size_t len)
|
|
|
|
|
{
|
|
|
|
|
String string = "";
|
|
|
|
|
|
|
|
|
|
for (int i = start; i < + start + len; ++i)
|
|
|
|
|
for (unsigned int i = start; i < + start + len; ++i)
|
|
|
|
|
{
|
|
|
|
|
uint8_t b = EEPROM.read(i);
|
|
|
|
|
|
|
|
|
|
if ((0xff == b) || (0 == b))
|
|
|
|
|
break;
|
|
|
|
|
string.concat(char(b));
|
|
|
|
|
string += char(b);
|
|
|
|
|
}
|
|
|
|
|
return string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void setEEPROMString(int start, int len, String string)
|
|
|
|
|
void setEEPROMString(unsigned int start, size_t len, String &string)
|
|
|
|
|
{
|
|
|
|
|
unsigned int si = 0;
|
|
|
|
|
|
|
|
|
|
for (int i = start; i < start + len; ++i)
|
|
|
|
|
for (unsigned int i = start; i < start + len; ++i)
|
|
|
|
|
{
|
|
|
|
|
char c;
|
|
|
|
|
|
|
|
|
@ -196,7 +216,7 @@ void setEEPROMString(int start, int len, String string)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
WiFiConfigUsrParameter::WiFiConfigUsrParameter(const char *id, const char *label, const char *defaultValue, int length, storeparam_cb cb)
|
|
|
|
|
WiFiConfigUsrParameter::WiFiConfigUsrParameter(const char *id, const char *label, const char *defaultValue, unsigned int length, storeparam_cb cb)
|
|
|
|
|
{
|
|
|
|
|
_next = NULL;
|
|
|
|
|
_cb = cb;
|
|
|
|
@ -204,7 +224,7 @@ WiFiConfigUsrParameter::WiFiConfigUsrParameter(const char *id, const char *label
|
|
|
|
|
_label = label;
|
|
|
|
|
_length = length;
|
|
|
|
|
_value = new char[length + 1];
|
|
|
|
|
for (int i = 0; i < length; i++)
|
|
|
|
|
for (unsigned int i = 0; i < length; i++)
|
|
|
|
|
{
|
|
|
|
|
_value[i] = 0;
|
|
|
|
|
}
|
|
|
|
@ -279,18 +299,16 @@ void WiFiConfig::addParameter(WiFiConfigUsrParameter *p)
|
|
|
|
|
void WiFiConfig::_handleNotFound(void)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
// _time = millis() + (_timeout * 1000); // spocitame si novy cas, kdy budeme modul restartovat
|
|
|
|
|
DEBUG_MSG("Requested URI: %s\r\n", server->uri().c_str());
|
|
|
|
|
|
|
|
|
|
if (server->uri().endsWith(String(F("favicon.ico"))))
|
|
|
|
|
{
|
|
|
|
|
server->send(404, F("text/plain"), F("Err"));
|
|
|
|
|
server->send_P(404, TEXTPLAIN, PSTR("Err"));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
server->sendHeader(F("Location"), String(F("http://")) + server->client().localIP().toString() + String(F("/index.htm")), true);
|
|
|
|
|
// server->sendHeader(F("Location"), String(F("http://")) + WiFi.softAPIP().toString() + String(F("/index.htm")), true);
|
|
|
|
|
server->send (302, F("text/plain"), F("Redirect"));
|
|
|
|
|
server->send_P(302, TEXTPLAIN, PSTR("Redirect"));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -318,20 +336,32 @@ void WiFiConfig::_handleInfo(void)
|
|
|
|
|
reply.concat(F("<form><table><TR><TH>System Info<TH>"));
|
|
|
|
|
|
|
|
|
|
reply.concat(F("<TR><TD>Core Version:<TD>"));
|
|
|
|
|
reply.concat(ESP.getCoreVersion());
|
|
|
|
|
#if defined(ESP8266)
|
|
|
|
|
reply.concat(ESP.getFullVersion());
|
|
|
|
|
#else
|
|
|
|
|
// TODO
|
|
|
|
|
reply.concat(ESP.getSdkVersion());
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
reply.concat(F("<TR><TD>Flash Size:<TD>"));
|
|
|
|
|
#if defined(ESP8266)
|
|
|
|
|
reply.concat(ESP.getFlashChipRealSize() / 1024);
|
|
|
|
|
#else
|
|
|
|
|
// TODO
|
|
|
|
|
reply.concat(ESP.getFlashChipSize());
|
|
|
|
|
#endif
|
|
|
|
|
reply.concat(F(" kB"));
|
|
|
|
|
|
|
|
|
|
#if defined(ESP8266)
|
|
|
|
|
reply.concat(F("<TR><TD>Sketch Size/Free:<TD>"));
|
|
|
|
|
reply.concat(ESP.getSketchSize() / 1024);
|
|
|
|
|
reply.concat(F(" kB / "));
|
|
|
|
|
reply.concat(ESP.getFreeSketchSpace() / 1024);
|
|
|
|
|
reply.concat(F(" kB"));
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
reply.concat(F("<TR><TD>STA MAC:<TD>"));
|
|
|
|
|
uint8_t mac[] = {0, 0, 0, 0, 0, 0};
|
|
|
|
|
uint8_t mac[6];
|
|
|
|
|
uint8_t* macread = WiFi.macAddress(mac);
|
|
|
|
|
char macaddress[20];
|
|
|
|
|
sprintf_P(macaddress, PSTR("%02x:%02x:%02x:%02x:%02x:%02x"), macread[0], macread[1], macread[2], macread[3], macread[4], macread[5]);
|
|
|
|
@ -343,15 +373,29 @@ void WiFiConfig::_handleInfo(void)
|
|
|
|
|
reply.concat(macaddress);
|
|
|
|
|
|
|
|
|
|
reply.concat(F("<TR><TD>ESP Chip ID:<TD>"));
|
|
|
|
|
sprintf_P(buff, PSTR("%06X"), ESP.getChipId());
|
|
|
|
|
sprintf_P(buff, PSTR("%06X"), ESP_getChipId());
|
|
|
|
|
reply.concat(buff);
|
|
|
|
|
|
|
|
|
|
#if defined(ESP8266)
|
|
|
|
|
reply.concat(F("<TR><TD>Flash Chip ID:<TD>"));
|
|
|
|
|
sprintf_P(buff, PSTR("%08X"), ESP.getFlashChipId());
|
|
|
|
|
reply.concat(buff);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
reply.concat(F("<TR><TD>Timeout:<TD>"));
|
|
|
|
|
reply.concat(_timeout);
|
|
|
|
|
reply.concat(F("/"));
|
|
|
|
|
reply.concat(_time);
|
|
|
|
|
|
|
|
|
|
#if defined(ESP8266)
|
|
|
|
|
uint8_t stations = wifi_softap_get_station_num();
|
|
|
|
|
|
|
|
|
|
reply.concat(F("<TR><TD>Clients:<TD>"));
|
|
|
|
|
reply.concat(stations);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
reply.concat(F("</table></form>"));
|
|
|
|
|
server->send(200, TEXTHTML, reply);
|
|
|
|
|
server->send(200, FPSTR(TEXTHTML), reply);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WiFiConfig::_handleRoot(void)
|
|
|
|
@ -362,6 +406,7 @@ void WiFiConfig::_handleRoot(void)
|
|
|
|
|
content = FPSTR(PAGE_CAPTIVEPORTALCATCH); // 1. cast stranky
|
|
|
|
|
// pridame informaci o stavu pokusu o pripojeni
|
|
|
|
|
content.concat(F("<div class=\"vl-info\">Pokus o připojení: "));
|
|
|
|
|
#if defined(ESP8266)
|
|
|
|
|
switch (_status)
|
|
|
|
|
{
|
|
|
|
|
case STATION_IDLE:
|
|
|
|
@ -397,12 +442,15 @@ void WiFiConfig::_handleRoot(void)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
content.concat(F("Neznámého"));
|
|
|
|
|
content.concat(F("Neznámý"));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
content.concat(F("Prozatím nedostupné"));
|
|
|
|
|
#endif
|
|
|
|
|
content.concat(F("</div>"));
|
|
|
|
|
content.concat(FPSTR(PAGE_CAPTIVEPORTALCATCH2)); // 2. cast stranky (ukonceni)
|
|
|
|
|
server->send(200, TEXTHTML, content);
|
|
|
|
|
server->send(200, FPSTR(TEXTHTML), content);
|
|
|
|
|
_time = millis() + (_timeout * 1000); // spocitame si novy cas, kdy budeme modul restartovat
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -436,22 +484,26 @@ void WiFiConfig::_handleDisplayAP(void)
|
|
|
|
|
s = FPSTR(SSID_ITEM);
|
|
|
|
|
s.replace(F("{v}"), WiFi.SSID(i));
|
|
|
|
|
s.replace(F("{a}"), String(quality));
|
|
|
|
|
#if defined(ESP8266)
|
|
|
|
|
s.replace(F("{s}"), (ENC_TYPE_NONE == WiFi.encryptionType(i)) ? F("") : F("l"));
|
|
|
|
|
#else
|
|
|
|
|
s.replace(F("{s}"), (WIFI_AUTH_OPEN == WiFi.encryptionType(i)) ? F("") : F("l"));
|
|
|
|
|
#endif
|
|
|
|
|
content.concat(s);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
s = FPSTR(PAGE_INDEX2);
|
|
|
|
|
v = getEEPROMString(configBase + offsetof(wificonfigarea_t, ssid), elementSize(wificonfigarea_t, ssid));
|
|
|
|
|
v = _readString(configBase + offsetof(wificonfigarea_t, ssid), elementSize(wificonfigarea_t, ssid));
|
|
|
|
|
if (v.length())
|
|
|
|
|
s.replace(F("{s}"), String(FPSTR(VALUE)) + v + F("'"));
|
|
|
|
|
else
|
|
|
|
|
s.replace(F("{s}"), F("placeholder='SSID'"));
|
|
|
|
|
v = getEEPROMString(configBase + offsetof(wificonfigarea_t, pass), elementSize(wificonfigarea_t, pass));
|
|
|
|
|
v = _readString(configBase + offsetof(wificonfigarea_t, pass), elementSize(wificonfigarea_t, pass));
|
|
|
|
|
if (v.length())
|
|
|
|
|
s.replace(F("{p}"), String(FPSTR(VALUE)) + v + F("'"));
|
|
|
|
|
else
|
|
|
|
|
s.replace(F("{p}"), F("placeholder='password'"));
|
|
|
|
|
v = getEEPROMString(configBase + offsetof(wificonfigarea_t, devname), elementSize(wificonfigarea_t, devname));
|
|
|
|
|
v = _readString(configBase + offsetof(wificonfigarea_t, devname), elementSize(wificonfigarea_t, devname));
|
|
|
|
|
if (v.length())
|
|
|
|
|
s.replace(F("{n}"), String(FPSTR(VALUE)) + v + F("'"));
|
|
|
|
|
else
|
|
|
|
@ -496,7 +548,7 @@ void WiFiConfig::_handleDisplayAP(void)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
content.concat(FPSTR(PAGE_END));
|
|
|
|
|
server->send(200, TEXTHTML, content);
|
|
|
|
|
server->send(200, FPSTR(TEXTHTML), content);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WiFiConfig::_handleSetAP(void)
|
|
|
|
@ -508,13 +560,13 @@ void WiFiConfig::_handleSetAP(void)
|
|
|
|
|
str = server->arg(F("_s"));
|
|
|
|
|
if (str.length() > 0)
|
|
|
|
|
{
|
|
|
|
|
setEEPROMString(configBase + offsetof(wificonfigarea_t, ssid), elementSize(wificonfigarea_t, ssid), str);
|
|
|
|
|
_storeString(configBase + offsetof(wificonfigarea_t, ssid), elementSize(wificonfigarea_t, ssid), str);
|
|
|
|
|
|
|
|
|
|
str = server->arg(F("_p"));
|
|
|
|
|
setEEPROMString(configBase + offsetof(wificonfigarea_t, pass), elementSize(wificonfigarea_t, pass), str);
|
|
|
|
|
_storeString(configBase + offsetof(wificonfigarea_t, pass), elementSize(wificonfigarea_t, pass), str);
|
|
|
|
|
|
|
|
|
|
str = server->arg(F("_n"));
|
|
|
|
|
setEEPROMString(configBase + offsetof(wificonfigarea_t, devname), elementSize(wificonfigarea_t, devname), str);
|
|
|
|
|
_storeString(configBase + offsetof(wificonfigarea_t, devname), elementSize(wificonfigarea_t, devname), str);
|
|
|
|
|
|
|
|
|
|
str = server->arg(F("_a"));
|
|
|
|
|
if (str.length() > 0)
|
|
|
|
@ -564,26 +616,28 @@ void WiFiConfig::_handleSetAP(void)
|
|
|
|
|
}
|
|
|
|
|
EEPROM.commit(); // skutecne ulozime data
|
|
|
|
|
}
|
|
|
|
|
server->send(200, F("text/html"), FPSTR(PAGE_SAVED));
|
|
|
|
|
server->send_P(200, TEXTHTML, PAGE_SAVED);
|
|
|
|
|
delay(2000); // cekame na odeslani dat
|
|
|
|
|
// nakonfigurujeme ESP dle nove nastavenych parametru
|
|
|
|
|
WiFi.disconnect(); // vsechno odpojime
|
|
|
|
|
WiFi.persistent(true); // chceme, aby si modul zapamatoval konfiguraci
|
|
|
|
|
|
|
|
|
|
String s = getEEPROMString(configBase + offsetof(wificonfigarea_t, devname), elementSize(wificonfigarea_t, devname));
|
|
|
|
|
String s = _readString(configBase + offsetof(wificonfigarea_t, devname), elementSize(wificonfigarea_t, devname));
|
|
|
|
|
strcpy(WiFiDeviceName, s.c_str());
|
|
|
|
|
s = getEEPROMString(configBase + offsetof(wificonfigarea_t, ssid), elementSize(wificonfigarea_t, ssid));
|
|
|
|
|
String pass = getEEPROMString(configBase + offsetof(wificonfigarea_t, pass), elementSize(wificonfigarea_t, pass));
|
|
|
|
|
s = _readString(configBase + offsetof(wificonfigarea_t, ssid), elementSize(wificonfigarea_t, ssid));
|
|
|
|
|
String pass = _readString(configBase + offsetof(wificonfigarea_t, pass), elementSize(wificonfigarea_t, pass));
|
|
|
|
|
|
|
|
|
|
switch (EEPROM.read(configBase + offsetof(wificonfigarea_t, mode)))
|
|
|
|
|
{
|
|
|
|
|
case WIFIMODE_STA:
|
|
|
|
|
{
|
|
|
|
|
DEBUG_MSG("STA mode.\r\n");
|
|
|
|
|
#if defined(ESP8266)
|
|
|
|
|
if (strlen(WiFiDeviceName))
|
|
|
|
|
{
|
|
|
|
|
WiFi.hostname(WiFiDeviceName); // nastavime jmeno zarizeni
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
if (IPCONFIG_STATIC == EEPROM.read(configBase + offsetof(wificonfigarea_t, ip)))
|
|
|
|
|
{
|
|
|
|
|
DEBUG_MSG("Static configuration.\r\n");
|
|
|
|
@ -593,8 +647,19 @@ void WiFiConfig::_handleSetAP(void)
|
|
|
|
|
}
|
|
|
|
|
WiFi.mode(WIFI_STA); // startujeme WiFi v rezimu klienta
|
|
|
|
|
WiFi.begin(s.c_str(), pass.c_str());
|
|
|
|
|
#if !defined(ESP8266)
|
|
|
|
|
if (strlen(WiFiDeviceName))
|
|
|
|
|
{
|
|
|
|
|
WiFi.setHostname(WiFiDeviceName); // nastavime jmeno zarizeni
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
#if defined(ESP8266)
|
|
|
|
|
wifi_station_set_auto_connect(true);
|
|
|
|
|
delay(1000);
|
|
|
|
|
#else
|
|
|
|
|
//esp_wifi_set_auto_connect(true);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
delay(1000);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
@ -634,7 +699,11 @@ wificonfigresult_t WiFiConfig::_setupAP(wificonfig_cb cb)
|
|
|
|
|
|
|
|
|
|
WiFi.persistent(false); // chranime flash pred zbytecnymi prepisy
|
|
|
|
|
dnsServer.reset(new DNSServer());
|
|
|
|
|
#if defined(ESP8266)
|
|
|
|
|
server.reset(new ESP8266WebServer(80));
|
|
|
|
|
#else
|
|
|
|
|
server.reset(new WebServer(80));
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/* Soft AP network parameters */
|
|
|
|
|
IPAddress apIP(192, 168, 4, 1);
|
|
|
|
@ -652,7 +721,7 @@ wificonfigresult_t WiFiConfig::_setupAP(wificonfig_cb cb)
|
|
|
|
|
//WiFi.softAPmacAddress(amac);
|
|
|
|
|
//sprintf_P(lmac, PSTR("%02X%02X%02X"), amac[3], amac[4], amac[5]);
|
|
|
|
|
|
|
|
|
|
sprintf_P(lmac, PSTR("%06X"), ESP.getChipId());
|
|
|
|
|
sprintf_P(lmac, PSTR("%06X"), ESP_getChipId());
|
|
|
|
|
ssid.replace(F("?"), String(lmac));
|
|
|
|
|
}
|
|
|
|
|
WiFi.softAP(ssid.c_str(), NULL, SETUP_CHANNEL);
|
|
|
|
@ -694,11 +763,142 @@ wificonfigresult_t WiFiConfig::_setupAP(wificonfig_cb cb)
|
|
|
|
|
return WCR_TIMEOUT; // nepripojeno, vyprsel timeout konfiguracniho AP
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String WiFiConfig::_readString(unsigned int start, size_t maxlen)
|
|
|
|
|
{
|
|
|
|
|
uint8_t key[4];
|
|
|
|
|
unsigned int enciphidx = 0;
|
|
|
|
|
String result;
|
|
|
|
|
unsigned int top = EEPROM.read(start);
|
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
|
|
result.reserve(maxlen);
|
|
|
|
|
uint32_t cid = ESP_getChipId();
|
|
|
|
|
uint32_t *pkey = (uint32_t *)&key[0];
|
|
|
|
|
*pkey = cid;
|
|
|
|
|
DEBUG_MSG("Key = %02X:%02X:%02X:%02X\r\n", key[0], key[1], key[2], key[3]);
|
|
|
|
|
if ((top > 0) && (top < (maxlen - 2)))
|
|
|
|
|
{
|
|
|
|
|
uint8_t b;
|
|
|
|
|
for (i = start + 1; i < + start + top + 1; ++i)
|
|
|
|
|
{
|
|
|
|
|
b = EEPROM.read(i);
|
|
|
|
|
|
|
|
|
|
b ^= key[enciphidx];
|
|
|
|
|
++enciphidx;
|
|
|
|
|
enciphidx %= sizeof(key);
|
|
|
|
|
|
|
|
|
|
result.concat(char(b));
|
|
|
|
|
}
|
|
|
|
|
b = EEPROM.read(i) ^ key[enciphidx];
|
|
|
|
|
if (b != 0)
|
|
|
|
|
{
|
|
|
|
|
DEBUG_MSG("_readString error! (%s)\r\n", result.c_str());
|
|
|
|
|
result = F(""); // spatna ukoncovaci nula - neplatny retezec
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool WiFiConfig::_storeString(unsigned int start, size_t maxlen, String &string)
|
|
|
|
|
{
|
|
|
|
|
uint8_t key[4];
|
|
|
|
|
unsigned int enciphidx = 0;
|
|
|
|
|
unsigned int si = 0;
|
|
|
|
|
unsigned int top;
|
|
|
|
|
char c;
|
|
|
|
|
bool result = false; // retezec nebyl ulozeny cely (nevesel s do bufferu)
|
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
|
|
if (string.length() > maxlen - 2)
|
|
|
|
|
top = maxlen - 2;
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
result = true; // retezec se do urceneho mista vejde
|
|
|
|
|
top = string.length();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint32_t cid = ESP_getChipId();
|
|
|
|
|
uint32_t *pkey = (uint32_t *)&key[0];
|
|
|
|
|
*pkey = cid;
|
|
|
|
|
DEBUG_MSG("Key = %02X:%02X:%02X:%02X\r\n", key[0], key[1], key[2], key[3]);
|
|
|
|
|
EEPROM.write(start, (uint8_t)top); // ulozime delku retezce (pouzite pr kontrolu pri vycitani)
|
|
|
|
|
for (i = start + 1; i < start + top + 1; ++i)
|
|
|
|
|
{
|
|
|
|
|
c = string[si];
|
|
|
|
|
c ^= key[enciphidx];
|
|
|
|
|
++enciphidx;
|
|
|
|
|
enciphidx %= sizeof(key);
|
|
|
|
|
EEPROM.write(i, c);
|
|
|
|
|
++si;
|
|
|
|
|
}
|
|
|
|
|
c = 0 ^ key[enciphidx];
|
|
|
|
|
EEPROM.write(i, c); // ukoncovaci a kontrolni nula
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Testovani, zda se modul pripojil k AP
|
|
|
|
|
bool WiFiConfig::_testWifi(wificonfig_cb cb)
|
|
|
|
|
{
|
|
|
|
|
uint32_t startt = millis();
|
|
|
|
|
|
|
|
|
|
#if !defined(ESP8266)
|
|
|
|
|
// nakonfigurujeme ESP dle nove nastavenych parametru
|
|
|
|
|
WiFi.disconnect(); // vsechno odpojime
|
|
|
|
|
|
|
|
|
|
String s = _readString(configBase + offsetof(wificonfigarea_t, devname), elementSize(wificonfigarea_t, devname));
|
|
|
|
|
strcpy(WiFiDeviceName, s.c_str());
|
|
|
|
|
s = _readString(configBase + offsetof(wificonfigarea_t, ssid), elementSize(wificonfigarea_t, ssid));
|
|
|
|
|
String pass = _readString(configBase + offsetof(wificonfigarea_t, pass), elementSize(wificonfigarea_t, pass));
|
|
|
|
|
|
|
|
|
|
switch (EEPROM.read(configBase + offsetof(wificonfigarea_t, mode)))
|
|
|
|
|
{
|
|
|
|
|
case WIFIMODE_STA:
|
|
|
|
|
{
|
|
|
|
|
DEBUG_MSG("STA mode.\r\n");
|
|
|
|
|
if (IPCONFIG_STATIC == EEPROM.read(configBase + offsetof(wificonfigarea_t, ip)))
|
|
|
|
|
{
|
|
|
|
|
DEBUG_MSG("Static configuration.\r\n");
|
|
|
|
|
WiFi.config(IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, ipaddr))), IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, gateway))),
|
|
|
|
|
IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, netmask))), IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, dns))));
|
|
|
|
|
delay(100);
|
|
|
|
|
}
|
|
|
|
|
WiFi.mode(WIFI_STA); // startujeme WiFi v rezimu klienta
|
|
|
|
|
WiFi.begin(s.c_str(), pass.c_str());
|
|
|
|
|
DEBUG_MSG("STA params: %s, %s\r\n", s.c_str(), pass.c_str());
|
|
|
|
|
if (strlen(WiFiDeviceName))
|
|
|
|
|
{
|
|
|
|
|
WiFi.setHostname(WiFiDeviceName); // nastavime jmeno zarizeni
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case WIFIMODE_AP:
|
|
|
|
|
DEBUG_MSG("AP mode.\r\n");
|
|
|
|
|
WiFi.mode(WIFI_AP); // startujeme AP
|
|
|
|
|
if (pass.length())
|
|
|
|
|
{
|
|
|
|
|
// je zadane heslo do AP
|
|
|
|
|
WiFi.softAP(s.c_str(), pass.c_str(), EEPROM.read(configBase + offsetof(wificonfigarea_t, apchannel)));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// otevreny AP
|
|
|
|
|
WiFi.softAP(s.c_str(), NULL, EEPROM.read(configBase + offsetof(wificonfigarea_t, apchannel)));
|
|
|
|
|
}
|
|
|
|
|
if (IPCONFIG_STATIC == EEPROM.read(configBase + offsetof(wificonfigarea_t, ip)))
|
|
|
|
|
{
|
|
|
|
|
WiFi.softAPConfig(IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, ipaddr))), IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, gateway))),
|
|
|
|
|
IPAddress(getEEPROMuint32(configBase + offsetof(wificonfigarea_t, netmask))));
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default: // jakykoliv neznamy rezim (mozna zavada na EEPROM???)
|
|
|
|
|
DEBUG_MSG("Mode Error!!\r\n");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
DEBUG_MSG("Trying to connect.\r\n");
|
|
|
|
|
while ((millis() - startt) < WIFI_STA_CONNECT_TIMEOUT)
|
|
|
|
|
{
|
|
|
|
@ -714,11 +914,15 @@ bool WiFiConfig::_testWifi(wificonfig_cb cb)
|
|
|
|
|
cb(WCS_CONNECTING); // signalizujeme pokracujici pokus o spojeni
|
|
|
|
|
}
|
|
|
|
|
DEBUG_MSG("Not connected!\r\n");
|
|
|
|
|
#if defined(ESP8266)
|
|
|
|
|
_status = wifi_station_get_connect_status();
|
|
|
|
|
#else
|
|
|
|
|
_status = WiFi.status();
|
|
|
|
|
#endif
|
|
|
|
|
return false; // pripojeni se nezdarilo
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wificonfigresult_t WiFiConfig::begin(int configarea, uint8_t forceConfigure, wificonfig_cb cb)
|
|
|
|
|
wificonfigresult_t WiFiConfig::begin(int configarea, int forceConfigure, wificonfig_cb cb)
|
|
|
|
|
{
|
|
|
|
|
wificonfigresult_t result = WCR_OK; // predpokladame, ze se pripojeni podari
|
|
|
|
|
|
|
|
|
@ -733,22 +937,28 @@ wificonfigresult_t WiFiConfig::begin(int configarea, uint8_t forceConfigure, wif
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
#if defined(ESP8266)
|
|
|
|
|
if (EEPROM.read(configBase + offsetof(wificonfigarea_t, mode)) != WiFi.getMode())
|
|
|
|
|
{ // neshoduje se rezim - musime spustit konfiguracni AP (poskozena konfigurace)
|
|
|
|
|
DEBUG_MSG("Wrong config\r\n");
|
|
|
|
|
DEBUG_MSG("Wrong config (%d, %d)\r\n", EEPROM.read(configBase + offsetof(wificonfigarea_t, mode)), WiFi.getMode());
|
|
|
|
|
_status = 0xfe;
|
|
|
|
|
result = _setupAP(cb);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
#endif
|
|
|
|
|
{
|
|
|
|
|
String s = getEEPROMString(configBase + offsetof(wificonfigarea_t, devname), elementSize(wificonfigarea_t, devname));
|
|
|
|
|
String s = _readString(configBase + offsetof(wificonfigarea_t, devname), elementSize(wificonfigarea_t, devname));
|
|
|
|
|
strcpy(WiFiDeviceName, s.c_str());
|
|
|
|
|
|
|
|
|
|
switch (EEPROM.read(configBase + offsetof(wificonfigarea_t, mode)))
|
|
|
|
|
{
|
|
|
|
|
case WIFIMODE_STA:
|
|
|
|
|
{
|
|
|
|
|
#if defined(ESP8266)
|
|
|
|
|
WiFi.hostname(WiFiDeviceName); // nastavime jmeno zarizeni
|
|
|
|
|
#else
|
|
|
|
|
WiFi.setHostname(WiFiDeviceName);
|
|
|
|
|
#endif
|
|
|
|
|
if (IPCONFIG_STATIC == EEPROM.read(configBase + offsetof(wificonfigarea_t, ip)))
|
|
|
|
|
{
|
|
|
|
|
DEBUG_MSG("Static configuration SET.\r\n");
|
|
|
|
@ -781,18 +991,14 @@ wificonfigresult_t WiFiConfig::begin(int configarea, uint8_t forceConfigure, wif
|
|
|
|
|
return result; // mame vyreseno
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wificonfigresult_t WiFiConfig::begin(int configarea, uint8_t forceConfigure, int timeout, wificonfig_cb cb = NULL)
|
|
|
|
|
wificonfigresult_t WiFiConfig::begin(int configarea, int forceConfigure, int timeout, wificonfig_cb cb = NULL)
|
|
|
|
|
{
|
|
|
|
|
if (forceConfigure)
|
|
|
|
|
{ // pouze pokud nemame vynucenou konfiguraci merime cas - to nam umozni dostat se ze spatne zadaneho timeoutu, ktery se neda zvladnout
|
|
|
|
|
if ((timeout > 0) && (40 > timeout))
|
|
|
|
|
timeout = 40; // timeout musi byt minimalne 40 sekund
|
|
|
|
|
_timeout = timeout;
|
|
|
|
|
if (_timeout > 0)
|
|
|
|
|
_time = millis() + (_timeout * 1000); // spocitame si novy cas, kdy budeme modul restartovat
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
_timeout = 0; // pri vynucene konfiguraci se parametr timeout neuplatni
|
|
|
|
|
|
|
|
|
|
if ((timeout > 0) && (40 > timeout))
|
|
|
|
|
timeout = 40; // timeout musi byt minimalne 40 sekund
|
|
|
|
|
_timeout = timeout;
|
|
|
|
|
if (_timeout > 0)
|
|
|
|
|
_time = millis() + (_timeout * 1000); // spocitame si novy cas, kdy budeme modul restartovat
|
|
|
|
|
return begin(configarea, forceConfigure, cb); // spustime WiFi
|
|
|
|
|
}
|
|
|
|
|
// EOF
|
|
|
|
|