Skip to content

Commit

Permalink
Set the default link key
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Jackson <chris@cd-jackson.com>
  • Loading branch information
cdjackson committed Sep 10, 2018
1 parent 0ab00ff commit 91391fd
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ public class ZigBeeBindingConstants {
public final static String CONFIGURATION_BAUD = "zigbee_baud";
public final static String CONFIGURATION_FLOWCONTROL = "zigbee_flowcontrol";
public static final String CONFIGURATION_NETWORKKEY = "zigbee_networkkey";
public static final String CONFIGURATION_LINKKEY = "zigbee_linkkey";
public static final String CONFIGURATION_PASSWORD = "zigbee_password";
public static final String CONFIGURATION_INITIALIZE = "zigbee_initialise";
public static final String CONFIGURATION_TRUSTCENTREMODE = "zigbee_trustcentremode";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public abstract class ZigBeeCoordinatorHandler extends BaseBridgeHandler

private ZigBeeNetworkStateSerializerImpl networkStateSerializer;

protected ZigBeeKey linkKey;
protected ZigBeeKey networkKey;

private TransportConfig transportConfig;
Expand All @@ -116,6 +117,12 @@ public abstract class ZigBeeCoordinatorHandler extends BaseBridgeHandler

private volatile boolean bridgeRemoved = false;

/**
* Default ZigBeeAlliance09 link key
*/
private final static ZigBeeKey KEY_ZIGBEE_ALLIANCE_O9 = new ZigBeeKey(new int[] { 0x5A, 0x69, 0x67, 0x42, 0x65,
0x65, 0x41, 0x6C, 0x6C, 0x69, 0x61, 0x6E, 0x63, 0x65, 0x30, 0x39 });

public ZigBeeCoordinatorHandler(Bridge coordinator) {
super(coordinator);
}
Expand All @@ -126,6 +133,7 @@ public void initialize() {
panId = 0xffff;
channelId = 0;
initializeNetwork = false;
String linkKeyString = "";
String networkKeyString = "";

try {
Expand All @@ -145,11 +153,16 @@ public void initialize() {
}

Object param = getConfig().get(CONFIGURATION_NETWORKKEY);
logger.debug("Key {}", getConfig().get(CONFIGURATION_NETWORKKEY));
logger.debug("Network Key {}", getConfig().get(CONFIGURATION_NETWORKKEY));
if (param != null && param instanceof String) {
networkKeyString = (String) param;
}

param = getConfig().get(CONFIGURATION_LINKKEY);
logger.debug("Link Key {}", getConfig().get(CONFIGURATION_LINKKEY));
if (param != null && param instanceof String) {
linkKeyString = (String) param;
}
} catch (ClassCastException | NumberFormatException e) {
logger.error("{}: ZigBee initialisation exception ", thing.getUID(), e);
updateStatus(ThingStatus.OFFLINE);
Expand Down Expand Up @@ -225,10 +238,9 @@ public void initialize() {
}
}

logger.debug("Key String {}", networkKeyString);

// Process the network key
try {
logger.debug("Network Key String {}", networkKeyString);
networkKey = new ZigBeeKey(networkKeyString);
} catch (IllegalArgumentException e) {
networkKey = new ZigBeeKey();
Expand All @@ -237,10 +249,20 @@ public void initialize() {
// If no key exists, generate a random key and save it back to the configuration
if (!networkKey.isValid()) {
networkKey = ZigBeeKey.createRandom();
logger.debug("Key initialised {}", networkKey);
logger.debug("Network key initialised {}", networkKey);
}

logger.debug("Key final array {}", networkKey);
logger.debug("Network key final array {}", networkKey);

// Process the link key
try {
logger.debug("Link Key String {}", linkKeyString);
linkKey = new ZigBeeKey(linkKeyString);
} catch (IllegalArgumentException e) {
linkKey = KEY_ZIGBEE_ALLIANCE_O9;
}

logger.debug("Link key final array {}", linkKey);
}

@Override
Expand Down Expand Up @@ -344,8 +366,6 @@ private void initialiseZigBee() {
networkManager.addExtension(new ZigBeeIasCieExtension());
networkManager.addExtension(new ZigBeeOtaUpgradeExtension());

logger.debug("Key initialise {}", networkKey);

// Add any listeners that were registered before the manager was registered
synchronized (listeners) {
for (ZigBeeNetworkNodeListener listener : listeners) {
Expand Down Expand Up @@ -375,6 +395,9 @@ private void initialiseZigBee() {
ExtendedPanId currentExtendedPanId = networkManager.getZigBeeExtendedPanId();

if (initializeNetwork) {
logger.debug("Link key initialise {}", linkKey);
logger.debug("Network key initialise {}", networkKey);
networkManager.setZigBeeLinkKey(linkKey);
networkManager.setZigBeeNetworkKey(networkKey);
networkManager.setZigBeeChannel(ZigBeeChannel.create(channelId));
networkManager.setZigBeePanId(panId);
Expand Down

0 comments on commit 91391fd

Please sign in to comment.