Skip to content

Commit

Permalink
Cleanup command implementations.
Browse files Browse the repository at this point in the history
  • Loading branch information
tjko committed Feb 28, 2024
1 parent 95cb46d commit baae5ca
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 43 deletions.
78 changes: 35 additions & 43 deletions src/command.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions src/fanpico.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
16 changes: 16 additions & 0 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <wctype.h>
#include <assert.h>
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit baae5ca

Please sign in to comment.