Search in sources :

Example 6 with LwM2mObjectEnabler

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();
}
Also used : LwM2mObjectEnabler(org.eclipse.leshan.client.resource.LwM2mObjectEnabler) CoapServer(org.eclipse.californium.core.CoapServer) Server(org.eclipse.leshan.client.object.Server) ObjectsInitializer(org.eclipse.leshan.client.resource.ObjectsInitializer) Device(org.eclipse.leshan.client.object.Device) InetSocketAddress(java.net.InetSocketAddress) LeshanClientBuilder(org.eclipse.leshan.client.californium.LeshanClientBuilder)

Example 7 with LwM2mObjectEnabler

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);
}
Also used : LwM2mObjectEnabler(org.eclipse.leshan.client.resource.LwM2mObjectEnabler) Server(org.eclipse.leshan.client.object.Server) ObjectsInitializer(org.eclipse.leshan.client.resource.ObjectsInitializer) InetSocketAddress(java.net.InetSocketAddress) Device(org.eclipse.leshan.client.object.Device) DtlsConnectorConfig(org.eclipse.californium.scandium.config.DtlsConnectorConfig) Builder(org.eclipse.californium.scandium.config.DtlsConnectorConfig.Builder) DTLSConnector(org.eclipse.californium.scandium.DTLSConnector) SecurityObjectPskStore(org.eclipse.leshan.client.californium.impl.SecurityObjectPskStore) CoapEndpoint(org.eclipse.californium.core.network.CoapEndpoint)

Example 8 with LwM2mObjectEnabler

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");
    }
}
Also used : LwM2mObjectEnabler(org.eclipse.leshan.client.resource.LwM2mObjectEnabler) DeleteRequest(org.eclipse.leshan.core.request.DeleteRequest) BootstrapDeleteRequest(org.eclipse.leshan.core.request.BootstrapDeleteRequest)

Example 9 with LwM2mObjectEnabler

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[] {});
}
Also used : LwM2mObjectEnabler(org.eclipse.leshan.client.resource.LwM2mObjectEnabler) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Link(org.eclipse.leshan.Link)

Example 10 with LwM2mObjectEnabler

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);
}
Also used : LwM2mInstanceEnabler(org.eclipse.leshan.client.resource.LwM2mInstanceEnabler) LwM2mObjectEnabler(org.eclipse.leshan.client.resource.LwM2mObjectEnabler) LwM2mObjectEnabler(org.eclipse.leshan.client.resource.LwM2mObjectEnabler) ObjectEnabler(org.eclipse.leshan.client.resource.ObjectEnabler) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) BaseInstanceEnabler(org.eclipse.leshan.client.resource.BaseInstanceEnabler) Link(org.eclipse.leshan.Link) Test(org.junit.Test)

Aggregations

LwM2mObjectEnabler (org.eclipse.leshan.client.resource.LwM2mObjectEnabler)16 LeshanClientBuilder (org.eclipse.leshan.client.californium.LeshanClientBuilder)8 ObjectsInitializer (org.eclipse.leshan.client.resource.ObjectsInitializer)8 Device (org.eclipse.leshan.client.object.Device)7 Server (org.eclipse.leshan.client.object.Server)7 ArrayList (java.util.ArrayList)5 InetSocketAddress (java.net.InetSocketAddress)4 HashMap (java.util.HashMap)4 Link (org.eclipse.leshan.Link)4 LwM2mInstanceEnabler (org.eclipse.leshan.client.resource.LwM2mInstanceEnabler)4 ObjectEnabler (org.eclipse.leshan.client.resource.ObjectEnabler)4 Test (org.junit.Test)4 CoapServer (org.eclipse.californium.core.CoapServer)3 CoapEndpoint (org.eclipse.californium.core.network.CoapEndpoint)3 NetworkConfig (org.eclipse.californium.core.network.config.NetworkConfig)3 DTLSConnector (org.eclipse.californium.scandium.DTLSConnector)3 DtlsConnectorConfig (org.eclipse.californium.scandium.config.DtlsConnectorConfig)3 LwM2mModel (org.eclipse.leshan.core.model.LwM2mModel)3 BaseInstanceEnabler (org.eclipse.leshan.client.resource.BaseInstanceEnabler)2 ExecuteResponse (org.eclipse.leshan.core.response.ExecuteResponse)2