Eliminate std::function: fn-pointer callbacks, per-test test_state structs, udp.cpp with link-time udp::client::handler, udp::address

This commit is contained in:
Ian Gulliver
2026-04-19 00:32:13 -07:00
parent 40f7fb5941
commit 32044a5cbd
11 changed files with 378 additions and 285 deletions

View File

@@ -1,7 +1,6 @@
#pragma once
#include <cstdarg>
#include <cstdio>
#include <functional>
#include <string>
#include <string_view>
#include <vector>
@@ -29,7 +28,8 @@ inline void dlogf(const char* fmt, ...) {
dlog(buf);
}
inline void dlog_if_slow(std::string_view label, uint32_t threshold_us, std::function<void()> fn) {
template <typename F>
inline void dlog_if_slow(std::string_view label, uint32_t threshold_us, F&& fn) {
uint32_t t0 = time_us_32();
fn();
uint32_t elapsed = time_us_32() - t0;

View File

@@ -1,26 +1,33 @@
#pragma once
#include <cstdint>
#include <cstdio>
#include <functional>
#include <optional>
#include <span>
#include "wire.h"
#include "timer_queue.h"
#include "net.h"
#include "prepend_buffer.h"
#include "udp.h"
struct responder {
uint32_t message_id;
send_fn send;
udp::address reply_to;
template <typename T>
void respond(const T& msg) const {
send([&](span_writer& out) {
return encode_response_into(out, message_id, msg);
});
const auto& ns = net_get_state();
prepend_buffer<4096> buf;
span_writer out(buf.payload_ptr(), 2048);
auto r = encode_response_into(out, message_id, msg);
if (!r) return;
buf.append(*r);
udp::prepend(buf, reply_to.mac, ns.mac, ns.ip, reply_to.ip,
PICOMAP_PORT_BE, reply_to.port, *r);
net_send_raw(buf.span());
}
};
using handler_fn = void (*)(responder resp, std::span<const uint8_t> payload);
using handler_fn = void (*)(const responder& resp, std::span<const uint8_t> payload);
struct handler_entry {
int8_t type_id;
@@ -28,7 +35,7 @@ struct handler_entry {
};
template <typename Req, auto Fn>
void typed_handler(responder resp, std::span<const uint8_t> payload) {
void typed_handler(const responder& resp, std::span<const uint8_t> payload) {
msgpack::parser p(payload.data(), static_cast<int>(payload.size()));
Req req;
auto tup = req.as_tuple();
@@ -46,6 +53,6 @@ void typed_handler(responder resp, std::span<const uint8_t> payload) {
}
void dispatch_init();
timer_handle dispatch_schedule_ms(uint32_t ms, std::function<void()> fn);
timer_handle dispatch_schedule_ms(uint32_t ms, void (*fn)());
bool dispatch_cancel_timer(timer_handle h);
[[noreturn]] void dispatch_run(std::span<const handler_entry> handlers);

View File

@@ -1,11 +1,9 @@
#pragma once
#include <expected>
#include <functional>
#include <cstdint>
#include <span>
#include "eth.h"
#include "ipv4.h"
#include "span_writer.h"
#include "msgpack.h"
#include "callback_list.h"
struct net_state {
@@ -13,22 +11,16 @@ struct net_state {
ipv4::ip4_addr ip;
};
using encode_fn = std::function<msgpack::result<size_t>(span_writer&)>;
using send_fn = std::function<void(encode_fn)>;
using net_handler = std::function<void(std::span<const uint8_t> payload,
send_fn send)>;
using net_frame_callback = std::function<bool(std::span<const uint8_t> frame)>;
using net_frame_callback = bool (*)(std::span<const uint8_t> frame);
using frame_cb_list = callback_list<net_frame_callback, 16>;
using frame_cb_handle = frame_cb_list::node*;
inline constexpr uint16_t PICOMAP_PORT_BE = __builtin_bswap16(28781);
bool net_init();
const net_state& net_get_state();
void net_set_handler(net_handler handler);
frame_cb_handle net_add_frame_callback(net_frame_callback cb);
void net_remove_frame_callback(frame_cb_handle h);
void net_poll(std::span<uint8_t> tx);
void net_send_raw(std::span<const uint8_t> data);
void net_handle_udp(std::span<const uint8_t> frame, span_writer& tx);

View File

@@ -1,11 +1,10 @@
#pragma once
#include <functional>
#include "pico/time.h"
#include "callback_list.h"
struct timer_entry {
absolute_time_t when;
std::function<void()> fn;
void (*fn)() = nullptr;
};
using timer_handle = callback_list<timer_entry, 16>::node*;
@@ -15,8 +14,8 @@ struct timer_queue {
alarm_id_t alarm = -1;
volatile bool irq_pending = false;
timer_handle schedule(absolute_time_t when, std::function<void()> fn) {
auto* n = list.insert_sorted({when, std::move(fn)},
timer_handle schedule(absolute_time_t when, void (*fn)()) {
auto* n = list.insert_sorted({when, fn},
[](const timer_entry& a, const timer_entry& b) {
return absolute_time_diff_us(b.when, a.when) < 0;
});
@@ -24,8 +23,8 @@ struct timer_queue {
return n;
}
timer_handle schedule_ms(uint32_t ms, std::function<void()> fn) {
return schedule(make_timeout_time_ms(ms), std::move(fn));
timer_handle schedule_ms(uint32_t ms, void (*fn)()) {
return schedule(make_timeout_time_ms(ms), fn);
}
bool cancel(timer_handle h) {
@@ -40,7 +39,7 @@ struct timer_queue {
irq_pending = false;
while (auto* n = list.front()) {
if (absolute_time_diff_us(get_absolute_time(), n->value.when) > 0) break;
auto fn = std::move(n->value.fn);
auto fn = n->value.fn;
list.remove(n);
fn();
}

View File

@@ -1,7 +1,10 @@
#pragma once
#include <cstdint>
#include <span>
#include "eth.h"
#include "ipv4.h"
#include "net.h"
#include "span_writer.h"
namespace udp {
@@ -13,6 +16,14 @@ struct __attribute__((packed)) header {
};
static_assert(sizeof(header) == 8);
struct address {
// mac is carried here until we grow an ARP cache; once we do, a
// destination mac comes from resolving ip and this field goes away.
eth::mac_addr mac;
ipv4::ip4_addr ip;
uint16_t port;
};
template <typename Buf>
void prepend(Buf& buf, const eth::mac_addr& dst_mac, const eth::mac_addr& src_mac,
ipv4::ip4_addr src_ip, ipv4::ip4_addr dst_ip,
@@ -26,4 +37,12 @@ void prepend(Buf& buf, const eth::mac_addr& dst_mac, const eth::mac_addr& src_ma
ipv4::prepend(buf, dst_mac, src_mac, src_ip, dst_ip, 17, sizeof(header) + payload_len, ttl);
}
void handle(std::span<const uint8_t> frame, span_writer& tx);
namespace client {
// Defined by the higher layer (dispatch) to receive decoded UDP payloads
// addressed to PICOMAP_PORT_BE. Resolved at link time.
void handler(std::span<const uint8_t> payload, const address& from);
} // namespace client
} // namespace udp