use of org.openhab.binding.zwave.internal.protocol.ZWaveNode in project openhab1-addons by openhab.
the class GetRoutingInfoMessageClass method handleResponse.
@Override
public boolean handleResponse(ZWaveController zController, SerialMessage lastSentMessage, SerialMessage incomingMessage) {
int nodeId = lastSentMessage.getMessagePayloadByte(0);
logger.debug("NODE {}: Got NodeRoutingInfo request.", nodeId);
// Get the node
ZWaveNode node = zController.getNode(nodeId);
if (node == null) {
logger.error("NODE {}: Routing information for unknown node", nodeId);
incomingMessage.setTransactionCanceled();
return false;
}
node.clearNeighbors();
boolean hasNeighbors = false;
for (int by = 0; by < NODE_BYTES; by++) {
for (int bi = 0; bi < 8; bi++) {
if ((incomingMessage.getMessagePayloadByte(by) & (0x01 << bi)) != 0) {
hasNeighbors = true;
// Add the node to the neighbor list
node.addNeighbor((by << 3) + bi + 1);
}
}
}
if (!hasNeighbors) {
logger.debug("NODE {}: No neighbors reported", nodeId);
} else {
String neighbors = "Neighbor nodes:";
for (Integer neighborNode : node.getNeighbors()) {
neighbors += " " + neighborNode;
}
logger.debug("NODE {}: {}", nodeId, neighbors);
}
zController.notifyEventListeners(new ZWaveNetworkEvent(ZWaveNetworkEvent.Type.NodeRoutingInfo, nodeId, ZWaveNetworkEvent.State.Success));
checkTransactionComplete(lastSentMessage, incomingMessage);
return true;
}
use of org.openhab.binding.zwave.internal.protocol.ZWaveNode in project openhab1-addons by openhab.
the class ZWaveNodeSerializer method DeserializeNode.
/**
* Deserializes an XML tree of a {@link ZWaveNode}
*
* @param nodeId
* the number of the node to deserialize
* @return returns the Node or null in case Serialization failed.
*/
public ZWaveNode DeserializeNode(int nodeId) {
synchronized (stream) {
File file = new File(this.folderName, String.format("node%d.xml", nodeId));
BufferedReader reader = null;
logger.debug("NODE {}: Serializing from file {}", nodeId, file.getPath());
if (!file.exists()) {
logger.debug("NODE {}: Error serializing from file: file does not exist.", nodeId);
return null;
}
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
return (ZWaveNode) stream.fromXML(reader);
} catch (IOException e) {
logger.error("NODE {}: Error serializing from file: {}", nodeId, e.getMessage());
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
}
}
}
return null;
}
}
use of org.openhab.binding.zwave.internal.protocol.ZWaveNode in project openhab1-addons by openhab.
the class ApplicationCommandMessageClass method handleRequest.
@Override
public boolean handleRequest(ZWaveController zController, SerialMessage lastSentMessage, SerialMessage incomingMessage) {
logger.trace("Handle Message Application Command Request");
int nodeId = incomingMessage.getMessagePayloadByte(1);
ZWaveNode node = zController.getNode(nodeId);
if (node == null) {
logger.warn("NODE {}: Not initialized yet, ignoring message.", nodeId);
return false;
}
logger.debug("NODE {}: Application Command Request ({}:{})", nodeId, node.getNodeState().toString(), node.getNodeInitializationStage().toString());
// If the node is DEAD, but we've just received a message from it, then it's not dead!
if (node.isDead()) {
node.setNodeState(ZWaveNodeState.ALIVE);
}
node.resetResendCount();
node.incrementReceiveCount();
int commandClassCode = incomingMessage.getMessagePayloadByte(3);
ZWaveCommandClass zwaveCommandClass = resolveZWaveCommandClass(node, commandClassCode, zController);
if (zwaveCommandClass == null) {
// Error message was logged in resolveZWaveCommandClass
return false;
}
final int commandByte = incomingMessage.getMessagePayloadByte(4);
if (zwaveCommandClass instanceof ZWaveSecurityCommandClass && (ZWaveSecurityCommandClass.bytesAreEqual(ZWaveSecurityCommandClass.SECURITY_MESSAGE_ENCAP, commandByte) || ZWaveSecurityCommandClass.bytesAreEqual(ZWaveSecurityCommandClass.SECURITY_MESSAGE_ENCAP_NONCE_GET, commandByte))) {
boolean isEncapNonceGet = ZWaveSecurityCommandClass.bytesAreEqual(ZWaveSecurityCommandClass.SECURITY_MESSAGE_ENCAP_NONCE_GET, commandByte);
// Intercept security encapsulated messages here and decrypt them.
ZWaveSecurityCommandClass zwaveSecurityCommandClass = (ZWaveSecurityCommandClass) zwaveCommandClass;
logger.trace("NODE {}: Preparing to decrypt security encapsulated message, messagePayload={}", nodeId, SerialMessage.bb2hex(incomingMessage.getMessagePayload()));
int toDecryptLength = incomingMessage.getMessageBuffer().length - 9;
byte[] toDecrypt = new byte[toDecryptLength];
System.arraycopy(incomingMessage.getMessageBuffer(), 8, toDecrypt, 0, toDecryptLength);
byte[] decryptedBytes = zwaveSecurityCommandClass.decryptMessage(toDecrypt, 0);
if (decryptedBytes == null) {
logger.error("NODE {}: Failed to decrypt message out of {} .", nodeId, incomingMessage);
} else {
// call handleApplicationCommandRequest with the decrypted message. Note that we do NOT set
// incomingMessage as that needs to be processed below with the original security encapsulated message
final SerialMessage decryptedMessage = new SerialMessage(incomingMessage.getMessageClass(), incomingMessage.getMessageType(), incomingMessage.getExpectedReply(), incomingMessage.getPriority());
decryptedMessage.setMessagePayload(decryptedBytes);
// Get the new command class with the decrypted contents
zwaveCommandClass = resolveZWaveCommandClass(node, decryptedBytes[1], zController);
// Use a flag bc we need to handle isEncapNonceGet either way
boolean failed = false;
if (zwaveCommandClass == null) {
// Error message was logged in resolveZWaveCommandClass
failed = true;
} else {
// Note that we do not call node.doesMessageRequireSecurityEncapsulation since it was encapsulated.
// Messages that are not required to be are allowed to be, just not the other way around
logger.debug("NODE {}: After decrypt, found Command Class {}, passing to handleApplicationCommandRequest", nodeId, zwaveCommandClass.getCommandClass().getLabel());
zwaveCommandClass.handleApplicationCommandRequest(decryptedMessage, 2, 0);
}
if (isEncapNonceGet) {
// the device also needs another nonce; send it regardless of the success/failure of decryption
zwaveSecurityCommandClass.sendNonceReport();
}
if (failed) {
return false;
}
}
} else {
// Message does not require decryption
if (node.doesMessageRequireSecurityEncapsulation(incomingMessage)) {
// Should have been security encapsulation but wasn't!
logger.error("NODE {}: Command Class {} {} was required to be security encapsulation but it wasn't! Dropping message.", nodeId, zwaveCommandClass.getCommandClass().getKey(), zwaveCommandClass.getCommandClass().getLabel());
// do not call zwaveCommandClass.handleApplicationCommandRequest();
} else {
logger.trace("NODE {}: Found Command Class {}, passing to handleApplicationCommandRequest", nodeId, zwaveCommandClass.getCommandClass().getLabel());
zwaveCommandClass.handleApplicationCommandRequest(incomingMessage, 4, 0);
}
}
if (node.getNodeId() == lastSentMessage.getMessageNode()) {
checkTransactionComplete(lastSentMessage, incomingMessage);
} else {
logger.debug("NODE {}: Transaction not completed: node address inconsistent. lastSent={}, incoming={}", lastSentMessage.getMessageNode(), lastSentMessage.getMessageNode(), incomingMessage.getMessageNode());
}
return true;
}
use of org.openhab.binding.zwave.internal.protocol.ZWaveNode in project openhab1-addons by openhab.
the class IdentifyNodeMessageClass method handleResponse.
@Override
public boolean handleResponse(ZWaveController zController, SerialMessage lastSentMessage, SerialMessage incomingMessage) {
logger.trace("Handle Message Get Node ProtocolInfo Response");
// Check that this request is consistent with the response
if (lastSentMessage.getMessageClass() != SerialMessageClass.IdentifyNode) {
logger.warn("Got IdentifyNodeMessage without request, ignoring. Last message was {}.", lastSentMessage.getMessageClass());
return false;
}
int nodeId = lastSentMessage.getMessagePayloadByte(0);
logger.debug("NODE {}: ProtocolInfo", nodeId);
ZWaveNode node = zController.getNode(nodeId);
boolean listening = (incomingMessage.getMessagePayloadByte(0) & 0x80) != 0 ? true : false;
boolean routing = (incomingMessage.getMessagePayloadByte(0) & 0x40) != 0 ? true : false;
int version = (incomingMessage.getMessagePayloadByte(0) & 0x07) + 1;
boolean frequentlyListening = (incomingMessage.getMessagePayloadByte(1) & 0x60) != 0 ? true : false;
boolean beaming = ((incomingMessage.getMessagePayloadByte(1) & 0x10) != 0);
boolean security = ((incomingMessage.getMessagePayloadByte(1) & 0x01) != 0);
int maxBaudRate = 9600;
if ((incomingMessage.getMessagePayloadByte(0) & 0x38) == 0x10) {
maxBaudRate = 40000;
}
logger.debug("NODE {}: Listening = {}", nodeId, listening);
logger.debug("NODE {}: Routing = {}", nodeId, routing);
logger.debug("NODE {}: Beaming = {}", nodeId, beaming);
logger.debug("NODE {}: Version = {}", nodeId, version);
logger.debug("NODE {}: FLIRS = {}", nodeId, frequentlyListening);
logger.debug("NODE {}: Security = {}", nodeId, security);
logger.debug("NODE {}: Max Baud = {}", nodeId, maxBaudRate);
node.setListening(listening);
node.setRouting(routing);
node.setVersion(version);
node.setFrequentlyListening(frequentlyListening);
node.setSecurity(security);
node.setBeaming(beaming);
node.setMaxBaud(maxBaudRate);
Basic basic = Basic.getBasic(incomingMessage.getMessagePayloadByte(3));
if (basic == null) {
logger.error(String.format("NODE %d: Basic device class 0x%02x not found", nodeId, incomingMessage.getMessagePayloadByte(3)));
return false;
}
logger.debug("NODE {}: Basic = {}", nodeId, basic.getLabel());
Generic generic = Generic.getGeneric(incomingMessage.getMessagePayloadByte(4));
if (generic == null) {
logger.error(String.format("NODE %d: Generic device class 0x%02x not found", nodeId, incomingMessage.getMessagePayloadByte(4)));
return false;
}
logger.debug("NODE {}: Generic = {}", nodeId, generic.getLabel());
Specific specific = Specific.getSpecific(generic, incomingMessage.getMessagePayloadByte(5));
if (specific == null) {
logger.error(String.format("NODE %d: Specific device class 0x%02x not found", nodeId, incomingMessage.getMessagePayloadByte(5)));
return false;
}
logger.debug("NODE {}: Specific = {}", nodeId, specific.getLabel());
ZWaveDeviceClass deviceClass = node.getDeviceClass();
deviceClass.setBasicDeviceClass(basic);
deviceClass.setGenericDeviceClass(generic);
deviceClass.setSpecificDeviceClass(specific);
// Add mandatory command classes as specified by its generic device class.
for (CommandClass commandClass : generic.getMandatoryCommandClasses()) {
ZWaveCommandClass zwaveCommandClass = ZWaveCommandClass.getInstance(commandClass.getKey(), node, zController);
if (zwaveCommandClass != null) {
zController.getNode(nodeId).addCommandClass(zwaveCommandClass);
}
}
// Add mandatory command classes as specified by its specific device class.
for (CommandClass commandClass : specific.getMandatoryCommandClasses()) {
ZWaveCommandClass zwaveCommandClass = ZWaveCommandClass.getInstance(commandClass.getKey(), node, zController);
if (zwaveCommandClass != null) {
node.addCommandClass(zwaveCommandClass);
}
}
checkTransactionComplete(lastSentMessage, incomingMessage);
return true;
}
use of org.openhab.binding.zwave.internal.protocol.ZWaveNode in project openhab1-addons by openhab.
the class IsFailedNodeMessageClass method handleResponse.
@Override
public boolean handleResponse(ZWaveController zController, SerialMessage lastSentMessage, SerialMessage incomingMessage) {
int nodeId = lastSentMessage.getMessagePayloadByte(0);
ZWaveNode node = zController.getNode(nodeId);
if (node == null) {
logger.error("NODE {}: Failed node message for unknown node", nodeId);
incomingMessage.setTransactionCanceled();
return false;
}
if (incomingMessage.getMessagePayloadByte(0) != 0x00) {
logger.warn("NODE {}: Is currently marked as failed by the controller!", nodeId);
node.setNodeState(ZWaveNodeState.FAILED);
} else {
logger.debug("NODE {}: Is currently marked as healthy by the controller", nodeId);
}
checkTransactionComplete(lastSentMessage, incomingMessage);
return true;
}
Aggregations