Search in sources :

Example 1 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_no_instances.

@Test
public void encode_client_description_with_version_2_0_no_instances() {
    List<LwM2mObjectEnabler> objectEnablers = new ArrayList<>();
    Map<Integer, LwM2mInstanceEnabler> instancesMap = new HashMap<>();
    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\"", 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) Link(org.eclipse.leshan.Link) Test(org.junit.Test)

Example 2 with LwM2mObjectEnabler

use of org.eclipse.leshan.client.resource.LwM2mObjectEnabler in project leshan by eclipse.

the class LinkFormatHelperTest method encode_client_description_with_version_1_0.

@Test
public void encode_client_description_with_version_1_0() {
    List<LwM2mObjectEnabler> objectEnablers = new ArrayList<>();
    Map<Integer, LwM2mInstanceEnabler> instancesMap = new HashMap<>();
    instancesMap.put(0, new BaseInstanceEnabler());
    objectEnablers.add(new ObjectEnabler(6, getObjectModel(6), instancesMap, null));
    Link[] links = LinkFormatHelper.getClientDescription(objectEnablers, null);
    String strLinks = Link.serialize(links);
    assertEquals("</>;rt=\"oma.lwm2m\", </6/0>", 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)

Example 3 with LwM2mObjectEnabler

use of org.eclipse.leshan.client.resource.LwM2mObjectEnabler in project leshan by eclipse.

the class LeshanClientDemo method createAndStartClient.

public static void createAndStartClient(String endpoint, String localAddress, int localPort, String secureLocalAddress, int secureLocalPort, boolean needBootstrap, String serverURI, byte[] pskIdentity, byte[] pskKey, Float latitude, Float longitude, float scaleFactor) {
    locationInstance = new MyLocation(latitude, longitude, scaleFactor);
    // Initialize model
    List<ObjectModel> models = ObjectLoader.loadDefault();
    models.addAll(ObjectLoader.loadDdfResources("/models", modelPaths));
    // Initialize object list
    ObjectsInitializer initializer = new ObjectsInitializer(new LwM2mModel(models));
    if (needBootstrap) {
        if (pskIdentity == null)
            initializer.setInstancesForObject(SECURITY, noSecBootstap(serverURI));
        else
            initializer.setInstancesForObject(SECURITY, pskBootstrap(serverURI, pskIdentity, pskKey));
    } else {
        if (pskIdentity == null) {
            initializer.setInstancesForObject(SECURITY, noSec(serverURI, 123));
            initializer.setInstancesForObject(SERVER, new Server(123, 30, BindingMode.U, false));
        } else {
            initializer.setInstancesForObject(SECURITY, psk(serverURI, 123, pskIdentity, pskKey));
            initializer.setInstancesForObject(SERVER, new Server(123, 30, BindingMode.U, false));
        }
    }
    initializer.setClassForObject(DEVICE, MyDevice.class);
    initializer.setInstancesForObject(LOCATION, locationInstance);
    initializer.setInstancesForObject(OBJECT_ID_TEMPERATURE_SENSOR, new RandomTemperatureSensor());
    List<LwM2mObjectEnabler> enablers = initializer.create(SECURITY, SERVER, DEVICE, LOCATION, OBJECT_ID_TEMPERATURE_SENSOR);
    // Create CoAP Config
    NetworkConfig coapConfig;
    File configFile = new File(NetworkConfig.DEFAULT_FILE_NAME);
    if (configFile.isFile()) {
        coapConfig = new NetworkConfig();
        coapConfig.load(configFile);
    } else {
        coapConfig = LeshanClientBuilder.createDefaultNetworkConfig();
        coapConfig.store(configFile);
    }
    // Create client
    LeshanClientBuilder builder = new LeshanClientBuilder(endpoint);
    builder.setLocalAddress(localAddress, localPort);
    builder.setLocalSecureAddress(secureLocalAddress, secureLocalPort);
    builder.setObjects(enablers);
    builder.setCoapConfig(coapConfig);
    // so we can disable the other one.
    if (!needBootstrap) {
        if (pskIdentity == null)
            builder.disableSecuredEndpoint();
        else
            builder.disableUnsecuredEndpoint();
    }
    final LeshanClient client = builder.build();
    LOG.info("Press 'w','a','s','d' to change reported Location ({},{}).", locationInstance.getLatitude(), locationInstance.getLongitude());
    // Start the client
    client.start();
    // De-register on shutdown and stop client.
    Runtime.getRuntime().addShutdownHook(new Thread() {

        @Override
        public void run() {
            // send de-registration request before destroy
            client.destroy(true);
        }
    });
    // Change the location through the Console
    try (Scanner scanner = new Scanner(System.in)) {
        while (scanner.hasNext()) {
            String nextMove = scanner.next();
            locationInstance.moveLocation(nextMove);
        }
    }
}
Also used : LwM2mObjectEnabler(org.eclipse.leshan.client.resource.LwM2mObjectEnabler) Scanner(java.util.Scanner) ObjectModel(org.eclipse.leshan.core.model.ObjectModel) Server(org.eclipse.leshan.client.object.Server) ObjectsInitializer(org.eclipse.leshan.client.resource.ObjectsInitializer) LeshanClientBuilder(org.eclipse.leshan.client.californium.LeshanClientBuilder) NetworkConfig(org.eclipse.californium.core.network.config.NetworkConfig) LwM2mModel(org.eclipse.leshan.core.model.LwM2mModel) File(java.io.File) LeshanClient(org.eclipse.leshan.client.californium.LeshanClient)

Example 4 with LwM2mObjectEnabler

use of org.eclipse.leshan.client.resource.LwM2mObjectEnabler in project leshan by eclipse.

the class ServersInfoExtractor method getInfo.

public static ServersInfo getInfo(Map<Integer, LwM2mObjectEnabler> objectEnablers) {
    LwM2mObjectEnabler securityEnabler = objectEnablers.get(SECURITY);
    LwM2mObjectEnabler serverEnabler = objectEnablers.get(SERVER);
    if (securityEnabler == null || serverEnabler == null)
        return null;
    ServersInfo infos = new ServersInfo();
    LwM2mObject securities = (LwM2mObject) securityEnabler.read(SYSTEM, new ReadRequest(SECURITY)).getContent();
    LwM2mObject servers = (LwM2mObject) serverEnabler.read(SYSTEM, new ReadRequest(SERVER)).getContent();
    for (LwM2mObjectInstance security : securities.getInstances().values()) {
        try {
            if ((boolean) security.getResource(SEC_BOOTSTRAP).getValue()) {
                if (infos.bootstrap != null) {
                    LOG.warn("There is more than one bootstrap configuration in security object.");
                } else {
                    // create bootstrap info
                    ServerInfo info = new ServerInfo();
                    LwM2mResource serverIdResource = security.getResource(SEC_SERVER_ID);
                    if (serverIdResource != null && serverIdResource.getValue() != null)
                        info.serverId = (long) serverIdResource.getValue();
                    else
                        info.serverId = 0;
                    info.serverUri = new URI((String) security.getResource(SEC_SERVER_URI).getValue());
                    info.secureMode = SecurityMode.fromCode((long) security.getResource(SEC_SECURITY_MODE).getValue());
                    infos.bootstrap = info;
                }
            } else {
                // create device management info
                DmServerInfo info = new DmServerInfo();
                info.serverUri = new URI((String) security.getResource(SEC_SERVER_URI).getValue());
                info.serverId = (long) security.getResource(SEC_SERVER_ID).getValue();
                info.secureMode = SecurityMode.fromCode((long) security.getResource(SEC_SECURITY_MODE).getValue());
                // search corresponding device management server
                for (LwM2mObjectInstance server : servers.getInstances().values()) {
                    if (info.serverId == (Long) server.getResource(SRV_SERVER_ID).getValue()) {
                        info.lifetime = (long) server.getResource(SRV_LIFETIME).getValue();
                        info.binding = BindingMode.valueOf((String) server.getResource(SRV_BINDING).getValue());
                        infos.deviceMangements.put(info.serverId, info);
                        break;
                    }
                }
            }
        } catch (URISyntaxException e) {
            LOG.error(String.format("Invalid URI %s", (String) security.getResource(SEC_SERVER_URI).getValue()), e);
        }
    }
    return infos;
}
Also used : LwM2mObjectEnabler(org.eclipse.leshan.client.resource.LwM2mObjectEnabler) LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) LwM2mObject(org.eclipse.leshan.core.node.LwM2mObject) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) LwM2mResource(org.eclipse.leshan.core.node.LwM2mResource) ReadRequest(org.eclipse.leshan.core.request.ReadRequest)

Example 5 with LwM2mObjectEnabler

use of org.eclipse.leshan.client.resource.LwM2mObjectEnabler in project leshan by eclipse.

the class RegistrationTest method fail_to_create_client_with_same_object_twice.

// TODO not really a registration test
@Test(expected = IllegalArgumentException.class)
public void fail_to_create_client_with_same_object_twice() {
    ObjectEnabler objectEnabler = new ObjectEnabler(1, null, new HashMap<Integer, LwM2mInstanceEnabler>(), null);
    ObjectEnabler objectEnabler2 = new ObjectEnabler(1, null, new HashMap<Integer, LwM2mInstanceEnabler>(), null);
    ArrayList<LwM2mObjectEnabler> objects = new ArrayList<>();
    objects.add(objectEnabler);
    objects.add(objectEnabler2);
    helper.client = new LeshanClientBuilder("test").setObjects(objects).build();
}
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) LeshanClientBuilder(org.eclipse.leshan.client.californium.LeshanClientBuilder) ArrayList(java.util.ArrayList) 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