Refactor command line arguments

This commit is contained in:
Theo Arends 2021-01-30 14:52:53 +01:00
parent aea856ca2c
commit f43c098158
3 changed files with 49 additions and 62 deletions

View File

@ -276,11 +276,20 @@ char* subStr(char* dest, char* str, const char *delim, int index) {
return dest;
}
char* ArgV(char* dest, int index) {
return subStr(dest, XdrvMailbox.data, ",", index);
}
uint32_t ParseParameters(uint32_t count, uint32_t *params)
{
char *p;
uint32_t i = 0;
for (char *str = strtok_r(XdrvMailbox.data, ", ", &p); str && i < count; str = strtok_r(nullptr, ", ", &p), i++) {
params[i] = strtoul(str, nullptr, 0);
}
return i;
}
float CharToFloat(const char *str)
{
// simple ascii to double, because atof or strtod are too large
@ -631,16 +640,6 @@ bool ParseIPv4(uint32_t* addr, const char* str_p)
return (3 == i);
}
uint32_t ParseParameters(uint32_t count, uint32_t *params)
{
char *p;
uint32_t i = 0;
for (char *str = strtok_r(XdrvMailbox.data, ", ", &p); str && i < count; str = strtok_r(nullptr, ", ", &p), i++) {
params[i] = strtoul(str, nullptr, 0);
}
return i;
}
// Function to parse & check if version_str is newer than our currently installed version.
bool NewerVersion(char* version_str)
{

View File

@ -1208,13 +1208,13 @@ void CmndKnxGa(void)
{
if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= MAX_KNX_GA)) {
if (XdrvMailbox.data_len) {
if (strchr(XdrvMailbox.data, ',') != nullptr) { // Process parameter entry
char sub_string[XdrvMailbox.data_len];
if (ArgC() > 1) { // Process parameter entry
char argument[XdrvMailbox.data_len];
int ga_option = atoi(subStr(sub_string, XdrvMailbox.data, ",", 1));
int ga_area = atoi(subStr(sub_string, XdrvMailbox.data, ",", 2));
int ga_line = atoi(subStr(sub_string, XdrvMailbox.data, ",", 3));
int ga_member = atoi(subStr(sub_string, XdrvMailbox.data, ",", 4));
int ga_option = atoi(ArgV(argument, 1));
int ga_area = atoi(ArgV(argument, 2));
int ga_line = atoi(ArgV(argument, 3));
int ga_member = atoi(ArgV(argument, 4));
if ( ((ga_area == 0) && (ga_line == 0) && (ga_member == 0))
|| (ga_area > 31) || (ga_line > 7) || (ga_member > 255)
@ -1259,13 +1259,13 @@ void CmndKnxCb(void)
{
if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= MAX_KNX_CB)) {
if (XdrvMailbox.data_len) {
if (strchr(XdrvMailbox.data, ',') != nullptr) { // Process parameter entry
char sub_string[XdrvMailbox.data_len];
if (ArgC() > 1) { // Process parameter entry
char argument[XdrvMailbox.data_len];
int cb_option = atoi(subStr(sub_string, XdrvMailbox.data, ",", 1));
int cb_area = atoi(subStr(sub_string, XdrvMailbox.data, ",", 2));
int cb_line = atoi(subStr(sub_string, XdrvMailbox.data, ",", 3));
int cb_member = atoi(subStr(sub_string, XdrvMailbox.data, ",", 4));
int cb_option = atoi(ArgV(argument, 1));
int cb_area = atoi(ArgV(argument, 2));
int cb_line = atoi(ArgV(argument, 3));
int cb_member = atoi(ArgV(argument, 4));
if ( ((cb_area == 0) && (cb_line == 0) && (cb_member == 0))
|| (cb_area > 31) || (cb_line > 7) || (cb_member > 255)

View File

@ -289,48 +289,36 @@ void WindMeterTriggerTele(void)
bool Xsns68Cmnd(void)
{
bool serviced = true;
bool show_parms = true;
char sub_string[XdrvMailbox.data_len +1];
switch (XdrvMailbox.payload) {
case 1:
if (strchr(XdrvMailbox.data, ',') != nullptr) {
Settings.windmeter_radius = (uint16_t)strtol(subStr(sub_string, XdrvMailbox.data, ",", 2), nullptr, 10);
}
break;
case 2:
if (strchr(XdrvMailbox.data, ',') != nullptr) {
Settings.windmeter_pulses_x_rot = (uint8_t)strtol(subStr(sub_string, XdrvMailbox.data, ",", 2), nullptr, 10);
}
break;
case 3:
if (strchr(XdrvMailbox.data, ',') != nullptr) {
Settings.windmeter_pulse_debounce = (uint16_t)strtol(subStr(sub_string, XdrvMailbox.data, ",", 2), nullptr, 10);
}
break;
case 4:
if (strchr(XdrvMailbox.data, ',') != nullptr) {
Settings.windmeter_speed_factor = (int16_t)(CharToFloat(subStr(sub_string, XdrvMailbox.data, ",", 2)) * 1000);
}
break;
case 5:
if (strchr(XdrvMailbox.data, ',') != nullptr) {
Settings.windmeter_tele_pchange = (uint8_t)strtol(subStr(sub_string, XdrvMailbox.data, ",", 2), nullptr, 10);
}
break;
if (ArgC() > 1) {
char argument[XdrvMailbox.data_len];
switch (XdrvMailbox.payload) {
case 1:
Settings.windmeter_radius = (uint16_t)strtol(ArgV(argument, 2), nullptr, 10);
break;
case 2:
Settings.windmeter_pulses_x_rot = (uint8_t)strtol(ArgV(argument, 2), nullptr, 10);
break;
case 3:
Settings.windmeter_pulse_debounce = (uint16_t)strtol(ArgV(argument, 2), nullptr, 10);
break;
case 4:
Settings.windmeter_speed_factor = (int16_t)(CharToFloat(ArgV(argument, 2)) * 1000);
break;
case 5:
Settings.windmeter_tele_pchange = (uint8_t)strtol(ArgV(argument, 2), nullptr, 10);
break;
}
}
if (show_parms) {
char speed_factor_string[FLOATSZ];
dtostrfd((float)Settings.windmeter_speed_factor / 1000, 3, speed_factor_string);
char tele_pchange_string[4] = "off";
if (Settings.windmeter_tele_pchange <= 100) {
itoa(Settings.windmeter_tele_pchange, tele_pchange_string, 10);
}
Response_P(PSTR("{\"" D_WINDMETER_NAME "\":{\"Radius\":%d,\"PulsesPerRot\":%d,\"PulseDebounce\":%d,\"SpeedFactor\":%s,\"TeleTriggerMin%Change\":%s}}"),
Settings.windmeter_radius, Settings.windmeter_pulses_x_rot, Settings.windmeter_pulse_debounce, speed_factor_string, tele_pchange_string);
float speed_factor = (float)Settings.windmeter_speed_factor / 1000;
char tele_pchange_string[4] = "off";
if (Settings.windmeter_tele_pchange <= 100) {
itoa(Settings.windmeter_tele_pchange, tele_pchange_string, 10);
}
return serviced;
Response_P(PSTR("{\"" D_WINDMETER_NAME "\":{\"Radius\":%d,\"PulsesPerRot\":%d,\"PulseDebounce\":%d,\"SpeedFactor\":%3_f,\"TeleTriggerMin%Change\":%s}}"),
Settings.windmeter_radius, Settings.windmeter_pulses_x_rot, Settings.windmeter_pulse_debounce, &speed_factor, tele_pchange_string);
return true;
}
/*********************************************************************************************\