From 8b34c4ff05eb5ce63e95a2dee671bfacc45bbca7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Tue, 31 Dec 2024 15:58:59 +0100 Subject: [PATCH] fix misc cppcheck things and compile time warnings (#5710) --- src/mesh/NodeDB.cpp | 13 +++--- .../Telemetry/Sensor/CGRadSensSensor.cpp | 3 +- .../Telemetry/Sensor/IndicatorSensor.cpp | 15 ++++--- src/platform/portduino/USBHal.h | 2 +- src/serialization/MeshPacketSerializer.cpp | 2 + src/serialization/MeshPacketSerializer.h | 1 - src/serialization/cobs.cpp | 40 +++++++++---------- src/sleep.cpp | 7 ++++ 8 files changed, 45 insertions(+), 38 deletions(-) diff --git a/src/mesh/NodeDB.cpp b/src/mesh/NodeDB.cpp index 994e59d35..bbc9eb1ea 100644 --- a/src/mesh/NodeDB.cpp +++ b/src/mesh/NodeDB.cpp @@ -992,8 +992,11 @@ void NodeDB::loadFromDisk() // Make sure we load hard coded admin keys even when the configuration file has none. // Initialize admin_key_count to zero byte numAdminKeys = 0; +#if defined(USERPREFS_USE_ADMIN_KEY_0) || defined(USERPREFS_USE_ADMIN_KEY_1) || defined(USERPREFS_USE_ADMIN_KEY_2) uint16_t sum = 0; +#endif #ifdef USERPREFS_USE_ADMIN_KEY_0 + for (uint8_t b = 0; b < 32; b++) { sum += config.security.admin_key[0].bytes[b]; } @@ -1002,8 +1005,6 @@ void NodeDB::loadFromDisk() LOG_INFO("Admin 0 key zero. Loading hard coded key from user preferences."); memcpy(config.security.admin_key[0].bytes, userprefs_admin_key_0, 32); config.security.admin_key[0].size = 32; - config.security.admin_key_count = numAdminKeys; - saveToDisk(SEGMENT_CONFIG); } #endif @@ -1017,8 +1018,6 @@ void NodeDB::loadFromDisk() LOG_INFO("Admin 1 key zero. Loading hard coded key from user preferences."); memcpy(config.security.admin_key[1].bytes, userprefs_admin_key_1, 32); config.security.admin_key[1].size = 32; - config.security.admin_key_count = numAdminKeys; - saveToDisk(SEGMENT_CONFIG); } #endif @@ -1032,10 +1031,14 @@ void NodeDB::loadFromDisk() LOG_INFO("Admin 2 key zero. Loading hard coded key from user preferences."); memcpy(config.security.admin_key[2].bytes, userprefs_admin_key_2, 32); config.security.admin_key[2].size = 32; + } +#endif + + if (numAdminKeys > 0) { + LOG_INFO("Saving %d hard coded admin keys.", numAdminKeys); config.security.admin_key_count = numAdminKeys; saveToDisk(SEGMENT_CONFIG); } -#endif state = loadProto(moduleConfigFileName, meshtastic_LocalModuleConfig_size, sizeof(meshtastic_LocalModuleConfig), &meshtastic_LocalModuleConfig_msg, &moduleConfig); diff --git a/src/modules/Telemetry/Sensor/CGRadSensSensor.cpp b/src/modules/Telemetry/Sensor/CGRadSensSensor.cpp index 5e69cc22f..ac5df1b81 100644 --- a/src/modules/Telemetry/Sensor/CGRadSensSensor.cpp +++ b/src/modules/Telemetry/Sensor/CGRadSensSensor.cpp @@ -41,13 +41,12 @@ void CGRadSensSensor::begin(TwoWire *wire, uint8_t addr) float CGRadSensSensor::getStaticRadiation() { // Read a register, following the same pattern as the RCWL9620Sensor - uint32_t data; _wire->beginTransmission(_addr); // Transfer data to addr. _wire->write(0x06); // Radiation intensity (static period T = 500 sec) if (_wire->endTransmission() == 0) { if (_wire->requestFrom(_addr, (uint8_t)3)) { ; // Request 3 bytes - data = _wire->read(); + uint32_t data = _wire->read(); data <<= 8; data |= _wire->read(); data <<= 8; diff --git a/src/modules/Telemetry/Sensor/IndicatorSensor.cpp b/src/modules/Telemetry/Sensor/IndicatorSensor.cpp index f3dcd1727..317357137 100644 --- a/src/modules/Telemetry/Sensor/IndicatorSensor.cpp +++ b/src/modules/Telemetry/Sensor/IndicatorSensor.cpp @@ -35,8 +35,8 @@ enum sensor_pkt_type { static int cmd_send(uint8_t cmd, const char *p_data, uint8_t len) { - uint8_t buf[32] = {0}; - uint8_t data[32] = {0}; + uint8_t send_buf[32] = {0}; + uint8_t send_data[32] = {0}; if (len > 31) { return -1; @@ -44,18 +44,18 @@ static int cmd_send(uint8_t cmd, const char *p_data, uint8_t len) uint8_t index = 1; - data[0] = cmd; + send_data[0] = cmd; if (len > 0 && p_data != NULL) { - memcpy(&data[1], p_data, len); + memcpy(&send_data[1], p_data, len); index += len; } - cobs_encode_result ret = cobs_encode(buf, sizeof(buf), data, index); + cobs_encode_result ret = cobs_encode(send_buf, sizeof(send_buf), send_data, index); // LOG_DEBUG("cobs TX status:%d, len:%d, type 0x%x", ret.status, ret.out_len, cmd); if (ret.status == COBS_ENCODE_OK) { - return uart_write_bytes(SENSOR_PORT_NUM, buf, ret.out_len + 1); + return uart_write_bytes(SENSOR_PORT_NUM, send_buf, ret.out_len + 1); } return -1; @@ -96,7 +96,6 @@ bool IndicatorSensor::getMetrics(meshtastic_Telemetry *measurement) int len = uart_read_bytes(SENSOR_PORT_NUM, buf, (SENSOR_BUF_SIZE - 1), 100 / portTICK_PERIOD_MS); float value = 0.0; - uint8_t pkt_type = 0; uint8_t *p_buf_start = buf; uint8_t *p_buf_end = buf; if (len > 0) { @@ -117,7 +116,7 @@ bool IndicatorSensor::getMetrics(meshtastic_Telemetry *measurement) if (ret.out_len > 1 && ret.status == COBS_DECODE_OK) { value = 0.0; - pkt_type = data[0]; + uint8_t pkt_type = data[0]; switch (pkt_type) { case PKT_TYPE_SENSOR_SCD41_CO2: { memcpy(&value, &data[1], sizeof(value)); diff --git a/src/platform/portduino/USBHal.h b/src/platform/portduino/USBHal.h index 2b0302ced..064f7ae36 100644 --- a/src/platform/portduino/USBHal.h +++ b/src/platform/portduino/USBHal.h @@ -26,7 +26,7 @@ class Ch341Hal : public RadioLibHal { public: // default constructor - initializes the base HAL and any needed private members - Ch341Hal(uint8_t spiChannel, uint32_t spiSpeed = 2000000, uint8_t spiDevice = 0, uint8_t gpioDevice = 0) + explicit Ch341Hal(uint8_t spiChannel, uint32_t spiSpeed = 2000000, uint8_t spiDevice = 0, uint8_t gpioDevice = 0) : RadioLibHal(PI_INPUT, PI_OUTPUT, PI_LOW, PI_HIGH, PI_RISING, PI_FALLING) { } diff --git a/src/serialization/MeshPacketSerializer.cpp b/src/serialization/MeshPacketSerializer.cpp index b4603186b..2f0d881f2 100644 --- a/src/serialization/MeshPacketSerializer.cpp +++ b/src/serialization/MeshPacketSerializer.cpp @@ -13,6 +13,8 @@ #include "mesh/generated/meshtastic/remote_hardware.pb.h" #include +static const char *errStr = "Error decoding proto for %s message!"; + std::string MeshPacketSerializer::JsonSerialize(const meshtastic_MeshPacket *mp, bool shouldLog) { // the created jsonObj is immutable after creation, so diff --git a/src/serialization/MeshPacketSerializer.h b/src/serialization/MeshPacketSerializer.h index 12efccb43..03860ab35 100644 --- a/src/serialization/MeshPacketSerializer.h +++ b/src/serialization/MeshPacketSerializer.h @@ -2,7 +2,6 @@ #include static const char hexChars[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; -static const char *errStr = "Error decoding proto for %s message!"; class MeshPacketSerializer { diff --git a/src/serialization/cobs.cpp b/src/serialization/cobs.cpp index 39ea019fd..afb868f50 100644 --- a/src/serialization/cobs.cpp +++ b/src/serialization/cobs.cpp @@ -5,21 +5,22 @@ cobs_encode_result cobs_encode(uint8_t *dst_buf_ptr, size_t dst_buf_len, const uint8_t *src_ptr, size_t src_len) { + cobs_encode_result result = {0, COBS_ENCODE_OK}; + + if (!dst_buf_ptr || !src_ptr) { + result.status = COBS_ENCODE_NULL_POINTER; + return result; + } + const uint8_t *src_read_ptr = src_ptr; const uint8_t *src_end_ptr = src_read_ptr + src_len; uint8_t *dst_buf_start_ptr = dst_buf_ptr; uint8_t *dst_buf_end_ptr = dst_buf_start_ptr + dst_buf_len; uint8_t *dst_code_write_ptr = dst_buf_ptr; uint8_t *dst_write_ptr = dst_code_write_ptr + 1; - uint8_t src_byte = 0; uint8_t search_len = 1; - if ((dst_buf_ptr == NULL) || (src_ptr == NULL)) { - result.status = COBS_ENCODE_NULL_POINTER; - return result; - } - if (src_len != 0) { for (;;) { if (dst_write_ptr >= dst_buf_end_ptr) { @@ -27,7 +28,7 @@ cobs_encode_result cobs_encode(uint8_t *dst_buf_ptr, size_t dst_buf_len, const u break; } - src_byte = *src_read_ptr++; + uint8_t src_byte = *src_read_ptr++; if (src_byte == 0) { *dst_code_write_ptr = search_len; dst_code_write_ptr = dst_write_ptr++; @@ -65,31 +66,28 @@ cobs_encode_result cobs_encode(uint8_t *dst_buf_ptr, size_t dst_buf_len, const u cobs_decode_result cobs_decode(uint8_t *dst_buf_ptr, size_t dst_buf_len, const uint8_t *src_ptr, size_t src_len) { cobs_decode_result result = {0, COBS_DECODE_OK}; - const uint8_t *src_read_ptr = src_ptr; - const uint8_t *src_end_ptr = src_read_ptr + src_len; - uint8_t *dst_buf_start_ptr = dst_buf_ptr; - uint8_t *dst_buf_end_ptr = dst_buf_start_ptr + dst_buf_len; - uint8_t *dst_write_ptr = dst_buf_ptr; - size_t remaining_bytes; - uint8_t src_byte; - uint8_t i; - uint8_t len_code; - if ((dst_buf_ptr == NULL) || (src_ptr == NULL)) { + if (!dst_buf_ptr || !src_ptr) { result.status = COBS_DECODE_NULL_POINTER; return result; } + const uint8_t *src_read_ptr = src_ptr; + const uint8_t *src_end_ptr = src_read_ptr + src_len; + uint8_t *dst_buf_start_ptr = dst_buf_ptr; + const uint8_t *dst_buf_end_ptr = dst_buf_start_ptr + dst_buf_len; + uint8_t *dst_write_ptr = dst_buf_ptr; + if (src_len != 0) { for (;;) { - len_code = *src_read_ptr++; + uint8_t len_code = *src_read_ptr++; if (len_code == 0) { result.status = (cobs_decode_status)(result.status | (cobs_decode_status)COBS_DECODE_ZERO_BYTE_IN_INPUT); break; } len_code--; - remaining_bytes = src_end_ptr - src_read_ptr; + size_t remaining_bytes = src_end_ptr - src_read_ptr; if (len_code > remaining_bytes) { result.status = (cobs_decode_status)(result.status | (cobs_decode_status)COBS_DECODE_INPUT_TOO_SHORT); len_code = remaining_bytes; @@ -101,8 +99,8 @@ cobs_decode_result cobs_decode(uint8_t *dst_buf_ptr, size_t dst_buf_len, const u len_code = remaining_bytes; } - for (i = len_code; i != 0; i--) { - src_byte = *src_read_ptr++; + for (uint8_t i = len_code; i != 0; i--) { + uint8_t src_byte = *src_read_ptr++; if (src_byte == 0) { result.status = (cobs_decode_status)(result.status | (cobs_decode_status)COBS_DECODE_ZERO_BYTE_IN_INPUT); } diff --git a/src/sleep.cpp b/src/sleep.cpp index 69eb0349a..5f1909a77 100644 --- a/src/sleep.cpp +++ b/src/sleep.cpp @@ -271,6 +271,12 @@ void doDeepSleep(uint32_t msecToWake, bool skipPreflight = false, bool skipSaveN gpio_hold_en((gpio_num_t)BUTTON_PIN); } #endif +#ifdef SENSECAP_INDICATOR + // Portexpander definition does not pass GPIO_IS_VALID_OUTPUT_GPIO + pinMode(LORA_CS, OUTPUT); + digitalWrite(LORA_CS, HIGH); + gpio_hold_en((gpio_num_t)LORA_CS); +#else if (GPIO_IS_VALID_OUTPUT_GPIO(LORA_CS)) { // LoRa CS (RADIO_NSS) needs to stay HIGH, even during deep sleep pinMode(LORA_CS, OUTPUT); @@ -278,6 +284,7 @@ void doDeepSleep(uint32_t msecToWake, bool skipPreflight = false, bool skipSaveN gpio_hold_en((gpio_num_t)LORA_CS); } #endif +#endif #ifdef HAS_PMU if (pmu_found && PMU) {