Search in sources :

Example 6 with ZclHeader

use of com.zsmartsystems.zigbee.zcl.ZclHeader 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 7 with ZclHeader

use of com.zsmartsystems.zigbee.zcl.ZclHeader in project com.zsmartsystems.zigbee by zsmartsystems.

the class ConfigureReportingResponseTest method testErrorInvalidDataType.

@Test
public void testErrorInvalidDataType() {
    int[] packet = getPacketData("08 6C 07 8D 00 00 00");
    ConfigureReportingResponse response = new ConfigureReportingResponse();
    DefaultDeserializer deserializer = new DefaultDeserializer(packet);
    ZclFieldDeserializer fieldDeserializer = new ZclFieldDeserializer(deserializer);
    ZclHeader zclHeader = new ZclHeader(fieldDeserializer);
    System.out.println(zclHeader);
    response.deserialize(fieldDeserializer);
    System.out.println(response);
    assertNull(response.getStatus());
    assertEquals(1, response.getRecords().size());
    AttributeStatusRecord record = response.getRecords().get(0);
    assertEquals(0, record.getAttributeIdentifier());
    assertEquals(ZclStatus.INVALID_DATA_TYPE, record.getStatus());
}
Also used : DefaultDeserializer(com.zsmartsystems.zigbee.serialization.DefaultDeserializer) ZclFieldDeserializer(com.zsmartsystems.zigbee.zcl.ZclFieldDeserializer) AttributeStatusRecord(com.zsmartsystems.zigbee.zcl.field.AttributeStatusRecord) ZclHeader(com.zsmartsystems.zigbee.zcl.ZclHeader) CommandTest(com.zsmartsystems.zigbee.CommandTest) Test(org.junit.Test)

Example 8 with ZclHeader

use of com.zsmartsystems.zigbee.zcl.ZclHeader in project com.zsmartsystems.zigbee by zsmartsystems.

the class ConfigureReportingResponseTest method test.

@Test
public void test() {
    int[] packet = getPacketData("18 1B 07 00 00 00 00");
    ConfigureReportingResponse response = new ConfigureReportingResponse();
    DefaultDeserializer deserializer = new DefaultDeserializer(packet);
    ZclFieldDeserializer fieldDeserializer = new ZclFieldDeserializer(deserializer);
    ZclHeader zclHeader = new ZclHeader(fieldDeserializer);
    System.out.println(zclHeader);
    response.deserialize(fieldDeserializer);
    System.out.println(response);
    assertNull(response.getStatus());
    assertEquals(1, response.getRecords().size());
    AttributeStatusRecord record = response.getRecords().get(0);
    assertEquals(0, record.getAttributeIdentifier());
    assertEquals(ZclStatus.SUCCESS, record.getStatus());
}
Also used : DefaultDeserializer(com.zsmartsystems.zigbee.serialization.DefaultDeserializer) ZclFieldDeserializer(com.zsmartsystems.zigbee.zcl.ZclFieldDeserializer) AttributeStatusRecord(com.zsmartsystems.zigbee.zcl.field.AttributeStatusRecord) ZclHeader(com.zsmartsystems.zigbee.zcl.ZclHeader) CommandTest(com.zsmartsystems.zigbee.CommandTest) Test(org.junit.Test)

Example 9 with ZclHeader

use of com.zsmartsystems.zigbee.zcl.ZclHeader in project com.zsmartsystems.zigbee by zsmartsystems.

the class ConfigureReportingResponseTest method testStatusOnly.

@Test
public void testStatusOnly() {
    int[] packet = getPacketData("18 11 07 00");
    ConfigureReportingResponse response = new ConfigureReportingResponse();
    DefaultDeserializer deserializer = new DefaultDeserializer(packet);
    ZclFieldDeserializer fieldDeserializer = new ZclFieldDeserializer(deserializer);
    ZclHeader zclHeader = new ZclHeader(fieldDeserializer);
    System.out.println(zclHeader);
    response.deserialize(fieldDeserializer);
    System.out.println(response);
    assertEquals(ZclStatus.SUCCESS, response.getStatus());
    assertNull(response.getRecords());
}
Also used : DefaultDeserializer(com.zsmartsystems.zigbee.serialization.DefaultDeserializer) ZclFieldDeserializer(com.zsmartsystems.zigbee.zcl.ZclFieldDeserializer) ZclHeader(com.zsmartsystems.zigbee.zcl.ZclHeader) CommandTest(com.zsmartsystems.zigbee.CommandTest) Test(org.junit.Test)

Aggregations

ZclHeader (com.zsmartsystems.zigbee.zcl.ZclHeader)9 Test (org.junit.Test)7 CommandTest (com.zsmartsystems.zigbee.CommandTest)6 DefaultDeserializer (com.zsmartsystems.zigbee.serialization.DefaultDeserializer)6 ZclFieldDeserializer (com.zsmartsystems.zigbee.zcl.ZclFieldDeserializer)6 ZclCommand (com.zsmartsystems.zigbee.zcl.ZclCommand)2 ZclFieldSerializer (com.zsmartsystems.zigbee.zcl.ZclFieldSerializer)2 AttributeStatusRecord (com.zsmartsystems.zigbee.zcl.field.AttributeStatusRecord)2 DefaultSerializer (com.zsmartsystems.zigbee.serialization.DefaultSerializer)1 ZigBeeSerializer (com.zsmartsystems.zigbee.serialization.ZigBeeSerializer)1 ReadAttributesCommand (com.zsmartsystems.zigbee.zcl.clusters.general.ReadAttributesCommand)1 ZclCommandType (com.zsmartsystems.zigbee.zcl.protocol.ZclCommandType)1 ZdoCommand (com.zsmartsystems.zigbee.zdo.ZdoCommand)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1