mirror of
https://github.com/anklimov/lighthub
synced 2025-12-06 11:49:51 +03:00
Refactoring complete
This commit is contained in:
@@ -9,8 +9,9 @@
|
|||||||
-DSYSLOG_ENABLE
|
-DSYSLOG_ENABLE
|
||||||
# - udp errors
|
# - udp errors
|
||||||
-DOTA
|
-DOTA
|
||||||
#-DARDUINO_OTA_MDNS_DISABLE
|
-DARDUINO_OTA_MDNS_DISABLE
|
||||||
#-DMDNS_ENABLE - ArduinoMDNS didnt working
|
#-DMDNS_ENABLE
|
||||||
|
#- ArduinoMDNS didnt working
|
||||||
|
|
||||||
-DMCP23017
|
-DMCP23017
|
||||||
-DMODBUS_TX_PIN=13
|
-DMODBUS_TX_PIN=13
|
||||||
@@ -43,3 +44,5 @@
|
|||||||
|
|
||||||
-DFS_STORAGE
|
-DFS_STORAGE
|
||||||
-DFS_PREPARE
|
-DFS_PREPARE
|
||||||
|
|
||||||
|
-D CORS=\"http://lazyhome.ru\"
|
||||||
|
|||||||
@@ -1,5 +1,14 @@
|
|||||||
#include "config.h"
|
#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()
|
bool systemConfig::isValidSysConf()
|
||||||
{
|
{
|
||||||
@@ -34,7 +43,7 @@ bool systemConfig::isValidSysConf()
|
|||||||
bool systemConfig::setMAC(macAddress& _mac)
|
bool systemConfig::setMAC(macAddress& _mac)
|
||||||
{
|
{
|
||||||
if (!stream || !isValidSysConf()) return false;
|
if (!stream || !isValidSysConf()) return false;
|
||||||
openStream('w');
|
openStream('a');
|
||||||
stream->seek(offsetof(systemConfigData,mac));
|
stream->seek(offsetof(systemConfigData,mac));
|
||||||
stream->write ((const uint8_t *)&_mac,sizeof(_mac));
|
stream->write ((const uint8_t *)&_mac,sizeof(_mac));
|
||||||
memcpy(mac, _mac, sizeof(mac));
|
memcpy(mac, _mac, sizeof(mac));
|
||||||
@@ -49,6 +58,10 @@ bool systemConfig::isValidSysConf()
|
|||||||
stream->seek(offsetof(systemConfigData,MQTTpwd));
|
stream->seek(offsetof(systemConfigData,MQTTpwd));
|
||||||
short bytes=stream->readBytesUntil(0,buffer,bufLen-1);
|
short bytes=stream->readBytesUntil(0,buffer,bufLen-1);
|
||||||
stream->close();
|
stream->close();
|
||||||
|
Serial.println("valid");
|
||||||
|
Serial.println(offsetof(systemConfigData,MQTTpwd));
|
||||||
|
Serial.println(bytes);
|
||||||
|
Serial.write(buffer,bytes);
|
||||||
if (bytes)
|
if (bytes)
|
||||||
{
|
{
|
||||||
buffer[bytes]=0;
|
buffer[bytes]=0;
|
||||||
@@ -60,7 +73,7 @@ bool systemConfig::isValidSysConf()
|
|||||||
bool systemConfig::setMQTTpwd(char * pwd)
|
bool systemConfig::setMQTTpwd(char * pwd)
|
||||||
{
|
{
|
||||||
if (!stream || !isValidSysConf() || (strlen(pwd)>=sizeof(systemConfigData::MQTTpwd))) return false;
|
if (!stream || !isValidSysConf() || (strlen(pwd)>=sizeof(systemConfigData::MQTTpwd))) return false;
|
||||||
openStream('w');
|
openStream('r');
|
||||||
stream->seek(offsetof(systemConfigData,MQTTpwd));
|
stream->seek(offsetof(systemConfigData,MQTTpwd));
|
||||||
stream->print(pwd);
|
stream->print(pwd);
|
||||||
int bytes = stream->write((uint8_t)'\0');
|
int bytes = stream->write((uint8_t)'\0');
|
||||||
@@ -87,7 +100,7 @@ bool systemConfig::isValidSysConf()
|
|||||||
bool systemConfig::setOTApwd(char * pwd)
|
bool systemConfig::setOTApwd(char * pwd)
|
||||||
{
|
{
|
||||||
if (!stream || !isValidSysConf() || (strlen(pwd)>=sizeof(systemConfigData::OTApwd))) return false;
|
if (!stream || !isValidSysConf() || (strlen(pwd)>=sizeof(systemConfigData::OTApwd))) return false;
|
||||||
openStream('w');
|
openStream('r');
|
||||||
stream->seek(offsetof(systemConfigData,OTApwd));
|
stream->seek(offsetof(systemConfigData,OTApwd));
|
||||||
stream->print(pwd);
|
stream->print(pwd);
|
||||||
int bytes = stream->write((uint8_t)'\0');
|
int bytes = stream->write((uint8_t)'\0');
|
||||||
@@ -114,7 +127,7 @@ bool systemConfig::isValidSysConf()
|
|||||||
bool systemConfig::setServer(char* url)
|
bool systemConfig::setServer(char* url)
|
||||||
{
|
{
|
||||||
if (!stream || !isValidSysConf() || (strlen(url)>=sizeof(systemConfigData::configURL))) return false;
|
if (!stream || !isValidSysConf() || (strlen(url)>=sizeof(systemConfigData::configURL))) return false;
|
||||||
openStream('w');
|
openStream('r');
|
||||||
stream->seek(offsetof(systemConfigData,configURL));
|
stream->seek(offsetof(systemConfigData,configURL));
|
||||||
stream->print(url);
|
stream->print(url);
|
||||||
int bytes = stream->write((uint8_t)'\0');
|
int bytes = stream->write((uint8_t)'\0');
|
||||||
@@ -183,7 +196,7 @@ bool systemConfig::isValidSysConf()
|
|||||||
bool systemConfig::setMask(IPAddress& mask)
|
bool systemConfig::setMask(IPAddress& mask)
|
||||||
{ uint32_t addr = mask;
|
{ uint32_t addr = mask;
|
||||||
if (!stream || !isValidSysConf()) return false;
|
if (!stream || !isValidSysConf()) return false;
|
||||||
openStream('w');
|
openStream('r');
|
||||||
stream->seek(offsetof(systemConfigData,mask));
|
stream->seek(offsetof(systemConfigData,mask));
|
||||||
int bytes = stream->write((uint8_t *) &addr, 4);
|
int bytes = stream->write((uint8_t *) &addr, 4);
|
||||||
stream->close();
|
stream->close();
|
||||||
@@ -193,7 +206,7 @@ bool systemConfig::isValidSysConf()
|
|||||||
bool systemConfig::setDNS(IPAddress& dns)
|
bool systemConfig::setDNS(IPAddress& dns)
|
||||||
{ uint32_t addr = dns;
|
{ uint32_t addr = dns;
|
||||||
if (!stream || !isValidSysConf()) return false;
|
if (!stream || !isValidSysConf()) return false;
|
||||||
openStream('w');
|
openStream('r');
|
||||||
stream->seek(offsetof(systemConfigData,dns));
|
stream->seek(offsetof(systemConfigData,dns));
|
||||||
int bytes = stream->write((uint8_t *) &addr, 4);
|
int bytes = stream->write((uint8_t *) &addr, 4);
|
||||||
stream->close();
|
stream->close();
|
||||||
@@ -204,7 +217,7 @@ bool systemConfig::isValidSysConf()
|
|||||||
bool systemConfig::setGW(IPAddress& gw)
|
bool systemConfig::setGW(IPAddress& gw)
|
||||||
{ uint32_t addr = gw;
|
{ uint32_t addr = gw;
|
||||||
if (!stream || !isValidSysConf()) return false;
|
if (!stream || !isValidSysConf()) return false;
|
||||||
openStream('w');
|
openStream('r');
|
||||||
stream->seek(offsetof(systemConfigData,gw));
|
stream->seek(offsetof(systemConfigData,gw));
|
||||||
int bytes = stream->write((uint8_t *) &addr, 4);
|
int bytes = stream->write((uint8_t *) &addr, 4);
|
||||||
stream->close();
|
stream->close();
|
||||||
|
|||||||
@@ -6,66 +6,13 @@
|
|||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include "flashstream.h"
|
#include "flashstream.h"
|
||||||
#include <IPAddress.h>
|
#include <IPAddress.h>
|
||||||
|
#include "systemconfigdata.h"
|
||||||
|
|
||||||
#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)
|
|
||||||
|
|
||||||
class systemConfig {
|
class systemConfig {
|
||||||
private:
|
private:
|
||||||
flashStream * stream;
|
flashStream * stream;
|
||||||
int openStream(char mode = '\0')
|
int openStream(char mode = '\0');
|
||||||
{
|
|
||||||
#if defined(FS_STORAGE)
|
|
||||||
stream->open("/config.bin",mode);
|
|
||||||
#else
|
|
||||||
stream->open(EEPROM_offsetJSON,mode);
|
|
||||||
#endif
|
|
||||||
};
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
macAddress mac;
|
macAddress mac;
|
||||||
|
|||||||
@@ -1,4 +1,39 @@
|
|||||||
#include <flashstream.h>
|
#include "flashstream.h"
|
||||||
|
#include "systemconfigdata.h"
|
||||||
|
|
||||||
|
#include <main.h>
|
||||||
|
#include <WiFiOTA.h>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#if defined(ARDUINO_ARCH_AVR)
|
||||||
|
#include <EEPROM.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(ESP32) && !defined(FS_STORAGE)
|
||||||
|
#include <EEPROM.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(ARDUINO_ARCH_ESP8266)
|
||||||
|
#include <ESP_EEPROM.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#if defined(__SAM3X8E__)
|
||||||
|
#include <DueFlashStorage.h>
|
||||||
|
extern DueFlashStorage EEPROM;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef NRF5
|
||||||
|
#include <NRFFlashStorage.h> //STUB
|
||||||
|
extern NRFFlashStorage EEPROM;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef ARDUINO_ARCH_STM32
|
||||||
|
#include <NRFFlashStorage.h> //STUB
|
||||||
|
extern NRFFlashStorage EEPROM;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#if defined(__SAM3X8E__)
|
#if defined(__SAM3X8E__)
|
||||||
DueFlashStorage EEPROM;
|
DueFlashStorage EEPROM;
|
||||||
@@ -11,3 +46,184 @@ NRFFlashStorage EEPROM;
|
|||||||
#ifdef ARDUINO_ARCH_STM32
|
#ifdef ARDUINO_ARCH_STM32
|
||||||
NRFFlashStorage EEPROM;
|
NRFFlashStorage EEPROM;
|
||||||
#endif
|
#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 ("))<<modestr<<(F(")"))<<filename<<endl;
|
||||||
|
return fs;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
openedMode='\0';
|
||||||
|
debugSerial<<("Open error (")<<modestr<<(")")<<filename<<endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
int flashStream::available()
|
||||||
|
{
|
||||||
|
if (!fs) return 0;
|
||||||
|
if (textMode && peek()==EOFchar) return 0;
|
||||||
|
return fs.available();
|
||||||
|
};
|
||||||
|
|
||||||
|
int flashStream::read() {
|
||||||
|
return fs.read();
|
||||||
|
};
|
||||||
|
|
||||||
|
int flashStream::peek() {
|
||||||
|
return fs.peek();
|
||||||
|
};
|
||||||
|
|
||||||
|
unsigned int flashStream::seek (unsigned int _pos)
|
||||||
|
{
|
||||||
|
debugSerial<<(F("Seek:"))<<_pos;
|
||||||
|
if (_pos>streamSize) _pos=streamSize;
|
||||||
|
unsigned int res = fs.seek(_pos,SeekSet);
|
||||||
|
debugSerial<<(F(" Res:"))<<res<<endl;
|
||||||
|
return res;
|
||||||
|
};
|
||||||
|
void flashStream::close() {fs.close(); };
|
||||||
|
void flashStream::flush() {fs.flush(); };
|
||||||
|
size_t flashStream::write(uint8_t ch)
|
||||||
|
{
|
||||||
|
return fs.write(ch);
|
||||||
|
};
|
||||||
|
|
||||||
|
flashStream::~flashStream () {if (fs) fs.close();} ;
|
||||||
|
|
||||||
|
|
||||||
|
#else
|
||||||
|
void flashStream::setSize(unsigned int _size) {streamSize=_size;};
|
||||||
|
int flashStream::open(short fileNum, char mode)
|
||||||
|
{
|
||||||
|
|
||||||
|
switch (fileNum) {
|
||||||
|
case FN_CONFIG_JSON:
|
||||||
|
pos = 0;
|
||||||
|
//streamSize = _size;
|
||||||
|
startPos = EEPROM_offsetJSON;
|
||||||
|
textMode = true;
|
||||||
|
contentType = HTTP_TEXT_JSON;
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
case FN_CONFIG_BIN:
|
||||||
|
pos = 0;
|
||||||
|
startPos = SYSCONF_OFFSET;
|
||||||
|
streamSize = SYSCONF_SIZE;
|
||||||
|
textMode =false;
|
||||||
|
contentType = HTTP_OCTET_STREAM;
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
default:
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
int flashStream::open(String _filename, char mode)
|
||||||
|
{
|
||||||
|
if (_filename == "/config.json") return open (FN_CONFIG_JSON,mode);
|
||||||
|
else if (_filename == "/config.bin") return open (FN_CONFIG_BIN,mode);
|
||||||
|
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;
|
||||||
|
};
|
||||||
|
*/
|
||||||
|
unsigned int flashStream::seek(unsigned int _pos)
|
||||||
|
{ pos=min(_pos, streamSize);
|
||||||
|
debugSerial<<F("Seek:")<<pos<<endl;
|
||||||
|
return pos;
|
||||||
|
};
|
||||||
|
int flashStream::available() {
|
||||||
|
//debugSerial<<pos<<"%"<<streamSize<<";";
|
||||||
|
if (textMode && peek()==EOFchar) return 0;
|
||||||
|
return (pos<streamSize);
|
||||||
|
};
|
||||||
|
int flashStream::read()
|
||||||
|
{
|
||||||
|
int ch = peek();
|
||||||
|
pos++;
|
||||||
|
//debugSerial<<"<"<<(char)ch;
|
||||||
|
return ch;
|
||||||
|
};
|
||||||
|
int flashStream::peek()
|
||||||
|
{
|
||||||
|
if (pos<streamSize)
|
||||||
|
return EEPROM.read(pos);
|
||||||
|
else return -1;
|
||||||
|
};
|
||||||
|
void flashStream::flush() {
|
||||||
|
#if defined(ESP8266) || defined(ESP32)
|
||||||
|
if (EEPROM.commitReset())
|
||||||
|
infoSerial<<"Commited to FLASH"<<endl;
|
||||||
|
else errorSerial<<"Commit error. len:"<<EEPROM.length()<<endl;
|
||||||
|
#endif
|
||||||
|
};
|
||||||
|
|
||||||
|
size_t flashStream::write(uint8_t ch)
|
||||||
|
{
|
||||||
|
//debugSerial<<">"<<(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:")<<pos<<" "<<size<<" bytes"<<endl;
|
||||||
|
EEPROM.write(startPos+pos,(byte*)buffer,size);
|
||||||
|
pos+=size;
|
||||||
|
return size;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(ESP8266) || defined(ESP32)
|
||||||
|
void flashStream::putEOF()
|
||||||
|
{
|
||||||
|
write (255);
|
||||||
|
EEPROM.commit();
|
||||||
|
};
|
||||||
|
#endif
|
||||||
|
|
||||||
|
void flashStream::close()
|
||||||
|
{
|
||||||
|
flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -1,49 +1,22 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#ifndef _FLASHSTREAM_H_
|
//#ifndef _FLASHSTREAM_H_
|
||||||
#define _FLASHSTREAM_H_
|
//#define _FLASHSTREAM_H_
|
||||||
|
|
||||||
#include <main.h>
|
|
||||||
#include <WiFiOTA.h>
|
|
||||||
|
#include <Stream.h>
|
||||||
|
#include <Arduino.h>
|
||||||
|
#include "seekablestream.h"
|
||||||
|
//#include "config.h"
|
||||||
|
|
||||||
#if defined(FS_STORAGE)
|
#if defined(FS_STORAGE)
|
||||||
#include <FS.h>
|
#include <FS.h>
|
||||||
#include <SPIFFS.h>
|
#include <SPIFFS.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(ARDUINO_ARCH_AVR)
|
#define FN_CONFIG_JSON 1
|
||||||
#include <EEPROM.h>
|
#define FN_CONFIG_BIN 2
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(ESP32) && !defined(FS_STORAGE)
|
|
||||||
#include <EEPROM.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(ARDUINO_ARCH_ESP8266)
|
|
||||||
#include <ESP_EEPROM.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
#if defined(__SAM3X8E__)
|
|
||||||
#include <DueFlashStorage.h>
|
|
||||||
extern DueFlashStorage EEPROM;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef NRF5
|
|
||||||
#include <NRFFlashStorage.h> //STUB
|
|
||||||
extern NRFFlashStorage EEPROM;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef ARDUINO_ARCH_STM32
|
|
||||||
#include <NRFFlashStorage.h> //STUB
|
|
||||||
extern NRFFlashStorage EEPROM;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
#include <Stream.h>
|
|
||||||
#include <Arduino.h>
|
|
||||||
#include "seekablestream.h"
|
|
||||||
|
|
||||||
#define EOF 255
|
|
||||||
|
|
||||||
#if defined(FS_STORAGE)
|
#if defined(FS_STORAGE)
|
||||||
class flashStream : public seekableStream
|
class flashStream : public seekableStream
|
||||||
@@ -56,127 +29,33 @@ public:
|
|||||||
flashStream():seekableStream(65535)
|
flashStream():seekableStream(65535)
|
||||||
{
|
{
|
||||||
openedMode='\0';
|
openedMode='\0';
|
||||||
//fs = 0;
|
|
||||||
filename = "";
|
filename = "";
|
||||||
// open('r');
|
|
||||||
};
|
};
|
||||||
|
virtual int open(String _filename, char mode) override;
|
||||||
virtual int open(String _filename, char mode) override
|
virtual int available() override;
|
||||||
{
|
virtual int read();
|
||||||
char modestr[2];
|
virtual int peek();
|
||||||
modestr[0]=mode; modestr[1]=0;
|
virtual unsigned int seek (unsigned int _pos = 0) override;
|
||||||
filename=_filename;
|
virtual void close() override;
|
||||||
|
virtual void flush() override;
|
||||||
if (fs = SPIFFS.open(_filename,modestr))
|
virtual size_t write(uint8_t ch);
|
||||||
{
|
|
||||||
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/")<<modestr<<(":")<<filename<<endl;
|
|
||||||
return fs;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
openedMode='\0';
|
|
||||||
debugSerial<<("Open error/")<<modestr<<(":")<<filename<<endl;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
};
|
|
||||||
virtual 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/")<<modestr<<(":")<<filename<<endl;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
openedMode='\0';
|
|
||||||
debugSerial<<("Open error/")<<modestr<<(":")<<filename<<endl;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return fs;
|
|
||||||
}
|
|
||||||
virtual int available() override
|
|
||||||
{
|
|
||||||
if (!fs) return 0;
|
|
||||||
if (textMode && peek()==EOF) return 0;
|
|
||||||
return fs.available();
|
|
||||||
};
|
|
||||||
|
|
||||||
virtual int read() {open('r');return fs.read();};
|
|
||||||
virtual int peek() {open('r'); return fs.peek();};
|
|
||||||
virtual unsigned int seek (unsigned int _pos = 0) override {return open();fs.seek(_pos,SeekSet);};
|
|
||||||
virtual void close() override {fs.close(); };
|
|
||||||
virtual void flush() override {fs.flush(); };
|
|
||||||
virtual size_t write(uint8_t ch) {open('w'); return fs.write(ch);};
|
|
||||||
using Print::write;
|
using Print::write;
|
||||||
|
virtual ~flashStream ();
|
||||||
virtual ~flashStream () {if (fs) fs.close();} ;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
#define MAX_STREAM_SIZE 4096
|
||||||
|
|
||||||
class flashStream : public seekableStream
|
class flashStream : public seekableStream
|
||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
unsigned int pos;
|
unsigned int pos;
|
||||||
unsigned int startPos;
|
unsigned int startPos;
|
||||||
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
flashStream(unsigned int _size=4096 ):seekableStream(_size)
|
flashStream():seekableStream(MAX_STREAM_SIZE){};
|
||||||
{
|
void setSize(unsigned int _size);
|
||||||
// pos = 0; startPos = _startPos;
|
int open(short fileNum, char mode='\0') ;
|
||||||
};
|
virtual int open(String _filename, char mode='\0') override;
|
||||||
|
|
||||||
void setSize(unsigned int _size) {streamSize=_size;};
|
|
||||||
|
|
||||||
virtual int open(String _filename, char mode='\0')
|
|
||||||
{
|
|
||||||
if (_filename == "config.json")
|
|
||||||
{
|
|
||||||
pos = 0;
|
|
||||||
streamSize = _size;
|
|
||||||
startPos = EEPROM_offsetJSON;
|
|
||||||
textMode = true;
|
|
||||||
contentType = HTTP_TEXT_JSON;
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
else if (_filename == "config.bin")
|
|
||||||
{
|
|
||||||
pos = 0;
|
|
||||||
startPos = SYSCONF_OFFSET;
|
|
||||||
streamSize = SYSCONF_SIZE;
|
|
||||||
textMode =false;
|
|
||||||
contentType = HTTP_OCTET_STREAM;
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
else return 0;
|
|
||||||
};
|
|
||||||
/*
|
/*
|
||||||
virtual int open(unsigned int _startPos=0, unsigned int _size=4096 char mode='\0')
|
virtual int open(unsigned int _startPos=0, unsigned int _size=4096 char mode='\0')
|
||||||
{
|
{
|
||||||
@@ -186,77 +65,25 @@ flashStream(unsigned int _size=4096 ):seekableStream(_size)
|
|||||||
return 1;
|
return 1;
|
||||||
};
|
};
|
||||||
*/
|
*/
|
||||||
virtual unsigned int seek(unsigned int _pos = 0)
|
virtual unsigned int seek(unsigned int _pos = 0);
|
||||||
{ pos=min(_pos, streamSize);
|
virtual int available() override;
|
||||||
debugSerial<<F("Seek:")<<pos<<endl;
|
virtual int read() ;
|
||||||
return pos;
|
virtual int peek() ;
|
||||||
};
|
virtual void flush();
|
||||||
virtual int available() {
|
virtual size_t write(uint8_t ch) ;
|
||||||
//debugSerial<<pos<<"%"<<streamSize<<";";
|
|
||||||
if (textMode && peek()==EOF) return 0;
|
|
||||||
return (pos<streamSize);
|
|
||||||
};
|
|
||||||
virtual int read()
|
|
||||||
{
|
|
||||||
int ch = peek();
|
|
||||||
pos++;
|
|
||||||
//debugSerial<<"<"<<(char)ch;
|
|
||||||
return ch;
|
|
||||||
};
|
|
||||||
virtual int peek()
|
|
||||||
{
|
|
||||||
if (pos<streamSize)
|
|
||||||
return EEPROM.read(pos);
|
|
||||||
else return -1;
|
|
||||||
};
|
|
||||||
virtual void flush() {
|
|
||||||
#if defined(ESP8266) || defined(ESP32)
|
|
||||||
if (EEPROM.commitReset())
|
|
||||||
infoSerial<<"Commited to FLASH"<<endl;
|
|
||||||
else errorSerial<<"Commit error. len:"<<EEPROM.length()<<endl;
|
|
||||||
#endif
|
|
||||||
};
|
|
||||||
|
|
||||||
virtual size_t write(uint8_t ch)
|
|
||||||
{
|
|
||||||
//debugSerial<<">"<<(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__)
|
#if defined(__SAM3X8E__)
|
||||||
virtual size_t write(const uint8_t *buffer, size_t size) override
|
virtual size_t write(const uint8_t *buffer, size_t size) override;
|
||||||
{
|
|
||||||
//debugSerial<<("Write from:")<<pos<<" "<<size<<" bytes"<<endl;
|
|
||||||
EEPROM.write(startPos+pos,(byte*)buffer,size);
|
|
||||||
pos+=size;
|
|
||||||
return size;
|
|
||||||
};
|
|
||||||
#else
|
#else
|
||||||
using Print::write;//(const uint8_t *buffer, size_t size);
|
using Print::write;//(const uint8_t *buffer, size_t size);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(ESP8266) || defined(ESP32)
|
#if defined(ESP8266) || defined(ESP32)
|
||||||
virtual void putEOF() override
|
virtual void putEOF() override ;
|
||||||
{
|
#endif
|
||||||
write (255);
|
|
||||||
EEPROM.commit();
|
virtual void close() override;
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
virtual void close() override
|
//#endif
|
||||||
{
|
|
||||||
flush();
|
|
||||||
}
|
|
||||||
|
|
||||||
};
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -176,10 +176,12 @@ if (configLocked)
|
|||||||
errorSerial<<F("Not unlocked in 10s - continue ...")<<endl;
|
errorSerial<<F("Not unlocked in 10s - continue ...")<<endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (root && items)
|
||||||
|
{
|
||||||
debugSerial<<F("Stopping channels ...")<<endl;
|
debugSerial<<F("Stopping channels ...")<<endl;
|
||||||
//Stoping the channels
|
//Stoping the channels
|
||||||
aJsonObject * item = items->child;
|
aJsonObject * item = items->child;
|
||||||
while (items && item)
|
while (item)
|
||||||
{
|
{
|
||||||
if (item->type == aJson_Array && aJson.getArraySize(item)>0)
|
if (item->type == aJson_Array && aJson.getArraySize(item)>0)
|
||||||
{
|
{
|
||||||
@@ -190,6 +192,7 @@ while (items && item)
|
|||||||
}
|
}
|
||||||
item = item->next;
|
item = item->next;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
pollingItem = NULL;
|
pollingItem = NULL;
|
||||||
debugSerial<<F("Stopped")<<endl;
|
debugSerial<<F("Stopped")<<endl;
|
||||||
|
|
||||||
@@ -237,12 +240,15 @@ uint16_t httpHandler(Client& client, String request, long contentLength, bool au
|
|||||||
debugSerial<<request<<endl;
|
debugSerial<<request<<endl;
|
||||||
if (request == (F("GET /")))
|
if (request == (F("GET /")))
|
||||||
{
|
{
|
||||||
response = "Location: http://lazyhome.ru/pwa?mac=";
|
ArduinoOTA.sendHttpResponse(client,301,false); // Send only HTTP header, no close socket
|
||||||
|
client.println(F("Location: http://lazyhome.ru/pwa?mac="));
|
||||||
// todo for (int i=0; i<6; i++) {response+=(sysConf.mac[i]>>4,HEX);response+=(sysConf.mac[i]&0xf,HEX);}
|
// todo for (int i=0; i<6; i++) {response+=(sysConf.mac[i]>>4,HEX);response+=(sysConf.mac[i]&0xf,HEX);}
|
||||||
response+=(F("&ip="));
|
//response+=(F("&ip="));
|
||||||
//todo response+=(Ethernet.localIP());
|
//todo response+=(Ethernet.localIP());
|
||||||
|
client.println();
|
||||||
delay(100);
|
delay(100);
|
||||||
return 301;
|
client.stop();
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
if (!authorized) return 401;
|
if (!authorized) return 401;
|
||||||
|
|
||||||
@@ -405,7 +411,7 @@ if (element && element->type == aJson_String) return element->valuestring;
|
|||||||
}
|
}
|
||||||
|
|
||||||
debugSerial<<passwordBuf<<endl;
|
debugSerial<<passwordBuf<<endl;
|
||||||
ArduinoOTA.begin(Ethernet.localIP(), "Lighthub", passwordBuf, InternalStorage, sysConfStream);//, JSONStream);
|
ArduinoOTA.begin(Ethernet.localIP(), "Lighthub", passwordBuf, InternalStorage, sysConfStream);
|
||||||
ArduinoOTA.setCustomHandler(httpHandler);
|
ArduinoOTA.setCustomHandler(httpHandler);
|
||||||
infoSerial<<F("OTA initialized\n");
|
infoSerial<<F("OTA initialized\n");
|
||||||
|
|
||||||
@@ -1227,7 +1233,7 @@ int loadConfigFromEEPROM()
|
|||||||
#if defined(FS_STORAGE)
|
#if defined(FS_STORAGE)
|
||||||
sysConfStream.open("/config.json",'r');
|
sysConfStream.open("/config.json",'r');
|
||||||
#else
|
#else
|
||||||
sysConfStream.open(EEPROM_offsetJSON,'r');
|
sysConfStream.open(FN_CONFIG_JSON,'r');
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
//JSONStream.seek();
|
//JSONStream.seek();
|
||||||
@@ -1266,7 +1272,7 @@ if (arg_cnt>1)
|
|||||||
#if defined(FS_STORAGE)
|
#if defined(FS_STORAGE)
|
||||||
sysConfStream.open("/config.json",'w');
|
sysConfStream.open("/config.json",'w');
|
||||||
#else
|
#else
|
||||||
sysConfStream.open(EEPROM_offsetJSON,'w');
|
sysConfStream.open(FN_CONFIG_JSON,'w');
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(__SAM3X8E__)
|
#if defined(__SAM3X8E__)
|
||||||
@@ -1281,8 +1287,8 @@ if (arg_cnt>1)
|
|||||||
aJson.print(root, &stringStream);
|
aJson.print(root, &stringStream);
|
||||||
int len = strlen(outBuf);
|
int len = strlen(outBuf);
|
||||||
outBuf[len++]= 255;
|
outBuf[len++]= 255;
|
||||||
JSONStream.seek();
|
//JSONStream.seek();
|
||||||
size_t res = JSONStream.write((byte*) outBuf,len);
|
size_t res = sysConfStream.write((byte*) outBuf,len);
|
||||||
free (outBuf);
|
free (outBuf);
|
||||||
infoSerial<<res<< F("bytes from ")<<len<<F(" are saved to EEPROM")<<endl;
|
infoSerial<<res<< F("bytes from ")<<len<<F(" are saved to EEPROM")<<endl;
|
||||||
#else
|
#else
|
||||||
@@ -1404,10 +1410,11 @@ void cmdFunctionSetMac(int arg_cnt, char **args) {
|
|||||||
void cmdFunctionGet(int arg_cnt, char **args) {
|
void cmdFunctionGet(int arg_cnt, char **args) {
|
||||||
if (arg_cnt>1)
|
if (arg_cnt>1)
|
||||||
{
|
{
|
||||||
if (!strcasecmp_P(args[1],ON_P)) sysConf.setLoadHTTPConfig(true);
|
if (!strcasecmp_P(args[1],ON_P)) {sysConf.setLoadHTTPConfig(true); return;};
|
||||||
if (!strcasecmp_P(args[1],OFF_P)) sysConf.setLoadHTTPConfig(false);
|
if (!strcasecmp_P(args[1],OFF_P)) {sysConf.setLoadHTTPConfig(false); return;};
|
||||||
|
|
||||||
infoSerial<<F("Loading HTTP config on startup:")<<sysConf.getLoadHTTPConfig()<<endl;
|
infoSerial<<F("Loading HTTP config on startup:")<<sysConf.getLoadHTTPConfig()<<endl;
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
lanStatus= loadConfigFromHttp(arg_cnt, args);
|
lanStatus= loadConfigFromHttp(arg_cnt, args);
|
||||||
|
|||||||
@@ -4,6 +4,8 @@
|
|||||||
#include <Stream.h>
|
#include <Stream.h>
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
|
||||||
|
#define EOFchar 255
|
||||||
|
|
||||||
class seekableStream : public Stream
|
class seekableStream : public Stream
|
||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
@@ -14,12 +16,12 @@ uint16_t contentType;
|
|||||||
public:
|
public:
|
||||||
seekableStream(unsigned int size):Stream(),streamSize(size) {};
|
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 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 int open(String _filename, char mode) = 0;
|
||||||
virtual void close() = 0;
|
virtual void close() = 0;
|
||||||
virtual uint16_t getContentType() {return contentType;};
|
virtual uint16_t getContentType() {return contentType;};
|
||||||
virtual void putEOF() {if (textMode) write (EOF);};
|
virtual void putEOF() {if (textMode) write (EOFchar);};
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
48
lighthub/systemconfigdata.h
Normal file
48
lighthub/systemconfigdata.h
Normal file
@@ -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)
|
||||||
Reference in New Issue
Block a user