|
|
|
@ -17,18 +17,11 @@ package org.thingsboard.server.transport.lwm2m.client; |
|
|
|
|
|
|
|
import lombok.Data; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.eclipse.californium.core.network.CoapEndpoint; |
|
|
|
import org.eclipse.californium.core.network.config.NetworkConfig; |
|
|
|
import org.eclipse.californium.elements.Connector; |
|
|
|
import org.eclipse.californium.core.observe.ObservationStore; |
|
|
|
import org.eclipse.californium.scandium.DTLSConnector; |
|
|
|
import org.eclipse.californium.scandium.config.DtlsConnectorConfig; |
|
|
|
import org.eclipse.californium.scandium.dtls.ClientHandshaker; |
|
|
|
import org.eclipse.californium.scandium.dtls.DTLSSession; |
|
|
|
import org.eclipse.californium.scandium.dtls.HandshakeException; |
|
|
|
import org.eclipse.californium.scandium.dtls.Handshaker; |
|
|
|
import org.eclipse.californium.scandium.dtls.ResumingClientHandshaker; |
|
|
|
import org.eclipse.californium.scandium.dtls.ResumingServerHandshaker; |
|
|
|
import org.eclipse.californium.scandium.dtls.ServerHandshaker; |
|
|
|
import org.eclipse.californium.scandium.dtls.SessionAdapter; |
|
|
|
import org.eclipse.leshan.client.californium.LeshanClient; |
|
|
|
import org.eclipse.leshan.client.californium.LeshanClientBuilder; |
|
|
|
import org.eclipse.leshan.client.engine.DefaultRegistrationEngineFactory; |
|
|
|
@ -40,7 +33,7 @@ import org.eclipse.leshan.client.resource.ObjectsInitializer; |
|
|
|
import org.eclipse.leshan.client.servers.ServerIdentity; |
|
|
|
import org.eclipse.leshan.core.LwM2mId; |
|
|
|
import org.eclipse.leshan.core.ResponseCode; |
|
|
|
import org.eclipse.leshan.core.californium.DefaultEndpointFactory; |
|
|
|
import org.eclipse.leshan.core.californium.EndpointFactory; |
|
|
|
import org.eclipse.leshan.core.model.InvalidDDFFileException; |
|
|
|
import org.eclipse.leshan.core.model.LwM2mModel; |
|
|
|
import org.eclipse.leshan.core.model.ObjectLoader; |
|
|
|
@ -54,6 +47,7 @@ import org.eclipse.leshan.core.request.RegisterRequest; |
|
|
|
import org.eclipse.leshan.core.request.UpdateRequest; |
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
import java.net.InetSocketAddress; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.List; |
|
|
|
import java.util.concurrent.ScheduledExecutorService; |
|
|
|
@ -81,80 +75,49 @@ public class LwM2MTestClient { |
|
|
|
initializer.setInstancesForObject(SECURITY, security); |
|
|
|
initializer.setInstancesForObject(SERVER, new Server(123, 300)); |
|
|
|
initializer.setInstancesForObject(DEVICE, new SimpleLwM2MDevice()); |
|
|
|
initializer.setClassForObject(LwM2mId.ACCESS_CONTROL, DummyInstanceEnabler.class); |
|
|
|
|
|
|
|
DtlsConnectorConfig.Builder dtlsConfig = new DtlsConnectorConfig.Builder(); |
|
|
|
dtlsConfig.setRecommendedCipherSuitesOnly(true); |
|
|
|
dtlsConfig.setClientOnly(); |
|
|
|
|
|
|
|
DefaultRegistrationEngineFactory engineFactory = new DefaultRegistrationEngineFactory(); |
|
|
|
engineFactory.setReconnectOnUpdate(false); |
|
|
|
engineFactory.setResumeOnConnect(true); |
|
|
|
|
|
|
|
DefaultEndpointFactory endpointFactory = new DefaultEndpointFactory(endpoint) { |
|
|
|
EndpointFactory endpointFactory = new EndpointFactory() { |
|
|
|
|
|
|
|
@Override |
|
|
|
public CoapEndpoint createUnsecuredEndpoint(InetSocketAddress address, NetworkConfig coapConfig, |
|
|
|
ObservationStore store) { |
|
|
|
CoapEndpoint.Builder builder = new CoapEndpoint.Builder(); |
|
|
|
builder.setInetSocketAddress(address); |
|
|
|
builder.setNetworkConfig(coapConfig); |
|
|
|
return builder.build(); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
protected Connector createSecuredConnector(DtlsConnectorConfig dtlsConfig) { |
|
|
|
|
|
|
|
return new DTLSConnector(dtlsConfig) { |
|
|
|
@Override |
|
|
|
protected void onInitializeHandshaker(Handshaker handshaker) { |
|
|
|
handshaker.addSessionListener(new SessionAdapter() { |
|
|
|
|
|
|
|
@Override |
|
|
|
public void handshakeStarted(Handshaker handshaker) throws HandshakeException { |
|
|
|
if (handshaker instanceof ServerHandshaker) { |
|
|
|
log.info("DTLS Full Handshake initiated by server : STARTED ..."); |
|
|
|
} else if (handshaker instanceof ResumingServerHandshaker) { |
|
|
|
log.info("DTLS abbreviated Handshake initiated by server : STARTED ..."); |
|
|
|
} else if (handshaker instanceof ClientHandshaker) { |
|
|
|
log.info("DTLS Full Handshake initiated by client : STARTED ..."); |
|
|
|
} else if (handshaker instanceof ResumingClientHandshaker) { |
|
|
|
log.info("DTLS abbreviated Handshake initiated by client : STARTED ..."); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void sessionEstablished(Handshaker handshaker, DTLSSession establishedSession) |
|
|
|
throws HandshakeException { |
|
|
|
if (handshaker instanceof ServerHandshaker) { |
|
|
|
log.info("DTLS Full Handshake initiated by server : SUCCEED, handshaker {}", handshaker); |
|
|
|
} else if (handshaker instanceof ResumingServerHandshaker) { |
|
|
|
log.info("DTLS abbreviated Handshake initiated by server : SUCCEED, handshaker {}", handshaker); |
|
|
|
} else if (handshaker instanceof ClientHandshaker) { |
|
|
|
log.info("DTLS Full Handshake initiated by client : SUCCEED, handshaker {}", handshaker); |
|
|
|
} else if (handshaker instanceof ResumingClientHandshaker) { |
|
|
|
log.info("DTLS abbreviated Handshake initiated by client : SUCCEED, handshaker {}", handshaker); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void handshakeFailed(Handshaker handshaker, Throwable error) { |
|
|
|
/** get cause */ |
|
|
|
String cause; |
|
|
|
if (error != null) { |
|
|
|
if (error.getMessage() != null) { |
|
|
|
cause = error.getMessage(); |
|
|
|
} else { |
|
|
|
cause = error.getClass().getName(); |
|
|
|
} |
|
|
|
} else { |
|
|
|
cause = "unknown cause"; |
|
|
|
} |
|
|
|
|
|
|
|
if (handshaker instanceof ServerHandshaker) { |
|
|
|
log.info("DTLS Full Handshake initiated by server : FAILED [{}]", cause); |
|
|
|
} else if (handshaker instanceof ResumingServerHandshaker) { |
|
|
|
log.info("DTLS abbreviated Handshake initiated by server : FAILED [{}]", cause); |
|
|
|
} else if (handshaker instanceof ClientHandshaker) { |
|
|
|
log.info("DTLS Full Handshake initiated by client : FAILED [{}]", cause); |
|
|
|
} else if (handshaker instanceof ResumingClientHandshaker) { |
|
|
|
log.info("DTLS abbreviated Handshake initiated by client : FAILED [{}]", cause); |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
}; |
|
|
|
public CoapEndpoint createSecuredEndpoint(DtlsConnectorConfig dtlsConfig, NetworkConfig coapConfig, |
|
|
|
ObservationStore store) { |
|
|
|
CoapEndpoint.Builder builder = new CoapEndpoint.Builder(); |
|
|
|
DtlsConnectorConfig.Builder dtlsConfigBuilder = new DtlsConnectorConfig.Builder(dtlsConfig); |
|
|
|
|
|
|
|
// tricks to be able to change psk information on the fly
|
|
|
|
// AdvancedPskStore pskStore = dtlsConfig.getAdvancedPskStore();
|
|
|
|
// if (pskStore != null) {
|
|
|
|
// PskPublicInformation identity = pskStore.getIdentity(null, null);
|
|
|
|
// SecretKey key = pskStore
|
|
|
|
// .requestPskSecretResult(ConnectionId.EMPTY, null, identity, null, null, null).getSecret();
|
|
|
|
// singlePSKStore = new SinglePSKStore(identity, key);
|
|
|
|
// dtlsConfigBuilder.setAdvancedPskStore(singlePSKStore);
|
|
|
|
// }
|
|
|
|
builder.setConnector(new DTLSConnector(dtlsConfigBuilder.build())); |
|
|
|
builder.setNetworkConfig(coapConfig); |
|
|
|
return builder.build(); |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
LeshanClientBuilder builder = new LeshanClientBuilder(endpoint); |
|
|
|
builder.setLocalAddress("0.0.0.0", 11000); |
|
|
|
builder.setObjects(initializer.createAll()); |
|
|
|
|