From 67c4dcf6b231fb8ba466671e69174747bd1f616d Mon Sep 17 00:00:00 2001 From: Clemens Sutor Date: Tue, 5 Nov 2024 07:36:52 +0100 Subject: [PATCH 1/2] Fix for some cppcheck findings --- EleksTubeHAX_pio/src/Buttons.h | 2 +- EleksTubeHAX_pio/src/ChipSelect.h | 35 ++++++++++++++--------------- EleksTubeHAX_pio/src/Clock.cpp | 4 ++-- EleksTubeHAX_pio/src/Menu.h | 2 +- EleksTubeHAX_pio/src/StoredConfig.h | 2 +- EleksTubeHAX_pio/src/TFTs.cpp | 12 +++++----- EleksTubeHAX_pio/src/WiFi_WPS.cpp | 13 +++++------ EleksTubeHAX_pio/src/main.cpp | 3 +-- 8 files changed, 34 insertions(+), 39 deletions(-) diff --git a/EleksTubeHAX_pio/src/Buttons.h b/EleksTubeHAX_pio/src/Buttons.h index 86a44ec..7de158d 100644 --- a/EleksTubeHAX_pio/src/Buttons.h +++ b/EleksTubeHAX_pio/src/Buttons.h @@ -14,7 +14,7 @@ class Button { public: - Button(uint8_t bpin, uint8_t active_state=LOW, uint32_t long_press_ms=500) + explicit Button(uint8_t bpin, uint8_t active_state=LOW, uint32_t long_press_ms=500) : bpin(bpin), active_state(active_state), long_press_ms(long_press_ms), down_last_time(false), state_changed(false), millis_at_last_transition(0), button_state(idle) {} diff --git a/EleksTubeHAX_pio/src/ChipSelect.h b/EleksTubeHAX_pio/src/ChipSelect.h index be849ac..8ad41b7 100644 --- a/EleksTubeHAX_pio/src/ChipSelect.h +++ b/EleksTubeHAX_pio/src/ChipSelect.h @@ -18,26 +18,26 @@ class ChipSelect { // So 0 is disabled, 1 is enabled (even though CS is active low, this gets mapped.) // So bit 0 (LSB), is index 0, is SECONDS_ONES // Translation to what the 74HC595 uses is done in update() - void setDigitMap(uint8_t map, bool update_=true) { digits_map = map; if (update_) update(); } - uint8_t getDigitMap() { return digits_map; } + void setDigitMap(uint8_t map, bool update_=true) { digits_map = map; if (update_) update(); } + uint8_t getDigitMap() { return digits_map; } // Helper functions // Sets just the one digit by digit number - void setDigit(uint8_t digit, bool update_=true) { setDigitMap(0x01 << digit, update_); } - void setAll(bool update_=true) { setDigitMap(all_on, update_); } - void clear(bool update_=true) { setDigitMap(all_off, update_); } - void setSecondsOnes() { setDigit(SECONDS_ONES); } - void setSecondsTens() { setDigit(SECONDS_TENS); } - void setMinutesOnes() { setDigit(MINUTES_ONES); } - void setMinutesTens() { setDigit(MINUTES_TENS); } - void setHoursOnes() { setDigit(HOURS_ONES); } - void setHoursTens() { setDigit(HOURS_TENS); } - bool isSecondsOnes() { return (digits_map&SECONDS_ONES_MAP > 0); } - bool isSecondsTens() { return (digits_map&SECONDS_TENS_MAP > 0); } - bool isMinutesOnes() { return (digits_map&MINUTES_ONES_MAP > 0); } - bool isMinutesTens() { return (digits_map&MINUTES_TENS_MAP > 0); } - bool isHoursOnes() { return (digits_map&HOURS_ONES_MAP > 0); } - bool isHoursTens() { return (digits_map&HOURS_TENS_MAP > 0); } + void setDigit(uint8_t digit, bool update_ = true) { setDigitMap(0x01 << digit, update_); } + void setAll(bool update_ = true) { setDigitMap(all_on, update_); } + void clear(bool update_ = true) { setDigitMap(all_off, update_); } + void setSecondsOnes() { setDigit(SECONDS_ONES); } + void setSecondsTens() { setDigit(SECONDS_TENS); } + void setMinutesOnes() { setDigit(MINUTES_ONES); } + void setMinutesTens() { setDigit(MINUTES_TENS); } + void setHoursOnes() { setDigit(HOURS_ONES); } + void setHoursTens() { setDigit(HOURS_TENS); } + bool isSecondsOnes() { return ((digits_map & SECONDS_ONES_MAP) > 0); } + bool isSecondsTens() { return ((digits_map & SECONDS_TENS_MAP) > 0); } + bool isMinutesOnes() { return ((digits_map & MINUTES_ONES_MAP) > 0); } + bool isMinutesTens() { return ((digits_map & MINUTES_TENS_MAP) > 0); } + bool isHoursOnes() { return ((digits_map & HOURS_ONES_MAP) > 0); } + bool isHoursTens() { return ((digits_map & HOURS_TENS_MAP) > 0); } private: uint8_t digits_map; @@ -45,5 +45,4 @@ class ChipSelect { const uint8_t all_off = 0x00; }; - #endif // CHIP_SELECT_H diff --git a/EleksTubeHAX_pio/src/Clock.cpp b/EleksTubeHAX_pio/src/Clock.cpp index ac9be7e..e3348b2 100644 --- a/EleksTubeHAX_pio/src/Clock.cpp +++ b/EleksTubeHAX_pio/src/Clock.cpp @@ -100,7 +100,7 @@ void Clock::loop() { // Static methods used for sync provider to TimeLib library. time_t Clock::syncProvider() { Serial.println("syncProvider()"); - time_t ntp_now, rtc_now; + time_t rtc_now; rtc_now = RtcGet(); if (millis() - millis_last_ntp > refresh_ntp_every_ms || millis_last_ntp == 0) { @@ -110,7 +110,7 @@ time_t Clock::syncProvider() { // ntpTimeClient.forceUpdate(); // maybe this breaks the NTP requests as this should not be done more than every minute. if (ntpTimeClient.update()) { Serial.print("."); - ntp_now = ntpTimeClient.getEpochTime(); + time_t ntp_now = ntpTimeClient.getEpochTime(); Serial.println("NTP query done."); Serial.print("NTP time = "); Serial.println(ntpTimeClient.getFormattedTime()); diff --git a/EleksTubeHAX_pio/src/Menu.h b/EleksTubeHAX_pio/src/Menu.h index e00e644..15a8075 100644 --- a/EleksTubeHAX_pio/src/Menu.h +++ b/EleksTubeHAX_pio/src/Menu.h @@ -12,7 +12,7 @@ class Menu { public: - Menu() : state(idle), change(0), millis_last_button_press(0) {} + Menu() : state(idle), change(0), millis_last_button_press(0), state_changed(false) {} void begin() {} void loop(Buttons &buttons); diff --git a/EleksTubeHAX_pio/src/StoredConfig.h b/EleksTubeHAX_pio/src/StoredConfig.h index 8cc73f2..b324322 100644 --- a/EleksTubeHAX_pio/src/StoredConfig.h +++ b/EleksTubeHAX_pio/src/StoredConfig.h @@ -18,7 +18,7 @@ class StoredConfig { public: - StoredConfig() : prefs(), config_size(sizeof(config)), loaded(false) {} + StoredConfig() : prefs(), config_size(sizeof(config)), loaded(false), config() {} void begin() { prefs.begin(SAVED_CONFIG_NAMESPACE, false); Serial.print("Config size: "); Serial.println(config_size); } void load() { prefs.getBytes(SAVED_CONFIG_NAMESPACE, &config, config_size); loaded = true; } void save() { prefs.putBytes(SAVED_CONFIG_NAMESPACE, &config, config_size); } diff --git a/EleksTubeHAX_pio/src/TFTs.cpp b/EleksTubeHAX_pio/src/TFTs.cpp index a990d91..eab1ea3 100644 --- a/EleksTubeHAX_pio/src/TFTs.cpp +++ b/EleksTubeHAX_pio/src/TFTs.cpp @@ -486,17 +486,17 @@ void TFTs::DrawImage(uint8_t file_index) { uint16_t TFTs::read16(fs::File &f) { uint16_t result; - ((uint8_t *)&result)[0] = f.read(); // LSB - ((uint8_t *)&result)[1] = f.read(); // MSB + reinterpret_cast(&result)[0] = f.read(); // LSB + reinterpret_cast(&result)[1] = f.read(); // MSB return result; } uint32_t TFTs::read32(fs::File &f) { uint32_t result; - ((uint8_t *)&result)[0] = f.read(); // LSB - ((uint8_t *)&result)[1] = f.read(); - ((uint8_t *)&result)[2] = f.read(); - ((uint8_t *)&result)[3] = f.read(); // MSB + reinterpret_cast(&result)[0] = f.read(); // LSB + reinterpret_cast(&result)[1] = f.read(); + reinterpret_cast(&result)[2] = f.read(); + reinterpret_cast(&result)[3] = f.read(); // MSB return result; } diff --git a/EleksTubeHAX_pio/src/WiFi_WPS.cpp b/EleksTubeHAX_pio/src/WiFi_WPS.cpp index 4ebe350..3da3f65 100644 --- a/EleksTubeHAX_pio/src/WiFi_WPS.cpp +++ b/EleksTubeHAX_pio/src/WiFi_WPS.cpp @@ -166,8 +166,8 @@ void WifiReconnect() { #ifdef WIFI_USE_WPS //// WPS code void WiFiStartWps() { // erase settings - sprintf(stored_config.config.wifi.ssid, ""); - sprintf(stored_config.config.wifi.password, ""); + snprintf(stored_config.config.wifi.ssid, sizeof(stored_config.config.wifi.ssid), "%s", WiFi.SSID().c_str()); + stored_config.config.wifi.password[0] = '\0'; // empty string as password stored_config.config.wifi.WPS_connected = 0x11; // invalid = different than 0x55 Serial.println(""); Serial.print("Saving config! Triggered from WPS start..."); stored_config.save(); @@ -200,12 +200,9 @@ void WiFiStartWps() { Serial.print("."); } tfts.setTextColor(TFT_WHITE, TFT_BLACK); - sprintf(stored_config.config.wifi.ssid, "%s", WiFi.SSID()); -// memset(stored_config.config.wifi.ssid, '\0', sizeof(stored_config.config.wifi.ssid)); -// strcpy(stored_config.config.wifi.ssid, WiFi.SSID()); - - sprintf(stored_config.config.wifi.password, ""); // can't save a password from WPS - stored_config.config.wifi.WPS_connected = StoredConfig::valid; + snprintf(stored_config.config.wifi.ssid, sizeof(stored_config.config.wifi.ssid), "%s", WiFi.SSID().c_str()); // Copy the SSID into the stored configuration safely + stored_config.config.wifi.password[0] = '\0'; // Since the password cannot be retrieved from WPS, set it to an empty string + stored_config.config.wifi.WPS_connected = StoredConfig::valid; // Mark the configuration as valid Serial.println(); Serial.print("Saving config! Triggered from WPS success..."); stored_config.save(); Serial.println(" WPS finished."); diff --git a/EleksTubeHAX_pio/src/main.cpp b/EleksTubeHAX_pio/src/main.cpp index 1b4fd7c..dc9bb60 100644 --- a/EleksTubeHAX_pio/src/main.cpp +++ b/EleksTubeHAX_pio/src/main.cpp @@ -490,8 +490,7 @@ void loop() { if (menu_change != 0) { // calculate the new offset time_t newOffsetAdjustmentValue = menu_change * 3600; - time_t newOffset = currOffset + newOffsetAdjustmentValue; - double newOffsetInHours = static_cast(newOffset) / 3600; + time_t newOffset = currOffset + newOffsetAdjustmentValue; // check if the new offset is within the allowed range of -12 to +12 hours // If the minutes part of the offset is 0, we want to change from +12 to -12 or vice versa (without changing the shown time on the displays) From 9e982b08af1685cb899c44b87845ff8b5b8d0285 Mon Sep 17 00:00:00 2001 From: Clemens Sutor Date: Tue, 5 Nov 2024 22:47:38 +0100 Subject: [PATCH 2/2] Fixed the rest of the "fixable" cppcheck findings --- EleksTubeHAX_pio/src/IPGeolocation_AO.h | 2 +- EleksTubeHAX_pio/src/NTPClient_AO.cpp | 2 +- EleksTubeHAX_pio/src/NTPClient_AO.h | 6 +++--- EleksTubeHAX_pio/src/TFTs.cpp | 2 +- EleksTubeHAX_pio/src/WiFi_WPS.cpp | 1 - 5 files changed, 6 insertions(+), 7 deletions(-) diff --git a/EleksTubeHAX_pio/src/IPGeolocation_AO.h b/EleksTubeHAX_pio/src/IPGeolocation_AO.h index eb12660..eca7368 100644 --- a/EleksTubeHAX_pio/src/IPGeolocation_AO.h +++ b/EleksTubeHAX_pio/src/IPGeolocation_AO.h @@ -36,7 +36,7 @@ struct IPGeo { class IPGeolocation { public: - IPGeolocation(String Key); + explicit IPGeolocation(String Key); IPGeolocation(String Key, String API); // Use IPG for api.ipgeolocation.io and ABSTRACT for app.abstractapi.com/api/ip-geolocation bool updateStatus(IPGeo *I); String getResponse(); diff --git a/EleksTubeHAX_pio/src/NTPClient_AO.cpp b/EleksTubeHAX_pio/src/NTPClient_AO.cpp index 1ad972a..f69a4f5 100644 --- a/EleksTubeHAX_pio/src/NTPClient_AO.cpp +++ b/EleksTubeHAX_pio/src/NTPClient_AO.cpp @@ -150,7 +150,7 @@ bool NTPClient::forceUpdate() { if( _packetBuffer[16] == 0 && _packetBuffer[17] == 0 && _packetBuffer[18] == 0 && _packetBuffer[19] == 0 && _packetBuffer[20] == 0 && _packetBuffer[21] == 0 && - _packetBuffer[22] == 0 && _packetBuffer[22] == 0) //Check for ReferenceTimestamp != 0 + _packetBuffer[22] == 0 && _packetBuffer[23] == 0) //Check for ReferenceTimestamp != 0 { #ifdef DEBUG_NTPClient Serial.println("err: Incorrect NTP Ref Timestamp"); diff --git a/EleksTubeHAX_pio/src/NTPClient_AO.h b/EleksTubeHAX_pio/src/NTPClient_AO.h index f41b927..bfb45a2 100644 --- a/EleksTubeHAX_pio/src/NTPClient_AO.h +++ b/EleksTubeHAX_pio/src/NTPClient_AO.h @@ -31,9 +31,9 @@ class NTPClient { bool sendNTPPacket(); public: - NTPClient(UDP& udp); - NTPClient(UDP& udp, long timeOffset); - NTPClient(UDP& udp, const char* poolServerName); + explicit NTPClient(UDP& udp); + explicit NTPClient(UDP& udp, long timeOffset); + explicit NTPClient(UDP& udp, const char* poolServerName); NTPClient(UDP& udp, const char* poolServerName, long timeOffset); NTPClient(UDP& udp, const char* poolServerName, long timeOffset, unsigned long updateInterval); diff --git a/EleksTubeHAX_pio/src/TFTs.cpp b/EleksTubeHAX_pio/src/TFTs.cpp index eab1ea3..094343d 100644 --- a/EleksTubeHAX_pio/src/TFTs.cpp +++ b/EleksTubeHAX_pio/src/TFTs.cpp @@ -470,7 +470,7 @@ void TFTs::DrawImage(uint8_t file_index) { bool oldSwapBytes = getSwapBytes(); setSwapBytes(true); - pushImage(0,0, TFT_WIDTH, TFT_HEIGHT, (uint16_t *)UnpackedImageBuffer); + pushImage(0, 0, TFT_WIDTH, TFT_HEIGHT, reinterpret_cast(UnpackedImageBuffer)); setSwapBytes(oldSwapBytes); #ifdef DEBUG_OUTPUT diff --git a/EleksTubeHAX_pio/src/WiFi_WPS.cpp b/EleksTubeHAX_pio/src/WiFi_WPS.cpp index 3da3f65..230c45d 100644 --- a/EleksTubeHAX_pio/src/WiFi_WPS.cpp +++ b/EleksTubeHAX_pio/src/WiFi_WPS.cpp @@ -217,7 +217,6 @@ void WiFiStartWps() { bool GetGeoLocationTimeZoneOffset() { Serial.println("Starting Geolocation query..."); // https://app.abstractapi.com/api/ip-geolocation/ // free for 5k loopkups per month. -// Live test: https://ipgeolocation.abstractapi.com/v1/?api_key=e11dc0f9bab446bfa9957aad2c4ad064 IPGeolocation location(GEOLOCATION_API_KEY,"ABSTRACT"); IPGeo IPG; if (location.updateStatus(&IPG)) {