Search in sources :

Example 1 with XBeeFrameHandler

use of com.zsmartsystems.zigbee.dongle.xbee.internal.XBeeFrameHandler in project com.zsmartsystems.zigbee by zsmartsystems.

the class ZigBeeDongleXBeeTest method sendCommand.

@Test
public void sendCommand() {
    XBeeFrameHandler frameHandler = Mockito.mock(XBeeFrameHandler.class);
    ArgumentCaptor<XBeeCommand> commandCapture = ArgumentCaptor.forClass(XBeeCommand.class);
    Mockito.when(frameHandler.sendRequestAsync(commandCapture.capture())).thenReturn(null);
    ZigBeeDongleXBee dongle = new ZigBeeDongleXBee(null);
    Field field;
    try {
        field = dongle.getClass().getDeclaredField("frameHandler");
        field.setAccessible(true);
        field.set(dongle, frameHandler);
    } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
        e.printStackTrace();
    }
    MatchDescriptorResponse command = new MatchDescriptorResponse();
    command.setDestinationAddress(new ZigBeeEndpointAddress(46946));
    command.setNwkAddrOfInterest(46946);
    command.setStatus(ZdoStatus.SUCCESS);
    command.setTransactionId(0x2A);
    List<Integer> matchList = new ArrayList<>();
    matchList.add(1);
    command.setMatchList(matchList);
    ZigBeeApsFrame apsFrame = new ZigBeeApsFrame();
    apsFrame.setDestinationAddress(46946);
    apsFrame.setDestinationEndpoint(0);
    apsFrame.setDestinationIeeeAddress(new IeeeAddress("000D6F00057CF7C6"));
    apsFrame.setCluster(32774);
    apsFrame.setAddressMode(ZigBeeNwkAddressMode.DEVICE);
    apsFrame.setRadius(31);
    apsFrame.setSequence(42);
    apsFrame.setPayload(new int[] { 0x00, 0x00, 0x2E, 0x5B, 0x01, 0x01 });
    System.out.println(command);
    System.out.println(apsFrame);
    dongle.sendCommand(apsFrame);
    assertEquals(1, commandCapture.getAllValues().size());
    XBeeTransmitRequestExplicitCommand sentCommand = (XBeeTransmitRequestExplicitCommand) commandCapture.getValue();
    sentCommand.setFrameId(32);
    System.out.println(sentCommand);
    int[] payload = new int[] { 0, 26, 17, 32, 0, 13, 111, 0, 5, 124, 247, 198, 183, 98, 0, 0, 128, 6, 0, 0, 0, 32, 0, 0, 46, 91, 1, 1, 202 };
    int[] output = sentCommand.serialize();
    assertTrue(Arrays.equals(payload, output));
}
Also used : XBeeCommand(com.zsmartsystems.zigbee.dongle.xbee.internal.protocol.XBeeCommand) ZigBeeApsFrame(com.zsmartsystems.zigbee.ZigBeeApsFrame) XBeeFrameHandler(com.zsmartsystems.zigbee.dongle.xbee.internal.XBeeFrameHandler) MatchDescriptorResponse(com.zsmartsystems.zigbee.zdo.command.MatchDescriptorResponse) ArrayList(java.util.ArrayList) IeeeAddress(com.zsmartsystems.zigbee.IeeeAddress) Field(java.lang.reflect.Field) ZigBeeEndpointAddress(com.zsmartsystems.zigbee.ZigBeeEndpointAddress) XBeeTransmitRequestExplicitCommand(com.zsmartsystems.zigbee.dongle.xbee.internal.protocol.XBeeTransmitRequestExplicitCommand) Test(org.junit.Test)

Example 2 with XBeeFrameHandler

use of com.zsmartsystems.zigbee.dongle.xbee.internal.XBeeFrameHandler 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;
}
Also used : XBeeSetCoordinatorEnableCommand(com.zsmartsystems.zigbee.dongle.xbee.internal.protocol.XBeeSetCoordinatorEnableCommand) XBeeExtendedPanIdResponse(com.zsmartsystems.zigbee.dongle.xbee.internal.protocol.XBeeExtendedPanIdResponse) XBeeGetHardwareVersionCommand(com.zsmartsystems.zigbee.dongle.xbee.internal.protocol.XBeeGetHardwareVersionCommand) XBeeSetSoftwareResetCommand(com.zsmartsystems.zigbee.dongle.xbee.internal.protocol.XBeeSetSoftwareResetCommand) XBeeModemStatusEvent(com.zsmartsystems.zigbee.dongle.xbee.internal.protocol.XBeeModemStatusEvent) XBeeGetPanIdCommand(com.zsmartsystems.zigbee.dongle.xbee.internal.protocol.XBeeGetPanIdCommand) XBeeSetApiEnableCommand(com.zsmartsystems.zigbee.dongle.xbee.internal.protocol.XBeeSetApiEnableCommand) XBeePanIdResponse(com.zsmartsystems.zigbee.dongle.xbee.internal.protocol.XBeePanIdResponse) XBeeGetExtendedPanIdCommand(com.zsmartsystems.zigbee.dongle.xbee.internal.protocol.XBeeGetExtendedPanIdCommand) XBeeGetFirmwareVersionCommand(com.zsmartsystems.zigbee.dongle.xbee.internal.protocol.XBeeGetFirmwareVersionCommand) XBeeFrameHandler(com.zsmartsystems.zigbee.dongle.xbee.internal.XBeeFrameHandler) XBeeSetEncryptionOptionsCommand(com.zsmartsystems.zigbee.dongle.xbee.internal.protocol.XBeeSetEncryptionOptionsCommand) XBeeGetDetailedVersionCommand(com.zsmartsystems.zigbee.dongle.xbee.internal.protocol.XBeeGetDetailedVersionCommand) XBeeIeeeAddressLowResponse(com.zsmartsystems.zigbee.dongle.xbee.internal.protocol.XBeeIeeeAddressLowResponse) XBeeIeeeAddressHighResponse(com.zsmartsystems.zigbee.dongle.xbee.internal.protocol.XBeeIeeeAddressHighResponse) XBeeSetLinkKeyCommand(com.zsmartsystems.zigbee.dongle.xbee.internal.protocol.XBeeSetLinkKeyCommand) XBeeSetZigbeeStackProfileCommand(com.zsmartsystems.zigbee.dongle.xbee.internal.protocol.XBeeSetZigbeeStackProfileCommand) XBeeSetEncryptionEnableCommand(com.zsmartsystems.zigbee.dongle.xbee.internal.protocol.XBeeSetEncryptionEnableCommand) IeeeAddress(com.zsmartsystems.zigbee.IeeeAddress) XBeeGetIeeeAddressHighCommand(com.zsmartsystems.zigbee.dongle.xbee.internal.protocol.XBeeGetIeeeAddressHighCommand) XBeeSetApiModeCommand(com.zsmartsystems.zigbee.dongle.xbee.internal.protocol.XBeeSetApiModeCommand) XBeeGetIeeeAddressLowCommand(com.zsmartsystems.zigbee.dongle.xbee.internal.protocol.XBeeGetIeeeAddressLowCommand) XBeeSetNetworkKeyCommand(com.zsmartsystems.zigbee.dongle.xbee.internal.protocol.XBeeSetNetworkKeyCommand) XBeeSetSaveDataCommand(com.zsmartsystems.zigbee.dongle.xbee.internal.protocol.XBeeSetSaveDataCommand) ZigBeeKey(com.zsmartsystems.zigbee.ZigBeeKey)

Aggregations

IeeeAddress (com.zsmartsystems.zigbee.IeeeAddress)2 XBeeFrameHandler (com.zsmartsystems.zigbee.dongle.xbee.internal.XBeeFrameHandler)2 ZigBeeApsFrame (com.zsmartsystems.zigbee.ZigBeeApsFrame)1 ZigBeeEndpointAddress (com.zsmartsystems.zigbee.ZigBeeEndpointAddress)1 ZigBeeKey (com.zsmartsystems.zigbee.ZigBeeKey)1 XBeeCommand (com.zsmartsystems.zigbee.dongle.xbee.internal.protocol.XBeeCommand)1 XBeeExtendedPanIdResponse (com.zsmartsystems.zigbee.dongle.xbee.internal.protocol.XBeeExtendedPanIdResponse)1 XBeeGetDetailedVersionCommand (com.zsmartsystems.zigbee.dongle.xbee.internal.protocol.XBeeGetDetailedVersionCommand)1 XBeeGetExtendedPanIdCommand (com.zsmartsystems.zigbee.dongle.xbee.internal.protocol.XBeeGetExtendedPanIdCommand)1 XBeeGetFirmwareVersionCommand (com.zsmartsystems.zigbee.dongle.xbee.internal.protocol.XBeeGetFirmwareVersionCommand)1 XBeeGetHardwareVersionCommand (com.zsmartsystems.zigbee.dongle.xbee.internal.protocol.XBeeGetHardwareVersionCommand)1 XBeeGetIeeeAddressHighCommand (com.zsmartsystems.zigbee.dongle.xbee.internal.protocol.XBeeGetIeeeAddressHighCommand)1 XBeeGetIeeeAddressLowCommand (com.zsmartsystems.zigbee.dongle.xbee.internal.protocol.XBeeGetIeeeAddressLowCommand)1 XBeeGetPanIdCommand (com.zsmartsystems.zigbee.dongle.xbee.internal.protocol.XBeeGetPanIdCommand)1 XBeeIeeeAddressHighResponse (com.zsmartsystems.zigbee.dongle.xbee.internal.protocol.XBeeIeeeAddressHighResponse)1 XBeeIeeeAddressLowResponse (com.zsmartsystems.zigbee.dongle.xbee.internal.protocol.XBeeIeeeAddressLowResponse)1 XBeeModemStatusEvent (com.zsmartsystems.zigbee.dongle.xbee.internal.protocol.XBeeModemStatusEvent)1 XBeePanIdResponse (com.zsmartsystems.zigbee.dongle.xbee.internal.protocol.XBeePanIdResponse)1 XBeeSetApiEnableCommand (com.zsmartsystems.zigbee.dongle.xbee.internal.protocol.XBeeSetApiEnableCommand)1 XBeeSetApiModeCommand (com.zsmartsystems.zigbee.dongle.xbee.internal.protocol.XBeeSetApiModeCommand)1