-
Notifications
You must be signed in to change notification settings - Fork 407
Getting Started : Bootstrap Server
Here you will see how to code your own LWM2M Bootstrap Server with Leshan.
To develop your server you need to use the leshan-server-cf
module.
To know the last 1.x version.
<dependencies>
<dependency>
<groupId>org.eclipse.leshan</groupId>
<artifactId>leshan-server-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>
You need to use Leshan builder to create bootstrap server.
LeshanBootstrapServerBuilder builder = new LeshanBootstrapServerBuilder();
LeshanBootstrapServer bootstrapServer = builder.build();
bootstrapServer.start();
If you execute this code, you should see something like this in your log :
... INFO ... - Bootstrap server started at coap://0.0.0.0/0.0.0.0:5683
You can try to connect a client, for example using leshan-client-demo
:
wget https://hudson.eclipse.org/leshan/job/leshan/lastSuccessfulBuild/artifact/leshan-client-demo.jar
java -jar ./leshan-client-demo.jar -b -n myDevice
You should see client failing to bootstrap :
... ERROR ... - Bootstrap failed: BAD_REQUEST no bootstrap config.
This is normal because we didn't add any bootstrap configuration for this device. Let's try to add it.
// Create Bootstrap Server
LeshanBootstrapServerBuilder builder = new LeshanBootstrapServerBuilder();
LeshanBootstrapServer bootstrapServer = builder.build();
// Create a Bootstrap config.
BootstrapConfig config = new BootstrapConfig();
// delete object /0 and /1
config.toDelete = Arrays.asList("/0","/1");
// write a security instance for LWM2M server.
// here we will use the leshan sandbox at : https://leshan.eclipseprojects.io/
ServerSecurity dmSecurity = new ServerSecurity();
dmSecurity.uri = "coap://leshan.eclipseprojects.io";
dmSecurity.serverId = 2222;
dmSecurity.securityMode = SecurityMode.NO_SEC;
config.security.put(1, dmSecurity); // O is reserved for bootstrap server
// write a server object for LWM2M server
ServerConfig dmConfig = new ServerConfig();
dmConfig.shortId = dmSecurity.serverId;
dmConfig.lifetime = 5*60;
config.servers.put(0, dmConfig);
// Add the config to the store for your device.
EditableBootstrapConfigStore configStore = (EditableBootstrapConfigStore) bootstrapServer.getBoostrapStore();
configStore.add("myDevice", config);
// Start server
bootstrapServer.start();
Now try to bootstrap your device again.
java -jar ./leshan-client-demo.jar -b -n myDevice
This time you should see :
... INFO ... - Bootstrap started
... INFO ... - Bootstrap finished Bootstrap Server [uri=coap://localhost:5683].
... INFO ... - New endpoint created for server coap://23.97.187.154:5683 at coap://0.0.0.0:59517
... INFO ... - Trying to register to coap://23.97.187.154:5683 ...
... INFO ... - Registered with location '/rd/kDDCMXTs4Y'.
And you should see you device registered on leshan sandbox.
You can have a look to the code of our server demo: LeshanBootstrapServerDemo
in leshan-bsserver-demo
.
All contributions you make to our web site (including this wiki) are governed by our Terms of Use, so please take the time to actually read it. Your interactions with the Eclipse Foundation web properties and any information you may provide us about yourself are governed by our Privacy Policy.