Compare commits

...
2 Commits
Author SHA1 Message Date
cpq b30acaaabb can_write() honors link state 2026-11-26 11:50:49 +00:00
robert dc91248aca Fix heap usage reporting in baremetal 2026-11-25 16:13:46 +02:00
5 changed files with 20 additions and 15 deletions

No files matched your search

+5 -3
View File
@@ -12008,7 +12008,7 @@ static void mg_tcpip_poll(struct mg_tcpip_if *ifp, uint64_t now) {
if (expired_1000ms) { // ifp->state rules over state6 if (expired_1000ms) { // ifp->state rules over state6
mg_ip_link(ifp, drv_up, l2_up); // Handle IPv4 mg_ip_link(ifp, drv_up, l2_up); // Handle IPv4
mg_ip6_link(ifp, drv_up, l2_up); // Handle IPv6 mg_ip6_link(ifp, drv_up, l2_up); // Handle IPv6
if (ifp->state < MG_TCPIP_STATE_UP) MG_ERROR(("Network is down")); if (ifp->state < MG_TCPIP_STATE_UP) MG_ERROR(("Network is down, state %hhu", ifp->state));
mg_tcpip_call(ifp, MG_TCPIP_EV_TIMER_1S, NULL); mg_tcpip_call(ifp, MG_TCPIP_EV_TIMER_1S, NULL);
} }
} }
@@ -12254,8 +12254,10 @@ static void write_conn(struct mg_connection *c) {
} }
static bool can_write(struct mg_connection *c) { static bool can_write(struct mg_connection *c) {
return c->is_connecting == 0 && c->is_resolving == 0 && c->send.len > 0 && struct mg_tcpip_if *ifp = c->mgr->ifp;
c->is_tls_hs == 0 && c->is_arplooking == 0; bool ready = (ifp->ip != 0 && ifp->state == MG_TCPIP_STATE_READY);
return ready && c->is_connecting == 0 && c->is_resolving == 0 &&
c->send.len > 0 && c->is_tls_hs == 0 && c->is_arplooking == 0;
} }
void mg_mgr_poll(struct mg_mgr *mgr, int ms) { void mg_mgr_poll(struct mg_mgr *mgr, int ms) {
+5 -3
View File
@@ -2200,7 +2200,7 @@ static void mg_tcpip_poll(struct mg_tcpip_if *ifp, uint64_t now) {
if (expired_1000ms) { // ifp->state rules over state6 if (expired_1000ms) { // ifp->state rules over state6
mg_ip_link(ifp, drv_up, l2_up); // Handle IPv4 mg_ip_link(ifp, drv_up, l2_up); // Handle IPv4
mg_ip6_link(ifp, drv_up, l2_up); // Handle IPv6 mg_ip6_link(ifp, drv_up, l2_up); // Handle IPv6
if (ifp->state < MG_TCPIP_STATE_UP) MG_ERROR(("Network is down")); if (ifp->state < MG_TCPIP_STATE_UP) MG_ERROR(("Network is down, state %hhu", ifp->state));
mg_tcpip_call(ifp, MG_TCPIP_EV_TIMER_1S, NULL); mg_tcpip_call(ifp, MG_TCPIP_EV_TIMER_1S, NULL);
} }
} }
@@ -2446,8 +2446,10 @@ static void write_conn(struct mg_connection *c) {
} }
static bool can_write(struct mg_connection *c) { static bool can_write(struct mg_connection *c) {
return c->is_connecting == 0 && c->is_resolving == 0 && c->send.len > 0 && struct mg_tcpip_if *ifp = c->mgr->ifp;
c->is_tls_hs == 0 && c->is_arplooking == 0; bool ready = (ifp->ip != 0 && ifp->state == MG_TCPIP_STATE_READY);
return ready && c->is_connecting == 0 && c->is_resolving == 0 &&
c->send.len > 0 && c->is_tls_hs == 0 && c->is_arplooking == 0;
} }
void mg_mgr_poll(struct mg_mgr *mgr, int ms) { void mg_mgr_poll(struct mg_mgr *mgr, int ms) {
+2
View File
@@ -1280,6 +1280,8 @@ static void test_frag_send_path(void) {
mif.driver = &driver; mif.driver = &driver;
mif.driver_data = &s_driver_data; mif.driver_data = &s_driver_data;
mg_tcpip_init(&mgr, &mif); mg_tcpip_init(&mgr, &mif);
mif.ip = mg_htonl(0xa00001);
mif.state = MG_TCPIP_STATE_READY; // so can_write() lets the data out
mif.mtu = 500; // force ad hoc small MTU to fragment IP mif.mtu = 500; // force ad hoc small MTU to fragment IP
mg_http_listen(&mgr, "http://0.0.0.0:80", frag_send_fn, NULL); mg_http_listen(&mgr, "http://0.0.0.0:80", frag_send_fn, NULL);
mgr.conns->pfn = NULL; mgr.conns->pfn = NULL;
@@ -23,6 +23,7 @@
/* Private includes ----------------------------------------------------------*/ /* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */ /* USER CODE BEGIN Includes */
#include "mongoose.h" #include "mongoose.h"
#include <malloc.h>
/* USER CODE END Includes */ /* USER CODE END Includes */
@@ -85,7 +86,6 @@ static void MX_USART3_UART_Init(void);
/* Private user code ---------------------------------------------------------*/ /* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */ /* USER CODE BEGIN 0 */
size_t mg_usage(int task, int metric) { size_t mg_usage(int task, int metric) {
extern void *_sbrk(ptrdiff_t incr);
extern unsigned char _end, _sstack, _estack; extern unsigned char _end, _sstack, _estack;
extern uint32_t _Min_Stack_Size; extern uint32_t _Min_Stack_Size;
switch (metric) { switch (metric) {
@@ -101,10 +101,9 @@ size_t mg_usage(int task, int metric) {
} }
case MG_USAGE_HEAP_TOTAL: case MG_USAGE_HEAP_TOTAL:
return (size_t) &_sstack - (size_t) &_end; return (size_t) &_sstack - (size_t) &_end;
case MG_USAGE_HEAP_USED: { case MG_USAGE_HEAP_USED:
size_t heap = (size_t) _sbrk(0), start = (size_t) &_end; // The program break includes freed blocks retained for reuse by malloc
return heap >= start && heap <= (size_t) &_sstack ? heap - start : 0; return (size_t) mallinfo().uordblks;
}
default: return 0; default: return 0;
} }
} }
+4 -4
View File
@@ -3,16 +3,16 @@
#include "hal.h" #include "hal.h"
#include "mongoose.h" #include "mongoose.h"
#include <malloc.h>
size_t mg_usage(int task, int metric) { size_t mg_usage(int task, int metric) {
extern void *_sbrk(int incr); extern unsigned char _estack[];
extern unsigned char _end[], _estack[];
if (metric == MG_USAGE_NUM_TASKS) return 1; if (metric == MG_USAGE_NUM_TASKS) return 1;
if (metric == MG_USAGE_TASK_NAME) return task == 0 ? (size_t) "main" : 0; if (metric == MG_USAGE_TASK_NAME) return task == 0 ? (size_t) "main" : 0;
// Heap and stack share RAM, so neither has a fixed total. // Heap and stack share RAM, so neither has a fixed total.
if (metric == MG_USAGE_HEAP_USED) { if (metric == MG_USAGE_HEAP_USED) {
size_t heap = (size_t) _sbrk(0), start = (size_t) _end; // The program break includes freed blocks retained for reuse by malloc
return heap >= start && heap <= (size_t) _estack ? heap - start : 0; return (size_t) mallinfo().uordblks;
} }
if (metric == MG_USAGE_STACK_USED && task == 0) { if (metric == MG_USAGE_STACK_USED && task == 0) {
size_t sp = (size_t) __get_MSP(), top = (size_t) _estack; size_t sp = (size_t) __get_MSP(), top = (size_t) _estack;