From 39071584371cdbf445c89b065061864c83d79994 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=BB=D0=B8=D0=BC=D0=BE=D0=B2=20=D0=90=D0=BD=D0=B4?= =?UTF-8?q?=D1=80=D0=B5=D0=B9=20=D0=9D=D0=B8=D0=BA=D0=BE=D0=BB=D0=B0=D0=B5?= =?UTF-8?q?=D0=B2=D0=B8=D1=87?= Date: Sun, 16 Jul 2023 20:29:59 +0300 Subject: [PATCH] mac save&AC swmode fixes, sha256 sign for cmd tst --- lighthub/config.cpp | 6 ++-- lighthub/flashstream.cpp | 25 +++------------ lighthub/item.cpp | 27 ++++++++++++++-- lighthub/item.h | 2 +- lighthub/itemCmd.h | 4 +-- lighthub/main.cpp | 48 +++++++++++++++++++++++----- lighthub/modules/out_ac.cpp | 2 +- lighthub/textconst.h | 1 + lighthub/utils.cpp | 62 +++++++++++++++++++++++++++++++++++-- lighthub/utils.h | 3 +- platformio.ini | 4 ++- 11 files changed, 143 insertions(+), 41 deletions(-) diff --git a/lighthub/config.cpp b/lighthub/config.cpp index 53e7f27..b304688 100644 --- a/lighthub/config.cpp +++ b/lighthub/config.cpp @@ -30,7 +30,7 @@ bool systemConfig::isValidSysConf() stream->close(); return false; } - stream->close(); + stream->close(); return true; }; @@ -44,7 +44,7 @@ bool systemConfig::isValidSysConf() bool isMacValid = false; for (short i = 0; i < 6; i++) { mac[i] = stream->read(); - if (mac[i] != 0 && mac[i] != 0xff) isMacValid = true; + if ((mac[i] != 0) && (mac[i] != 0xff)) isMacValid = true; } stream->close(); return isMacValid; @@ -53,7 +53,7 @@ bool systemConfig::isValidSysConf() bool systemConfig::setMAC(macAddress& _mac) { if (!stream || !isValidSysConf()) return false; - openStream('a'); + openStream('r'); //was 'a' stream->seek(offsetof(systemConfigData,mac)); stream->write ((const uint8_t *)&_mac,sizeof(_mac)); memcpy(mac, _mac, sizeof(mac)); diff --git a/lighthub/flashstream.cpp b/lighthub/flashstream.cpp index 5b899be..735e627 100644 --- a/lighthub/flashstream.cpp +++ b/lighthub/flashstream.cpp @@ -23,30 +23,13 @@ #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; - -// static char samBuffer[64]; -// short samBufferPos = 0; -//#endif - - -#if defined(__SAM3X8E__) DueFlashStorage EEPROM; static char samBuffer[64]; short samBufferPos = 0; #endif #ifdef NRF5 +#include //STUB NRFFlashStorage EEPROM; #endif @@ -200,8 +183,10 @@ NRFFlashStorage EEPROM; //eeprom_buffer_flush(); #endif - pos=min(_pos, streamSize); - //debugSerial<0); toExecute=true; @@ -955,6 +969,13 @@ int Item::Ctrl(itemCmd cmd, char* subItem, bool allowRecursion) cmd.Cmd(CMD_ON); } status2Send |=FLAG_COMMAND | FLAG_SEND_IMMEDIATE; + } + else + { + if (getFlag(FLAG_DISABLED)) clearFlag(FLAG_DISABLED); else setFlag(FLAG_DISABLED); + status2Send |= FLAG_FLAGS | FLAG_SEND_IMMEDIATE; + res=1; + } break; @@ -1127,7 +1148,7 @@ int Item::Ctrl(itemCmd cmd, char* subItem, bool allowRecursion) else // Fast track for commands to subitems { if (driver) return driver->Ctrl(cmd,subItem,toExecute); - return 0; + ///// return 0; } } diff --git a/lighthub/item.h b/lighthub/item.h index c0f8bba..ebdbd20 100644 --- a/lighthub/item.h +++ b/lighthub/item.h @@ -119,7 +119,7 @@ class Item boolean Setup(); void Stop(); //int Ctrl(short cmd, short n=0, int * Parameters=NULL, int suffixCode=0, char* subItem=NULL); - int Ctrl(itemCmd cmd, char* subItem=NULL, bool allowRecursion = true); + int Ctrl(itemCmd cmd, char* subItem=NULL, bool allowRecursion = true, bool authorized=false); int Ctrl(char * payload, char * subItem=NULL); int getArg(short n=0); diff --git a/lighthub/itemCmd.h b/lighthub/itemCmd.h index 4034891..85d163a 100644 --- a/lighthub/itemCmd.h +++ b/lighthub/itemCmd.h @@ -46,8 +46,8 @@ const cmdstr commands_P[] PROGMEM = #define CMD_ENABLE 0xa /// for PID regulator and XON/XOFF - chan limitation #define CMD_DISABLE 0xb /// for PID regulator -#define CMD_UNFREEZE 0xc /// Aliase for ON -#define CMD_FREEZE 0xd /// Aliase for OFF +#define CMD_UNFREEZE 0xc /// +#define CMD_FREEZE 0xd /// #define CMD_AUTO 0xe /// Thermostat/AC set to Auto mode #define CMD_FAN 0xf /// AC set to Fan-only mode diff --git a/lighthub/main.cpp b/lighthub/main.cpp index ba7f929..37ad6fe 100644 --- a/lighthub/main.cpp +++ b/lighthub/main.cpp @@ -27,6 +27,9 @@ e-mail anklimov@gmail.com #include "TimerInterrupt_Generic.h" #endif +#ifdef CRYPT +#include "RNG.h" +#endif #ifdef SYSLOG_ENABLE #include @@ -132,6 +135,7 @@ volatile uint32_t timerSensorCheck =0; volatile unsigned long timerCount=0; volatile int16_t timerNumber=-1; volatile int8_t timerHandlerBusy=0; +volatile uint32_t cryptoSalt=0; aJsonObject *pollingItem = NULL; @@ -681,6 +685,12 @@ lan_status lanLoop() { onMQTTConnect(); + #ifdef CRYPT + //setTopic(buf,sizeof(buf),T_OUT); + strncpy(buf, "+/+/$salt", sizeof(buf)); // Only on separated cmd/val topics + mqttClient.subscribe(buf); + #endif + lanStatus = OPERATION;//3; infoSerial<itemArr->name, "OFF","/compressor"); - publishTopic(item->itemArr->name, (long) swing,"/swing"); + //publishTopic(item->itemArr->name, (long) swing,"/swing"); //publishTopic(item->itemArr->name, (long) fan_spd,"/fan"); ///////////////////////////////// diff --git a/lighthub/textconst.h b/lighthub/textconst.h index 60dd33c..f6e01e2 100644 --- a/lighthub/textconst.h +++ b/lighthub/textconst.h @@ -3,6 +3,7 @@ const char state_P[] PROGMEM = "$state"; const char disconnected_P[] PROGMEM = "disconnected"; const char ready_P[] PROGMEM = "ready"; +const char salt_P[] PROGMEM = "$salt"; const char homie_P[] PROGMEM = "$homie"; const char homiever_P[] PROGMEM = "3.0.1"; diff --git a/lighthub/utils.cpp b/lighthub/utils.cpp index 400074c..52f25c4 100644 --- a/lighthub/utils.cpp +++ b/lighthub/utils.cpp @@ -28,6 +28,10 @@ e-mail anklimov@gmail.com #include #include +#ifdef CRYPT +#include "SHA256.h" +#endif + #ifndef debugSerialPort #define debugSerialPort Serial #endif @@ -324,8 +328,8 @@ Awesome work Mark T!*/ __attribute__ ((section (".ramfunc"))) - -void ReadUniqueID( uint32_t * pdwUniqueID ) +// #pragma optimize("", off) +uint32_t ReadUniqueID( uint32_t * pdwUniqueID ) { unsigned int status ; @@ -354,8 +358,14 @@ void ReadUniqueID( uint32_t * pdwUniqueID ) { status = EFC1->EEFC_FSR ; } while ( (status & EEFC_FSR_FRDY) != EEFC_FSR_FRDY ) ; + + + return *(uint32_t *)(IFLASH1_ADDR + 128); // dont remove: SAM defect workaround - MPU dont leave Unique Identifier mode until read flash out UID of range + + #endif } +//#pragma optimize("", on) int _inet_aton(const char* aIPAddrString, IPAddress& aResult) @@ -804,7 +814,55 @@ bool getPinVal(uint8_t pin) { return (0!=(*portOutputRegister( digitalPinToPort(pin) ) & digitalPinToBitMask(pin))); } +#ifdef CRYPT +#define HASH_SIZE 32 +SHA256 sha256; +extern uint32_t cryptoSalt; +//extern +const char cryptoKey[] ="12345678"; + +bool checkToken(char * token, char * data) +{ + // Make valid random salted data + char saltStr[32]; + printUlongValueToStr(saltStr, cryptoSalt); + + + // get hash + uint8_t result[HASH_SIZE]; + memset(result, 0xAA, sizeof(result)); + + sha256.reset(); + sha256.update(saltStr,strlen(saltStr)); + sha256.update(cryptoKey,strlen(cryptoKey)); + if (data) sha256.update(data,strlen(data)); + sha256.finalize(result,sizeof(result)); + sha256.clear(); + //hmac(result, HASH_SIZE, cryptoKey, strlen(cryptoKey), testData, strlen(testData)); + + //for (int i=0;i