Search in sources :

Example 21 with ZToolPacket

use of com.zsmartsystems.zigbee.dongle.cc2531.network.packet.ZToolPacket in project com.zsmartsystems.zigbee by zsmartsystems.

the class ZDO_MSG_CB_INCOMING method translate.

/**
 * Translates the ZigBee ZDO cluster packet into a ZTool RSP packet
 */
public ZToolPacket translate() {
    ZToolPacket newPacket;
    int[] frame;
    logger.trace("Translating ZDO cluster callback {}", ClusterId);
    Class<? extends ZToolPacket> newPacketClass = clusterToRSP.get(ClusterId.get16BitValue());
    if (newPacketClass == null) {
        logger.error("Unhandled ZDO cluster callback {}", ClusterId);
        return this;
    } else if (newPacketClass == ZDO_NWK_ADDR_RSP.class || newPacketClass == ZDO_IEEE_ADDR_RSP.class) {
        // The address responses don't need SrcAddr. NumAssocDev and StartIndex positions are reversed.
        // The new response frame is at least 13 bytes long.
        frame = new int[Math.max(Data.length, 13)];
        System.arraycopy(Data, 0, frame, 0, Data.length);
        // If RequestType == 1 there are two extra bytes in the frame
        if (Data.length > 12) {
            // NumAssocDev
            frame[11] = Data[12];
            // StartIndex
            frame[12] = Data[11];
        } else {
            frame[11] = 0;
            frame[12] = 0;
        }
    } else {
        // Default frame translation, this works for most callbacks.
        // Get 2 extra bytes at the beginning to put source address into.
        frame = new int[Data.length + 2];
        System.arraycopy(Data, 0, frame, 2, Data.length);
        frame[0] = SrcAddr.getLsb();
        frame[1] = SrcAddr.getMsb();
    }
    try {
        newPacket = newPacketClass.getConstructor(int[].class).newInstance(frame);
    } catch (Exception e) {
        logger.error("Error constructing response packet {}", e);
        return this;
    }
    // Set checksum with original packet checksum
    newPacket.setFCS(this.FCS);
    return newPacket;
}
Also used : ZToolPacket(com.zsmartsystems.zigbee.dongle.cc2531.network.packet.ZToolPacket)

Aggregations

ZToolPacket (com.zsmartsystems.zigbee.dongle.cc2531.network.packet.ZToolPacket)16 Test (org.junit.Test)13 ZigBeeApsFrame (com.zsmartsystems.zigbee.ZigBeeApsFrame)12 DoubleByte (com.zsmartsystems.zigbee.dongle.cc2531.zigbee.util.DoubleByte)4 AsynchronousCommandListener (com.zsmartsystems.zigbee.dongle.cc2531.network.AsynchronousCommandListener)2 IOException (java.io.IOException)2 SynchronousCommandListener (com.zsmartsystems.zigbee.dongle.cc2531.network.SynchronousCommandListener)1 BlockingCommandReceiver (com.zsmartsystems.zigbee.dongle.cc2531.network.impl.BlockingCommandReceiver)1 ZToolPacketStream (com.zsmartsystems.zigbee.dongle.cc2531.network.packet.ZToolPacketStream)1 SYS_RESET_RESPONSE (com.zsmartsystems.zigbee.dongle.cc2531.network.packet.system.SYS_RESET_RESPONSE)1 SYS_VERSION_RESPONSE (com.zsmartsystems.zigbee.dongle.cc2531.network.packet.system.SYS_VERSION_RESPONSE)1 ZigBeePort (com.zsmartsystems.zigbee.transport.ZigBeePort)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1