Search in sources :

Example 1 with ZclCommand

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

the class ZdoResponseMatcherTest method testMatch.

@Test
public void testMatch() {
    ZdoTransactionMatcher matcher = new ZdoTransactionMatcher();
    ZdoRequest zdoCommand = new BindRequest();
    BindResponse zdoResponse = new BindResponse();
    zdoCommand.setDestinationAddress(new ZigBeeEndpointAddress(1234));
    zdoResponse.setSourceAddress(new ZigBeeEndpointAddress(1234));
    assertTrue(matcher.isTransactionMatch(zdoCommand, zdoResponse));
    zdoResponse.setSourceAddress(new ZigBeeEndpointAddress(5678));
    assertFalse(matcher.isTransactionMatch(zdoCommand, zdoResponse));
    ZclCommand zclResponse = new OffCommand();
    assertFalse(matcher.isTransactionMatch(zdoCommand, zclResponse));
}
Also used : ZigBeeEndpointAddress(com.zsmartsystems.zigbee.ZigBeeEndpointAddress) BindRequest(com.zsmartsystems.zigbee.zdo.command.BindRequest) OffCommand(com.zsmartsystems.zigbee.zcl.clusters.onoff.OffCommand) BindResponse(com.zsmartsystems.zigbee.zdo.command.BindResponse) ZclCommand(com.zsmartsystems.zigbee.zcl.ZclCommand) Test(org.junit.Test)

Example 2 with ZclCommand

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

the class ZigBeeNetworkManager method receiveZclCommand.

private ZigBeeCommand receiveZclCommand(final ZclFieldDeserializer fieldDeserializer, final ZigBeeApsFrame apsFrame) {
    // Process the ZCL header
    ZclHeader zclHeader = new ZclHeader(fieldDeserializer);
    logger.debug("RX ZCL: {}", zclHeader);
    // Get the command type
    ZclCommandType commandType = null;
    if (zclHeader.getFrameType() == ZclFrameType.ENTIRE_PROFILE_COMMAND) {
        commandType = ZclCommandType.getGeneric(zclHeader.getCommandId());
    } else {
        commandType = ZclCommandType.getCommandType(apsFrame.getCluster(), zclHeader.getCommandId(), zclHeader.getDirection());
    }
    if (commandType == null) {
        logger.debug("No command type found for {}, cluster={}, command={}, direction={}", zclHeader.getFrameType(), apsFrame.getCluster(), zclHeader.getCommandId(), zclHeader.getDirection());
        return null;
    }
    ZclCommand command = commandType.instantiateCommand();
    if (command == null) {
        logger.debug("No command found for {}, cluster={}, command={}", zclHeader.getFrameType(), apsFrame.getCluster(), zclHeader.getCommandId());
        return null;
    }
    command.setCommandDirection(zclHeader.getDirection());
    command.deserialize(fieldDeserializer);
    command.setClusterId(apsFrame.getCluster());
    command.setTransactionId(zclHeader.getSequenceNumber());
    return command;
}
Also used : ZclCommandType(com.zsmartsystems.zigbee.zcl.protocol.ZclCommandType) ZclHeader(com.zsmartsystems.zigbee.zcl.ZclHeader) ZclCommand(com.zsmartsystems.zigbee.zcl.ZclCommand)

Example 3 with ZclCommand

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

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

the class ZigBeeNode method commandReceived.

@Override
public void commandReceived(ZigBeeCommand command) {
    // Check if it's our address
    if (command.getSourceAddress().getAddress() != networkAddress) {
        return;
    }
    // If we have local servers matching the request, then we need to respond
    if (command instanceof MatchDescriptorRequest) {
        MatchDescriptorRequest matchRequest = (MatchDescriptorRequest) command;
        if (matchRequest.getProfileId() != 0x104) {
            // TODO: Remove this constant ?
            return;
        }
        // We want to match any of our local servers (ie our input clusters) with any
        // requested clusters in the requests cluster list
        boolean matched = false;
        for (ZigBeeEndpoint endpoint : endpoints.values()) {
            for (int clusterId : matchRequest.getInClusterList()) {
                if (endpoint.getApplication(clusterId) != null) {
                    matched = true;
                    break;
                }
            }
            if (matched) {
                break;
            }
        }
        if (!matched) {
            return;
        }
        MatchDescriptorResponse matchResponse = new MatchDescriptorResponse();
        matchResponse.setStatus(ZdoStatus.SUCCESS);
        List<Integer> matchList = new ArrayList<Integer>();
        matchList.add(1);
        matchResponse.setMatchList(matchList);
        matchResponse.setDestinationAddress(command.getSourceAddress());
        networkManager.sendCommand(matchResponse);
    }
    if (!(command instanceof ZclCommand)) {
        return;
    }
    ZclCommand zclCommand = (ZclCommand) command;
    ZigBeeEndpointAddress endpointAddress = (ZigBeeEndpointAddress) zclCommand.getSourceAddress();
    ZigBeeEndpoint endpoint = endpoints.get(endpointAddress.getEndpoint());
    if (endpoint == null) {
        return;
    }
    endpoint.commandReceived(zclCommand);
}
Also used : MatchDescriptorResponse(com.zsmartsystems.zigbee.zdo.command.MatchDescriptorResponse) ArrayList(java.util.ArrayList) MatchDescriptorRequest(com.zsmartsystems.zigbee.zdo.command.MatchDescriptorRequest) ZclCommand(com.zsmartsystems.zigbee.zcl.ZclCommand)

Example 5 with ZclCommand

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

the class ZclCommandTypeTest method instantiateCommand.

@Test
public void instantiateCommand() {
    ZclCommandType cmd = ZclCommandType.ADD_GROUP_COMMAND;
    ZclCommand cmdClass = cmd.instantiateCommand();
    assertTrue(cmdClass instanceof AddGroupCommand);
}
Also used : AddGroupCommand(com.zsmartsystems.zigbee.zcl.clusters.groups.AddGroupCommand) ZclCommand(com.zsmartsystems.zigbee.zcl.ZclCommand) Test(org.junit.Test)

Aggregations

ZclCommand (com.zsmartsystems.zigbee.zcl.ZclCommand)6 ZigBeeEndpointAddress (com.zsmartsystems.zigbee.ZigBeeEndpointAddress)2 ZclHeader (com.zsmartsystems.zigbee.zcl.ZclHeader)2 Test (org.junit.Test)2 ZigBeeSerializer (com.zsmartsystems.zigbee.serialization.ZigBeeSerializer)1 ZclFieldSerializer (com.zsmartsystems.zigbee.zcl.ZclFieldSerializer)1 AddGroupCommand (com.zsmartsystems.zigbee.zcl.clusters.groups.AddGroupCommand)1 OffCommand (com.zsmartsystems.zigbee.zcl.clusters.onoff.OffCommand)1 ZclCommandType (com.zsmartsystems.zigbee.zcl.protocol.ZclCommandType)1 ZdoCommand (com.zsmartsystems.zigbee.zdo.ZdoCommand)1 BindRequest (com.zsmartsystems.zigbee.zdo.command.BindRequest)1 BindResponse (com.zsmartsystems.zigbee.zdo.command.BindResponse)1 DeviceAnnounce (com.zsmartsystems.zigbee.zdo.command.DeviceAnnounce)1 MatchDescriptorRequest (com.zsmartsystems.zigbee.zdo.command.MatchDescriptorRequest)1 MatchDescriptorResponse (com.zsmartsystems.zigbee.zdo.command.MatchDescriptorResponse)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1