From 337fc64602fff8de6027221c45827771687fdd71 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Mon, 23 Jul 2018 08:25:23 +0200 Subject: [PATCH] code sanitizations --- src/button.cpp | 21 ++++++++++++++++ src/button.h | 8 ++++++ src/display.cpp | 15 ++++++++++++ src/display.h | 2 ++ src/globals.h | 7 +++++- src/homecycle.cpp | 15 ++++++++++++ src/homecycle.h | 2 ++ src/macsniff.cpp | 9 ++++++- src/macsniff.h | 1 + src/main.cpp | 62 ----------------------------------------------- src/senddata.cpp | 8 +++++- src/senddata.h | 1 + 12 files changed, 86 insertions(+), 65 deletions(-) create mode 100644 src/button.cpp create mode 100644 src/button.h diff --git a/src/button.cpp b/src/button.cpp new file mode 100644 index 00000000..b2c5abe4 --- /dev/null +++ b/src/button.cpp @@ -0,0 +1,21 @@ +#ifdef HAS_BUTTON + +#include "globals.h" + +// Local logging tag +static const char TAG[] = "main"; + +void IRAM_ATTR ButtonIRQ() { ButtonPressedIRQ++; } + +void readButton() { + if (ButtonPressedIRQ) { + portENTER_CRITICAL(&timerMux); + ButtonPressedIRQ = 0; + portEXIT_CRITICAL(&timerMux); + ESP_LOGI(TAG, "Button pressed"); + payload.reset(); + payload.addButton(0x01); + senddata(BUTTONPORT); + } +} +#endif \ No newline at end of file diff --git a/src/button.h b/src/button.h new file mode 100644 index 00000000..8a5c91ca --- /dev/null +++ b/src/button.h @@ -0,0 +1,8 @@ +#ifndef _BUTTON_H +#define _BUTTON_H + + +void IRAM_ATTR ButtonIRQ(void); +void readButton(void); + +#endif \ No newline at end of file diff --git a/src/display.cpp b/src/display.cpp index 52decae1..d8999bb0 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -170,4 +170,19 @@ void refreshtheDisplay() { #endif // HAS_LORA } // refreshDisplay() +void IRAM_ATTR DisplayIRQ() { + portENTER_CRITICAL_ISR(&timerMux); + DisplayTimerIRQ++; + portEXIT_CRITICAL_ISR(&timerMux); +} + +void updateDisplay() { + if (DisplayTimerIRQ) { + portENTER_CRITICAL(&timerMux); + DisplayTimerIRQ = 0; + portEXIT_CRITICAL(&timerMux); + refreshtheDisplay(); + } +} + #endif // HAS_DISPLAY \ No newline at end of file diff --git a/src/display.h b/src/display.h index c88d9315..52fb4d7e 100644 --- a/src/display.h +++ b/src/display.h @@ -8,5 +8,7 @@ extern uint8_t DisplayState; void init_display(const char *Productname, const char *Version); void refreshtheDisplay(void); void DisplayKey(const uint8_t *key, uint8_t len, bool lsb); +void updateDisplay(void); +void DisplayIRQ(void); #endif \ No newline at end of file diff --git a/src/globals.h b/src/globals.h index 9cc066f2..6b648d14 100644 --- a/src/globals.h +++ b/src/globals.h @@ -44,7 +44,8 @@ extern uint16_t 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; +extern volatile int SendCycleTimerIRQ, HomeCycleIRQ, DisplayTimerIRQ, + ChannelTimerIRQ, ButtonPressedIRQ; #ifdef HAS_GPS #include "gps.h" @@ -60,6 +61,10 @@ extern volatile int SendCycleTimerIRQ; #include "display.h" #endif +#ifdef HAS_BUTTON +#include "button.h" +#endif + #ifdef BLECOUNTER #include "blescan.h" #endif diff --git a/src/homecycle.cpp b/src/homecycle.cpp index c325e232..f6197363 100644 --- a/src/homecycle.cpp +++ b/src/homecycle.cpp @@ -37,4 +37,19 @@ void doHomework() { reset_counters(); // clear macs container and reset all counters reset_salt(); // get new salt for salting hashes } +} + +void checkHousekeeping() { + if (HomeCycleIRQ) { + portENTER_CRITICAL(&timerMux); + HomeCycleIRQ = 0; + portEXIT_CRITICAL(&timerMux); + doHomework(); + } +} + +void IRAM_ATTR homeCycleIRQ() { + portENTER_CRITICAL(&timerMux); + HomeCycleIRQ++; + portEXIT_CRITICAL(&timerMux); } \ No newline at end of file diff --git a/src/homecycle.h b/src/homecycle.h index 4d1cd468..f6a49850 100644 --- a/src/homecycle.h +++ b/src/homecycle.h @@ -2,5 +2,7 @@ #define _HOMECYCLE_H void doHomework(void); +void checkHousekeeping(void); +void homeCycleIRQ(void); #endif \ No newline at end of file diff --git a/src/macsniff.cpp b/src/macsniff.cpp index 7da0dbd4..b7833e77 100644 --- a/src/macsniff.cpp +++ b/src/macsniff.cpp @@ -16,7 +16,8 @@ static wifi_country_t wifi_country = {WIFI_MY_COUNTRY, WIFI_CHANNEL_MIN, */ static wifi_country_t wifi_country = {WIFI_MY_COUNTRY, WIFI_CHANNEL_MIN, - WIFI_CHANNEL_MAX, WIFI_COUNTRY_POLICY_MANUAL}; + WIFI_CHANNEL_MAX, + WIFI_COUNTRY_POLICY_MANUAL}; // globals uint16_t salt; @@ -138,3 +139,9 @@ IRAM_ATTR void wifi_sniffer_packet_handler(void *buff, mac_add(p, ppkt->rx_ctrl.rssi, MAC_SNIFF_WIFI); } } + +void IRAM_ATTR ChannelSwitchIRQ() { + portENTER_CRITICAL(&timerMux); + ChannelTimerIRQ++; + portEXIT_CRITICAL(&timerMux); +} diff --git a/src/macsniff.h b/src/macsniff.h index 22992f09..c18bc487 100644 --- a/src/macsniff.h +++ b/src/macsniff.h @@ -31,5 +31,6 @@ void wifi_sniffer_init(void); void wifi_sniffer_set_channel(uint8_t channel); void wifi_sniffer_packet_handler(void *buff, wifi_promiscuous_pkt_type_t type); bool mac_add(uint8_t *paddr, int8_t rssi, bool sniff_type); +void ChannelSwitchIRQ(void); #endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 7cf1d1fc..19543a48 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -98,68 +98,6 @@ void lorawan_loop(void *pvParameters) { #endif // HAS_LORA -// Setup IRQ handler routines -// attn see https://github.com/espressif/arduino-esp32/issues/855 - -void IRAM_ATTR ChannelSwitchIRQ() { - portENTER_CRITICAL(&timerMux); - ChannelTimerIRQ++; - portEXIT_CRITICAL(&timerMux); -} - -void IRAM_ATTR SendCycleIRQ() { - portENTER_CRITICAL(&timerMux); - SendCycleTimerIRQ++; - portEXIT_CRITICAL(&timerMux); -} - -void IRAM_ATTR homeCycleIRQ() { - portENTER_CRITICAL(&timerMux); - HomeCycleIRQ++; - portEXIT_CRITICAL(&timerMux); -} - -#ifdef HAS_DISPLAY -void IRAM_ATTR DisplayIRQ() { - portENTER_CRITICAL_ISR(&timerMux); - DisplayTimerIRQ++; - portEXIT_CRITICAL_ISR(&timerMux); -} -void updateDisplay() { - if (DisplayTimerIRQ) { - portENTER_CRITICAL(&timerMux); - DisplayTimerIRQ = 0; - portEXIT_CRITICAL(&timerMux); - refreshtheDisplay(); - } -} -#endif - -void checkHousekeeping() { - if (HomeCycleIRQ) { - portENTER_CRITICAL(&timerMux); - HomeCycleIRQ = 0; - portEXIT_CRITICAL(&timerMux); - doHomework(); - } -} - -#ifdef HAS_BUTTON -void IRAM_ATTR ButtonIRQ() { ButtonPressedIRQ++; } - -void readButton() { - if (ButtonPressedIRQ) { - portENTER_CRITICAL(&timerMux); - ButtonPressedIRQ = 0; - portEXIT_CRITICAL(&timerMux); - ESP_LOGI(TAG, "Button pressed"); - payload.reset(); - payload.addButton(0x01); - senddata(BUTTONPORT); - } -} -#endif - // Wifi channel rotation task void wifi_channel_loop(void *pvParameters) { diff --git a/src/senddata.cpp b/src/senddata.cpp index e9133491..8d86065f 100644 --- a/src/senddata.cpp +++ b/src/senddata.cpp @@ -65,4 +65,10 @@ void sendPayload() { #endif senddata(COUNTERPORT); } -} // sendpayload(); \ No newline at end of file +} // sendpayload(); + +void IRAM_ATTR SendCycleIRQ() { + portENTER_CRITICAL(&timerMux); + SendCycleTimerIRQ++; + portEXIT_CRITICAL(&timerMux); +} \ No newline at end of file diff --git a/src/senddata.h b/src/senddata.h index 49be9f75..00ff6d05 100644 --- a/src/senddata.h +++ b/src/senddata.h @@ -3,5 +3,6 @@ void senddata(uint8_t port); void sendPayload(void); +void SendCycleIRQ(void); #endif // _SENDDATA_H_ \ No newline at end of file