如何使用 node-coap 包注册 LwM2M 客户端?
How to register LwM2M client with node-coap package?
我正在尝试使用 node.js coap package 模拟 LwM2M 客户端节点。我有 LwM2M 服务器 运行 并在我的 Raspberry Pi 上监听端口 5555
3.
目前我所取得的成就是将 UDP 数据包发送到 lo
(环回)接口。 (然而,它被封装在另一个 UDP 数据包中,该数据包的源和目标为 00:00:00:00:00:00,但现在对我来说这并不重要)。
const coap = require('../') // Script is located inside package examples directory
const endpointClientName = 'someSensor'
const lifetime = '600'
const version = '1.0'
const binding = 'UQ'
let bodyString = '?ep=' + endpointClientName + '<=' + lifetime + '&lwm2m=' + version + '&b=' + binding;
let responseBody = '';
let options = {
host : 'fd72:cafe:face:0:fade:deaf:1234:5678',
port : 5555,
pathname : "/rd",
method : 'POST',
confirmable : 'true',
options : {
'Accept' : 'application/json'
}
};
let request = coap.request(options);
request.on('response', function (response) {
response.on('data', function () {
responseBody += response.payload.toString();
});
response.on('end', function () {
if (response.code == '2.01') {
console.log('[coap] device registered.');
var obj = JSON.parse(responseBody);
console.log('[coap] responseBody', obj);
} else {
console.log('[coap] coap response.code=' + response.code);
}
});
});
request.write(bodyString);
request.end();
上面的代码生成包含数据的 UDP 数据包:
Data (50 bytes)
Data: 44021dff8ea487ebb272646132ff3f65703d736f6d655365...
[Length: 50]
以下数据从 wireshark 粘贴为十六进制转储
0000 44 02 1d ff 8e a4 87 eb b2 72 64 61 32 ff 3f 65
0010 70 3d 73 6f 6d 65 53 65 6e 73 6f 72 26 6c 74 3d
0020 36 30 30 26 6c 77 6d 32 6d 3d 31 2e 30 26 62 3d
0030 55 51
但是我希望 Wireshark 将数据包显示为 CoAP 数据包,其中包含分成几个部分的段:
Opt Name: #1: Uri-Path: rd
Opt Name: #2: Uri-Query: ep=someSensor
Opt Name: #3: Uri-Query: lt=600
Opt Name: #4: Uri-Query: lwm2m=1.0
Opt Name: #5: Uri-Query: b=UQ
我做错了什么?也许我设置了错误的选项?我注意到有些发送请求到地址 coap://localhost:port/path
,但是我使用其他方式未能取得更好的结果。
整个问题是 lifetime
和 lwm2m
版本等选项在数据包主体中,但必须在 query
:
中描述
query: Query string. Defaults to ''. Should not include the path, e.g. 'a=b&c=d'
注册设备后,响应也不是json格式,所有信息也不在响应正文中,而是在响应选项中:
message.options
All the CoAP options, as parsed by CoAP-packet.
All the options are in binary format, except for 'Content-Format'
,
'Accept'
and 'ETag'
. See registerOption()
to know how to register
more.
See the spec for all the possible options.
话虽如此,稍微修改一下代码就解决了问题:
const coap = require('../') // Script is located inside package examples directory
const serverAddress = 'fd72:cafe:face:0:fade:deaf:1234:5678'
const serverPort = 5555
const endpointClientName = 'someSensor'
const lifetime = '600'
const version = '1.0'
const binding = 'UQ'
const uriQuery = 'ep=' + endpointClientName + '<=' + lifetime + '&lwm2m=' + version + '&b=' + binding;
const clientResources = '/1/0'
let options = {
host : serverAddress,
port : serverPort,
pathname : "/rd",
method : 'POST',
confirmable : 'true',
query : uriQuery,
};
let request = coap.request(options);
request.on('response', function (response) {
console.log('[coap] coap response.code = ' + response.code);
console.log('[coap] coap response.options = ' + response.options);
});
request.write(clientResources);
request.end();
我正在尝试使用 node.js coap package 模拟 LwM2M 客户端节点。我有 LwM2M 服务器 运行 并在我的 Raspberry Pi 上监听端口 5555
3.
目前我所取得的成就是将 UDP 数据包发送到 lo
(环回)接口。 (然而,它被封装在另一个 UDP 数据包中,该数据包的源和目标为 00:00:00:00:00:00,但现在对我来说这并不重要)。
const coap = require('../') // Script is located inside package examples directory
const endpointClientName = 'someSensor'
const lifetime = '600'
const version = '1.0'
const binding = 'UQ'
let bodyString = '?ep=' + endpointClientName + '<=' + lifetime + '&lwm2m=' + version + '&b=' + binding;
let responseBody = '';
let options = {
host : 'fd72:cafe:face:0:fade:deaf:1234:5678',
port : 5555,
pathname : "/rd",
method : 'POST',
confirmable : 'true',
options : {
'Accept' : 'application/json'
}
};
let request = coap.request(options);
request.on('response', function (response) {
response.on('data', function () {
responseBody += response.payload.toString();
});
response.on('end', function () {
if (response.code == '2.01') {
console.log('[coap] device registered.');
var obj = JSON.parse(responseBody);
console.log('[coap] responseBody', obj);
} else {
console.log('[coap] coap response.code=' + response.code);
}
});
});
request.write(bodyString);
request.end();
上面的代码生成包含数据的 UDP 数据包:
Data (50 bytes)
Data: 44021dff8ea487ebb272646132ff3f65703d736f6d655365...
[Length: 50]
以下数据从 wireshark 粘贴为十六进制转储
0000 44 02 1d ff 8e a4 87 eb b2 72 64 61 32 ff 3f 65
0010 70 3d 73 6f 6d 65 53 65 6e 73 6f 72 26 6c 74 3d
0020 36 30 30 26 6c 77 6d 32 6d 3d 31 2e 30 26 62 3d
0030 55 51
但是我希望 Wireshark 将数据包显示为 CoAP 数据包,其中包含分成几个部分的段:
Opt Name: #1: Uri-Path: rd
Opt Name: #2: Uri-Query: ep=someSensor
Opt Name: #3: Uri-Query: lt=600
Opt Name: #4: Uri-Query: lwm2m=1.0
Opt Name: #5: Uri-Query: b=UQ
我做错了什么?也许我设置了错误的选项?我注意到有些发送请求到地址 coap://localhost:port/path
,但是我使用其他方式未能取得更好的结果。
整个问题是 lifetime
和 lwm2m
版本等选项在数据包主体中,但必须在 query
:
query: Query string. Defaults to ''. Should not include the path, e.g.
'a=b&c=d'
注册设备后,响应也不是json格式,所有信息也不在响应正文中,而是在响应选项中:
message.options
All the CoAP options, as parsed by CoAP-packet.
All the options are in binary format, except for
'Content-Format'
,'Accept'
and'ETag'
. SeeregisterOption()
to know how to register more.See the spec for all the possible options.
话虽如此,稍微修改一下代码就解决了问题:
const coap = require('../') // Script is located inside package examples directory
const serverAddress = 'fd72:cafe:face:0:fade:deaf:1234:5678'
const serverPort = 5555
const endpointClientName = 'someSensor'
const lifetime = '600'
const version = '1.0'
const binding = 'UQ'
const uriQuery = 'ep=' + endpointClientName + '<=' + lifetime + '&lwm2m=' + version + '&b=' + binding;
const clientResources = '/1/0'
let options = {
host : serverAddress,
port : serverPort,
pathname : "/rd",
method : 'POST',
confirmable : 'true',
query : uriQuery,
};
let request = coap.request(options);
request.on('response', function (response) {
console.log('[coap] coap response.code = ' + response.code);
console.log('[coap] coap response.options = ' + response.options);
});
request.write(clientResources);
request.end();