From 8985ee3603a0cd658ac1881e3bf6083ad7ecf293 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Sun, 23 Sep 2018 22:12:10 +0200 Subject: [PATCH] code sanitization (volatiles check) --- src/cyclic.cpp | 2 +- src/display.cpp | 2 +- src/display.h | 2 +- src/globals.h | 9 +++++---- src/macsniff.cpp | 9 ++++----- src/macsniff.h | 2 +- src/main.cpp | 14 +++++++------- src/ota.cpp | 4 ++-- src/rcommand.cpp | 2 +- src/senddata.cpp | 2 +- src/wifiscan.cpp | 18 +++++++++--------- src/wifiscan.h | 2 +- 12 files changed, 34 insertions(+), 34 deletions(-) diff --git a/src/cyclic.cpp b/src/cyclic.cpp index b0c1a0b5..0d5d1a3d 100644 --- a/src/cyclic.cpp +++ b/src/cyclic.cpp @@ -48,7 +48,7 @@ void doHousekeeping() { esp_get_minimum_free_heap_size(), ESP.getFreeHeap()); SendData(COUNTERPORT); // send data before clearing counters reset_counters(); // clear macs container and reset all counters - reset_salt(); // get new salt for salting hashes + get_salt(); // get new salt for salting hashes if (esp_get_minimum_free_heap_size() <= MEM_LOW) // check again esp_restart(); // memory leak, reset device diff --git a/src/display.cpp b/src/display.cpp index ba71a544..b3ed4ee4 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -13,7 +13,7 @@ const char lora_datarate[] = {"1211100908077BFSNA"}; const char lora_datarate[] = {"100908078CNA121110090807"}; #endif -uint8_t DisplayState = 0; +uint8_t volatile DisplayState = 0; // helper function, prints a hex key on display void DisplayKey(const uint8_t *key, uint8_t len, bool lsb) { diff --git a/src/display.h b/src/display.h index e85b9ee1..e009216d 100644 --- a/src/display.h +++ b/src/display.h @@ -3,7 +3,7 @@ #include -extern uint8_t DisplayState; +extern uint8_t volatile DisplayState; extern HAS_DISPLAY u8x8; void init_display(const char *Productname, const char *Version); diff --git a/src/globals.h b/src/globals.h index 9d79575d..28d9fde7 100644 --- a/src/globals.h +++ b/src/globals.h @@ -39,14 +39,15 @@ typedef struct { } MessageBuffer_t; // global variables -extern configData_t cfg; // current device configuration +extern configData_t cfg; // current device configuration extern char display_line6[], display_line7[]; // screen buffers -extern uint8_t channel; // wifi channel rotation counter -extern uint16_t macs_total, macs_wifi, macs_ble, batt_voltage; // display values +extern uint8_t volatile channel; // wifi channel rotation counter +extern uint16_t volatile macs_total, macs_wifi, macs_ble, + batt_voltage; // display values extern std::set macs; // temp storage for MACs extern hw_timer_t *channelSwitch, *sendCycle; extern portMUX_TYPE timerMux; -extern volatile int SendCycleTimerIRQ, HomeCycleIRQ, DisplayTimerIRQ, +extern volatile uint8_t SendCycleTimerIRQ, HomeCycleIRQ, DisplayTimerIRQ, ChannelTimerIRQ, ButtonPressedIRQ; extern std::array::iterator it; diff --git a/src/macsniff.cpp b/src/macsniff.cpp index ac037e28..52056753 100644 --- a/src/macsniff.cpp +++ b/src/macsniff.cpp @@ -11,8 +11,8 @@ static const char TAG[] = "main"; uint16_t salt; -uint16_t reset_salt(void) { - salt = random(65536); // get new 16bit random for salting hashes +uint16_t get_salt(void) { + salt = (uint16_t)random(65536); // get new 16bit random for salting hashes return salt; } @@ -71,8 +71,8 @@ bool mac_add(uint8_t *paddr, int8_t rssi, bool sniff_type) { // https://en.wikipedia.org/wiki/MAC_Address_Anonymization snprintf(buff, sizeof(buff), "%08X", - addr2int + (uint32_t)salt); // convert usigned 32-bit salted MAC to - // 8 digit hex string + addr2int + (uint32_t)salt); // convert usigned 32-bit salted MAC + // to 8 digit hex string hashedmac = rokkit(&buff[3], 5); // hash MAC last string value, use 5 chars // to fit hash in uint16_t container auto newmac = macs.insert(hashedmac); // add hashed MAC, if new unique @@ -81,7 +81,6 @@ bool mac_add(uint8_t *paddr, int8_t rssi, bool sniff_type) { // Count only if MAC was not yet seen if (added) { - // increment counter and one blink led if (sniff_type == MAC_SNIFF_WIFI) { macs_wifi++; // increment Wifi MACs counter diff --git a/src/macsniff.h b/src/macsniff.h index 40adb6cf..81e5a2ed 100644 --- a/src/macsniff.h +++ b/src/macsniff.h @@ -12,7 +12,7 @@ #define MAC_SNIFF_WIFI 0 #define MAC_SNIFF_BLE 1 -uint16_t reset_salt(void); +uint16_t get_salt(void); uint64_t macConvert(uint8_t *paddr); bool mac_add(uint8_t *paddr, int8_t rssi, bool sniff_type); void printKey(const char *name, const uint8_t *key, uint8_t len, bool lsb); diff --git a/src/main.cpp b/src/main.cpp index 8f4bd99c..3022f63c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -47,16 +47,16 @@ ESP32 hardware timers configData_t cfg; // struct holds current device configuration char display_line6[16], display_line7[16]; // display buffers -uint8_t channel = 0; // channel rotation counter -uint16_t macs_total = 0, macs_wifi = 0, macs_ble = 0, - batt_voltage = 0; // globals for display +uint8_t volatile channel = 0; // channel rotation counter +uint16_t volatile macs_total = 0, macs_wifi = 0, macs_ble = 0, + batt_voltage = 0; // globals for display // hardware timer for cyclic tasks hw_timer_t *channelSwitch, *displaytimer, *sendCycle, *homeCycle; // this variables will be changed in the ISR, and read in main loop -volatile int ButtonPressedIRQ = 0, ChannelTimerIRQ = 0, SendCycleTimerIRQ = 0, - DisplayTimerIRQ = 0, HomeCycleIRQ = 0; +uint8_t volatile ButtonPressedIRQ = 0, ChannelTimerIRQ = 0, + SendCycleTimerIRQ = 0, DisplayTimerIRQ = 0, HomeCycleIRQ = 0; TaskHandle_t StateTask = NULL; @@ -96,7 +96,7 @@ void setup() { // disable brownout detection #ifdef DISABLE_BROWNOUT // register with brownout is at address DR_REG_RTCCNTL_BASE + 0xd4 - (*((volatile uint32_t *)ETS_UNCACHED_ADDR((DR_REG_RTCCNTL_BASE + 0xd4)))) = 0; + (*((uint32_t volatile *)ETS_UNCACHED_ADDR((DR_REG_RTCCNTL_BASE + 0xd4)))) = 0; #endif // setup debug output or silence device @@ -329,7 +329,7 @@ void setup() { // initialize salt value using esp_random() called by random() in // arduino-esp32 core. Note: do this *after* wifi has started, since // function gets it's seed from RF noise - reset_salt(); // get new 16bit for salting hashes + get_salt(); // get new 16bit for salting hashes // start state machine ESP_LOGI(TAG, "Starting Statemachine..."); diff --git a/src/ota.cpp b/src/ota.cpp index b2da167d..6c0a9471 100644 --- a/src/ota.cpp +++ b/src/ota.cpp @@ -29,8 +29,8 @@ const int port = 443; const uint32_t RESPONSE_TIMEOUT_MS = 5000; // Variables to validate firmware content -volatile int contentLength = 0; -volatile bool isValidContentType = false; +int volatile contentLength = 0; +bool volatile isValidContentType = false; // Local logging tag static const char TAG[] = "main"; diff --git a/src/rcommand.cpp b/src/rcommand.cpp index 7f984cdb..994b0f44 100644 --- a/src/rcommand.cpp +++ b/src/rcommand.cpp @@ -18,7 +18,7 @@ void set_reset(uint8_t val[]) { case 1: // reset MAC counter ESP_LOGI(TAG, "Remote command: reset MAC counter"); reset_counters(); // clear macs - reset_salt(); // get new salt + get_salt(); // get new salt sprintf(display_line6, "Reset counter"); break; case 2: // reset device to factory settings diff --git a/src/senddata.cpp b/src/senddata.cpp index f738ee6b..2ca7973a 100644 --- a/src/senddata.cpp +++ b/src/senddata.cpp @@ -29,7 +29,7 @@ void SendData(uint8_t port) { // clear counter if not in cumulative counter mode if ((port == COUNTERPORT) && (cfg.countermode != 1)) { reset_counters(); // clear macs container and reset all counters - reset_salt(); // get new salt for salting hashes + get_salt(); // get new salt for salting hashes ESP_LOGI(TAG, "Counter cleared"); } } // SendData diff --git a/src/wifiscan.cpp b/src/wifiscan.cpp index 55c690ff..d6012afa 100644 --- a/src/wifiscan.cpp +++ b/src/wifiscan.cpp @@ -44,15 +44,15 @@ void wifi_sniffer_init(void) { } // Wifi channel rotation -void switchWifiChannel(uint8_t &ch) { - portENTER_CRITICAL(&timerMux); - ChannelTimerIRQ = 0; - portEXIT_CRITICAL(&timerMux); - // rotates variable channel 1..WIFI_CHANNEL_MAX - ch = (ch % WIFI_CHANNEL_MAX) + 1; - esp_wifi_set_channel(ch, WIFI_SECOND_CHAN_NONE); - ESP_LOGD(TAG, "Wifi set channel %d", &ch); - } +void switchWifiChannel(uint8_t volatile &ch) { + portENTER_CRITICAL(&timerMux); + ChannelTimerIRQ = 0; + portEXIT_CRITICAL(&timerMux); + // rotates variable channel 1..WIFI_CHANNEL_MAX + ch = (ch % WIFI_CHANNEL_MAX) + 1; + esp_wifi_set_channel(ch, WIFI_SECOND_CHAN_NONE); + ESP_LOGD(TAG, "Wifi set channel %d", ch); +} // IRQ handler void IRAM_ATTR ChannelSwitchIRQ() { diff --git a/src/wifiscan.h b/src/wifiscan.h index 5e8aae9a..581d7b0d 100644 --- a/src/wifiscan.h +++ b/src/wifiscan.h @@ -28,6 +28,6 @@ typedef struct { void wifi_sniffer_init(void); void wifi_sniffer_packet_handler(void *buff, wifi_promiscuous_pkt_type_t type); void ChannelSwitchIRQ(void); -void switchWifiChannel(uint8_t &ch); +void switchWifiChannel(uint8_t volatile &ch); #endif \ No newline at end of file