diff --git a/src/command.c b/src/command.c index dd3d462..61cc741 100644 --- a/src/command.c +++ b/src/command.c @@ -145,6 +145,33 @@ int uint32_setting(const char *cmd, const char *args, int query, char *prev_cmd, return 1; } +int uint8_setting(const char *cmd, const char *args, int query, char *prev_cmd, + uint8_t *var, uint8_t min_val, uint8_t max_val, const char *name) +{ + uint8_t val; + int v; + + if (query) { + printf("%u\n", *var); + return 0; + } + + if (str_to_int(args, &v, 10)) { + val = v; + if (val >= min_val && val <= max_val) { + if (*var != val) { + log_msg(LOG_NOTICE, "%s change %u --> %u", name, *var, val); + *var = val; + } + } else { + log_msg(LOG_WARNING, "Invalid %s value: %s", name, args); + return 2; + } + return 0; + } + return 1; +} + int bool_setting(const char *cmd, const char *args, int query, char *prev_cmd, bool *var, const char *name) { @@ -1811,19 +1838,9 @@ int cmd_wifi_stats(const char *cmd, const char *args, int query, char *prev_cmd) int cmd_wifi_country(const char *cmd, const char *args, int query, char *prev_cmd) { - if (query) { - printf("%s\n", conf->wifi_country); - } else { - if (valid_wifi_country(args)) { - log_msg(LOG_NOTICE, "Wi-Fi Country change '%s' --> '%s'", - conf->wifi_country, args); - strncopy(conf->wifi_country, args, sizeof(conf->wifi_country)); - } else { - log_msg(LOG_WARNING, "Invalid Wi-Fi country: %s", args); - return 2; - } - } - return 0; + return string_setting(cmd, args, query, prev_cmd, + conf->wifi_country, sizeof(conf->wifi_country), + "WiFi Country", valid_wifi_country); } int cmd_wifi_password(const char *cmd, const char *args, int query, char *prev_cmd) @@ -1834,40 +1851,15 @@ int cmd_wifi_password(const char *cmd, const char *args, int query, char *prev_c int cmd_wifi_hostname(const char *cmd, const char *args, int query, char *prev_cmd) { - if (query) { - printf("%s\n", conf->hostname); - } else { - for (int i = 0; i < strlen(args); i++) { - if (!(isalpha((int)args[i]) || args[i] == '-')) { - return 1; - } - } - log_msg(LOG_NOTICE, "System hostname change '%s' --> '%s'", conf->hostname, args); - strncopy(conf->hostname, args, sizeof(conf->hostname)); - } - return 0; + return string_setting(cmd, args, query, prev_cmd, + conf->hostname, sizeof(conf->hostname), + "WiFi Hostname", valid_hostname); } int cmd_wifi_mode(const char *cmd, const char *args, int query, char *prev_cmd) { - int val; - - if (query) { - printf("%u\n", conf->wifi_mode); - return 0; - } - - if (str_to_int(args, &val, 10)) { - if (val >= 0 && val <= 1) { - log_msg(LOG_NOTICE, "WiFi mode change %d --> %d", cfg->wifi_mode, val); - conf->wifi_mode = val; - } else { - log_msg(LOG_WARNING, "Invalid WiFi mode: %s", args); - return 2; - } - return 0; - } - return 1; + return uint8_setting(cmd, args, query, prev_cmd, + &conf->wifi_mode, 0, 1, "WiFi Mode"); } int cmd_mqtt_server(const char *cmd, const char *args, int query, char *prev_cmd) diff --git a/src/fanpico.h b/src/fanpico.h index f89387f..fd1320e 100644 --- a/src/fanpico.h +++ b/src/fanpico.h @@ -427,6 +427,7 @@ struct tm *datetime_to_tm(const datetime_t *t, struct tm *tm); time_t datetime_to_time(const datetime_t *datetime); const char *mac_address_str(const uint8_t *mac); int valid_wifi_country(const char *country); +int valid_hostname(const char *name); int check_for_change(double oldval, double newval, double threshold); int64_t pow_i64(int64_t x, uint8_t y); double round_decimal(double val, unsigned int decimal); diff --git a/src/util.c b/src/util.c index 87df485..11f22bc 100644 --- a/src/util.c +++ b/src/util.c @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -241,6 +242,21 @@ int valid_wifi_country(const char *country) } +int valid_hostname(const char *name) +{ + if (!name) + return 0; + + for (int i = 0; i < strlen(name); i++) { + if (!(isalnum((int)name[i]) || name[i] == '-')) { + return 0; + } + } + + return 1; +} + + int check_for_change(double oldval, double newval, double threshold) { double delta = fabs(oldval - newval);