Skip to content
Snippets Groups Projects
Commit 444dd63f authored by fxk8y's avatar fxk8y :spider:
Browse files

Begin building NVSExplorer's MQTT interface

parent 0205e0df
No related branches found
No related tags found
No related merge requests found
......@@ -2,6 +2,7 @@
// C++ system level
#include <set>
#include <vector>
#include <cstring> // memset, memcpy, strncmp
// #include <cstdlib>
#include <cinttypes>
......@@ -14,11 +15,11 @@
// #include "driver/uart.h"
// project specific
// #include ""
#include <SpiderLib/Util.hpp>
// qthing stuff
// #include <qthing>
#include <qthing>
#include <qthing/mqtt_common.hpp>
......@@ -36,6 +37,45 @@ namespace SiliconTorch {
ESP_LOGI(TAG, "Starting NVSExplorer[📡]");
nvs_flash_init();
using MessageHandler = std::function<void(std::vector<std::string>&, qthing::multipart_message_t)>;
uint16_t deviceTopicLEN = deviceTopic("").length();
auto wrapMessageHandler = [deviceTopicLEN](MessageHandler handler) {
// TODO: capture handler byVal or byRef ?
return [&](qthing::multipart_message_t msg) {
if (msg.length == msg.total_length && msg.offset == 0) {
std::string _topic(msg.topic.c_str() + deviceTopicLEN);
ESP_LOGW(TAG, "wrapMessageHandler() -> _topic[ %s ]", _topic.c_str());
std::vector<std::string> topicParts = SpiderLib::Util::split(_topic, '/');
handler(topicParts, msg);
} else {
ESP_LOGW(TAG, "Multipart messages are not supported atm. topic[ %s ]", msg.topic.c_str());
}
};
};
MessageHandler sharp = [](std::vector<std::string>& topic, qthing::multipart_message_t msg) {
ESP_LOGE(TAG, "sharpyyyyy handler called on topic[ %s ] HÖ HÖ HÖ HÖ HÖÖÖöööö", msg.topic.c_str());
};
qthing::add_binary_message_callback(deviceTopic("#"), wrapMessageHandler(sharp));
/* qthing::add_binary_message_callback("+/+/test", [&](qthing::multipart_message_t msg) {
char buf[128];
memset(buf, 0x00, 128);
memcpy(buf, msg.payload, std::min(msg.length, (uint32_t)127));
ESP_LOGW("+/+/test", "topic[ %s ] message[ %s ]", msg.topic.c_str(), buf);
}); */
}
......@@ -99,6 +139,10 @@ namespace SiliconTorch {
}
std::string NVSExplorer::deviceTopic(const std::string& suffix) {
return std::string(DEVICE_NAMESPACE + "NVS/") + suffix;
}
std::string toString(std::set<std::string>& set, std::string delimiter) {
......@@ -135,6 +179,5 @@ namespace SiliconTorch {
return std::string(str);
}
}
}
......@@ -38,6 +38,7 @@ namespace SiliconTorch {
std::set<std::string> listNamespaces();
std::set<std::string> listKeys(std::string& _namespace);
std::string deviceTopic(const std::string& suffix);
// ⬇⬇ Singleton stuff ⬇⬇
......
......@@ -34,6 +34,8 @@
#include <algorithm>
SiliconTorch::CyanBus::CyanBus* cyanBus = NULL;
......@@ -60,20 +62,35 @@ void device_main() {
qthing::add_binary_message_callback("+/+/test", [&](qthing::multipart_message_t msg) {
// if (msg.length != msg.total_length) {
// ESP_LOGD("+/+/test", "multipart messages not supported atm");
// return;
// }
char buf[128];
memset(buf, 0x00, 128);
memcpy(buf, msg.payload, msg.length);
memcpy(buf, msg.payload, std::min(msg.length, (uint32_t)127));
ESP_LOGW("+/+/test", "topic[ %s ] message[ %s ]", msg.topic.c_str(), buf);
});
qthing::add_binary_message_callback("+/+/+", [](qthing::multipart_message_t msg) {
char buf[128];
memset(buf, 0x00, 128);
memcpy(buf, msg.payload, std::min(msg.length, (uint32_t)127));
ESP_LOGE("+/+/test", "HDL_0 topic[ %s ] message[ %s ]", msg.topic.c_str(), buf);
ESP_LOGW("+/+/+", "topic[ %s ] message[ %s ]", msg.topic.c_str(), buf);
});
qthing::add_binary_message_callback("test/+/+", [](qthing::multipart_message_t msg) {
char buf[128];
memset(buf, 0x00, 128);
memcpy(buf, msg.payload, std::min(msg.length, (uint32_t)127));
ESP_LOGW("test/+/+", "topic[ %s ] message[ %s ]", msg.topic.c_str(), buf);
});
/* qthing::add_binary_message_callback("#", [&](qthing::multipart_message_t msg) {
char buf[128];
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment