code sanitizations

This commit is contained in:
Klaus K Wilting 2018-07-19 22:33:37 +02:00
parent ed18316616
commit 742050f01b
6 changed files with 69 additions and 101 deletions

View File

@ -12,11 +12,11 @@
; ---> SELECT TARGET PLATFORM HERE! <--- ; ---> SELECT TARGET PLATFORM HERE! <---
[platformio] [platformio]
;env_default = lolin32lite, heltec, generic ;env_default = lolin32lite, heltec, generic
env_default = generic ;env_default = generic
;env_default = heltec ;env_default = heltec
;env_default = ttgov1 ;env_default = ttgov1
;env_default = ttgov2 ;env_default = ttgov2
;env_default = ttgov21 env_default = ttgov21
;env_default = ttgobeam ;env_default = ttgobeam
;env_default = lopy ;env_default = lopy
;env_default = lopy4 ;env_default = lopy4

View File

@ -41,7 +41,7 @@ extern configData_t cfg; // current device configuration
extern char display_line6[], display_line7[]; // screen buffers extern char display_line6[], display_line7[]; // screen buffers
extern uint8_t channel; // wifi channel rotation counter extern uint8_t channel; // wifi channel rotation counter
extern uint16_t macs_total, macs_wifi, macs_ble; // MAC counters extern uint16_t macs_total, macs_wifi, macs_ble; // MAC counters
extern std::set<uint16_t> macs; // temp storeage for sniffed MACs extern std::set<uint16_t> macs; // temp storage for MACs
extern hw_timer_t *channelSwitch, *sendCycle; extern hw_timer_t *channelSwitch, *sendCycle;
extern portMUX_TYPE timerMux; extern portMUX_TYPE timerMux;
@ -49,6 +49,8 @@ extern portMUX_TYPE timerMux;
#include "gps.h" #include "gps.h"
#endif #endif
#include "payload.h"
#ifdef HAS_LORA #ifdef HAS_LORA
#include "lorawan.h" #include "lorawan.h"
#endif #endif
@ -69,20 +71,6 @@ extern portMUX_TYPE timerMux;
#include "antenna.h" #include "antenna.h"
#endif #endif
// class for preparing payload data
#include "payload.h"
// payload encoder
#if PAYLOAD_ENCODER == 1
extern TTNplain payload;
#elif PAYLOAD_ENCODER == 2
extern TTNpacked payload;
#elif PAYLOAD_ENCODER == 3
extern CayenneLPP payload;
#else
#error "No valid payload converter defined"
#endif
void reset_counters(void); void reset_counters(void);
void blink_LED(uint16_t set_color, uint16_t set_blinkduration); void blink_LED(uint16_t set_color, uint16_t set_blinkduration);
void led_loop(void); void led_loop(void);

View File

@ -28,8 +28,6 @@ licenses. Refer to LICENSE.txt file in repository for more details.
#include "main.h" #include "main.h"
configData_t cfg; // struct holds current device configuration configData_t cfg; // struct holds current device configuration
// Initialize variables
char display_line6[16], display_line7[16]; // display buffers char display_line6[16], display_line7[16]; // display buffers
uint8_t channel = 0; // channel rotation counter uint8_t channel = 0; // channel rotation counter
uint16_t macs_total = 0, macs_wifi = 0, uint16_t macs_total = 0, macs_wifi = 0,
@ -48,16 +46,8 @@ portMUX_TYPE timerMux =
std::set<uint16_t> macs; // associative container holds total of unique MAC std::set<uint16_t> macs; // associative container holds total of unique MAC
// adress hashes (Wifi + BLE) // adress hashes (Wifi + BLE)
// initialize payload encoder // initialize payload ncoder
#if PAYLOAD_ENCODER == 1 PayloadConvert payload(PAYLOAD_BUFFER_SIZE);
TTNplain payload(PAYLOAD_BUFFER_SIZE);
#elif PAYLOAD_ENCODER == 2
TTNpacked payload(PAYLOAD_BUFFER_SIZE);
#elif PAYLOAD_ENCODER == 3
CayenneLPP payload(PAYLOAD_BUFFER_SIZE);
#else
#error "No valid payload converter defined"
#endif
// local Tag for logging // local Tag for logging
static const char TAG[] = "main"; static const char TAG[] = "main";

View File

@ -36,7 +36,7 @@
#define WIFI_CHANNEL_SWITCH_INTERVAL 50 // [seconds/100] -> 0,5 sec. #define WIFI_CHANNEL_SWITCH_INTERVAL 50 // [seconds/100] -> 0,5 sec.
// LoRa payload default parameters // LoRa payload default parameters
#define PAYLOAD_ENCODER 2 // select payload encoder: 1=Plain [default], 2=Packed, 3=CayenneLPP #define PAYLOAD_ENCODER 2 // select payload encoder: 1=Plain, 2=Packed, 3=CayenneLPP
#define SEND_SECS 120 // payload send cycle [seconds/2] -> 240 sec. #define SEND_SECS 120 // payload send cycle [seconds/2] -> 240 sec.
#define MEM_LOW 2048 // [Bytes] low memory threshold triggering a send cycle #define MEM_LOW 2048 // [Bytes] low memory threshold triggering a send cycle
#define RETRANSMIT_RCMD 5 // [seconds] wait time before retransmitting rcommand results #define RETRANSMIT_RCMD 5 // [seconds] wait time before retransmitting rcommand results

View File

@ -4,20 +4,22 @@
/* ---------------- plain format without special encoding ---------- */ /* ---------------- plain format without special encoding ---------- */
TTNplain::TTNplain(uint8_t size) { #if PAYLOAD_ENCODER == 1
PayloadConvert::PayloadConvert(uint8_t size) {
buffer = (uint8_t *)malloc(size); buffer = (uint8_t *)malloc(size);
cursor = 0; cursor = 0;
} }
TTNplain::~TTNplain(void) { free(buffer); } PayloadConvert::~PayloadConvert(void) { free(buffer); }
void TTNplain::reset(void) { cursor = 0; } void PayloadConvert::reset(void) { cursor = 0; }
uint8_t TTNplain::getSize(void) { return cursor; } uint8_t PayloadConvert::getSize(void) { return cursor; }
uint8_t *TTNplain::getBuffer(void) { return buffer; } uint8_t *PayloadConvert::getBuffer(void) { return buffer; }
void TTNplain::addCount(uint16_t value1, uint16_t value2) { void PayloadConvert::addCount(uint16_t value1, uint16_t value2) {
buffer[cursor++] = value1 >> 8; buffer[cursor++] = value1 >> 8;
buffer[cursor++] = value1; buffer[cursor++] = value1;
buffer[cursor++] = value2 >> 8; buffer[cursor++] = value2 >> 8;
@ -25,7 +27,7 @@ void TTNplain::addCount(uint16_t value1, uint16_t value2) {
} }
#ifdef HAS_GPS #ifdef HAS_GPS
void TTNplain::addGPS(gpsStatus_t value) { void PayloadConvert::addGPS(gpsStatus_t value) {
buffer[cursor++] = value.latitude >> 24; buffer[cursor++] = value.latitude >> 24;
buffer[cursor++] = value.latitude >> 16; buffer[cursor++] = value.latitude >> 16;
buffer[cursor++] = value.latitude >> 8; buffer[cursor++] = value.latitude >> 8;
@ -42,7 +44,7 @@ void TTNplain::addGPS(gpsStatus_t value) {
} }
#endif #endif
void TTNplain::addConfig(configData_t value) { void PayloadConvert::addConfig(configData_t value) {
buffer[cursor++] = value.lorasf; buffer[cursor++] = value.lorasf;
buffer[cursor++] = value.txpower; buffer[cursor++] = value.txpower;
buffer[cursor++] = value.adrmode; buffer[cursor++] = value.adrmode;
@ -63,7 +65,7 @@ void TTNplain::addConfig(configData_t value) {
cursor += 10; cursor += 10;
} }
void TTNplain::addStatus(uint16_t voltage, uint64_t uptime, float cputemp) { void PayloadConvert::addStatus(uint16_t voltage, uint64_t uptime, float cputemp) {
buffer[cursor++] = voltage >> 8; buffer[cursor++] = voltage >> 8;
buffer[cursor++] = voltage; buffer[cursor++] = voltage;
buffer[cursor++] = uptime >> 56; buffer[cursor++] = uptime >> 56;
@ -80,30 +82,32 @@ void TTNplain::addStatus(uint16_t voltage, uint64_t uptime, float cputemp) {
buffer[cursor++] = (uint32_t)cputemp; buffer[cursor++] = (uint32_t)cputemp;
} }
#elif PAYLOAD_ENCODER == 2
/* ---------------- packed format with LoRa serialization Encoder ---------- */ /* ---------------- packed format with LoRa serialization Encoder ---------- */
// derived from // derived from
// https://github.com/thesolarnomad/lora-serialization/blob/master/src/LoraEncoder.cpp // https://github.com/thesolarnomad/lora-serialization/blob/master/src/LoraEncoder.cpp
TTNpacked::TTNpacked(uint8_t size) { PayloadConvert::PayloadConvert(uint8_t size) {
buffer = (uint8_t *)malloc(size); buffer = (uint8_t *)malloc(size);
cursor = 0; cursor = 0;
} }
TTNpacked::~TTNpacked(void) { free(buffer); } PayloadConvert::~PayloadConvert(void) { free(buffer); }
void TTNpacked::reset(void) { cursor = 0; } void PayloadConvert::reset(void) { cursor = 0; }
uint8_t TTNpacked::getSize(void) { return cursor; } uint8_t PayloadConvert::getSize(void) { return cursor; }
uint8_t *TTNpacked::getBuffer(void) { return buffer; } uint8_t *PayloadConvert::getBuffer(void) { return buffer; }
void TTNpacked::addCount(uint16_t value1, uint16_t value2) { void PayloadConvert::addCount(uint16_t value1, uint16_t value2) {
writeUint16(value1); writeUint16(value1);
writeUint16(value2); writeUint16(value2);
} }
#ifdef HAS_GPS #ifdef HAS_GPS
void TTNpacked::addGPS(gpsStatus_t value) { void PayloadConvert::addGPS(gpsStatus_t value) {
writeLatLng(value.latitude, value.longitude); writeLatLng(value.latitude, value.longitude);
writeUint8(value.satellites); writeUint8(value.satellites);
writeUint16(value.hdop); writeUint16(value.hdop);
@ -111,7 +115,7 @@ void TTNpacked::addGPS(gpsStatus_t value) {
} }
#endif #endif
void TTNpacked::addConfig(configData_t value) { void PayloadConvert::addConfig(configData_t value) {
writeUint8(value.lorasf); writeUint8(value.lorasf);
writeUint8(value.txpower); writeUint8(value.txpower);
writeUint16(value.rssilimit); writeUint16(value.rssilimit);
@ -125,33 +129,33 @@ void TTNpacked::addConfig(configData_t value) {
value.vendorfilter ? true : false, value.gpsmode ? true : false); value.vendorfilter ? true : false, value.gpsmode ? true : false);
} }
void TTNpacked::addStatus(uint16_t voltage, uint64_t uptime, float cputemp) { void PayloadConvert::addStatus(uint16_t voltage, uint64_t uptime, float cputemp) {
writeUint16(voltage); writeUint16(voltage);
writeUptime(uptime); writeUptime(uptime);
writeTemperature(cputemp); writeTemperature(cputemp);
} }
void TTNpacked::intToBytes(uint8_t pos, int32_t i, uint8_t byteSize) { void PayloadConvert::intToBytes(uint8_t pos, int32_t i, uint8_t byteSize) {
for (uint8_t x = 0; x < byteSize; x++) { for (uint8_t x = 0; x < byteSize; x++) {
buffer[x + pos] = (byte)(i >> (x * 8)); buffer[x + pos] = (byte)(i >> (x * 8));
} }
cursor += byteSize; cursor += byteSize;
} }
void TTNpacked::writeUptime(uint64_t uptime) { void PayloadConvert::writeUptime(uint64_t uptime) {
intToBytes(cursor, uptime, 8); intToBytes(cursor, uptime, 8);
} }
void TTNpacked::writeLatLng(double latitude, double longitude) { void PayloadConvert::writeLatLng(double latitude, double longitude) {
intToBytes(cursor, latitude, 4); intToBytes(cursor, latitude, 4);
intToBytes(cursor, longitude, 4); intToBytes(cursor, longitude, 4);
} }
void TTNpacked::writeUint16(uint16_t i) { intToBytes(cursor, i, 2); } void PayloadConvert::writeUint16(uint16_t i) { intToBytes(cursor, i, 2); }
void TTNpacked::writeUint8(uint8_t i) { intToBytes(cursor, i, 1); } void PayloadConvert::writeUint8(uint8_t i) { intToBytes(cursor, i, 1); }
void TTNpacked::writeHumidity(float humidity) { void PayloadConvert::writeHumidity(float humidity) {
int16_t h = (int16_t)(humidity * 100); int16_t h = (int16_t)(humidity * 100);
intToBytes(cursor, h, 2); intToBytes(cursor, h, 2);
} }
@ -160,7 +164,7 @@ void TTNpacked::writeHumidity(float humidity) {
* Uses a 16bit two's complement with two decimals, so the range is * Uses a 16bit two's complement with two decimals, so the range is
* -327.68 to +327.67 degrees * -327.68 to +327.67 degrees
*/ */
void TTNpacked::writeTemperature(float temperature) { void PayloadConvert::writeTemperature(float temperature) {
int16_t t = (int16_t)(temperature * 100); int16_t t = (int16_t)(temperature * 100);
if (temperature < 0) { if (temperature < 0) {
t = ~-t; t = ~-t;
@ -170,7 +174,7 @@ void TTNpacked::writeTemperature(float temperature) {
buffer[cursor++] = (byte)t & 0xFF; buffer[cursor++] = (byte)t & 0xFF;
} }
void TTNpacked::writeBitmap(bool a, bool b, bool c, bool d, bool e, bool f, void PayloadConvert::writeBitmap(bool a, bool b, bool c, bool d, bool e, bool f,
bool g, bool h) { bool g, bool h) {
uint8_t bitmap = 0; uint8_t bitmap = 0;
// LSB first // LSB first
@ -185,22 +189,24 @@ void TTNpacked::writeBitmap(bool a, bool b, bool c, bool d, bool e, bool f,
writeUint8(bitmap); writeUint8(bitmap);
} }
#elif PAYLOAD_ENCODER == 3
/* ---------------- Cayenne LPP format ---------- */ /* ---------------- Cayenne LPP format ---------- */
CayenneLPP::CayenneLPP(uint8_t size) { PayloadConvert::PayloadConvert(uint8_t size) {
buffer = (uint8_t *)malloc(size); buffer = (uint8_t *)malloc(size);
cursor = 0; cursor = 0;
} }
CayenneLPP::~CayenneLPP(void) { free(buffer); } PayloadConvert::~PayloadConvert(void) { free(buffer); }
void CayenneLPP::reset(void) { cursor = 0; } void PayloadConvert::reset(void) { cursor = 0; }
uint8_t CayenneLPP::getSize(void) { return cursor; } uint8_t PayloadConvert::getSize(void) { return cursor; }
uint8_t *CayenneLPP::getBuffer(void) { return buffer; } uint8_t *PayloadConvert::getBuffer(void) { return buffer; }
void CayenneLPP::addCount(uint16_t value1, uint16_t value2) { void PayloadConvert::addCount(uint16_t value1, uint16_t value2) {
buffer[cursor++] = LPP_COUNT_WIFI_CHANNEL; buffer[cursor++] = LPP_COUNT_WIFI_CHANNEL;
buffer[cursor++] = LPP_ANALOG_INPUT; // workaround, type meter not found? buffer[cursor++] = LPP_ANALOG_INPUT; // workaround, type meter not found?
buffer[cursor++] = value1 >> 8; buffer[cursor++] = value1 >> 8;
@ -212,7 +218,7 @@ void CayenneLPP::addCount(uint16_t value1, uint16_t value2) {
} }
#ifdef HAS_GPS #ifdef HAS_GPS
void CayenneLPP::addGPS(gpsStatus_t value) { void PayloadConvert::addGPS(gpsStatus_t value) {
int32_t lat = value.latitude / 100; int32_t lat = value.latitude / 100;
int32_t lon = value.longitude / 100; int32_t lon = value.longitude / 100;
int32_t alt = value.altitude; int32_t alt = value.altitude;
@ -230,13 +236,13 @@ void CayenneLPP::addGPS(gpsStatus_t value) {
} }
#endif #endif
void CayenneLPP::addConfig(configData_t value) { void PayloadConvert::addConfig(configData_t value) {
buffer[cursor++] = LPP_ADR_CHANNEL; buffer[cursor++] = LPP_ADR_CHANNEL;
buffer[cursor++] = LPP_DIGITAL_INPUT; buffer[cursor++] = LPP_DIGITAL_INPUT;
buffer[cursor++] = value.adrmode; buffer[cursor++] = value.adrmode;
} }
void CayenneLPP::addStatus(uint16_t voltage, uint64_t uptime, float cputemp) { void PayloadConvert::addStatus(uint16_t voltage, uint64_t uptime, float cputemp) {
buffer[cursor++] = LPP_BATT_CHANNEL; buffer[cursor++] = LPP_BATT_CHANNEL;
buffer[cursor++] = LPP_ANALOG_INPUT; buffer[cursor++] = LPP_ANALOG_INPUT;
buffer[cursor++] = voltage >> 8; buffer[cursor++] = voltage >> 8;
@ -245,4 +251,8 @@ void CayenneLPP::addStatus(uint16_t voltage, uint64_t uptime, float cputemp) {
buffer[cursor++] = LPP_TEMPERATURE; buffer[cursor++] = LPP_TEMPERATURE;
buffer[cursor++] = (uint16_t)cputemp >> 8; buffer[cursor++] = (uint16_t)cputemp >> 8;
buffer[cursor++] = (uint16_t)cputemp; buffer[cursor++] = (uint16_t)cputemp;
} }
#else
#error "No valid payload converter defined"
#endif

View File

@ -18,15 +18,15 @@
#define LPP_ANALOG_INPUT 2 // 2 bytes, 0.01 signed #define LPP_ANALOG_INPUT 2 // 2 bytes, 0.01 signed
#define LPP_LUMINOSITY 101 // 2 bytes, 1 lux unsigned #define LPP_LUMINOSITY 101 // 2 bytes, 1 lux unsigned
class TTNplain { class PayloadConvert {
public: public:
TTNplain(uint8_t size); PayloadConvert(uint8_t size);
~TTNplain(); ~PayloadConvert();
void reset(void); void reset(void);
uint8_t getSize(void); uint8_t getSize(void);
uint8_t *getBuffer(void); uint8_t *getBuffer(void);
void addCount(uint16_t value1, uint16_t value2); void addCount(uint16_t value1, uint16_t value2);
void addConfig(configData_t value); void addConfig(configData_t value);
void addStatus(uint16_t voltage, uint64_t uptime, float cputemp); void addStatus(uint16_t voltage, uint64_t uptime, float cputemp);
@ -34,26 +34,14 @@ public:
void addGPS(gpsStatus_t value); void addGPS(gpsStatus_t value);
#endif #endif
#if PAYLOAD_ENCODER == 1 // format plain
private: private:
uint8_t *buffer; uint8_t *buffer;
uint8_t cursor; uint8_t cursor;
}; };
class TTNpacked { #elif PAYLOAD_ENCODER == 2 // format packed
public:
TTNpacked(uint8_t size);
~TTNpacked();
void reset(void);
uint8_t getSize(void);
uint8_t *getBuffer(void);
void addCount(uint16_t value1, uint16_t value2);
void addConfig(configData_t value);
void addStatus(uint16_t voltage, uint64_t uptime, float cputemp);
#ifdef HAS_GPS
void addGPS(gpsStatus_t value);
#endif
private: private:
uint8_t *buffer; uint8_t *buffer;
@ -69,21 +57,7 @@ private:
bool h); bool h);
}; };
class CayenneLPP { #elif PAYLOAD_ENCODER == 3 // format cayenne lpp
public:
CayenneLPP(uint8_t size);
~CayenneLPP();
void reset(void);
uint8_t getSize(void);
uint8_t *getBuffer(void);
void addCount(uint16_t value1, uint16_t value2);
void addConfig(configData_t value);
void addStatus(uint16_t voltage, uint64_t uptime, float cputemp);
#ifdef HAS_GPS
void addGPS(gpsStatus_t value);
#endif
private: private:
uint8_t *buffer; uint8_t *buffer;
@ -91,4 +65,10 @@ private:
uint8_t cursor; uint8_t cursor;
}; };
#endif // _PAYLOAD_H_ #else
#error "No valid payload converter defined"
#endif
extern PayloadConvert payload;
#endif // _PAYLOAD_H_