NO-IP devices persistence

STM Flash cfg fix
 CAN GROUP chan fix
 CRC16 fix
This commit is contained in:
2024-08-21 14:01:06 +03:00
parent 2b638b1310
commit 806e99eb92
10 changed files with 113 additions and 43 deletions

View File

@@ -156,12 +156,21 @@ return res;
bool canDriver::begin() bool canDriver::begin()
{ {
if (!root) return false; if (root)
canConfigObj = aJson.getObjectItem(root, "can"); {
if (!canConfigObj) return false; canConfigObj = aJson.getObjectItem(root, "can");
canRemoteConfigObj= aJson.getObjectItem(canConfigObj, "conf"); if (canConfigObj)
{
canRemoteConfigObj= aJson.getObjectItem(canConfigObj, "conf");
controllerId = getMyId();
}
confCRC=getCRC(root);
}
controllerId = getMyId(); #ifndef NOIP
if (!canConfigObj) return false;
#endif
if (!ready) // not reInitialization if (!ready) // not reInitialization
{ {
@@ -314,7 +323,7 @@ switch (state)
if (CANConfStream.peek() == '{') { if (CANConfStream.peek() == '{') {
debugSerial<<F("CAN: JSON detected")<<endl; debugSerial<<F("CAN: JSON detected")<<endl;
aJsonStream as = aJsonStream(&CANConfStream); aJsonStream as = aJsonStream(&CANConfStream);
cleanConf(false); cleanConf(1);
root = aJson.parse(&as); root = aJson.parse(&as);
CANConfStream.close(); CANConfStream.close();
if (!root) { if (!root) {
@@ -327,7 +336,9 @@ switch (state)
} }
infoSerial<<F("CAN: config Loaded")<<endl; infoSerial<<F("CAN: config Loaded")<<endl;
configLocked--; configLocked--;
applyConfig(); cmdFunctionSave(0,NULL);
if (applyConfig()) ;
// debugSerial.print(aJson.print(root,false));
sysConf.loadETAG(); sysConf.loadETAG();
state = canState::Idle; state = canState::Idle;
return ; return ;
@@ -446,10 +457,12 @@ if (id.status){
debugSerial<<F("CAN: item ")<<it.itemArr->name<<" "; debugSerial<<F("CAN: item ")<<it.itemArr->name<<" ";
ic.debugOut(); ic.debugOut();
if (ic.isCommand()) flags |= FLAG_COMMAND;
if (ic.isValue()) flags |= FLAG_PARAMETERS; if (ic.isValue()) flags |= FLAG_PARAMETERS;
if (ic.getSuffix()==S_DELAYED) flags |= FLAG_SEND_DELAYED;
else if (ic.isCommand()) flags |= FLAG_COMMAND;
ic.saveItem(&it,flags); ic.saveItem(&it,flags);
it.SendStatusImmediate(ic,flags | FLAG_NOT_SEND_CAN,it.getSubItemStrById(id.subItemId)); it.SendStatusImmediate(ic,flags | FLAG_NOT_SEND_CAN, it.getSubItemStrById(id.subItemId));
return true; return true;
} }
@@ -465,7 +478,7 @@ if (id.status){
case canState::MACLookup: case canState::MACLookup:
if ((id.payloadType == payloadType::lookupMAC)) if ((id.payloadType == payloadType::lookupMAC))
{ {
if (canConfigObj && (id.subjId == getCRC(canConfigObj))) ///? if (root && (id.subjId == confCRC)) ///?
{ {
infoSerial << (F("Valid config already onboard")) << endl; infoSerial << (F("Valid config already onboard")) << endl;
state = canState::Idle; state = canState::Idle;
@@ -661,8 +674,11 @@ return 0;
} }
bool canDriver::write(uint32_t msg_id, datagram_t * buf, uint8_t size) bool canDriver::write(uint32_t msg_id, datagram_t * buf, uint8_t size)
{ //return 0; { //
if (!ready) errorSerial<<"CAN: not initialized"<<endl; if (!ready) {
errorSerial<<"CAN: not initialized"<<endl;
return false;
}
bool res; bool res;
if (size>8) size = 8; if (size>8) size = 8;

View File

@@ -119,7 +119,7 @@ Error
class canDriver class canDriver
{ {
public: public:
canDriver(){ready=false; controllerId=0; responseTimer=0; state=canState::stateUnknown;canConfigObj=NULL;canRemoteConfigObj=NULL;}; canDriver(){ready=false; controllerId=0; responseTimer=0; state=canState::stateUnknown;canConfigObj=NULL;canRemoteConfigObj=NULL;confCRC=0xFFFF;};
uint8_t getMyId(); uint8_t getMyId();
bool sendStatus(uint16_t itemNum, itemCmd cmd, int subItem = NO_SUBITEM); bool sendStatus(uint16_t itemNum, itemCmd cmd, int subItem = NO_SUBITEM);
bool sendCommand(uint8_t devID, uint16_t itemID, itemCmd cmd, bool status=false, int subItemID=NO_SUBITEM ); bool sendCommand(uint8_t devID, uint16_t itemID, itemCmd cmd, bool status=false, int subItemID=NO_SUBITEM );
@@ -143,6 +143,7 @@ uint8_t getControllerID(){return controllerId;};
uint8_t getIdByMac(macAddress mac); uint8_t getIdByMac(macAddress mac);
aJsonObject * canConfigObj; aJsonObject * canConfigObj;
aJsonObject * canRemoteConfigObj; aJsonObject * canRemoteConfigObj;
uint16_t confCRC;
datagram_t RXpacket; datagram_t RXpacket;
canid_t RXid; canid_t RXid;
uint8_t RXlen; uint8_t RXlen;

View File

@@ -287,6 +287,10 @@ NRFFlashStorage EEPROM;
putEOF(); putEOF();
debugSerial<<F("EOF")<<endl; debugSerial<<F("EOF")<<endl;
} }
#if defined (ARDUINO_ARCH_STM32)
eeprom_buffer_flush();
#endif
#if defined(__SAM3X8E__) #if defined(__SAM3X8E__)
if (samBufferPos) flush(); if (samBufferPos) flush();
#endif #endif

View File

@@ -159,10 +159,21 @@ void Item::Parse() {
aJson.addItemToArray(itemArr, aJson.createNull());//( (long int) 0)); aJson.addItemToArray(itemArr, aJson.createNull());//( (long int) 0));
// int(defval[i]) )); //Enlarge item to 4 elements. VAL=int if no other definition in conf // int(defval[i]) )); //Enlarge item to 4 elements. VAL=int if no other definition in conf
//itemType = aJson.getArrayItem(itemArr, I_TYPE)->valueint; //itemType = aJson.getArrayItem(itemArr, I_TYPE)->valueint;
/*
itemType = replaceTypeToInt (aJson.getArrayItem(itemArr, I_TYPE)); itemType = replaceTypeToInt (aJson.getArrayItem(itemArr, I_TYPE));
itemArg = aJson.getArrayItem(itemArr, I_ARG); itemArg = aJson.getArrayItem(itemArr, I_ARG);
itemVal = aJson.getArrayItem(itemArr, I_VAL); itemVal = aJson.getArrayItem(itemArr, I_VAL);
itemExt = aJson.getArrayItem(itemArr, I_EXT); itemExt = aJson.getArrayItem(itemArr, I_EXT);
*/
aJsonObject * itemTypeObj = itemArr->child;
if (itemTypeObj) itemArg = itemTypeObj->next;
if (itemArg) itemVal = itemArg->next;
if (itemVal) itemExt = itemVal->next;
itemType = replaceTypeToInt (itemTypeObj);
switch (itemType) switch (itemType)
{ {
#ifndef PWM_DISABLE #ifndef PWM_DISABLE
@@ -861,7 +872,9 @@ bool Item::digGroup (aJsonObject *itemArr, itemCmd *cmd, char* subItem, bool aut
aJsonObject *i = itemArr->child; aJsonObject *i = itemArr->child;
configLocked++; configLocked++;
while (i) { while (i) {
if (i->type == aJson_String) switch (i->type)
{
case aJson_String:
{ //debugSerial<< i->valuestring<<endl; { //debugSerial<< i->valuestring<<endl;
aJsonObject *nextItem = aJson.getObjectItem(rootItems, i->valuestring); aJsonObject *nextItem = aJson.getObjectItem(rootItems, i->valuestring);
if (nextItem && nextItem->type == aJson_Array) //nextItem is correct item if (nextItem && nextItem->type == aJson_Array) //nextItem is correct item
@@ -869,8 +882,9 @@ bool Item::digGroup (aJsonObject *itemArr, itemCmd *cmd, char* subItem, bool aut
Item it(nextItem); Item it(nextItem);
if (cmd && it.isValid()) it.Ctrl(*cmd,subItem,false,authorized); //Execute (non recursive) if (cmd && it.isValid()) it.Ctrl(*cmd,subItem,false,authorized); //Execute (non recursive)
//Retrieve itemType //Retrieve itemType
aJsonObject * itemtype = aJson.getArrayItem(nextItem,0); //aJsonObject * itemtype = aJson.getArrayItem(nextItem,0);
if (itemtype && itemtype->type == aJson_Int && itemtype->valueint == CH_GROUP) //if (itemtype && itemtype->type == aJson_Int && itemtype->valueint == CH_GROUP)
if (it.itemType == CH_GROUP)
{ //is Group { //is Group
aJsonObject * itemSubArray = aJson.getArrayItem(nextItem,1); aJsonObject * itemSubArray = aJson.getArrayItem(nextItem,1);
short res = digGroup(itemSubArray,cmd,subItem,authorized); short res = digGroup(itemSubArray,cmd,subItem,authorized);
@@ -888,6 +902,11 @@ bool Item::digGroup (aJsonObject *itemArr, itemCmd *cmd, char* subItem, bool aut
} }
} }
} }
break;
case aJson_Object:
case aJson_Array:
executeCommand(i,-1,*cmd);
}//switch
i = i->next; i = i->next;
} //while } //while
configLocked--; configLocked--;

View File

@@ -177,11 +177,11 @@ int8_t mqttErrorRate=0;
void watchdogSetup(void) {} //Do not remove - strong re-definition WDT Init for DUE void watchdogSetup(void) {} //Do not remove - strong re-definition WDT Init for DUE
#endif #endif
bool cleanConf(bool wait) bool cleanConf(short locksAlowed )
{ {
if (!root) return true; if (!root) return true;
bool clean = true; bool clean = true;
if (configLocked) if (configLocked>locksAlowed)
{ {
errorSerial<<F("Can not clean - locked")<<endl; errorSerial<<F("Can not clean - locked")<<endl;
return false; return false;
@@ -436,6 +436,7 @@ void mqttCallback(char *topic, byte *payload, unsigned int length)
char * itemName = NULL; char * itemName = NULL;
char savedTopic[MQTT_TOPIC_LENGTH] = ""; char savedTopic[MQTT_TOPIC_LENGTH] = "";
bool forLocal=false; bool forLocal=false;
bool forBcast=false;
aJsonObject * remoteConfig = NULL; aJsonObject * remoteConfig = NULL;
int remoteID=0; int remoteID=0;
@@ -461,15 +462,29 @@ if (lanStatus == RETAINING_COLLECTING)
} }
else else
{ {
pfxlen=inTopic(topic,T_DEV); /* pfxlen=inTopic(topic,T_DEV);
if (!pfxlen) pfxlen = inTopic(topic,T_BCST); if (!pfxlen) pfxlen = inTopic(topic,T_BCST);
else // Personal device topic else // Personal device topic
strncpy(savedTopic,topic,sizeof(savedTopic)-1); //Save topic to delete after exec strncpy(savedTopic,topic,sizeof(savedTopic)-1); //Save topic to delete after exec
*/
pfxlen=inTopic(topic,T_DEV);
if (pfxlen)
{
strncpy(savedTopic,topic,sizeof(savedTopic)-1); //Save topic to delete after exec
forLocal=true;
}
else
{
pfxlen = inTopic(topic,T_BCST);
if (pfxlen) forBcast = true;
}
if (pfxlen) forLocal=true;
//if (pfxlen) forLocal=true;
#ifdef CANDRV #ifdef CANDRV
else //Nor local or bcst name, try can names // else //Nor local or bcst name, try can names
if (!forLocal && !forBcast)
{ {
pfxlen=inTopic(topic,T_ROOT); //check root pfxlen=inTopic(topic,T_ROOT); //check root
if (pfxlen) if (pfxlen)
@@ -497,7 +512,7 @@ else
// debugSerial<<itemName<<endl; // debugSerial<<itemName<<endl;
if(!strcmp_P(itemName,CMDTOPIC_P) && payload && (strlen((char*) payload)>1)) { if(!strcmp_P(itemName,CMDTOPIC_P) && payload && (strlen((char*) payload)>1)) {
mqttClient.deleteTopic(topic); mqttClient.deleteTopic(topic);
if (forLocal)((char *)payload); if (forLocal || forBcast)((char *)payload);
//TODO implement for remote //TODO implement for remote
return;// -4; return;// -4;
} }
@@ -518,7 +533,7 @@ else
bool succeeded = false; bool succeeded = false;
if (forLocal) if (forLocal || forBcast)
{ {
Item item(itemName); Item item(itemName);
if (item.isValid()) //Local item if (item.isValid()) //Local item
@@ -536,7 +551,8 @@ else
Item remoteItem(itemName,remoteItems); Item remoteItem(itemName,remoteItems);
if (remoteItem.Ctrl((char *)payload,NULL,remoteID)>0) succeeded = true; if (remoteItem.Ctrl((char *)payload,NULL,remoteID)>0) succeeded = true;
} }
} }
/// TODO bcast - scan CAN
#endif #endif
if (succeeded && savedTopic[0] && lanStatus != RETAINING_COLLECTING) if (succeeded && savedTopic[0] && lanStatus != RETAINING_COLLECTING)
@@ -1446,8 +1462,8 @@ return 200;
} }
#endif #endif
void applyConfig() { bool applyConfig() {
if (!root || configLocked) return; if (!root || configLocked) return false;
configLocked++; configLocked++;
infoSerial<<F("Applying config")<<endl; infoSerial<<F("Applying config")<<endl;
items = aJson.getObjectItem(root, "items"); items = aJson.getObjectItem(root, "items");
@@ -1593,6 +1609,7 @@ lanStatus=IP_READY_CONFIG_LOADED_CONNECTING_TO_BROKER; ///change
if (lanStatus == OPERATION_NO_MQTT) lanStatus=IP_READY_CONFIG_LOADED_CONNECTING_TO_BROKER; if (lanStatus == OPERATION_NO_MQTT) lanStatus=IP_READY_CONFIG_LOADED_CONNECTING_TO_BROKER;
configLocked--; configLocked--;
return configLoaded;
} }
void printConfigSummary() { void printConfigSummary() {
@@ -1655,7 +1672,7 @@ int loadConfigFromEEPROM()
if (sysConfStream.peek() == '{') { if (sysConfStream.peek() == '{') {
debugSerial<<F("JSON detected")<<endl; debugSerial<<F("JSON detected")<<endl;
aJsonStream as = aJsonStream(&sysConfStream); aJsonStream as = aJsonStream(&sysConfStream);
cleanConf(false); cleanConf(0); ///WTF!
root = aJson.parse(&as); root = aJson.parse(&as);
sysConfStream.close(); sysConfStream.close();
if (!root) { if (!root) {
@@ -1665,7 +1682,8 @@ int loadConfigFromEEPROM()
configLocked--; configLocked--;
return 0; return 0;
} }
infoSerial<<F("Loaded from EEPROM")<<endl; infoSerial<<F("Loaded from EEPROM")<<endl;
// debugSerial.print(aJson.print(root));
configLocked--; configLocked--;
applyConfig(); applyConfig();
sysConf.loadETAG(); sysConf.loadETAG();
@@ -2518,11 +2536,15 @@ WiFi.onEvent(WiFiEvent);
infoSerial<<QUOTE(W5500_CS_PIN)<<endl; infoSerial<<QUOTE(W5500_CS_PIN)<<endl;
#endif #endif
#endif //NOIP #endif //NOIP
loadConfigFromEEPROM();
#ifdef CANDRV #ifdef CANDRV
LHCAN.begin(); LHCAN.begin();
#endif
loadConfigFromEEPROM();
#ifdef CANDRV
LHCAN.lookupMAC(); LHCAN.lookupMAC();
#endif #endif
} }

View File

@@ -268,7 +268,7 @@ int cmdFunctionHelp(int arg_cnt, char **args);
int cmdFunctionKill(int arg_cnt, char **args); int cmdFunctionKill(int arg_cnt, char **args);
void applyConfig(); bool applyConfig();
int cmdFunctionLoad(int arg_cnt, char **args); int cmdFunctionLoad(int arg_cnt, char **args);
@@ -283,6 +283,8 @@ int cmdFunctionGet(int arg_cnt, char **args);
int cmdFunctionLoglevel(int arg_cnt, char **args); int cmdFunctionLoglevel(int arg_cnt, char **args);
void printBool(bool arg); void printBool(bool arg);
int cmdFunctionSave(int arg_cnt, char **args);
/* /*
void saveFlash(short n, char *str); void saveFlash(short n, char *str);
@@ -333,7 +335,7 @@ bool disabledDisconnected(const aJsonObject *thermoExtensionArray, int thermoLat
void resetHard(); void resetHard();
bool cleanConf(bool wait); bool cleanConf(short locksAlowed=0);
void printCurentLanConfig(); void printCurentLanConfig();

View File

@@ -482,7 +482,11 @@ itemCmd out_Modbus::findRegister(uint16_t registerNum, uint16_t posInBuffer, uin
} }
else else
{ {
if (doExecution) executeCommand(execObj, -1, mappedParam); if (doExecution)
{
debugSerial<<F("MBUS: exec ");mappedParam.debugOut();
executeCommand(execObj, -1, mappedParam);
}
// if param updated by device and no new value queued to send - update @V to avoid "Ignored - equal with setted val" // if param updated by device and no new value queued to send - update @V to avoid "Ignored - equal with setted val"
if (settedValue && !(execObj->subtype & MB_NEED_SEND)) if (settedValue && !(execObj->subtype & MB_NEED_SEND))
settedValue->valueint=param; settedValue->valueint=param;
@@ -696,7 +700,7 @@ int out_Modbus::Poll(short cause)
if (cause==POLLING_SLOW) return 0; if (cause==POLLING_SLOW) return 0;
bool lineInitialized = false; bool lineInitialized = false;
if (modbusBusy || (Status() != CST_INITIALIZED) || ( mbusSlenceTimer && !isTimeOver(mbusSlenceTimer,millis(),100))) return 0; if (modbusBusy || (Status() != CST_INITIALIZED) || ( mbusSlenceTimer && !isTimeOver(mbusSlenceTimer,millis(),200))) return 0;
aJsonObject * itemParametersObj = aJson.getArrayItem(item->itemArg, 2); aJsonObject * itemParametersObj = aJson.getArrayItem(item->itemArg, 2);
if (itemParametersObj && itemParametersObj->type ==aJson_Object) if (itemParametersObj && itemParametersObj->type ==aJson_Object)
@@ -731,7 +735,7 @@ if (itemParametersObj && itemParametersObj->type ==aJson_Object)
debugSerial<<"MBUS: SEND "<<item->itemArr->name<<" "; debugSerial<<"MBUS: SEND "<<item->itemArr->name<<" ";
sendRes = sendModbus(execObj->name,outValue); sendRes = sendModbus(execObj->name,outValue);
needResend = (savedValue != outValue->valueint); needResend = (savedValue != outValue->valueint);
while(needResend && mbusSlenceTimer && !isTimeOver(mbusSlenceTimer,millis(),100)) modbusIdle(); while(needResend && mbusSlenceTimer && !isTimeOver(mbusSlenceTimer,millis(),200)) modbusIdle();
} }
while (needResend); //repeat sending if target value changed while we're waited for mbus responce while (needResend); //repeat sending if target value changed while we're waited for mbus responce
@@ -768,7 +772,7 @@ if (itemParametersObj && itemParametersObj->type ==aJson_Object)
} }
if (isTimeOver(store->timestamp,millis(),store->pollingInterval) && ( !mbusSlenceTimer || isTimeOver(mbusSlenceTimer,millis(),100))) if (isTimeOver(store->timestamp,millis(),store->pollingInterval) && ( !mbusSlenceTimer || isTimeOver(mbusSlenceTimer,millis(),200)))
{ {
// Clean_up FLAG_SEND_ERROR flag // Clean_up FLAG_SEND_ERROR flag

View File

@@ -983,7 +983,10 @@ uint16_t getCRC(aJsonObject * in)
if (!in) return 0; if (!in) return 0;
CRCStream crcStream; CRCStream crcStream;
aJsonStream aJsonCrcStream = aJsonStream(&crcStream); aJsonStream aJsonCrcStream = aJsonStream(&crcStream);
aJson.print(in,&aJsonCrcStream); debugSerial<<"CRC: in";
debugSerial.print(aJson.print(in));
aJson.print(in,&aJsonCrcStream,false);
debugSerial<<"\nCRC:"<<crcStream.getCRC16();
return crcStream.getCRC16(); return crcStream.getCRC16();
} }

View File

@@ -82,7 +82,7 @@ bool checkToken(char * token, char * data);
bool isProtectedPin(short pin); bool isProtectedPin(short pin);
bool i2cReset(); bool i2cReset();
uint16_t getCRC(aJsonObject * in); uint16_t getCRC(aJsonObject * in);
#include "util/crc16_.h" #include "util/crc16_.h"
class CRCStream : public Stream class CRCStream : public Stream
{ {
@@ -98,7 +98,7 @@ public:
virtual void flush(){}; virtual void flush(){};
// Print methods // Print methods
virtual size_t write(uint8_t c) {CRC16 = crc16_update(CRC16, c);}; virtual size_t write(uint8_t c) {CRC16 = crc16_update(CRC16, c);return 1;};
virtual int availableForWrite(){return 1;}; virtual int availableForWrite(){return 1;};
}; };

View File

@@ -820,7 +820,6 @@ lib_ignore =
DallasTemperature DallasTemperature
Adafruit Unified Sensor Adafruit Unified Sensor
DS2482_OneWire DS2482_OneWire
ModbusMaster
Syslog Syslog
;EEPROM ;EEPROM
ClosedCube HDC1080 ClosedCube HDC1080
@@ -843,6 +842,7 @@ lib_deps =
br3ttb/PID@^1.2.1 br3ttb/PID@^1.2.1
ArduinoMDNS ArduinoMDNS
https://github.com/khoih-prog/TimerInterrupt_Generic.git https://github.com/khoih-prog/TimerInterrupt_Generic.git
https://github.com/anklimov/ModbusMaster
monitor_speed = 115200 monitor_speed = 115200
@@ -888,7 +888,6 @@ lib_ignore =
DallasTemperature DallasTemperature
Adafruit Unified Sensor Adafruit Unified Sensor
DS2482_OneWire DS2482_OneWire
ModbusMaster
Syslog Syslog
NRFFlashStorage NRFFlashStorage
ClosedCube HDC1080 ClosedCube HDC1080
@@ -912,6 +911,7 @@ lib_deps =
ArduinoMDNS ArduinoMDNS
https://github.com/khoih-prog/TimerInterrupt_Generic.git https://github.com/khoih-prog/TimerInterrupt_Generic.git
;https://github.com/anklimov/ModbusMaster ;https://github.com/anklimov/ModbusMaster
https://github.com/anklimov/ModbusMaster
monitor_speed = 115200 monitor_speed = 115200
@@ -957,7 +957,6 @@ lib_ignore =
DallasTemperature DallasTemperature
Adafruit Unified Sensor Adafruit Unified Sensor
DS2482_OneWire DS2482_OneWire
ModbusMaster
Syslog Syslog
NRFFlashStorage NRFFlashStorage
ClosedCube HDC1080 ClosedCube HDC1080
@@ -980,7 +979,7 @@ lib_deps =
br3ttb/PID@^1.2.1 br3ttb/PID@^1.2.1
; ArduinoMDNS ; ArduinoMDNS
https://github.com/khoih-prog/TimerInterrupt_Generic.git https://github.com/khoih-prog/TimerInterrupt_Generic.git
;https://github.com/anklimov/ModbusMaster https://github.com/anklimov/ModbusMaster
pazi88/STM32_CAN pazi88/STM32_CAN
ericksimoes/Ultrasonic ericksimoes/Ultrasonic