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<