use of com.zsmartsystems.zigbee.dongle.cc2531.zigbee.util.DoubleByte in project com.zsmartsystems.zigbee by zsmartsystems.
the class CommandInterfaceImpl method handlePacket.
/**
* Handle parsed packet.
*
* @param packet the packet
*/
@Override
public void handlePacket(final ZToolPacket packet) {
final DoubleByte cmdId = packet.getCMD();
switch(cmdId.getMsb() & 0xE0) {
// Received incoming message which can be either message from dongle or remote device.
case 0x40:
logger.debug("<-- {} ({})", packet.getClass().getSimpleName(), ByteUtils.toBase16(packet.getPacket()));
notifyAsynchronousCommand(packet);
break;
// Received synchronous command response.
case 0x60:
logger.debug("<- {} ({})", packet.getClass().getSimpleName(), ByteUtils.toBase16(packet.getPacket()));
notifySynchronousCommand(packet);
break;
default:
logger.error("Received unknown packet. {}", packet.getClass().getSimpleName());
break;
}
}
use of com.zsmartsystems.zigbee.dongle.cc2531.zigbee.util.DoubleByte in project com.zsmartsystems.zigbee by zsmartsystems.
the class ZigBeeNetworkManager method createZigBeeNetwork.
private boolean createZigBeeNetwork() {
createCustomDevicesOnDongle();
logger.debug("Creating network as {}", mode.toString());
final int ALL_CLUSTERS = 0xFFFF;
logger.trace("Reset seq: Trying MSG_CB_REGISTER");
ZDO_MSG_CB_REGISTER_SRSP responseCb = (ZDO_MSG_CB_REGISTER_SRSP) sendSynchronous(new ZDO_MSG_CB_REGISTER(new DoubleByte(ALL_CLUSTERS)));
if (responseCb == null) {
return false;
}
ZB_WRITE_CONFIGURATION_RSP responseCfg;
responseCfg = (ZB_WRITE_CONFIGURATION_RSP) sendSynchronous(new ZB_WRITE_CONFIGURATION(ZB_WRITE_CONFIGURATION.CONFIG_ID.ZCD_NV_ZDO_DIRECT_CB, new int[] { 1 }));
if (responseCfg == null) {
return false;
}
final int instantStartup = 0;
ZDO_STARTUP_FROM_APP_SRSP response = (ZDO_STARTUP_FROM_APP_SRSP) sendSynchronous(new ZDO_STARTUP_FROM_APP(instantStartup), STARTUP_TIMEOUT);
if (response == null) {
return false;
}
switch(response.Status) {
case 0:
{
logger.info("Initialized ZigBee network with existing network state.");
return true;
}
case 1:
{
logger.info("Initialized ZigBee network with new or reset network state.");
return true;
}
case 2:
{
logger.warn("Initializing ZigBee network failed.");
return false;
}
default:
{
logger.error("Unexpected response state for ZDO_STARTUP_FROM_APP {}", response.Status);
return false;
}
}
}
use of com.zsmartsystems.zigbee.dongle.cc2531.zigbee.util.DoubleByte in project com.zsmartsystems.zigbee by zsmartsystems.
the class CommandInterfaceImpl method sendSynchronousCommand.
/**
* Sends synchronous command and adds listener.
*
* @param packet the command packet
* @param listener the synchronous command response listener
* @param timeoutMillis the timeout
* @throws IOException if IO exception occurs in packet sending
*/
@Override
public void sendSynchronousCommand(final ZToolPacket packet, final SynchronousCommandListener listener, final long timeoutMillis) throws IOException {
if (timeoutMillis == -1L) {
synchronousCommandListenerTimeouts.put(listener, -1L);
} else {
final long expirationTime = System.currentTimeMillis() + timeoutMillis;
synchronousCommandListenerTimeouts.put(listener, expirationTime);
}
final DoubleByte cmdId = packet.getCMD();
final int value = (cmdId.getMsb() & 0xE0);
if (value != 0x20) {
throw new IllegalArgumentException("You are trying to send a non SREQ packet as synchronous command. " + "Evaluated " + value + " instead of " + 0x20 + "\nPacket " + packet.getClass().getName() + "\n" + packet);
}
cleanExpiredSynchronousCommandListeners();
if (supportMultipleSynchrounsCommand) {
synchronized (synchronousCommandListeners) {
final short id = (short) (cmdId.get16BitValue() & 0x1FFF);
while (synchronousCommandListeners.get(cmdId) != null) {
try {
logger.trace("Waiting for other request {} to complete", id);
synchronousCommandListeners.wait(500);
cleanExpiredSynchronousCommandListeners();
} catch (InterruptedException ignored) {
}
}
synchronousCommandListeners.put(id, listener);
}
} else {
synchronized (synchronousCommandListeners) {
final short id = (short) (cmdId.get16BitValue() & 0x1FFF);
while (!synchronousCommandListeners.isEmpty()) {
try {
logger.trace("Waiting for other request to complete");
synchronousCommandListeners.wait(500);
cleanExpiredSynchronousCommandListeners();
} catch (InterruptedException ignored) {
}
}
logger.trace("Put synchronousCommandListeners listener for {} command", id);
synchronousCommandListeners.put(id, listener);
}
}
logger.trace("Sending SynchronousCommand {} ", packet);
sendPacket(packet);
}
use of com.zsmartsystems.zigbee.dongle.cc2531.zigbee.util.DoubleByte in project com.zsmartsystems.zigbee by zsmartsystems.
the class CommandInterfaceImpl method notifySynchronousCommand.
/**
* Notifies listeners about synchronous command response.
*
* @param packet the received packet
*/
private void notifySynchronousCommand(final ZToolPacket packet) {
final DoubleByte cmdId = packet.getCMD();
synchronized (synchronousCommandListeners) {
final short id = (short) (cmdId.get16BitValue() & 0x1FFF);
final SynchronousCommandListener listener = synchronousCommandListeners.get(id);
if (listener != null) {
listener.receivedCommandResponse(packet);
synchronousCommandListeners.remove(id);
synchronousCommandListeners.notifyAll();
} else {
// Notify asynchronous command listeners of unclaimed asynchronous command responses.
final AsynchronousCommandListener[] listeners;
synchronized (asynchrounsCommandListeners) {
listeners = asynchrounsCommandListeners.toArray(new AsynchronousCommandListener[] {});
}
for (final AsynchronousCommandListener asynchronousCommandListener : listeners) {
try {
asynchronousCommandListener.receivedUnclaimedSynchronousCommandResponse(packet);
} catch (Throwable e) {
logger.error("Error in incoming asynchronous message processing: ", e);
}
}
}
}
}
use of com.zsmartsystems.zigbee.dongle.cc2531.zigbee.util.DoubleByte in project com.zsmartsystems.zigbee by zsmartsystems.
the class ZToolPacketStream method parsePacket.
public ZToolPacket parsePacket() throws IOException {
Exception exception;
done = false;
bytesRead = 0;
try {
final ZToolPacket response;
// int byteLength = this.read("Length");
this.length = read("Length");
// log.debug("data length is " + ByteUtils.formatByte(length.getLength()));
final int[] frameData;
final int apiIdMSB = this.read("API PROFILE_ID_HOME_AUTOMATION MSB");
final int apiIdLSB = this.read("API PROFILE_ID_HOME_AUTOMATION LSB");
final DoubleByte apiId = new DoubleByte(apiIdMSB, apiIdLSB);
// generic = true;
if (generic) {
// log.info("Parsing data as generic");
int i = 0;
frameData = new int[length];
// Read all data bytes without parsing
while (i < frameData.length) {
frameData[i] = this.read("Data " + i + "-th");
i++;
}
response = new ZToolPacket(apiId, frameData);
} else {
frameData = this.readRemainingBytes();
response = parsePayload(apiId, frameData);
}
// response.setFCS(this.read("Checksum"));
int fcs = this.read("Checksum");
// setDone(true);
if (fcs != response.getFCS()) {
// log.debug("Checksum of packet failed: received =" + fcs + " expected = " + response.getFCS());
throw new ZToolParseException("Packet checksum failed");
}
if (!this.isDone()) {
// TODO this is not the answer!
throw new ZToolParseException("Packet stream is not finished yet we seem to think it is");
}
return response;
} catch (Exception e) {
logger.error("Packet parsing failed due to exception.", e);
exception = e;
}
final ZToolPacket exceptionResponse = new ErrorPacket();
if (exception != null) {
exceptionResponse.setError(true);
exceptionResponse.setErrorMsg(exception.getMessage());
}
return exceptionResponse;
}
Aggregations