Split net stack into eth/arp/ipv4/icmp, deferred handler responses, ping tests
This commit is contained in:
@@ -1,98 +1,97 @@
|
||||
#include "test_handlers.h"
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
#include "pico/stdlib.h"
|
||||
#include "pico/time.h"
|
||||
#include "w6300.h"
|
||||
#include "net.h"
|
||||
#include "icmp.h"
|
||||
|
||||
static w6300::socket_id test_socket{1};
|
||||
|
||||
static ResponseTest test_discovery() {
|
||||
static ResponseTest test_discovery(const responder&) {
|
||||
ResponseTest resp;
|
||||
resp.pass = true;
|
||||
|
||||
uint8_t req_buf[1514];
|
||||
span_writer req_out(req_buf, sizeof(req_buf));
|
||||
auto req_len = encode_response_into(req_out, 0, RequestInfo{});
|
||||
if (!req_len) {
|
||||
resp.pass = false;
|
||||
resp.messages.push_back("encode: overflow");
|
||||
return resp;
|
||||
}
|
||||
auto send_result = w6300::send(test_socket, std::span<const uint8_t>{req_buf, *req_len});
|
||||
if (!send_result) {
|
||||
resp.pass = false;
|
||||
resp.messages.push_back("send: error " + std::to_string(static_cast<int>(send_result.error())));
|
||||
return resp;
|
||||
}
|
||||
|
||||
uint8_t rx_buf[512];
|
||||
|
||||
auto deadline = make_timeout_time_ms(5000);
|
||||
std::expected<uint16_t, w6300::sock_error> recv_result = std::unexpected(w6300::sock_error::busy);
|
||||
while (get_absolute_time() < deadline) {
|
||||
recv_result = w6300::recv(test_socket, std::span{rx_buf});
|
||||
if (recv_result || recv_result.error() != w6300::sock_error::busy) break;
|
||||
}
|
||||
|
||||
if (!recv_result) {
|
||||
resp.pass = false;
|
||||
if (recv_result.error() == w6300::sock_error::busy) {
|
||||
resp.messages.push_back("recv: timed out after 5s");
|
||||
} else {
|
||||
resp.messages.push_back("recv: error " + std::to_string(static_cast<int>(recv_result.error())));
|
||||
}
|
||||
return resp;
|
||||
}
|
||||
|
||||
resp.messages.push_back("received " + std::to_string(*recv_result) + " bytes");
|
||||
|
||||
auto info = decode_response<ResponseInfo>(rx_buf, *recv_result);
|
||||
if (!info) {
|
||||
resp.pass = false;
|
||||
resp.messages.push_back("decode: msgpack error " + std::to_string(static_cast<int>(info.error())));
|
||||
return resp;
|
||||
}
|
||||
|
||||
if (info->firmware_name.empty()) {
|
||||
resp.pass = false;
|
||||
resp.messages.push_back("firmware_name is empty");
|
||||
} else {
|
||||
resp.messages.push_back("firmware_name: " + info->firmware_name);
|
||||
}
|
||||
|
||||
bool mac_zero = true;
|
||||
for (auto b : info->mac) { if (b != 0) { mac_zero = false; break; } }
|
||||
if (mac_zero) {
|
||||
resp.pass = false;
|
||||
resp.messages.push_back("mac is all zeros");
|
||||
}
|
||||
|
||||
bool ip_zero = true;
|
||||
for (auto b : info->ip) { if (b != 0) { ip_zero = false; break; } }
|
||||
if (ip_zero) {
|
||||
resp.pass = false;
|
||||
resp.messages.push_back("ip is all zeros");
|
||||
}
|
||||
|
||||
resp.messages.push_back("TODO: rewrite as deferred test");
|
||||
return resp;
|
||||
}
|
||||
|
||||
using test_fn = ResponseTest (*)();
|
||||
static void test_ping(const responder& resp, ipv4::ip4_addr dst_ip) {
|
||||
auto& ns = net_get_state();
|
||||
uint16_t ping_id = 0x1234;
|
||||
|
||||
static const std::unordered_map<std::string_view, test_fn> tests = {
|
||||
{"discovery", test_discovery},
|
||||
uint8_t tx_buf[128];
|
||||
size_t len = icmp::build_echo_request(
|
||||
std::span{tx_buf}, ns.mac, ns.ip,
|
||||
eth::MAC_BROADCAST, dst_ip, ping_id, 1);
|
||||
if (len == 0) {
|
||||
resp.respond(ResponseTest{false, {"build_echo_request failed"}});
|
||||
return;
|
||||
}
|
||||
|
||||
net_send_raw(std::span<const uint8_t>{tx_buf, len});
|
||||
|
||||
ipv4::ip4_addr our_ip = ns.ip;
|
||||
|
||||
auto done = std::make_shared<bool>(false);
|
||||
auto cb = std::make_shared<std::function<void(std::span<const uint8_t>)>>();
|
||||
*cb = [resp, ping_id, our_ip, done, cb](std::span<const uint8_t> frame) {
|
||||
if (*done) return;
|
||||
ipv4::ip4_addr src_ip;
|
||||
if (!icmp::parse_echo_reply(frame, src_ip, ping_id)) {
|
||||
net_add_frame_callback(*cb);
|
||||
return;
|
||||
}
|
||||
if (src_ip == our_ip) {
|
||||
net_add_frame_callback(*cb);
|
||||
return;
|
||||
}
|
||||
*done = true;
|
||||
std::string ip_str = std::to_string(src_ip[0]) + "." + std::to_string(src_ip[1]) + "." +
|
||||
std::to_string(src_ip[2]) + "." + std::to_string(src_ip[3]);
|
||||
resp.respond(ResponseTest{true, {"reply from " + ip_str}});
|
||||
};
|
||||
net_add_frame_callback(*cb);
|
||||
|
||||
dispatch_schedule_ms(5000, [resp, done]() {
|
||||
if (*done) return;
|
||||
*done = true;
|
||||
resp.respond(ResponseTest{false, {"no reply from non-self host within 5s"}});
|
||||
});
|
||||
}
|
||||
|
||||
static void test_ping_subnet(const responder& resp) {
|
||||
test_ping(resp, {169, 254, 255, 255});
|
||||
}
|
||||
|
||||
static void test_ping_global(const responder& resp) {
|
||||
test_ping(resp, {255, 255, 255, 255});
|
||||
}
|
||||
|
||||
using sync_test_fn = ResponseTest (*)(const responder&);
|
||||
using async_test_fn = void (*)(const responder&);
|
||||
|
||||
struct test_entry {
|
||||
sync_test_fn sync;
|
||||
async_test_fn async;
|
||||
};
|
||||
|
||||
ResponseListTests handle_list_tests(const RequestListTests&) {
|
||||
static const std::unordered_map<std::string_view, test_entry> tests = {
|
||||
{"discovery", {test_discovery, nullptr}},
|
||||
{"ping_subnet", {nullptr, test_ping_subnet}},
|
||||
{"ping_global", {nullptr, test_ping_global}},
|
||||
};
|
||||
|
||||
std::optional<ResponseListTests> handle_list_tests(const responder&, const RequestListTests&) {
|
||||
ResponseListTests resp;
|
||||
for (const auto& [name, _] : tests)
|
||||
resp.names.emplace_back(name);
|
||||
return resp;
|
||||
}
|
||||
|
||||
ResponseTest handle_test(const RequestTest& req) {
|
||||
std::optional<ResponseTest> handle_test(const responder& resp, const RequestTest& req) {
|
||||
auto it = tests.find(req.name);
|
||||
if (it == tests.end())
|
||||
return {false, {"unknown test: " + req.name}};
|
||||
return it->second();
|
||||
return ResponseTest{false, {"unknown test: " + req.name}};
|
||||
if (it->second.sync)
|
||||
return it->second.sync(resp);
|
||||
it->second.async(resp);
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user