Revert californium commits in shelly and tradfri (#20750)

This commit is contained in:
lsiepel
2026-05-17 22:19:24 +02:00
committed by GitHub
parent 3ab40ce9c0
commit 191aca20a7
3 changed files with 42 additions and 87 deletions
@@ -33,9 +33,6 @@ import org.eclipse.californium.core.coap.Option;
import org.eclipse.californium.core.coap.OptionNumberRegistry;
import org.eclipse.californium.core.coap.Request;
import org.eclipse.californium.core.coap.Response;
import org.eclipse.californium.core.coap.option.IntegerOption;
import org.eclipse.californium.core.coap.option.OpaqueOption;
import org.eclipse.californium.core.coap.option.StringOption;
import org.eclipse.californium.core.network.Endpoint;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
@@ -185,16 +182,14 @@ public class Shelly1CoapHandler implements Shelly1CoapListener {
// We can't identify device by IP, so we need to check the CoAP header's Global Device ID
for (Option opt : options) {
if (opt.getNumber() == COIOT_OPTION_GLOBAL_DEVID) {
if (opt instanceof StringOption strOpt) {
String devid = strOpt.getStringValue();
if (devid.contains("#") && profile.device.mac != null) {
// Format: <device type>#<mac address>#<coap version>
String macid = substringBetween(devid, "#", "#");
if (getString(profile.device.mac).toUpperCase(Locale.ROOT)
.contains(macid.toUpperCase(Locale.ROOT))) {
match = true;
break;
}
String devid = opt.getStringValue();
if (devid.contains("#") && profile.device.mac != null) {
// Format: <device type>#<mac address>#<coap version>
String macid = substringBetween(devid, "#", "#");
if (getString(profile.device.mac).toUpperCase(Locale.ROOT)
.contains(macid.toUpperCase(Locale.ROOT))) {
match = true;
break;
}
}
}
@@ -231,67 +226,52 @@ public class Shelly1CoapHandler implements Shelly1CoapListener {
for (Option opt : options) {
switch (opt.getNumber()) {
case OptionNumberRegistry.URI_PATH:
if (opt instanceof StringOption strOpt) {
uri = COLOIT_URI_BASE + strOpt.getStringValue();
} else {
logger.debug("{}: URI_PATH option is not a StringOption, type: {}", thingName,
opt.getClass().getSimpleName());
}
uri = COLOIT_URI_BASE + opt.getStringValue();
break;
case OptionNumberRegistry.URI_HOST: // ignore
break;
case OptionNumberRegistry.CONTENT_FORMAT: // ignore
break;
case COIOT_OPTION_GLOBAL_DEVID:
if (opt instanceof StringOption strOpt) {
devId = strOpt.getStringValue();
String sVersion = substringAfterLast(devId, "#");
int iVersion;
try {
iVersion = Integer.parseInt(sVersion);
} catch (NumberFormatException e) {
logger.debug("{}: Unable to parse version in CoIoT message: {}", thingName, devId);
thingHandler.incProtErrors();
devId = opt.getStringValue();
String sVersion = substringAfterLast(devId, "#");
int iVersion;
try {
iVersion = Integer.parseInt(sVersion);
} catch (NumberFormatException e) {
logger.debug("{}: Unable to parse version in CoIoT message: {}", thingName, devId);
thingHandler.incProtErrors();
return;
}
if (coiotBound && coiotVers != iVersion) {
logger.debug("{}: CoIoT version has changed from {} to {}, maybe the firmware was upgraded",
thingName, coiotVers, iVersion);
thingHandler.reinitializeThing();
coiotBound = false;
}
if (!coiotBound) {
thingHandler.updateProperties(PROPERTY_COAP_VERSION, sVersion);
logger.debug("{}: CoIoT Version {} detected", thingName, iVersion);
if (iVersion == COIOT_VERSION_1) {
coiot = new Shelly1CoIoTVersion1(thingName, thingHandler, blkMap, sensorMap);
} else if (iVersion == COIOT_VERSION_2) {
coiot = new Shelly1CoIoTVersion2(thingName, thingHandler, blkMap, sensorMap);
} else {
logger.warn("{}: Unsupported CoAP version detected: {}", thingName, sVersion);
return;
}
if (coiotBound && coiotVers != iVersion) {
logger.debug("{}: CoIoT version has changed from {} to {}, maybe the firmware was upgraded",
thingName, coiotVers, iVersion);
thingHandler.reinitializeThing();
coiotBound = false;
}
if (!coiotBound) {
thingHandler.updateProperties(PROPERTY_COAP_VERSION, sVersion);
logger.debug("{}: CoIoT Version {} detected", thingName, iVersion);
if (iVersion == COIOT_VERSION_1) {
coiot = new Shelly1CoIoTVersion1(thingName, thingHandler, blkMap, sensorMap);
} else if (iVersion == COIOT_VERSION_2) {
coiot = new Shelly1CoIoTVersion2(thingName, thingHandler, blkMap, sensorMap);
} else {
logger.warn("{}: Unsupported CoAP version detected: {}", thingName, sVersion);
return;
}
coiotVers = iVersion;
coiotBound = true;
}
} else {
logger.debug("{}: COIOT_OPTION_GLOBAL_DEVID option is not a StringOption, type: {}", thingName,
opt.getClass().getSimpleName());
coiotVers = iVersion;
coiotBound = true;
}
break;
case COIOT_OPTION_STATUS_VALIDITY:
break;
case COIOT_OPTION_STATUS_SERIAL:
if (opt instanceof IntegerOption intOpt) {
serial = intOpt.getIntegerValue();
} else {
logger.debug("{}: COIOT_OPTION_STATUS_SERIAL option is not an IntegerOption, type: {}",
thingName, opt.getClass().getSimpleName());
}
serial = opt.getIntegerValue();
break;
default:
logger.debug("{} ({}): CoAP option {} with value {} skipped", thingName, devId, opt.getNumber(),
opt instanceof OpaqueOption opOpt ? opOpt.getValue() : opt);
opt.getValue());
}
}
@@ -12,9 +12,6 @@
*/
package org.openhab.binding.shelly.internal.api1;
import static org.openhab.binding.shelly.internal.api1.Shelly1CoapJSonDTO.COIOT_OPTION_GLOBAL_DEVID;
import static org.openhab.binding.shelly.internal.api1.Shelly1CoapJSonDTO.COIOT_OPTION_STATUS_SERIAL;
import static org.openhab.binding.shelly.internal.api1.Shelly1CoapJSonDTO.COIOT_OPTION_STATUS_VALIDITY;
import static org.openhab.binding.shelly.internal.api1.Shelly1CoapJSonDTO.COIOT_PORT;
import java.net.InetAddress;
@@ -30,11 +27,6 @@ import org.eclipse.californium.core.coap.CoAP.Code;
import org.eclipse.californium.core.coap.CoAP.ResponseCode;
import org.eclipse.californium.core.coap.Request;
import org.eclipse.californium.core.coap.Response;
import org.eclipse.californium.core.coap.option.IntegerOption;
import org.eclipse.californium.core.coap.option.MapBasedOptionRegistry;
import org.eclipse.californium.core.coap.option.OptionRegistry;
import org.eclipse.californium.core.coap.option.StandardOptionRegistry;
import org.eclipse.californium.core.coap.option.StringOption;
import org.eclipse.californium.core.network.CoapEndpoint;
import org.eclipse.californium.core.network.Exchange;
import org.eclipse.californium.elements.UdpMulticastConnector;
@@ -52,27 +44,9 @@ import org.slf4j.LoggerFactory;
*/
@NonNullByDefault
public class Shelly1CoapServer {
// Define custom CoIoT option definitions for Shelly devices
private static final StringOption.Definition COIOT_GLOBAL_DEVID_DEF = new StringOption.Definition(
COIOT_OPTION_GLOBAL_DEVID, "Shelly-GlobalDevId", true, 0, 255);
private static final IntegerOption.Definition COIOT_STATUS_VALIDITY_DEF = new IntegerOption.Definition(
COIOT_OPTION_STATUS_VALIDITY, "Shelly-StatusValidity", true, 0, 4);
private static final IntegerOption.Definition COIOT_STATUS_SERIAL_DEF = new IntegerOption.Definition(
COIOT_OPTION_STATUS_SERIAL, "Shelly-StatusSerial", true, 0, 4);
static {
// register configurations before Configuration.getStandard() is used
DtlsConfig.register();
// register custom CoIoT options for Shelly devices
// Merge with existing default registry to preserve options from other add-ons (e.g., Tradfri)
OptionRegistry currentRegistry = StandardOptionRegistry.getDefaultOptionRegistry();
OptionRegistry mergedRegistry = MapBasedOptionRegistry.builder() //
.add(currentRegistry) //
.add(COIOT_GLOBAL_DEVID_DEF) //
.add(COIOT_STATUS_VALIDITY_DEF) //
.add(COIOT_STATUS_SERIAL_DEF) //
.build();
StandardOptionRegistry.setDefaultOptionRegistry(mergedRegistry);
}
private final Logger logger = LoggerFactory.getLogger(Shelly1CoapServer.class);
@@ -32,7 +32,7 @@ import org.eclipse.californium.elements.exception.ConnectorException;
import org.eclipse.californium.scandium.DTLSConnector;
import org.eclipse.californium.scandium.config.DtlsConfig;
import org.eclipse.californium.scandium.config.DtlsConnectorConfig;
import org.eclipse.californium.scandium.dtls.pskstore.SinglePskStore;
import org.eclipse.californium.scandium.dtls.pskstore.AdvancedSinglePskStore;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.tradfri.internal.CoapCallback;
@@ -162,7 +162,8 @@ public class TradfriGatewayHandler extends BaseBridgeHandler implements CoapCall
org.eclipse.californium.elements.config.Configuration.getStandard()
.set(DtlsConfig.DTLS_MAX_CONNECTIONS, 100)
.set(DtlsConfig.DTLS_STALE_CONNECTION_THRESHOLD, 60, TimeUnit.SECONDS));
builder.setPskStore(new SinglePskStore(configuration.identity, configuration.preSharedKey.getBytes()));
builder.setAdvancedPskStore(
new AdvancedSinglePskStore(configuration.identity, configuration.preSharedKey.getBytes()));
dtlsConnector = new DTLSConnector(builder.build());
endPoint = new CoapEndpoint.Builder().setConnector(dtlsConnector).build();
deviceClient.setEndpoint(endPoint);
@@ -191,7 +192,7 @@ public class TradfriGatewayHandler extends BaseBridgeHandler implements CoapCall
try {
DtlsConnectorConfig.Builder builder = new DtlsConnectorConfig.Builder(
org.eclipse.californium.elements.config.Configuration.getStandard());
builder.setPskStore(new SinglePskStore("Client_identity", configuration.code.getBytes()));
builder.setAdvancedPskStore(new AdvancedSinglePskStore("Client_identity", configuration.code.getBytes()));
DTLSConnector dtlsConnector = new DTLSConnector(builder.build());
CoapEndpoint.Builder authEndpointBuilder = new CoapEndpoint.Builder();