Search in sources :

Example 1 with ZigBeeSerializer

use of com.zsmartsystems.zigbee.serialization.ZigBeeSerializer in project com.zsmartsystems.zigbee by zsmartsystems.

the class ZigBeeNetworkManager method sendCommand.

@Override
public int sendCommand(ZigBeeCommand command) {
    // Create the application frame
    ZigBeeApsFrame apsFrame = new ZigBeeApsFrame();
    int sequence = sequenceNumber.getAndIncrement() & 0xff;
    command.setTransactionId(sequence);
    // Set the source address - should probably be improved!
    // Note that the endpoint is set (currently!) in the transport layer
    // TODO: Use only a single endpoint for HA and fix this here
    command.setSourceAddress(new ZigBeeEndpointAddress(0));
    logger.debug("TX CMD: {}", command);
    apsFrame.setCluster(command.getClusterId());
    apsFrame.setApsCounter(apsCounter.getAndIncrement() & 0xff);
    // TODO: Set the source address correctly?
    apsFrame.setSourceAddress(0);
    apsFrame.setSequence(sequence);
    apsFrame.setRadius(31);
    if (command.getDestinationAddress() instanceof ZigBeeEndpointAddress) {
        apsFrame.setAddressMode(ZigBeeNwkAddressMode.DEVICE);
        apsFrame.setDestinationAddress(((ZigBeeEndpointAddress) command.getDestinationAddress()).getAddress());
        apsFrame.setDestinationEndpoint(((ZigBeeEndpointAddress) command.getDestinationAddress()).getEndpoint());
        ZigBeeNode node = getNode(command.getDestinationAddress().getAddress());
        if (node != null) {
            apsFrame.setDestinationIeeeAddress(node.getIeeeAddress());
        }
    } else {
        apsFrame.setAddressMode(ZigBeeNwkAddressMode.GROUP);
    // TODO: Handle multicast
    }
    final ZclFieldSerializer fieldSerializer;
    try {
        Constructor<? extends ZigBeeSerializer> constructor;
        constructor = serializerClass.getConstructor();
        ZigBeeSerializer serializer = constructor.newInstance();
        fieldSerializer = new ZclFieldSerializer(serializer);
    } catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
        logger.debug("Error serializing ZigBee frame {}", e);
        return 0;
    }
    if (command instanceof ZdoCommand) {
        // Source endpoint is (currently) set by the dongle since it registers the clusters into an endpoint
        // apsHeader.setSourceEndpoint(sourceEndpoint);
        apsFrame.setProfile(0);
        apsFrame.setSourceEndpoint(0);
        apsFrame.setDestinationEndpoint(0);
        command.serialize(fieldSerializer);
        // Serialise the ZCL header and add the payload
        apsFrame.setPayload(fieldSerializer.getPayload());
    }
    if (command instanceof ZclCommand) {
        // For ZCL commands we pass the NWK and APS headers as classes to the transport layer.
        // The ZCL packet is serialised here.
        ZclCommand zclCommand = (ZclCommand) command;
        apsFrame.setSourceEndpoint(1);
        // TODO set the profile properly
        apsFrame.setProfile(0x104);
        // Create the cluster library header
        ZclHeader zclHeader = new ZclHeader();
        zclHeader.setFrameType(zclCommand.isGenericCommand() ? ZclFrameType.ENTIRE_PROFILE_COMMAND : ZclFrameType.CLUSTER_SPECIFIC_COMMAND);
        zclHeader.setCommandId(zclCommand.getCommandId());
        zclHeader.setSequenceNumber(sequence);
        zclHeader.setDirection(zclCommand.getCommandDirection());
        command.serialize(fieldSerializer);
        // Serialise the ZCL header and add the payload
        apsFrame.setPayload(zclHeader.serialize(fieldSerializer, fieldSerializer.getPayload()));
        logger.debug("TX ZCL: {}", zclHeader);
    }
    logger.debug("TX APS: {}", apsFrame);
    transport.sendCommand(apsFrame);
    return sequence;
}
Also used : ZclFieldSerializer(com.zsmartsystems.zigbee.zcl.ZclFieldSerializer) ZdoCommand(com.zsmartsystems.zigbee.zdo.ZdoCommand) InvocationTargetException(java.lang.reflect.InvocationTargetException) ZclCommand(com.zsmartsystems.zigbee.zcl.ZclCommand) ZclHeader(com.zsmartsystems.zigbee.zcl.ZclHeader) ZigBeeSerializer(com.zsmartsystems.zigbee.serialization.ZigBeeSerializer)

Example 2 with ZigBeeSerializer

use of com.zsmartsystems.zigbee.serialization.ZigBeeSerializer in project com.zsmartsystems.zigbee by zsmartsystems.

the class MatchDescriptorResponseTest method testSendEndpoint2.

@Test
public void testSendEndpoint2() {
    MatchDescriptorResponse matchResponse = new MatchDescriptorResponse();
    matchResponse.setStatus(ZdoStatus.SUCCESS);
    List<Integer> matchList = new ArrayList<Integer>();
    matchList.add(1);
    matchList.add(2);
    matchResponse.setMatchList(matchList);
    matchResponse.setDestinationAddress(new ZigBeeEndpointAddress(1234, 5));
    matchResponse.setNwkAddrOfInterest(1234);
    System.out.println(matchResponse);
    ZigBeeSerializer serializer = new DefaultSerializer();
    ZclFieldSerializer fieldSerializer = new ZclFieldSerializer(serializer);
    matchResponse.serialize(fieldSerializer);
    assertTrue(Arrays.equals(getPacketData("00 00 D2 04 02 01 02"), serializer.getPayload()));
}
Also used : DefaultSerializer(com.zsmartsystems.zigbee.serialization.DefaultSerializer) ZigBeeSerializer(com.zsmartsystems.zigbee.serialization.ZigBeeSerializer) ZigBeeEndpointAddress(com.zsmartsystems.zigbee.ZigBeeEndpointAddress) ArrayList(java.util.ArrayList) ZclFieldSerializer(com.zsmartsystems.zigbee.zcl.ZclFieldSerializer) CommandTest(com.zsmartsystems.zigbee.CommandTest) Test(org.junit.Test)

Example 3 with ZigBeeSerializer

use of com.zsmartsystems.zigbee.serialization.ZigBeeSerializer in project com.zsmartsystems.zigbee by zsmartsystems.

the class MatchDescriptorResponseTest method testSendEndpoint1.

@Test
public void testSendEndpoint1() {
    MatchDescriptorResponse matchResponse = new MatchDescriptorResponse();
    matchResponse.setStatus(ZdoStatus.SUCCESS);
    List<Integer> matchList = new ArrayList<Integer>();
    matchList.add(1);
    matchResponse.setMatchList(matchList);
    matchResponse.setDestinationAddress(new ZigBeeEndpointAddress(1234, 5));
    matchResponse.setNwkAddrOfInterest(1234);
    System.out.println(matchResponse);
    ZigBeeSerializer serializer = new DefaultSerializer();
    ZclFieldSerializer fieldSerializer = new ZclFieldSerializer(serializer);
    matchResponse.serialize(fieldSerializer);
    assertTrue(Arrays.equals(getPacketData("00 00 D2 04 01 01"), serializer.getPayload()));
}
Also used : DefaultSerializer(com.zsmartsystems.zigbee.serialization.DefaultSerializer) ZigBeeSerializer(com.zsmartsystems.zigbee.serialization.ZigBeeSerializer) ZigBeeEndpointAddress(com.zsmartsystems.zigbee.ZigBeeEndpointAddress) ArrayList(java.util.ArrayList) ZclFieldSerializer(com.zsmartsystems.zigbee.zcl.ZclFieldSerializer) CommandTest(com.zsmartsystems.zigbee.CommandTest) Test(org.junit.Test)

Example 4 with ZigBeeSerializer

use of com.zsmartsystems.zigbee.serialization.ZigBeeSerializer in project com.zsmartsystems.zigbee by zsmartsystems.

the class ImageNotifyCommandTest method testSend.

@Test
public void testSend() {
    ImageNotifyCommand command = new ImageNotifyCommand();
    command.setSourceAddress(new ZigBeeEndpointAddress(0, 1));
    command.setDestinationAddress(new ZigBeeEndpointAddress(57337, 3));
    command.setImageType(6);
    command.setQueryJitter(72);
    command.setManufacturerCode(4364);
    command.setNewFileVersion(16909063);
    System.out.println(command);
    ZigBeeSerializer serializer = new DefaultSerializer();
    ZclFieldSerializer fieldSerializer = new ZclFieldSerializer(serializer);
    command.setPayloadType(0);
    command.serialize(fieldSerializer);
    assertTrue(Arrays.equals(getPacketData("00 48"), serializer.getPayload()));
    serializer = new DefaultSerializer();
    fieldSerializer = new ZclFieldSerializer(serializer);
    command.setPayloadType(1);
    command.serialize(fieldSerializer);
    assertTrue(Arrays.equals(getPacketData("01 48 0C 11"), serializer.getPayload()));
    serializer = new DefaultSerializer();
    fieldSerializer = new ZclFieldSerializer(serializer);
    command.setPayloadType(1);
    command.serialize(fieldSerializer);
    assertTrue(Arrays.equals(getPacketData("01 48 0C 11"), serializer.getPayload()));
    serializer = new DefaultSerializer();
    fieldSerializer = new ZclFieldSerializer(serializer);
    command.setPayloadType(2);
    command.serialize(fieldSerializer);
    assertTrue(Arrays.equals(getPacketData("02 48 0C 11 06 00"), serializer.getPayload()));
    serializer = new DefaultSerializer();
    fieldSerializer = new ZclFieldSerializer(serializer);
    command.setPayloadType(3);
    command.serialize(fieldSerializer);
    assertTrue(Arrays.equals(getPacketData("03 48 0C 11 06 00 07 03 02 01"), serializer.getPayload()));
}
Also used : DefaultSerializer(com.zsmartsystems.zigbee.serialization.DefaultSerializer) ZigBeeSerializer(com.zsmartsystems.zigbee.serialization.ZigBeeSerializer) ZigBeeEndpointAddress(com.zsmartsystems.zigbee.ZigBeeEndpointAddress) ZclFieldSerializer(com.zsmartsystems.zigbee.zcl.ZclFieldSerializer) CommandTest(com.zsmartsystems.zigbee.CommandTest) Test(org.junit.Test)

Aggregations

ZigBeeSerializer (com.zsmartsystems.zigbee.serialization.ZigBeeSerializer)4 ZclFieldSerializer (com.zsmartsystems.zigbee.zcl.ZclFieldSerializer)4 CommandTest (com.zsmartsystems.zigbee.CommandTest)3 ZigBeeEndpointAddress (com.zsmartsystems.zigbee.ZigBeeEndpointAddress)3 DefaultSerializer (com.zsmartsystems.zigbee.serialization.DefaultSerializer)3 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 ZclCommand (com.zsmartsystems.zigbee.zcl.ZclCommand)1 ZclHeader (com.zsmartsystems.zigbee.zcl.ZclHeader)1 ZdoCommand (com.zsmartsystems.zigbee.zdo.ZdoCommand)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1