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));
}
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;
}
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;
}
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);
}
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);
}
Aggregations