From fa11eba85cb8dbe1df87bb48e9d5319ddad2d347 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Mon, 23 Jul 2018 13:20:06 +0200 Subject: [PATCH] code sanitizations --- platformio.ini | 6 +-- src/button.cpp | 1 + src/button.h | 1 - src/homecycle.cpp | 26 ++++++++++++- src/homecycle.h | 3 ++ src/lorawan.cpp | 21 ++++++++++ src/lorawan.h | 1 + src/macsniff.cpp | 22 +++++++++++ src/macsniff.h | 1 + src/main.cpp | 99 ++++++----------------------------------------- src/main.h | 5 --- 11 files changed, 89 insertions(+), 97 deletions(-) diff --git a/platformio.ini b/platformio.ini index d48e5771..75d8d189 100644 --- a/platformio.ini +++ b/platformio.ini @@ -11,11 +11,11 @@ ; ---> SELECT TARGET PLATFORM HERE! <--- [platformio] -;env_default = generic +env_default = generic ;env_default = heltec ;env_default = ttgov1 ;env_default = ttgov2 -env_default = ttgov21 +;env_default = ttgov21 ;env_default = ttgobeam ;env_default = lopy ;env_default = lopy4 @@ -44,7 +44,7 @@ build_flags = ; otherwise device may crash in dense environments due to serial buffer overflow ; ; -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_NONE -;-DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_INFO + -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_INFO ; -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_DEBUG ; -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_VERBOSE ; diff --git a/src/button.cpp b/src/button.cpp index b2c5abe4..e9b0fe0e 100644 --- a/src/button.cpp +++ b/src/button.cpp @@ -1,6 +1,7 @@ #ifdef HAS_BUTTON #include "globals.h" +#include "senddata.h" // Local logging tag static const char TAG[] = "main"; diff --git a/src/button.h b/src/button.h index 8a5c91ca..1900ac6d 100644 --- a/src/button.h +++ b/src/button.h @@ -1,7 +1,6 @@ #ifndef _BUTTON_H #define _BUTTON_H - void IRAM_ATTR ButtonIRQ(void); void readButton(void); diff --git a/src/homecycle.cpp b/src/homecycle.cpp index f6197363..0ee1bd1e 100644 --- a/src/homecycle.cpp +++ b/src/homecycle.cpp @@ -52,4 +52,28 @@ void IRAM_ATTR homeCycleIRQ() { portENTER_CRITICAL(&timerMux); HomeCycleIRQ++; portEXIT_CRITICAL(&timerMux); -} \ No newline at end of file +} + +// uptime counter 64bit to prevent millis() rollover after 49 days +uint64_t uptime() { + static uint32_t low32, high32; + uint32_t new_low32 = millis(); + if (new_low32 < low32) + high32++; + low32 = new_low32; + return (uint64_t)high32 << 32 | low32; +} + +void reset_counters() { + macs.clear(); // clear all macs container + macs_total = 0; // reset all counters + macs_wifi = 0; + macs_ble = 0; +} + +#ifndef VERBOSE +int redirect_log(const char *fmt, va_list args) { + // do nothing + return 0; +} +#endif diff --git a/src/homecycle.h b/src/homecycle.h index f6a49850..9e1f74a3 100644 --- a/src/homecycle.h +++ b/src/homecycle.h @@ -4,5 +4,8 @@ void doHomework(void); void checkHousekeeping(void); void homeCycleIRQ(void); +uint64_t uptime(void); +void reset_counters(void); +int redirect_log(const char *fmt, va_list args); #endif \ No newline at end of file diff --git a/src/lorawan.cpp b/src/lorawan.cpp index eba99276..15187b4d 100644 --- a/src/lorawan.cpp +++ b/src/lorawan.cpp @@ -11,6 +11,15 @@ // Local logging Tag static const char TAG[] = "lora"; +// LMIC enhanced Pin mapping +const lmic_pinmap lmic_pins = {.mosi = PIN_SPI_MOSI, + .miso = PIN_SPI_MISO, + .sck = PIN_SPI_SCK, + .nss = PIN_SPI_SS, + .rxtx = LMIC_UNUSED_PIN, + .rst = RST, + .dio = {DIO0, DIO1, DIO2}}; + // DevEUI generator using devices's MAC address void gen_lora_deveui(uint8_t *pdeveui) { uint8_t *p = pdeveui, dmac[6]; @@ -231,4 +240,16 @@ void onEvent(ev_t ev) { } // onEvent() +// LMIC FreeRTos Task +void lorawan_loop(void *pvParameters) { + + configASSERT(((uint32_t)pvParameters) == 1); // FreeRTOS check + + while (1) { + os_runloop_once(); // execute LMIC jobs + vTaskDelay(1 / portTICK_PERIOD_MS); // reset watchdog + } +} + + #endif // HAS_LORA \ No newline at end of file diff --git a/src/lorawan.h b/src/lorawan.h index 3467d35c..45123d9e 100644 --- a/src/lorawan.h +++ b/src/lorawan.h @@ -14,5 +14,6 @@ void os_getDevKey(u1_t *buf); void os_getArtEui(u1_t *buf); void os_getDevEui(u1_t *buf); void printKeys(void); +void lorawan_loop(void *pvParameters); #endif \ No newline at end of file diff --git a/src/macsniff.cpp b/src/macsniff.cpp index b7833e77..18f743d7 100644 --- a/src/macsniff.cpp +++ b/src/macsniff.cpp @@ -145,3 +145,25 @@ void IRAM_ATTR ChannelSwitchIRQ() { ChannelTimerIRQ++; portEXIT_CRITICAL(&timerMux); } + +// Wifi channel rotation task +void wifi_channel_loop(void *pvParameters) { + + configASSERT(((uint32_t)pvParameters) == 1); // FreeRTOS check + + while (1) { + + if (ChannelTimerIRQ) { + portENTER_CRITICAL(&timerMux); + ChannelTimerIRQ = 0; + portEXIT_CRITICAL(&timerMux); + // rotates variable channel 1..WIFI_CHANNEL_MAX + channel = (channel % WIFI_CHANNEL_MAX) + 1; + wifi_sniffer_set_channel(channel); + ESP_LOGD(TAG, "Wifi set channel %d", channel); + + vTaskDelay(1 / portTICK_PERIOD_MS); // reset watchdog + } + + } // end of infinite wifi channel rotation loop +} \ No newline at end of file diff --git a/src/macsniff.h b/src/macsniff.h index c18bc487..ed42d957 100644 --- a/src/macsniff.h +++ b/src/macsniff.h @@ -32,5 +32,6 @@ 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); +void wifi_channel_loop(void *pvParameters); #endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 19543a48..7cfe2e6c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -54,82 +54,6 @@ PayloadConvert payload(PAYLOAD_BUFFER_SIZE); // local Tag for logging static const char TAG[] = "main"; -#ifndef VERBOSE -int redirect_log(const char *fmt, va_list args) { - // do nothing - return 0; -} -#endif - -void reset_counters() { - macs.clear(); // clear all macs container - macs_total = 0; // reset all counters - macs_wifi = 0; - macs_ble = 0; -} - -#ifdef HAS_LORA - -// LMIC enhanced Pin mapping -const lmic_pinmap lmic_pins = {.mosi = PIN_SPI_MOSI, - .miso = PIN_SPI_MISO, - .sck = PIN_SPI_SCK, - .nss = PIN_SPI_SS, - .rxtx = LMIC_UNUSED_PIN, - .rst = RST, - .dio = {DIO0, DIO1, DIO2}}; - -// Get MCP 24AA02E64 hardware DEVEUI (override default settings if found) -#ifdef MCP_24AA02E64_I2C_ADDRESS -get_hard_deveui(buf); -RevBytes(buf, 8); // swap bytes to LSB format -#endif - -// LMIC FreeRTos Task -void lorawan_loop(void *pvParameters) { - - configASSERT(((uint32_t)pvParameters) == 1); // FreeRTOS check - - while (1) { - os_runloop_once(); // execute LMIC jobs - vTaskDelay(1 / portTICK_PERIOD_MS); // reset watchdog - } -} - -#endif // HAS_LORA - -// Wifi channel rotation task -void wifi_channel_loop(void *pvParameters) { - - configASSERT(((uint32_t)pvParameters) == 1); // FreeRTOS check - - while (1) { - - if (ChannelTimerIRQ) { - portENTER_CRITICAL(&timerMux); - ChannelTimerIRQ = 0; - portEXIT_CRITICAL(&timerMux); - // rotates variable channel 1..WIFI_CHANNEL_MAX - channel = (channel % WIFI_CHANNEL_MAX) + 1; - wifi_sniffer_set_channel(channel); - ESP_LOGD(TAG, "Wifi set channel %d", channel); - - vTaskDelay(1 / portTICK_PERIOD_MS); // reset watchdog - } - - } // end of infinite wifi channel rotation loop -} - -// uptime counter 64bit to prevent millis() rollover after 49 days -uint64_t uptime() { - static uint32_t low32, high32; - uint32_t new_low32 = millis(); - if (new_low32 < low32) - high32++; - low32 = new_low32; - return (uint64_t)high32 << 32 | low32; -} - /* begin Aruino SETUP * ------------------------------------------------------------ */ @@ -183,7 +107,12 @@ void setup() { // read settings from NVRAM loadConfig(); // includes initialize if necessary - // initialize led if needed +// initialize LoRa +#if HAS_LORA + strcat_P(features, "LORA"); +#endif + + // initialize led #if (HAS_LED != NOT_A_PIN) pinMode(HAS_LED, OUTPUT); strcat_P(features, " LED"); @@ -194,7 +123,7 @@ void setup() { strcat_P(features, " RGB"); #endif - // initialize button handling if needed + // initialize button #ifdef HAS_BUTTON strcat_P(features, " BTN_"); #ifdef BUTTON_PULLUP @@ -210,7 +139,7 @@ void setup() { #endif // BUTTON_PULLUP #endif // HAS_BUTTON - // initialize wifi antenna if needed + // initialize wifi antenna #ifdef HAS_ANTENNA_SWITCH strcat_P(features, " ANT"); antenna_init(); @@ -224,19 +153,19 @@ void setup() { bool btstop = btStop(); #endif -// initialize gps if present +// initialize gps #ifdef HAS_GPS strcat_P(features, " GPS"); #endif -// initialize battery status if present +// initialize battery status #ifdef HAS_BATTERY_PROBE strcat_P(features, " BATT"); calibrate_voltage(); batt_voltage = read_voltage(); #endif -// initialize display if present +// initialize display #ifdef HAS_DISPLAY strcat_P(features, " OLED"); DisplayState = cfg.screenon; @@ -288,7 +217,6 @@ void setup() { ESP_LOGI(TAG, "Features: %s", features); #ifdef HAS_LORA - // output LoRaWAN keys to console #ifdef VERBOSE printKeys(); @@ -351,8 +279,7 @@ void setup() { void loop() { while (1) { - - // state machine for uptime, display, LED, button, lowmemory, senddata + // state machine for switching display, LED, button, housekeeping, senddata #if (HAS_LED != NOT_A_PIN) || defined(HAS_RGB_LED) led_loop(); @@ -368,10 +295,8 @@ void loop() { // check housekeeping cycle and to homework if expired checkHousekeeping(); - // check send cycle and send payload if cycle is expired sendPayload(); - vTaskDelay(1 / portTICK_PERIOD_MS); // reset watchdog } // end of infinite main loop diff --git a/src/main.h b/src/main.h index 6b418289..ec4d8304 100644 --- a/src/main.h +++ b/src/main.h @@ -12,9 +12,4 @@ #include -void reset_counters(void); -void blink_LED(uint16_t set_color, uint16_t set_blinkduration); -void led_loop(void); -uint64_t uptime(); - #endif \ No newline at end of file