diff --git a/src/WiFiConfig.cpp b/src/WiFiConfig.cpp index be6c72e..82b9421 100644 --- a/src/WiFiConfig.cpp +++ b/src/WiFiConfig.cpp @@ -164,36 +164,26 @@ void setEEPROMuint32(unsigned int start, uint32_t val) } } -String getEEPROMString(unsigned int start, size_t maxlen) +String getEEPROMString(unsigned int start, size_t len) { - uint8_t mac[WL_MAC_ADDR_LENGTH]; - unsigned int enciphidx = 0; String string = ""; - WiFi.softAPmacAddress(mac); - for (unsigned int i = start; i < + start + maxlen; ++i) + for (unsigned int i = start; i < + start + len; ++i) { uint8_t b = EEPROM.read(i); - b ^= mac[enciphidx]; - ++enciphidx; - enciphidx %= WL_MAC_ADDR_LENGTH; - if ((0xff == b) || (0 == b)) break; - string.concat(char(b)); + string += char(b); } return string; } -void setEEPROMString(unsigned int start, size_t maxlen, String &string) +void setEEPROMString(unsigned int start, size_t len, String &string) { - uint8_t mac[WL_MAC_ADDR_LENGTH]; - unsigned int enciphidx = 0; unsigned int si = 0; - WiFi.softAPmacAddress(mac); - for (unsigned int i = start; i < start + maxlen; ++i) + for (unsigned int i = start; i < start + len; ++i) { char c; @@ -205,9 +195,6 @@ void setEEPROMString(unsigned int start, size_t maxlen, String &string) { c = 0; } - c ^= mac[enciphidx]; - ++enciphidx; - enciphidx %= WL_MAC_ADDR_LENGTH; EEPROM.write(i, c); ++si; } @@ -456,17 +443,17 @@ void WiFiConfig::_handleDisplayAP(void) } } 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 @@ -523,13 +510,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) @@ -585,10 +572,10 @@ void WiFiConfig::_handleSetAP(void) 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))) { @@ -709,6 +696,71 @@ 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 mac[WL_MAC_ADDR_LENGTH]; + unsigned int enciphidx = 0; + String result; + unsigned int top = EEPROM.read(start); + unsigned int i; + + result.reserve(maxlen); + WiFi.softAPmacAddress(mac); + if ((top > 0) && (top < (maxlen - 2))) + { + uint8_t b; + for (i = start + 1; i < + start + top + 1; ++i) + { + b = EEPROM.read(i); + + b ^= mac[enciphidx]; + ++enciphidx; + enciphidx %= WL_MAC_ADDR_LENGTH; + + result.concat(char(b)); + } + b = EEPROM.read(i) ^ mac[enciphidx]; + if (b != 0) + result = F(""); // spatna ukoncovaci nula - neplatny retezec + } + return result; +} + +bool WiFiConfig::_storeString(unsigned int start, size_t maxlen, String &string) +{ + uint8_t mac[WL_MAC_ADDR_LENGTH]; + 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(); + } + + WiFi.softAPmacAddress(mac); + EEPROM.write(start, (uint8_t)top); // ulozime delku retezce (pouzite pr kontrolu pri vycitani) + for (i = start + 1; i < start + maxlen + 1; ++i) + { + c = string[si]; + c ^= mac[enciphidx]; + ++enciphidx; + enciphidx %= WL_MAC_ADDR_LENGTH; + EEPROM.write(i, c); + ++si; + } + c = 0 ^ mac[enciphidx]; + EEPROM.write(i, c); // ukoncovaci a kontrolni nula + + return result; +} + // Testovani, zda se modul pripojil k AP bool WiFiConfig::_testWifi(wificonfig_cb cb) { @@ -756,7 +808,7 @@ wificonfigresult_t WiFiConfig::begin(int configarea, uint8_t forceConfigure, wif } else { - 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))) diff --git a/src/WiFiConfig.h b/src/WiFiConfig.h index ad089c2..6dc2609 100644 --- a/src/WiFiConfig.h +++ b/src/WiFiConfig.h @@ -42,9 +42,9 @@ typedef struct { uint8_t mode; // rezim prace AP/STA uint8_t ip; // konfigurace ip (staticka/DHCP) - char ssid[32]; // SSID site - char pass[64]; // heslo - char devname[32]; // jmeno zarizeni (pro NBNS plati jen 16 znaku) + char ssid[32 + 2]; // SSID site + char pass[64 + 2]; // heslo + char devname[32 + 2]; // jmeno zarizeni (pro NBNS plati jen 16 znaku) uint32_t ipaddr; // ip adresa v pripade staticke konfigurace uint32_t netmask; // sitova maska v pripade staticke konfigurace uint32_t gateway; // sitova brana v pripade staticke konfigurace @@ -94,7 +94,7 @@ typedef void (*storeparam_cb)(const char *newvalue); // callback pro ulozeni uzi * \param [in] len Delka retezce * \return Vycteny retezec */ -String getEEPROMString(unsigned int start, size_t maxlen); +String getEEPROMString(unsigned int start, size_t len); /** * \brief Ulozeni retezce do EEPROM @@ -103,7 +103,7 @@ String getEEPROMString(unsigned int start, size_t maxlen); * \param [in] len Maximalni delka ulozeneho retezce * \param [in] string Ukladany retezec */ -void setEEPROMString(unsigned int start, size_t maxlen, String &string); +void setEEPROMString(unsigned int start, size_t len, String &string); /** * \brief Ziskani nasi ip adresy @@ -149,18 +149,18 @@ class WiFiConfigUsrParameter const char *getID(); const char *getValue(); const char *getLabel(); - int getValueLength(); - void setNext(WiFiConfigUsrParameter *n); + int getValueLength(); + void setNext(WiFiConfigUsrParameter *n); WiFiConfigUsrParameter *getNext(); - void setNewValue(const char *newval); + void setNewValue(const char *newval); private: const char *_id; const char *_label; char *_value; int _length; - storeparam_cb _cb; - WiFiConfigUsrParameter *_next; + storeparam_cb _cb; + WiFiConfigUsrParameter *_next; friend class WiFiConfig; }; @@ -171,7 +171,7 @@ public: WiFiConfig(): _params(NULL), _timeout(0) {} wificonfigresult_t begin(int configarea, uint8_t forceConfigure, wificonfig_cb cb); wificonfigresult_t begin(int configarea, uint8_t forceConfigure, int timeout, wificonfig_cb cb); - void addParameter(WiFiConfigUsrParameter *p); + void addParameter(WiFiConfigUsrParameter *p); private: wificonfigresult_t _setupAP(wificonfig_cb cb); void _handleDisplayAP(void); @@ -181,6 +181,8 @@ private: void _handleNotFound(); // CaptivePortal redirector void _handleRoot(); // jen jednoducha stranka kvuli CaptivePortalu umoznuje prejit na spravnou stranku (ale nedela to...) bool _testWifi(wificonfig_cb cb); + bool _storeString(unsigned int start, size_t maxlen, String &string); // sifrovany retezec, max 255 znaku + String _readString(unsigned int start, size_t maxlen); // sifrovany retezec, max. 255 znaku WiFiConfigUsrParameter *_searchUsrParameter(const char *name); WiFiConfigUsrParameter *_params; // ukazatel na posledni zadany uzivatelsky parametr