-
Notifications
You must be signed in to change notification settings - Fork 2
Description
Hello,
My issue I am having is not being able to successfully use the linkadrreq mac command via mqtt downlink from my server. My question is how to correctly utilize the link_adr variable and input it into the phypayload in order to successfully create a linkadrreq downlink message from the server.
I am currently using the Link_adr variable that was created from the link_adr.js file on the github. I inputted the variable along with the various functions and other variables needed for its functionality into my script. I inputted the LINK_ADR variable into the FRMpayload of the encoded packet, "var packet = new LoRaPacket..." and used the pack function to encode the entire payload (phypayload). Once this was done I encoded the entire phypayload into a JSON object (encoding the entire downlink packet) and sent it to my respective gateway.
I have attached my code below:
//LINK_ADR
// REQ
var mqtt = require('mqtt')
var client = mqtt.connect('mqtt://localhost:1883')
var OFFSET_REQ_DATARATE_TX = 0;
var SIZE_REQ_DATARATE_TX = 1;
var OFFSET_REQ_CHMASK = SIZE_REQ_DATARATE_TX;
var SIZE_REQ_CHMASK = 2;
var OFFSET_REDUNDANCY = SIZE_REQ_DATARATE_TX + SIZE_REQ_CHMASK;
var BITS_REQ_DATARATE = 0b11110000;
var OFFSET_REQ_DATARATE = 4;
var BITS_REQ_TXPOWER = 0b00001111;
var OFFSET_REQ_TXPOWER = 0;
var BITS_REQ_CHMASKCNTL = 0b01110000;
var OFFSET_REQ_CHMASKCNTL = 4;
var BITS_REQ_NBTRANS = 0b00001111;
var OFFSET_REQ_NBTRANS = 0;
// ANS
var BITS_ANS_POWERACK = 0b00000100;
var OFFSET_ANS_POWERACK = 2;
var BITS_ANS_DATARATEACK = 0b00000010;
var OFFSET_ANS_DATARATEACK = 1;
var BITS_ANS_CHANNELMASKACK = 0b00000001;
var OFFSET_ANS_CHANNELMASKACK = 0;
function decodeLinkAdrReq (buffer, offset)
{
debug ('decodeReq', {buffer: buffer, offset: offset});
let dataRateTxPower = buffer.readUInt8 (offset+OFFSET_REQ_DATARATE_TX);
let redundancy = buffer.readUInt8 (offset+OFFSET_REDUNDANCY);
let decoded = {
dataRate: (dataRateTxPower & BITS_REQ_DATARATE) >>> OFFSET_REQ_DATARATE,
txPower: (dataRateTxPower & BITS_REQ_TXPOWER),
chMask: buffer.readUInt16 (offset+OFFSET_REQ_CHMASK),
chMaskCntl: (redundancy & BITS_REQ_CHMASKCNTL) >>> OFFSET_REQ_CHMASKCNTL,
nbTrans: (redundancy & BITS_REQ_NBTRANS) >>> OFFSET_REQ_NBTRANS
};
return decoded;
}
function decodeLinkAdrAns (buffer, offset)
{
debug ('decodeAns', {buffer: buffer, offset: offset});
let status = buffer.readUInt8 (offset);
let decoded = {
powerAck: (status & BITS_ANS_POWERACK) >>> OFFSET_ANS_POWERACK,
dataRateAck: (status & BITS_ANS_DATARATEACK) >>> OFFSET_ANS_DATARATEACK,
channelMaskAck: (status & BITS_ANS_CHANNELMASKACK) >>> OFFSET_ANS_CHANNELMASKACK
};
return decoded;
}
function encodeLinkAdrReq (decoded, buffer, offset)
{
debug ('encodeReq', {buffer: buffer, offset: offset});
if (!buffer)
{
buffer = new Buffer (LINK_ADR.REQ.SIZE);
offset = 0;
}
let dataRateTxPower = 2;
let redundancy = 0;
dataRateTxPower = dataRateTxPower | (((decoded.dataRate << OFFSET_REQ_DATARATE)>>>0) & BITS_REQ_DATARATE);
dataRateTxPower = dataRateTxPower | (((decoded.txPower << OFFSET_REQ_TXPOWER)>>>0) & BITS_REQ_TXPOWER);
buffer.writeUInt8 (dataRateTxPower, offset+OFFSET_REQ_DATARATE_TX);
buffer.writeUInt16 (decoded.chMask, offset+OFFSET_REQ_CHMASK);
redundancy = redundancy | (((decoded.chMaskCntl << OFFSET_REQ_CHMASKCNTL)>>>0) & BITS_REQ_CHMASKCNTL);
redundancy = redundancy | (((decoded.nbTrans << OFFSET_REQ_NBTRANS)>>>0) & BITS_REQ_NBTRANS);
buffer.writeUInt8 (redundancy, offset+OFFSET_REDUNDANCY);
return buffer;
}
function encodeLinkAdrAns (decoded, buffer, offset)
{
debug ('decodeReq', {buffer: buffer, offset: offset});
if (!buffer)
{
buffer = new Buffer (LINK_ADR.ANS.SIZE);
offset = 0;
}
let status = 0;
status = status | (((decoded.powerAck << OFFSET_ANS_POWERACK)>>>0) & BITS_ANS_POWERACK);
status = status | (((decoded.dataRateAck << OFFSET_ANS_DATARATEACK)>>>0) & BITS_ANS_DATARATEACK);
status = status | (((decoded.channelMaskAck << OFFSET_ANS_CHANNELMASKACK)>>>0) & BITS_ANS_CHANNELMASKACK);
buffer.writeUInt8 (status, offset);
return buffer;
}
//Linkadrreq Var
var LINK_ADR = {
REQ: {
SIZE: 4,
decode: decodeLinkAdrReq,
encode: encodeLinkAdrReq
},
ANS: {
SIZE: 1,
decode: decodeLinkAdrAns,
encode: encodeLinkAdrAns
}
};
var LoRaPacket = require ('lorapacket');
//Set AppSkey and NwkSkey
LoRaPacket.prototype.setNwkSKey('2b7e151628aed2a6abf7158809cf4f3c');
LoRaPacket.prototype.setAppSKey('2b7e151628aed2a6abf7158809cf4f3c');
var NwkSKey = LoRaPacket.prototype.getNwkSKey();
var AppSKey = LoRaPacket.prototype.getAppSKey();
//console
var packet = new LoRaPacket ({
mtype: 0b011,
devAddr: '008538f5',
fCtrl:
{
adr: 1,
adrAckReq: 0,
ack: 0,
pending: 0,
fOptsLen: 0
},
fCnt: 1,
fPort: 0,
frmPayload: new Buffer (LINK_ADR.REQ.encode)
}, {NwkSKey: NwkSKey, AppSKey: AppSKey,
encoding: 'base64'
}
);
//encode phypayload
var a = packet.pack('base64',true);
console.log(a)
//encode entire downlink message
var jsonObj={"token":23080,"txInfo":{"mac":"b827ebfffec36deb","immediately":true,"frequency":923300000,"power":20,"dataRate":{"modulation":"LORA","spreadFactor":12,"bandwidth":500},"codeRate":"4/5","iPol":true,"board":0,"antenna":0},"phyPayload":""}
jsonObj.phyPayload = a
var pm = JSON.stringify(jsonObj);
//send downlink to gateway
client.publish("gateway/b827ebfffec36deb/tx",pm);
//process.exit()