Logo Pico-Framework A web-first embedded framework for C++
Loading...
Searching...
No Matches
utility.cpp File Reference

Implementation of diagnostics and utility functions for memory, stack, and lwIP state. More...

#include "framework_config.h"
#include "DebugTrace.h"
#include "utility/utility.h"
#include "FreeRTOS.h"
#include "task.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <algorithm>
#include <sstream>
#include "pico/stdlib.h"
#include <lwip/memp.h>
#include <lwip/stats.h>
#include <lwip/pbuf.h>
#include <lwip/sockets.h>
#include <lwip/ip_addr.h>
#include "RP2040.h"
+ Include dependency graph for utility.cpp:

Go to the source code of this file.

Functions

void printTaskStackSizes ()
 
void logSystemStats ()
 
void printHeapInfo ()
 
void printSystemMemoryInfo ()
 
std::string toLower (std::string str)
 
void runTimeStats ()
 
void printActivePCBs ()
 
void printTCPState ()
 
void printMemsize ()
 
int is_in_interrupt (void)
 
void debug_print (const char *msg)
 
void debug_print (const std::string &msg)
 
void debug_print (const char *msg, size_t len)
 


 
void warning (const std::string &msg)
 
void warning (const std::string &msg, int32_t code)
 
void rebootSystem ()
 Reboot the device immediately.
 

Detailed Description

Author
Ian Archbell

Provides runtime tools for embedded system visibility including FreeRTOS and lwIP stats, memory usage, and debug trace control.

Version
0.1
Date
2025-03-26
License:\n MIT License

Definition in file utility.cpp.

Function Documentation

◆ debug_print() [1/3]

void debug_print ( const char *  msg)

Print in a mode that can be used in an ISR context.

Print in a mode that can be used in an ISR context.

Definition at line 184 of file utility.cpp.

185{
186 if (msg)
187 {
188 for (const char *p = msg; *p; ++p)
189 {
190 uart_putc(uart0, *p);
191 }
192 }
193}

Referenced by EventManager::enqueue().

+ Here is the caller graph for this function:

◆ debug_print() [2/3]

void debug_print ( const char *  msg,
size_t  len 
)



Definition at line 203 of file utility.cpp.

204{
205 if (msg && len > 0)
206 {
207 for (size_t i = 0; i < len; ++i)
208 {
209 uart_putc(uart0, msg[i]);
210 }
211 }
212}

◆ debug_print() [3/3]

void debug_print ( const std::string &  msg)

Definition at line 195 of file utility.cpp.

196{
197 for (const char *p = msg.c_str(); *p; ++p)
198 {
199 uart_putc(uart0, *p);
200 }
201}

◆ is_in_interrupt()

int is_in_interrupt ( void  )

Check whether currently in interrupt context.

Check whether currently in interrupt context.

Definition at line 172 of file utility.cpp.

173{
174 uint32_t ipsr_value;
175#ifndef UNIT_TEST
176 __asm volatile("MRS %0, ipsr" : "=r"(ipsr_value));
177#else
178 ipsr_value = 0;
179#endif
180 return (ipsr_value != 0);
181}

Referenced by EventManager::enqueue(), and EventManager::postNotification().

+ Here is the caller graph for this function:

◆ logSystemStats()

void logSystemStats ( )

Print FreeRTOS heap and task stack status.

Print FreeRTOS heap and task stack status.

Definition at line 65 of file utility.cpp.

66{
67 printf("\n===== SYSTEM STATS =====\n");
68 printf("Free heap size: %zu bytes\n", xPortGetFreeHeapSize());
69 printf("Minimum ever free heap: %zu bytes\n", xPortGetMinimumEverFreeHeapSize());
70 printf("Stack watermark: AcceptConnect: %zu, HandleClient: %zu, tcpip_thread: %zu\n",
71 uxTaskGetStackHighWaterMark(NULL),
72 uxTaskGetStackHighWaterMark(NULL),
73 uxTaskGetStackHighWaterMark(NULL));
74 printf("========================\n");
75}

◆ printActivePCBs()

void printActivePCBs ( )

Print number of active lwIP TCP PCBs.

Print number of active lwIP TCP PCBs.

Definition at line 142 of file utility.cpp.

143{
144#if LWIP_STATS && MEMP_STATS
145 TRACE("Active TCP PCBs: %d\n", lwip_stats.memp[MEMP_TCP_PCB]->used);
146#else
147 TRACE("LWIP stats are not enabled, cannot track active PCBs.\n");
148#endif
149}
#define TRACE(...)
Default trace (INFO level).
Definition DebugTrace.h:187

References TRACE.

◆ printHeapInfo()

void printHeapInfo ( )

Print heap size and minimum ever free heap.

Print heap size and minimum ever free heap.

Definition at line 78 of file utility.cpp.

79{
80 TRACE("Heap Information:\n");
81 TRACE("Free heap size: %u bytes\n", (unsigned int)xPortGetFreeHeapSize());
82 TRACE("Minimum ever free heap size: %u bytes\n", (unsigned int)xPortGetMinimumEverFreeHeapSize());
83}

References TRACE.

Referenced by printSystemMemoryInfo().

+ Here is the caller graph for this function:

◆ printMemsize()

void printMemsize ( )

Print lwIP memory pool statistics.

Print lwIP memory pool statistics.

Definition at line 165 of file utility.cpp.

166{
167 printf("MEM SIZE: %d\n", lwip_stats.mem.avail);
168 printf("MEM USED: %d\n", lwip_stats.mem.used);
169}

◆ printSystemMemoryInfo()

void printSystemMemoryInfo ( )

Print stack and heap usage together.

Print stack and heap usage together.

Definition at line 86 of file utility.cpp.

87{
90}
void printTaskStackSizes()
Definition utility.cpp:39
void printHeapInfo()
Definition utility.cpp:78

References printHeapInfo(), and printTaskStackSizes().

+ Here is the call graph for this function:

◆ printTaskStackSizes()

void printTaskStackSizes ( )

Print stack high watermark for all FreeRTOS tasks.

Print stack high watermark for all FreeRTOS tasks.

Definition at line 39 of file utility.cpp.

40{
41 TRACE("Task Stack Sizes:\n");
42
43 TaskStatus_t *pxTaskStatusArray;
44 UBaseType_t uxTaskCount = uxTaskGetNumberOfTasks();
45 pxTaskStatusArray = (TaskStatus_t *)pvPortMalloc(uxTaskCount * sizeof(TaskStatus_t));
46
47 if (pxTaskStatusArray != NULL)
48 {
49 uxTaskGetSystemState(pxTaskStatusArray, uxTaskCount, NULL);
50 for (UBaseType_t i = 0; i < uxTaskCount; i++)
51 {
52 TRACE("Task %s: Stack high watermark: %u bytes\n",
53 pxTaskStatusArray[i].pcTaskName,
54 pxTaskStatusArray[i].usStackHighWaterMark);
55 }
56 vPortFree(pxTaskStatusArray);
57 }
58 else
59 {
60 TRACE("Failed to allocate memory for task status array.\n");
61 }
62}

References TRACE.

Referenced by printSystemMemoryInfo().

+ Here is the caller graph for this function:

◆ printTCPState()

void printTCPState ( )

Print full lwIP statistics if available.

Print full lwIP statistics if available.

Definition at line 152 of file utility.cpp.

153{
154 TRACE("\n===== TCP/IP STATE =====\n");
155#if LWIP_STATS && TRACE_INFO
156 extern void stats_display(void);
157 stats_display();
158#else
159 TRACE("lwIP stats unavailable. Ensure LWIP_STATS is enabled in lwipopts.h\n");
160#endif
161 TRACE("========================\n");
162}

References TRACE.

◆ rebootSystem()

void rebootSystem ( )

This function performs a clean system reboot using the appropriate method for the platform (RP2040 or RP2350). Intended for controlled resets, such as after persistent failures or user-initiated reboots.

Definition at line 237 of file utility.cpp.

237 {
238 NVIC_SystemReset();
239}

Referenced by FrameworkManager::poll().

+ Here is the caller graph for this function:

◆ runTimeStats()

void runTimeStats ( )

Display task priorities, stack usage, and heap stats.

Display task priorities, stack usage, and heap stats.

Definition at line 102 of file utility.cpp.

103{
104 TaskStatus_t *pxTaskStatusArray;
105 UBaseType_t uxArraySize = uxTaskGetNumberOfTasks();
106 unsigned long ulTotalRunTime;
107
108 TRACE("Number of tasks %d\n", uxArraySize);
109 pxTaskStatusArray = (TaskStatus_t *)pvPortMalloc(uxArraySize * sizeof(TaskStatus_t));
110
111 if (pxTaskStatusArray != NULL)
112 {
113 uxArraySize = uxTaskGetSystemState(pxTaskStatusArray, uxArraySize, &ulTotalRunTime);
114
115 for (UBaseType_t x = 0; x < uxArraySize; x++)
116 {
117 printf("Task: %d \t cPri:%d \t bPri:%d \t hw:%zu \t%s \t core: %s\n",
118 pxTaskStatusArray[x].xTaskNumber,
119 pxTaskStatusArray[x].uxCurrentPriority,
120 pxTaskStatusArray[x].uxBasePriority,
121 pxTaskStatusArray[x].usStackHighWaterMark,
122 pxTaskStatusArray[x].pcTaskName);
123 printf("%d\n", get_core_num());
124 }
125 vPortFree(pxTaskStatusArray);
126 }
127 else
128 {
129 panic("Failed to allocate space for stats\n");
130 }
131
132 HeapStats_t heapStats;
133 vPortGetHeapStats(&heapStats);
134 printf("HEAP avl: %zu, blocks %zu, alloc: %zu, free: %zu\n",
135 heapStats.xAvailableHeapSpaceInBytes,
136 heapStats.xNumberOfFreeBlocks,
137 heapStats.xNumberOfSuccessfulAllocations,
138 heapStats.xNumberOfSuccessfulFrees);
139}

References TRACE.

◆ toLower()

std::string toLower ( std::string  str)

Convert string to lowercase.

Convert string to lowercase.

Definition at line 93 of file utility.cpp.

94{
95 std::transform(str.begin(), str.end(), str.begin(),
96 [](unsigned char c)
97 { return std::tolower(c); });
98 return str;
99}

Referenced by HttpRequest::getHeader(), and HttpParser::isChunkedEncoding().

+ Here is the caller graph for this function:

◆ warning() [1/2]

void warning ( const std::string &  msg)

Definition at line 215 of file utility.cpp.

216{
217 printf("\033[31m");
218 printf("WARNING: %s\n", msg.c_str());
219 printf("\033[0m");
220}

Referenced by HttpServer::run(), and Tcp::send().

+ Here is the caller graph for this function:

◆ warning() [2/2]

void warning ( const std::string &  msg,
int32_t  code 
)

Definition at line 223 of file utility.cpp.

224{
225 std::string fullMsg = msg + " (code: " + std::to_string(code) + ")\n";
226 printf("\033[31m");
227 printf("WARNING: %s\n", fullMsg.c_str());
228 printf("\033[0m");
229}