// Copyright (c) 2026 Cesanta Software Limited // All rights reserved #include "hal.h" #include "mongoose.h" int echo_init(uint16_t port); void echo_poll(void); #ifndef UART_DEBUG #define UART_DEBUG USART3 #define UART_DEBUG_TX_PIN PIN('D', 8) #define UART_DEBUG_RX_PIN PIN('D', 9) #else #define UART_DEBUG_TX_PIN PIN('A', 9) #define UART_DEBUG_RX_PIN PIN('A', 10) #endif #define LED1 PIN('B', 0) #define LED2 PIN('E', 1) #define LED3 PIN('B', 14) static void log_fn(char ch, void *param) { hal_uart_write_buf(param, &ch, 1); } static void blink_task(void) { static uint64_t blink_timer = 0; if (hal_timer_expired(&blink_timer, 500, hal_get_tick())) { hal_gpio_toggle(LED2); } } uint64_t mg_millis(void) { return hal_get_tick(); } bool mg_random(void *buf, size_t len) { for (size_t n = 0; n < len; n += sizeof(uint32_t)) { uint32_t r = hal_rng_read(); memcpy((char *) buf + n, &r, n + sizeof(r) > len ? len - n : sizeof(r)); } return true; } // BSD sockets API UDP echo server: sends each datagram back to its sender static int s_udp_sock = -1; static void udp_echo_init(uint16_t port) { struct sockaddr_in sa = { .sin_family = AF_INET, .sin_port = htons(port), .sin_addr = {.s_addr = INADDR_ANY}, }; s_udp_sock = socket(AF_INET, SOCK_DGRAM, 0); bind(s_udp_sock, (struct sockaddr *) &sa, sizeof(sa)); MG_INFO(("UDP echo server on port %hu, sock %d", port, s_udp_sock)); } static void udp_echo_poll(void) { char buf[1500]; struct sockaddr_in sin; struct sockaddr *sa = (struct sockaddr *) &sin; socklen_t len = sizeof(sin); ssize_t n = recvfrom(s_udp_sock, buf, sizeof(buf), MSG_DONTWAIT, sa, &len); if (n > 0) sendto(s_udp_sock, buf, (size_t) n, MSG_DONTWAIT, sa, len); } static void http_ev_handler(struct mg_connection *c, int ev, void *ev_data) { if (ev == MG_EV_HTTP_HDRS) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; if (mg_match(hm->uri, mg_str("/ota"), NULL)) { mg_http_stream_body(c, hm, mg_http_ota_cb, NULL); } } else if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; if (mg_match(hm->uri, mg_str("/api/tick"), NULL)) { mg_http_reply(c, 200, "", "{%m:%llu}\n", MG_ESC("tick"), hal_get_tick()); } else { mg_http_reply(c, 200, "", "Hi from Mongoose, tick %llu\n", hal_get_tick()); } } } int main(void) { hal_clock_init(); hal_uart_init(UART_DEBUG, UART_DEBUG_TX_PIN, UART_DEBUG_RX_PIN, 115200); mg_log_set_fn(log_fn, UART_DEBUG); hal_rng_init(); hal_ethernet_init(); hal_gpio_output(LED1); hal_gpio_output(LED2); hal_gpio_output(LED3); MG_INFO(("Initialised. CPU clock: %lu MHz", SystemCoreClock / 1000000)); // Mongoose event manager processes both Mongoose API and BSD sockets API struct mg_mgr mgr; mg_mgr_init(&mgr); // This is Mongoose-API HTTP server mg_http_listen(&mgr, "http://0.0.0.0", http_ev_handler, NULL); // This is BSD sockets API tcp echo server that handles multiple concurrent // connections. Note - baremetal implementation is non-blocking only. echo_init(1234); // This is BSD sockets API UDP echo server, non-blocking too udp_echo_init(1234); // Baremetal superloop. Each function in the loop must not block for (;;) { mg_mgr_poll(&mgr, 0); echo_poll(); udp_echo_poll(); blink_task(); } return 0; }