diff --git a/tasmota/xdrv_23_zigbee_1z_libs.ino b/tasmota/xdrv_23_zigbee_1z_libs.ino index a9d6ef244..65ea266d2 100644 --- a/tasmota/xdrv_23_zigbee_1z_libs.ino +++ b/tasmota/xdrv_23_zigbee_1z_libs.ino @@ -166,6 +166,10 @@ public: void setBuf(const SBuffer &buf, size_t index, size_t len); + // specific formatters + void setHex32(uint32_t _val); + void setHex64(uint64_t _val); + // set the string value // PMEM argument is allowed // string will be copied, so it can be changed later @@ -388,6 +392,19 @@ void Z_attribute::setBuf(const SBuffer &buf, size_t index, size_t len) { type = Za_type::Za_raw; } +void Z_attribute::setHex32(uint32_t _val) { + char hex[8]; + snprintf_P(hex, sizeof(hex), PSTR("0x%04X"), _val); + setStr(hex); +} +void Z_attribute::setHex64(uint64_t _val) { + char hex[22]; + hex[0] = '0'; // prefix with '0x' + hex[1] = 'x'; + Uint64toHex(_val, &hex[2], 64); + setStr(hex); +} + // set the string value // PMEM argument is allowed // string will be copied, so it can be changed later diff --git a/tasmota/xdrv_23_zigbee_2_devices.ino b/tasmota/xdrv_23_zigbee_2_devices.ino index 4271c0d42..3c6137bc5 100644 --- a/tasmota/xdrv_23_zigbee_2_devices.ino +++ b/tasmota/xdrv_23_zigbee_2_devices.ino @@ -912,6 +912,7 @@ public: // Dump json String dumpDevice(uint32_t dump_mode, const Z_Device & device) const; + String dumpCoordinator(void) const; int32_t deviceRestore(JsonParserObject json); // Hue support @@ -950,7 +951,7 @@ public: void clean(void); // avoid writing to flash the last changes // Find device by name, can be short_addr, long_addr, number_in_array or name - Z_Device & parseDeviceFromName(const char * param, bool short_must_be_known = false); + Z_Device & parseDeviceFromName(const char * param, uint16_t * parsed_shortaddr = nullptr); bool isTuyaProtocol(uint16_t shortaddr, uint8_t ep = 0) const; diff --git a/tasmota/xdrv_23_zigbee_2a_devices_impl.ino b/tasmota/xdrv_23_zigbee_2a_devices_impl.ino index ae5e7e469..396f8ec06 100644 --- a/tasmota/xdrv_23_zigbee_2a_devices_impl.ino +++ b/tasmota/xdrv_23_zigbee_2a_devices_impl.ino @@ -588,12 +588,15 @@ void Z_Devices::clean(void) { // - a long address starting with "0x", example: 0x7CB03EBB0A0292DD // - a number 0..99, the index number in ZigbeeStatus // - a friendly name, between quotes, example: "Room_Temp" -Z_Device & Z_Devices::parseDeviceFromName(const char * param, bool short_must_be_known) { +// +// In case the device is not found, the parsed 0x.... short address is passed to *parsed_shortaddr +Z_Device & Z_Devices::parseDeviceFromName(const char * param, uint16_t * parsed_shortaddr) { if (nullptr == param) { return device_unk; } size_t param_len = strlen(param); char dataBuf[param_len + 1]; strcpy(dataBuf, param); RemoveSpace(dataBuf); + if (parsed_shortaddr != nullptr) { *parsed_shortaddr = BAD_SHORTADDR; } // if it goes wrong, mark as bad if ((dataBuf[0] >= '0') && (dataBuf[0] <= '9') && (strlen(dataBuf) < 4)) { // simple number 0..99 @@ -607,11 +610,8 @@ Z_Device & Z_Devices::parseDeviceFromName(const char * param, bool short_must_be if (strlen(dataBuf) < 18) { // expect a short address uint16_t shortaddr = strtoull(dataBuf, nullptr, 0); - if (short_must_be_known) { - return (Z_Device&) findShortAddr(shortaddr); // if not found, it reverts to the unknown_device with address BAD_SHORTADDR - } else { - return getShortAddr(shortaddr); // create it if not registered - } + if (parsed_shortaddr != nullptr) { *parsed_shortaddr = shortaddr; } // return the parsed shortaddr even if the device doesn't exist + return (Z_Device&) findShortAddr(shortaddr); // if not found, it reverts to the unknown_device with address BAD_SHORTADDR } else { // expect a long address uint64_t longaddr = strtoull(dataBuf, nullptr, 0); @@ -632,12 +632,10 @@ Z_Device & Z_Devices::parseDeviceFromName(const char * param, bool short_must_be // Add "Device":"0x1234","Name":"FrienflyName" void Z_Device::jsonAddDeviceNamme(Z_attribute_list & attr_list) const { - char hex[8]; const char * fname = friendlyName; bool use_fname = (Settings.flag4.zigbee_use_names) && (fname); // should we replace shortaddr with friendlyname? - snprintf_P(hex, sizeof(hex), PSTR("0x%04X"), shortaddr); - attr_list.addAttributePMEM(PSTR(D_JSON_ZIGBEE_DEVICE)).setStr(hex); + attr_list.addAttributePMEM(PSTR(D_JSON_ZIGBEE_DEVICE)).setHex32(shortaddr); if (fname) { attr_list.addAttributePMEM(PSTR(D_JSON_ZIGBEE_NAME)).setStr(fname); } @@ -645,11 +643,7 @@ void Z_Device::jsonAddDeviceNamme(Z_attribute_list & attr_list) const { // Add "IEEEAddr":"0x1234567812345678" void Z_Device::jsonAddIEEE(Z_attribute_list & attr_list) const { - char hex[22]; - hex[0] = '0'; // prefix with '0x' - hex[1] = 'x'; - Uint64toHex(longaddr, &hex[2], 64); - attr_list.addAttributePMEM(PSTR("IEEEAddr")).setStr(hex); + attr_list.addAttributePMEM(PSTR("IEEEAddr")).setHex64(longaddr); } // Add "ModelId":"","Manufacturer":"" void Z_Device::jsonAddModelManuf(Z_attribute_list & attr_list) const { @@ -751,6 +745,17 @@ void Z_Device::jsonDumpSingleDevice(Z_attribute_list & attr_list, uint32_t dump_ } } +// Dump coordinator specific data +String Z_Devices::dumpCoordinator(void) const { + Z_attribute_list attr_list; + + attr_list.addAttributePMEM(PSTR(D_JSON_ZIGBEE_DEVICE)).setHex32(localShortAddr); + attr_list.addAttributePMEM(PSTR("IEEEAddr")).setHex64(localIEEEAddr); + attr_list.addAttributePMEM(PSTR("TotalDevices")).setUInt(zigbee_devices.devicesSize()); + + return attr_list.toString(); +} + // If &device == nullptr, then dump all String Z_Devices::dumpDevice(uint32_t dump_mode, const Z_Device & device) const { JsonGeneratorArray json_arr; diff --git a/tasmota/xdrv_23_zigbee_5_converters.ino b/tasmota/xdrv_23_zigbee_5_converters.ino index ec4901fe4..acc4105d8 100644 --- a/tasmota/xdrv_23_zigbee_5_converters.ino +++ b/tasmota/xdrv_23_zigbee_5_converters.ino @@ -2063,8 +2063,8 @@ void Z_Data::toAttributes(Z_attribute_list & attr_list) const { case Zenum16: case Zuint16: uval32 = *(uint16_t*)attr_address; if (uval32 != 0x0000FFFF) data_size = 16; break; case Zuint32: uval32 = *(uint32_t*)attr_address; if (uval32 != 0xFFFFFFFF) data_size = 32; break; - case Zint8: ival32 = *(int8_t*)attr_address; if (ival32 != -0xFFFFFF80) data_size = -8; break; - case Zint16: ival32 = *(int16_t*)attr_address; if (ival32 != -0xFFFF8000) data_size = -16; break; + case Zint8: ival32 = *(int8_t*)attr_address; if (ival32 != -0x80) data_size = -8; break; + case Zint16: ival32 = *(int16_t*)attr_address; if (ival32 != -0x8000) data_size = -16; break; case Zint32: ival32 = *(int32_t*)attr_address; if (ival32 != -0x80000000) data_size = -32; break; } if (data_size != 0) { diff --git a/tasmota/xdrv_23_zigbee_A_impl.ino b/tasmota/xdrv_23_zigbee_A_impl.ino index a3525746e..34d4a770c 100644 --- a/tasmota/xdrv_23_zigbee_A_impl.ino +++ b/tasmota/xdrv_23_zigbee_A_impl.ino @@ -734,7 +734,7 @@ void CmndZbSend(void) { // parse "Device" and "Group" JsonParserToken val_device = root[PSTR(D_CMND_ZIGBEE_DEVICE)]; if (val_device) { - device = zigbee_devices.parseDeviceFromName(val_device.getStr(), true).shortaddr; + device = zigbee_devices.parseDeviceFromName(val_device.getStr()).shortaddr; if (BAD_SHORTADDR == device) { ResponseCmndChar_P(PSTR("Invalid parameter")); return; } } if (BAD_SHORTADDR == device) { // if not found, check if we have a group @@ -877,7 +877,7 @@ void ZbBindUnbind(bool unbind) { // false = bind, true = unbind // Information about source device: "Device", "Endpoint", "Cluster" // - the source endpoint must have a known IEEE address - const Z_Device & src_device = zigbee_devices.parseDeviceFromName(root.getStr(PSTR(D_CMND_ZIGBEE_DEVICE), nullptr), true); + const Z_Device & src_device = zigbee_devices.parseDeviceFromName(root.getStr(PSTR(D_CMND_ZIGBEE_DEVICE), nullptr)); if (!src_device.valid()) { ResponseCmndChar_P(PSTR("Unknown source device")); return; } // check if IEEE address is known uint64_t srcLongAddr = src_device.longaddr; @@ -912,7 +912,7 @@ void ZbBindUnbind(bool unbind) { // false = bind, true = unbind } if (dst_device) { - const Z_Device & dstDevice = zigbee_devices.parseDeviceFromName(dst_device.getStr(nullptr), true); + const Z_Device & dstDevice = zigbee_devices.parseDeviceFromName(dst_device.getStr(nullptr)); if (!dstDevice.valid()) { ResponseCmndChar_P(PSTR("Unknown dest device")); return; } dstLongAddr = dstDevice.longaddr; } @@ -990,7 +990,7 @@ void CmndZbUnbind(void) { // void CmndZbLeave(void) { if (zigbee.init_phase) { ResponseCmndChar_P(PSTR(D_ZIGBEE_NOT_STARTED)); return; } - uint16_t shortaddr = zigbee_devices.parseDeviceFromName(XdrvMailbox.data, true).shortaddr; + uint16_t shortaddr = zigbee_devices.parseDeviceFromName(XdrvMailbox.data).shortaddr; if (BAD_SHORTADDR == shortaddr) { ResponseCmndChar_P(PSTR("Unknown device")); return; } #ifdef USE_ZIGBEE_ZNP @@ -1019,11 +1019,26 @@ void CmndZbLeave(void) { -void CmndZbBindState_or_Map(uint16_t zdo_cmd) { +void CmndZbBindState_or_Map(bool map) { if (zigbee.init_phase) { ResponseCmndChar_P(PSTR(D_ZIGBEE_NOT_STARTED)); return; } - uint16_t shortaddr = zigbee_devices.parseDeviceFromName(XdrvMailbox.data, true).shortaddr; - if (BAD_SHORTADDR == shortaddr) { ResponseCmndChar_P(PSTR("Unknown device")); return; } + uint16_t parsed_shortaddr;; + uint16_t shortaddr = zigbee_devices.parseDeviceFromName(XdrvMailbox.data, &parsed_shortaddr).shortaddr; + if (BAD_SHORTADDR == shortaddr) { + if ((map) && (parsed_shortaddr != shortaddr)) { + shortaddr = parsed_shortaddr; // allow a non-existent address when ZbMap + } else { + ResponseCmndChar_P(PSTR("Unknown device")); + return; + } + } uint8_t index = XdrvMailbox.index - 1; // change default 1 to 0 + uint16_t zdo_cmd; +#ifdef USE_ZIGBEE_ZNP + zdo_cmd = map ? ZDO_MGMT_LQI_REQ : ZDO_MGMT_BIND_REQ; +#endif // USE_ZIGBEE_ZNP +#ifdef USE_ZIGBEE_EZSP + zdo_cmd = map ? ZDO_Mgmt_Lqi_req : ZDO_Mgmt_Bind_req; +#endif // USE_ZIGBEE_EZSP Z_Send_State_or_Map(shortaddr, index, zdo_cmd); ResponseCmndDone(); @@ -1034,12 +1049,7 @@ void CmndZbBindState_or_Map(uint16_t zdo_cmd) { // `ZbBindState` as index if it does not fit. If default, `1` starts at the beginning // void CmndZbBindState(void) { -#ifdef USE_ZIGBEE_ZNP - CmndZbBindState_or_Map(ZDO_MGMT_BIND_REQ); -#endif // USE_ZIGBEE_ZNP -#ifdef USE_ZIGBEE_EZSP - CmndZbBindState_or_Map(ZDO_Mgmt_Bind_req); -#endif // USE_ZIGBEE_EZSP + CmndZbBindState_or_Map(false); } // @@ -1062,12 +1072,7 @@ void CmndZbMap(void) { zigbee_devices.setTimer(BAD_SHORTADDR, 0, wait_ms, 0, 0, Z_CAT_ALWAYS, 0 /* value = index */, &Z_Map); ResponseCmndDone(); } else { -#ifdef USE_ZIGBEE_ZNP - CmndZbBindState_or_Map(ZDO_MGMT_LQI_REQ); -#endif // USE_ZIGBEE_ZNP -#ifdef USE_ZIGBEE_EZSP - CmndZbBindState_or_Map(ZDO_Mgmt_Lqi_req); -#endif // USE_ZIGBEE_EZSP + CmndZbBindState_or_Map(true); } } @@ -1081,7 +1086,7 @@ void CmndZbProbe(void) { // void CmndZbProbeOrPing(boolean probe) { if (zigbee.init_phase) { ResponseCmndChar_P(PSTR(D_ZIGBEE_NOT_STARTED)); return; } - uint16_t shortaddr = zigbee_devices.parseDeviceFromName(XdrvMailbox.data, true).shortaddr; + uint16_t shortaddr = zigbee_devices.parseDeviceFromName(XdrvMailbox.data).shortaddr; if (BAD_SHORTADDR == shortaddr) { ResponseCmndChar_P(PSTR("Unknown device")); return; } // set a timer for Reachable - 2s default value @@ -1119,7 +1124,7 @@ void CmndZbName(void) { strtok_r(XdrvMailbox.data, ",", &p); // parse first part, - Z_Device & device = zigbee_devices.parseDeviceFromName(XdrvMailbox.data, false); // it's the only case where we create a new device + Z_Device & device = zigbee_devices.parseDeviceFromName(XdrvMailbox.data); // it's the only case where we create a new device if (!device.valid()) { ResponseCmndChar_P(PSTR("Unknown device")); return; } if (p == nullptr) { @@ -1151,7 +1156,7 @@ void CmndZbModelId(void) { strtok_r(XdrvMailbox.data, ",", &p); // parse first part, - Z_Device & device = zigbee_devices.parseDeviceFromName(XdrvMailbox.data, true); // in case of short_addr, it must be already registered + Z_Device & device = zigbee_devices.parseDeviceFromName(XdrvMailbox.data); // in case of short_addr, it must be already registered if (!device.valid()) { ResponseCmndChar_P(PSTR("Unknown device")); return; } if (p != nullptr) { @@ -1178,7 +1183,7 @@ void CmndZbLight(void) { strtok_r(XdrvMailbox.data, ", ", &p); // parse first part, - Z_Device & device = zigbee_devices.parseDeviceFromName(XdrvMailbox.data, true); // in case of short_addr, it must be already registered + Z_Device & device = zigbee_devices.parseDeviceFromName(XdrvMailbox.data); // in case of short_addr, it must be already registered if (!device.valid()) { ResponseCmndChar_P(PSTR("Unknown device")); return; } if (p) { @@ -1222,7 +1227,7 @@ void CmndZbOccupancy(void) { strtok_r(XdrvMailbox.data, ", ", &p); // parse first part, - Z_Device & device = zigbee_devices.parseDeviceFromName(XdrvMailbox.data, true); // in case of short_addr, it must be already registered + Z_Device & device = zigbee_devices.parseDeviceFromName(XdrvMailbox.data); // in case of short_addr, it must be already registered if (!device.valid()) { ResponseCmndChar_P(PSTR("Unknown device")); return; } int8_t occupancy_time = -1; @@ -1249,7 +1254,7 @@ void CmndZbOccupancy(void) { // void CmndZbForget(void) { if (zigbee.init_phase) { ResponseCmndChar_P(PSTR(D_ZIGBEE_NOT_STARTED)); return; } - Z_Device & device = zigbee_devices.parseDeviceFromName(XdrvMailbox.data, true); // in case of short_addr, it must be already registered + Z_Device & device = zigbee_devices.parseDeviceFromName(XdrvMailbox.data); // in case of short_addr, it must be already registered if (!device.valid()) { ResponseCmndChar_P(PSTR("Unknown device")); return; } // everything is good, we can send the command @@ -1264,16 +1269,30 @@ void CmndZbForget(void) { // Command `ZbInfo` // Display all information known about a device, this equivalent to `2bStatus3` with a simpler JSON output // +void CmndZbInfo_inner(const Z_Device & device) { + Z_attribute_list attr_list; + device.jsonDumpSingleDevice(attr_list, 3, false); // don't add Device/Name + device.jsonPublishAttrList(PSTR(D_JSON_ZIGBEE_INFO), attr_list); // publish as ZbReceived +} void CmndZbInfo(void) { if (zigbee.init_phase) { ResponseCmndChar_P(PSTR(D_ZIGBEE_NOT_STARTED)); return; } - Z_Device & device = zigbee_devices.parseDeviceFromName(XdrvMailbox.data, true); // in case of short_addr, it must be already registered - if (!device.valid()) { ResponseCmndChar_P(PSTR("Unknown device")); return; } + RemoveSpace(XdrvMailbox.data); - // everything is good, we can send the command + if (strlen(XdrvMailbox.data) == 0) { + // if empty, dump for all values + for (const auto & device : zigbee_devices.getDevices()) { + CmndZbInfo_inner(device); + } + } else { // try JSON + Z_Device & device = zigbee_devices.parseDeviceFromName(XdrvMailbox.data); // in case of short_addr, it must be already registered + if (!device.valid()) { ResponseCmndChar_P(PSTR("Unknown device")); return; } - Z_attribute_list attr_list; - device.jsonDumpSingleDevice(attr_list, 3, false); // don't add Device/Name - device.jsonPublishAttrList(PSTR(D_JSON_ZIGBEE_INFO), attr_list); // publish as ZbReceived + // everything is good, we can send the command + + Z_attribute_list attr_list; + device.jsonDumpSingleDevice(attr_list, 3, false); // don't add Device/Name + device.jsonPublishAttrList(PSTR(D_JSON_ZIGBEE_INFO), attr_list); // publish as ZbReceived + } ResponseCmndDone(); } @@ -1502,13 +1521,17 @@ void CmndZbStatus(void) { if (zigbee.init_phase) { ResponseCmndChar_P(PSTR(D_ZIGBEE_NOT_STARTED)); return; } String dump; - Z_Device & device = zigbee_devices.parseDeviceFromName(XdrvMailbox.data, true); - if (XdrvMailbox.data_len > 0) { - if (!device.valid()) { ResponseCmndChar_P(PSTR("Unknown device")); return; } - dump = zigbee_devices.dumpDevice(XdrvMailbox.index, device); + if (0 == XdrvMailbox.index) { + dump = zigbee_devices.dumpCoordinator(); } else { - if (XdrvMailbox.index >= 2) { ResponseCmndChar_P(PSTR("Unknown device")); return; } - dump = zigbee_devices.dumpDevice(XdrvMailbox.index, *(Z_Device*)nullptr); + Z_Device & device = zigbee_devices.parseDeviceFromName(XdrvMailbox.data); + if (XdrvMailbox.data_len > 0) { + if (!device.valid()) { ResponseCmndChar_P(PSTR("Unknown device")); return; } + dump = zigbee_devices.dumpDevice(XdrvMailbox.index, device); + } else { + if (XdrvMailbox.index >= 2) { ResponseCmndChar_P(PSTR("Unknown device")); return; } + dump = zigbee_devices.dumpDevice(XdrvMailbox.index, *(Z_Device*)nullptr); + } } Response_P(PSTR("{\"%s%d\":%s}"), XdrvMailbox.command, XdrvMailbox.index, dump.c_str()); @@ -1533,7 +1556,7 @@ void CmndZbData(void) { strtok_r(XdrvMailbox.data, ",", &p); // parse first part, - Z_Device & device = zigbee_devices.parseDeviceFromName(XdrvMailbox.data, true); // in case of short_addr, it must be already registered + Z_Device & device = zigbee_devices.parseDeviceFromName(XdrvMailbox.data); // in case of short_addr, it must be already registered if (!device.valid()) { ResponseCmndChar_P(PSTR("Unknown device")); return; } if (p) {