Logo Pico-Framework A web-first embedded framework for C++
Loading...
Searching...
No Matches
FrameworkManager.cpp
Go to the documentation of this file.
1
17#include <pico/async_context.h>
18#include <pico/async_context_freertos.h>
19#if PICO_RP2350
20#include "RP2350.h"
21#else
22#include "RP2040.h"
23#endif
24#include "framework_config.h" // Must be included before DebugTrace.h to ensure framework_config.h is processed first
25#include "DebugTrace.h" // For trace logging
26TRACE_INIT(FrameworkManager); // Initialize tracing for this module
28
29#include <iostream>
32#include "framework_config.h"
34#include "time/TimeManager.h"
35#include "events/EventManager.h"
36#include "events/Event.h"
37#include "events/Notification.h"
38#include "utility/utility.h"
39#ifdef PICO_HTTP_ENABLE_JWT
41#endif // PICO_HTTP_ENABLE_JWT
42
43
46 : FrameworkController("FrameworkManager", router, 1024, 2),
47 app(app),
48 networkTaskHandle(nullptr)
49{
50}
51
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}
100
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}
125
128{
129 // Not implemented (placeholder)
130 vTaskDelete(nullptr);
131}
132
134{
137 {
138 printf("[FrameworkManager] HttpServer started, notifying TimeManager...\n");
139 AppContext::get<TimeManager>()->onHttpServerStarted();
140 }
141}
142
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}
193
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}
Macro-based debug trace system with optional SD file logging.
#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
Event pub/sub manager for embedded applications using FreeRTOS.
Defines the Event structure and related utilities for event messaging.
Base class for applications using the PicoFramework.
Orchestrates application startup and network initialization.
#define configASSERT(x)
Stateless singleton class for creating and validating JWTs using HMAC-SHA256.
constexpr uint32_t eventMask(Enum e)
Helper function to create an event mask from an enum value.
static AppContext & getInstance()
void initFrameworkServices()
static constexpr std::uintptr_t getTypeKey()
Definition AppContext.h:91
Base class for applications using the framework.
virtual void start()
Initializes the application and its framework services.
Base class for event-driven control logic in embedded applications.
Starts and coordinates core system services like networking and time sync.
void onEvent(const Event &event)
Handles events posted to the FrameworkManager.
void poll()
Polling function for the FrameworkManager.
FrameworkApp * app
Pointer to the application task.
void setupTraceFromConfig()
Sets up debug tracing from configuration.
FrameworkManager(FrameworkApp *app, Router &router)
Constructor.
static void app_task(void *params)
Placeholder for an application-level task, if used.
void onStart()
Initializes the network and application tasks.
Network network
Network management.
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)
static bool initialize()
Start Wi-Fi with resilience, retrying connection if it fails.
Definition Network.cpp:41
static bool checkAndReconnect()
Attempt to connect to Wi-Fi with retries.
Definition Network.cpp:84
static bool startWifiWithResilience()
Start Wi-Fi with resilience, retrying connection if it fails.
Definition Network.cpp:63
static bool restart_wifi()
Restart the Wi-Fi interface.
Definition Network.cpp:149
The central router for handling HTTP requests and middleware.
Definition Router.h:60
void onNetworkReady()
hanles network ready event.
void start()
Start the time manager.
Delegates to user or system configuration.
#define TRACE_LOG_PATH
#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,...
Represents a framework event, optionally carrying payload data.
Definition Event.h:24
Notification notification
Notification identifier (system or user)
Definition Event.h:25
SystemNotification system
NotificationKind kind
void rebootSystem()
Reboot the device immediately.
Definition utility.cpp:237
System utilities for diagnostics, memory, stack usage, and tracing.