From f22e84a6db1f13c1c5fbb1a155409ca9b6748227 Mon Sep 17 00:00:00 2001 From: Andrey Klimov Date: Sun, 19 Sep 2021 23:32:34 +0300 Subject: [PATCH 1/3] begin refactoring persist, no_MQTT, nullCfg --- lighthub/config.cpp | 65 ++++++++++++++++++++++------------------ lighthub/config.h | 10 +++++++ lighthub/flashstream.h | 67 ++++++++++++++++++++++++++++++++---------- lighthub/item.cpp | 7 +++-- lighthub/main.cpp | 50 ++++++++++++++++++++++++------- lighthub/main.h | 1 + 6 files changed, 143 insertions(+), 57 deletions(-) diff --git a/lighthub/config.cpp b/lighthub/config.cpp index a18adf1..db677ae 100644 --- a/lighthub/config.cpp +++ b/lighthub/config.cpp @@ -4,11 +4,12 @@ bool systemConfig::isValidSysConf() { if (!stream) return false; - stream->open('r'); + openStream('r'); stream->seek(offsetof(systemConfigData,signature)); for (int i=0;iread()!=EEPROM_signature[i]) { + stream->close(); return false; } return true; @@ -18,7 +19,7 @@ bool systemConfig::isValidSysConf() bool systemConfig::getMAC() { if (!stream || !isValidSysConf()) return false; - stream->open('r'); + openStream('r'); stream->seek(offsetof(systemConfigData,mac)); bool isMacValid = false; @@ -26,26 +27,28 @@ bool systemConfig::isValidSysConf() mac[i] = stream->read(); if (mac[i] != 0 && mac[i] != 0xff) isMacValid = true; } + stream->close(); return isMacValid; } bool systemConfig::setMAC(macAddress& _mac) { if (!stream || !isValidSysConf()) return false; - stream->open('w'); + openStream('w'); stream->seek(offsetof(systemConfigData,mac)); stream->write ((const uint8_t *)&_mac,sizeof(_mac)); memcpy(mac, _mac, sizeof(mac)); - stream->flush(); + stream->close(); return true; } char * systemConfig::getMQTTpwd(char * buffer, uint16_t bufLen) { if (!stream || !isValidSysConf()) return NULL; - stream->open('r'); + openStream('r'); stream->seek(offsetof(systemConfigData,MQTTpwd)); - short bytes=stream->readBytesUntil(0,buffer,bufLen-1); + short bytes=stream->readBytesUntil(0,buffer,bufLen-1); + stream->close(); if (bytes) { buffer[bytes]=0; @@ -57,11 +60,11 @@ bool systemConfig::isValidSysConf() bool systemConfig::setMQTTpwd(char * pwd) { if (!stream || !isValidSysConf() || (strlen(pwd)>=sizeof(systemConfigData::MQTTpwd))) return false; - stream->open('w'); + openStream('w'); stream->seek(offsetof(systemConfigData,MQTTpwd)); stream->print(pwd); int bytes = stream->write((uint8_t)'\0'); - stream->flush(); + stream->close(); return bytes; } @@ -69,9 +72,10 @@ bool systemConfig::isValidSysConf() char * systemConfig::getOTApwd(char * buffer, uint16_t bufLen) { if (!stream || !isValidSysConf()) return NULL; - stream->open('r'); + openStream('r'); stream->seek(offsetof(systemConfigData,OTApwd)); short bytes=stream->readBytesUntil(0,buffer,bufLen-1); + stream->close(); if (bytes) { buffer[bytes]=0; @@ -83,11 +87,11 @@ bool systemConfig::isValidSysConf() bool systemConfig::setOTApwd(char * pwd) { if (!stream || !isValidSysConf() || (strlen(pwd)>=sizeof(systemConfigData::OTApwd))) return false; - stream->open('w'); + openStream('w'); stream->seek(offsetof(systemConfigData,OTApwd)); stream->print(pwd); int bytes = stream->write((uint8_t)'\0'); - stream->flush(); + stream->close(); return bytes; } @@ -95,9 +99,10 @@ bool systemConfig::isValidSysConf() char * systemConfig::getServer(char * buffer, uint16_t bufLen) { if (!stream || !isValidSysConf()) return NULL; - stream->open('r'); + openStream('r'); stream->seek(offsetof(systemConfigData,configURL)); short bytes=stream->readBytesUntil(0,buffer,bufLen-1); + stream->close(); if (bytes) { buffer[bytes]=0; @@ -109,11 +114,11 @@ bool systemConfig::isValidSysConf() bool systemConfig::setServer(char* url) { if (!stream || !isValidSysConf() || (strlen(url)>=sizeof(systemConfigData::configURL))) return false; - stream->open('w'); + openStream('w'); stream->seek(offsetof(systemConfigData,configURL)); stream->print(url); int bytes = stream->write((uint8_t)'\0'); - stream->flush(); + stream->close(); return bytes; } @@ -122,10 +127,11 @@ bool systemConfig::isValidSysConf() { uint32_t addr; if (!stream || !isValidSysConf()) return false; - stream->open('r'); + openStream('r'); stream->seek(offsetof(systemConfigData,ip)); stream->readBytes((uint8_t *) &addr,4); ip=addr; + stream->close(); return (ip[0] && ((ip[0] != 0xff) || (ip[1] != 0xff) || (ip[2] != 0xff) || (ip[3] != 0xff))); } @@ -133,30 +139,33 @@ bool systemConfig::isValidSysConf() { uint32_t addr; if (!stream || !isValidSysConf()) return false; - stream->open('r'); + openStream('r'); stream->seek(offsetof(systemConfigData,mask)); stream->readBytes((uint8_t *) &addr,4); mask=addr; + stream->close(); return (mask[0] && ((mask[0] != 0xff) || (mask[1] != 0xff) || (mask[2] != 0xff) || (mask[3] != 0xff))); } bool systemConfig::getDNS(IPAddress& dns) { uint32_t addr; if (!stream || !isValidSysConf()) return false; - stream->open('r'); + openStream('r'); stream->seek(offsetof(systemConfigData,dns)); stream->readBytes((uint8_t *) &addr,4); dns = addr; + stream->close(); return (dns[0] && ((dns[0] != 0xff) || (dns[1] != 0xff) || (dns[2] != 0xff) || (dns[3] != 0xff))); } bool systemConfig::getGW(IPAddress& gw) { uint32_t addr; if (!stream || !isValidSysConf()) return false; - stream->open('r'); + openStream('r'); stream->seek(offsetof(systemConfigData,gw)); stream->readBytes((uint8_t *) &addr,4); gw=addr; + stream->close(); return (gw[0] && ((gw[0] != 0xff) || (gw[1] != 0xff) || (gw[2] != 0xff) || (gw[3] != 0xff))); } @@ -164,30 +173,30 @@ bool systemConfig::isValidSysConf() bool systemConfig::setIP(IPAddress& ip) { uint32_t addr=ip; if (!stream || !isValidSysConf()) return false; - stream->open('r'); + openStream('r'); stream->seek(offsetof(systemConfigData,ip)); int bytes = stream->write((uint8_t *) &addr, 4); - stream->flush(); + stream->close(); return bytes; } bool systemConfig::setMask(IPAddress& mask) { uint32_t addr = mask; if (!stream || !isValidSysConf()) return false; - stream->open('w'); + openStream('w'); stream->seek(offsetof(systemConfigData,mask)); int bytes = stream->write((uint8_t *) &addr, 4); - stream->flush(); + stream->close(); return bytes; } bool systemConfig::setDNS(IPAddress& dns) { uint32_t addr = dns; if (!stream || !isValidSysConf()) return false; - stream->open('w'); + openStream('w'); stream->seek(offsetof(systemConfigData,dns)); int bytes = stream->write((uint8_t *) &addr, 4); - stream->flush(); + stream->close(); return bytes; } @@ -195,10 +204,10 @@ bool systemConfig::isValidSysConf() bool systemConfig::setGW(IPAddress& gw) { uint32_t addr = gw; if (!stream || !isValidSysConf()) return false; - stream->open('w'); + openStream('w'); stream->seek(offsetof(systemConfigData,gw)); int bytes = stream->write((uint8_t *) &addr, 4); - stream->flush(); + stream->close(); return bytes; } @@ -206,7 +215,7 @@ bool systemConfig::isValidSysConf() bool systemConfig::clear() { if (!stream) return false; - stream->open('w'); + openStream('w'); stream->seek(0); for (unsigned int i = 0; i < stream->getSize(); i++) { stream->write((uint8_t)'\0'); @@ -214,7 +223,7 @@ bool systemConfig::isValidSysConf() stream->seek(offsetof(systemConfigData,signature)); for (unsigned int i=0;iwrite(EEPROM_signature[i])); - stream->flush(); + stream->close(); return true; } diff --git a/lighthub/config.h b/lighthub/config.h index a861ad9..0dd9800 100644 --- a/lighthub/config.h +++ b/lighthub/config.h @@ -16,6 +16,7 @@ const char EEPROM_signature[] = EEPROM_SIGNATURE; #define SYSCONF_OFFSET 0 #define EEPROM_offset_NotAlligned SYSCONF_OFFSET+sizeof(systemConfigData) +#define SYSCONF_SIZE EEPROM_offsetJSON #define EEPROM_offsetJSON EEPROM_offset_NotAlligned + (4 -(EEPROM_offset_NotAlligned & 3)) //#define EEPROM_offsetJSON IFLASH_PAGE_SIZE #define EEPROM_FIX_PART_LEN EEPROM_offsetJSON-SYSCONF_OFFSET @@ -57,6 +58,15 @@ const char EEPROM_signature[] = EEPROM_SIGNATURE; class systemConfig { private: flashStream * stream; + int openStream(char mode = '\0') + { + #if defined(FS_STORAGE) + stream.open("/config.bin",mode); + #else + stream.open(EEPROM_offsetJSON,mode); + #endif + }; + public: macAddress mac; systemConfig() {stream=NULL;}; diff --git a/lighthub/flashstream.h b/lighthub/flashstream.h index de76770..d538594 100644 --- a/lighthub/flashstream.h +++ b/lighthub/flashstream.h @@ -44,21 +44,25 @@ extern NRFFlashStorage EEPROM; class seekableStream : public Stream { protected: -unsigned int streamSize; +unsigned int streamSize; +bool textMode; + public: seekableStream(unsigned int size):Stream(),streamSize(size) {}; unsigned int getSize() {return streamSize;} virtual unsigned int seek(unsigned int _pos = 0) = 0; +virtual int open(unsigned int _startPos=0, unsigned int _size=4096, char mode='\0') = 0; +virtual int open(String _filename, char mode='\0') = 0; +virtual void close() = 0; }; #if defined(FS_STORAGE) class flashStream : public seekableStream { private: - File fs; - String filename; - char openedMode; - +String filename; +char openedMode; +File fs; public: flashStream(String _filename):seekableStream(65535) { @@ -66,8 +70,12 @@ flashStream(String _filename):seekableStream(65535) openedMode='\0'; open('r'); } - - int open(char mode='\0') + virtual int open(String _filename, char mode='\0') + { + textMode = _filename suffix == txt or json + open file + }; + virtual int open(char mode='\0') { if (!mode && openedMode) mode=openedMode; if (!mode && !openedMode) mode='r'; @@ -97,7 +105,7 @@ flashStream(String _filename):seekableStream(65535) virtual int read() {open('r');return fs.read();}; virtual int peek() {open('r'); return fs.peek();}; virtual unsigned int seek(unsigned int _pos = 0){return open();fs.seek(_pos,SeekSet);}; - virtual void flush() {fs.flush(); open('r'); }; + virtual void close() {fs.flush(); open('r'); }; virtual size_t write(uint8_t ch) {open('w'); return fs.write(ch);}; using Print::write; void putEOF(){write (255);}; @@ -117,15 +125,44 @@ unsigned int startPos; public: flashStream(unsigned int _startPos=0, unsigned int _size=4096 ):seekableStream(_size) { - pos = _startPos; startPos = _startPos; + pos = 0; startPos = _startPos; }; void setSize(unsigned int _size) {streamSize=_size;}; - int open(bool write=false) {return 1;}; + + virtual int open(String _filename, char mode='\0') + { + if (_filename == "config.json") + { + pos = 0; + streamSize = _size; + startPos = EEPROM_offsetJSON; + textMode = true; + + return 1; + } + else if (_filename == "config.bin") + { + pos = 0; + startPos = SYSCONF_OFFSET; + streamSize = SYSCONF_SIZE; + textMode =false; + return 1; + } + else return 0; + }; + + virtual int open(unsigned int _startPos=0, unsigned int _size=4096 char mode='\0') + { + pos = 0; + startPos = _startPos; + streamSize = _size; + return 1; + }; virtual unsigned int seek(unsigned int _pos = 0) - { pos=min(startPos+_pos, startPos+streamSize); + { pos=min(_pos, streamSize); debugSerial<"<<(char)ch; #if defined(__AVR__) - EEPROM.update(pos++,(char)ch); + EEPROM.update(startPos+pos++,(char)ch); return 1; #elif defined(__SAM3X8E__) - return EEPROM.write(pos++,(char)ch); + return EEPROM.write(startPos+pos++,(char)ch); #else - EEPROM.write(pos++,(char)ch); + EEPROM.write(startPos+pos++,(char)ch); return 1; #endif @@ -180,7 +217,7 @@ flashStream(unsigned int _startPos=0, unsigned int _size=4096 ):seekableStream(_ virtual size_t write(const uint8_t *buffer, size_t size) override { //debugSerial<<("Write from:")<0) { timestampObj->valueint = millis()+cmd.getInt(); + timestampObj->type = aJson_Int; timestampObj->subtype=cmd.getCmd(); debugSerial<valueint<type == aJson_String) return element->valuestring; } debugSerial<1) infoSerial<1) free (outBuf); infoSerial< Date: Tue, 5 Oct 2021 03:44:31 +0300 Subject: [PATCH 2/3] interim (compiled) commit --- lighthub/config.h | 4 +- lighthub/flashstream.h | 116 +++++++++++++++++++++++--------------- lighthub/main.cpp | 69 ++++++++++------------- lighthub/seekablestream.h | 25 ++++++++ 4 files changed, 128 insertions(+), 86 deletions(-) create mode 100644 lighthub/seekablestream.h diff --git a/lighthub/config.h b/lighthub/config.h index 0dd9800..d315aa9 100644 --- a/lighthub/config.h +++ b/lighthub/config.h @@ -61,9 +61,9 @@ class systemConfig { int openStream(char mode = '\0') { #if defined(FS_STORAGE) - stream.open("/config.bin",mode); + stream->open("/config.bin",mode); #else - stream.open(EEPROM_offsetJSON,mode); + stream->open(EEPROM_offsetJSON,mode); #endif }; diff --git a/lighthub/flashstream.h b/lighthub/flashstream.h index d538594..d48c3da 100644 --- a/lighthub/flashstream.h +++ b/lighthub/flashstream.h @@ -3,6 +3,7 @@ #define _FLASHSTREAM_H_ #include +#include #if defined(FS_STORAGE) #include @@ -40,21 +41,9 @@ extern NRFFlashStorage EEPROM; #include #include +#include "seekablestream.h" -class seekableStream : public Stream -{ -protected: -unsigned int streamSize; -bool textMode; - -public: -seekableStream(unsigned int size):Stream(),streamSize(size) {}; -unsigned int getSize() {return streamSize;} -virtual unsigned int seek(unsigned int _pos = 0) = 0; -virtual int open(unsigned int _startPos=0, unsigned int _size=4096, char mode='\0') = 0; -virtual int open(String _filename, char mode='\0') = 0; -virtual void close() = 0; -}; +#define EOF 255 #if defined(FS_STORAGE) class flashStream : public seekableStream @@ -64,16 +53,43 @@ String filename; char openedMode; File fs; public: -flashStream(String _filename):seekableStream(65535) +flashStream():seekableStream(65535) { - filename=_filename; openedMode='\0'; - open('r'); - } - virtual int open(String _filename, char mode='\0') + //fs = 0; + filename = ""; + // open('r'); + }; + + virtual int open(String _filename, char mode) override { - textMode = _filename suffix == txt or json - open file + char modestr[2]; + modestr[0]=mode; modestr[1]=0; + filename=_filename; + + if (fs = SPIFFS.open(_filename,modestr)) + { + openedMode=mode; + //fs.seek(savedPos); + + if (_filename.endsWith(".json")) {contentType=HTTP_TEXT_JSON;textMode=true;} + else if (_filename.endsWith(".bin")) {contentType=HTTP_OCTET_STREAM;textMode=false;} + else if (_filename.endsWith(".txt")) {contentType=HTTP_TEXT_PLAIN;textMode=true;} + + + debugSerial<<("Opened/")<1) #else //JSONStream.open('w'); //JSONStream.seek(); - aJsonStream jsonEEPROMStream = aJsonStream(&JSONStream); + aJsonStream jsonEEPROMStream = aJsonStream(&sysConfStream); infoSerial< +#include + +class seekableStream : public Stream +{ +protected: +unsigned int streamSize; +bool textMode; +uint16_t contentType; + +public: +seekableStream(unsigned int size):Stream(),streamSize(size) {}; +unsigned int getSize() {return streamSize;} +virtual unsigned int seek(unsigned int _pos = 0) = 0; +//virtual int open(unsigned int _startPos=0, unsigned int _size=4096, char mode='\0') = 0; +virtual int open(String _filename, char mode) = 0; +virtual void close() = 0; +virtual uint16_t getContentType() {return contentType;}; +virtual void putEOF() {if (textMode) write (EOF);}; +}; + +#endif \ No newline at end of file From 74cea9c6e3ea6f59cba0d26c00f9d49264295c00 Mon Sep 17 00:00:00 2001 From: Andrey Klimov Date: Tue, 5 Oct 2021 22:46:57 +0300 Subject: [PATCH 3/3] Refactoring complete --- build-flags/build_flags_esp32-wifi | 9 +- lighthub/config.cpp | 29 +++- lighthub/config.h | 61 +------ lighthub/flashstream.cpp | 218 +++++++++++++++++++++++- lighthub/flashstream.h | 255 +++++------------------------ lighthub/main.cpp | 51 +++--- lighthub/seekablestream.h | 16 +- lighthub/systemconfigdata.h | 48 ++++++ 8 files changed, 375 insertions(+), 312 deletions(-) create mode 100644 lighthub/systemconfigdata.h diff --git a/build-flags/build_flags_esp32-wifi b/build-flags/build_flags_esp32-wifi index bf5603d..103fbcf 100644 --- a/build-flags/build_flags_esp32-wifi +++ b/build-flags/build_flags_esp32-wifi @@ -9,8 +9,9 @@ -DSYSLOG_ENABLE # - udp errors -DOTA -#-DARDUINO_OTA_MDNS_DISABLE -#-DMDNS_ENABLE - ArduinoMDNS didnt working +-DARDUINO_OTA_MDNS_DISABLE +#-DMDNS_ENABLE +#- ArduinoMDNS didnt working -DMCP23017 -DMODBUS_TX_PIN=13 @@ -42,4 +43,6 @@ #-DAUTOCONNECT_RECONNECT_WAITTIME=60 -DFS_STORAGE --DFS_PREPARE \ No newline at end of file +-DFS_PREPARE + +-D CORS=\"http://lazyhome.ru\" diff --git a/lighthub/config.cpp b/lighthub/config.cpp index db677ae..a6645a4 100644 --- a/lighthub/config.cpp +++ b/lighthub/config.cpp @@ -1,5 +1,14 @@ #include "config.h" +int systemConfig::openStream(char mode) + { + #if defined(FS_STORAGE) + stream->open("/config.bin",mode); + #else + stream->open(FN_CONFIG_BIN,mode); + #endif + stream->setSize(SYSCONF_SIZE); + }; bool systemConfig::isValidSysConf() { @@ -34,7 +43,7 @@ bool systemConfig::isValidSysConf() bool systemConfig::setMAC(macAddress& _mac) { if (!stream || !isValidSysConf()) return false; - openStream('w'); + openStream('a'); stream->seek(offsetof(systemConfigData,mac)); stream->write ((const uint8_t *)&_mac,sizeof(_mac)); memcpy(mac, _mac, sizeof(mac)); @@ -44,11 +53,15 @@ bool systemConfig::isValidSysConf() char * systemConfig::getMQTTpwd(char * buffer, uint16_t bufLen) { - if (!stream || !isValidSysConf()) return NULL; + if (!stream || !isValidSysConf()) return NULL; openStream('r'); stream->seek(offsetof(systemConfigData,MQTTpwd)); short bytes=stream->readBytesUntil(0,buffer,bufLen-1); stream->close(); + Serial.println("valid"); + Serial.println(offsetof(systemConfigData,MQTTpwd)); + Serial.println(bytes); + Serial.write(buffer,bytes); if (bytes) { buffer[bytes]=0; @@ -60,7 +73,7 @@ bool systemConfig::isValidSysConf() bool systemConfig::setMQTTpwd(char * pwd) { if (!stream || !isValidSysConf() || (strlen(pwd)>=sizeof(systemConfigData::MQTTpwd))) return false; - openStream('w'); + openStream('r'); stream->seek(offsetof(systemConfigData,MQTTpwd)); stream->print(pwd); int bytes = stream->write((uint8_t)'\0'); @@ -87,7 +100,7 @@ bool systemConfig::isValidSysConf() bool systemConfig::setOTApwd(char * pwd) { if (!stream || !isValidSysConf() || (strlen(pwd)>=sizeof(systemConfigData::OTApwd))) return false; - openStream('w'); + openStream('r'); stream->seek(offsetof(systemConfigData,OTApwd)); stream->print(pwd); int bytes = stream->write((uint8_t)'\0'); @@ -114,7 +127,7 @@ bool systemConfig::isValidSysConf() bool systemConfig::setServer(char* url) { if (!stream || !isValidSysConf() || (strlen(url)>=sizeof(systemConfigData::configURL))) return false; - openStream('w'); + openStream('r'); stream->seek(offsetof(systemConfigData,configURL)); stream->print(url); int bytes = stream->write((uint8_t)'\0'); @@ -183,7 +196,7 @@ bool systemConfig::isValidSysConf() bool systemConfig::setMask(IPAddress& mask) { uint32_t addr = mask; if (!stream || !isValidSysConf()) return false; - openStream('w'); + openStream('r'); stream->seek(offsetof(systemConfigData,mask)); int bytes = stream->write((uint8_t *) &addr, 4); stream->close(); @@ -193,7 +206,7 @@ bool systemConfig::isValidSysConf() bool systemConfig::setDNS(IPAddress& dns) { uint32_t addr = dns; if (!stream || !isValidSysConf()) return false; - openStream('w'); + openStream('r'); stream->seek(offsetof(systemConfigData,dns)); int bytes = stream->write((uint8_t *) &addr, 4); stream->close(); @@ -204,7 +217,7 @@ bool systemConfig::isValidSysConf() bool systemConfig::setGW(IPAddress& gw) { uint32_t addr = gw; if (!stream || !isValidSysConf()) return false; - openStream('w'); + openStream('r'); stream->seek(offsetof(systemConfigData,gw)); int bytes = stream->write((uint8_t *) &addr, 4); stream->close(); diff --git a/lighthub/config.h b/lighthub/config.h index d315aa9..e80997b 100644 --- a/lighthub/config.h +++ b/lighthub/config.h @@ -6,67 +6,14 @@ #include #include "flashstream.h" #include - -#define MAXFLASHSTR 32 -#define PWDFLASHSTR 16 -#define EEPROM_SIGNATURE "LHCF" -#define EEPROM_SIGNATURE_LENGTH 4 -const char EEPROM_signature[] = EEPROM_SIGNATURE; - -#define SYSCONF_OFFSET 0 - -#define EEPROM_offset_NotAlligned SYSCONF_OFFSET+sizeof(systemConfigData) -#define SYSCONF_SIZE EEPROM_offsetJSON -#define EEPROM_offsetJSON EEPROM_offset_NotAlligned + (4 -(EEPROM_offset_NotAlligned & 3)) -//#define EEPROM_offsetJSON IFLASH_PAGE_SIZE -#define EEPROM_FIX_PART_LEN EEPROM_offsetJSON-SYSCONF_OFFSET - - typedef char flashstr[MAXFLASHSTR]; - typedef char flashpwd[PWDFLASHSTR]; - typedef uint8_t macAddress[6]; - - #pragma pack(push, 1) - typedef struct - { - char signature[4]; - macAddress mac; //6 bytes - union { - uint16_t configFlags; - struct - { - uint8_t serialDebugLevel:4; - uint8_t syslogDebugLevel:4; - uint8_t notGetConfigFromHTTP:1; - uint8_t saveToFlash:1; - }; - }; - uint32_t ip; - uint32_t dns; - uint32_t gw; - uint32_t mask; - - flashstr configURL; - flashpwd MQTTpwd; - flashpwd OTApwd; - - uint16_t sysConfigHash; - uint16_t JSONHash; - - } systemConfigData; - #pragma (pop) +#include "systemconfigdata.h" + class systemConfig { private: flashStream * stream; - int openStream(char mode = '\0') - { - #if defined(FS_STORAGE) - stream->open("/config.bin",mode); - #else - stream->open(EEPROM_offsetJSON,mode); - #endif - }; - + int openStream(char mode = '\0'); + public: macAddress mac; systemConfig() {stream=NULL;}; diff --git a/lighthub/flashstream.cpp b/lighthub/flashstream.cpp index 9c96f7d..4952066 100644 --- a/lighthub/flashstream.cpp +++ b/lighthub/flashstream.cpp @@ -1,4 +1,39 @@ -#include +#include "flashstream.h" +#include "systemconfigdata.h" + +#include +#include + + + +#if defined(ARDUINO_ARCH_AVR) +#include +#endif + +#if defined(ESP32) && !defined(FS_STORAGE) +#include +#endif + +#if defined(ARDUINO_ARCH_ESP8266) +#include +#endif + + +#if defined(__SAM3X8E__) +#include +extern DueFlashStorage EEPROM; +#endif + +#ifdef NRF5 +#include //STUB +extern NRFFlashStorage EEPROM; +#endif + +#ifdef ARDUINO_ARCH_STM32 +#include //STUB +extern NRFFlashStorage EEPROM; +#endif + #if defined(__SAM3X8E__) DueFlashStorage EEPROM; @@ -10,4 +45,185 @@ NRFFlashStorage EEPROM; #ifdef ARDUINO_ARCH_STM32 NRFFlashStorage EEPROM; +#endif + +#if defined(FS_STORAGE) + + + int flashStream::open(String _filename, char mode) + { + char modestr[4]; + modestr[0]=mode; modestr[1]='b'; modestr[2]='+'; modestr[3]='\0'; + if (fs) fs.close(); + filename=_filename; + + if (fs = SPIFFS.open(_filename,modestr)) + { + openedMode=mode; + + if (_filename.endsWith(".json")) {contentType=HTTP_TEXT_JSON;textMode=true;} + else if (_filename.endsWith(".bin")) {contentType=HTTP_OCTET_STREAM;textMode=false;} + else if (_filename.endsWith(".txt")) {contentType=HTTP_TEXT_PLAIN;textMode=true;} + else if (_filename.endsWith(".html")) {contentType=HTTP_TEXT_HTML;textMode=true;} + else if (_filename.endsWith(".gif")) {contentType=HTTP_IMAGE_GIF;textMode=false;} + else if (_filename.endsWith(".jpg")) {contentType=HTTP_IMAGE_JPEG;textMode=false;} + + + debugSerial<<(F("Opened ("))<streamSize) _pos=streamSize; + unsigned int res = fs.seek(_pos,SeekSet); + debugSerial<<(F(" Res:"))<"<<(char)ch; + #if defined(__AVR__) + EEPROM.update(startPos+pos++,(char)ch); + return 1; + #elif defined(__SAM3X8E__) + return EEPROM.write(startPos+pos++,(char)ch); + #else + EEPROM.write(startPos+pos++,(char)ch); + return 1; + #endif + }; + + #if defined(__SAM3X8E__) + size_t flashStream::write(const uint8_t *buffer, size_t size) + { + //debugSerial<<("Write from:")< -#include + + +#include +#include +#include "seekablestream.h" +//#include "config.h" #if defined(FS_STORAGE) #include #include #endif -#if defined(ARDUINO_ARCH_AVR) -#include -#endif +#define FN_CONFIG_JSON 1 +#define FN_CONFIG_BIN 2 -#if defined(ESP32) && !defined(FS_STORAGE) -#include -#endif - -#if defined(ARDUINO_ARCH_ESP8266) -#include -#endif - - -#if defined(__SAM3X8E__) -#include -extern DueFlashStorage EEPROM; -#endif - -#ifdef NRF5 -#include //STUB -extern NRFFlashStorage EEPROM; -#endif - -#ifdef ARDUINO_ARCH_STM32 -#include //STUB -extern NRFFlashStorage EEPROM; -#endif - - -#include -#include -#include "seekablestream.h" - -#define EOF 255 #if defined(FS_STORAGE) class flashStream : public seekableStream @@ -53,130 +26,36 @@ String filename; char openedMode; File fs; public: -flashStream():seekableStream(65535) + flashStream():seekableStream(65535) { openedMode='\0'; - //fs = 0; filename = ""; - // open('r'); - }; - - virtual int open(String _filename, char mode) override - { - char modestr[2]; - modestr[0]=mode; modestr[1]=0; - filename=_filename; - - if (fs = SPIFFS.open(_filename,modestr)) - { - openedMode=mode; - //fs.seek(savedPos); - - if (_filename.endsWith(".json")) {contentType=HTTP_TEXT_JSON;textMode=true;} - else if (_filename.endsWith(".bin")) {contentType=HTTP_OCTET_STREAM;textMode=false;} - else if (_filename.endsWith(".txt")) {contentType=HTTP_TEXT_PLAIN;textMode=true;} - - - debugSerial<<("Opened/")<"<<(char)ch; - #if defined(__AVR__) - EEPROM.update(startPos+pos++,(char)ch); - return 1; - #elif defined(__SAM3X8E__) - return EEPROM.write(startPos+pos++,(char)ch); - #else - EEPROM.write(startPos+pos++,(char)ch); - return 1; - #endif - }; + virtual unsigned int seek(unsigned int _pos = 0); + virtual int available() override; + virtual int read() ; + virtual int peek() ; + virtual void flush(); + virtual size_t write(uint8_t ch) ; - #if defined(__SAM3X8E__) - virtual size_t write(const uint8_t *buffer, size_t size) override - { - //debugSerial<<("Write from:")<child; -while (items && item) - { - if (item->type == aJson_Array && aJson.getArraySize(item)>0) +if (root && items) +{ + debugSerial<child; + while (item) { - Item it(item->name); - if (it.isValid()) it.Stop(); - yield(); + if (item->type == aJson_Array && aJson.getArraySize(item)>0) + { + Item it(item->name); + if (it.isValid()) it.Stop(); + yield(); + } + item = item->next; } - item = item->next; - } +} pollingItem = NULL; debugSerial<1) #if defined(FS_STORAGE) sysConfStream.open("/config.json",'w'); #else - sysConfStream.open(EEPROM_offsetJSON,'w'); + sysConfStream.open(FN_CONFIG_JSON,'w'); #endif #if defined(__SAM3X8E__) @@ -1281,8 +1287,8 @@ if (arg_cnt>1) aJson.print(root, &stringStream); int len = strlen(outBuf); outBuf[len++]= 255; - JSONStream.seek(); - size_t res = JSONStream.write((byte*) outBuf,len); + //JSONStream.seek(); + size_t res = sysConfStream.write((byte*) outBuf,len); free (outBuf); infoSerial<1) { - if (!strcasecmp_P(args[1],ON_P)) sysConf.setLoadHTTPConfig(true); - if (!strcasecmp_P(args[1],OFF_P)) sysConf.setLoadHTTPConfig(false); + if (!strcasecmp_P(args[1],ON_P)) {sysConf.setLoadHTTPConfig(true); return;}; + if (!strcasecmp_P(args[1],OFF_P)) {sysConf.setLoadHTTPConfig(false); return;}; + infoSerial< #include +#define EOFchar 255 + class seekableStream : public Stream { protected: unsigned int streamSize; -bool textMode; -uint16_t contentType; +bool textMode; +uint16_t contentType; public: seekableStream(unsigned int size):Stream(),streamSize(size) {}; -unsigned int getSize() {return streamSize;} +unsigned int getSize() {return streamSize;} +void setSize (unsigned int size) {streamSize = size;}; virtual unsigned int seek(unsigned int _pos = 0) = 0; -//virtual int open(unsigned int _startPos=0, unsigned int _size=4096, char mode='\0') = 0; -virtual int open(String _filename, char mode) = 0; -virtual void close() = 0; +virtual int open(String _filename, char mode) = 0; +virtual void close() = 0; virtual uint16_t getContentType() {return contentType;}; -virtual void putEOF() {if (textMode) write (EOF);}; +virtual void putEOF() {if (textMode) write (EOFchar);}; }; #endif \ No newline at end of file diff --git a/lighthub/systemconfigdata.h b/lighthub/systemconfigdata.h new file mode 100644 index 0000000..40aebb0 --- /dev/null +++ b/lighthub/systemconfigdata.h @@ -0,0 +1,48 @@ +#define SYSCONF_OFFSET 0 +#define EEPROM_offset_NotAlligned SYSCONF_OFFSET+sizeof(systemConfigData) +#define SYSCONF_SIZE EEPROM_offsetJSON +#define EEPROM_offsetJSON EEPROM_offset_NotAlligned + (4 -(EEPROM_offset_NotAlligned & 3)) + +#define MAXFLASHSTR 32 +#define PWDFLASHSTR 16 +#define EEPROM_SIGNATURE "LHCF" +#define EEPROM_SIGNATURE_LENGTH 4 + +//#define EEPROM_offsetJSON IFLASH_PAGE_SIZE +#define EEPROM_FIX_PART_LEN EEPROM_offsetJSON-SYSCONF_OFFSET + +const char EEPROM_signature[] = EEPROM_SIGNATURE; + + typedef char flashstr[MAXFLASHSTR]; + typedef char flashpwd[PWDFLASHSTR]; + typedef uint8_t macAddress[6]; + + #pragma pack(push, 1) + typedef struct + { + char signature[4]; + macAddress mac; //6 bytes + union { + uint16_t configFlags; + struct + { + uint8_t serialDebugLevel:4; + uint8_t syslogDebugLevel:4; + uint8_t notGetConfigFromHTTP:1; + uint8_t saveToFlash:1; + }; + }; + uint32_t ip; + uint32_t dns; + uint32_t gw; + uint32_t mask; + + flashstr configURL; + flashpwd MQTTpwd; + flashpwd OTApwd; + + uint16_t sysConfigHash; + uint16_t JSONHash; + + } systemConfigData; + #pragma (pop) \ No newline at end of file