From 0b216e2e83c04c4c07b351e821872628fefa1754 Mon Sep 17 00:00:00 2001 From: Jean Jacques Avril Date: Sat, 19 Mar 2022 14:47:03 +0100 Subject: [PATCH] Implemented persistent settings as binary format, got rid of json architecture --- Notes.md | 10 +++++ data/admin | Bin 12 -> 12 bytes data/config.json | 5 --- data/settings | Bin 0 -> 77 bytes data/text.json | 5 --- data/wifi | 1 - src/Config.cpp | 103 ++++++++++++++++++++++++++++++++++++++++++----- src/Config.h | 14 ++++++- src/main.cpp | 5 ++- 9 files changed, 120 insertions(+), 23 deletions(-) delete mode 100644 data/config.json create mode 100644 data/settings delete mode 100644 data/text.json delete mode 100644 data/wifi diff --git a/Notes.md b/Notes.md index b827aac..d902548 100644 --- a/Notes.md +++ b/Notes.md @@ -25,3 +25,13 @@ ### 19.02.2022 + Stable and fast csv database implemented. + Basic REST API: read, create and delete users / read and drop csv db + +### 19.03.2022 ++ Settings pos + + WifiSSID - terminator 0x00 - Pos.: 0-31 (00 - 1F) + + WifiPassword - terminator 0x00 - Pos.: 32-63 (20 - 3F) + + Device IP - 40-43 + + Subnet 44 - 47 + + Gateway 48 - 4B ++ Flags: + + Mode 0 - station; 1 - client 4C \ No newline at end of file diff --git a/data/admin b/data/admin index 002dce9ae6e7644c5aa16d2f9e06d7d7304d1aca..125ce967188d2fe2c2fe2dbd0c8436377bbf6a48 100644 GIT binary patch literal 12 QcmYdH$<54TNCc63033P*K>z>% literal 12 PcmYdH$<54TNCcAr983fg diff --git a/data/config.json b/data/config.json deleted file mode 100644 index 79531f6..0000000 --- a/data/config.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "THEMEID": 1, - "SSID":"DoorLock", - "PASS":"geheim123" -} \ No newline at end of file diff --git a/data/settings b/data/settings new file mode 100644 index 0000000000000000000000000000000000000000..deafdd0669a9b2d2e32b34ed5951193f189f07a9 GIT binary patch literal 77 mcmZ?C&oA=HPtIn*0n$@5QZsW6jf`=~b1?=m0`Y$k%>V#!FbFjO literal 0 HcmV?d00001 diff --git a/data/text.json b/data/text.json deleted file mode 100644 index 01a9a9e..0000000 --- a/data/text.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "GREET":"Hallo $name", - "FAIL":"PIN Incorrect", - "HOME":"Willkommen!|Pin oder Karte" -} \ No newline at end of file diff --git a/data/wifi b/data/wifi deleted file mode 100644 index 4a60e06..0000000 --- a/data/wifi +++ /dev/null @@ -1 +0,0 @@ -1 DoorLock geheim123 10.1.4.22 diff --git a/src/Config.cpp b/src/Config.cpp index 5100fbb..9deae1a 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -1,5 +1,5 @@ #include "Config.h" - +#define CONFIG_SIZE 0x4C Config::Config() { if (!LittleFS.begin()) @@ -8,14 +8,99 @@ Config::Config() return; } } - - -bool Config::loadConfig() +Config::~Config() { - StaticJsonDocument<1024> doc; - File config_file = LittleFS.open("/config.json","r"); - deserializeJson(doc, config_file); - this->SSID=doc["SSID"].as(); - this->PASS=doc["PASS"].as(); + free(buffer); +} + +bool Config::loadBin() +{ + File config_file = LittleFS.open("/settings", "r"); + if(buffer==nullptr) + buffer = (uint8_t*) malloc(CONFIG_SIZE); + if (config_file.available()) + { + config_file.read(buffer, CONFIG_SIZE); + SSID = (char*) buffer; + buffer[0x1F] = 0x00; + buffer[0x3F] = 0x00; + PASS = (char*)(buffer + 0x20); + ip = (uint32_t *)(buffer + 0x40); + subnet = (uint32_t *)(buffer + 0x44); + gw = (uint32_t *)(buffer + 0x48); + mode = (uint8_t *)(buffer + 0x4C); + } + config_file.close(); return true; } + +bool Config::saveBin() +{ + File config_file = LittleFS.open("/settings", "w"); + config_file.write(buffer,CONFIG_SIZE); + delay(100); + config_file.close(); + return true; +} +bool Config::setSSID(const char *ssid){ + size_t offset = 0; + bool copy = true; + for(int i=0; i<31;i++){ + if(ssid[i]==0x00) + copy = false; + buffer[offset+i] = copy?ssid[i]:0x00; + } + buffer[offset+31] = '\n'; + return true; +} +bool Config::setPASS(const char *pass){ + size_t offset = 0x20; + bool copy = true; + for(int i=0; i<31;i++){ + if(pass[i]==0x00) + copy = false; + buffer[offset+i] = copy?pass[i]:0x00; + } + buffer[offset+31] = '\n'; + return true; +} +void Config::print(){ + Serial.print("BufferHEX "); + for(int i=0; i"); + Serial.print(buffer[i],HEX); + Serial.print("|"); + } + Serial.print("SSID: "); + Serial.println(SSID); + Serial.print("PASS: "); + Serial.println(PASS); + Serial.print("IP"); + Serial.print((uint8_t) *ip, DEC); + Serial.print("."); + Serial.print((uint8_t)*((uint8_t*)ip+1), DEC); + Serial.print("."); + Serial.print((uint8_t) *((uint8_t*)ip+2), DEC); + Serial.print("."); + Serial.println((uint8_t) *((uint8_t*)ip+3), DEC); + Serial.print("GW"); + Serial.print((uint8_t) *((uint8_t*)gw), DEC); + Serial.print("."); + Serial.print((uint8_t) *((uint8_t*)gw+1), DEC); + Serial.print("."); + Serial.print((uint8_t) *((uint8_t*)gw+2), DEC); + Serial.print("."); + Serial.println((uint8_t) *((uint8_t*)gw+3), DEC); + Serial.print("SUBNET"); + Serial.print((uint8_t)*((uint8_t*)subnet), DEC); + Serial.print("."); + Serial.print((uint8_t)*((uint8_t*)subnet+1), DEC); + Serial.print("."); + Serial.print((uint8_t)*((uint8_t*)subnet+2), DEC); + Serial.print("."); + Serial.println((uint8_t)*((uint8_t*)subnet+3), DEC); + + + +} \ No newline at end of file diff --git a/src/Config.h b/src/Config.h index c37f1ff..073f44c 100644 --- a/src/Config.h +++ b/src/Config.h @@ -3,10 +3,22 @@ #include "ArduinoJson.h" class Config { +private: + uint8_t *buffer = nullptr; + public: Config(); + ~Config(); const char *SSID; const char *PASS; - int THEME; + uint8_t *mode; + uint32_t *ip; + uint32_t *subnet; + uint32_t *gw; bool loadConfig(); + bool loadBin(); + bool saveBin(); + void print(); + bool setSSID(const char *ssid); + bool setPASS(const char *pass); }; \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 9048314..8bdacce 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -31,12 +31,13 @@ IPAddress dns(192, 168, 178, 1); DNSServer dnsServer; void setup() { - config.loadConfig(); + //config.loadConfig(); + config.loadBin(); Serial.begin(115200); Serial.println("Starting System"); Serial.print("\t1. Network config ->"); WiFi.mode(WIFI_AP); - Serial.println(WiFi.softAPConfig(local_IP, local_IP, subnet) ? "Ready" : "Failed!"); + Serial.println(WiFi.softAPConfig(*config.ip, *config.gw, *config.subnet) ? "Ready" : "Failed!"); Serial.print("\t2 AP setup " + String(config.SSID) + " -> "); if (strlen(config.PASS) > 0) Serial.println(WiFi.softAP(config.SSID, config.PASS) ? "Ready" : "Failed!");