99 lines
3.0 KiB
C++
99 lines
3.0 KiB
C++
|
|
#include "test_handlers.h"
|
||
|
|
#include <unordered_map>
|
||
|
|
#include "pico/stdlib.h"
|
||
|
|
#include "pico/time.h"
|
||
|
|
#include "w6300.h"
|
||
|
|
|
||
|
|
static w6300::socket_id test_socket{1};
|
||
|
|
|
||
|
|
static ResponseTest test_discovery() {
|
||
|
|
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");
|
||
|
|
}
|
||
|
|
|
||
|
|
return resp;
|
||
|
|
}
|
||
|
|
|
||
|
|
using test_fn = ResponseTest (*)();
|
||
|
|
|
||
|
|
static const std::unordered_map<std::string_view, test_fn> tests = {
|
||
|
|
{"discovery", test_discovery},
|
||
|
|
};
|
||
|
|
|
||
|
|
ResponseListTests handle_list_tests(const RequestListTests&) {
|
||
|
|
ResponseListTests resp;
|
||
|
|
for (const auto& [name, _] : tests)
|
||
|
|
resp.names.emplace_back(name);
|
||
|
|
return resp;
|
||
|
|
}
|
||
|
|
|
||
|
|
ResponseTest handle_test(const RequestTest& req) {
|
||
|
|
auto it = tests.find(req.name);
|
||
|
|
if (it == tests.end())
|
||
|
|
return {false, {"unknown test: " + req.name}};
|
||
|
|
return it->second();
|
||
|
|
}
|