Merge pull request #445 from cyberman54/development

fix Issue #444 & lorawan.cpp sanitizations
This commit is contained in:
Verkehrsrot 2019-09-20 09:05:57 +02:00 committed by GitHub
commit 32f11694ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 67 additions and 46 deletions

View File

@ -49,6 +49,7 @@ void myRxCallback(void *pUserData, uint8_t port, const uint8_t *pMsg,
void myTxCallback(void *pUserData, int fSuccess); void myTxCallback(void *pUserData, int fSuccess);
void mac_decode(const uint8_t cmd[], const uint8_t cmdlen, const mac_t table[], void mac_decode(const uint8_t cmd[], const uint8_t cmdlen, const mac_t table[],
const uint8_t tablesize); const uint8_t tablesize);
uint8_t getBattLevel(void);
#if (TIME_SYNC_LORAWAN) #if (TIME_SYNC_LORAWAN)
void user_request_network_time_callback(void *pVoidUserUTCTime, void user_request_network_time_callback(void *pVoidUserUTCTime,

View File

@ -19,6 +19,6 @@ void AXP192_init(void);
void AXP192_displaypower(void); void AXP192_displaypower(void);
uint16_t read_voltage(void); uint16_t read_voltage(void);
void calibrate_voltage(void); void calibrate_voltage(void);
uint8_t getBattLevel(void); bool batt_sufficient(void);
#endif #endif

View File

@ -57,13 +57,16 @@ void doHousekeeping() {
// read battery voltage into global variable // read battery voltage into global variable
#if (defined BAT_MEASURE_ADC || defined HAS_PMU) #if (defined BAT_MEASURE_ADC || defined HAS_PMU)
batt_voltage = read_voltage(); batt_voltage = read_voltage();
ESP_LOGI(TAG, "Voltage: %dmV", batt_voltage); if (batt_voltage = 0xffff)
ESP_LOGI(TAG, "Battery: external power");
else
ESP_LOGI(TAG, "Battery: %dmV", batt_voltage);
#ifdef HAS_PMU #ifdef HAS_PMU
if (I2C_MUTEX_LOCK()) { if (I2C_MUTEX_LOCK()) {
AXP192_displaypower(); AXP192_displaypower();
I2C_MUTEX_UNLOCK(); I2C_MUTEX_UNLOCK();
} }
#endif // HAS_PMU #endif
#endif #endif
// display BME680/280 sensor data // display BME680/280 sensor data

View File

@ -234,13 +234,10 @@ void onEvent(ev_t ev) {
sprintf(display_line6, " "); // clear previous lmic status sprintf(display_line6, " "); // clear previous lmic status
// set data rate adaptation according to saved setting // set data rate adaptation according to saved setting
LMIC_setAdrMode(cfg.adrmode); LMIC_setAdrMode(cfg.adrmode);
// set cyclic lmic link check to off if no ADR because is not supported by // set data rate and transmit power if we have no ADR
// ttn (but enabled by lmic after join) if (!cfg.adrmode)
LMIC_setLinkCheckMode(cfg.adrmode); switch_lora(cfg.lorasf, cfg.txpower);
// Set data rate and transmit power (note: txpower seems to be ignored by // show current devaddr
// the library)
switch_lora(cfg.lorasf, cfg.txpower);
// show effective LoRa parameters after join
ESP_LOGI(TAG, "DEVaddr=%08X", LMIC.devaddr); ESP_LOGI(TAG, "DEVaddr=%08X", LMIC.devaddr);
break; break;
@ -541,6 +538,7 @@ void lmictask(void *pvParameters) {
os_init(); // initialize lmic run-time environment os_init(); // initialize lmic run-time environment
LMIC_reset(); // initialize lmic MAC LMIC_reset(); // initialize lmic MAC
LMIC_setLinkCheckMode(0); LMIC_setLinkCheckMode(0);
// This tells LMIC to make the receive windows bigger, in case your clock is // This tells LMIC to make the receive windows bigger, in case your clock is
// faster or slower. This causes the transceiver to be earlier switched on, // faster or slower. This causes the transceiver to be earlier switched on,
// so consuming more power. You may sharpen (reduce) CLOCK_ERROR_PERCENTAGE // so consuming more power. You may sharpen (reduce) CLOCK_ERROR_PERCENTAGE
@ -548,15 +546,9 @@ void lmictask(void *pvParameters) {
#ifdef CLOCK_ERROR_PROCENTAGE #ifdef CLOCK_ERROR_PROCENTAGE
LMIC_setClockError(MAX_CLOCK_ERROR * CLOCK_ERROR_PROCENTAGE / 100); LMIC_setClockError(MAX_CLOCK_ERROR * CLOCK_ERROR_PROCENTAGE / 100);
#endif #endif
// Set the data rate to Spreading Factor 7. This is the fastest supported
// rate for 125 kHz channels, and it minimizes air time and battery power.
// Set the transmission power to 14 dBi (25 mW).
LMIC_setDrTxpow(DR_SF7, 14);
// register a callback for downlink messages. We aren't trying to write
// reentrant code, so pUserData is NULL.
LMIC_registerRxMessageCb(myRxCallback, NULL);
#if defined(CFG_US915) || defined(CFG_au921) //#if defined(CFG_US915) || defined(CFG_au921)
#if CFG_LMIC_US_like
// in the US, with TTN, it saves join time if we start on subband 1 // in the US, with TTN, it saves join time if we start on subband 1
// (channels 8-15). This will get overridden after the join by parameters // (channels 8-15). This will get overridden after the join by parameters
// from the network. If working with other networks or in other regions, // from the network. If working with other networks or in other regions,
@ -564,6 +556,15 @@ void lmictask(void *pvParameters) {
LMIC_selectSubBand(1); LMIC_selectSubBand(1);
#endif #endif
// Set the data rate to Spreading Factor 7. This is the fastest supported
// rate for 125 kHz channels, and it minimizes air time and battery power.
// Set the transmission power to 14 dBi (25 mW).
LMIC_setDrTxpow(DR_SF7, 14);
// register a callback for downlink messages. We aren't trying to write
// reentrant code, so pUserData is NULL.
LMIC_registerRxMessageCb(myRxCallback, NULL);
while (1) { while (1) {
os_runloop_once(); // execute lmic scheduled jobs and events os_runloop_once(); // execute lmic scheduled jobs and events
delay(2); // yield to CPU delay(2); // yield to CPU
@ -665,4 +666,30 @@ void mac_decode(const uint8_t cmd[], const uint8_t cmdlen, const mac_t table[],
} // mac_decode() } // mac_decode()
uint8_t getBattLevel() {
/*
return values:
MCMD_DEVS_EXT_POWER = 0x00, // external power supply
MCMD_DEVS_BATT_MIN = 0x01, // min battery value
MCMD_DEVS_BATT_MAX = 0xFE, // max battery value
MCMD_DEVS_BATT_NOINFO = 0xFF, // unknown battery level
*/
#if (defined HAS_PMU || defined BAT_MEASURE_ADC)
uint16_t voltage = read_voltage();
switch (voltage) {
case 0:
return MCMD_DEVS_BATT_NOINFO;
case 0xffff:
return MCMD_DEVS_EXT_POWER;
default:
return (voltage > OTA_MIN_BATT ? MCMD_DEVS_BATT_MAX : MCMD_DEVS_BATT_MIN);
}
#else // we don't have any info on battery level
return MCMD_DEVS_BATT_NOINFO;
#endif
} // getBattLevel()
//u1_t os_getBattLevel(void) { return getBattLevel(); };
#endif // HAS_LORA #endif // HAS_LORA

View File

@ -41,7 +41,7 @@ inline String getHeaderValue(String header, String headerName) {
void start_ota_update() { void start_ota_update() {
// check battery status if we can before doing ota // check battery status if we can before doing ota
if (getBattLevel() == MCMD_DEVS_BATT_MIN) { if (!batt_sufficient()) {
ESP_LOGE(TAG, "Battery voltage %dmV too low for OTA", batt_voltage); ESP_LOGE(TAG, "Battery voltage %dmV too low for OTA", batt_voltage);
return; return;
} }

View File

@ -78,12 +78,18 @@ void AXP192_power(bool on) {
void AXP192_displaypower(void) { void AXP192_displaypower(void) {
if (pmu.isBatteryConnect()) if (pmu.isBatteryConnect())
if (pmu.isChargeing()) if (pmu.isChargeing())
ESP_LOGI(TAG, "Battery charging @ %.0fmAh", pmu.getBattChargeCurrent()); ESP_LOGI(TAG, "Battery charging %.0fmAh @ Temp %.1f°C",
pmu.getBattChargeCurrent(), pmu.getTSTemp());
else else
ESP_LOGI(TAG, "Battery discharging @ %0.fmAh", ESP_LOGI(TAG, "Battery not charging, Temp %.1f°C", pmu.getTSTemp());
pmu.getBattDischargeCurrent());
else else
ESP_LOGI(TAG, "No Battery"); ESP_LOGI(TAG, "No Battery");
if (pmu.isVBUSPlug())
ESP_LOGI(TAG, "USB present, %.0fmAh @ %.1fV", pmu.getVbusCurrent(),
pmu.getVbusVoltage());
else
ESP_LOGI(TAG, "USB not present");
} }
void AXP192_init(void) { void AXP192_init(void) {
@ -111,7 +117,7 @@ void AXP192_init(void) {
pmu.clearIRQ(); pmu.clearIRQ();
#endif // PMU_INT #endif // PMU_INT
ESP_LOGI(TAG, "AXP192 PMU initialized."); ESP_LOGI(TAG, "AXP192 PMU initialized, chip Temp %.1f°C", pmu.getTemp());
AXP192_displaypower(); AXP192_displaypower();
} }
I2C_MUTEX_UNLOCK(); // release i2c bus access I2C_MUTEX_UNLOCK(); // release i2c bus access
@ -161,31 +167,15 @@ void calibrate_voltage(void) {
#endif #endif
} }
uint8_t getBattLevel() { bool batt_sufficient() {
/*
return values:
MCMD_DEVS_EXT_POWER = 0x00, // external power supply
MCMD_DEVS_BATT_MIN = 0x01, // min battery value
MCMD_DEVS_BATT_MAX = 0xFE, // max battery value
MCMD_DEVS_BATT_NOINFO = 0xFF, // unknown battery level
*/
#if (defined HAS_PMU || defined BAT_MEASURE_ADC) #if (defined HAS_PMU || defined BAT_MEASURE_ADC)
uint16_t voltage = read_voltage(); uint16_t volts = read_voltage();
return ((volts < 1000) ||
switch (voltage) { (volts > OTA_MIN_BATT)); // no battery or battery sufficient
case 0: #else
return MCMD_DEVS_BATT_NOINFO; return true;
case 0xffff:
return MCMD_DEVS_EXT_POWER;
default:
return (voltage > OTA_MIN_BATT ? MCMD_DEVS_BATT_MAX : MCMD_DEVS_BATT_MIN);
}
#else // we don't have any info on battery level
return MCMD_DEVS_BATT_NOINFO;
#endif #endif
} // getBattLevel() }
// u1_t os_getBattLevel(void) { return getBattLevel(); };
uint16_t read_voltage() { uint16_t read_voltage() {