Search in sources :

Example 1 with CommandClass

use of org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveCommandClass.CommandClass in project openhab1-addons by openhab.

the class ZWaveNode method setSecuredClasses.

/**
     * Invoked by {@link ZWaveSecurityCommandClass} when a
     * {@link ZWaveSecurityCommandClass#SECURITY_SUPPORTED_REPORT} is received.
     *
     * @param data the class id for each class which must be encrypted in transmission
     */
public void setSecuredClasses(byte[] data) {
    logger.info("NODE {}:  Setting secured command classes for node with {}", this.getNodeId(), SerialMessage.bb2hex(data));
    boolean afterMark = false;
    // reset the existing list
    securedCommandClasses.clear();
    for (final byte aByte : data) {
        // TODO: DB support extended commandClass format by checking for 0xF1 - 0xFF
        if (ZWaveSecurityCommandClass.bytesAreEqual(aByte, ZWaveSecurityCommandClass.COMMAND_CLASS_MARK)) {
            /**
                 * Marks the end of the list of supported command classes. The remaining classes are those
                 * that can be controlled by the device. These classes are created without values.
                 * Messages received cause notification events instead.
                 */
            afterMark = true;
            continue;
        }
        // Check if this is a commandClass that is already registered with the node
        final CommandClass commandClass = CommandClass.getCommandClass((aByte & 0xFF));
        if (commandClass == null) {
            // Not supported by OpenHab
            logger.error("NODE {}:  setSecuredClasses requested secure " + "class NOT supported by OpenHab: {}   afterMark={}", this.getNodeId(), commandClass, afterMark);
        } else {
            // can't be set that way since it's the one doing the encryption work So ignore that.
            if (commandClass == CommandClass.SECURITY) {
                continue;
            } else if (afterMark) {
                // Nothing to do, we don't track devices that control other devices
                logger.info("NODE {}:  is after mark for commandClass {}", this.getNodeId(), commandClass);
                break;
            } else {
                if (!this.supportsCommandClass(commandClass)) {
                    logger.info("NODE {}:  Adding secured command class to supported that wasn't in original list {}", this.getNodeId(), commandClass.getLabel());
                    final ZWaveCommandClass classInstance = ZWaveCommandClass.getInstance((aByte & 0xFF), this, controller);
                    if (classInstance != null) {
                        addCommandClass(classInstance);
                    }
                }
                securedCommandClasses.add(commandClass);
                logger.info("NODE {}:  (Secured) {}", this.getNodeId(), commandClass.getLabel());
            }
        }
    }
    if (logger.isInfoEnabled()) {
        // show which classes are still insecure after the update
        final StringBuilder buf = new StringBuilder("NODE " + this.getNodeId() + ": After update, INSECURE command classes are: ");
        for (final ZWaveCommandClass zwCommandClass : this.getCommandClasses()) {
            if (!securedCommandClasses.contains(zwCommandClass.getCommandClass())) {
                buf.append(zwCommandClass.getCommandClass() + ", ");
            }
        }
        logger.info(buf.toString().substring(0, buf.toString().length() - 1));
    }
}
Also used : ZWaveCommandClass(org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveCommandClass) ZWaveMultiInstanceCommandClass(org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveMultiInstanceCommandClass) ZWaveAssociationCommandClass(org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveAssociationCommandClass) ZWaveWakeUpCommandClass(org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveWakeUpCommandClass) CommandClass(org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveCommandClass.CommandClass) ZWaveSecurityCommandClass(org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveSecurityCommandClass) ZWaveVersionCommandClass(org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveVersionCommandClass) ZWaveNodeNamingCommandClass(org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveNodeNamingCommandClass) ZWaveCommandClass(org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveCommandClass)

Example 2 with CommandClass

use of org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveCommandClass.CommandClass in project openhab1-addons by openhab.

the class ZWaveConverterBase method getStateConverter.

/**
     * Gets a {@link ZWaveStateConverter} that is suitable for this {@link CommandClass} and the data types supported by
     * the {@link Item}
     *
     * @param commandClass the {@link CommandClass} that sent the value.
     * @param item the {@link Item} that has to receive the State;
     * @return a converter object that converts between the value and the state;
     */
protected ZWaveStateConverter<?, ?> getStateConverter(Item item, Object value) {
    if (item == null) {
        return null;
    }
    List<Class<? extends State>> list = new ArrayList<Class<? extends State>>(item.getAcceptedDataTypes());
    Collections.sort(list, new StateComparator());
    for (Class<? extends State> stateClass : list) {
        ZWaveStateConverter<?, ?> result = stateConverters.get(stateClass);
        if (result == null || !result.getType().isInstance(value)) {
            continue;
        }
        return result;
    }
    return null;
}
Also used : State(org.openhab.core.types.State) ArrayList(java.util.ArrayList) CommandClass(org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveCommandClass.CommandClass) StateComparator(org.openhab.binding.zwave.internal.converter.state.StateComparator)

Example 3 with CommandClass

use of org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveCommandClass.CommandClass in project openhab1-addons by openhab.

the class ApplicationUpdateMessageClass method handleRequest.

@Override
public boolean handleRequest(ZWaveController zController, SerialMessage lastSentMessage, SerialMessage incomingMessage) {
    int nodeId;
    boolean result = true;
    UpdateState updateState = UpdateState.getUpdateState(incomingMessage.getMessagePayloadByte(0));
    switch(updateState) {
        case NODE_INFO_RECEIVED:
            // We've received a NIF, and this contains the node ID.
            nodeId = incomingMessage.getMessagePayloadByte(1);
            logger.debug("NODE {}: Application update request. Node information received.", nodeId);
            int length = incomingMessage.getMessagePayloadByte(2);
            ZWaveNode node = zController.getNode(nodeId);
            if (node == null) {
                logger.debug("NODE {}: Application update request. Node not known!", nodeId);
                // This allows everyone to be notified.
                if (nodeId > 0 && nodeId <= 232) {
                    zController.notifyEventListeners(new ZWaveInclusionEvent(ZWaveInclusionEvent.Type.IncludeDone, incomingMessage.getMessagePayloadByte(2)));
                }
                break;
            }
            node.resetResendCount();
            // Remember that we've received this so we can continue initialisation
            node.setApplicationUpdateReceived(true);
            // If we're finished initialisation, then we can treat this like a HAIL
            if (node.getNodeInitializationStage() == ZWaveNodeInitStage.DONE) {
                // If this node supports associations, then assume this should be handled through that mechanism
                if (node.getCommandClass(CommandClass.ASSOCIATION) == null) {
                    // If we receive an Application Update Request and the node is already
                    // fully initialised we assume this is a request to the controller to
                    // re-get the current node values
                    logger.debug("NODE {}: Application update request. Requesting node state.", nodeId);
                    zController.pollNode(node);
                }
            } else {
                List<Integer> nif = new ArrayList<Integer>();
                for (int i = 6; i < length + 3; i++) {
                    int data = incomingMessage.getMessagePayloadByte(i);
                    if (data == 0xef) {
                        // TODO: Implement control command classes
                        break;
                    }
                    logger.trace(String.format("NODE %d: Command class 0x%02X is supported.", nodeId, data));
                    // See if the command class already exists on the node
                    CommandClass commandClass = CommandClass.getCommandClass(data);
                    if (node.getCommandClass(commandClass) == null) {
                        // add it
                        ZWaveCommandClass zwaveCommandClass = ZWaveCommandClass.getInstance(data, node, zController);
                        if (zwaveCommandClass != null) {
                            logger.trace(String.format("NODE %d: Application update request. Adding Command class %s.", nodeId, commandClass));
                            node.addCommandClass(zwaveCommandClass);
                        }
                    }
                }
                node.updateNIF(nif);
            }
            // Notify we received an info frame
            zController.notifyEventListeners(new ZWaveNodeInfoEvent(nodeId));
            // Treat the node information frame as a wakeup
            ZWaveWakeUpCommandClass wakeUp = (ZWaveWakeUpCommandClass) node.getCommandClass(ZWaveCommandClass.CommandClass.WAKE_UP);
            if (wakeUp != null) {
                wakeUp.setAwake(true);
            }
            break;
        case NODE_INFO_REQ_FAILED:
            // Make sure we can correlate the request before we use the nodeId
            if (lastSentMessage.getMessageClass() != SerialMessageClass.RequestNodeInfo) {
                logger.warn("Got ApplicationUpdateMessage without request, ignoring. Last message was {}.", lastSentMessage.getMessageClass());
                return false;
            }
            // The failed message doesn't contain the node number, so use the info from the request.
            nodeId = lastSentMessage.getMessageNode();
            logger.debug("NODE {}: Application update request. Node Info Request Failed.", nodeId);
            // Handle retries
            if (--lastSentMessage.attempts >= 0) {
                logger.error("NODE {}: Got Node Info Request Failed. Requeueing", nodeId);
                zController.enqueue(lastSentMessage);
            } else {
                logger.warn("NODE {}: Node Info Request Failed 3x. Discarding message: {}", nodeId, lastSentMessage.toString());
            }
            // Transaction is not successful
            incomingMessage.setTransactionCanceled();
            result = false;
            break;
        default:
            logger.warn("TODO: Implement Application Update Request Handling of {} ({}).", updateState.getLabel(), updateState.getKey());
    }
    // Check if this completes the transaction
    checkTransactionComplete(lastSentMessage, incomingMessage);
    return result;
}
Also used : ZWaveCommandClass(org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveCommandClass) ZWaveNode(org.openhab.binding.zwave.internal.protocol.ZWaveNode) ZWaveInclusionEvent(org.openhab.binding.zwave.internal.protocol.event.ZWaveInclusionEvent) ArrayList(java.util.ArrayList) ZWaveWakeUpCommandClass(org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveWakeUpCommandClass) CommandClass(org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveCommandClass.CommandClass) ZWaveCommandClass(org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveCommandClass) ZWaveNodeInfoEvent(org.openhab.binding.zwave.internal.protocol.event.ZWaveNodeInfoEvent) ZWaveWakeUpCommandClass(org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveWakeUpCommandClass)

Example 4 with CommandClass

use of org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveCommandClass.CommandClass in project openhab1-addons by openhab.

the class ApplicationCommandMessageClass method resolveZWaveCommandClass.

/**
     * Takes the given commandClassCode and tries to instantiate the corresponding {@link ZWaveCommandClass}
     * for the given node
     *
     * @return the zwave command class for this node or null if it is not possible
     */
private ZWaveCommandClass resolveZWaveCommandClass(ZWaveNode node, int commandClassCode, ZWaveController zController) {
    CommandClass commandClass = CommandClass.getCommandClass(commandClassCode & 0xff);
    if (commandClass == null) {
        logger.error(String.format("NODE %d: Unsupported command class 0x%02x", node.getNodeId(), commandClassCode));
        return null;
    }
    logger.debug(String.format("NODE %d: Incoming command class %s (0x%02x)", node.getNodeId(), commandClass.getLabel(), commandClass.getKey()));
    ZWaveCommandClass zwaveCommandClass = node.getCommandClass(commandClass);
    // Let's add it now then to support handling this message.
    if (zwaveCommandClass == null) {
        logger.debug(String.format("NODE %d: Command class %s (0x%02x) not found, trying to add it.", node.getNodeId(), commandClass.getLabel(), commandClass.getKey()));
        zwaveCommandClass = ZWaveCommandClass.getInstance(commandClass.getKey(), node, zController);
        if (zwaveCommandClass == null) {
            // We got an unsupported command class, leave zwaveCommandClass as null
            logger.error(String.format("NODE %d: Unsupported zwave command class %s (0x%02x)", node.getNodeId(), commandClass.getLabel(), commandClassCode));
        } else {
            logger.debug(String.format("NODE %d: Adding command class %s (0x%02x)", node.getNodeId(), commandClass.getLabel(), commandClass.getKey()));
            node.addCommandClass(zwaveCommandClass);
        }
    }
    return zwaveCommandClass;
}
Also used : ZWaveCommandClass(org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveCommandClass) CommandClass(org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveCommandClass.CommandClass) ZWaveSecurityCommandClass(org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveSecurityCommandClass) ZWaveCommandClass(org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveCommandClass)

Example 5 with CommandClass

use of org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveCommandClass.CommandClass 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;
}
Also used : Basic(org.openhab.binding.zwave.internal.protocol.ZWaveDeviceClass.Basic) ZWaveCommandClass(org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveCommandClass) ZWaveNode(org.openhab.binding.zwave.internal.protocol.ZWaveNode) Generic(org.openhab.binding.zwave.internal.protocol.ZWaveDeviceClass.Generic) CommandClass(org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveCommandClass.CommandClass) ZWaveCommandClass(org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveCommandClass) Specific(org.openhab.binding.zwave.internal.protocol.ZWaveDeviceClass.Specific) ZWaveDeviceClass(org.openhab.binding.zwave.internal.protocol.ZWaveDeviceClass)

Aggregations

CommandClass (org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveCommandClass.CommandClass)6 ZWaveCommandClass (org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveCommandClass)5 ZWaveSecurityCommandClass (org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveSecurityCommandClass)3 ZWaveWakeUpCommandClass (org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveWakeUpCommandClass)3 ArrayList (java.util.ArrayList)2 ZWaveNode (org.openhab.binding.zwave.internal.protocol.ZWaveNode)2 ZWaveAssociationCommandClass (org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveAssociationCommandClass)2 ZWaveMultiInstanceCommandClass (org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveMultiInstanceCommandClass)2 ZWaveNodeNamingCommandClass (org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveNodeNamingCommandClass)2 ZWaveVersionCommandClass (org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveVersionCommandClass)2 StateComparator (org.openhab.binding.zwave.internal.converter.state.StateComparator)1 ZWaveDeviceClass (org.openhab.binding.zwave.internal.protocol.ZWaveDeviceClass)1 Basic (org.openhab.binding.zwave.internal.protocol.ZWaveDeviceClass.Basic)1 Generic (org.openhab.binding.zwave.internal.protocol.ZWaveDeviceClass.Generic)1 Specific (org.openhab.binding.zwave.internal.protocol.ZWaveDeviceClass.Specific)1 ZWaveInclusionEvent (org.openhab.binding.zwave.internal.protocol.event.ZWaveInclusionEvent)1 ZWaveNodeInfoEvent (org.openhab.binding.zwave.internal.protocol.event.ZWaveNodeInfoEvent)1 State (org.openhab.core.types.State)1