Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix coding of the "Margin" field of the "DevStatusAns" command payload #131

Merged
merged 9 commits into from
Sep 27, 2018
10 changes: 8 additions & 2 deletions src/lmic/lmic.c
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,10 @@ scan_mac_cmds(
}
case MCMD_DEVS_REQ: {
LMIC.devsAns = 1;
// LMIC.snr is SNR time 4, convert to real SNR; rounding towards zero.
const int snr = (LMIC.snr + 2) / 4;
// per [1.02] 5.5. the margin is the SNR.
LMIC.devAnsMargin = (u1_t)(0b00111111 & (snr <= -32 ? -32 : snr >= 31 ? 31 : snr));
oidx += 1;
continue;
}
Expand Down Expand Up @@ -838,11 +842,13 @@ static bit_t decodeFrame (void) {
if( LMIC.adrAckReq != LINK_CHECK_OFF )
LMIC.adrAckReq = LINK_CHECK_INIT;

// Process OPTS
int m = LMIC.rssi - RSSI_OFF - getSensitivity(LMIC.rps);
LMIC.margin = m < 0 ? 0 : m > 31 ? 31 : m;
// for legacy reasons, LMIC.margin is set to the unsigned sensitivity. It can never be negative.
// it's only computed for legacy clients
LMIC.margin = m < 0 ? 0 : m > 254 ? 254 : m;

#if LMIC_DEBUG_LEVEL > 0
// Process OPTS
LMIC_DEBUG_PRINTF("%lu: process options (olen=%#x)\n", os_getTime(), olen);
#endif

Expand Down
3 changes: 2 additions & 1 deletion src/lmic/lmic.h
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ struct lmic_t {

u4_t freq;
s1_t rssi;
s1_t snr;
s1_t snr; // LMIC.snr is SNR time 4
frazar marked this conversation as resolved.
Show resolved Hide resolved
rps_t rps;
u1_t rxsyms;
u1_t dndr;
Expand Down Expand Up @@ -315,6 +315,7 @@ struct lmic_t {
u1_t margin;
bit_t ladrAns; // link adr adapt answer pending
bit_t devsAns; // device status answer pending
s1_t devAnsMargin; // SNR value between -32 and 31 (inclusive) for the last successfully received DevStatusReq command
u1_t adrEnabled;
u1_t moreData; // NWK has more data pending
#if !defined(DISABLE_MCMD_DCAP_REQ)
Expand Down