diff --git a/CLC-qthing/SiliconTorch/Service/Service.cpp b/CLC-qthing/SiliconTorch/Service/Service.cpp index 6da3240ee42c5a961d4616d7bdaa07258667f77c..ee68ad5b7c63ce13eec8e7d97560b82154e4f2e6 100644 --- a/CLC-qthing/SiliconTorch/Service/Service.cpp +++ b/CLC-qthing/SiliconTorch/Service/Service.cpp @@ -42,14 +42,37 @@ namespace SiliconTorch { attrsLocked = true; + + for (const auto& name : *wantedNames) { + Service* service = otherByName(name); + + if (service != NULL) wantedServices.insert(service); + } + + for (const auto& name : *requiredNames) { + Service* service = otherByName(name); + + if (service != NULL) { + requiredServices.insert(service); + } else { + hasErrors = true; // service is errorneous because of unsatisfied dependency + ESP_LOGW(TAG, "Service[ %s ] is errorneous because of unsatisfied dependency[ %s ]", getName().c_str(), name.c_str()); + } + } + + delete wantedNames; + delete requiredNames; + + wantedNames = NULL; + requiredNames = NULL; + + u8 state = 0; // disabled -> 0 enabled -> 1 state = SiliconTorch::NVSExplorer::NVSExplorer::instance().getUnsignedInt(getNameSpace(), NVSStateKey, state); if (state >= (u8)Invalid) state = (u8)Disabled; nvsState = (NVSState)state; - ESP_LOGW(TAG, "Service[ %s ] may not start due to errorneous initialization", getName().c_str()); - } bool Service::isEnabled() const { @@ -96,10 +119,7 @@ namespace SiliconTorch { void Service::addWantedService(const str& name) { if (!attrsLocked) { - Service* service = otherByName(name); - - if (service != NULL) - wantedServices.insert(service); + wantedNames->insert(name); } else { // TODO: report error somehow? } @@ -107,14 +127,7 @@ namespace SiliconTorch { void Service::addRequiredService(const str& name) { if (!attrsLocked) { - Service* service = otherByName(name); - - if (service != NULL) { - requiredServices.insert(service); - } else { - hasErrors = true; - ESP_LOGW(TAG, "Service[ %s ] requires unknown otherService[ %s ]", getName().c_str(), name.c_str()); - } + requiredNames->insert(name); } else { // TODO: report error somehow? } diff --git a/CLC-qthing/SiliconTorch/Service/Service.hpp b/CLC-qthing/SiliconTorch/Service/Service.hpp index aaf4f8bed03d91652fed24c543f973a9ad33e61f..f5bdd54109c8d8b378980227887c6f63f29e301b 100644 --- a/CLC-qthing/SiliconTorch/Service/Service.hpp +++ b/CLC-qthing/SiliconTorch/Service/Service.hpp @@ -84,6 +84,9 @@ namespace SiliconTorch { str name = ""; str nameSpace = ""; + std::set<std::string>* wantedNames = new std::set<std::string>(); + std::set<std::string>* requiredNames = new std::set<std::string>(); + ServiceSet wantedServices; ServiceSet requiredServices; diff --git a/CLC-qthing/SiliconTorch/Service/ServiceManager.cpp b/CLC-qthing/SiliconTorch/Service/ServiceManager.cpp index 8c7a92f5bcfc6526f2b0879ae13932f521d51a1b..7235a48e29096d423bcabe14a351c8accadf631a 100644 --- a/CLC-qthing/SiliconTorch/Service/ServiceManager.cpp +++ b/CLC-qthing/SiliconTorch/Service/ServiceManager.cpp @@ -39,6 +39,9 @@ namespace SiliconTorch { registrationLocked = true; + for (auto& [ignored, service] : serviceMap) + service->postInit(); + auto sers = startAlgo(); ESP_LOGI(TAG, "===== Services starting order ====="); @@ -71,7 +74,7 @@ namespace SiliconTorch { if (service->isEnabled()) { - bool reqsOK = true; // TODO: requirements_"zufriedengestellt" …? (naming) + bool reqsSatisfied = true; ServiceSet deps; ServiceSet wanted = service->getWantedServices(); @@ -84,9 +87,9 @@ namespace SiliconTorch { // copy required services for (const auto& sr : required) if (sr->isEnabled()) deps.insert(sr); - else reqsOK = false; + else reqsSatisfied = false; - if (reqsOK) + if (reqsSatisfied) services[service] = deps; } @@ -282,8 +285,9 @@ namespace SiliconTorch { return byName(name); }; + s->preInit(serviceLookup); s->init(); - s->postInit();//serviceLookup); + // only do init() here to avoid name-lookup cycles in postInit() ! serviceMap[s->getName()] = s; diff --git a/CLC-qthing/SiliconTorch/Service/TestServices.cpp b/CLC-qthing/SiliconTorch/Service/TestServices.cpp new file mode 100644 index 0000000000000000000000000000000000000000..74c18d8cf6a3b4b7f9690f213927b22f487ab355 --- /dev/null +++ b/CLC-qthing/SiliconTorch/Service/TestServices.cpp @@ -0,0 +1,118 @@ +#include "TestServices.hpp" + +// C++ system level +// #include <cstring> // memset, strncmp +// #include <cstdlib> // TODO: is this for memcpy? +// #include <functional> + +// ESP32 specific +#include "esp_log.h" + +// project specific +#include <Types.hpp> +#include "SiliconTorch/NVSExplorer.hpp" + +// qthing stuff +// #include <qthing> + + + +namespace SiliconTorch { + + namespace Service { + + + void Cycle0::init() { + + addRequiredService("Cycle1"); + + setName("Cycle0"); + setNameSpace("Cycle0"); + } + + void Cycle0::start() {ESP_LOGW(getName().c_str(), "Starting service[ %s ] *WHOOP* *WHOOP*", getName().c_str()); } + + + + void Cycle1::init() { + + addRequiredService("Cycle0"); + + setName("Cycle1"); + setNameSpace("Cycle1"); + } + + void Cycle1::start() {ESP_LOGW(getName().c_str(), "Starting service[ %s ] *WHOOP* *WHOOP*", getName().c_str()); } + + + + + + void AAA::init() { + setName("AAA"); + setNameSpace("AAA"); + } + + void AAA::start() {ESP_LOGW(getName().c_str(), "Starting service[ %s ] *WHOOP* *WHOOP*", getName().c_str()); } + + void BBB::init() { + + addRequiredService("AAA"); + + setName("BBB"); + setNameSpace("BBB"); + } + + void BBB::start() {ESP_LOGW(getName().c_str(), "Starting service[ %s ] *WHOOP* *WHOOP*", getName().c_str()); } + + void CCC::init() { + + addRequiredService("BBB"); + + setName("CCC"); + setNameSpace("CCC"); + } + + void CCC::start() {ESP_LOGW(getName().c_str(), "Starting service[ %s ] *WHOOP* *WHOOP*", getName().c_str()); } + + void WantsAAA::init() { + + addWantedService("AAA"); + + setName("WantsAAA"); + setNameSpace("WantsAAA"); + } + + void WantsAAA::start() {ESP_LOGW(getName().c_str(), "Starting service[ %s ] *WHOOP* *WHOOP*", getName().c_str()); } + + + void ABC::init() { + + addRequiredService("AAA"); + addRequiredService("BBB"); + addRequiredService("CCC"); + + setName("ABC"); + setNameSpace("ABC"); + } + + void ABC::start() {ESP_LOGW(getName().c_str(), "Starting service[ %s ] *WHOOP* *WHOOP*", getName().c_str()); } + + void ABCD::init() { + + addWantedService("AAA"); + addRequiredService("BBB"); + addRequiredService("CCC"); + addRequiredService("ABC"); + + setName("ABCD"); + setNameSpace("ABCD"); + } + + void ABCD::start() {ESP_LOGW(getName().c_str(), "Starting service[ %s ] *WHOOP* *WHOOP*", getName().c_str()); } + + + + + } +} diff --git a/CLC-qthing/SiliconTorch/Service/TestServices.hpp b/CLC-qthing/SiliconTorch/Service/TestServices.hpp new file mode 100644 index 0000000000000000000000000000000000000000..5322fd32abd06be492ee514673131f802fb90da0 --- /dev/null +++ b/CLC-qthing/SiliconTorch/Service/TestServices.hpp @@ -0,0 +1,74 @@ +#pragma once + +// C++ system level +// #include <cstring> // memset, strncmp +// #include <cstdlib> // TODO: is this for memcpy? +// #include <functional> + +// ESP32 specific +#include "esp_log.h" + +// project specific +#include <Types.hpp> +#include "Service.hpp" + +// qthing stuff +// #include <qthing> + + +namespace SiliconTorch { + + namespace Service { + + class Cycle0 : public ServiceManager::Service { + public: + void init(); + void start(); + }; + + class Cycle1 : public ServiceManager::Service { + public: + void init(); + void start(); + }; + + + class AAA : public ServiceManager::Service { + public: + void init(); + void start(); + }; + + class BBB : public ServiceManager::Service { + public: + void init(); + void start(); + }; + + class CCC : public ServiceManager::Service { + public: + void init(); + void start(); + }; + + class WantsAAA : public ServiceManager::Service { + public: + void init(); + void start(); + }; + + class ABC : public ServiceManager::Service { + public: + void init(); + void start(); + }; + + class ABCD : public ServiceManager::Service { + public: + void init(); + void start(); + }; + + + } +} diff --git a/CLC-qthing/SiliconTorch/Service/__services__.cpp b/CLC-qthing/SiliconTorch/Service/__services__.cpp index f11bdcf0a7a7f17d0746dc3a0fab403b5c9ee99d..c9110871e47694426893fb54e14a89c0b72b72a2 100644 --- a/CLC-qthing/SiliconTorch/Service/__services__.cpp +++ b/CLC-qthing/SiliconTorch/Service/__services__.cpp @@ -6,6 +6,8 @@ #include "CyanStripe.hpp" +#include "TestServices.hpp" + namespace SiliconTorch { namespace ServiceManager { @@ -17,6 +19,17 @@ namespace SiliconTorch { mgr->registerService(new SiliconTorch::Service::FxPublish()); mgr->registerService(new SiliconTorch::Service::CyanStripe()); + + mgr->registerService(new SiliconTorch::Service::ABC()); + mgr->registerService(new SiliconTorch::Service::ABCD()); + mgr->registerService(new SiliconTorch::Service::Cycle0()); + mgr->registerService(new SiliconTorch::Service::Cycle1()); + mgr->registerService(new SiliconTorch::Service::AAA()); + mgr->registerService(new SiliconTorch::Service::BBB()); + mgr->registerService(new SiliconTorch::Service::CCC()); + mgr->registerService(new SiliconTorch::Service::WantsAAA()); + + } diff --git a/CyanBusInjector-FW/device_main.cpp b/CyanBusInjector-FW/device_main.cpp index 04cb967b67f4790d554b0278eb634ecb612cdd30..65fce7cee5ddb44eb7c0af1a4d4bd59215f7d933 100644 --- a/CyanBusInjector-FW/device_main.cpp +++ b/CyanBusInjector-FW/device_main.cpp @@ -9,6 +9,7 @@ SiliconTorch::CyanBusInjector::CyanBusInjector* injector; + void device_main() { qthing::Config cfg; @@ -21,6 +22,17 @@ void device_main() { + + // #include <map> + // #include <string> + // std::map<int, std::string> x { + // { 42, "foo" }, + // { 3, "bar" } + // }; + + + + // injector = new SiliconTorch::CyanBusInjector::CyanBusInjector(23, 34, 32, 33); injector = new SiliconTorch::CyanBusInjector::CyanBusInjector_ProtoPCB();