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 a18adf1..a6645a4 100644 --- a/lighthub/config.cpp +++ b/lighthub/config.cpp @@ -1,14 +1,24 @@ #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() { 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 +28,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 +36,32 @@ 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('a'); 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'); + if (!stream || !isValidSysConf()) return NULL; + 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(); + Serial.println("valid"); + Serial.println(offsetof(systemConfigData,MQTTpwd)); + Serial.println(bytes); + Serial.write(buffer,bytes); if (bytes) { buffer[bytes]=0; @@ -57,11 +73,11 @@ bool systemConfig::isValidSysConf() bool systemConfig::setMQTTpwd(char * pwd) { if (!stream || !isValidSysConf() || (strlen(pwd)>=sizeof(systemConfigData::MQTTpwd))) return false; - stream->open('w'); + openStream('r'); stream->seek(offsetof(systemConfigData,MQTTpwd)); stream->print(pwd); int bytes = stream->write((uint8_t)'\0'); - stream->flush(); + stream->close(); return bytes; } @@ -69,9 +85,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 +100,11 @@ bool systemConfig::isValidSysConf() bool systemConfig::setOTApwd(char * pwd) { if (!stream || !isValidSysConf() || (strlen(pwd)>=sizeof(systemConfigData::OTApwd))) return false; - stream->open('w'); + openStream('r'); stream->seek(offsetof(systemConfigData,OTApwd)); stream->print(pwd); int bytes = stream->write((uint8_t)'\0'); - stream->flush(); + stream->close(); return bytes; } @@ -95,9 +112,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 +127,11 @@ bool systemConfig::isValidSysConf() bool systemConfig::setServer(char* url) { if (!stream || !isValidSysConf() || (strlen(url)>=sizeof(systemConfigData::configURL))) return false; - stream->open('w'); + openStream('r'); stream->seek(offsetof(systemConfigData,configURL)); stream->print(url); int bytes = stream->write((uint8_t)'\0'); - stream->flush(); + stream->close(); return bytes; } @@ -122,10 +140,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 +152,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 +186,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('r'); 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('r'); stream->seek(offsetof(systemConfigData,dns)); int bytes = stream->write((uint8_t *) &addr, 4); - stream->flush(); + stream->close(); return bytes; } @@ -195,10 +217,10 @@ bool systemConfig::isValidSysConf() bool systemConfig::setGW(IPAddress& gw) { uint32_t addr = gw; if (!stream || !isValidSysConf()) return false; - stream->open('w'); + openStream('r'); stream->seek(offsetof(systemConfigData,gw)); int bytes = stream->write((uint8_t *) &addr, 4); - stream->flush(); + stream->close(); return bytes; } @@ -206,7 +228,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 +236,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..e80997b 100644 --- a/lighthub/config.h +++ b/lighthub/config.h @@ -6,57 +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 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'); + 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 "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 - -class seekableStream : public Stream -{ -protected: -unsigned int streamSize; -public: -seekableStream(unsigned int size):Stream(),streamSize(size) {}; -unsigned int getSize() {return streamSize;} -virtual unsigned int seek(unsigned int _pos = 0) = 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) + flashStream():seekableStream(65535) { - filename=_filename; openedMode='\0'; - open('r'); - } - - int open(char mode='\0') - { - if (!mode && openedMode) mode=openedMode; - if (!mode && !openedMode) mode='r'; - if (openedMode!=mode) - { - char modestr[2]; - modestr[0]=mode;modestr[1]=0; - - unsigned int savedPos=fs.position(); - if (fs) fs.close(); - - if (fs = SPIFFS.open(filename,modestr)) - { - openedMode=mode; - fs.seek(savedPos); - debugSerial<<("Opened/")<"<<(char)ch; - #if defined(__AVR__) - EEPROM.update(pos++,(char)ch); - return 1; - #elif defined(__SAM3X8E__) - return EEPROM.write(pos++,(char)ch); - #else - EEPROM.write(pos++,(char)ch); - return 1; - #endif - - // #if defined(__SAM3X8E__) - // return EEPROM.write(pos++,(char)ch); - // #else - // EEPROM.update(pos++,(char)ch); - // return 1; - // #endif - // return 0; - }; + pos = 0; + startPos = _startPos; + streamSize = _size; + return 1; + }; +*/ + 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:")<0) { timestampObj->valueint = millis()+cmd.getInt(); + timestampObj->type = aJson_Int; timestampObj->subtype=cmd.getCmd(); debugSerial<valueint<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<type == aJson_String) return element->valuestring; } debugSerial<1) infoSerial<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; + +public: +seekableStream(unsigned int size):Stream(),streamSize(size) {}; +unsigned int getSize() {return streamSize;} +void setSize (unsigned int size) {streamSize = size;}; +virtual unsigned int seek(unsigned int _pos = 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 (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