use of com.zsmartsystems.zigbee.dongle.xbee.internal.protocol.XBeeSetSaveDataCommand in project com.zsmartsystems.zigbee by zsmartsystems.
the class ZigBeeDongleXBee method initialize.
@Override
public ZigBeeInitializeResponse initialize() {
logger.debug("XBee dongle initialize.");
zigbeeTransportReceive.setNetworkState(ZigBeeTransportState.UNINITIALISED);
if (!serialPort.open()) {
logger.error("Unable to open XBee serial port");
return ZigBeeInitializeResponse.FAILED;
}
// Create and start the frame handler
frameHandler = new XBeeFrameHandler();
frameHandler.start(serialPort);
frameHandler.addEventListener(this);
// Reset to a known state
// Device sends WATCHDOG_TIMER_RESET event
// A retry mechanism is used as sometimes the reset response is not received.
// This appears to happen if there are other events queued in the stick.
int resetCount = 0;
do {
if (resetCount >= MAX_RESET_RETRIES) {
logger.info("XBee dongle reset failed after {} tries.", ++resetCount);
return ZigBeeInitializeResponse.FAILED;
}
logger.debug("XBee dongle reset {}.", ++resetCount);
XBeeSetSoftwareResetCommand resetCommand = new XBeeSetSoftwareResetCommand();
frameHandler.sendRequest(resetCommand);
} while (frameHandler.eventWait(XBeeModemStatusEvent.class) == null);
// Enable the API with escaping
XBeeSetApiEnableCommand apiEnableCommand = new XBeeSetApiEnableCommand();
apiEnableCommand.setMode(2);
frameHandler.sendRequest(apiEnableCommand);
// Set the API mode so we receive detailed data including ZDO
XBeeSetApiModeCommand apiModeCommand = new XBeeSetApiModeCommand();
apiModeCommand.setMode(3);
frameHandler.sendRequest(apiModeCommand);
// Get the product information
XBeeGetHardwareVersionCommand hwVersionCommand = new XBeeGetHardwareVersionCommand();
frameHandler.sendRequest(hwVersionCommand);
XBeeGetFirmwareVersionCommand fwVersionCommand = new XBeeGetFirmwareVersionCommand();
frameHandler.sendRequest(fwVersionCommand);
XBeeGetDetailedVersionCommand versionCommand = new XBeeGetDetailedVersionCommand();
frameHandler.sendRequest(versionCommand);
// Get Ieee Address
XBeeGetIeeeAddressHighCommand ieeeHighCommand = new XBeeGetIeeeAddressHighCommand();
XBeeIeeeAddressHighResponse ieeeHighResponse = (XBeeIeeeAddressHighResponse) frameHandler.sendRequest(ieeeHighCommand);
XBeeGetIeeeAddressLowCommand ieeeLowCommand = new XBeeGetIeeeAddressLowCommand();
XBeeIeeeAddressLowResponse ieeeLowResponse = (XBeeIeeeAddressLowResponse) frameHandler.sendRequest(ieeeLowCommand);
if (ieeeHighResponse == null || ieeeLowCommand == null) {
logger.error("Unable to get XBee IEEE address");
return ZigBeeInitializeResponse.FAILED;
}
int[] tmpAddress = new int[8];
tmpAddress[0] = ieeeLowResponse.getIeeeAddress()[3];
tmpAddress[1] = ieeeLowResponse.getIeeeAddress()[2];
tmpAddress[2] = ieeeLowResponse.getIeeeAddress()[1];
tmpAddress[3] = ieeeLowResponse.getIeeeAddress()[0];
tmpAddress[4] = ieeeHighResponse.getIeeeAddress()[3];
tmpAddress[5] = ieeeHighResponse.getIeeeAddress()[2];
tmpAddress[6] = ieeeHighResponse.getIeeeAddress()[1];
tmpAddress[7] = ieeeHighResponse.getIeeeAddress()[0];
ieeeAddress = new IeeeAddress(tmpAddress);
logger.debug("XBee IeeeAddress={}", ieeeAddress);
// Set the ZigBee stack profile
XBeeSetZigbeeStackProfileCommand stackProfile = new XBeeSetZigbeeStackProfileCommand();
stackProfile.setStackProfile(2);
frameHandler.sendRequest(stackProfile);
// Enable Security
XBeeSetEncryptionEnableCommand enableEncryption = new XBeeSetEncryptionEnableCommand();
enableEncryption.setEnableEncryption(true);
frameHandler.sendRequest(enableEncryption);
XBeeSetEncryptionOptionsCommand encryptionOptions = new XBeeSetEncryptionOptionsCommand();
encryptionOptions.addEncryptionOptions(EncryptionOptions.ENABLE_TRUST_CENTRE);
frameHandler.sendRequest(encryptionOptions);
// Enable coordinator mode
XBeeSetCoordinatorEnableCommand coordinatorEnable = new XBeeSetCoordinatorEnableCommand();
coordinatorEnable.setEnable(true);
frameHandler.sendRequest(coordinatorEnable);
// Set the network key
XBeeSetNetworkKeyCommand networkKey = new XBeeSetNetworkKeyCommand();
networkKey.setNetworkKey(new ZigBeeKey());
frameHandler.sendRequest(networkKey);
// Set the link key
XBeeSetLinkKeyCommand setLinkKey = new XBeeSetLinkKeyCommand();
setLinkKey.setLinkKey(linkKey);
frameHandler.sendRequest(setLinkKey);
// Save the configuration in the XBee
XBeeSetSaveDataCommand saveData = new XBeeSetSaveDataCommand();
frameHandler.sendRequest(saveData);
// Get network information
XBeeGetPanIdCommand getPanId = new XBeeGetPanIdCommand();
XBeePanIdResponse panIdResponse = (XBeePanIdResponse) frameHandler.sendRequest(getPanId);
panId = panIdResponse.getPanId();
XBeeGetExtendedPanIdCommand getEPanId = new XBeeGetExtendedPanIdCommand();
XBeeExtendedPanIdResponse epanIdResponse = (XBeeExtendedPanIdResponse) frameHandler.sendRequest(getEPanId);
extendedPanId = epanIdResponse.getExtendedPanId();
zigbeeTransportReceive.setNetworkState(ZigBeeTransportState.INITIALISING);
return ZigBeeInitializeResponse.JOINED;
}
Aggregations