From 3952fe9139235653c21da478c7cc4232a824d8b5 Mon Sep 17 00:00:00 2001 From: jgromes Date: Sat, 26 Oct 2024 16:59:13 +0100 Subject: [PATCH] Use pointer assert --- src/modules/LR11x0/LR11x0.cpp | 79 +++++-------------- src/modules/SX126x/SX126x.cpp | 4 +- src/modules/SX127x/SX127x.cpp | 4 +- src/modules/SX128x/SX128x.cpp | 4 +- src/protocols/Pager/Pager.cpp | 4 +- src/protocols/PhysicalLayer/PhysicalLayer.cpp | 8 +- 6 files changed, 27 insertions(+), 76 deletions(-) diff --git a/src/modules/LR11x0/LR11x0.cpp b/src/modules/LR11x0/LR11x0.cpp index 03d32d04..2f97cc8f 100644 --- a/src/modules/LR11x0/LR11x0.cpp +++ b/src/modules/LR11x0/LR11x0.cpp @@ -1571,9 +1571,7 @@ int16_t LR11x0::getWifiScanResultsCount(uint8_t* count) { } int16_t LR11x0::getWifiScanResult(LR11x0WifiResult_t* result, uint8_t index, bool brief) { - if(!result) { - return(RADIOLIB_ERR_MEMORY_ALLOCATION_FAILED); - } + RADIOLIB_ASSERT_PTR(result); // read a single result uint8_t format = brief ? RADIOLIB_LR11X0_WIFI_RESULT_TYPE_BASIC : RADIOLIB_LR11X0_WIFI_RESULT_TYPE_COMPLETE; @@ -1647,9 +1645,7 @@ int16_t LR11x0::getWifiScanResult(LR11x0WifiResult_t* result, uint8_t index, boo } int16_t LR11x0::wifiScan(uint8_t wifiType, uint8_t* count, uint8_t mode, uint16_t chanMask, uint8_t numScans, uint16_t timeout) { - if(!count) { - return(RADIOLIB_ERR_MEMORY_ALLOCATION_FAILED); - } + RADIOLIB_ASSERT_PTR(count); // start scan RADIOLIB_DEBUG_BASIC_PRINTLN("WiFi scan start"); @@ -1674,9 +1670,7 @@ int16_t LR11x0::wifiScan(uint8_t wifiType, uint8_t* count, uint8_t mode, uint16_ } int16_t LR11x0::getVersionInfo(LR11x0VersionInfo_t* info) { - if(!info) { - return(RADIOLIB_ERR_MEMORY_ALLOCATION_FAILED); - } + RADIOLIB_ASSERT_PTR(info); int16_t state = this->getVersion(&info->hardware, &info->device, &info->fwMajor, &info->fwMinor); RADIOLIB_ASSERT(state); @@ -1696,9 +1690,7 @@ int16_t LR11x0::getVersionInfo(LR11x0VersionInfo_t* info) { } int16_t LR11x0::updateFirmware(const uint32_t* image, size_t size, bool nonvolatile) { - if(!image) { - return(RADIOLIB_ERR_MEMORY_ALLOCATION_FAILED); - } + RADIOLIB_ASSERT_PTR(image); // put the device to bootloader mode int16_t state = this->reboot(true); @@ -1802,9 +1794,7 @@ int16_t LR11x0::isGnssScanCapable() { } int16_t LR11x0::gnssScan(LR11x0GnssResult_t* res) { - if(!res) { - return(RADIOLIB_ERR_MEMORY_ALLOCATION_FAILED); - } + RADIOLIB_ASSERT_PTR(res); // go to standby int16_t state = standby(); @@ -1872,9 +1862,7 @@ int16_t LR11x0::gnssScan(LR11x0GnssResult_t* res) { } int16_t LR11x0::getGnssAlmanacStatus(LR11x0GnssAlmanacStatus_t *stat) { - if(!stat) { - return(RADIOLIB_ERR_MEMORY_ALLOCATION_FAILED); - } + RADIOLIB_ASSERT_PTR(stat); // save the time the time until subframe is relative to stat->start = this->mod->hal->millis(); @@ -1913,9 +1901,7 @@ int16_t LR11x0::getGnssAlmanacStatus(LR11x0GnssAlmanacStatus_t *stat) { } int16_t LR11x0::gnssDelayUntilSubframe(LR11x0GnssAlmanacStatus_t *stat, uint8_t constellation) { - if(!stat) { - return(RADIOLIB_ERR_MEMORY_ALLOCATION_FAILED); - } + RADIOLIB_ASSERT_PTR(stat); // almanac update has to be called at least 1.3 seconds before the subframe // we use 2.3 seconds to be on the safe side @@ -1969,9 +1955,7 @@ int16_t LR11x0::updateGnssAlmanac(uint8_t constellation) { } int16_t LR11x0::getGnssPosition(LR11x0GnssPosition_t* pos, bool filtered) { - if(!pos) { - return(RADIOLIB_ERR_MEMORY_ALLOCATION_FAILED); - } + RADIOLIB_ASSERT_PTR(pos); uint8_t error = 0; int16_t state; @@ -1991,7 +1975,8 @@ int16_t LR11x0::getGnssPosition(LR11x0GnssPosition_t* pos, bool filtered) { } int16_t LR11x0::getGnssSatellites(LR11x0GnssSatellite_t* sats, uint8_t numSats) { - if((!sats) || (numSats >= 32)) { + RADIOLIB_ASSERT_PTR(sats); + if(numSats >= 32) { return(RADIOLIB_ERR_MEMORY_ALLOCATION_FAILED); } @@ -2010,9 +1995,7 @@ int16_t LR11x0::getGnssSatellites(LR11x0GnssSatellite_t* sats, uint8_t numSats) } int16_t LR11x0::getModem(ModemType_t* modem) { - if(!modem) { - return(RADIOLIB_ERR_MEMORY_ALLOCATION_FAILED); - } + RADIOLIB_ASSERT_PTR(modem); uint8_t packetType = RADIOLIB_LR11X0_PACKET_TYPE_NONE; int16_t state = getPacketType(&packetType); @@ -2577,23 +2560,17 @@ int16_t LR11x0::readInfoPage(uint16_t addr, uint32_t* data, size_t len) { } int16_t LR11x0::getChipEui(uint8_t* eui) { - if(!eui) { - return(RADIOLIB_ERR_MEMORY_ALLOCATION_FAILED); - } + RADIOLIB_ASSERT_PTR(eui); return(this->SPIcommand(RADIOLIB_LR11X0_CMD_GET_CHIP_EUI, false, eui, RADIOLIB_LR11X0_EUI_LEN)); } int16_t LR11x0::getSemtechJoinEui(uint8_t* eui) { - if(!eui) { - return(RADIOLIB_ERR_MEMORY_ALLOCATION_FAILED); - } + RADIOLIB_ASSERT_PTR(eui); return(this->SPIcommand(RADIOLIB_LR11X0_CMD_GET_SEMTECH_JOIN_EUI, false, eui, RADIOLIB_LR11X0_EUI_LEN)); } int16_t LR11x0::deriveRootKeysAndGetPin(uint8_t* pin) { - if(!pin) { - return(RADIOLIB_ERR_MEMORY_ALLOCATION_FAILED); - } + RADIOLIB_ASSERT_PTR(pin); return(this->SPIcommand(RADIOLIB_LR11X0_CMD_DERIVE_ROOT_KEYS_AND_GET_PIN, false, pin, RADIOLIB_LR11X0_PIN_LEN)); } @@ -2689,9 +2666,7 @@ int16_t LR11x0::getRssiInst(float* rssi) { } int16_t LR11x0::setGfskSyncWord(uint8_t* sync) { - if(!sync) { - return(RADIOLIB_ERR_MEMORY_ALLOCATION_FAILED); - } + RADIOLIB_ASSERT_PTR(sync); return(this->SPIcommand(RADIOLIB_LR11X0_CMD_SET_GFSK_SYNC_WORD, true, sync, RADIOLIB_LR11X0_GFSK_SYNC_WORD_LEN)); } @@ -3340,9 +3315,7 @@ int16_t LR11x0::gnssGetResultSize(uint16_t* size) { } int16_t LR11x0::gnssReadResults(uint8_t* result, uint16_t size) { - if(!result) { - return(RADIOLIB_ERR_MEMORY_ALLOCATION_FAILED); - } + RADIOLIB_ASSERT_PTR(result); return(this->SPIcommand(RADIOLIB_LR11X0_CMD_GNSS_READ_RESULTS, false, result, size)); } @@ -3639,9 +3612,7 @@ void LR11x0::gnssAbort() { } int16_t LR11x0::cryptoSetKey(uint8_t keyId, uint8_t* key) { - if(!key) { - return(RADIOLIB_ERR_MEMORY_ALLOCATION_FAILED); - } + RADIOLIB_ASSERT_PTR(key); uint8_t buff[1 + RADIOLIB_AES128_KEY_SIZE] = { 0 }; buff[0] = keyId; memcpy(&buff[1], key, RADIOLIB_AES128_KEY_SIZE); @@ -3649,9 +3620,7 @@ int16_t LR11x0::cryptoSetKey(uint8_t keyId, uint8_t* key) { } int16_t LR11x0::cryptoDeriveKey(uint8_t srcKeyId, uint8_t dstKeyId, uint8_t* key) { - if(!key) { - return(RADIOLIB_ERR_MEMORY_ALLOCATION_FAILED); - } + RADIOLIB_ASSERT_PTR(key); uint8_t buff[2 + RADIOLIB_AES128_KEY_SIZE] = { 0 }; buff[0] = srcKeyId; buff[1] = dstKeyId; @@ -3849,23 +3818,17 @@ int16_t LR11x0::bootReboot(bool stay) { } int16_t LR11x0::bootGetPin(uint8_t* pin) { - if(!pin) { - return(RADIOLIB_ERR_MEMORY_ALLOCATION_FAILED); - } + RADIOLIB_ASSERT_PTR(pin); return(this->SPIcommand(RADIOLIB_LR11X0_CMD_BOOT_GET_PIN, false, pin, RADIOLIB_LR11X0_PIN_LEN)); } int16_t LR11x0::bootGetChipEui(uint8_t* eui) { - if(!eui) { - return(RADIOLIB_ERR_MEMORY_ALLOCATION_FAILED); - } + RADIOLIB_ASSERT_PTR(eui); return(this->SPIcommand(RADIOLIB_LR11X0_CMD_BOOT_GET_CHIP_EUI, false, eui, RADIOLIB_LR11X0_EUI_LEN)); } int16_t LR11x0::bootGetJoinEui(uint8_t* eui) { - if(!eui) { - return(RADIOLIB_ERR_MEMORY_ALLOCATION_FAILED); - } + RADIOLIB_ASSERT_PTR(eui); return(this->SPIcommand(RADIOLIB_LR11X0_CMD_BOOT_GET_JOIN_EUI, false, eui, RADIOLIB_LR11X0_EUI_LEN)); } diff --git a/src/modules/SX126x/SX126x.cpp b/src/modules/SX126x/SX126x.cpp index 135ed05c..46c744e3 100644 --- a/src/modules/SX126x/SX126x.cpp +++ b/src/modules/SX126x/SX126x.cpp @@ -1663,9 +1663,7 @@ int16_t SX126x::invertIQ(bool enable) { } int16_t SX126x::getModem(ModemType_t* modem) { - if(!modem) { - return(RADIOLIB_ERR_MEMORY_ALLOCATION_FAILED); - } + RADIOLIB_ASSERT_PTR(modem); uint8_t packetType = getPacketType(); switch(packetType) { diff --git a/src/modules/SX127x/SX127x.cpp b/src/modules/SX127x/SX127x.cpp index 425b1cb1..a8c61227 100644 --- a/src/modules/SX127x/SX127x.cpp +++ b/src/modules/SX127x/SX127x.cpp @@ -1755,9 +1755,7 @@ int16_t SX127x::invertIQ(bool enable) { } int16_t SX127x::getModem(ModemType_t* modem) { - if(!modem) { - return(RADIOLIB_ERR_MEMORY_ALLOCATION_FAILED); - } + RADIOLIB_ASSERT_PTR(modem); int16_t packetType = getActiveModem(); switch(packetType) { diff --git a/src/modules/SX128x/SX128x.cpp b/src/modules/SX128x/SX128x.cpp index 5847f3d6..7a5fc20b 100644 --- a/src/modules/SX128x/SX128x.cpp +++ b/src/modules/SX128x/SX128x.cpp @@ -870,9 +870,7 @@ int16_t SX128x::setModem(ModemType_t modem) { } int16_t SX128x::getModem(ModemType_t* modem) { - if(!modem) { - return(RADIOLIB_ERR_MEMORY_ALLOCATION_FAILED); - } + RADIOLIB_ASSERT_PTR(modem); uint8_t packetType = getPacketType(); switch(packetType) { diff --git a/src/protocols/Pager/Pager.cpp b/src/protocols/Pager/Pager.cpp index e4914dd6..9d56ea40 100644 --- a/src/protocols/Pager/Pager.cpp +++ b/src/protocols/Pager/Pager.cpp @@ -316,9 +316,7 @@ int16_t PagerClient::readData(String& str, size_t len, uint32_t* addr) { uint8_t data[RADIOLIB_STATIC_ARRAY_SIZE + 1]; #else uint8_t* data = new uint8_t[length + 1]; - if(!data) { - return(RADIOLIB_ERR_MEMORY_ALLOCATION_FAILED); - } + RADIOLIB_ASSERT_PTR(data); #endif // read the received data diff --git a/src/protocols/PhysicalLayer/PhysicalLayer.cpp b/src/protocols/PhysicalLayer/PhysicalLayer.cpp index 1ad04e69..2872cca3 100644 --- a/src/protocols/PhysicalLayer/PhysicalLayer.cpp +++ b/src/protocols/PhysicalLayer/PhysicalLayer.cpp @@ -78,9 +78,7 @@ int16_t PhysicalLayer::receive(String& str, size_t len) { } else { data = new uint8_t[length + 1]; } - if(!data) { - return(RADIOLIB_ERR_MEMORY_ALLOCATION_FAILED); - } + RADIOLIB_ASSERT_PTR(data); #endif // attempt packet reception @@ -180,9 +178,7 @@ int16_t PhysicalLayer::readData(String& str, size_t len) { uint8_t data[RADIOLIB_STATIC_ARRAY_SIZE + 1]; #else uint8_t* data = new uint8_t[length + 1]; - if(!data) { - return(RADIOLIB_ERR_MEMORY_ALLOCATION_FAILED); - } + RADIOLIB_ASSERT_PTR(data); #endif // read the received data