Support config introspection

This commit is contained in:
Sassa NF
2024-12-22 22:00:53 +00:00
parent edf66ec9ee
commit ae7bef7b3e
5 changed files with 182 additions and 50 deletions
+61
View File
@@ -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;
}
}
+10 -1
View File
@@ -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;
+93 -49
View File
@@ -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();
+2
View File
@@ -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);
};