Gateway TLG3901B/BLV2 Series User Manual
V2.0.4
37
December 2020
4.1.2.
Parse LoRaWAN Packet
As described at above section, there is a
lora-packet
library provided. This library can used
to parse LoRaWAN packet in LoRaScript. List 4-5 is example code and Figure 4-2 shows
the execution result.
List 4-5 Example of Parsing LoRaWAN Packet
var lora_packet = require('lora-packet');
function onInit() {debug_print("on init"); }
function onLoRaRx (data) {
debug_print("on Recv data");
var raw = JSON.parse(data);
var lorawan_packet = new Buffer(raw.data, 'base64');
if(lorawan_packet.length>12) {
var device = new Buffer(4);
for (var i=0; i<4; i++) {
device[i] = lorawan_packet[4-i];
}
var packet = lora_packet.fromWire(lorawan_packet);
var NwkSKey = new Buffer("2b7e151628aed2a6abf7158809cf4f3c", 'hex');
if(lora_packet.verifyMIC(packet, NwkSKey)){
var AppSKey = new Buffer("2b7e151628aed2a6abf7158809cf4f3c",
'hex');
var dec = lora_packet.decrypt(packet, AppSKey,
NwkSKey).toString('hex');
var msg = {
devaddr: device.toString('hex'),
data: dec,
fport: packet.getFPort(),
counter: packet.getFCnt(),
freq: raw.freq,
datr: raw.datr
};
debug_print(JSON.stringify(msg));
}
}
}