Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dsmr] Made additional key for smarty meter an option #13359

Merged
merged 2 commits into from Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions bundles/org.openhab.binding.dsmr/README.md
Expand Up @@ -72,6 +72,7 @@ The configuration for the `smartyBridge` consists of the following parameters:
|---------------------|-------------------------------------------------------------------------------------------------------------|
| serialPort | The serial port where the P1-port is connected to (e.g. Linux: `/dev/ttyUSB1`, Windows: `COM2`) (mandatory) |
| decryptionKey | The meter specific decryption key (mandatory) |
| additionalKey | Additional key for meters that require a secondary key. Some meters in Austria require this |
| receivedTimeout | The time out period in which messages are expected to arrive, default is 120 seconds |


Expand Down
Expand Up @@ -46,6 +46,8 @@ public final class DSMRBindingConstants {

public static final String CONFIGURATION_DECRYPTION_KEY = "decryptionKey";
public static final String CONFIGURATION_DECRYPTION_KEY_EMPTY = "";
public static final String CONFIGURATION_ADDITIONAL_KEY = "additionalKey";
public static final String ADDITIONAL_KEY_DEFAULT = "3000112233445566778899AABBCCDDEEFF";

private DSMRBindingConstants() {
// Constants class
Expand Down
Expand Up @@ -12,13 +12,16 @@
*/
package org.openhab.binding.dsmr.internal.device;

import org.openhab.binding.dsmr.internal.DSMRBindingConstants;

/**
* Class describing the DSMR bridge user configuration
*
* @author M. Volaart - Initial contribution
* @author Hilbrand Bouwkamp - added receivedTimeout configuration
*/
public class DSMRDeviceConfiguration {

/**
* Serial port name
*/
Expand All @@ -45,10 +48,15 @@ public class DSMRDeviceConfiguration {
public String stopbits;

/**
* The Luxembourgish smart meter decryption key
* The Luxembourgish/Austria smart meter decryption key
*/
public String decryptionKey;

/**
* Austria smart meter additional decryption key
*/
public String additionalKey = DSMRBindingConstants.ADDITIONAL_KEY_DEFAULT;

/**
* When no message was received after the configured number of seconds action will be taken.
*/
Expand All @@ -66,6 +74,6 @@ public boolean isSerialFixedSettings() {
public String toString() {
return "DSMRDeviceConfiguration [serialPort=" + serialPort + ", Baudrate=" + baudrate + ", Databits=" + databits
+ ", Parity=" + parity + ", Stopbits=" + stopbits + ", decryptionKey=" + decryptionKey
+ ", receivedTimeout=" + receivedTimeout + "]";
+ ", additionalKey=" + additionalKey + ", receivedTimeout=" + receivedTimeout + "]";
}
}
Expand Up @@ -55,27 +55,28 @@ public DSMRTelegramListener() {
* Constructs {@link DSMRTelegramListener} with a Smarty decryptor to first decrypt incoming messages.
*
* @param decryptionKey Smarty decryption key
* @param additionalKey Additional optional descryption key
*/
public DSMRTelegramListener(String decryptionKey) {
parser = new SmartyDecrypter(new P1TelegramParser(this), this, decryptionKey);
public DSMRTelegramListener(final String decryptionKey, final String additionalKey) {
parser = new SmartyDecrypter(new P1TelegramParser(this), this, decryptionKey, additionalKey);
}

/**
* Set the DSMR event listener.
*
* @param eventListener the listener to set
*/
public void setDsmrEventListener(DSMREventListener eventListener) {
public void setDsmrEventListener(final DSMREventListener eventListener) {
this.dsmrEventListener = eventListener;
}

@Override
public void handleData(byte[] data, int length) {
public void handleData(final byte[] data, final int length) {
parser.parse(data, length);
}

@Override
public void handleErrorEvent(DSMRConnectorErrorEvent portEvent) {
public void handleErrorEvent(final DSMRConnectorErrorEvent portEvent) {
dsmrEventListener.handleErrorEvent(portEvent);
parser.reset();
}
Expand All @@ -86,7 +87,7 @@ public void handleErrorEvent(DSMRConnectorErrorEvent portEvent) {
* @param telegram the received telegram.
*/
@Override
public void telegramReceived(P1Telegram telegram) {
public void telegramReceived(final P1Telegram telegram) {
final TelegramState telegramState = telegram.getTelegramState();
final List<CosemObject> cosemObjects = telegram.getCosemObjects();

Expand All @@ -106,7 +107,7 @@ public void telegramReceived(P1Telegram telegram) {
/**
* @param lenientMode the lenientMode to set
*/
public void setLenientMode(boolean lenientMode) {
public void setLenientMode(final boolean lenientMode) {
parser.setLenientMode(lenientMode);
}
}
Expand Up @@ -27,6 +27,7 @@

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.dsmr.internal.DSMRBindingConstants;
import org.openhab.binding.dsmr.internal.device.p1telegram.P1Telegram;
import org.openhab.binding.dsmr.internal.device.p1telegram.P1Telegram.TelegramState;
import org.openhab.binding.dsmr.internal.device.p1telegram.P1TelegramListener;
Expand Down Expand Up @@ -60,12 +61,11 @@ private enum State {
private static final byte SEPARATOR_82 = (byte) 0x82;
private static final byte SEPARATOR_30 = 0x30;
private static final int ADD_LENGTH = 17;
private static final String ADD = "3000112233445566778899AABBCCDDEEFF";
private static final byte[] ADD_DECODED = HexUtils.hexToBytes(ADD);
private static final int IV_BUFFER_LENGTH = 40;
private static final int GCM_TAG_LENGTH = 12;
private static final int GCM_BITS = GCM_TAG_LENGTH * Byte.SIZE;
private static final int MESSAGES_BUFFER_SIZE = 4096;
private static final String ADDITIONAL_ADD_PREFIX = "30";

private final Logger logger = LoggerFactory.getLogger(SmartyDecrypter.class);
private final ByteBuffer iv = ByteBuffer.allocate(IV_BUFFER_LENGTH);
Expand All @@ -80,19 +80,23 @@ private enum State {
private int dataLength;
private boolean lenientMode;
private final P1TelegramListener telegramListener;
private final byte[] addKey;

/**
* Constructor.
*
* @param parser parser of the Cosem messages
* @param telegramListener
* @param decryptionKey The key to decrypt the messages
* @param additionalKey Additional optional key to decrypt the message
*/
public SmartyDecrypter(final TelegramParser parser, final P1TelegramListener telegramListener,
final String decryptionKey) {
final String decryptionKey, final String additionalKey) {
this.parser = parser;
this.telegramListener = telegramListener;
secretKeySpec = decryptionKey.isEmpty() ? null : new SecretKeySpec(HexUtils.hexToBytes(decryptionKey), "AES");
addKey = HexUtils.hexToBytes(additionalKey.isBlank() ? DSMRBindingConstants.ADDITIONAL_KEY_DEFAULT
: ((additionalKey.length() == 32 ? (ADDITIONAL_ADD_PREFIX) : "") + additionalKey));
}

@Override
Expand Down Expand Up @@ -209,7 +213,7 @@ private void processCompleted() {
final Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec,
new GCMParameterSpec(GCM_BITS, iv.array(), 0, ivLength));
cipher.updateAAD(ADD_DECODED);
cipher.updateAAD(addKey);
return cipher.doFinal(cipherText.array(), 0, cipherText.position());
}
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException
Expand Down
Expand Up @@ -12,7 +12,13 @@
*/
package org.openhab.binding.dsmr.internal.discovery;

import static org.openhab.binding.dsmr.internal.DSMRBindingConstants.*;
import static org.openhab.binding.dsmr.internal.DSMRBindingConstants.CONFIGURATION_ADDITIONAL_KEY;
import static org.openhab.binding.dsmr.internal.DSMRBindingConstants.CONFIGURATION_DECRYPTION_KEY;
import static org.openhab.binding.dsmr.internal.DSMRBindingConstants.CONFIGURATION_DECRYPTION_KEY_EMPTY;
import static org.openhab.binding.dsmr.internal.DSMRBindingConstants.CONFIGURATION_SERIAL_PORT;
import static org.openhab.binding.dsmr.internal.DSMRBindingConstants.DSMR_PORT_NAME;
import static org.openhab.binding.dsmr.internal.DSMRBindingConstants.THING_TYPE_DSMR_BRIDGE;
import static org.openhab.binding.dsmr.internal.DSMRBindingConstants.THING_TYPE_SMARTY_BRIDGE;

import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -109,7 +115,7 @@ public DSMRBridgeDiscoveryService(final @Reference TranslationProvider i18nProvi
protected void startScan() {
logger.debug("Started DSMR discovery scan");
scanning = true;
Stream<SerialPortIdentifier> portEnum = serialPortManager.getIdentifiers();
final Stream<SerialPortIdentifier> portEnum = serialPortManager.getIdentifiers();

// Traverse each available serial port
portEnum.forEach(portIdentifier -> {
Expand All @@ -126,7 +132,8 @@ protected void startScan() {
} else {
logger.debug("Start discovery on serial port: {}", currentScannedPortName);
//
final DSMRTelegramListener telegramListener = new DSMRTelegramListener("");
final DSMRTelegramListener telegramListener = new DSMRTelegramListener("",
CONFIGURATION_ADDITIONAL_KEY);
final DSMRSerialAutoDevice device = new DSMRSerialAutoDevice(serialPortManager,
portIdentifier.getName(), this, telegramListener, scheduler,
BAUDRATE_SWITCH_TIMEOUT_SECONDS);
Expand Down Expand Up @@ -166,8 +173,8 @@ private void stopSerialPortScan() {
* @param telegram the received telegram
*/
@Override
public void handleTelegramReceived(P1Telegram telegram) {
List<CosemObject> cosemObjects = telegram.getCosemObjects();
public void handleTelegramReceived(final P1Telegram telegram) {
final List<CosemObject> cosemObjects = telegram.getCosemObjects();

if (logger.isDebugEnabled()) {
logger.debug("[{}] Received {} cosemObjects", currentScannedPortName, cosemObjects.size());
Expand All @@ -176,7 +183,7 @@ public void handleTelegramReceived(P1Telegram telegram) {
bridgeDiscovered(THING_TYPE_SMARTY_BRIDGE);
stopSerialPortScan();
} else if (!cosemObjects.isEmpty()) {
ThingUID bridgeThingUID = bridgeDiscovered(THING_TYPE_DSMR_BRIDGE);
final ThingUID bridgeThingUID = bridgeDiscovered(THING_TYPE_DSMR_BRIDGE);
meterDetector.detectMeters(telegram).getKey().forEach(m -> meterDiscovered(m, bridgeThingUID));
stopSerialPortScan();
}
Expand All @@ -187,19 +194,21 @@ public void handleTelegramReceived(P1Telegram telegram) {
*
* @return The {@link ThingUID} of the newly created bridge
*/
private ThingUID bridgeDiscovered(ThingTypeUID bridgeThingTypeUID) {
ThingUID thingUID = new ThingUID(bridgeThingTypeUID, Integer.toHexString(currentScannedPortName.hashCode()));
private ThingUID bridgeDiscovered(final ThingTypeUID bridgeThingTypeUID) {
final ThingUID thingUID = new ThingUID(bridgeThingTypeUID,
Integer.toHexString(currentScannedPortName.hashCode()));
final boolean smarty = THING_TYPE_SMARTY_BRIDGE.equals(bridgeThingTypeUID);
final String label = String.format("@text/thing-type.dsmr.%s.label", smarty ? "smartyBridge" : "dsmrBridge");

// Construct the configuration for this meter
Map<String, Object> properties = new HashMap<>();
final Map<String, Object> properties = new HashMap<>();
properties.put(CONFIGURATION_SERIAL_PORT, currentScannedPortName);
if (smarty) {
properties.put(CONFIGURATION_DECRYPTION_KEY, CONFIGURATION_DECRYPTION_KEY_EMPTY);
properties.put(CONFIGURATION_ADDITIONAL_KEY, CONFIGURATION_ADDITIONAL_KEY);
}
DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withThingType(bridgeThingTypeUID)
.withProperties(properties).withLabel(label).build();
final DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID)
.withThingType(bridgeThingTypeUID).withProperties(properties).withLabel(label).build();

logger.debug("[{}] discovery result:{}", currentScannedPortName, discoveryResult);

Expand All @@ -208,7 +217,7 @@ private ThingUID bridgeDiscovered(ThingTypeUID bridgeThingTypeUID) {
}

@Override
public void handleErrorEvent(DSMRConnectorErrorEvent portEvent) {
public void handleErrorEvent(final DSMRConnectorErrorEvent portEvent) {
logger.debug("[{}] Error on port during discovery: {}", currentScannedPortName, portEvent);
stopSerialPortScan();
}
Expand Down
Expand Up @@ -111,7 +111,7 @@ public class DSMRBridgeHandler extends BaseBridgeHandler implements DSMREventLis
* @param bridge the Bridge ThingType
* @param serialPortManager The Serial port manager
*/
public DSMRBridgeHandler(Bridge bridge, SerialPortManager serialPortManager) {
public DSMRBridgeHandler(final Bridge bridge, final SerialPortManager serialPortManager) {
super(bridge);
this.serialPortManager = serialPortManager;
smartyMeter = THING_TYPE_SMARTY_BRIDGE.equals(bridge.getThingTypeUID());
Expand All @@ -129,7 +129,7 @@ public Collection<Class<? extends ThingHandlerService>> getServices() {
* @param command the {@link Command}
*/
@Override
public void handleCommand(ChannelUID channelUID, Command command) {
public void handleCommand(final ChannelUID channelUID, final Command command) {
// DSMRBridgeHandler does not support commands
}

Expand Down Expand Up @@ -170,12 +170,13 @@ public void initialize() {
* @param deviceConfig device configuration
* @return Specific {@link DSMRDevice} instance
*/
private DSMRDevice createDevice(DSMRDeviceConfiguration deviceConfig) {
private DSMRDevice createDevice(final DSMRDeviceConfiguration deviceConfig) {
final DSMRDevice dsmrDevice;

if (smartyMeter) {
dsmrDevice = new DSMRFixedConfigDevice(serialPortManager, deviceConfig.serialPort,
DSMRSerialSettings.HIGH_SPEED_SETTINGS, this, new DSMRTelegramListener(deviceConfig.decryptionKey));
DSMRSerialSettings.HIGH_SPEED_SETTINGS, this,
new DSMRTelegramListener(deviceConfig.decryptionKey, deviceConfig.additionalKey));
} else {
final DSMRTelegramListener telegramListener = new DSMRTelegramListener();

Expand All @@ -196,7 +197,7 @@ private DSMRDevice createDevice(DSMRDeviceConfiguration deviceConfig) {
* @param meterListener the meter discovery listener to add
* @return true if listener is added, false otherwise
*/
public boolean registerDSMRMeterListener(P1TelegramListener meterListener) {
public boolean registerDSMRMeterListener(final P1TelegramListener meterListener) {
logger.trace("Register DSMRMeterListener");
return meterListeners.add(meterListener);
}
Expand All @@ -207,7 +208,7 @@ public boolean registerDSMRMeterListener(P1TelegramListener meterListener) {
* @param meterListener the meter discovery listener to remove
* @return true is listener is removed, false otherwise
*/
public boolean unregisterDSMRMeterListener(P1TelegramListener meterListener) {
public boolean unregisterDSMRMeterListener(final P1TelegramListener meterListener) {
logger.trace("Unregister DSMRMeterListener");
return meterListeners.remove(meterListener);
}
Expand Down Expand Up @@ -247,7 +248,7 @@ private void resetLastReceivedState() {
}

@Override
public synchronized void handleTelegramReceived(P1Telegram telegram) {
public synchronized void handleTelegramReceived(final P1Telegram telegram) {
if (telegram.getCosemObjects().isEmpty()) {
logger.debug("Parsing worked but something went wrong, so there were no CosemObjects:{}",
telegram.getTelegramState().stateDetails);
Expand All @@ -259,7 +260,7 @@ public synchronized void handleTelegramReceived(P1Telegram telegram) {
}

@Override
public void handleErrorEvent(DSMRConnectorErrorEvent portEvent) {
public void handleErrorEvent(final DSMRConnectorErrorEvent portEvent) {
if (portEvent != DSMRConnectorErrorEvent.READ_ERROR) {
deviceOffline(ThingStatusDetail.CONFIGURATION_ERROR, portEvent.getEventDetails());
}
Expand All @@ -270,7 +271,7 @@ public void handleErrorEvent(DSMRConnectorErrorEvent portEvent) {
*
* @param telegram received meter values.
*/
private void meterValueReceived(P1Telegram telegram) {
private void meterValueReceived(final P1Telegram telegram) {
if (isInitialized() && getThing().getStatus() != ThingStatus.ONLINE) {
updateStatus(ThingStatus.ONLINE);
}
Expand Down Expand Up @@ -302,7 +303,7 @@ public void dispose() {
/**
* @param lenientMode the lenientMode to set
*/
public void setLenientMode(boolean lenientMode) {
public void setLenientMode(final boolean lenientMode) {
logger.trace("SetLenientMode: {}", lenientMode);
if (dsmrDevice != null) {
dsmrDevice.setLenientMode(lenientMode);
Expand All @@ -315,7 +316,7 @@ public void setLenientMode(boolean lenientMode) {
* @param status off line status
* @param details off line detailed message
*/
private void deviceOffline(ThingStatusDetail status, String details) {
private void deviceOffline(final ThingStatusDetail status, final String details) {
updateStatus(ThingStatus.OFFLINE, status, details);
}
}
Expand Up @@ -73,8 +73,12 @@
</parameter>
<parameter name="decryptionKey" type="text" required="true">
<label>Decryption Key</label>
<description>The Luxembourgian Smart meter decryption key. Ask for your energy grid operator for your Smart meter P1
key.</description>
<description>Smart meter decryption key. Ask your energy grid operator for your smart meter P1 key.</description>
</parameter>
<parameter name="additionalKey" type="text">
<label>Additional Decryption Key</label>
<description>Additional decryption key. Ask your energy grid operator for your smart meter P1 key.</description>
<default>3000112233445566778899AABBCCDDEEFF</default>
</parameter>
<parameter name="receivedTimeout" type="integer" min="1" unit="s">
<default>30</default>
Expand Down
Expand Up @@ -118,8 +118,10 @@ thing-type.config.dsmr.meterdescriptor.channel.label = Channel
thing-type.config.dsmr.meterdescriptor.channel.description = The DSMR-device channel for this meter (M-Bus channel). The binding will auto detect this value. In normal situations it is not necessary to adapt this value. If the auto detection failed or if physical changes are made to the meter setup (changed water, gas, heating) meters it can be necessary to update the M-Bus channel.
thing-type.config.dsmr.meterdescriptor.refresh.label = Refresh
thing-type.config.dsmr.meterdescriptor.refresh.description = The time interval the data is refreshed in seconds
thing-type.config.dsmr.smartybridgesettings.additionalKey.label = Additional Decryption Key
thing-type.config.dsmr.smartybridgesettings.additionalKey.description = Additional decryption key. Ask your energy grid operator for your smart meter P1 key.
thing-type.config.dsmr.smartybridgesettings.decryptionKey.label = Decryption Key
thing-type.config.dsmr.smartybridgesettings.decryptionKey.description = The Luxembourgian Smart meter decryption key. Ask for your energy grid operator for your Smart meter P1 key.
thing-type.config.dsmr.smartybridgesettings.decryptionKey.description = Smart meter decryption key. Ask your energy grid operator for your smart meter P1 key.
thing-type.config.dsmr.smartybridgesettings.receivedTimeout.label = Received Timeout
thing-type.config.dsmr.smartybridgesettings.receivedTimeout.description = The time period within results are expected in seconds
thing-type.config.dsmr.smartybridgesettings.serialPort.label = Serial Port
Expand Down