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

ST: Add service listing

parent 0893f314
No related branches found
No related tags found
No related merge requests found
......@@ -69,10 +69,6 @@ namespace SiliconTorch {
}
}
// u16 Service::startOrder() {
// return 1337; // actually a sane default 😏
// }
void Service::setName(const str& name) {
if (!attrsLocked) {
......@@ -92,7 +88,7 @@ namespace SiliconTorch {
void Service::setStartOrder(u16 startOrder) {
if (!attrsLocked) {
this->startOrder = startOrder;
this->startOrder = startOrder;
} else {
// TODO: report error somehow?
}
......
......@@ -38,6 +38,8 @@ namespace SiliconTorch {
// internal stuff, only to be called by ServiceManager!
void postInit();
// To be implemented by user!
virtual void start() = 0;
virtual void stop() = 0;
......
......@@ -9,6 +9,8 @@
// ESP32 specific
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
// project specific
#include <Types.hpp>
......@@ -35,24 +37,108 @@ namespace SiliconTorch {
registrationLocked = true;
// TODO: start services!
printServiceTable();
}
void ServiceManager::startEnabledServices() {
std::vector<Service*> ServiceManager::getServicesInStartOrder() {
std::vector<Service*> services;
for (const auto& [key, value] : serviceMap)
services.push_back(value);
for (const auto& [_, service] : serviceMap)
services.push_back(service);
std::sort(services.begin(), services.end(), [](const Service* x, const Service* y) {
return x->getStartOrder() < y->getStartOrder();
});
return services;
}
void ServiceManager::startEnabledServices() {
std::vector<Service*> services = getServicesInStartOrder();
for (const auto& service : services)
if (service->isEnabled()) service->start();
}
void ServiceManager::printServiceTable() {
// burn a few cycles because Espressivs logging is mehr :/
// while (xTaskGetTickCount() * portTICK_PERIOD_MS < 1000) {
// vTaskDelay(42 / portTICK_PERIOD_MS);
// }
vTaskDelay(100 / portTICK_PERIOD_MS);
// helper function, never used outside of this
// TODO: should we move it to utils…?
auto strmul = [](const str& target, u32 count) {
str out = "";
for (u32 n = 0; n < count; n++)
out += target;
return out;
};
u8 nameColSize = 4; // size of "name"
for (const auto& [name, _] : serviceMap)
if (name.length() > nameColSize) nameColSize = name.length();
nameColSize += 2; // padding
u8 startOrderColSize = 10 + 2; // size of "StartOrder" + padding
u8 enabledColSize = 8 + 2; // size of "Enabled " + padding (must be even)
ESP_LOGI(TAG, "┌%s┬%s┬%s┐",
strmul("─", nameColSize).c_str(),
strmul("─", startOrderColSize).c_str(),
strmul("─", enabledColSize).c_str()
);
ESP_LOGI(TAG, "│ Name %s│ StartOrder %s│ Enabled %s│",
strmul(" ", nameColSize - 4 - 2).c_str(),
strmul(" ", startOrderColSize - 10 - 2).c_str(),
strmul(" ", enabledColSize - 8 - 2).c_str()
);
ESP_LOGI(TAG, "╞%s╪%s╪%s╡",
strmul("═", nameColSize).c_str(),
strmul("═", startOrderColSize).c_str(),
strmul("═", enabledColSize).c_str()
);
std::vector<Service*> services = getServicesInStartOrder();
for (const auto& service : services) {
u8 nameSize = service->getName().length();
ESP_LOGI(TAG, "│ %s %s│ % 5d %s│%s%s%s│",
service->getName().c_str(),
strmul(" ", nameColSize - 2 - nameSize).c_str(),
service->getStartOrder(),
strmul(" ", startOrderColSize - 2 - 5).c_str(),
strmul(" ", (enabledColSize - 2) / 2).c_str(),
service->isEnabled() ? "✅" : "❌",
strmul(" ", (enabledColSize - 2) / 2).c_str()
);
}
ESP_LOGI(TAG, "└%s┴%s┴%s┘",
strmul("─", nameColSize).c_str(),
strmul("─", startOrderColSize).c_str(),
strmul("─", enabledColSize).c_str()
);
}
void ServiceManager::setServiceEnabled(const str& name, bool enabled) {
if (serviceMap.count(name) > 0) {
serviceMap.at(name)->setEnabled(enabled);
......
......@@ -2,6 +2,7 @@
// C++ system level
#include <map>
#include <vector>
// #include <cstring> // memset, strncmp
// #include <cstdlib> // TODO: is this for memcpy?
// #include <functional>
......@@ -34,10 +35,15 @@ namespace SiliconTorch {
void startEnabledServices();
void printServiceTable();
void setServiceEnabled(const str& name, bool enabled);
bool getServiceEnabled(const str& name);
std::vector<Service*> getServicesInStartOrder();
void registerService(Service* s);
private:
......
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