50
Hands-on with Lightweight M2M Run a smartwatch on the Internet-of-Things

Hands on with lightweight m2m and Eclipse Leshan

Embed Size (px)

Citation preview

Page 1: Hands on with lightweight m2m and Eclipse Leshan

Hands-on with Lightweight M2M

Run a smartwatch on the Internet-of-Things

Page 2: Hands on with lightweight m2m and Eclipse Leshan

Agenda

IntroCoAP & Lightweight M2MDemoEclipse LeshanHands-on!Eclipse WakaamaGoing further

Page 3: Hands on with lightweight m2m and Eclipse Leshan

Your devoted presenters

Julien VermillardManuel SangoïSimon Bernard

Page 4: Hands on with lightweight m2m and Eclipse Leshan

Lightweight M2MOpen Mobile Alliance

Page 5: Hands on with lightweight m2m and Eclipse Leshan

Lightweight M2M - What is it?

A reboot of OMA-DM targeting M2M

Built on top of CoAP (RFC 7252) and DTLS (RFC 6347)

A REST API for device management and applications

Page 6: Hands on with lightweight m2m and Eclipse Leshan

Device management?Operate, monitor, configure, upgrade fleets of

communicating objects

Page 7: Hands on with lightweight m2m and Eclipse Leshan

Device management

Firmware/software upgrade

Configure “OTA” the device

Monitor connectivity (signal, statistics,..)

Reboot, reset to factory

Rotate security keys

In a consistent way for different hardware

Page 8: Hands on with lightweight m2m and Eclipse Leshan

Based on CoAP?The Constrained Application Protocol

Page 9: Hands on with lightweight m2m and Eclipse Leshan

A new protocol for a new world

Class 1 devices~100KiB Flash~10KiB RAM

Low-power networks<100Bytes packets

Page 10: Hands on with lightweight m2m and Eclipse Leshan

Where old solutions doesn’t fit

Targetof less than $1 for IoT SoC

TCP and HTTPare not a good fit

Page 11: Hands on with lightweight m2m and Eclipse Leshan

CoAP - RFC 7252

RESTful protocol designed from scratch

URIs, Internet Media Types

GET, POST, PUT, DELETE

Transparent mapping to HTTP

Additional features for M2M scenarios

Observe

Page 12: Hands on with lightweight m2m and Eclipse Leshan

Compact and binary

Binary protocol- Low parsing complexity

- Small message size

Options- Binary HTTP-like headers

0 – 8 Bytes TokenExchange handle for client

4-byte Base HeaderVersion | Type | T-len | Code | ID

OptionsLocation, Max-Age, ETag, …

Marker0xFF

PayloadRepresentation

Page 13: Hands on with lightweight m2m and Eclipse Leshan

CoAP Security

Based on DTLS 1.2 (TLS for Datagrams)

Focus on AES and Elliptic Curve Crypto (ECC)

Pre-shared secrets, X.509, or raw public keys

Hardware acceleration in IoT oriented SoC

Page 14: Hands on with lightweight m2m and Eclipse Leshan

LwM2MAn API on top of CoAP

Page 15: Hands on with lightweight m2m and Eclipse Leshan

Lightweight M2M

REST API for:

Security, Device, Server

Connectivity monitoring, Connectivity statistics

Location, Firmware Upgrade, Software management,

Swipe & Lock

But objects have a numerical identifier

Page 16: Hands on with lightweight m2m and Eclipse Leshan

LwM2M URLs

/{object}/{instance}/{resource}

Examples:● "/6/0" the whole location object (binary record)

● "/6/0/1" only the longitude (degree)

Page 17: Hands on with lightweight m2m and Eclipse Leshan

Standard objects

Example: Device ManufacturerModel numberSerial numberFirmware versionRebootFactory resetPower sources

Power V/ABattery levelMemory freeError codeCurrent timeUTC offsetTimezone

Page 18: Hands on with lightweight m2m and Eclipse Leshan

Custom objects

You can define your own objects

Discoverable using CoAP Link Format

IPSO Alliance Smart Objects:

accelerometer, temperature, sensors,...

Page 19: Hands on with lightweight m2m and Eclipse Leshan

Showtime!

The smartwatch demo

Page 20: Hands on with lightweight m2m and Eclipse Leshan

Eclipse LeshanLightweight M2M for Java

Page 21: Hands on with lightweight m2m and Eclipse Leshan

Leshan

Java library for implementing servers & clients

Friendly for any Java developer

Simple (no framework, few dependencies)

But also a Web UI for discovering and testing

Build using “mvn install”

Based on Californium and Scandium

Page 22: Hands on with lightweight m2m and Eclipse Leshan

History

Started 22 Jul. 2013 @ Sierra Wireless

First external contribution 10 March 2014

Public sandbox Jul. 2014

Proposed as an Eclipse project Sep. 2014

Client contributed Oct. 2014

Page 23: Hands on with lightweight m2m and Eclipse Leshan

LwM2M playground: Public sandbox

http://leshan.eclipse.org

Bleeding edge: deployed on master commit

IPv4 and IPv6

Press “CoAP messages” for low-level traces

Page 24: Hands on with lightweight m2m and Eclipse Leshan

Commiters

Simon Bernard - Sierra Wireless

Kai Hudalla - Bosch Software Innovations

Manuel Sangoï - Sierra Wireless

J.F. Schloman - Zebra Technologies, Zatar

Julien Vermillard - Sierra Wireless

Page 25: Hands on with lightweight m2m and Eclipse Leshan

Leshan Features

Client initiated bootstrap

Registration/Deregistration

Read, Write, Create objects

TLV encoding/decoding

OSGi friendly:https://github.com/eclipse/leshan.osgi

Page 26: Hands on with lightweight m2m and Eclipse Leshan

Leshan security

DTLS Pre-Shared-Key

DTLS Raw-Public-Key

DTLS X.509

Page 27: Hands on with lightweight m2m and Eclipse Leshan

Maven modules

leshan-core commons elements

leshan-server-core server lwm2m logic

leshan-server-cf californium server

leshan-client-core client lwm2m logic

leshan-client-cf californium client

leshan-all everything above in 1 jar

leshan-client-example

leshan-standalone application with web UI

leshan-bs-server standalone bootstrap

leshan-integration-tests

Page 28: Hands on with lightweight m2m and Eclipse Leshan

Server API

// Build LWM2M serverlwServer = new LeshanServerBuilder().build();lwServer.getClientRegistry().addListener(new ClientRegistryListener() {

@Overridepublic void registered(Client client) {

System.out.println("New registered client with endpoint: " +client.getEndpoint());

}

@Overridepublic void updated(Client clientUpdated) {

System.out.println("Registration updated”);}

@Overridepublic void unregistered(Client client) {

System.out.println("Registration deleted”);}

});

// StartlwServer.start();System.out.println("Demo server started");

Page 29: Hands on with lightweight m2m and Eclipse Leshan

Server API

// Prepare the new valueLwM2mResource currentTimeResource = new LwM2mResource(13,

Value.newDateValue(new Date()));

// Send a write request to a clientWriteRequest writeCurrentTime = new WriteRequest(client, 3, 0, 13,

currentTimeResource, ContentFormat.TEXT, true);

ClientResponse response = lwServer.send(writeCurrentTime);System.out.println("Response to write request from client " +

client.getEndpoint() + ": " +response.getCode());

Page 30: Hands on with lightweight m2m and Eclipse Leshan

Implements your own store

ClientRegistry: Store currently registered clients

SecurityRegistry: Store security informations

Default implementations are “in-memory”for demo only!

Page 31: Hands on with lightweight m2m and Eclipse Leshan

Client API

// Initialize object tree with device(3) and location(6) objectsList<ObjectEnabler> enablers = new ObjectsInitializer().create(3, 6);

// Create a clientLeshanClient client = new LeshanClient(new InetSocketAddress("127.0.0.1", 5683), new ArrayList<LwM2mObjectEnabler>(enablers));

// Start itclient.start();

// Register to the serverRegisterResponse response = client.send(new RegisterRequest("myDevice"));System.out.println("Response to register request from server : " + response.getCode());

Page 32: Hands on with lightweight m2m and Eclipse Leshan

Implements your own LwM2m Objects

Lwm2m Object instance are mapped on Java instance.

Default implementation are just stupid mock for demo only.

Page 33: Hands on with lightweight m2m and Eclipse Leshan

Hands-on!

Page 34: Hands on with lightweight m2m and Eclipse Leshan

Getting started

● Tutorial projectsFrom the USB stick

orhttps://github.com/msangoi/leshan-tuto

● Launch Eclipse and import the projectsFile > Import... > Existing projects into workspace

Page 35: Hands on with lightweight m2m and Eclipse Leshan

Step 1

Register a LWM2M client with a server

1. Complete the codeSend a registration request to the server

2. Run a local server (leshan standalone)java -jar leshan-standalone.jarWeb UI available: http://localhost:8080/#/clients

3. Run the client

Page 36: Hands on with lightweight m2m and Eclipse Leshan

Step 2

Define your own Device standard object

1. Complete the codea. Handle Read requests for some resources

(Manufacturer model, Current timestamp…)b. Handle Write requests for Current timestamp resourcec. Handle Exec requests for Reboot resource

2. Run the client (same server as Step 1)

Page 37: Hands on with lightweight m2m and Eclipse Leshan

Step 3

Run a server synchronizing the current time of each new registered client

1. Complete the codea. Build a leshan serverb. Listen for new client registrationsc. Send a write request to synchronize the current time

of the client (resource with path /3/0/13)

2. Run the server3. Test with the client from Step 2

Page 38: Hands on with lightweight m2m and Eclipse Leshan

Step 4

Observe the current time of a registered client

1. Update the client (from step 2) to notify when the current time value has changed

2. Complete the server codea. Listen for new observations and log the new value

when a new notification is receivedb. Listen for new registrations and send an observe

request (path /3/0/13) to the new clients

Page 39: Hands on with lightweight m2m and Eclipse Leshan

Going further

Page 40: Hands on with lightweight m2m and Eclipse Leshan

Eclipse Wakaama

A C client and server implementation of LwM2M

Not a shared library (.so/.dll)

Embedded friendly but using malloc/free

Plug your own IP stack and DTLS implementation

Page 41: Hands on with lightweight m2m and Eclipse Leshan

Wakaama: Features

Register, registration update, deregister

Read, write resources

Read, write, create, delete object instances

TLV or plain text

Observe

Page 42: Hands on with lightweight m2m and Eclipse Leshan

Wakaama: Structure

core :internals.h liblwm2m.c liblwm2m.hlist.c management.c objects.c observe.cpacket.c registration.c tlv.c transaction.curi.c utils.c

core/er-coap-13 :er-coap-13.c er-coap-13.h

Page 43: Hands on with lightweight m2m and Eclipse Leshan

lwm2m_object_t * get_object_device(){ lwm2m_object_t * deviceObj; deviceObj = (lwm2m_object_t *)lwm2m_malloc(sizeof(lwm2m_object_t));

if (NULL != deviceObj) { memset(deviceObj, 0, sizeof(lwm2m_object_t)); deviceObj->objID = 3;

deviceObj->readFunc = prv_device_read; deviceObj->writeFunc = prv_device_write; deviceObj->executeFunc = prv_device_execute; deviceObj->userData = lwm2m_malloc(sizeof(device_data_t));

if (NULL != deviceObj->userData) { ((device_data_t*)deviceObj->userData)->time = 1367491215; strcpy(((device_data_t*)deviceObj->userData)->time_offset, "+01:00"); } else { lwm2m_free(deviceObj); deviceObj = NULL; } }

return deviceObj;}

Create objects!

Page 44: Hands on with lightweight m2m and Eclipse Leshan

objArray[0] = get_object_device();if (NULL == objArray[0]){ fprintf(stderr, "Failed to create Device object\r\n"); return -1;}objArray[1] = get_object_firmware();if (NULL == objArray[1]){ fprintf(stderr, "Failed to create Firmware object\r\n"); return -1;}objArray[2] = get_test_object();if (NULL == objArray[2]){ fprintf(stderr, "Failed to create test object\r\n"); return -1;}lwm2mH = lwm2m_init(prv_connect_server, prv_buffer_send, &data);if (NULL == lwm2mH){ fprintf(stderr, "lwm2m_init() failed\r\n"); return -1;}result = lwm2m_configure(lwm2mH, "testlwm2mclient", BINDING_U, NULL, OBJ_COUNT, objArray);...result = lwm2m_start(lwm2mH);

ConfigureWakaama!

Page 45: Hands on with lightweight m2m and Eclipse Leshan

while (0 == g_quit){ struct timeval tv; tv.tv_sec = 60; tv.tv_usec = 0;

/* * This function does two things: * - first it does the work needed by liblwm2m (eg. (re)sending some packets). * - Secondly it adjust the timeout value (default 60s) depending on the state of the transaction * (eg. retransmission) and the time between the next operation */ result = lwm2m_step(lwm2mH, &tv); if (result != 0) { fprintf(stderr, "lwm2m_step() failed: 0x%X\r\n", result); return -1; }

Active Loop

Page 46: Hands on with lightweight m2m and Eclipse Leshan

TinyDTLS

Eclipse Proposal“Support session multiplexing in single-threaded applications and thus targets specifically on embedded systems.”

Examples for Linux, or Contiki OS

TLS_PSK_WITH_AES_128_CCM_8TLS_ECDHE_ECDSA_WITH_AES128_CCM_8

Page 47: Hands on with lightweight m2m and Eclipse Leshan

In real hardware?

Page 48: Hands on with lightweight m2m and Eclipse Leshan

Spark Core:Cortex-M3 STM32, RAM/ROM 20/128k, 72MHzWiFi

Arduino MegaAVR, ATmega2560,

RAM/ROM 8/256k, 16MHzEthernet

Page 49: Hands on with lightweight m2m and Eclipse Leshan

Thank you!

Questions?

Page 50: Hands on with lightweight m2m and Eclipse Leshan