Search in sources :

Example 6 with SerialMessage

use of org.openhab.binding.zwave.internal.protocol.SerialMessage in project openhab1-addons by openhab.

the class ZWaveSecurityCommandClassWithInitialization method handleApplicationCommandRequest.

/**
     * {@inheritDoc}
     */
@Override
public void handleApplicationCommandRequest(SerialMessage serialMessage, int offset, int endpoint) {
    byte command = (byte) serialMessage.getMessagePayloadByte(offset);
    if (logger.isDebugEnabled()) {
        logger.debug(String.format("NODE %s: Received Security Message 0x%02X %s ", this.getNode().getNodeId(), command, commandToString(command)));
    }
    traceHex("payload bytes for incoming security message", serialMessage.getMessagePayload());
    lastReceivedMessageTimestamp = System.currentTimeMillis();
    if (inclusionStateTracker != null && !inclusionStateTracker.verifyAndAdvanceState(command)) {
        // bad order, abort
        return;
    }
    switch(command) {
        case SECURITY_SCHEME_REPORT:
            // Should be received during inclusion only
            if (!wasThisNodeJustIncluded() || inclusionStateTracker == null) {
                logger.error("NODE {}: Received SECURITY_SCHEME_REPORT but we are not in inclusion mode! {}", serialMessage);
                return;
            }
            int schemes = serialMessage.getMessagePayloadByte(offset + 1);
            logger.debug("NODE {}: Received Security Scheme Report: ", this.getNode().getNodeId(), schemes);
            if (schemes == SECURITY_SCHEME_ZERO) {
                // Since we've agreed on a scheme for which to exchange our key, we now send our NetworkKey to the
                // device
                logger.debug("NODE {}: Security scheme agreed.", this.getNode().getNodeId());
                // create the NetworkKey Packet
                SerialMessage networkKeyMessage = new SerialMessage(this.getNode().getNodeId(), SerialMessageClass.SendData, SerialMessageType.Request, SerialMessageClass.ApplicationCommandHandler, SECURITY_MESSAGE_PRIORITY);
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                baos.write((byte) this.getNode().getNodeId());
                baos.write(18);
                baos.write((byte) getCommandClass().getKey());
                baos.write(SECURITY_NETWORK_KEY_SET);
                try {
                    baos.write(realNetworkKey.getEncoded());
                    networkKeyMessage.setMessagePayload(baos.toByteArray());
                    // We can't set SECURITY_NETWORK_KEY_SET in inclusionStateTracker because we need to do a
                    // NONCE_GET before sending. So put this in our encrypt send queue
                    // and give inclusionStateTracker/ZWaveNodeStageAdvancer the NONCE_GET
                    queueMessageForEncapsulationAndTransmission(networkKeyMessage);
                    if (!inclusionStateTracker.verifyAndAdvanceState(SECURITY_NETWORK_KEY_SET)) {
                        return;
                    }
                    SerialMessage message = nonceGeneration.buildNonceGetIfNeeded();
                    // Since we are in init mode, message should always != null
                    if (message != null) {
                        // TODO: DB is this true?: logger.error("NODE {}: "+SECURE_INCLUSION_FAILED_MESSAGE+" In
                        // inclusion mode but buildNonceGetIfNeeded returned null, this may result in a deadlock");
                        // Let ZWaveNodeStageAdvancer come get the
                        inclusionStateTracker.setNextRequest(message);
                    // NONCE_GET
                    }
                } catch (IOException e) {
                    logger.error("NODE {}: IOException trying to write SECURITY_NETWORK_KEY_SET, aborted", e);
                }
            } else {
                // No common security scheme. This really shouldn't happen
                inclusionStateTracker.setErrorState("TODO: Security scheme " + schemes + " is not supported");
                logger.error("NODE {}: " + SECURE_INCLUSION_FAILED_MESSAGE + "  No common security scheme.  The device will continue as an unsecured node.  " + "Scheme requested was {}", this.getNode().getNodeId(), schemes);
            }
            return;
        case SECURITY_NETWORK_KEY_VERIFY:
            // Should be received during inclusion only
            if (!wasThisNodeJustIncluded() || inclusionStateTracker == null) {
                logger.error("NODE {}: Received SECURITY_NETWORK_KEY_VERIFY but we are not in inclusion mode! {}", serialMessage);
                return;
            }
            // Next step is to send SECURITY_COMMANDS_SUPPORTED_GET
            if (SEND_SECURITY_COMMANDS_SUPPORTED_GET_ON_STARTUP) {
                securePairingComplete = true;
            }
            SerialMessage supportedGetMessage = new SerialMessage(this.getNode().getNodeId(), SerialMessageClass.SendData, SerialMessageType.Request, SerialMessageClass.ApplicationCommandHandler, SECURITY_MESSAGE_PRIORITY);
            byte[] payload = { (byte) this.getNode().getNodeId(), 2, (byte) getCommandClass().getKey(), SECURITY_COMMANDS_SUPPORTED_GET };
            supportedGetMessage.setMessagePayload(payload);
            inclusionStateTracker.verifyAndAdvanceState(SECURITY_COMMANDS_SUPPORTED_GET);
            SerialMessage nonceGetMessage = nonceGeneration.buildNonceGetIfNeeded();
            // Since we are in init mode, message should always != null
            if (nonceGetMessage == null) {
                inclusionStateTracker.setErrorState(SECURE_INCLUSION_FAILED_MESSAGE + " In inclusion mode but buildNonceGetIfNeeded returned null," + " this may result in a deadlock");
                return;
            }
            // Let ZWaveNodeStageAdvancer come get it
            inclusionStateTracker.setNextRequest(nonceGetMessage);
            // We can't set SECURITY_COMMANDS_SUPPORTED_GET in inclusionStateTracker because we need to do a
            // NONCE_GET before sending. So put this in our encrypt send queue
            // and give inclusionStateTracker/ZWaveNodeStageAdvancer the NONCE_GET
            queueMessageForEncapsulationAndTransmission(supportedGetMessage);
            return;
        case SECURITY_COMMANDS_SUPPORTED_REPORT:
            processSecurityCommandsSupportedReport(serialMessage, offset);
            // This can be received during device inclusion or outside of it
            if (inclusionStateTracker != null) {
                // We're done with all of our NodeStage#SECURITY_REPORT stuff, set inclusionStateTracker to null
                inclusionStateTracker = null;
            }
            return;
        // SECURITY_NONCE_GET is handled by superclass
        case SECURITY_NONCE_GET:
        case // SECURITY_NONCE_GET is handled by superclass
        SECURITY_NONCE_REPORT:
            super.handleApplicationCommandRequest(serialMessage, offset, endpoint);
            return;
        // we shouldn't get a NetworkKeySet from a node if we are the controller as
        case SECURITY_NETWORK_KEY_SET:
        // SECURITY_MESSAGE_ENCAP should be caught and handled in {@link
        case SECURITY_MESSAGE_ENCAP:
        // ApplicationCommandMessageClass}
        case // SECURITY_MESSAGE_ENCAP_NONCE_GET should be caught and handled in
        SECURITY_MESSAGE_ENCAP_NONCE_GET:
            // {@link ApplicationCommandMessageClass}
            logger.info("NODE {}: Received {} from node but we shouldn't have gotten it.", this.getNode().getNodeId(), commandToString(command));
            return;
        default:
            logger.warn(String.format("NODE %s: Unsupported Command 0x%02X for command class %s (0x%02X) for message %s.", this.getNode().getNodeId(), command, this.getCommandClass().getLabel(), this.getCommandClass().getKey(), serialMessage));
    }
}
Also used : SecurityEncapsulatedSerialMessage(org.openhab.binding.zwave.internal.protocol.SecurityEncapsulatedSerialMessage) SerialMessage(org.openhab.binding.zwave.internal.protocol.SerialMessage) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ZWaveEndpoint(org.openhab.binding.zwave.internal.protocol.ZWaveEndpoint)

Example 7 with SerialMessage

use of org.openhab.binding.zwave.internal.protocol.SerialMessage in project openhab1-addons by openhab.

the class ZWaveSwitchAllCommandClass method allOnMessage.

/**
     * Create the All On message
     *
     * @return
     */
public SerialMessage allOnMessage() {
    logger.debug("NODE {}: Switch All - Creating All On message.", this.getNode().getNodeId());
    SerialMessage result = new SerialMessage(this.getNode().getNodeId(), SerialMessage.SerialMessageClass.SendData, SerialMessage.SerialMessageType.Request, SerialMessage.SerialMessageClass.SendData, SerialMessage.SerialMessagePriority.Set);
    byte[] newPayload = { (byte) this.getNode().getNodeId(), 2, (byte) this.getCommandClass().getKey(), (byte) SWITCH_ALL_ON };
    result.setMessagePayload(newPayload);
    return result;
}
Also used : SerialMessage(org.openhab.binding.zwave.internal.protocol.SerialMessage)

Example 8 with SerialMessage

use of org.openhab.binding.zwave.internal.protocol.SerialMessage in project openhab1-addons by openhab.

the class ZWaveSwitchAllCommandClass method allOffMessage.

/**
     * Create the All Off message
     *
     * @return
     */
public SerialMessage allOffMessage() {
    logger.debug("NODE {}: Switch All - Creating All Off message.", this.getNode().getNodeId());
    SerialMessage result = new SerialMessage(this.getNode().getNodeId(), SerialMessage.SerialMessageClass.SendData, SerialMessage.SerialMessageType.Request, SerialMessage.SerialMessageClass.SendData, SerialMessage.SerialMessagePriority.Set);
    byte[] newPayload = { (byte) this.getNode().getNodeId(), 2, (byte) this.getCommandClass().getKey(), (byte) SWITCH_ALL_OFF };
    result.setMessagePayload(newPayload);
    return result;
}
Also used : SerialMessage(org.openhab.binding.zwave.internal.protocol.SerialMessage)

Example 9 with SerialMessage

use of org.openhab.binding.zwave.internal.protocol.SerialMessage in project openhab1-addons by openhab.

the class ZWaveThermostatFanModeCommandClass method getValueMessage.

/**
     * {@inheritDoc}
     */
@Override
public SerialMessage getValueMessage() {
    if (isGetSupported == false) {
        logger.debug("NODE {}: Node doesn't support get requests", this.getNode().getNodeId());
        return null;
    }
    logger.debug("NODE {}: Creating new message for application command THERMOSTAT_FAN_MODE_GET", this.getNode().getNodeId());
    SerialMessage result = new SerialMessage(this.getNode().getNodeId(), SerialMessageClass.SendData, SerialMessageType.Request, SerialMessageClass.ApplicationCommandHandler, SerialMessagePriority.Get);
    byte[] payload = { (byte) this.getNode().getNodeId(), 2, (byte) getCommandClass().getKey(), THERMOSTAT_FAN_MODE_GET };
    result.setMessagePayload(payload);
    return result;
}
Also used : SerialMessage(org.openhab.binding.zwave.internal.protocol.SerialMessage)

Example 10 with SerialMessage

use of org.openhab.binding.zwave.internal.protocol.SerialMessage in project openhab1-addons by openhab.

the class ZWaveConfigurationCommandClass method setConfigMessage.

/**
     * Gets a SerialMessage with the CONFIGURATIONCMD_SET command
     *
     * @param parameter the parameter to set.
     * @return the serial message
     */
public SerialMessage setConfigMessage(ConfigurationParameter parameter) {
    if (parameter != null && parameter.getReadOnly() == true) {
        logger.debug("NODE {}: CONFIGURATIONCMD_SET ignored for parameter {} - parameter is read only", this.getNode().getNodeId(), parameter);
        return null;
    }
    logger.debug("NODE {}: Creating new message for application command CONFIGURATIONCMD_SET", this.getNode().getNodeId());
    SerialMessage result = new SerialMessage(this.getNode().getNodeId(), SerialMessageClass.SendData, SerialMessageType.Request, SerialMessageClass.SendData, SerialMessagePriority.Config);
    byte[] newPayload = new byte[parameter.getSize() + 6];
    newPayload[0] = (byte) this.getNode().getNodeId();
    newPayload[1] = (byte) (4 + parameter.getSize());
    newPayload[2] = (byte) getCommandClass().getKey();
    newPayload[3] = (byte) CONFIGURATIONCMD_SET;
    newPayload[4] = (byte) (parameter.getIndex() & 0xFF);
    newPayload[5] = (byte) (parameter.getSize() & 0xFF);
    for (int i = 0; i < parameter.getSize(); i++) {
        newPayload[6 + i] = (byte) (parameter.getValue() >> ((parameter.getSize() - i - 1) * 8) & 0xFF);
    }
    result.setMessagePayload(newPayload);
    return result;
}
Also used : SerialMessage(org.openhab.binding.zwave.internal.protocol.SerialMessage) ZWaveEndpoint(org.openhab.binding.zwave.internal.protocol.ZWaveEndpoint)

Aggregations

SerialMessage (org.openhab.binding.zwave.internal.protocol.SerialMessage)125 State (org.openhab.core.types.State)12 ZWaveEndpoint (org.openhab.binding.zwave.internal.protocol.ZWaveEndpoint)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 HashMap (java.util.HashMap)5 ZWaveNode (org.openhab.binding.zwave.internal.protocol.ZWaveNode)5 ZWaveWakeUpCommandClass (org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveWakeUpCommandClass)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 ConfigurationParameter (org.openhab.binding.zwave.internal.protocol.ConfigurationParameter)3 SecurityEncapsulatedSerialMessage (org.openhab.binding.zwave.internal.protocol.SecurityEncapsulatedSerialMessage)3 ZWaveCommandClass (org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveCommandClass)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 ZWaveDbConfigurationParameter (org.openhab.binding.zwave.internal.config.ZWaveDbConfigurationParameter)2 ZWaveProductDatabase (org.openhab.binding.zwave.internal.config.ZWaveProductDatabase)2 MultiLevelPercentCommandConverter (org.openhab.binding.zwave.internal.converter.command.MultiLevelPercentCommandConverter)2 ZWaveAssociationCommandClass (org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveAssociationCommandClass)2 ZWaveConfigurationCommandClass (org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveConfigurationCommandClass)2 ZWaveNetworkEvent (org.openhab.binding.zwave.internal.protocol.event.ZWaveNetworkEvent)2