4

I have to develop a node.js app which can interact with another oneM2M server. In particular the node.js app need to exchange data using CoaP protocol, but I don't know how to do that in a oneM2M compliant manner.

I started my node.js project using this module: https://github.com/mcollina/node-coap but I need some support to create a CoaP client using oneM2M protocol.

Code samples are really appreciated.

vp-platform
  • 601
  • 6
  • 17

2 Answers2

1

If you want to communicate with a oneM2M CSE from your application, then you should implement the Mca interface. This is the interface between a CSE and your application, technically called an Application Entity, or AE.

The simplest way is to use the REST binding. You may have a look at the oneM2M's developer guide at http://www.onem2m.org/application-developer-guide.

You also might want to read the following specifications:

You can find the latest version of these and other specifications here: http://www.onem2m.org/technical/published-documents

Andreas Kraft
  • 3,754
  • 1
  • 30
  • 39
  • Unfortunately no. But the examples in the developer guide are a good start. You basically need to implement the REST API. Another starting point could be this guide of the Eclipse om2m project: [http://wiki.eclipse.org/OM2M/one/REST_API](http://wiki.eclipse.org/OM2M/one/REST_API) . – Andreas Kraft Nov 16 '17 at 21:36
1

According to TS-0008: CoAP Protocol Binding in chapter 6.2.1 Header there is the following oneM2M operations mapping with CoAP methods:

Operation      CoAP Method
CREATE         POST
RETRIEVE       GET
UPDATE         PUT
DELETE         DELETE
NOTIFY         POST 

Then in chapter 6.2.2.4 Definition of New Options a new set of CoAP options is introduced, mapping oneM2M header parameters. Here the main HTTP header variables are listed with CoAP option equivalents:

Header HTTP variable   CoAP Option
X-M2M-Origin           256
X-M2M-RI               257
oneM2M-TY              267

So here is a minimal node.js script to do a GET operation, that is to retrieve latest contentInstance in the container resource with path /<cseBase>/<AE>/<Container>:

var coap = require('coap');

var options = {
    host : '<hostname>',
    port : 5683,
    pathname : "/<cseBase>/<AE>/<Container>/la",
    method : 'get',
    confirmable : 'true',
    options : {
        'Accept' : 'application/json'
    }
};
var bodyString = '';    // void string for GET operation
var responseBody = '';

var req = coap.request(options);
req.setOption("256", new Buffer("<origin>"));   // X-M2M-Origin (mandatory)
req.setOption("257", new Buffer('123456'));     // X-M2M-RI (mandatory)

req.on('response', function (res) {
    res.on('data', function () {
        responseBody += res.payload.toString();
    });
    res.on('end', function () {
        if (res.code == '2.05') {
            console.log('[coap] coap ready, request OK');
            var obj = JSON.parse(responseBody);
            console.log('[coap] responseBody', obj);
        } else {
            console.log('[coap] coap res.code='+res.code);
        }
    });
});
req.write(bodyString);
req.end();

And this is a minimal POST operation example, that is to create a contentInstance in the container resource with path /<cseBase>/<AE>/<Container>:

var coap = require('coap');

var options = {
    host : '<hostname>',
    port : 5683,
    pathname : "/<cseBase>/<AE>/<Container>",
    method : 'post',
    confirmable : 'true',

    options : {
        'Content-Format' : 'application/json'
    }
};
var bodyString = new Buffer('{"m2m:cin":{ "con":{"temperature":33}}}');
var responseBody = '';

var req = coap.request(options);
req.setOption("256", new Buffer("<origin>"));   // X-M2M-Origin (mandatory)
req.setOption("257", new Buffer('123456'));     // X-M2M-RI (mandatory)
req.setOption("267", new Buffer([4]));          // ty = 4, ContentInstance resource type

req.on('response', function (res) {
    res.on('data', function () {
        responseBody += res.payload.toString();
    });
    res.on('end', function () {
        if (res.code == '2.05') {
            console.log('[coap] coap ready, request OK');
            var obj = JSON.parse(responseBody);
            console.log('[coap] responseBody', obj);
        } else {
            console.log('[coap] coap res.code='+res.code);
        }
    });
});
req.write(bodyString);
req.end();
beaver
  • 17,333
  • 2
  • 40
  • 66