-
Notifications
You must be signed in to change notification settings - Fork 2
/
poc_decoder.js
60 lines (51 loc) · 1.51 KB
/
poc_decoder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
* Decoder for proof of coverage paylaod
*
* @Author: Didier DONSEZ, Université Grenoble Alpes
*/
function byteToHexString(uint8arr, size) {
var hexStr = '';
for (var i = size-1; i >= 0 ; i--) {
var hex = (uint8arr[i] & 0xff).toString(16);
hex = (hex.length === 1) ? '0' + hex : hex;
hexStr += hex;
}
return hexStr;
}
function readUInt16BE(buf, offset) {
offset = offset >>> 0;
return (buf[offset] << 8) | buf[offset + 1];
}
function readUInt8(buf, offset) {
offset = offset >>> 0;
return (buf[offset]);
}
// For Chirpstack v3
// Decode decodes an array of bytes into an object.
// - fPort contains the LoRaWAN fPort number
// - bytes is an array of bytes, e.g. [225, 230, 255, 0]
// The function must return an object, e.g. {"temperature": 22.5}
function Decode(fPort, bytes) {
// TODO add optional lattitude (float32), longitude (float32), altitude (uint16)
return {
gweui: byteToHexString(bytes, 8),
token: readUInt16BE(bytes, 8),
txpower: readUInt8(bytes, 10) // in dBm
}
}
// For TTNv2
// Decode decodes an array of bytes into an object.
// - bytes is an array of bytes, e.g. [225, 230, 255, 0]
// - fPort contains the LoRaWAN fPort number
// The function must return an object, e.g. {"temperature": 22.5}
function Decoder(bytes, fPort) {
return Decode(fPort, bytes);
}
// For TTNv3
function decodeUplink(input) {
return {
data: Decoder(input.bytes, input.fPort),
warnings: [],
errors: []
};
}