code sanitizations

This commit is contained in:
Klaus K Wilting 2018-07-23 13:20:06 +02:00
parent 337fc64602
commit fa11eba85c
11 changed files with 89 additions and 97 deletions

View File

@ -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
;

View File

@ -1,6 +1,7 @@
#ifdef HAS_BUTTON
#include "globals.h"
#include "senddata.h"
// Local logging tag
static const char TAG[] = "main";

View File

@ -1,7 +1,6 @@
#ifndef _BUTTON_H
#define _BUTTON_H
void IRAM_ATTR ButtonIRQ(void);
void readButton(void);

View File

@ -53,3 +53,27 @@ void IRAM_ATTR homeCycleIRQ() {
HomeCycleIRQ++;
portEXIT_CRITICAL(&timerMux);
}
// 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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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
}

View File

@ -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

View File

@ -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

View File

@ -12,9 +12,4 @@
#include <TimeLib.h>
void reset_counters(void);
void blink_LED(uint16_t set_color, uint16_t set_blinkduration);
void led_loop(void);
uint64_t uptime();
#endif