From cc0d69194ed9fd5fef9e094ae621decee815d732 Mon Sep 17 00:00:00 2001 From: Francesco Zardi Date: Mon, 24 Sep 2018 23:08:21 +0200 Subject: [PATCH 1/9] Change the maxium allowed value of margin from 254 to 31 --- src/lmic/lmic.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lmic/lmic.c b/src/lmic/lmic.c index f5ff4220..5d999a4d 100644 --- a/src/lmic/lmic.c +++ b/src/lmic/lmic.c @@ -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 @@ -840,7 +840,7 @@ static bit_t decodeFrame (void) { // Process OPTS int m = LMIC.rssi - RSSI_OFF - getSensitivity(LMIC.rps); - LMIC.margin = m < 0 ? 0 : m > 254 ? 254 : m; + LMIC.margin = m < 0 ? 0 : m > 31 ? 31 : m; #if LMIC_DEBUG_LEVEL > 0 LMIC_DEBUG_PRINTF("%lu: process options (olen=%#x)\n", os_getTime(), olen); From 62a8177b5e644410b34a589ceb9a0316b5c3d373 Mon Sep 17 00:00:00 2001 From: Francesco Zardi Date: Tue, 25 Sep 2018 07:43:22 +0200 Subject: [PATCH 2/9] Revert "Change the maxium allowed value of margin from 254 to 31" This reverts commit cc0d69194ed9fd5fef9e094ae621decee815d732. --- src/lmic/lmic.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lmic/lmic.c b/src/lmic/lmic.c index 5d999a4d..f5ff4220 100644 --- a/src/lmic/lmic.c +++ b/src/lmic/lmic.c @@ -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 @@ -840,7 +840,7 @@ static bit_t decodeFrame (void) { // Process OPTS int m = LMIC.rssi - RSSI_OFF - getSensitivity(LMIC.rps); - LMIC.margin = m < 0 ? 0 : m > 31 ? 31 : m; + LMIC.margin = m < 0 ? 0 : m > 254 ? 254 : m; #if LMIC_DEBUG_LEVEL > 0 LMIC_DEBUG_PRINTF("%lu: process options (olen=%#x)\n", os_getTime(), olen); From 5a0119b88db8a895413ca039211c0e19a636999a Mon Sep 17 00:00:00 2001 From: Francesco Zardi Date: Tue, 25 Sep 2018 07:46:01 +0200 Subject: [PATCH 3/9] Move comment to correct line --- src/lmic/lmic.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lmic/lmic.c b/src/lmic/lmic.c index f5ff4220..a62b4360 100644 --- a/src/lmic/lmic.c +++ b/src/lmic/lmic.c @@ -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 @@ -838,11 +838,11 @@ 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 > 254 ? 254 : m; #if LMIC_DEBUG_LEVEL > 0 + // Process OPTS LMIC_DEBUG_PRINTF("%lu: process options (olen=%#x)\n", os_getTime(), olen); #endif From 4ff49b29889946d2d0703416f752a969f7e2a4c0 Mon Sep 17 00:00:00 2001 From: Francesco Zardi Date: Tue, 25 Sep 2018 08:19:53 +0200 Subject: [PATCH 4/9] Add the `devsAnsMargin` field to the `lmic_t` struct` --- src/lmic/lmic.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lmic/lmic.h b/src/lmic/lmic.h index 8bdb8e17..20062bb2 100644 --- a/src/lmic/lmic.h +++ b/src/lmic/lmic.h @@ -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) From f619fd1ed64c9a886f1360efc0cc79503d5fa35c Mon Sep 17 00:00:00 2001 From: Francesco Zardi Date: Tue, 25 Sep 2018 08:20:33 +0200 Subject: [PATCH 5/9] Update `devsAnsMargin` when receiving a 'DevStatusReq' command --- src/lmic/lmic.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/lmic/lmic.c b/src/lmic/lmic.c index a62b4360..df093b0c 100644 --- a/src/lmic/lmic.c +++ b/src/lmic/lmic.c @@ -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.devsAnsMargin = (u1_t)(0b00111111 & (snr <= -32 ? -32 : snr >= 31 ? 31 : snr)); oidx += 1; continue; } From 95ea3c04d0a80da96f5fc760d322f26659bd7ea4 Mon Sep 17 00:00:00 2001 From: Francesco Zardi Date: Tue, 25 Sep 2018 08:20:57 +0200 Subject: [PATCH 6/9] Add comments --- src/lmic/lmic.c | 2 ++ src/lmic/lmic.h | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/lmic/lmic.c b/src/lmic/lmic.c index df093b0c..62024246 100644 --- a/src/lmic/lmic.c +++ b/src/lmic/lmic.c @@ -843,6 +843,8 @@ static bit_t decodeFrame (void) { LMIC.adrAckReq = LINK_CHECK_INIT; 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 diff --git a/src/lmic/lmic.h b/src/lmic/lmic.h index 20062bb2..a6655e9f 100644 --- a/src/lmic/lmic.h +++ b/src/lmic/lmic.h @@ -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 rps_t rps; u1_t rxsyms; u1_t dndr; From 071be04bb4d14a0121db7c906d2133620d7eda51 Mon Sep 17 00:00:00 2001 From: Francesco Zardi Date: Tue, 25 Sep 2018 08:35:05 +0200 Subject: [PATCH 7/9] Fix typo --- src/lmic/lmic.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lmic/lmic.c b/src/lmic/lmic.c index 62024246..9c69f1c4 100644 --- a/src/lmic/lmic.c +++ b/src/lmic/lmic.c @@ -612,7 +612,7 @@ scan_mac_cmds( // 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.devsAnsMargin = (u1_t)(0b00111111 & (snr <= -32 ? -32 : snr >= 31 ? 31 : snr)); + LMIC.devAnsMargin = (u1_t)(0b00111111 & (snr <= -32 ? -32 : snr >= 31 ? 31 : snr)); oidx += 1; continue; } From c79676d8f6153c0f4fead8e92e586ca4e12f330a Mon Sep 17 00:00:00 2001 From: Francesco Zardi Date: Wed, 26 Sep 2018 08:26:45 +0200 Subject: [PATCH 8/9] Set the correct value for margin --- src/lmic/lmic.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lmic/lmic.c b/src/lmic/lmic.c index 9c69f1c4..c6c0a867 100644 --- a/src/lmic/lmic.c +++ b/src/lmic/lmic.c @@ -1263,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; } From 99848e06d5d87acfc9d9a43e40798030a46b0c18 Mon Sep 17 00:00:00 2001 From: Francesco Zardi Date: Wed, 26 Sep 2018 08:26:55 +0200 Subject: [PATCH 9/9] Fix typo --- src/lmic/lmic.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lmic/lmic.h b/src/lmic/lmic.h index a6655e9f..fc896ef8 100644 --- a/src/lmic/lmic.h +++ b/src/lmic/lmic.h @@ -253,7 +253,7 @@ struct lmic_t { u4_t freq; s1_t rssi; - s1_t snr; // LMIC.snr is SNR time 4 + s1_t snr; // LMIC.snr is SNR times 4 rps_t rps; u1_t rxsyms; u1_t dndr;