Skip to content

Getting Started : Client

Simon edited this page Apr 15, 2020 · 9 revisions

Here you will see how to code your own LWM2M client with Leshan.

Get Leshan

To develop your client you need to use the leshan-client-cf module. To know the last 1.x version.

Using maven

<dependencies>
    <dependency>
        <groupId>org.eclipse.leshan</groupId>
        <artifactId>leshan-client-cf</artifactId>
        <version><!-- use the last version --></version>                  
    </dependency>
    <!-- add any slf4j backend, here we use one of the smallest one: slf4j-simple -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>1.7.30</version>
        <scope>runtime</scope>
    </dependency>
</dependencies>

Create a Simple Client.

You need to use Leshan builder to create client.

String endpoint = "..." ; // choose an endpoint name
LeshanClientBuilder builder = new LeshanClientBuilder(endpoint);
LeshanClient client = builder.build();
client.start();

If you execute this code (with internet access), you should see something like this in your log :

[...] INFO ... - Leshan client[endpoint:myClient] started at coap://0.0.0.0/0.0.0.0:46535 coaps://0.0.0.0/0.0.0.0:36490
[...] INFO ... - Trying to register to coap://leshan.eclipseprojects.io:5683 ...
[...] INFO ... - Registered with location '/rd/w50JjnP76l'.
[...] INFO ... - Next registration update in 270.0s...

That's means that you are successfully register your client to the leshan sandbox. You can check if you see your device in the web UI and you can play with it (Read or Write on Device object).

Create my own objects

By default the Leshan client is created with mandatory objects (Security, Server and Device). Internally LeshanClientBuilder uses the ObjectsInitializer to create those objects, like this :

// create objects
ObjectsInitializer initializer = new ObjectsInitializer();
initializer.setInstancesForObject(LwM2mId.SECURITY, Security.noSec("coap://leshan.eclipseprojects.io: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"));

// add it to the client
builder.setObjects(initializer.createAll());

We provide only 3 really simple object implementation and you will need to create your own LWM2M objects.
To do that you need to create a class which implement LwM2mInstanceEnabler or BaseInstanceEnabler.

public class ConnectivityStatistics extends BaseInstanceEnabler {

    @Override
    public ReadResponse read(ServerIdentity identity, int resourceid) {
        switch (resourceid) {
        case 0:
            return ReadResponse.success(resourceid, getSmsTxCounter());
        }
        return ReadResponse.notFound();
    }

    @Override
    public WriteResponse write(ServerIdentity identity, int resourceid, LwM2mResource value) {
        switch (resourceid) {
        case 15: 
            setCollectionPeriod((Long) value.getValue());
            return WriteResponse.success();
        }
        return WriteResponse.notFound();
    }

    @Override
    public ExecuteResponse execute(ServerIdentity identity, int resourceid, String params) {
        switch (resourceid) {
        case 12: 
            start();
            return ExecuteResponse.success();
        }
        return ExecuteResponse.notFound();
    }

Now we add it to your client:

ObjectsInitializer initializer = new ObjectsInitializer();
initializer.setInstancesForObject(LwM2mId.SECURITY, Security.noSec("coap://leshan.eclipseprojects.io: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"));
initializer.setInstancesForObject(7, new ConnectivityStatistics());

// add it to the client
builder.setObjects(initializer.createAll());

To go further

You can have a look to the code of our client demo: LeshanClientDemo in leshan-client-demo.