Move C++ tree into firmware/
This commit is contained in:
55
firmware/dhcp.cpp
Normal file
55
firmware/dhcp.cpp
Normal file
@@ -0,0 +1,55 @@
|
||||
#include "dhcp.h"
|
||||
#include <span>
|
||||
#include "pico/rand.h"
|
||||
#include "w6300.h"
|
||||
|
||||
namespace dhcp_opt {
|
||||
constexpr uint8_t message_type = 53;
|
||||
constexpr uint8_t param_request = 55;
|
||||
constexpr uint8_t end = 255;
|
||||
constexpr uint8_t discover = 1;
|
||||
constexpr uint8_t subnet_mask = 1;
|
||||
}
|
||||
|
||||
struct __attribute__((packed)) dhcp_discover {
|
||||
uint8_t op = 1;
|
||||
uint8_t htype = 1;
|
||||
uint8_t hlen = 6;
|
||||
uint8_t hops = 0;
|
||||
uint32_t xid = __builtin_bswap32(static_cast<uint32_t>(get_rand_64()));
|
||||
uint16_t secs = 0;
|
||||
uint16_t flags = __builtin_bswap16(0x8000);
|
||||
std::array<uint8_t, 4> ciaddr = {};
|
||||
std::array<uint8_t, 4> yiaddr = {};
|
||||
std::array<uint8_t, 4> siaddr = {};
|
||||
std::array<uint8_t, 4> giaddr = {};
|
||||
std::array<uint8_t, 16> chaddr = {};
|
||||
std::array<uint8_t, 64> sname = {};
|
||||
std::array<uint8_t, 128> file = {};
|
||||
std::array<uint8_t, 4> magic = {99, 130, 83, 99};
|
||||
uint8_t opt_msg_type[3] = {dhcp_opt::message_type, 1, dhcp_opt::discover};
|
||||
uint8_t opt_params[3] = {dhcp_opt::param_request, 1, dhcp_opt::subnet_mask};
|
||||
uint8_t opt_end = dhcp_opt::end;
|
||||
};
|
||||
|
||||
static_assert(sizeof(dhcp_discover) == 247);
|
||||
|
||||
static void send_discover(timer_queue& timers, const std::array<uint8_t, 6>& mac) {
|
||||
auto sn = w6300::socket_id{0};
|
||||
w6300::open_socket(sn, w6300::protocol::udp, w6300::port_num{68}, w6300::sock_flag::none);
|
||||
|
||||
dhcp_discover pkt;
|
||||
std::copy(mac.begin(), mac.end(), pkt.chaddr.begin());
|
||||
|
||||
w6300::ip_address broadcast = {};
|
||||
broadcast.ip = {255, 255, 255, 255};
|
||||
broadcast.len = 4;
|
||||
|
||||
w6300::sendto(sn, std::span{reinterpret_cast<uint8_t*>(&pkt), sizeof(pkt)}, broadcast, w6300::port_num{67});
|
||||
|
||||
timers.schedule_ms(5000, [&timers, mac]() { send_discover(timers, mac); });
|
||||
}
|
||||
|
||||
void dhcp_start(timer_queue& timers, const std::array<uint8_t, 6>& mac) {
|
||||
send_discover(timers, mac);
|
||||
}
|
||||
Reference in New Issue
Block a user