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 b21c8cb4f..8036839a4 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 @@ -575,7 +575,6 @@ public ZigBeeStatus startup(boolean reinitialize) { ncp.sendManyToOneRouteRequest(concentratorType, radius); } - ncp.getConfiguration(EzspConfigId.EZSP_CONFIG_PACKET_BUFFER_COUNT); return joinedNetwork ? ZigBeeStatus.SUCCESS : ZigBeeStatus.BAD_RESPONSE; } @@ -763,7 +762,27 @@ public ZigBeeStatus setNetworkState(ZigBeeNetworkState networkState) { // token area once - subsequent writes will fail, and therefore changing IEEE address (eg from a // backup/restore) may fail. ncp.tokenFactoryReset(false, false); - return ncp.leaveNetwork() == EmberStatus.EMBER_SUCCESS ? ZigBeeStatus.SUCCESS : ZigBeeStatus.FAILURE; + if (ncp.leaveNetwork() != EmberStatus.EMBER_SUCCESS) { + return ZigBeeStatus.FAILURE; + } + + // Wait for the notification from the NCP that it is offline. + // This comes through the EzspStackStatusHandler which is processed elsewhere + // and sets networkStateUp to false + long timer = System.currentTimeMillis() + WAIT_FOR_ONLINE; + do { + if (!networkStateUp) { + return ZigBeeStatus.SUCCESS; + } + + try { + Thread.sleep(250); + } catch (InterruptedException e) { + break; + } + } while (timer > System.currentTimeMillis()); + + return ZigBeeStatus.INVALID_STATE; case ONLINE: return ncp.networkInit() == EmberStatus.EMBER_SUCCESS ? ZigBeeStatus.SUCCESS : ZigBeeStatus.FAILURE; default: diff --git a/com.zsmartsystems.zigbee/src/main/java/com/zsmartsystems/zigbee/ZigBeeNetworkManager.java b/com.zsmartsystems.zigbee/src/main/java/com/zsmartsystems/zigbee/ZigBeeNetworkManager.java index 050ed03b0..f2ef63944 100644 --- a/com.zsmartsystems.zigbee/src/main/java/com/zsmartsystems/zigbee/ZigBeeNetworkManager.java +++ b/com.zsmartsystems.zigbee/src/main/java/com/zsmartsystems/zigbee/ZigBeeNetworkManager.java @@ -2092,7 +2092,11 @@ public ZigBeeStatus restoreBackup(UUID uuid) { logger.debug("RestoreBackup: Backup read from {}", uuid); // Take the network offline for reconfiguration - transport.setNetworkState(ZigBeeNetworkState.UNINITIALISED); + ZigBeeStatus offlineResponse = transport.setNetworkState(ZigBeeNetworkState.UNINITIALISED); + if (offlineResponse != ZigBeeStatus.SUCCESS) { + logger.error("RestoreBackup: Failed to set network to UNINITIALISED with response {}", offlineResponse); + return ZigBeeStatus.INVALID_STATE; + } // To properly re-add nodes, we must be INITIALIZING // To call startup, we must be INITIALIZING