Skip to content

Commit

Permalink
Add MQTT command for reading settings
Browse files Browse the repository at this point in the history
  • Loading branch information
hg committed Nov 18, 2020
1 parent ec48f21 commit a34ea66
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
14 changes: 11 additions & 3 deletions firmware/main/commands.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@ static bool handlePing(mqtt::Client &client, const mqtt::Message &msg) {
esp_restart();
}

static bool handleReadSettings(mqtt::Client &client, const mqtt::Message &msg) {
const std::string json = appSettings.format();
client.send(msg.respTopic, json.c_str());
return true;
}

static bool handleWriteSetting(mqtt::Client &client, const mqtt::Message &msg,
const std::vector<std::string> &args) {
if (args.size() != 3) {
client.send(msg.respTopic, "usage: setting/set name_spaces value_also");
client.send(msg.respTopic, "usage: setting/set name_no_spaces value_also");
return false;
}
const esp_err_t err = appSettings.write(args[1].c_str(), args[2].c_str());
Expand Down Expand Up @@ -99,6 +105,9 @@ static bool handleMessage(mqtt::Client &client, const mqtt::Message &msg) {
if (command == "setting/set") {
return handleWriteSetting(client, msg, tokens);
}
if (command == "setting/get") {
return handleReadSettings(client, msg);
}
return handleUnknown(client, msg);
}

Expand All @@ -114,6 +123,5 @@ static bool handleMessage(mqtt::Client &client, const mqtt::Message &msg) {
}

void initCommandHandler(mqtt::Client &client) {
xTaskCreate(taskCommandHandler, "mqtt_cmd_handler", KiB(2), &client, 4,
nullptr);
xTaskCreate(taskCommandHandler, "mqtt_cmd", KiB(4), &client, 4, nullptr);
}
16 changes: 16 additions & 0 deletions firmware/main/settings.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "settings.hh"
#include "common.hh"
#include <cstring>

AppSettings appSettings;

Expand Down Expand Up @@ -67,3 +68,18 @@ esp_err_t AppSettings::write(const char *const name, const char *const value) {
}
return err;
}

std::string AppSettings::format() const {
constexpr auto tpl =
R"({"dev":"%s","wifi":{"ssid":"%s","pass":"%s"},"mqtt":{"broker":"%s","user":"%s","pass":"%s"}})";
// this is pretty slow, but we rarely call this command, and it will prevent
// using more heap than necessary
const size_t size = strlen(tpl) + strlen(devName) + strlen(wifi.ssid) +
strlen(wifi.pass) + strlen(mqtt.broker) +
strlen(mqtt.username) + strlen(mqtt.password);
std::string json;
json.reserve(size);
snprintf(&json[0], json.capacity(), tpl, devName, wifi.ssid, wifi.pass,
mqtt.broker, mqtt.username, mqtt.password);
return json;
}
1 change: 1 addition & 0 deletions firmware/main/settings.hh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ struct AppSettings {

esp_err_t write(const char *name, const char *value);

std::string format() const;
private:
static esp_err_t readString(nvs::NVSHandle &nvs, const char *name,
const char *&dst);
Expand Down

0 comments on commit a34ea66

Please sign in to comment.