From cca33259f0737061a97d49f2b676116b7ad3b3d8 Mon Sep 17 00:00:00 2001 From: Simon Bernard Date: Thu, 26 Oct 2023 17:54:03 +0200 Subject: [PATCH] Add coap+tcp support based on java-coap to client-demo. --- leshan-client-demo/pom.xml | 4 ++++ .../leshan/client/demo/LeshanClientDemo.java | 19 +++++++++++++++---- .../eclipse/leshan/client/demo/MyDevice.java | 4 +++- .../client/demo/cli/LeshanClientDemoCLI.java | 11 +++++------ 4 files changed, 27 insertions(+), 11 deletions(-) diff --git a/leshan-client-demo/pom.xml b/leshan-client-demo/pom.xml index b7eec3c818..f92f544c78 100644 --- a/leshan-client-demo/pom.xml +++ b/leshan-client-demo/pom.xml @@ -37,6 +37,10 @@ Contributors: org.eclipse.leshan leshan-tl-javacoap-client + + org.eclipse.leshan + leshan-tl-javacoap-client-coaptcp + org.eclipse.leshan leshan-core-demo diff --git a/leshan-client-demo/src/main/java/org/eclipse/leshan/client/demo/LeshanClientDemo.java b/leshan-client-demo/src/main/java/org/eclipse/leshan/client/demo/LeshanClientDemo.java index 2fb1f7119b..158114f91f 100644 --- a/leshan-client-demo/src/main/java/org/eclipse/leshan/client/demo/LeshanClientDemo.java +++ b/leshan-client-demo/src/main/java/org/eclipse/leshan/client/demo/LeshanClientDemo.java @@ -34,6 +34,7 @@ import java.io.File; import java.io.PrintWriter; import java.util.ArrayList; +import java.util.EnumSet; import java.util.List; import org.eclipse.californium.elements.config.Configuration; @@ -64,6 +65,7 @@ import org.eclipse.leshan.core.demo.LwM2mDemoConstant; import org.eclipse.leshan.core.demo.cli.ShortErrorMessageHandler; import org.eclipse.leshan.core.demo.cli.interactive.InteractiveCLI; +import org.eclipse.leshan.core.endpoint.Protocol; import org.eclipse.leshan.core.model.LwM2mModelRepository; import org.eclipse.leshan.core.model.ObjectLoader; import org.eclipse.leshan.core.model.ObjectModel; @@ -71,9 +73,11 @@ import org.eclipse.leshan.core.node.codec.DefaultLwM2mDecoder; import org.eclipse.leshan.core.node.codec.DefaultLwM2mEncoder; import org.eclipse.leshan.core.node.codec.text.LwM2mNodeTextDecoder; +import org.eclipse.leshan.core.request.BindingMode; import org.eclipse.leshan.core.request.BootstrapWriteRequest; import org.eclipse.leshan.core.request.ContentFormat; import org.eclipse.leshan.core.response.BootstrapWriteResponse; +import org.eclipse.leshan.transport.javacoap.client.coaptcp.endpoint.JavaCoapTcpClientEndpointsProvider; import org.eclipse.leshan.transport.javacoap.client.endpoint.JavaCoapClientEndpointsProvider; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -201,31 +205,37 @@ public static LeshanClient createClient(LeshanClientDemoCLI cli, LwM2mModelRepos initializer.setClassForObject(SERVER, Server.class); } } else { + BindingMode serverBindingMode = BindingMode.fromProtocol(Protocol.fromUri(cli.main.url)); + if (cli.identity.isPSK()) { // TODO OSCORE support OSCORE with DTLS/PSK initializer.setInstancesForObject(SECURITY, psk(cli.main.url, 123, cli.identity.getPsk().identity.getBytes(), cli.identity.getPsk().sharekey.getBytes())); - initializer.setInstancesForObject(SERVER, new Server(123, cli.main.lifetimeInSec)); + initializer.setInstancesForObject(SERVER, new Server(123, cli.main.lifetimeInSec, + EnumSet.of(serverBindingMode), false, serverBindingMode)); } else if (cli.identity.isRPK()) { // TODO OSCORE support OSCORE with DTLS/RPK initializer.setInstancesForObject(SECURITY, rpk(cli.main.url, 123, cli.identity.getRPK().cpubk.getEncoded(), cli.identity.getRPK().cprik.getEncoded(), cli.identity.getRPK().spubk.getEncoded())); - initializer.setInstancesForObject(SERVER, new Server(123, cli.main.lifetimeInSec)); + initializer.setInstancesForObject(SERVER, new Server(123, cli.main.lifetimeInSec, + EnumSet.of(serverBindingMode), false, serverBindingMode)); } else if (cli.identity.isx509()) { // TODO OSCORE support OSCORE with DTLS/X509 initializer.setInstancesForObject(SECURITY, x509(cli.main.url, 123, cli.identity.getX509().ccert.getEncoded(), cli.identity.getX509().cprik.getEncoded(), cli.identity.getX509().scert.getEncoded(), cli.identity.getX509().certUsage.code)); - initializer.setInstancesForObject(SERVER, new Server(123, cli.main.lifetimeInSec)); + initializer.setInstancesForObject(SERVER, new Server(123, cli.main.lifetimeInSec, + EnumSet.of(serverBindingMode), false, serverBindingMode)); } else { if (oscoreObjectInstanceId != null) { initializer.setInstancesForObject(SECURITY, oscoreOnly(cli.main.url, 123, oscoreObjectInstanceId)); } else { initializer.setInstancesForObject(SECURITY, noSec(cli.main.url, 123)); } - initializer.setInstancesForObject(SERVER, new Server(123, cli.main.lifetimeInSec)); + initializer.setInstancesForObject(SERVER, new Server(123, cli.main.lifetimeInSec, + EnumSet.of(serverBindingMode), false, serverBindingMode)); } } initializer.setInstancesForObject(DEVICE, new MyDevice()); @@ -308,6 +318,7 @@ protected DtlsConnectorConfig.Builder createRootDtlsConnectorConfigBuilder( if (cli.main.useJavaCoap) { endpointsProvider.add(new JavaCoapClientEndpointsProvider()); } + endpointsProvider.add(new JavaCoapTcpClientEndpointsProvider()); // Create client LeshanClientBuilder builder = new LeshanClientBuilder(cli.main.endpoint); diff --git a/leshan-client-demo/src/main/java/org/eclipse/leshan/client/demo/MyDevice.java b/leshan-client-demo/src/main/java/org/eclipse/leshan/client/demo/MyDevice.java index e0d7e5abc9..26b9b8e493 100644 --- a/leshan-client-demo/src/main/java/org/eclipse/leshan/client/demo/MyDevice.java +++ b/leshan-client-demo/src/main/java/org/eclipse/leshan/client/demo/MyDevice.java @@ -18,6 +18,7 @@ import java.util.Arrays; import java.util.Calendar; import java.util.Date; +import java.util.EnumSet; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -32,6 +33,7 @@ import org.eclipse.leshan.core.model.ObjectModel; import org.eclipse.leshan.core.model.ResourceModel.Type; import org.eclipse.leshan.core.node.LwM2mResource; +import org.eclipse.leshan.core.request.BindingMode; import org.eclipse.leshan.core.request.argument.Arguments; import org.eclipse.leshan.core.response.ExecuteResponse; import org.eclipse.leshan.core.response.ReadResponse; @@ -200,7 +202,7 @@ private void setTimezone(String t) { } private String getSupportedBinding() { - return "U"; + return BindingMode.toString(EnumSet.of(BindingMode.U, BindingMode.T)); } private String getDeviceType() { diff --git a/leshan-client-demo/src/main/java/org/eclipse/leshan/client/demo/cli/LeshanClientDemoCLI.java b/leshan-client-demo/src/main/java/org/eclipse/leshan/client/demo/cli/LeshanClientDemoCLI.java index c9545835cc..9857981e65 100644 --- a/leshan-client-demo/src/main/java/org/eclipse/leshan/client/demo/cli/LeshanClientDemoCLI.java +++ b/leshan-client-demo/src/main/java/org/eclipse/leshan/client/demo/cli/LeshanClientDemoCLI.java @@ -317,11 +317,10 @@ public void run() { int indexOf = main.url.indexOf("://"); String scheme = main.url.substring(0, indexOf); // we support only coap and coaps - if (!"coap".equals(scheme) && !"coaps".equals(scheme)) { - throw new MultiParameterException(spec.commandLine(), - String.format("Invalid URL %s : unknown scheme '%s', we support only 'coap' or 'coaps' for now", - main.url, scheme), - "-u"); + if (!"coap".equals(scheme) && !"coaps".equals(scheme) && !"coap+tcp".equals(scheme)) { + throw new MultiParameterException(spec.commandLine(), String.format( + "Invalid URL %s : unknown scheme '%s', we support only 'coap' or 'coaps' or 'coap+tcp' for now", + main.url, scheme), "-u"); } // check scheme matches configuration if (identity.hasIdentity()) { @@ -331,7 +330,7 @@ public void run() { main.url, scheme), "-u"); } } else { - if (!scheme.equals("coap")) { + if (!scheme.equals("coap") && !scheme.equals("coap+tcp")) { throw new MultiParameterException(spec.commandLine(), String.format( "Invalid URL %s : '%s' scheme must be used with PSK, RPK or x509 option. Do you mean 'coap' ? ", main.url, scheme), "-u");