diff --git a/lib/comms/comms.cpp b/lib/comms/comms.cpp index 28b157b..0882096 100644 --- a/lib/comms/comms.cpp +++ b/lib/comms/comms.cpp @@ -241,12 +241,39 @@ bool ReadlineComms::send(Message &m) case MessageType::SCAN_RESULT: p = _scan_result_str(m.payload.dump); break; + case MessageType::CONFIG_TASK: + p = m.payload.config.is_set ? "SET " : "GET "; + p += *m.payload.config.key; + if (m.payload.config.is_set) + { + p += " " + *m.payload.config.value; + } + break; } serial.print(_wrap_str(p)); return true; } +String _stringParam(String &p, String default_v) +{ + p.trim(); + int i = p.indexOf(' '); + if (i < 0) + { + i = p.length(); + } + + String v = p.substring(0, i); + p = p.substring(i + 1); + + if (i == 0) + { + v = default_v; + } + return v; +} + int64_t _intParam(String &p, int64_t default_v) { p.trim(); @@ -313,6 +340,28 @@ Message *_parsePacket(String p) m->payload.scan.delay = _intParam(p, -1); return m; } + + if (cmd.equalsIgnoreCase("get")) + { + Message *m = new Message(); + m->type = MessageType::CONFIG_TASK; + m->payload.config.is_set = false; + m->payload.config.key = new String(_stringParam(p, "")); + m->payload.config.value = NULL; + return m; + } + + if (cmd.equalsIgnoreCase("set")) + { + Message *m = new Message(); + m->type = MessageType::CONFIG_TASK; + m->payload.config.is_set = true; + m->payload.config.key = new String(_stringParam(p, "")); + m->payload.config.value = new String(_stringParam(p, "")); + + return m; + } + return NULL; } @@ -353,4 +402,16 @@ Message::~Message() return; } + + if (type == CONFIG_TASK) + { + delete payload.config.key; + + if (payload.config.is_set) + { + delete payload.config.value; + } + + return; + } } diff --git a/lib/comms/comms.h b/lib/comms/comms.h index 34933e8..64663ad 100644 --- a/lib/comms/comms.h +++ b/lib/comms/comms.h @@ -15,7 +15,8 @@ enum MessageType WRAP = 0, SCAN, SCAN_RESULT, - _MAX_MESSAGE_TYPE = SCAN_RESULT + CONFIG_TASK, + _MAX_MESSAGE_TYPE = CONFIG_TASK }; struct Wrapper @@ -37,12 +38,20 @@ struct ScanTaskResult int16_t *rssis; }; +struct ConfigTask +{ + String *key; + String *value; + bool is_set; +}; + struct Message { MessageType type; union { Wrapper wrap; + ConfigTask config; ScanTask scan; ScanTaskResult dump; } payload; diff --git a/lib/config/config.cpp b/lib/config/config.cpp index 1221072..ab766fe 100644 --- a/lib/config/config.cpp +++ b/lib/config/config.cpp @@ -83,50 +83,8 @@ Config Config::init() } // do something with known keys and values - - if (r.key.equalsIgnoreCase("print_profile_time")) + if (c.updateConfig(r.key, r.value)) { - String v = r.value; - bool p = v.equalsIgnoreCase("true"); - if (!p && !v.equalsIgnoreCase("false")) - { - Serial.printf("Expected bool for '%s', found '%s' - ignoring\n", - r.key.c_str(), r.value.c_str()); - } - else - { - c.print_profile_time = p; - } - continue; - } - - if (r.key.equalsIgnoreCase("log_data_json_interval")) - { - c.log_data_json_interval = r.value.toInt(); - continue; - } - - if (r.key.equalsIgnoreCase("listen_on_serial0")) - { - c.listen_on_serial0 = r.value; - continue; - } - - if (r.key.equalsIgnoreCase("listen_on_serial1")) - { - c.listen_on_serial1 = r.value; - continue; - } - - if (r.key.equalsIgnoreCase("listen_on_usb")) - { - c.listen_on_serial0 = r.value; - continue; - } - - if (r.key.equalsIgnoreCase("detection_strategy")) - { - c.configureDetectionStrategy(r.value); continue; } @@ -137,6 +95,57 @@ Config Config::init() return c; } +bool Config::updateConfig(String key, String value) +{ + if (key.equalsIgnoreCase("print_profile_time")) + { + String v = value; + bool p = v.equalsIgnoreCase("true"); + if (!p && !v.equalsIgnoreCase("false")) + { + Serial.printf("Expected bool for '%s', found '%s' - ignoring\n", key.c_str(), + value.c_str()); + } + else + { + print_profile_time = p; + } + return true; + } + + if (key.equalsIgnoreCase("log_data_json_interval")) + { + log_data_json_interval = value.toInt(); + return true; + } + + if (key.equalsIgnoreCase("listen_on_serial0")) + { + listen_on_serial0 = value; + return true; + } + + if (key.equalsIgnoreCase("listen_on_serial1")) + { + listen_on_serial1 = value; + return true; + } + + if (key.equalsIgnoreCase("listen_on_usb")) + { + listen_on_serial0 = value; + return true; + } + + if (key.equalsIgnoreCase("detection_strategy")) + { + configureDetectionStrategy(value); + return true; + } + + return false; +} + String detectionStrategyToStr(Config &c) { String res = c.detection_strategy; @@ -312,18 +321,53 @@ bool Config::write_config(const char *path) return false; } - f.println("print_profile_time = " + String(print_profile_time ? "true" : "false")); - f.println("log_data_json_interval = " + String(log_data_json_interval)); - f.println("listen_on_serial0 = " + listen_on_serial0); - f.println("listen_on_serial1 = " + listen_on_serial1); - f.println("listen_on_usb = " + listen_on_usb); + f.println("print_profile_time = " + getConfig("print_profile_time")); + f.println("log_data_json_interval = " + getConfig("log_data_json_interval")); + f.println("listen_on_serial0 = " + getConfig("listen_on_serial0")); + f.println("listen_on_serial1 = " + getConfig("listen_on_serial1")); + f.println("listen_on_usb = " + getConfig("listen_on_usb")); - f.println("detection_strategy = " + detectionStrategyToStr(*this)); + f.println("detection_strategy = " + getConfig("detection_strategy")); f.close(); return true; } +String Config::getConfig(String key) +{ + if (key.equalsIgnoreCase("print_profile_time")) + { + return String(print_profile_time ? "true" : "false"); + } + + if (key.equalsIgnoreCase("log_data_json_interval")) + { + return String(log_data_json_interval); + } + + if (key.equalsIgnoreCase("listen_on_serial0")) + { + return listen_on_serial0; + } + + if (key.equalsIgnoreCase("listen_on_serial1")) + { + return listen_on_serial1; + } + + if (key.equalsIgnoreCase("listen_on_usb")) + { + return listen_on_usb; + } + + if (key.equalsIgnoreCase("detection_strategy")) + { + return detectionStrategyToStr(*this); + } + + return ""; +} + ParseResult parse_config_line(String ln) { ln.trim(); diff --git a/lib/config/config.h b/lib/config/config.h index 859c428..59bc2e4 100644 --- a/lib/config/config.h +++ b/lib/config/config.h @@ -32,6 +32,8 @@ struct Config bool write_config(const char *path); static Config init(); + bool updateConfig(String key, String value); + String getConfig(String key); void configureDetectionStrategy(String cfg); }; diff --git a/src/main.cpp b/src/main.cpp index ab85959..25dc541 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1387,6 +1387,22 @@ void checkComms() Comms0->send(*m); // forward to peer Comms1->send(*m); // forward to peer break; + case MessageType::CONFIG_TASK: + if (m->payload.config.is_set) + { + String v = config.getConfig(*m->payload.config.key); + bool r = + config.updateConfig(*m->payload.config.key, *m->payload.config.value); + Serial.printf("SET config (%s): %s = %s (was: %s)\n", r ? "OK" : "failed", + m->payload.config.key->c_str(), + m->payload.config.value->c_str(), v.c_str()); + } + else + { + Serial.printf("GET config: %s = %s\n", m->payload.config.key->c_str(), + config.getConfig(*m->payload.config.key).c_str()); + } + break; } delete m; }