use of org.eclipse.leshan.client.resource.LwM2mObjectEnabler in project leshan by eclipse.
the class SecureIntegrationTestHelper method createPSKClient.
public void createPSKClient() {
ObjectsInitializer initializer = new ObjectsInitializer();
initializer.setInstancesForObject(LwM2mId.SECURITY, Security.psk("coaps://" + server.getSecuredAddress().getHostString() + ":" + server.getSecuredAddress().getPort(), 12345, GOOD_PSK_ID.getBytes(StandardCharsets.UTF_8), GOOD_PSK_KEY));
initializer.setInstancesForObject(LwM2mId.SERVER, new Server(12345, LIFETIME, BindingMode.U, false));
initializer.setInstancesForObject(LwM2mId.DEVICE, new Device("Eclipse Leshan", MODEL_NUMBER, "12345", "U"));
List<LwM2mObjectEnabler> objects = initializer.createMandatory();
objects.add(initializer.create(2));
InetSocketAddress clientAddress = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
LeshanClientBuilder builder = new LeshanClientBuilder(getCurrentEndpoint());
builder.setLocalAddress(clientAddress.getHostString(), clientAddress.getPort());
builder.setObjects(objects);
client = builder.build();
}
use of org.eclipse.leshan.client.resource.LwM2mObjectEnabler in project leshan by eclipse.
the class LeshanClientBuilder method build.
/**
* Creates an instance of {@link LeshanClient} based on the properties set on this builder.
*/
public LeshanClient build() {
if (localAddress == null) {
localAddress = new InetSocketAddress(0);
}
if (objectEnablers == null) {
ObjectsInitializer initializer = new ObjectsInitializer();
initializer.setInstancesForObject(LwM2mId.SECURITY, Security.noSec("coap://leshan.eclipse.org:5683", 12345));
initializer.setInstancesForObject(LwM2mId.SERVER, new Server(12345, 5 * 60, BindingMode.U, false));
initializer.setInstancesForObject(LwM2mId.DEVICE, new Device("Eclipse Leshan", "model12345", "12345", "U"));
objectEnablers = initializer.createMandatory();
}
if (coapConfig == null) {
coapConfig = createDefaultNetworkConfig();
}
// handle dtlsConfig
DtlsConnectorConfig dtlsConfig = null;
if (dtlsConfigBuilder == null) {
dtlsConfigBuilder = new DtlsConnectorConfig.Builder();
}
DtlsConnectorConfig incompleteConfig = dtlsConfigBuilder.getIncompleteConfig();
// Handle PSK Store
LwM2mObjectEnabler securityEnabler = this.objectEnablers.get(LwM2mId.SECURITY);
if (securityEnabler == null) {
throw new IllegalArgumentException("Security object is mandatory");
}
if (incompleteConfig.getPskStore() == null) {
dtlsConfigBuilder.setPskStore(new SecurityObjectPskStore(securityEnabler));
} else {
LOG.warn("PskStore should be automatically set by Leshan. Using a custom implementation is not advised.");
}
// Handle secure address
if (incompleteConfig.getAddress() == null) {
if (localSecureAddress == null) {
localSecureAddress = new InetSocketAddress(0);
}
dtlsConfigBuilder.setAddress(localSecureAddress);
} else if (localSecureAddress != null && !localSecureAddress.equals(incompleteConfig.getAddress())) {
throw new IllegalStateException(String.format("Configuration conflict between LeshanBuilder and DtlsConnectorConfig.Builder for secure address: %s != %s", localSecureAddress, incompleteConfig.getAddress()));
}
// Handle active peers
if (incompleteConfig.getMaxConnections() == null)
dtlsConfigBuilder.setMaxConnections(coapConfig.getInt(Keys.MAX_ACTIVE_PEERS));
if (incompleteConfig.getStaleConnectionThreshold() == null)
dtlsConfigBuilder.setStaleConnectionThreshold(coapConfig.getLong(Keys.MAX_PEER_INACTIVITY_PERIOD));
// Use only 1 thread to handle DTLS connection by default
if (incompleteConfig.getConnectionThreadCount() == null) {
dtlsConfigBuilder.setConnectionThreadCount(1);
}
dtlsConfig = dtlsConfigBuilder.build();
// create endpoints
CoapEndpoint unsecuredEndpoint = null;
if (!noUnsecuredEndpoint) {
if (endpointFactory != null) {
unsecuredEndpoint = endpointFactory.createUnsecuredEndpoint(localAddress, coapConfig, null);
} else {
CoapEndpoint.CoapEndpointBuilder builder = new CoapEndpoint.CoapEndpointBuilder();
builder.setInetSocketAddress(localAddress);
builder.setNetworkConfig(coapConfig);
unsecuredEndpoint = builder.build();
}
}
CoapEndpoint securedEndpoint = null;
if (!noSecuredEndpoint) {
if (endpointFactory != null) {
securedEndpoint = endpointFactory.createSecuredEndpoint(dtlsConfig, coapConfig, null);
} else {
CoapEndpoint.CoapEndpointBuilder builder = new CoapEndpoint.CoapEndpointBuilder();
builder.setConnector(new DTLSConnector(dtlsConfig));
builder.setNetworkConfig(coapConfig);
securedEndpoint = builder.build();
}
}
if (securedEndpoint == null && unsecuredEndpoint == null) {
throw new IllegalStateException("All CoAP enpoints are deactivated, at least one endpoint should be activated");
}
return new LeshanClient(endpoint, unsecuredEndpoint, securedEndpoint, objectEnablers, coapConfig, additionalAttributes);
}
use of org.eclipse.leshan.client.resource.LwM2mObjectEnabler in project leshan by eclipse.
the class BootstrapHandler method delete.
public synchronized BootstrapDeleteResponse delete(ServerIdentity identity, BootstrapDeleteRequest deleteRequest) {
if (bootstrapping) {
// Only if the request is from the bootstrap server
if (!isBootstrapServer(identity)) {
return BootstrapDeleteResponse.badRequest("not from a bootstrap server");
}
// The spec say that delete on "/" should delete all the existing Object Instances - except LWM2M
// Bootstrap Server Account, (see 5.2.5.2 Bootstrap Delete)
// For now we only remove security and server object.
// Delete all device management server
LwM2mObjectEnabler serverObject = objects.get(SERVER);
for (Integer instanceId : serverObject.getAvailableInstanceIds()) {
serverObject.delete(identity, new DeleteRequest(SERVER, instanceId));
}
// Delete all security instance (except bootstrap one)
// TODO do not delete bootstrap server (see 5.2.5.2 Bootstrap Delete)
LwM2mObjectEnabler securityObject = objects.get(SECURITY);
for (Integer instanceId : securityObject.getAvailableInstanceIds()) {
securityObject.delete(identity, new DeleteRequest(SECURITY, instanceId));
}
return BootstrapDeleteResponse.success();
} else {
return BootstrapDeleteResponse.badRequest("no pending bootstrap session");
}
}
use of org.eclipse.leshan.client.resource.LwM2mObjectEnabler in project leshan by eclipse.
the class LinkFormatHelper method getClientDescription.
public static Link[] getClientDescription(Collection<LwM2mObjectEnabler> objectEnablers, String rootPath) {
List<Link> links = new ArrayList<>();
// clean root path
String root = rootPath == null ? "" : rootPath;
// create links for "object"
String rootURL = getPath("/", root);
Map<String, Object> attributes = new HashMap<>();
attributes.put("rt", "oma.lwm2m");
links.add(new Link(rootURL, attributes));
// sort resources
List<LwM2mObjectEnabler> objEnablerList = new ArrayList<>(objectEnablers);
Collections.sort(objEnablerList, new Comparator<LwM2mObjectEnabler>() {
@Override
public int compare(LwM2mObjectEnabler o1, LwM2mObjectEnabler o2) {
return o1.getId() - o2.getId();
}
});
for (LwM2mObjectEnabler objectEnabler : objEnablerList) {
// skip the security Object
if (objectEnabler.getId() == LwM2mId.SECURITY)
continue;
List<Integer> availableInstance = objectEnabler.getAvailableInstanceIds();
// Include an object link if there are no instances or there are object attributes (e.g. "ver")
Map<String, ?> objectAttributes = getObjectAttributes(objectEnabler.getObjectModel());
if (availableInstance.isEmpty() || (objectAttributes != null)) {
String objectInstanceUrl = getPath("/", root, Integer.toString(objectEnabler.getId()));
links.add(new Link(objectInstanceUrl, objectAttributes));
}
for (Integer instanceId : objectEnabler.getAvailableInstanceIds()) {
String objectInstanceUrl = getPath("/", root, Integer.toString(objectEnabler.getId()), instanceId.toString());
links.add(new Link(objectInstanceUrl));
}
}
return links.toArray(new Link[] {});
}
use of org.eclipse.leshan.client.resource.LwM2mObjectEnabler in project leshan by eclipse.
the class LinkFormatHelperTest method encode_client_description_with_version_2_0.
@Test
public void encode_client_description_with_version_2_0() {
List<LwM2mObjectEnabler> objectEnablers = new ArrayList<>();
Map<Integer, LwM2mInstanceEnabler> instancesMap = new HashMap<>();
instancesMap.put(0, new BaseInstanceEnabler());
instancesMap.put(1, new BaseInstanceEnabler());
objectEnablers.add(new ObjectEnabler(6, getVersionedObjectModel(6, "2.0"), instancesMap, null));
Link[] links = LinkFormatHelper.getClientDescription(objectEnablers, null);
String strLinks = Link.serialize(links);
assertEquals("</>;rt=\"oma.lwm2m\", </6>;ver=\"2.0\", </6/0>, </6/1>", strLinks);
}
Aggregations