From 531ee120e0c4eb0705d32350073a789b2eb81ac1 Mon Sep 17 00:00:00 2001 From: Chris Jackson Date: Sat, 5 Oct 2024 13:47:16 +1300 Subject: [PATCH] Handle handler error during initialisation (#1440) * Handle handler error during initialisation Signed-off-by: Chris Jackson * Fix tests Signed-off-by: Chris Jackson --------- Signed-off-by: Chris Jackson --- .../zigbee/dongle/ember/ZigBeeDongleEzsp.java | 22 +++++++- .../dongle/ember/ZigBeeDongleEzspTest.java | 51 +++++++++++++++++++ .../ember/ZigBeeTransportTransmitTest.java | 14 ++++- 3 files changed, 84 insertions(+), 3 deletions(-) diff --git a/com.zsmartsystems.zigbee.dongle.ember/src/main/java/com/zsmartsystems/zigbee/dongle/ember/ZigBeeDongleEzsp.java b/com.zsmartsystems.zigbee.dongle.ember/src/main/java/com/zsmartsystems/zigbee/dongle/ember/ZigBeeDongleEzsp.java index 60f77402c..f63ff13b3 100644 --- a/com.zsmartsystems.zigbee.dongle.ember/src/main/java/com/zsmartsystems/zigbee/dongle/ember/ZigBeeDongleEzsp.java +++ b/com.zsmartsystems.zigbee.dongle.ember/src/main/java/com/zsmartsystems/zigbee/dongle/ember/ZigBeeDongleEzsp.java @@ -457,7 +457,13 @@ public ZigBeeStatus initialize() { stackConfigurer.setPolicy(stackPolicies); policies = stackConfigurer.getPolicy(stackPolicies.keySet()); for (Entry policy : policies.entrySet()) { - EzspDecisionId decisionId = EzspDecisionId.getEzspDecisionId(policy.getValue()); + EzspDecisionId decisionId = null; + try { + decisionId = EzspDecisionId.getEzspDecisionId(policy.getValue()); + } catch (Exception e) { + // Eat me! + // This is only for logging. + } logger.debug("Policy state {} = {} [{}]", policy.getKey(), decisionId, String.format("%02X", policy.getValue())); } @@ -465,7 +471,14 @@ public ZigBeeStatus initialize() { EmberNcp ncp = getEmberNcp(); // Get the current network parameters so that any configuration updates start from here - networkParameters = ncp.getNetworkParameters().getParameters(); + EzspGetNetworkParametersResponse networkParametersResponse = ncp.getNetworkParameters(); + if (networkParametersResponse == null) { + return ZigBeeStatus.COMMUNICATION_ERROR; + } + EmberNetworkParameters localNetworkParameters = networkParametersResponse.getParameters(); + if (localNetworkParameters != null) { + networkParameters = localNetworkParameters; + } logger.debug("Ember initial network parameters are {}", networkParameters); ieeeAddress = ncp.getIeeeAddress(); @@ -473,6 +486,11 @@ public ZigBeeStatus initialize() { ncp.getNetworkParameters(); + if (!frameHandler.isAlive()) { + logger.error("Ember frame handler is not alive after initialize!"); + return ZigBeeStatus.COMMUNICATION_ERROR; + } + isConfigured = true; logger.debug("EZSP Dongle: initialize done"); diff --git a/com.zsmartsystems.zigbee.dongle.ember/src/test/java/com/zsmartsystems/zigbee/dongle/ember/ZigBeeDongleEzspTest.java b/com.zsmartsystems.zigbee.dongle.ember/src/test/java/com/zsmartsystems/zigbee/dongle/ember/ZigBeeDongleEzspTest.java index 01c4eb48d..bdc7ffc56 100644 --- a/com.zsmartsystems.zigbee.dongle.ember/src/test/java/com/zsmartsystems/zigbee/dongle/ember/ZigBeeDongleEzspTest.java +++ b/com.zsmartsystems.zigbee.dongle.ember/src/test/java/com/zsmartsystems/zigbee/dongle/ember/ZigBeeDongleEzspTest.java @@ -728,4 +728,55 @@ public EmberNcp getEmberNcp() { assertNull(dongle.getTcLinkKey()); assertNotNull(dongle.getZigBeeNetworkKey()); } + + @Test + public void initializeNoNetorkParameters() { + System.out.println("--- " + Thread.currentThread().getStackTrace()[1].getMethodName()); + final EmberNcp ncp = Mockito.mock(EmberNcp.class); + final EzspVersionResponse version = Mockito.mock(EzspVersionResponse.class); + Mockito.when(version.getProtocolVersion()).thenReturn(4); + + Mockito.when(ncp.getVersion()).thenReturn(version); + + final ZigBeePort port = Mockito.mock(ZigBeePort.class); + Mockito.when(port.open()).thenReturn(true); + + ZigBeeDongleEzsp dongle = new ZigBeeDongleEzsp(port) { + @Override + public EmberNcp getEmberNcp() { + return ncp; + } + }; + + final ZigBeeTransportReceive receiver = Mockito.mock(ZigBeeTransportReceive.class); + + dongle.setZigBeeTransportReceive(receiver); + assertEquals(ZigBeeStatus.COMMUNICATION_ERROR, dongle.initialize()); + } + + @Test + public void initializeSuccess() { + System.out.println("--- " + Thread.currentThread().getStackTrace()[1].getMethodName()); + final EmberNcp ncp = Mockito.mock(EmberNcp.class); + final EzspVersionResponse version = Mockito.mock(EzspVersionResponse.class); + Mockito.when(version.getProtocolVersion()).thenReturn(4); + + Mockito.when(ncp.getVersion()).thenReturn(version); + Mockito.when(ncp.getNetworkParameters()).thenReturn(Mockito.mock(EzspGetNetworkParametersResponse.class)); + + final ZigBeePort port = Mockito.mock(ZigBeePort.class); + Mockito.when(port.open()).thenReturn(true); + + ZigBeeDongleEzsp dongle = new ZigBeeDongleEzsp(port) { + @Override + public EmberNcp getEmberNcp() { + return ncp; + } + }; + + final ZigBeeTransportReceive receiver = Mockito.mock(ZigBeeTransportReceive.class); + + dongle.setZigBeeTransportReceive(receiver); + assertEquals(ZigBeeStatus.SUCCESS, dongle.initialize()); + } } diff --git a/com.zsmartsystems.zigbee.dongle.ember/src/test/java/com/zsmartsystems/zigbee/dongle/ember/ZigBeeTransportTransmitTest.java b/com.zsmartsystems.zigbee.dongle.ember/src/test/java/com/zsmartsystems/zigbee/dongle/ember/ZigBeeTransportTransmitTest.java index 15e8fd9f5..76dd2f65b 100644 --- a/com.zsmartsystems.zigbee.dongle.ember/src/test/java/com/zsmartsystems/zigbee/dongle/ember/ZigBeeTransportTransmitTest.java +++ b/com.zsmartsystems.zigbee.dongle.ember/src/test/java/com/zsmartsystems/zigbee/dongle/ember/ZigBeeTransportTransmitTest.java @@ -15,6 +15,7 @@ import com.zsmartsystems.zigbee.TestUtilities; import com.zsmartsystems.zigbee.dongle.ember.ezsp.command.EzspGetNetworkParametersResponse; import com.zsmartsystems.zigbee.dongle.ember.ezsp.command.EzspGetParentChildParametersResponse; +import com.zsmartsystems.zigbee.dongle.ember.ezsp.command.EzspVersionResponse; import com.zsmartsystems.zigbee.dongle.ember.ezsp.structure.EmberCurrentSecurityState; import com.zsmartsystems.zigbee.dongle.ember.ezsp.structure.EmberNetworkParameters; import com.zsmartsystems.zigbee.dongle.ember.ezsp.structure.EmberNetworkStatus; @@ -24,6 +25,7 @@ import com.zsmartsystems.zigbee.dongle.ember.ezsp.structure.EzspStatus; import com.zsmartsystems.zigbee.dongle.ember.internal.EzspProtocolHandler; import com.zsmartsystems.zigbee.transport.ZigBeePort; +import com.zsmartsystems.zigbee.transport.ZigBeeTransportReceive; import com.zsmartsystems.zigbee.transport.ZigBeeTransportTransmitAbstractTest; /** @@ -52,6 +54,11 @@ public void initialiseTransport() throws Exception { Mockito.when(ncp.setRadioPower(ArgumentMatchers.anyInt())).thenReturn(EmberStatus.EMBER_SUCCESS); Mockito.when(ncp.getNwkAddress()).thenReturn(Integer.valueOf(0)); Mockito.when(ncp.getIeeeAddress()).thenReturn(new IeeeAddress("1234567890ABCDEF")); + final EzspVersionResponse version = Mockito.mock(EzspVersionResponse.class); + Mockito.when(version.getProtocolVersion()).thenReturn(4); + + Mockito.when(ncp.getVersion()).thenReturn(version); + Mockito.when(ncp.getNetworkParameters()).thenReturn(Mockito.mock(EzspGetNetworkParametersResponse.class)); ZigBeePort port = Mockito.mock(ZigBeePort.class); Mockito.when(port.open()).thenReturn(Boolean.TRUE); @@ -62,8 +69,13 @@ public EmberNcp getEmberNcp() { return ncp; } }; + EzspProtocolHandler frameHandler = Mockito.mock(EzspProtocolHandler.class); + Mockito.when(frameHandler.isAlive()).thenReturn(Boolean.TRUE); + TestUtilities.setField(ZigBeeDongleEzsp.class, dongle, "frameHandler", frameHandler); + + final ZigBeeTransportReceive receiver = Mockito.mock(ZigBeeTransportReceive.class); - TestUtilities.setField(ZigBeeDongleEzsp.class, dongle, "frameHandler", Mockito.mock(EzspProtocolHandler.class)); + dongle.setZigBeeTransportReceive(receiver); transport = dongle; }