Skip to content

Commit

Permalink
Add coap+tcp support based on java-coap to client-demo.
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Apr 23, 2024
1 parent 3dbde22 commit cca3325
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
4 changes: 4 additions & 0 deletions leshan-client-demo/pom.xml
Expand Up @@ -37,6 +37,10 @@ Contributors:
<groupId>org.eclipse.leshan</groupId>
<artifactId>leshan-tl-javacoap-client</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.leshan</groupId>
<artifactId>leshan-tl-javacoap-client-coaptcp</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.leshan</groupId>
<artifactId>leshan-core-demo</artifactId>
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -64,16 +65,19 @@
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;
import org.eclipse.leshan.core.node.LwM2mSingleResource;
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;
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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);
Expand Down
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down
Expand Up @@ -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()) {
Expand All @@ -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");
Expand Down

0 comments on commit cca3325

Please sign in to comment.