Skip to content

Commit

Permalink
Handle handler error during initialisation (#1440)
Browse files Browse the repository at this point in the history
* Handle handler error during initialisation

Signed-off-by: Chris Jackson <[email protected]>

* Fix tests

Signed-off-by: Chris Jackson <[email protected]>

---------

Signed-off-by: Chris Jackson <[email protected]>
  • Loading branch information
cdjackson authored Oct 5, 2024
1 parent c038c6f commit 531ee12
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -457,22 +457,40 @@ public ZigBeeStatus initialize() {
stackConfigurer.setPolicy(stackPolicies);
policies = stackConfigurer.getPolicy(stackPolicies.keySet());
for (Entry<EzspPolicyId, Integer> 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()));
}

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();
logger.debug("Ember local IEEE Address is {}", ieeeAddress);

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");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

/**
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
Expand Down

0 comments on commit 531ee12

Please sign in to comment.