use of com.zsmartsystems.zigbee.zcl.ZclFieldSerializer in project com.zsmartsystems.zigbee by zsmartsystems.
the class ManagementPermitJoiningRequestTest method testReceive.
@Test
public void testReceive() {
// Short response - ie not extended
int[] packet = getPacketData("00 FF 01");
ManagementPermitJoiningRequest request = new ManagementPermitJoiningRequest();
request.setDestinationAddress(new ZigBeeEndpointAddress(0));
request.setTcSignificance(true);
request.setPermitDuration(255);
DefaultSerializer serializer = new DefaultSerializer();
ZclFieldSerializer fieldSerializer = new ZclFieldSerializer(serializer);
request.serialize(fieldSerializer);
assertTrue(Arrays.equals(packet, serializer.getPayload()));
}
use of com.zsmartsystems.zigbee.zcl.ZclFieldSerializer in project com.zsmartsystems.zigbee by zsmartsystems.
the class ZigBeeNetworkManagerTest method testReceiveZclCommand.
@Test
public void testReceiveZclCommand() {
ZigBeeNetworkManager networkManager = mockZigBeeNetworkManager();
networkManager.setSerializer(DefaultSerializer.class, DefaultDeserializer.class);
ZigBeeApsFrame apsFrame = new ZigBeeApsFrame();
apsFrame.setSourceAddress(1234);
apsFrame.setDestinationAddress(0);
apsFrame.setSequence(1);
apsFrame.setCluster(6);
apsFrame.setDestinationEndpoint(2);
apsFrame.setProfile(0x104);
apsFrame.setSourceEndpoint(5);
ZclHeader zclHeader = new ZclHeader();
zclHeader.setCommandId(0);
zclHeader.setFrameType(ZclFrameType.ENTIRE_PROFILE_COMMAND);
zclHeader.setSequenceNumber(1);
DefaultSerializer serializer = new DefaultSerializer();
ZclFieldSerializer fieldSerializer = new ZclFieldSerializer(serializer);
apsFrame.setPayload(zclHeader.serialize(fieldSerializer, new int[] {}));
networkManager.receiveCommand(apsFrame);
org.awaitility.Awaitility.await().until(commandListenerUpdated(), org.hamcrest.Matchers.equalTo(1));
ReadAttributesCommand response = (ReadAttributesCommand) commandListenerCapture.get(0);
assertEquals(6, (int) response.getClusterId());
assertEquals(0, (int) response.getCommandId());
assertEquals(1, (int) response.getTransactionId());
assertEquals(new ZigBeeEndpointAddress(1234, 5), response.getSourceAddress());
}
use of com.zsmartsystems.zigbee.zcl.ZclFieldSerializer in project com.zsmartsystems.zigbee by zsmartsystems.
the class EzspSendUnicastTest method testSendPermitJoining.
@Test
public void testSendPermitJoining() {
EzspFrame.setEzspVersion(4);
ManagementPermitJoiningRequest permitJoining = new ManagementPermitJoiningRequest();
permitJoining.setDestinationAddress(new ZigBeeEndpointAddress(0x401C));
permitJoining.setSourceAddress(new ZigBeeEndpointAddress(0));
permitJoining.setTcSignificance(false);
permitJoining.setPermitDuration(60);
DefaultSerializer serializer = new DefaultSerializer();
ZclFieldSerializer fieldSerializer = new ZclFieldSerializer(serializer);
permitJoining.serialize(fieldSerializer);
int[] payload = serializer.getPayload();
EzspSendUnicastRequest emberUnicast = new EzspSendUnicastRequest();
EmberApsFrame apsFrame = new EmberApsFrame();
apsFrame.setClusterId(permitJoining.getClusterId());
apsFrame.setProfileId(0);
apsFrame.setSourceEndpoint(1);
apsFrame.setDestinationEndpoint(0);
apsFrame.setSequence(0x88);
apsFrame.addOptions(EmberApsOption.EMBER_APS_OPTION_RETRY);
apsFrame.addOptions(EmberApsOption.EMBER_APS_OPTION_ENABLE_ADDRESS_DISCOVERY);
apsFrame.addOptions(EmberApsOption.EMBER_APS_OPTION_ENABLE_ROUTE_DISCOVERY);
apsFrame.setGroupId(0xffff);
emberUnicast.setMessageTag(0x99);
emberUnicast.setSequenceNumber(0xaa);
emberUnicast.setType(EmberOutgoingMessageType.EMBER_OUTGOING_DIRECT);
emberUnicast.setApsFrame(apsFrame);
emberUnicast.setIndexOrDestination(permitJoining.getDestinationAddress().getAddress());
emberUnicast.setMessageContents(payload);
int[] messageToSend = emberUnicast.serialize();
System.out.println(emberUnicast.toString());
System.out.println(Arrays.toString(messageToSend));
String out = "";
for (int c : messageToSend) {
out += String.format("%02X ", c);
}
System.out.println(out);
}
use of com.zsmartsystems.zigbee.zcl.ZclFieldSerializer in project com.zsmartsystems.zigbee by zsmartsystems.
the class EzspSendBroadcastTest method testSendPermitJoining.
@Test
public void testSendPermitJoining() {
ManagementPermitJoiningRequest permitJoining = new ManagementPermitJoiningRequest();
permitJoining.setDestinationAddress(new ZigBeeEndpointAddress(0xFFFC));
permitJoining.setSourceAddress(new ZigBeeEndpointAddress(0));
permitJoining.setTcSignificance(true);
permitJoining.setPermitDuration(255);
DefaultSerializer serializer = new DefaultSerializer();
ZclFieldSerializer fieldSerializer = new ZclFieldSerializer(serializer);
permitJoining.serialize(fieldSerializer);
int[] payload = serializer.getPayload();
EzspSendBroadcastRequest emberBroadcast = new EzspSendBroadcastRequest();
EmberApsFrame apsFrame = new EmberApsFrame();
apsFrame.setClusterId(permitJoining.getClusterId());
apsFrame.setProfileId(0);
apsFrame.setSourceEndpoint(0);
apsFrame.setDestinationEndpoint(0);
apsFrame.setSequence(5);
apsFrame.addOptions(EmberApsOption.EMBER_APS_OPTION_RETRY);
apsFrame.addOptions(EmberApsOption.EMBER_APS_OPTION_ENABLE_ADDRESS_DISCOVERY);
apsFrame.addOptions(EmberApsOption.EMBER_APS_OPTION_ENABLE_ROUTE_DISCOVERY);
apsFrame.setGroupId(0);
emberBroadcast.setMessageTag(5);
emberBroadcast.setSequenceNumber(5);
emberBroadcast.setApsFrame(apsFrame);
emberBroadcast.setDestination(permitJoining.getDestinationAddress().getAddress());
emberBroadcast.setMessageContents(payload);
emberBroadcast.setRadius(31);
int[] messageToSend = emberBroadcast.serialize();
String out = "";
for (int c : messageToSend) {
out += String.format("%02X ", c);
}
System.out.println(out);
assertTrue(Arrays.equals(getPacketData("05 00 36 FC FF 00 00 36 00 00 00 40 11 00 00 05 1F 05 03 00 FF 01"), messageToSend));
}
use of com.zsmartsystems.zigbee.zcl.ZclFieldSerializer 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;
}
Aggregations