Logo Pico-Framework A web-first embedded framework for C++
Loading...
Searching...
No Matches
FrameworkManager Class Reference

Starts and coordinates core system services like networking and time sync. More...

#include <FrameworkManager.h>

+ Inheritance diagram for FrameworkManager:
+ Collaboration diagram for FrameworkManager:

Public Member Functions

 FrameworkManager (FrameworkApp *app, Router &router)
 Constructor.
 
- Public Member Functions inherited from FrameworkController
 FrameworkController (const char *name, Router &sharedRouter, uint16_t stackSize=1024, UBaseType_t priority=tskIDLE_PRIORITY+1)
 Constructor.
 
void run () override final
 Main task loop.
 
const char * getName () const
 Get the name of this controller.
 
void enableEventQueue (size_t depth=8)
 Enable the event queue for this controller.
 
QueueHandle_t getEventQueue () const
 Get the event queue for this controller.
 
bool getNextEvent (Event &event, uint32_t timeoutMs)
 Check if there are any pending events in the queue.
 
- Public Member Functions inherited from FrameworkTask
 FrameworkTask (const char *name, uint16_t stackSize=1024, UBaseType_t priority=1)
 Constructor.
 
virtual ~FrameworkTask ()
 Destructor.
 
bool start ()
 Starts the task via FreeRTOS.
 
void suspend ()
 Suspends the task using vTaskSuspend().
 
void resume ()
 Resumes the task using vTaskResume().
 
TaskHandle_t getHandle () const
 Returns the FreeRTOS task handle.
 
const char * getName () const
 Returns the task name.
 
void notify (uint8_t index, uint32_t value=1)
 Sends a notification to this task using an index.
 
void notify (Notification n, uint32_t value=1)
 Sends a notification using a framework-defined enum.
 
void notifyFromISR (uint8_t index, uint32_t value=1, BaseType_t *pxHigherPriorityTaskWoken=nullptr)
 Sends a notification from an ISR (by index).
 
void notifyFromISR (Notification n, uint32_t value=1, BaseType_t *pxHigherPriorityTaskWoken=nullptr)
 Sends a notification from ISR using enum identifier.
 
bool waitFor (uint8_t index, TickType_t timeout=portMAX_DELAY)
 Waits for a notification (by index).
 
bool waitFor (Notification n, TickType_t timeout=portMAX_DELAY)
 Waits for a notification (by enum identifier).
 
Notification waitForAny (uint8_t index, uint32_t mask, TickType_t timeout=portMAX_DELAY)
 waits for any notification matching the given mask.
 

Private Member Functions

void setupTraceFromConfig ()
 Sets up debug tracing from configuration.
 
void onStart ()
 Initializes the network and application tasks.
 
void warmUp ()
 
void poll ()
 Polling function for the FrameworkManager.
 
void onEvent (const Event &event)
 Handles events posted to the FrameworkManager.
 

Static Private Member Functions

static void app_task (void *params)
 Placeholder for an application-level task, if used.
 

Private Attributes

FrameworkAppapp
 Pointer to the application task.
 
TaskHandle_t networkTaskHandle
 Handle for the network task.
 
TaskHandle_t applicationTaskHandle
 (Unused) optional handle for app task
 
Network network
 Network management.
 

Additional Inherited Members

- Protected Member Functions inherited from FrameworkController
virtual void initRoutes ()
 Initialize routes for this controller.
 
virtual TickType_t getPollIntervalTicks ()
 Returns the polling interval in ticks used in run().
 
void runEvery (uint32_t intervalMs, const std::function< void()> &fn, const char *id)
 Run a function periodically with millisecond resolution.
 
- Protected Member Functions inherited from FrameworkTask
uint32_t waitFor (TickType_t timeout=portMAX_DELAY)
 Wait for any notification (default index).
 
bool createQueue (size_t itemSize, size_t length)
 Creates an internal FreeRTOS queue.
 
bool sendToQueue (const void *item, TickType_t timeout=0)
 Sends an item to the internal queue.
 
bool receiveFromQueue (void *item, TickType_t timeout=portMAX_DELAY)
 Receives an item from the internal queue.
 
- Protected Attributes inherited from FrameworkController
Routerrouter
 Handles path-to-handler mapping - reference to shared Router instance.
 
- Protected Attributes inherited from FrameworkTask
const char * _name
 
uint16_t _stackSize
 
UBaseType_t _priority
 
TaskHandle_t _handle = nullptr
 
QueueHandle_t _queue = nullptr
 

Detailed Description

Responsible for launching the network task, waiting for connection, and notifying the application via FrameworkNotification::NetworkReady.

Definition at line 38 of file FrameworkManager.h.

Constructor & Destructor Documentation

◆ FrameworkManager()

FrameworkManager::FrameworkManager ( FrameworkApp app,
Router router 
)

Constructor.

Parameters
appPointer to the application, which will be notified when the network is ready.
Parameters
appPointer to the application, which will be notified when the network is ready.

Definition at line 45 of file FrameworkManager.cpp.

46 : FrameworkController("FrameworkManager", router, 1024, 2),
47 app(app),
48 networkTaskHandle(nullptr)
49{
50}
Base class for event-driven control logic in embedded applications.
Router & router
Handles path-to-handler mapping - reference to shared Router instance.
TaskHandle_t networkTaskHandle
Handle for the network task.
FrameworkApp * app
Pointer to the application task.

Member Function Documentation

◆ app_task()

void FrameworkManager::app_task ( void *  params)
staticprivate

Placeholder for an application-level task, if used.

Definition at line 127 of file FrameworkManager.cpp.

128{
129 // Not implemented (placeholder)
130 vTaskDelete(nullptr);
131}

◆ onEvent()

void FrameworkManager::onEvent ( const Event event)
privatevirtual

This function is called by the EventManager when an event is posted to the FrameworkManager's event queue.

Reimplemented from FrameworkController.

Definition at line 133 of file FrameworkManager.cpp.

134{
137 {
138 printf("[FrameworkManager] HttpServer started, notifying TimeManager...\n");
139 AppContext::get<TimeManager>()->onHttpServerStarted();
140 }
141}
static constexpr std::uintptr_t getTypeKey()
Definition AppContext.h:91
Notification notification
Notification identifier (system or user)
Definition Event.h:25
SystemNotification system
NotificationKind kind

References AppContext::getTypeKey(), HttpServerStarted, Notification::kind, Event::notification, System, and Notification::system.

+ Here is the call graph for this function:

◆ onStart()

void FrameworkManager::onStart ( )
privatevirtual

Initializes the network and application tasks.

This function is called to set up the network and application tasks with the appropriate stack sizes and priorities.

This function is called to set up the network and application tasks with the appropriate stack sizes and priorities.

Reimplemented from FrameworkController.

Definition at line 53 of file FrameworkManager.cpp.

54{
56 std::cout << "[Framework Manager] Initializing framework..." << std::endl;
57
58 // It is important to ensure that the AppContext is initialized
59 // before we start using it in the Framework.
60 // For example EventManager and other application service must be available for the user in onStart();
62
64 configASSERT(timeMgr); // Will hard fault early if registration failed
65 timeMgr->start(); // If AON timer is running it will post a TimerValid event
66
67 // TimeManager will handle the time sync and timezone detection
69
71 {
72 printf("[Framework Manager] Failed to initialize network stack.\n");
73 }
74
75 warmUp(); // Warm up the JSON parser and other components
76
77 // needs to be started after scheduler running to ensure full use of FreeRTOS
78 app->start(); // Starts the app task
79
81 {
82#if WIFI_REBOOT_ON_FAILURE
83 printf("[Framework Manager] WiFi failed — rebooting...\n");
84 NVIC_SystemReset();
85#else
86 printf("[Framework Manager] WiFi failed after retries. Continuing without network.\n");
87 return;
88#endif
89 }
90 printf("[Framework Manager] Framework services initialized.\n");
91
92 timeMgr->onNetworkReady(); // tell timemanager so it can do what it needs to do
93
94 printf("[Framework Manager] Network up. Notifying app task...\n");
95
96 Event event;
98 AppContext::get<EventManager>()->postEvent(event);
99}
#define configASSERT(x)
constexpr uint32_t eventMask(Enum e)
Helper function to create an event mask from an enum value.
static AppContext & getInstance()
void initFrameworkServices()
virtual void start()
Initializes the application and its framework services.
void setupTraceFromConfig()
Sets up debug tracing from configuration.
Network network
Network management.
static bool initialize()
Start Wi-Fi with resilience, retrying connection if it fails.
Definition Network.cpp:41
static bool startWifiWithResilience()
Start Wi-Fi with resilience, retrying connection if it fails.
Definition Network.cpp:63
void onNetworkReady()
hanles network ready event.
void start()
Start the time manager.
Represents a framework event, optionally carrying payload data.
Definition Event.h:24

References app, configASSERT, eventMask(), AppContext::getInstance(), AppContext::getTypeKey(), HttpServerStarted, AppContext::initFrameworkServices(), Network::initialize(), network, NetworkReady, Event::notification, TimeManager::onNetworkReady(), setupTraceFromConfig(), FrameworkApp::start(), TimeManager::start(), Network::startWifiWithResilience(), and warmUp().

+ Here is the call graph for this function:

◆ poll()

void FrameworkManager::poll ( )
privatevirtual

This function checks the Wi-Fi connection status at regular intervals and attempts to reconnect if the connection is lost.

Reimplemented from FrameworkController.

Definition at line 148 of file FrameworkManager.cpp.

149{
150#if WIFI_MONITOR_INTERVAL_MS > 0
151 static uint32_t lastCheck = 0;
152 static int networkFailures = 0; // Track consecutive failures
153 uint32_t now = xTaskGetTickCount() * portTICK_PERIOD_MS;
154
155 if (now - lastCheck >= WIFI_MONITOR_INTERVAL_MS)
156 {
157 lastCheck = now;
158 printf("[FrameworkManager] Polling for Wi-Fi status...\n");
159
161 {
162 printf("[FrameworkManager] Reconnect failed. Restarting Wi-Fi...\n");
163
165 {
166 networkFailures++;
167
169 AppContext::get<EventManager>()->postEvent(event);
170
171 if (WIFI_REBOOT_ON_FAILURE && networkFailures >= 3)
172 {
173 printf("[FrameworkManager] Rebooting after 3 failed recovery attempts.\n");
174 rebootSystem();
175 }
176 }
177 else
178 {
179 networkFailures = 0; // success resets the counter
181 AppContext::get<EventManager>()->postEvent(event);
182 }
183 }
184 else
185 {
186 networkFailures = 0; // normal path
187 }
188 }
189#endif
190
191 // ... other poll logic ...
192}
static bool checkAndReconnect()
Attempt to connect to Wi-Fi with retries.
Definition Network.cpp:84
static bool restart_wifi()
Restart the Wi-Fi interface.
Definition Network.cpp:149
#define WIFI_MONITOR_INTERVAL_MS
This setting defines the interval for checking WiFi connection status The default is 30000 ms (30 sec...
#define WIFI_REBOOT_ON_FAILURE
This setting defines whether to reboot the device on WiFi connection failure The default is false,...
void rebootSystem()
Reboot the device immediately.
Definition utility.cpp:237

References Network::checkAndReconnect(), AppContext::getTypeKey(), NetworkDown, NetworkReady, rebootSystem(), Network::restart_wifi(), WIFI_MONITOR_INTERVAL_MS, and WIFI_REBOOT_ON_FAILURE.

+ Here is the call graph for this function:

◆ setupTraceFromConfig()

void FrameworkManager::setupTraceFromConfig ( )
private

Sets up debug tracing from configuration.

Uses framework_config to set up tracing options, including file output if enabled. This function should be called after the framework is initialized and before any trace calls are made.

Uses framework_config to set up tracing options, including file output if enabled. This function should be called after the framework is initialized and before any trace calls are made.

Definition at line 195 of file FrameworkManager.cpp.

196{
197#if TRACE_USE_SD
199#else
200 setTraceOutputToFile(nullptr, "");
201#endif
202
203#if TRACE_SYSTEM
204 TRACE_INIT(SYSTEM);
205 TRACE("Tracing initialized from framework.");
206#endif
207}
#define TRACE_INIT(MODULE_NAME)
Declare trace usage in a source file for a given module.
Definition DebugTrace.h:170
#define TRACE(...)
Default trace (INFO level).
Definition DebugTrace.h:187
void setTraceOutputToFile(StorageManager *sm, const std::string &path)
Set trace output to SD file using FatFsStorageManager.
Definition DebugTrace.h:51
#define TRACE_LOG_PATH

References AppContext::getTypeKey(), setTraceOutputToFile(), TRACE, TRACE_INIT, and TRACE_LOG_PATH.

Referenced by onStart().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ warmUp()

void FrameworkManager::warmUp ( )
private

Definition at line 102 of file FrameworkManager.cpp.

103{
104 // Warm up JSON parser
105 {
106 nlohmann::json j = nlohmann::json::parse("{\"warmup\":true}", nullptr, false);
107 (void)j.dump(); // Force stringify
108 }
109
110 // Warm up HttpRequest
111 {
112 HttpRequest dummy;
113 dummy.setMethod("GET");
114 dummy.setPath("/warmup");
115 dummy.setHeader("X-Warmup", "true");
116 (void)dummy.getHeader("X-Warmup");
117 }
118
119 // Force common string ops
120 std::string("warmup");
121
122 // Force a task yield
123 vTaskDelay(pdMS_TO_TICKS(1));
124}
Forward declaration for potential routing needs.
Definition HttpRequest.h:32
HttpRequest & setMethod(const std::string &method)
Set the HTTP method (e.g., GET, POST).
HttpRequest & setPath(const std::string &path)
Set the request path.
std::string getHeader(const std::string &field) const
Get a specific header field (case-insensitive).
HttpRequest & setHeader(const std::string &key, const std::string &value)

References HttpRequest::getHeader(), HttpRequest::setHeader(), HttpRequest::setMethod(), and HttpRequest::setPath().

Referenced by onStart().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Member Data Documentation

◆ app

FrameworkApp* FrameworkManager::app
private

Definition at line 50 of file FrameworkManager.h.

Referenced by onStart().

◆ applicationTaskHandle

TaskHandle_t FrameworkManager::applicationTaskHandle
private

Definition at line 52 of file FrameworkManager.h.

◆ network

Network FrameworkManager::network
private

Definition at line 54 of file FrameworkManager.h.

Referenced by onStart().

◆ networkTaskHandle

TaskHandle_t FrameworkManager::networkTaskHandle
private

Definition at line 51 of file FrameworkManager.h.


The documentation for this class was generated from the following files: