diff --git a/lighthub/candriver.cpp b/lighthub/candriver.cpp index bef8e3a..56dc4c1 100644 --- a/lighthub/candriver.cpp +++ b/lighthub/candriver.cpp @@ -29,7 +29,7 @@ extern bool configLoaded; void printFrame(datagram_t * frame, uint8_t len ) { - debugSerial.print(" Data: 0x"); + debugSerial.print(" Data:"); for (int count = 0; count < len; count++) { debugSerial.print(frame->data[count], HEX); debugSerial.print(" "); @@ -40,7 +40,7 @@ void printFrame(datagram_t * frame, uint8_t len ) { bool canDriver::upTime(uint32_t ut) { - // return 0; + if (!controllerId) return false; canid_t id; datagram_t packet; @@ -48,7 +48,8 @@ bool canDriver::upTime(uint32_t ut) id.status=1; id.payloadType=payloadType::metric; id.deviceId=controllerId; - id.itemId=metricType::UpTime; + id.subjId=metricType::UpTime; + packet.metric1=ut; @@ -58,6 +59,7 @@ bool canDriver::upTime(uint32_t ut) bool canDriver::salt(uint32_t salt) { + if (!controllerId) return false; canid_t id; datagram_t packet; @@ -65,7 +67,9 @@ bool canDriver::salt(uint32_t salt) id.status=1; id.payloadType=payloadType::metric; id.deviceId=controllerId; - id.itemId=metricType::Salt; + id.subjId=metricType::Salt; + + packet.metric1=salt; @@ -84,7 +88,9 @@ bool canDriver::lookupMAC() id.status=0; id.payloadType=payloadType::lookupMAC; id.deviceId=0; - id.itemId=0; //CRC? + id.subjId=0; //CRC? + + memcpy(packet.mac,sysConf.mac,6); @@ -107,7 +113,9 @@ bool canDriver::requestFrame(uint8_t devId, payloadType _payloadType, uint16_t s id.status=0; id.payloadType=_payloadType; id.deviceId=devId; - id.itemId=seqNo; + id.subjId=seqNo; + + packet.metric1 =0; //memcpy(packet.mac,sysConf.mac,6); //delay (100); @@ -131,7 +139,7 @@ bool canDriver::sendRemoteID(macAddress mac) id.status=1; //response id.payloadType=payloadType::lookupMAC; - id.itemId=200; //CRC16 of remote config + id.subjId=200; //CRC16 of remote config //packet.data[0]=1; debugSerial<<("CAN: Send remote ID")<cmd; ic.param = packet->param; - - debugSerial<mac); //debugSerial<<"ID requested"<type == aJson_Array)) { + int subItem=NO_SUBITEM; aJsonObject * dev = aJson.getArrayItem(can,0); aJsonObject * it = aJson.getArrayItem(can,1); aJsonObject * sfx = aJson.getArrayItem(can,2); + aJsonObject * subItemObj = aJson.getArrayItem(can,3); if (sfx) switch (sfx->type) { @@ -602,14 +648,15 @@ bool canDriver::sendCommand(aJsonObject * can,itemCmd cmd, bool status) int suffix=txt2subItem(sfx->valuestring); if (suffix) cmd.setSuffix(suffix); } - debugSerial<valueint << ":" << it->valueint<type==aJson_Int && subItemObj->valueint>=0 && subItemObj->valueint<63) subItem=subItemObj->valueint; + if (dev && it && dev->type == aJson_Int && it->type == aJson_Int) - return sendCommand(dev->valueint, it->valueint, cmd, status); + return sendCommand(dev->valueint, it->valueint, cmd, status,subItem); } return false; } -bool canDriver::sendCommand(uint8_t devID, uint16_t itemID, itemCmd cmd, bool status) +bool canDriver::sendCommand(uint8_t devID, uint16_t itemID,itemCmd cmd, bool status,int subItemID ) { canid_t id; datagram_t packet; @@ -621,18 +668,20 @@ bool canDriver::sendCommand(uint8_t devID, uint16_t itemID, itemCmd cmd, bool st id.payloadType=payloadType::itemCommand; id.deviceId=devID; id.itemId=itemID; + id.subItemId=subItemID; packet.cmd = cmd.cmd; packet.param = cmd.param; debugSerial << ((status)?"CAN: send Status":"CAN: send Command"); - debugSerial<[")<write (id.id, &writeBuffer, len); writePos=0; @@ -695,8 +744,8 @@ bool canDriver::sendCommand(uint8_t devID, uint16_t itemID, itemCmd cmd, bool st if ((c=driver->readFrame()>0) && (driver->RXid.deviceId == devId) && (driver ->RXid.payloadType == pType) && driver->RXid.status) { state = canState::FrameReceived; - debugSerial<RXlen<< " "<RXpacket.payload<<"#"<RXid.itemId<RXid.itemId; + debugSerial<RXlen<< " "<RXpacket.payload<<"#"<RXid.subjId<RXid.subjId; failedCount=0; return driver->RXlen; } @@ -729,8 +778,8 @@ bool canDriver::sendCommand(uint8_t devID, uint16_t itemID, itemCmd cmd, bool st (driver->RXid.status == 0) ) { - debugSerial<RXid.itemId<RXid.itemId) + debugSerial<RXid.subjId<RXid.subjId) { state = canState::StreamOpenedWrite; seqNo++; diff --git a/lighthub/candriver.h b/lighthub/candriver.h index fbe2878..cc7f030 100644 --- a/lighthub/candriver.h +++ b/lighthub/candriver.h @@ -21,8 +21,15 @@ typedef union { uint32_t id; struct - { - uint16_t itemId; + { union + { + struct + { + uint16_t subItemId:6; + uint16_t itemId:10; + }; + uint16_t subjId; + }; uint8_t deviceId; uint8_t payloadType:4; uint8_t status:1; @@ -30,6 +37,7 @@ typedef union }; } canid_t; +#define NO_SUBITEM 63 enum payloadType { unknown=0, @@ -112,8 +120,8 @@ class canDriver public: canDriver(){ready=false; controllerId=0; responseTimer=0; state=canState::stateUnknown;}; uint8_t getMyId(); -bool sendStatus(uint8_t itemNum, itemCmd cmd); -bool sendCommand(uint8_t devID, uint16_t itemID, itemCmd cmd, bool status=false); +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(aJsonObject * can,itemCmd cmd, bool status = false); bool upTime(uint32_t ut); bool salt(uint32_t salt); diff --git a/lighthub/item.cpp b/lighthub/item.cpp index 5b1384c..80cfa66 100644 --- a/lighthub/item.cpp +++ b/lighthub/item.cpp @@ -322,7 +322,7 @@ Item::Item(char *name, aJsonObject *_items) //Constructor Parse(); } -uint8_t getCanNum(aJsonObject* verb) +uint16_t getCanNum(aJsonObject* verb) { if (!verb) return 0; switch (verb->type) @@ -343,7 +343,52 @@ uint8_t getCanNum(aJsonObject* verb) return 0; } - Item::Item(uint16_t num, aJsonObject *_items) +char * Item::getSubItemStrById(uint8_t subItem) +{ +if (subItem == NO_SUBITEM) return NULL; +if (!itemArg) return NULL; +aJsonObject * i = itemArg; +if (i->type == aJson_Array) i=i->child; +while (i) +{ + if (i->type == aJson_Object) + { + aJsonObject * s = aJson.getArrayItem(i,subItem); + if (s && (s->type == aJson_Object)) return s->name; + return NULL; + } + i=i->next; +} +return NULL; +}; + + uint8_t Item::getSubitemId(char * subItem) +{ +if (!subItem) return NO_SUBITEM; +if (!itemArg) return NO_SUBITEM; + +aJsonObject * i = itemArg; +if (i->type == aJson_Array) i=i->child; + +while (i) +{ + if (i->type == aJson_Object) + { + aJsonObject *c = i->child; + uint8_t index=0; + while (c) + { + if (!strcasecmp(c->name, subItem)) return index; + c = c->next; index++; + } + return NO_SUBITEM; + } + i=i->next; +}; +return NO_SUBITEM; +} + + Item::Item(uint16_t num, uint8_t subItem, aJsonObject *_items) { itemArr = NULL; @@ -358,12 +403,15 @@ uint8_t getCanNum(aJsonObject* verb) if (!rootItems) return; itemArr = rootItems->child; + if (!num) return; while (itemArr) { if (getCanNum(itemArr->child) == num) { debugSerial<<"Find item: "<< itemArr->name << " addr:" << num << endl; Parse(); + char * subItemStr = getSubItemStrById(subItem); + if (subItemStr) strncpy(defaultSubItem,subItemStr,sizeof(defaultSubItem)); return; } itemArr = itemArr->next; @@ -1778,7 +1826,6 @@ int Item::SendStatus(int sendFlags) { itemCmd st(ST_VOID,CMD_VOID); st.loadItem(this, FLAG_COMMAND | FLAG_PARAMETERS); sendFlags |= getFlag(FLAG_COMMAND | FLAG_PARAMETERS | FLAG_FLAGS); //if some delayed status is pending - //debugSerial<child),st); + if (!(sendFlags & FLAG_NOT_SEND_CAN)) LHCAN.sendStatus(getCanNum(itemArr->child),st, getSubitemId(subItem)); #endif if (sendFlags & FLAG_COMMAND) @@ -1821,7 +1869,7 @@ int Item::SendStatus(int sendFlags) { break; default: - debugSerial<setCmd(cmd.cmdCode); + } if (optionsFlag & FLAG_PARAMETERS) switch (cmd.itemArgType) @@ -1310,7 +1311,7 @@ return false; { case aJson_Array: { - debugSerial<<"Array mapping"<child; //if first array element is not array - this is default mapping value if (i && i->type==aJson_Int) diff --git a/lighthub/itemCmd.h b/lighthub/itemCmd.h index 8176d69..19efef5 100644 --- a/lighthub/itemCmd.h +++ b/lighthub/itemCmd.h @@ -157,10 +157,9 @@ typedef union struct { uint8_t cmdCode; - uint8_t suffixCode:4; uint8_t itemArgType:4; - - uint8_t cmdEffect; //Reserve + uint8_t cmdEffect:4; //Reserve + uint8_t suffixCode; uint8_t cmdParam; //Reserve }; } itemCmdStore; diff --git a/lighthub/main.cpp b/lighthub/main.cpp index 7e826c7..649c588 100644 --- a/lighthub/main.cpp +++ b/lighthub/main.cpp @@ -1394,6 +1394,10 @@ topics = aJson.getObjectItem(root, "topics"); inputs = aJson.getObjectItem(root, "in"); brokersArr = aJson.getObjectItem(root, "mqtt"); +#ifdef CANDRV +LHCAN.begin(); //Applying updated CAN addr +#endif + if (brokersArr && brokersArr->child && (brokersArr->child->type == aJson_Array)) { infoSerial<