diff --git a/firmware/include/ring_buffer.h b/firmware/include/ring_buffer.h index 6070c05..01e820c 100644 --- a/firmware/include/ring_buffer.h +++ b/firmware/include/ring_buffer.h @@ -13,15 +13,17 @@ struct ring_buffer { uint16_t free() const { return N - used(); } bool empty() const { return head == tail; } - void push(std::span src) { - if (src.size() > free()) return; + bool push(std::span src) { + if (src.size() > free()) return false; for (auto& v : src) data[(tail++) % N] = v; + return true; } - void push(const T& v) { - if (free() == 0) return; + bool push(const T& v) { + if (free() == 0) return false; data[(tail++) % N] = v; + return true; } void push_overwrite(const T& v) { diff --git a/firmware/include/usb_cdc.h b/firmware/include/usb_cdc.h index cd82a16..bf75333 100644 --- a/firmware/include/usb_cdc.h +++ b/firmware/include/usb_cdc.h @@ -8,9 +8,10 @@ struct usb_cdc { ring_buffer tx; - void send(std::span data) { - tx.push(data); + bool send(std::span data) { + if (!tx.push(data)) return false; drain(); + return true; } void send(const std::vector& data) { diff --git a/firmware/lib/dispatch.cpp b/firmware/lib/dispatch.cpp index 3a11861..e2a5206 100644 --- a/firmware/lib/dispatch.cpp +++ b/firmware/lib/dispatch.cpp @@ -26,7 +26,9 @@ void dispatch_init() { } timer_handle dispatch_schedule_ms(uint32_t ms, std::function fn) { - return timers.schedule_ms(ms, std::move(fn)); + auto h = timers.schedule_ms(ms, std::move(fn)); + if (!h) dlogf("timer alloc failed: %lu ms", static_cast(ms)); + return h; } bool dispatch_cancel_timer(timer_handle h) { @@ -45,7 +47,10 @@ bool dispatch_cancel_timer(timer_handle h) { auto dispatch_msg = [&](const DecodedMessage& msg, std::function)> send) { auto it = handler_map.find(msg.type_id); - if (it == handler_map.end()) return; + if (it == handler_map.end()) { + dlogf("dispatch: unknown type_id %d", msg.type_id); + return; + } responder resp{msg.message_id, std::move(send)}; it->second(resp, msg.payload); }; @@ -79,8 +84,10 @@ bool dispatch_cancel_timer(timer_handle h) { dispatch_msg(*msg, [&](std::span data) { if (data.size() <= usb.tx.free()) { - usb.send(data); + if (!usb.send(data)) + dlogf("usb send failed: %zu bytes, %u free", data.size(), usb.tx.free()); } else { + dlogf("usb response too large: %zu bytes, %u free", data.size(), usb.tx.free()); uint8_t err_buf[256]; span_writer err_out(err_buf, sizeof(err_buf)); auto err = encode_response_into(err_out, msg->message_id, diff --git a/firmware/lib/net.cpp b/firmware/lib/net.cpp index fa76346..2583c0e 100644 --- a/firmware/lib/net.cpp +++ b/firmware/lib/net.cpp @@ -22,7 +22,10 @@ static std::vector frame_callbacks; void net_send_raw(std::span data) { dlog_if_slow("net_send_raw", 1000, [&]{ - w6300::send(raw_socket, data); + auto result = w6300::send(raw_socket, data); + if (!result) + dlogf("w6300 send failed: %zu bytes, err %d", + data.size(), static_cast(result.error())); }); } diff --git a/firmware/lib/test_handlers.cpp b/firmware/lib/test_handlers.cpp index 01cd67d..625db72 100644 --- a/firmware/lib/test_handlers.cpp +++ b/firmware/lib/test_handlers.cpp @@ -254,7 +254,7 @@ static void start_ping_rate(const responder& resp, uint16_t target, ping_rate_send_one(st); net_add_frame_callback(*cb); - st->timer = dispatch_schedule_ms(60000, [st]() { + st->timer = dispatch_schedule_ms(10000, [st]() { uint32_t elapsed_us = time_us_32() - st->start_us; char msg[64]; snprintf(msg, sizeof(msg), "timeout after %u/%u rt in %lu ms",