diff --git a/Config.cpp b/Config.cpp index 89401cc0..4e707a88 100644 --- a/Config.cpp +++ b/Config.cpp @@ -270,6 +270,92 @@ Config& Config::CreateRAM() return cfg; } +/******************************** ConfigBase ********************************/ + +ConfigBase::ConfigBase() +{ + // Flash最后一块作为配置区 + if(!Config::Current) Config::Current = &Config::CreateFlash(); + + Cfg = Config::Current; + + New = true; + + _Name = NULL; +} + +uint ConfigBase::Size() const +{ + assert_param2(_End && _Start, "_Start & _End == NULL"); + + return (uint)_End - (uint)_Start; +} + +Array ConfigBase::ToArray() +{ + return Array(_Start, Size()); +} + +const Array ConfigBase::ToArray() const +{ + return Array(_Start, Size()); +} + +void ConfigBase::Init() +{ + memset(_Start, 0, Size()); +} + +void ConfigBase::Load() +{ + if(!Cfg) return; + + // 尝试加载配置区设置 + auto bs = ToArray(); + New = !Cfg->GetOrSet(_Name, bs); + if(New) + debug_printf("%s::Load 首次运行,创建配置区!\r\n", _Name); + else + debug_printf("%s::Load 从配置区加载配置\r\n", _Name); +} + +void ConfigBase::Save() const +{ + if(!Cfg) return; + + debug_printf("%s::Save \r\n", _Name); + + Cfg->Set(_Name, ToArray()); +} + +void ConfigBase::Clear() +{ + if(!Cfg) return; + + Init(); + + debug_printf("%s::Clear \r\n", _Name); + + Cfg->Set(_Name, ToArray()); +} + +void ConfigBase::Show() const +{ +} + +void ConfigBase::Write(Stream& ms) const +{ + ms.Write(ToArray()); +} + +void ConfigBase::Read(Stream& ms) +{ + auto bs = ToArray(); + ms.Read(bs); +} + +/******************************** HotConfig ********************************/ + void* HotConfig::Next() const { return (void*)&this[1]; diff --git a/Config.h b/Config.h index 507744e9..ea1482cd 100644 --- a/Config.h +++ b/Config.h @@ -38,11 +38,46 @@ public: static Config& CreateRAM(); }; +/******************************** ConfigBase ********************************/ + +// 应用配置基类 +class ConfigBase +{ +public: + bool New; + + ConfigBase(); + virtual void Init(); + + virtual void Load(); + virtual void Save() const; + virtual void Clear(); + virtual void Show() const; + + // 序列化到消息数据流 + void Write(Stream& ms) const; + void Read(Stream& ms); + +protected: + Config* Cfg; + const char* _Name; + + void* _Start; + void* _End; + + uint Size() const; + + Array ToArray(); + const Array ToArray() const; +}; + // 必须设定为1字节对齐,否则offsetof会得到错误的位置 #pragma pack(push) // 保存对齐状态 // 强制结构体紧凑分配空间 #pragma pack(1) +/******************************** HotConfig ********************************/ + // 系统配置信息 class HotConfig { @@ -54,6 +89,8 @@ public: static HotConfig& Current(); }; +/******************************** SysConfig ********************************/ + // 系统配置信息 class SysConfig { diff --git a/TinyNet/Tiny.cpp b/TinyNet/Tiny.cpp index bb144222..f490f147 100644 --- a/TinyNet/Tiny.cpp +++ b/TinyNet/Tiny.cpp @@ -73,7 +73,7 @@ ITransport* Create2401(SPI_TypeDef* spi_, Pin ce, Pin irq, Pin power, bool power auto nrf = new NRF24L01(); nrf->Init(spi, ce, irq, power); - auto tc = TinyConfig::Init(); + auto tc = TinyConfig::Create(); if(tc->Interval < 40) { tc->Channel = 120; @@ -121,7 +121,7 @@ void ShunComExternalCfg(void * param) ITransport* CreateShunCom(COM_Def index, int baudRate, Pin rst, Pin power, Pin slp, Pin cfg, IDataPort* led) { - auto tc = TinyConfig::Init(); + auto tc = TinyConfig::Create(); debug_printf("tc->Interval %d\r\n",tc->Interval ); if(tc->Interval == 0) { @@ -181,7 +181,7 @@ void* InitConfig(void* data, uint size) ((byte*)data)[0] = size; } - auto tc = TinyConfig::Init(); + auto tc = TinyConfig::Create(); // 尝试加载配置区设置 tc->Load(); diff --git a/TinyNet/TinyClient.cpp b/TinyNet/TinyClient.cpp index c56a0f11..e3bba8fd 100644 --- a/TinyNet/TinyClient.cpp +++ b/TinyNet/TinyClient.cpp @@ -437,8 +437,7 @@ bool TinyClient::OnDisjoin(const TinyMessage& msg) return false; } - Cfg->LoadDefault(); - Cfg->Save(); + Cfg->Clear(); Sys.Sleep(3000); Sys.Reset(); @@ -461,7 +460,7 @@ void TinyClient::Ping() { if(Server == 0) return; - debug_printf(" %d 秒无法联系网关,最后活跃时间:%d,系统当前时间:%d\r\n",off,LastActive,now); + debug_printf(" %d 秒无法联系网关,最后活跃时间: %d ,系统当前时间:%d \r\n", off, (int)LastActive, (int)now); Sys.SetTaskPeriod(_TaskID, 5000); // 掉线以后,重发组网信息,基本功能继续执行 diff --git a/TinyNet/TinyConfig.cpp b/TinyNet/TinyConfig.cpp index 2c90c4f6..4a60d902 100644 --- a/TinyNet/TinyConfig.cpp +++ b/TinyNet/TinyConfig.cpp @@ -3,103 +3,35 @@ TinyConfig* TinyConfig::Current = NULL; -TinyConfig::TinyConfig() +TinyConfig::TinyConfig() : ConfigBase() { - Cfg = Config::Current; + _Name = "TCFG"; + _Start = &Length; + _End = &TagEnd; + + Init(); } -uint TinyConfig::Size() const +void TinyConfig::Init() { - return (uint)&New - (uint)&Length; -} - -Array TinyConfig::ToArray() -{ - return Array(&Length, Size()); -} - -const Array TinyConfig::ToArray() const -{ - return Array(&Length, Size()); -} - -void TinyConfig::LoadDefault() -{ - // 实际内存大小,减去头部大小 - uint len = Size(); - memset(&Length, 0, len); - Length = len; + ConfigBase::Init(); + + Length = Size(); Kind = Sys.Code; - //Server = 0x01; - //Channel = 120; - //Speed = 250; - - //PingTime = 20; + PingTime = 20; OfflineTime = 60; } -void TinyConfig::Load() -{ - if(!Cfg) return; - - // 尝试加载配置区设置 - auto bs = ToArray(); - New = !Cfg->GetOrSet("TCFG", bs); - if(New) - debug_printf("TinyConfig::Load 首次运行,创建配置区!\r\n"); - else - debug_printf("TinyConfig::Load 从配置区加载配置\r\n"); - - if(Kind != Sys.Code) - { - debug_printf("TinyConfig::Load 设备类型变更\r\n"); - - Kind = Sys.Code; - Cfg->Set("TCFG", bs); - } -} - -void TinyConfig::Save() const -{ - if(!Cfg) return; - - debug_printf("TinyConfig::Save \r\n"); - - Cfg->Set("TCFG", ToArray()); -} - -void TinyConfig::Clear() -{ - if(!Cfg) return; - - LoadDefault(); - - debug_printf("TinyConfig::Clear \r\n"); - - Cfg->Set("TCFG", ToArray()); -} - -void TinyConfig::Write(Stream& ms) const -{ - ms.Write(ToArray()); -} - -void TinyConfig::Read(Stream& ms) -{ - auto bs = ToArray(); - ms.Read(bs); -} - -TinyConfig* TinyConfig::Init() +TinyConfig* TinyConfig::Create() { // 默认出厂设置 static TinyConfig tc; if(!Current) { Current = &tc; - tc.LoadDefault(); + tc.Init(); } return &tc; diff --git a/TinyNet/TinyConfig.h b/TinyNet/TinyConfig.h index 625ad4c1..07407471 100644 --- a/TinyNet/TinyConfig.h +++ b/TinyNet/TinyConfig.h @@ -11,7 +11,7 @@ #pragma pack(1) // 配置信息 -class TinyConfig +class TinyConfig : public ConfigBase { public: byte Length; // 数据长度 @@ -35,28 +35,14 @@ public: byte Password[16]; // 通信密码 byte Mac[6]; // 无线物理地址 - bool New; // 是否新创建的配置 - TinyConfig(); - void LoadDefault(); - - void Load(); - void Save() const; - void Clear(); - - // 序列化到消息数据流 - void Write(Stream& ms) const; - void Read(Stream& ms); + virtual void Init(); static TinyConfig* Current; - static TinyConfig* Init(); - + static TinyConfig* Create(); + private: - Config* Cfg; - - uint Size() const; - Array ToArray(); - const Array ToArray() const; + byte TagEnd; // 数据区结束标识符 }; #pragma pack(pop) // 恢复对齐状态 diff --git a/TinyNet/TinyMessage.cpp b/TinyNet/TinyMessage.cpp index a753c3c0..2a88b70c 100644 --- a/TinyNet/TinyMessage.cpp +++ b/TinyNet/TinyMessage.cpp @@ -215,7 +215,7 @@ TinyController::TinyController() : Controller() Mode = 0; Interval = 20; Timeout = 200; - auto cfg = TinyConfig::Init(); + auto cfg = TinyConfig::Create(); if(cfg) { bool flag = false; @@ -250,7 +250,7 @@ TinyController::~TinyController() void TinyController::ApplyConfig() { - auto cfg = TinyConfig::Init(); + auto cfg = TinyConfig::Create(); if(cfg) { // 调整参数 diff --git a/TokenNet/Token.cpp b/TokenNet/Token.cpp index 06430640..7076f896 100644 --- a/TokenNet/Token.cpp +++ b/TokenNet/Token.cpp @@ -211,8 +211,8 @@ ITransport* Token::Create2401(SPI_TypeDef* spi_, Pin ce, Pin irq, Pin power, boo static NRF24L01 nrf; nrf.Init(&spi, ce, irq, power); - auto tc = TinyConfig::Init(); - if(tc->Interval==0) + auto tc = TinyConfig::Create(); + if(tc->Interval == 0) { tc->Channel = 120; tc->Speed = 250; @@ -237,8 +237,8 @@ ITransport* Token::Create2401(SPI_TypeDef* spi_, Pin ce, Pin irq, Pin power, boo ITransport* Token::CreateShunCom(COM_Def index, int baudRate, Pin rst, Pin power, Pin slp, Pin cfg, IDataPort* led) { - auto tc = TinyConfig::Init(); - if(tc->Interval==0) + auto tc = TinyConfig::Create(); + if(tc->Interval == 0) { tc->Channel = 0x0F; tc->Speed = 250; diff --git a/TokenNet/TokenConfig.cpp b/TokenNet/TokenConfig.cpp index af395dcc..59caabb3 100644 --- a/TokenNet/TokenConfig.cpp +++ b/TokenNet/TokenConfig.cpp @@ -4,17 +4,20 @@ TokenConfig* TokenConfig::Current = NULL; -uint TokenConfig::Size() const +TokenConfig::TokenConfig() : ConfigBase() { - return (uint)&New - (uint)&Length; + _Name = "TKCF"; + _Start = &Length; + _End = &TagEnd; + + Init(); } -void TokenConfig::LoadDefault() +void TokenConfig::Init() { - // 实际内存大小,减去头部大小 - uint len = Size(); - memset(&Length, 0, len); - Length = len; + ConfigBase::Init(); + + Length = Size(); ServerIP = 0; SoftVer = Sys.Version; @@ -23,47 +26,7 @@ void TokenConfig::LoadDefault() Protocol = 2; } -bool TokenConfig::Load() -{ - // Flash最后一块作为配置区 - if(!Config::Current) Config::Current = &Config::CreateFlash(); - - // 尝试加载配置区设置 - uint len = Size(); - if(!len) len = sizeof(this[0]); - Array bs(&Length, len); - /*if(!Config::Current->GetOrSet("TKCF", bs)) - debug_printf("TokenConfig::Load 首次运行,创建配置区!\r\n"); - else - debug_printf("TokenConfig::Load 从配置区加载配置\r\n");*/ - if(Config::Current->Get("TKCF", bs)) - { - debug_printf("TokenConfig::Load 从配置区加载配置\r\n"); - if(ServerIP == 0) - New = true; - else - New = false; - return true; - } - else - New = true; - - - return false; -} - -void TokenConfig::Save() -{ - uint len = Size(); - if(!len) len = sizeof(this[0]); - - debug_printf("TokenConfig::Save \r\n"); - - Array bs(&Length, len); - Config::Current->Set("TKCF", bs); -} - -void TokenConfig::Show() +void TokenConfig::Show() const { #if DEBUG debug_printf("TokenConfig::令牌配置:\r\n"); @@ -78,58 +41,42 @@ void TokenConfig::Show() debug_printf("\t厂商: %s \r\n", Vendor); debug_printf("\t登录: %s \r\n", Name); debug_printf("\t密码: %s \r\n", Key); - - #endif } -void TokenConfig::Write(Stream& ms) const +TokenConfig* TokenConfig::Create(const char* vendor, byte protocol, ushort sport, ushort port) { - uint len = Size(); - if(!len) len = sizeof(this[0]); - - ms.Write(&Length, 0, len); -} - -void TokenConfig::Read(Stream& ms) -{ - uint len = Size(); - if(!len) len = sizeof(this[0]); - - ms.Read(&Length, 0, len); -} - -TokenConfig* TokenConfig::Init(const char* vendor, byte protocol, ushort sport, ushort port) -{ - debug_printf("\r\n"); - static TokenConfig tc; - TokenConfig::Current = &tc; - tc.LoadDefault(); - - //strcpy(tc.Vendor, vendor); - bool rs = tc.Load(); - - if(tc.Vendor[0] == 0) + if(!Current) { - strncpy(tc.Vendor, vendor, ArrayLength(tc.Vendor)); + TokenConfig::Current = &tc; + tc.Init(); - rs = false; + //strcpy(tc.Vendor, vendor); + tc.Load(); + bool rs = tc.New; + + if(tc.Vendor[0] == 0) + { + strncpy(tc.Vendor, vendor, ArrayLength(tc.Vendor)); + + rs = false; + } + + if(tc.Server[0] == 0) + { + strncpy(tc.Server, tc.Vendor, ArrayLength(tc.Server)); + + //tc.ServerIP = svr.Value; + tc.ServerPort = sport; + tc.Port = port; + + rs = false; + } + if(!rs) tc.Save(); + + tc.Show(); } - if(tc.Server[0] == 0) - { - strncpy(tc.Server, tc.Vendor, ArrayLength(tc.Server)); - - //tc.ServerIP = svr.Value; - tc.ServerPort = sport; - tc.Port = port; - - rs = false; - } - if(!rs) tc.Save(); - - tc.Show(); - return &tc; } diff --git a/TokenNet/TokenConfig.h b/TokenNet/TokenConfig.h index 59d18391..6163d909 100644 --- a/TokenNet/TokenConfig.h +++ b/TokenNet/TokenConfig.h @@ -3,7 +3,7 @@ #include "Sys.h" #include "Stream.h" -#include "Storage\Storage.h" +#include "Config.h" // 必须设定为1字节对齐,否则offsetof会得到错误的位置 #pragma pack(push) // 保存对齐状态 @@ -11,13 +11,15 @@ #pragma pack(1) // 配置信息 -struct TokenConfig +class TokenConfig : public ConfigBase { - byte Length; // 数据长度 - char Name[16]; // 登录名 - char Key[16]; // 登录密码 - ushort HardVer; // 硬件版本 - ushort SoftVer; // 软件版本 +public: + byte Length; // 数据长度 + + char Name[16]; // 登录名 + char Key[16]; // 登录密码 + ushort HardVer; // 硬件版本 + ushort SoftVer; // 软件版本 byte PingTime; // 心跳时间。秒 byte Protocol; // 协议,TCP=1/UDP=2 @@ -27,21 +29,15 @@ struct TokenConfig char Server[32]; // 服务器域名。出厂为空,从厂商服务器覆盖,恢复出厂设置时清空 char Vendor[32]; // 厂商服务器域名。原始厂商服务器地址 - bool New; - - void LoadDefault(); - - bool Load(); - void Save(); - void Show(); - uint Size() const; - - // 序列化到消息数据流 - void Write(Stream& ms) const; - void Read(Stream& ms); + TokenConfig(); + virtual void Init(); + virtual void Show() const; static TokenConfig* Current; - static TokenConfig* Init(const char* vendor, byte protocol, ushort sport, ushort port); + static TokenConfig* Create(const char* vendor, byte protocol, ushort sport, ushort port); + +private: + byte TagEnd; // 数据区结束标识符 }; #pragma pack(pop) // 恢复对齐状态