Bug Fix: bounds checks for LVC did not allow 0

This commit is contained in:
Ray Jones 2019-07-23 06:15:18 +10:00
parent 672645c59a
commit db3343d362
4 changed files with 26 additions and 19 deletions

View file

@ -32,6 +32,7 @@
#include "../cfg/BTCConfig.h"
#include "macros.h"
#include "../Protocol/Protocol.h"
#include <string.h>
char defaultJSONstr[64];
CModerator JSONmoderator;
@ -289,17 +290,15 @@ void interpretJsonCommand(char* pLine)
}
else if(strcmp("LowVoltCutout", it->key) == 0) {
float fCal = it->value.as<float>();
if(fCal != 0) {
bool bOK = false;
if(NVstore.getHeaterTuning().sysVoltage == 120)
bOK = INBOUNDS(fCal, 10.0, 12.5);
else
bOK = INBOUNDS(fCal, 20.0, 25.0);
if(bOK) {
sHeaterTuning ht = NVstore.getHeaterTuning();
ht.lowVolts = uint8_t(fCal * 10);
NVstore.setHeaterTuning(ht);
}
bool bOK = false;
if(NVstore.getHeaterTuning().sysVoltage == 120)
bOK |= (fCal == 0) || INBOUNDS(fCal, 10.0, 12.5);
else
bOK |= (fCal == 0) || INBOUNDS(fCal, 20.0, 25.0);
if(bOK) {
sHeaterTuning ht = NVstore.getHeaterTuning();
ht.lowVolts = uint8_t(fCal * 10);
NVstore.setHeaterTuning(ht);
}
}
}

View file

@ -23,6 +23,7 @@
#include "NVCore.h"
#include "DebugPort.h"
#include <functional>
#include <string.h>
#define INBOUNDS(TST, MIN, MAX) (((TST) >= (MIN)) && ((TST) <= (MAX)))
@ -45,7 +46,7 @@ CESP32_NVStorage::validatedLoad(const char* key, char* val, int maxlen, const ch
}
bool
CESP32_NVStorage::validatedLoad(const char* key, uint8_t& val, int defVal, std::function<bool(uint8_t, uint8_t, uint8_t)> validator, int min, int max, uint8_t mask)
CESP32_NVStorage::validatedLoad(const char* key, uint8_t& val, uint8_t defVal, std::function<bool(uint8_t, uint8_t, uint8_t)> validator, uint8_t min, uint8_t max, uint8_t mask)
{
val = preferences.getUChar(key, defVal);
if(!validator(val & mask, min, max)) {
@ -61,7 +62,7 @@ CESP32_NVStorage::validatedLoad(const char* key, uint8_t& val, int defVal, std::
}
bool
CESP32_NVStorage::validatedLoad(const char* key, int8_t& val, int defVal, std::function<bool(int8_t, int8_t, int8_t)> validator, int min, int max)
CESP32_NVStorage::validatedLoad(const char* key, int8_t& val, int8_t defVal, std::function<bool(int8_t, int8_t, int8_t)> validator, int8_t min, int8_t max)
{
val = preferences.getChar(key, defVal);
if(!validator(val, min, max)) {
@ -77,7 +78,7 @@ CESP32_NVStorage::validatedLoad(const char* key, int8_t& val, int defVal, std::f
}
bool
CESP32_NVStorage::validatedLoad(const char* key, uint16_t& val, int defVal, std::function<bool(uint16_t, uint16_t, uint16_t)> validator, int min, int max)
CESP32_NVStorage::validatedLoad(const char* key, uint16_t& val, uint16_t defVal, std::function<bool(uint16_t, uint16_t, uint16_t)> validator, uint16_t min, uint16_t max)
{
val = preferences.getUShort(key, defVal);
if(!validator(val, min, max)) {
@ -134,6 +135,12 @@ bool u8inBounds(uint8_t test, uint8_t minLim, uint8_t maxLim)
return INBOUNDS(test, minLim, maxLim);
}
bool u8inBoundsOrZero(uint8_t test, uint8_t minLim, uint8_t maxLim)
{
return INBOUNDS(test, minLim, maxLim) || (test == 0);
}
bool s8inBounds(int8_t test, int8_t minLim, int8_t maxLim)
{
return INBOUNDS(test, minLim, maxLim);

View file

@ -28,6 +28,7 @@
bool finBounds(float tets, float min, float max);
bool u8inBounds(uint8_t test, uint8_t minLim, uint8_t maxLim);
bool u8inBoundsOrZero(uint8_t test, uint8_t minLim, uint8_t maxLim);
bool s8inBounds(int8_t test, int8_t minLim, int8_t maxLim);
bool u8Match2(uint8_t test, uint8_t test1, uint8_t test2);
bool u16inBounds(uint16_t test, uint16_t minLim, uint16_t maxLim);
@ -47,9 +48,9 @@ class CESP32_NVStorage {
protected:
Preferences preferences;
protected:
bool validatedLoad(const char* key, int8_t& val, int defVal, std::function<bool(int8_t, int8_t, int8_t)> validator, int min, int max);
bool validatedLoad(const char* key, uint8_t& val, int defVal, std::function<bool(uint8_t, uint8_t, uint8_t)> validator, int min, int max, uint8_t mask=0xff);
bool validatedLoad(const char* key, uint16_t& val, int defVal, std::function<bool(uint16_t, uint16_t, uint16_t)> validator, int min, int max);
bool validatedLoad(const char* key, int8_t& val, int8_t defVal, std::function<bool(int8_t, int8_t, int8_t)> validator, int8_t min, int8_t max);
bool validatedLoad(const char* key, uint8_t& val, uint8_t defVal, std::function<bool(uint8_t, uint8_t, uint8_t)> validator, uint8_t min, uint8_t max, uint8_t mask=0xff);
bool validatedLoad(const char* key, uint16_t& val, uint16_t defVal, std::function<bool(uint16_t, uint16_t, uint16_t)> validator, uint16_t min, uint16_t max);
bool validatedLoad(const char* key, long& val, long defVal, long min, long max);
bool validatedLoad(const char* key, char* val, int maxlen, const char* defVal);
bool validatedLoad(const char* key, float& val, float defVal, float min, float max);

View file

@ -241,9 +241,9 @@ sHeaterTuning::load()
validatedLoad("fanSensor", fanSensor, 1, u8inBounds, 1, 2);
validatedLoad("glowDrive", glowDrive, 5, u8inBounds, 1, 6);
if(sysVoltage == 120)
validatedLoad("lowVolts", lowVolts, 115, u8inBounds, 100, 125);
validatedLoad("lowVolts", lowVolts, 115, u8inBoundsOrZero, 100, 125);
else
validatedLoad("lowVolts", lowVolts, 230, u8inBounds, 200, 250);
validatedLoad("lowVolts", lowVolts, 230, u8inBoundsOrZero, 200, 250);
validatedLoad("pumpCal", pumpCal, 0.02, 0.001, 1);
validatedLoad("tempOfs", tempOfs, 0.0, -10.0, +10.0);
preferences.end();