Skip to content

Commit

Permalink
Merge pull request #131 from frazar/set_max_margin_to_31
Browse files Browse the repository at this point in the history
Fix #130: Fix coding of the "Margin" field of the "DevStatusAns" MAC command payload
  • Loading branch information
terrillmoore authored Sep 27, 2018
2 parents 7faf65a + 99848e0 commit d9225fb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
12 changes: 9 additions & 3 deletions src/lmic/lmic.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*
* Copyright (c) 2016-2018 MCCI Corporation.
* All rights reserved.
*
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
Expand Down 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);
// 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 Expand Up @@ -1257,7 +1263,7 @@ static void buildDataFrame (void) {
if( LMIC.devsAns ) { // answer to device status
LMIC.frame[end+0] = MCMD_DEVS_ANS;
LMIC.frame[end+1] = os_getBattLevel();
LMIC.frame[end+2] = LMIC.margin;
LMIC.frame[end+2] = LMIC.devAnsMargin;
end += 3;
LMIC.devsAns = 0;
}
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 times 4
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

0 comments on commit d9225fb

Please sign in to comment.