From 75591fb201c75686e05054ca17670479d432080d Mon Sep 17 00:00:00 2001 From: Stephan Hadinger Date: Tue, 12 May 2020 21:13:46 +0200 Subject: [PATCH] Zigbee optimized state machine and support for better ZCL Step commands --- tasmota/xdrv_23_zigbee_6_commands.ino | 14 ++++-- tasmota/xdrv_23_zigbee_7_statemachine.ino | 56 +++++++++++++---------- tasmota/xdrv_23_zigbee_8_parsers.ino | 50 ++++++++++++++++++-- 3 files changed, 89 insertions(+), 31 deletions(-) diff --git a/tasmota/xdrv_23_zigbee_6_commands.ino b/tasmota/xdrv_23_zigbee_6_commands.ino index 8cb695bd6..e27f37f7a 100644 --- a/tasmota/xdrv_23_zigbee_6_commands.ino +++ b/tasmota/xdrv_23_zigbee_6_commands.ino @@ -49,13 +49,14 @@ ZF(ResetAlarm) ZF(ResetAllAlarms) ZF(HueSat) ZF(Color) ZF(ShutterOpen) ZF(ShutterClose) ZF(ShutterStop) ZF(ShutterLift) ZF(ShutterTilt) ZF(Shutter) //ZF(Occupancy) -ZF(DimmerMove) ZF(DimmerStep) -ZF(HueMove) ZF(HueStep) ZF(SatMove) ZF(SatStep) ZF(ColorMove) ZF(ColorStep) +ZF(DimmerMove) ZF(DimmerStep) ZF(DimmerStepUp) ZF(DimmerStepDown) +ZF(HueMove) ZF(HueStep) ZF(HueStepUp) ZF(HueStepDown) ZF(SatMove) ZF(SatStep) ZF(ColorMove) ZF(ColorStep) ZF(ColorTempStep) ZF(ColorTempStepUp) ZF(ColorTempStepDown) ZF(ArrowClick) ZF(ArrowHold) ZF(ArrowRelease) ZF(ZoneStatusChange) -ZF(xxxx00) ZF(xxxx) ZF(01xxxx) ZF(00) ZF(01) ZF() ZF(xxxxyy) ZF(00190200) ZF(01190200) ZF(xxyyyy) ZF(xx) +ZF(xxxx00) ZF(xxxx) ZF(01xxxx) ZF(03xxxx) ZF(00) ZF(01) ZF() ZF(xxxxyy) ZF(00190200) ZF(01190200) ZF(xxyyyy) ZF(xx) ZF(xx000A00) ZF(xx0A00) ZF(xxyy0A00) ZF(xxxxyyyy0A00) ZF(xxxx0A00) ZF(xx0A) ZF(xx190A00) ZF(xx19) ZF(xx190A) ZF(xxxxyyyy) ZF(xxxxyyzz) ZF(xxyyzzzz) ZF(xxyyyyzz) +ZF(00xx0A00) ZF(01xx0A00) ZF(03xx0A00) ZF(01xxxx0A0000000000) ZF(03xxxx0A0000000000) ZF(xxyyyy0A0000000000) // Cluster specific commands // Note: the table is both for sending commands, but also displaying received commands @@ -103,17 +104,24 @@ const Z_CommandConverter Z_Commands[] PROGMEM = { // Decoders only - normally not used to send, and names may be masked by previous definitions { Z(Dimmer), 0x0008, 0x00, 0x01, Z(xx) }, { Z(DimmerMove), 0x0008, 0x01, 0x01, Z(xx0A) }, + { Z(DimmerStepUp), 0x0008, 0x02, 0x01, Z(00xx0A00) }, + { Z(DimmerStepDown), 0x0008, 0x02, 0x01, Z(01xx0A00) }, { Z(DimmerStep), 0x0008, 0x02, 0x01, Z(xx190A00) }, { Z(DimmerMove), 0x0008, 0x05, 0x01, Z(xx0A) }, { Z(DimmerUp), 0x0008, 0x06, 0x01, Z(00) }, { Z(DimmerDown), 0x0008, 0x06, 0x01, Z(01) }, { Z(DimmerStop), 0x0008, 0x07, 0x01, Z() }, { Z(HueMove), 0x0300, 0x01, 0x01, Z(xx19) }, + { Z(HueStepUp), 0x0300, 0x02, 0x01, Z(01xx0A00) }, + { Z(HueStepDown), 0x0300, 0x02, 0x01, Z(03xx0A00) }, { Z(HueStep), 0x0300, 0x02, 0x01, Z(xx190A00) }, { Z(SatMove), 0x0300, 0x04, 0x01, Z(xx19) }, { Z(SatStep), 0x0300, 0x05, 0x01, Z(xx190A) }, { Z(ColorMove), 0x0300, 0x08, 0x01, Z(xxxxyyyy) }, { Z(ColorStep), 0x0300, 0x09, 0x01, Z(xxxxyyyy0A00) }, + { Z(ColorTempStepUp), 0x0300, 0x4C, 0x01, Z(01xxxx0A0000000000) }, //xxxx = step + { Z(ColorTempStepDown),0x0300, 0x4C, 0x01, Z(03xxxx0A0000000000) }, //xxxx = step + { Z(ColorTempStep), 0x0300, 0x4C, 0x01, Z(xxyyyy0A0000000000) }, //xx = 0x01 up, 0x03 down, yyyy = step // Tradfri { Z(ArrowClick), 0x0005, 0x07, 0x01, Z(xx) }, // xx == 0x01 = left, 0x00 = right { Z(ArrowHold), 0x0005, 0x08, 0x01, Z(xx) }, // xx == 0x01 = left, 0x00 = right diff --git a/tasmota/xdrv_23_zigbee_7_statemachine.ino b/tasmota/xdrv_23_zigbee_7_statemachine.ino index e2f0f039d..3531ba0a7 100644 --- a/tasmota/xdrv_23_zigbee_7_statemachine.ino +++ b/tasmota/xdrv_23_zigbee_7_statemachine.ino @@ -109,7 +109,8 @@ const uint8_t ZIGBEE_LABEL_START_COORD = 11; // Start ZNP as coordinator const uint8_t ZIGBEE_LABEL_INIT_ROUTER = 12; // Start ZNP as router const uint8_t ZIGBEE_LABEL_START_ROUTER = 13; // Start ZNP as router const uint8_t ZIGBEE_LABEL_INIT_DEVICE = 14; // Start ZNP as end-device -const uint8_t ZIGBEE_LABEL_START_DEVICE = 15; // Start ZNP as end-device +// const uint8_t ZIGBEE_LABEL_START_DEVICE = 15; // Start ZNP as end-device - same as ZIGBEE_LABEL_START_ROUTER +const uint8_t ZIGBEE_LABEL_FACT_RESET_ROUTER_DEVICE_POST = 19; // common post configuration for router and device const uint8_t ZIGBEE_LABEL_READY = 20; // goto label 20 for main loop const uint8_t ZIGBEE_LABEL_MAIN_LOOP = 21; // main loop const uint8_t ZIGBEE_LABEL_PERMIT_JOIN_CLOSE = 30; // disable permit join @@ -283,6 +284,7 @@ ZBM(ZBS_WNV_ZNPHC, Z_SREQ | Z_SYS, SYS_OSAL_NV_WRITE, Z_B0(ZNP_HAS_CONFIGURED), // Z_ZDO:startupFromApp ZBM(ZBS_STARTUPFROMAPP, Z_SREQ | Z_ZDO, ZDO_STARTUP_FROM_APP, 100, 0 /* delay */) // 25406400 ZBM(ZBR_STARTUPFROMAPP, Z_SRSP | Z_ZDO, ZDO_STARTUP_FROM_APP ) // 6540 + 01 for new network, 00 for exisitng network, 02 for error +ZBM(AREQ_STARTUPFROMAPP, Z_AREQ | Z_ZDO, ZDO_STATE_CHANGE_IND ) // 45C00xx - state change ZBM(AREQ_STARTUPFROMAPP_COORD, Z_AREQ | Z_ZDO, ZDO_STATE_CHANGE_IND, ZDO_DEV_ZB_COORD ) // 45C009 + 08 = starting, 09 = started ZBM(AREQ_STARTUPFROMAPP_ROUTER, Z_AREQ | Z_ZDO, ZDO_STATE_CHANGE_IND, ZDO_DEV_ROUTER ) // 45C009 + 02 = looking PanID, 07 = started ZBM(AREQ_STARTUPFROMAPP_DEVICE, Z_AREQ | Z_ZDO, ZDO_STATE_CHANGE_IND, ZDO_DEV_END_DEVICE ) // 45C009 + 02 = looking PanID, 06 = started @@ -456,7 +458,7 @@ static const Zigbee_Instruction zb_prog[] PROGMEM = { //ZI_LOG(LOG_LEVEL_INFO, D_LOG_ZIGBEE "starting zigbee coordinator") ZI_SEND(ZBS_STARTUPFROMAPP) // start coordinator ZI_WAIT_RECV(2000, ZBR_STARTUPFROMAPP) // wait for sync ack of command - ZI_WAIT_UNTIL(10000, AREQ_STARTUPFROMAPP_COORD) // wait for async message that coordinator started + ZI_WAIT_UNTIL_FUNC(10000, AREQ_STARTUPFROMAPP, &Z_ReceiveStateChange) // wait for async message that coordinator started ZI_SEND(ZBS_GETDEVICEINFO) // GetDeviceInfo ZI_WAIT_RECV_FUNC(2000, ZBR_GETDEVICEINFO, &Z_ReceiveDeviceInfo) //ZI_WAIT_RECV(2000, ZBR_GETDEVICEINFO) // memorize info @@ -542,7 +544,7 @@ static const Zigbee_Instruction zb_prog[] PROGMEM = { ZI_WAIT_RECV(1000, ZBR_AF_REGISTER) ZI_SEND(ZBS_STARTUPFROMAPP) // start router ZI_WAIT_RECV(2000, ZBR_STARTUPFROMAPP) // wait for sync ack of command - ZI_WAIT_UNTIL_FUNC(0xFFFF, AREQ_STARTUPFROMAPP_ROUTER, &Z_ReceiveStateChange) // wait for async message that coordinator started + ZI_WAIT_UNTIL_FUNC(0xFFFF, AREQ_STARTUPFROMAPP, &Z_ReceiveStateChange) // wait for async message that coordinator started ZI_SEND(ZBS_GETDEVICEINFO) // GetDeviceInfo ZI_WAIT_RECV_FUNC(2000, ZBR_GETDEVICEINFO, &Z_ReceiveDeviceInfo) ZI_GOTO(ZIGBEE_LABEL_READY) @@ -556,6 +558,7 @@ static const Zigbee_Instruction zb_prog[] PROGMEM = { ZI_WAIT_RECV(5000, ZBR_RESET) ZI_SEND(ZBS_W_LOGTYP_ROUTER) // write Logical Type = router ZI_WAIT_RECV(1000, ZBR_W_OK) + ZI_LABEL(ZIGBEE_LABEL_FACT_RESET_ROUTER_DEVICE_POST) ZI_SEND(ZBS_W_ALL_PAN) // write universal PAN ID = 0xFFFF ZI_WAIT_RECV(1000, ZBR_W_OK) ZI_SEND(ZBS_W_ALL_CHANN) // write Allows all CHANNELS = 0x07FFF800, 11-26 @@ -580,17 +583,19 @@ static const Zigbee_Instruction zb_prog[] PROGMEM = { ZI_SEND(ZBS_LOGTYPE) // check the logical type ZI_WAIT_RECV(1000, ZBS_LOGTYPE_DEVICE) // it should be coordinator - ZI_LABEL(ZIGBEE_LABEL_START_DEVICE) // Init as a router - ZI_MQTT_STATE(ZIGBEE_STATUS_STARTING, kConfiguredDevice) - ZI_ON_ERROR_GOTO(ZIGBEE_LABEL_ABORT) - ZI_SEND(ZBS_AF_REGISTER_ALL) // Z_AF register for endpoint 01, profile 0x0104 Home Automation - ZI_WAIT_RECV(1000, ZBR_AF_REGISTER) - ZI_SEND(ZBS_STARTUPFROMAPP) // start router - ZI_WAIT_RECV(2000, ZBR_STARTUPFROMAPP) // wait for sync ack of command - ZI_WAIT_UNTIL_FUNC(0xFFFF, AREQ_STARTUPFROMAPP_ROUTER, &Z_ReceiveStateChange) // wait forever for async message that coordinator started - ZI_SEND(ZBS_GETDEVICEINFO) // GetDeviceInfo - ZI_WAIT_RECV_FUNC(2000, ZBR_GETDEVICEINFO, &Z_ReceiveDeviceInfo) - ZI_GOTO(ZIGBEE_LABEL_READY) + ZI_GOTO(ZIGBEE_LABEL_START_ROUTER) + // Device and Router code is common from now + // ZI_LABEL(ZIGBEE_LABEL_START_DEVICE) // Init as a router + // ZI_MQTT_STATE(ZIGBEE_STATUS_STARTING, kConfiguredDevice) + // ZI_ON_ERROR_GOTO(ZIGBEE_LABEL_ABORT) + // ZI_SEND(ZBS_AF_REGISTER_ALL) // Z_AF register for endpoint 01, profile 0x0104 Home Automation + // ZI_WAIT_RECV(1000, ZBR_AF_REGISTER) + // ZI_SEND(ZBS_STARTUPFROMAPP) // start router + // ZI_WAIT_RECV(2000, ZBR_STARTUPFROMAPP) // wait for sync ack of command + // ZI_WAIT_UNTIL_FUNC(0xFFFF, AREQ_STARTUPFROMAPP, &Z_ReceiveStateChange) // wait forever for async message that coordinator started + // ZI_SEND(ZBS_GETDEVICEINFO) // GetDeviceInfo + // ZI_WAIT_RECV_FUNC(2000, ZBR_GETDEVICEINFO, &Z_ReceiveDeviceInfo) + // ZI_GOTO(ZIGBEE_LABEL_READY) ZI_LABEL(ZIGBEE_LABEL_FACT_RESET_DEVICE) // Factory reset for router ZI_MQTT_STATE(ZIGBEE_STATUS_RESET_CONF, kResetting) @@ -601,18 +606,21 @@ static const Zigbee_Instruction zb_prog[] PROGMEM = { ZI_WAIT_RECV(5000, ZBR_RESET) ZI_SEND(ZBS_W_LOGTYP_DEVICE) // write Logical Type = router ZI_WAIT_RECV(1000, ZBR_W_OK) - ZI_SEND(ZBS_W_ALL_PAN) // write universal PAN ID = 0xFFFF - ZI_WAIT_RECV(1000, ZBR_W_OK) - ZI_SEND(ZBS_W_ALL_CHANN) // write Allows all CHANNELS = 0x07FFF800, 11-26 - ZI_WAIT_RECV(1000, ZBR_W_OK) + ZI_GOTO(ZIGBEE_LABEL_FACT_RESET_ROUTER_DEVICE_POST) - // Now mark the device as ready, writing 0x55 in memory slot 0x0F00 - ZI_SEND(ZBS_WNV_INITZNPHC) // Init NV ZNP Has Configured - ZI_WAIT_RECV_FUNC(1000, ZBR_WNV_INIT_OK, &Z_CheckNVWrite) - ZI_SEND(ZBS_WNV_ZNPHC) // Write NV ZNP Has Configured - ZI_WAIT_RECV(1000, ZBR_WNV_OK) + // ZI_SEND(ZBS_W_ALL_PAN) // write universal PAN ID = 0xFFFF + // ZI_WAIT_RECV(1000, ZBR_W_OK) + // ZI_SEND(ZBS_W_ALL_CHANN) // write Allows all CHANNELS = 0x07FFF800, 11-26 + // ZI_WAIT_RECV(1000, ZBR_W_OK) - ZI_GOTO(ZIGBEE_LABEL_START_DEVICE) + // // Now mark the device as ready, writing 0x55 in memory slot 0x0F00 + // ZI_SEND(ZBS_WNV_INITZNPHC) // Init NV ZNP Has Configured + // ZI_WAIT_RECV_FUNC(1000, ZBR_WNV_INIT_OK, &Z_CheckNVWrite) + // ZI_SEND(ZBS_WNV_ZNPHC) // Write NV ZNP Has Configured + // ZI_WAIT_RECV(1000, ZBR_WNV_OK) + + // ZI_GOTO(ZIGBEE_LABEL_START_ROUTER) + // ZI_GOTO(ZIGBEE_LABEL_START_DEVICE) ZI_LABEL(ZIGBEE_LABEL_UNSUPPORTED_VERSION) diff --git a/tasmota/xdrv_23_zigbee_8_parsers.ino b/tasmota/xdrv_23_zigbee_8_parsers.ino index 96f6ecd9d..cae5441b7 100644 --- a/tasmota/xdrv_23_zigbee_8_parsers.ino +++ b/tasmota/xdrv_23_zigbee_8_parsers.ino @@ -321,20 +321,62 @@ int32_t Z_DataConfirm(int32_t res, const class SBuffer &buf) { // // Handle State Change Indication incoming message // +// Reference: +// 0x00: Initialized - not started automatically +// 0x01: Initialized - not connected to anything +// 0x02: Discovering PAN's to join +// 0x03: Joining a PAN +// 0x04: Rejoining a PAN, only for end devices +// 0x05: Joined but not yet authenticated by trust center +// 0x06: Started as device after authentication +// 0x07: Device joined, authenticated and is a router +// 0x08: Starting as ZigBee Coordinator +// 0x09: Started as ZigBee Coordinator +// 0x0A: Device has lost information about its parent int32_t Z_ReceiveStateChange(int32_t res, const class SBuffer &buf) { uint8_t state = buf.get8(2); + const char * msg = nullptr; - if (ZDO_DEV_NWK_DISC == state) { + switch (state) { + case ZDO_DEV_NWK_DISC: // 0x02 + msg = PSTR("Scanning Zigbee network"); + break; + case ZDO_DEV_NWK_JOINING: // 0x03 + case ZDO_DEV_NWK_REJOIN: // 0x04 + msg = PSTR("Joining a PAN"); + break; + case ZDO_DEV_END_DEVICE_UNAUTH: // 0x05 + msg = PSTR("Joined, not yet authenticated"); + break; + case ZDO_DEV_END_DEVICE: // 0x06 + msg = PSTR("Started as device"); + break; + case ZDO_DEV_ROUTER: // 0x07 + msg = PSTR("Started as router"); + break; + case ZDO_DEV_ZB_COORD: // 0x09 + msg = PSTR("Started as coordinator"); + break; + case ZDO_DEV_NWK_ORPHAN: // 0x0A + msg = PSTR("Device has lost its parent"); + break; + }; + + if (msg) { Response_P(PSTR("{\"" D_JSON_ZIGBEE_STATE "\":{" - "\"Status\":%d,\"Message\":\"%s\"}}"), - ZIGBEE_STATUS_SCANNING, PSTR("Scanning Zigbee network") + "\"Status\":%d,\"NewState\":%d,\"Message\":\"%s\"}}"), + ZIGBEE_STATUS_SCANNING, state, msg ); MqttPublishPrefixTopic_P(RESULT_OR_TELE, PSTR(D_JSON_ZIGBEEZCL_RECEIVED)); XdrvRulesProcess(); } - return res; + if ((ZDO_DEV_END_DEVICE == state) || (ZDO_DEV_ROUTER == state) || (ZDO_DEV_ZB_COORD == state)) { + return 0; // device sucessfully started + } else { + return -1; // ignore + } } //