Home

Awesome

lwm2m-node-lib

Dependency Status

Index

<a name="overview"/> Overview

The Open Mobile Alliance Lightweight M2M protocol is a machine to machine communication protocol built over COAP, and meant to communicate resource constrained devices. The protocol defines two roles for the devices: a Lightweight M2M Client (the constrained device) and a Lightweight M2M Server (meant to consume the device data and control its execution).

This library aims to provide a simple way to build Lightweight M2M Servers and Clients with Node.js, giving an abstraction over the COAP Protocol based on function calls and handlers.

Features provided by the server library:

Features provided by the client library:

The following table shows what operations are implemented and what operations pending from the defined interfaces:

InterfacesOperationServer statusClient status
Bootstrap InterfaceAnyNot implementedNot implemented
Client Registration InterfaceRegisterImplementedImplemented
Update RegisterImplementedImplemented
De-registerImplementedImplemented
Device Management & Service Enablement InterfaceAnyNot implementedNot implemented
ReadImplementedImplemented
WriteImplementedImplemented
Create /xNot implementedNot implemented
Create /x/yNot implementedNot implemented
Delete /x/yNot implementedNot implemented
Discover /xImplementedImplemented
Discover /x/yImplementedImplemented
Discover /x/y/zImplementedImplemented
Write AttributesImplementedImplemented
ExecuteImplementedImplemented
Information Reporting InterfaceObserveImplementedImplemented
NotifyImplementedImplemented
CancelImplementedImplemented

The library also provides command line clients to test both its client and server capabilities.

<a name="commandline"/> Command line applications

The library provides two command line applications in order to help developing both Lightweight M2M clients and/or servers. This applications can be used to simulate the behavior of one of the peers of the communication. Both of them use the lwm2m-node-lib library to serve all the LWTM2M requests. The following sections explain the basic features of each one.

There are multiple ways of using the applications:

Take into account that all the information loaded or registered by any of the applications is transient, so it will be lost once the processes have been stopped.

Server Command Line Application

Description

This application simulates the use of a Lightweight M2M server. It provides commands to start and stop the server, manage and query the devices connected to the server, and perform read and write operations over the resources provided by each of the connected devices.

Usage

From the root of the project type (make sure the npm install command has been previously executed to download all the dependencies):

bin/iotagent-lwm2m-server.js

You can type help in the command line at any moment to get a full list of the available commands.

All the server configuration is read from the config.js file in the root of the project. You can print the configuration that is actually being used using the config command.

To exit the command line client, use CTRL-C.

Command reference

start  

	Starts a new Lightweight M2M server listening in the prefconfigured port.

stop  

	Stops the current LWTM2M Server running.

list  

	List all the devices connected to the server.

write <deviceId> <resourceId> <resourceValue>  

	Writes the given value to the resource indicated by the URI (in LWTM2M format) in the givendevice.

execute <deviceId> <resourceId> <executionArguments>  

	Executes the selected resource with the given arguments.

read <deviceId> <resourceId>  

	Reads the value of the resource indicated by the URI (in LWTM2M format) in the given device.

discover <deviceId> <objTypeId> <objInstanceId> <resourceId>  

	Sends a discover order for the given resource to the given device.

discoverObj <deviceId> <objTypeId> <objInstanceId>  

	Sends a discover order for the given instance to the given device.

discoverType <deviceId> <objTypeId>  

	Sends a discover order for the given resource to the given device.

observe <deviceId> <objTypeId> <objInstanceId> <resourceId>  

	Stablish an observation over the selected resource.

writeAttr <deviceId> <objTypeId> <objInstanceId> <resourceId> <attributes>  

	Write a new set of observation attributes to the selected resource. The attributes should be
	 in the following format: name=value(,name=value)*. E.g.: pmin=1,pmax=2.

cancel <deviceId> <objTypeId> <objInstanceId> <resourceId>  

	Cancel the observation order for the given resource (defined with a LWTM2M URI) to the given device.

config  

	Print the current config.

Client Command Line Application

Description

This application simulates the use of a Lightweight M2M Client (typically a device or device hub). It provides the following features:

Usage

From the root of the project type (make sure the npm install command has been previously executed to download all the dependencies):

bin/iotagent-lwm2m-client.js

You can type help in the command line at any moment to get a full list of the available commands.

All the client configuration is read from the config.js file in the root of the project. You can print the configuration that is actually being used using the config command.

To exit the command line client, use CTRL-C or the quit command.

The command line client can also be used to execute scripts. Each line of the script is interpreted as a line in the command line. You have to take two things into account:

The following is an example of script:

create /75001/2
create /75002/2
set /75001/2 0 440.81
set /75002/2 1 Connected
connect localhost 60001 PruebasDuplex /
quit

Command reference

create <objectUri>  

	Create a new object. The object is specified using the /type/id OMA notation.

get <objectUri>  

	Get all the information on the selected object.

remove <objectUri>  

	Remove an object. The object is specified using the /type/id OMA notation.

set <objectUri> <resourceId> <resourceValue>  

	Set the value for a resource. If the resource does not exist, it is created.

unset <objectUri> <resourceId>  

	Removes a resource from the selected object.

list  

	List all the available objects along with its resource names and values.

connect <host> <port> <endpointName> <url>  

	Connect to the server in the selected host and port, using the selected endpointName.

updateConnection  

	Updates the current connection to a server.

disconnect  

	Disconnect from the current server.

config  

	Print the current config.

quit  

	Exit the client.

<a name="libraryusage"/> Usage

In order to use the library, add the following dependency to your package.json file:

"lwm2m-node-lib": "*"

In order to use this library, first you must require it:

var lwtm2m = require('lwm2m-node-lib');

As a Lightweight M2M Server, the library supports four groups of features, one for each direction of the communication: client-to-server and server-to-client (and each flow both for the client and the server). Each feature set is defined in the following sections.

Server features (client -> server)

Starting and stopping the server

To start the LWTM2M Server execute the following command:

lwtm2m.start(config, function(error) {
  console.log('Listening');
});

The config object contains all the information required to start the server (see its structure in the Configuration section below).

Only one server can be listening at a time in the library (is treated as a singleton), so multiple calls to 'start()' without a previous call to 'stop()' will end up in an error.

To stop the server, execute the following method:

lwtm2m.stop(function(error) {
  console.log('Server stopped');
});

No information is needed to stop the server, as there is a single instance per module.

Handling incoming messages

The server listens to multiple kinds incoming messages from the devices, described in the different LWTM2M Interfaces. For each operation of an interface that needs to be captured by the server, this library provides a handler that will have the opportunity to manage the event.

The following table lists the current supported events along with the expected signature of the handlers. Be careful with the handler signatures, as an interruption in the callbacks pipeline may hang up your server.

InterfaceOperationCodeSignature
RegistrationRegisterregistrationfn(endpoint, lifetime, version, binding, callback)
RegistrationUpdateunregistrationfn(device, callback)
RegistrationDe-registerupdateRegistrationfunction(object, callback)

The meaning of each parameter should be clear reading the operation description in OMA's documentation.

Server features (server -> client)

Each writing feature is modelled as a function in the LWTM2M module. The following sections describe the implemented features, identified by its Interface and name of the operation.

Device Management Interface: Write

Signature:

function write(deviceId, objectType, objectId, resourceId, value, callback)

Execute a Write operation over the selected resource, identified following the LWTM2M conventions by its: deviceId, objectType, objectId and resourceId, changing its value to the value passed as a parameter. The device id can be found from the register, based on the name or listing all the available ones.

Device Management Interface: Execute

Signature:

function execute(deviceId, objectType, objectId, resourceId, arguments, callback)

Executes the resource identified following the LWTM2M conventions by its: deviceId, objectType, objectId and resourceId, with the arguments passed as a parameter. The device id can be found from the register, based on the name or listing all the available ones.

Device Management Interface: Read

Signature:

function read(deviceId, objectType, objectId, resourceId, callback)

Execute a read operation for the selected resource, identified following the LWTM2M conventions by its: deviceId, objectType, objectId and resourceId. The device id can be found from the register, based on the name or listing all the available ones.

Client features

Configuration

The LWM2M Client library has to be configured before any interaction with the remote LWM2M server. This configuration is done through the use of the init() function. This function takes a configuration object (can be the same one passed to the server), that has a client attribute with the configuraiton for the client (as described in the configuration section). Failing to do so may lead to unexpected results.

Registration

Before making any interaction with a Lightweight M2M server, a client must register to it. This registration can be done with the following function:

    lwm2mClient.register(host, port, endpointName, function (error, deviceInfo) {
	...
    });

The registration process needs the host and port of the destination server and an endpointName for the device (that must be unique for that server). The server will keep the client's IP in order to send the server-initiated requests.

The callback of the register function returns all the information about the created connection. There are two important pieces of information in this object:

When the client register to a server, it also opens a socket for listening in its machine, so to receive server-initiated requests (the port for listening can be configured in the config object).

The connection to the server can be closed using the following function:

    lwm2mClient.unregister(deviceInformation, function (error) {
	...
    })

If the client changes its IP for whatever reason, it must updates its registration in the server, by using the following function:

    lwm2mClient.update(deviceInformation, function (error) {
	...
    })

Object repository

All the information in a Lightweight M2M client is organized in objects and resources of those objects. To assist the client with the management of this information, the library provides a object repository (currently a in-memory transient repository only). This repository can be accessed in the lwm2mClient.registry attribute.

The repository offers the standard CRUD operations over objects, and methods to set and unset resource values inside the objects.

All the objects are identified by a URI that is composed of an Object ID and an Object Instance sepparated by slashes, as specified by the Lightweight M2M specification (e.g.: /1/3).

<a name="configuration"/> Configuration

The configuration object should contain the following fields:

<a name="multipleinterfaces"/> Configuring multiple southbound interfaces

The Lightweight M2M Server library can be configured to accept registrations in multiple southbound paths (all of them sharing IP and Port). In this case, each device will be assigned a different root base for its requests (that will be returned in the Location-path option), and will be assigned a device type, that can be used to group devices.

The southbound interfaces can be configured in the server.types configuration parameter. This parameter is a list of objects composed of two attributes:

Devices that arrive to the global /rd registration path will be assigned the default type instead (configured in server.defaultType configuration attribute).

Configuration with environment variables

Some of the more common variables can be configured using environment variables.

Environment variableConfiguration attribute
LWM2M_PORTport
LWM2M_PROTOCOLserverProtocol
LWM2M_REGISTRY_TYPEdeviceRegistry.type
LWM2M_LOG_LEVELmqtt.password
LWM2M_WRITE_FORMATwriteFormat
LWM2M_DEFAULT_ACCEPT_FORMATdefaultAcceptFormat
LWM2M_MONGO_HOSTmongodb.host
LWM2M_MONGO_PORTmongodb.port
LWM2M_MONGO_DBmongodb.db
LWM2M_MONGO_REPLICASETmongodb.replicaSet

<a name="development"/> Development documentation

Contributions

All contributions to this project are welcome. Developers planning to contribute should follow the Contribution Guidelines

Project build

The project is managed using npm.

For a list of available task, type

npm run

The following sections show the available options in detail.

Testing

Mocha Test Runner + Should.js Assertion Library.

The test environment is preconfigured to run BDD testing style.

Module mocking during testing can be done with proxyquire

To run tests, type

npm test

MongoDB should be up and running in the same host where you run the command above. Otherwise some tests may fail.

Coding guidelines

jshint

Uses provided .jshintrc flag file. To check source code style, type

npm run lint

Continuous testing

Support for continuous testing by modifying a src file or a test. For continuous testing, type

npm run test:watch

If you want to continuously check also source code style, use instead:

npm run watch

Code Coverage

Istanbul

Analizes the code coverage of your tests.

To generate an HTML coverage report under site/coverage/ and to print out a summary, type

# Use git-bash on Windows
npm run test:coverage

Clean

Removes node_modules and coverage folders, and package-lock.json file so that a fresh copy of the project is restored.

# Use git-bash on Windows
npm run clean

<a name="deprecated"/> Deprecated features

Please check deprecated features.