From 352e6fbd234e047fe67e7c742cc5685c4ec5f97c Mon Sep 17 00:00:00 2001 From: Mikhail Isaev Date: Wed, 24 Nov 2021 21:56:42 +0300 Subject: [PATCH] Fix overflow at extremely low RSSI Some LoRaWAN modem like SX1272 can receive downlink packets with RSSI level less than -127. So "int8_t" is not enough for store all possible RSSI values. For example, SX1272 has sensitivity at -137 dBm. Problem was manifested in the file "SX1272_LoRaRadio.cpp" at SX1272_LoRaRadio::handle_dio0_irq() method. When value of _rf_settings.lora_packet_handler.rssi_value calculated incorrect RSSI will be stored. Example case: Value readen from register REG_LR_PKTSNRVALUE _rf_settings.lora_packet_handler.snr_value equals -47. Value readen from register REG_LR_PKTRSSIVALUE equals 17. RSSI_OFFSET equals "-139", snr equals "-11". For case MODEM_LORA value calculated by formula: _rf_settings.lora_packet_handler.rssi_value = RSSI_OFFSET + rssi + (rssi >> 4) + snr; and result value will be "124" because of int8_t overflow so it's not correct value (too high). Correct value must be: -139 + 17 + (1) + (-11) = -132. Another motivation: at all other places int16_t type used to store RSSI value. --- connectivity/lorawan/include/lorawan/LoRaRadio.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/connectivity/lorawan/include/lorawan/LoRaRadio.h b/connectivity/lorawan/include/lorawan/LoRaRadio.h index 601c6b9e52..f6673399ac 100644 --- a/connectivity/lorawan/include/lorawan/LoRaRadio.h +++ b/connectivity/lorawan/include/lorawan/LoRaRadio.h @@ -200,7 +200,7 @@ typedef struct radio_fsk_packet_handler { /** * Storage for RSSI value of the received signal. */ - int8_t rssi_value; + int16_t rssi_value; /** * Automated frequency correction value. @@ -333,7 +333,7 @@ typedef struct radio_lora_packet_handler { /** * RSSI value in dBm for the received packet. */ - int8_t rssi_value; + int16_t rssi_value; /** * Size of the transmitted or received packet.