2026-04-07 06:58:39 +09:00
|
|
|
#include <unordered_map>
|
2026-04-07 07:34:24 +09:00
|
|
|
#include "pico/stdlib.h"
|
2026-04-07 06:58:39 +09:00
|
|
|
#include "pico/time.h"
|
2026-04-07 07:34:24 +09:00
|
|
|
#include "hardware/gpio.h"
|
2026-04-06 17:36:41 +09:00
|
|
|
#include "dispatch.h"
|
2026-04-06 17:24:34 +09:00
|
|
|
#include "handlers.h"
|
2026-04-07 06:58:39 +09:00
|
|
|
#include "w6300.h"
|
2026-04-05 21:04:56 +09:00
|
|
|
|
2026-04-07 07:34:24 +09:00
|
|
|
static constexpr uint8_t LED_PIN = 25;
|
|
|
|
|
|
|
|
|
|
static void led_toggle() {
|
|
|
|
|
gpio_xor_mask(1 << LED_PIN);
|
|
|
|
|
dispatch_schedule_ms(1000, led_toggle);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-06 20:09:30 +09:00
|
|
|
std::string_view firmware_name = "picomap_test";
|
|
|
|
|
|
2026-04-07 06:58:39 +09:00
|
|
|
static constexpr uint16_t PICOMAP_DISCOVERY_PORT = 28777;
|
|
|
|
|
|
|
|
|
|
static constexpr std::array<uint8_t, 4> picomap_discovery_ip = {239, 0, 112, 109};
|
|
|
|
|
|
|
|
|
|
static constexpr std::array<uint8_t, 6> picomap_discovery_mac = {
|
|
|
|
|
0x01, 0x00, 0x5e, 0x00, 0x70, 0x6d,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static w6300::socket_id test_socket{1};
|
|
|
|
|
|
|
|
|
|
static ResponseTest test_discovery() {
|
|
|
|
|
ResponseTest resp;
|
|
|
|
|
resp.pass = true;
|
|
|
|
|
|
|
|
|
|
w6300::ip_address dest = {};
|
|
|
|
|
std::copy(picomap_discovery_ip.begin(), picomap_discovery_ip.end(), dest.ip.begin());
|
|
|
|
|
dest.len = 4;
|
|
|
|
|
|
|
|
|
|
w6300::set_socket_dest_mac(test_socket, picomap_discovery_mac);
|
|
|
|
|
|
|
|
|
|
auto req = encode_request(0, RequestInfo{});
|
|
|
|
|
auto send_result = w6300::sendto(test_socket, std::span<const uint8_t>{req}, dest,
|
|
|
|
|
w6300::port_num{PICOMAP_DISCOVERY_PORT});
|
|
|
|
|
if (!send_result) {
|
|
|
|
|
resp.pass = false;
|
|
|
|
|
resp.messages.push_back("sendto: error " + std::to_string(static_cast<int>(send_result.error())));
|
|
|
|
|
return resp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint8_t rx_buf[512];
|
|
|
|
|
w6300::ip_address src_addr = {};
|
|
|
|
|
w6300::port_num src_port{0};
|
|
|
|
|
|
|
|
|
|
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::recvfrom(test_socket, std::span{rx_buf}, src_addr, src_port);
|
|
|
|
|
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("recvfrom: timed out after 5s");
|
|
|
|
|
} else {
|
|
|
|
|
resp.messages.push_back("recvfrom: error " + std::to_string(static_cast<int>(recv_result.error())));
|
|
|
|
|
}
|
|
|
|
|
return resp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resp.messages.push_back("received " + std::to_string(*recv_result) + " bytes from port " +
|
|
|
|
|
std::to_string(static_cast<uint16_t>(src_port)));
|
|
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return resp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using test_fn = ResponseTest (*)();
|
|
|
|
|
|
|
|
|
|
static const std::unordered_map<std::string_view, test_fn> tests = {
|
|
|
|
|
{"discovery", test_discovery},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static std::vector<std::vector<uint8_t>> handle_test(uint32_t message_id, const RequestTest& req) {
|
|
|
|
|
auto it = tests.find(req.name);
|
|
|
|
|
if (it == tests.end()) {
|
|
|
|
|
return {encode_response(message_id, ResponseTest{false, {"unknown test: " + req.name}})};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {encode_response(message_id, it->second())};
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-06 17:36:41 +09:00
|
|
|
static constexpr handler_entry handlers[] = {
|
|
|
|
|
{RequestPICOBOOT::ext_id, handle_picoboot},
|
|
|
|
|
{RequestInfo::ext_id, handle_info},
|
2026-04-07 06:58:39 +09:00
|
|
|
{RequestTest::ext_id, typed_handler<RequestTest, handle_test>},
|
2026-04-06 17:36:41 +09:00
|
|
|
};
|
2026-04-05 21:04:56 +09:00
|
|
|
|
|
|
|
|
int main() {
|
2026-04-06 20:01:22 +09:00
|
|
|
dispatch_init();
|
2026-04-07 06:58:39 +09:00
|
|
|
|
2026-04-07 07:34:24 +09:00
|
|
|
gpio_init(LED_PIN);
|
|
|
|
|
gpio_set_dir(LED_PIN, GPIO_OUT);
|
|
|
|
|
dispatch_schedule_ms(1000, led_toggle);
|
2026-04-07 06:58:39 +09:00
|
|
|
|
2026-04-06 20:01:22 +09:00
|
|
|
dispatch_run(handlers);
|
2026-04-05 21:04:56 +09:00
|
|
|
}
|