Fix unsigned conversion of negative TX power in CommonRadioPrefs

This commit is contained in:
rado committed 2026-10-17 09:36:59 +02:00
1 parent 428a95c643
commit 85ad886589
4 files changed
+63 -6

No files matched your search

+2 -2
View File
@@ -102,8 +102,8 @@ private:
void setIntThresh(uint8_t t) override { _parent->interference_threshold = t; markDirty(); }
uint8_t getRxGain() const override { return _parent->rx_boosted_gain; }
void setRxGain(uint8_t g) override { _parent->rx_boosted_gain = g; markDirty(); }
uint8_t getTxPower() const override { return _parent->tx_power_dbm; }
void setTxPower(uint8_t dbm) override { _parent->tx_power_dbm = dbm; markDirty(); }
int8_t getTxPower() const override { return _parent->tx_power_dbm; }
void setTxPower(int8_t dbm) override { _parent->tx_power_dbm = dbm; markDirty(); }
float getRxDelay() const override { return _parent->rx_delay_base; }
void setRxDelay(float d) override { _parent->rx_delay_base = d; markDirty(); }
uint16_t getAgcResetInt() const override { return _parent->agc_reset_interval * 4; }
+2 -2
View File
@@ -115,8 +115,8 @@ private:
void setIntThresh(uint8_t t) override { _parent->interference_threshold = t; markDirty(); }
uint8_t getRxGain() const override { return _parent->rx_boosted_gain; }
void setRxGain(uint8_t g) override { _parent->rx_boosted_gain = g; markDirty(); }
uint8_t getTxPower() const override { return _parent->tx_power_dbm; }
void setTxPower(uint8_t dbm) override { _parent->tx_power_dbm = dbm; markDirty(); }
int8_t getTxPower() const override { return _parent->tx_power_dbm; }
void setTxPower(int8_t dbm) override { _parent->tx_power_dbm = dbm; markDirty(); }
float getRxDelay() const override { return _parent->rx_delay_base; }
void setRxDelay(float d) override { _parent->rx_delay_base = d; markDirty(); }
uint16_t getAgcResetInt() const override { return _parent->agc_reset_interval * 4; }
+2 -2
View File
@@ -35,8 +35,8 @@ public:
virtual uint8_t getRxGain() const = 0;
virtual void setRxGain(uint8_t g) = 0;
virtual uint8_t getTxPower() const = 0;
virtual void setTxPower(uint8_t dbm) = 0;
virtual int8_t getTxPower() const = 0;
virtual void setTxPower(int8_t dbm) = 0;
virtual float getRxDelay() const = 0;
virtual void setRxDelay(float d) = 0;
@@ -6,6 +6,32 @@
#include "../../examples/companion_radio/NodePrefs.h"
bool CommonRadioPrefs::getByKey(const char* key, char* value, size_t max_len) {
if (strcmp(key, "fem_rxgain") == 0) {
snprintf(value, max_len, "%d", (uint32_t)getFEMRxGain());
return true;
}
if (strcmp(key, "fem_txgain") == 0) {
snprintf(value, max_len, "%d", (uint32_t)getFEMTxGain());
return true;
}
return false;
}
bool CommonRadioPrefs::setByKey(const char* key, const char* value) {
if (strcmp(key, "fem_rxgain") == 0) {
setFEMRxGain(atoi(value));
markDirty();
return true;
}
if (strcmp(key, "fem_txgain") == 0) {
setFEMTxGain(atoi(value));
markDirty();
return true;
}
return false;
}
class ReplayStream : public Stream {
const char* _text;
int _pos = 0;
@@ -77,6 +103,37 @@ TEST(CompanionNodePrefs, RxGainSettingsRoundTripIndependently) {
}
#endif
TEST(CompanionNodePrefs, NegativeTxPowerPreservedAsSigned) {
NodePrefs prefs;
CommonRadioPrefs* radio = prefs.getRadioPrefs();
radio->setTxPower(-9);
EXPECT_EQ(-9, prefs.tx_power_dbm);
EXPECT_EQ(-9, radio->getTxPower());
EXPECT_EQ(-9, static_cast<int32_t>(radio->getTxPower()));
char reply[32];
snprintf(reply, sizeof(reply), "> %d", static_cast<int32_t>(radio->getTxPower()));
EXPECT_STREQ("> -9", reply);
}
TEST(CompanionNodePrefs, NegativeTxPowerRoundTripsSerial) {
NodePrefs saved;
saved.getRadioPrefs()->setTxPower(-9);
CaptureStream output;
ASSERT_TRUE(saved.saveSerial(output));
EXPECT_NE(std::string::npos, output.text().find("tx:-9"));
ReplayStream input("{radio:{tx:-9}}");
NodePrefs loaded;
loaded.tx_power_dbm = 22;
ASSERT_TRUE(loaded.loadSerial(input));
EXPECT_EQ(-9, loaded.tx_power_dbm);
EXPECT_EQ(-9, loaded.getRadioPrefs()->getTxPower());
}
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();