Fix public API: const refs, return by value, ip_address for addrs, remove void* dispatch

This commit is contained in:
Ian Gulliver
2026-04-05 15:32:20 +09:00
parent 732ea4250d
commit b7fd5f7668
3 changed files with 107 additions and 210 deletions
+2 -3
View File
@@ -69,9 +69,8 @@ int main() {
pico_unique_board_id_t uid; pico_unique_board_id_t uid;
pico_get_unique_board_id(&uid); pico_get_unique_board_id(&uid);
std::copy(uid.id, uid.id + 8, resp.board_id.begin()); std::copy(uid.id, uid.id + 8, resp.board_id.begin());
w6300::net_info info; auto ninfo = w6300::get_net_info();
w6300::get_net_info(&info); resp.mac = ninfo.mac;
resp.mac = info.mac;
send_bytes(encode_response(msg->message_id, resp)); send_bytes(encode_response(msg->message_id, resp));
break; break;
} }
+83 -185
View File
@@ -260,20 +260,6 @@ constexpr uint8_t PACK_REMAINED = static_cast<uint8_t>(pack_info::remained);
constexpr uint8_t PACK_COMPLETED = static_cast<uint8_t>(pack_info::completed); constexpr uint8_t PACK_COMPLETED = static_cast<uint8_t>(pack_info::completed);
constexpr uint8_t PACK_IPv6 = static_cast<uint8_t>(pack_info::ipv6); constexpr uint8_t PACK_IPv6 = static_cast<uint8_t>(pack_info::ipv6);
enum chip_ctl {
CW_SYS_LOCK, CW_SYS_UNLOCK, CW_GET_SYSLOCK,
CW_RESET_WIZCHIP, CW_INIT_WIZCHIP,
CW_GET_INTERRUPT, CW_CLR_INTERRUPT, CW_SET_INTRMASK, CW_GET_INTRMASK,
CW_SET_INTRTIME, CW_GET_INTRTIME, CW_SET_IEN, CW_GET_IEN,
CW_GET_ID, CW_GET_VER, CW_SET_SYSCLK, CW_GET_SYSCLK,
CW_RESET_PHY, CW_SET_PHYCONF, CW_GET_PHYCONF, CW_GET_PHYSTATUS,
CW_SET_PHYPOWMODE, CW_GET_PHYPOWMODE, CW_GET_PHYLINK
};
enum net_ctl {
CN_SET_NETINFO, CN_GET_NETINFO, CN_SET_NETMODE, CN_GET_NETMODE,
CN_SET_TIMEOUT, CN_GET_TIMEOUT, CN_SET_PREFER, CN_GET_PREFER,
};
enum sockint_kind { SIK_CONNECTED = 1, SIK_DISCONNECTED = 2, SIK_RECEIVED = 4, SIK_TIMEOUT = 8, SIK_SENT = 16, SIK_ALL = 0x1F }; enum sockint_kind { SIK_CONNECTED = 1, SIK_DISCONNECTED = 2, SIK_RECEIVED = 4, SIK_TIMEOUT = 8, SIK_SENT = 16, SIK_ALL = 0x1F };
@@ -950,101 +936,6 @@ static ipconf_mode ipmode_;
static constexpr char CHIP_ID[] = "W6300"; static constexpr char CHIP_ID[] = "W6300";
int8_t ctl_chip(chip_ctl cwtype, void* arg) {
uint8_t tmp = *(uint8_t*)arg;
uint8_t* ptmp[2] = {0, 0};
switch (cwtype) {
case CW_SYS_LOCK:
if (tmp & SYS_CHIP_LOCK) CHIPLOCK();
if (tmp & SYS_NET_LOCK) NETLOCK();
if (tmp & SYS_PHY_LOCK) PHYLOCK();
break;
case CW_SYS_UNLOCK:
if (tmp & SYS_CHIP_LOCK) CHIPUNLOCK();
if (tmp & SYS_NET_LOCK) NETUNLOCK();
if (tmp & SYS_PHY_LOCK) PHYUNLOCK();
break;
case CW_GET_SYSLOCK:
*(uint8_t*)arg = getSYSR() >> 5;
break;
case CW_RESET_WIZCHIP:
soft_reset();
break;
case CW_INIT_WIZCHIP:
if (arg) {
ptmp[0] = (uint8_t*)arg;
ptmp[1] = ptmp[0] + SOCK_COUNT;
}
return init_buffers(ptmp[0], ptmp[1]);
case CW_CLR_INTERRUPT:
clear_interrupt(*((intr_kind*)arg));
break;
case CW_GET_INTERRUPT:
*((intr_kind*)arg) = get_interrupt();
break;
case CW_SET_INTRMASK:
set_interrupt_mask(*((intr_kind*)arg));
break;
case CW_GET_INTRMASK:
*((intr_kind*)arg) = get_interrupt_mask();
break;
case CW_SET_INTRTIME:
setINTPTMR(*(uint16_t*)arg);
break;
case CW_GET_INTRTIME:
*(uint16_t*)arg = getINTPTMR();
break;
case CW_GET_ID:
memcpy(arg, CHIP_ID, sizeof(CHIP_ID));
break;
case CW_GET_VER:
*(uint16_t*)arg = getVER();
break;
case CW_RESET_PHY:
reset_phy();
break;
case CW_SET_PHYCONF:
set_phy_conf((phy_conf*)arg);
break;
case CW_GET_PHYCONF:
get_phy_conf((phy_conf*)arg);
break;
case CW_GET_PHYSTATUS:
break;
case CW_SET_PHYPOWMODE:
set_phy_power_mode(*(uint8_t*)arg);
break;
case CW_GET_PHYPOWMODE:
tmp = get_phy_power_mode();
if ((int8_t)tmp == -1) return -1;
*(uint8_t*)arg = tmp;
break;
case CW_GET_PHYLINK:
tmp = get_phy_link();
if ((int8_t)tmp == -1) return -1;
*(uint8_t*)arg = tmp;
break;
default:
return -1;
}
return 0;
}
int8_t ctl_net(net_ctl cntype, void* arg) {
switch (cntype) {
case CN_SET_NETINFO: set_net_info((net_info*)arg); break;
case CN_GET_NETINFO: get_net_info((net_info*)arg); break;
case CN_SET_NETMODE: set_net_mode(*(netmode_type*)arg); break;
case CN_GET_NETMODE: *(netmode_type*)arg = get_net_mode(); break;
case CN_SET_TIMEOUT: set_timeout((net_timeout*)arg); break;
case CN_GET_TIMEOUT: get_timeout((net_timeout*)arg); break;
case CN_SET_PREFER: setSLPSR(*(uint8_t*)arg); break;
case CN_GET_PREFER: *(uint8_t*)arg = getSLPSR(); break;
default: return -1;
}
return 0;
}
void soft_reset() { void soft_reset() {
uint8_t gw[4], sn[4], sip[4], mac[6]; uint8_t gw[4], sn[4], sip[4], mac[6];
@@ -1065,9 +956,9 @@ void soft_reset() {
if (islock & SYSR_NETL) NETLOCK(); if (islock & SYSR_NETL) NETLOCK();
} }
int8_t init_buffers(uint8_t* txsize, uint8_t* rxsize) { int8_t init_buffers(std::span<const uint8_t> txsize, std::span<const uint8_t> rxsize) {
soft_reset(); soft_reset();
if (txsize) { if (!txsize.empty()) {
int8_t tmp = 0; int8_t tmp = 0;
for (int i = 0; i < SOCK_COUNT; i++) { for (int i = 0; i < SOCK_COUNT; i++) {
tmp += txsize[i]; tmp += txsize[i];
@@ -1075,7 +966,7 @@ int8_t init_buffers(uint8_t* txsize, uint8_t* rxsize) {
} }
for (int i = 0; i < SOCK_COUNT; i++) setSn_TXBUF_SIZE(i, txsize[i]); for (int i = 0; i < SOCK_COUNT; i++) setSn_TXBUF_SIZE(i, txsize[i]);
} }
if (rxsize) { if (!rxsize.empty()) {
int8_t tmp = 0; int8_t tmp = 0;
for (int i = 0; i < SOCK_COUNT; i++) { for (int i = 0; i < SOCK_COUNT; i++) {
tmp += rxsize[i]; tmp += rxsize[i];
@@ -1129,36 +1020,40 @@ void reset_phy() {
while (mdio_read(PHYRAR_BMCR) & BMCR_RST); while (mdio_read(PHYRAR_BMCR) & BMCR_RST);
} }
void set_phy_conf(phy_conf* phyconf) { void set_phy_conf(const phy_conf& phyconf) {
uint16_t tmp = mdio_read(PHYRAR_BMCR); uint16_t tmp = mdio_read(PHYRAR_BMCR);
if (phyconf->mode == PHY_MODE_TE) { if (phyconf.mode == PHY_MODE_TE) {
setPHYCR1(getPHYCR1() | PHYCR1_TE); setPHYCR1(getPHYCR1() | PHYCR1_TE);
setPHYCR0(PHYCR0_AUTO); setPHYCR0(PHYCR0_AUTO);
} else { } else {
setPHYCR1(getPHYCR1() & ~PHYCR1_TE); setPHYCR1(getPHYCR1() & ~PHYCR1_TE);
if (phyconf->mode == PHY_MODE_AUTONEGO) { if (phyconf.mode == PHY_MODE_AUTONEGO) {
tmp |= BMCR_ANE; tmp |= BMCR_ANE;
} else { } else {
tmp &= ~(BMCR_ANE | BMCR_DPX | BMCR_SPD); tmp &= ~(BMCR_ANE | BMCR_DPX | BMCR_SPD);
if (phyconf->duplex == PHY_DUPLEX_FULL) tmp |= BMCR_DPX; if (phyconf.duplex == PHY_DUPLEX_FULL) tmp |= BMCR_DPX;
if (phyconf->speed == PHY_SPEED_100) tmp |= BMCR_SPD; if (phyconf.speed == PHY_SPEED_100) tmp |= BMCR_SPD;
} }
mdio_write(PHYRAR_BMCR, tmp); mdio_write(PHYRAR_BMCR, tmp);
} }
} }
void get_phy_conf(phy_conf* phyconf) { phy_conf get_phy_conf() {
phy_conf result;
uint16_t tmp = mdio_read(PHYRAR_BMCR); uint16_t tmp = mdio_read(PHYRAR_BMCR);
phyconf->mode = (getPHYCR1() & PHYCR1_TE) ? PHY_MODE_TE : ((tmp & BMCR_ANE) ? PHY_MODE_AUTONEGO : PHY_MODE_MANUAL); result.mode = (getPHYCR1() & PHYCR1_TE) ? PHY_MODE_TE : ((tmp & BMCR_ANE) ? PHY_MODE_AUTONEGO : PHY_MODE_MANUAL);
phyconf->duplex = (tmp & BMCR_DPX) ? PHY_DUPLEX_FULL : PHY_DUPLEX_HALF; result.duplex = (tmp & BMCR_DPX) ? PHY_DUPLEX_FULL : PHY_DUPLEX_HALF;
phyconf->speed = (tmp & BMCR_SPD) ? PHY_SPEED_100 : PHY_SPEED_10; result.speed = (tmp & BMCR_SPD) ? PHY_SPEED_100 : PHY_SPEED_10;
return result;
} }
void get_phy_status(phy_conf* phyconf) { phy_conf get_phy_status() {
phy_conf result;
uint8_t tmp = getPHYSR(); uint8_t tmp = getPHYSR();
phyconf->mode = (getPHYCR1() & PHYCR1_TE) ? PHY_MODE_TE : ((tmp & (1 << 5)) ? PHY_MODE_MANUAL : PHY_MODE_AUTONEGO); result.mode = (getPHYCR1() & PHYCR1_TE) ? PHY_MODE_TE : ((tmp & (1 << 5)) ? PHY_MODE_MANUAL : PHY_MODE_AUTONEGO);
phyconf->speed = (tmp & PHYSR_SPD) ? PHY_SPEED_10 : PHY_SPEED_100; result.speed = (tmp & PHYSR_SPD) ? PHY_SPEED_10 : PHY_SPEED_100;
phyconf->duplex = (tmp & PHYSR_DPX) ? PHY_DUPLEX_HALF : PHY_DUPLEX_FULL; result.duplex = (tmp & PHYSR_DPX) ? PHY_DUPLEX_HALF : PHY_DUPLEX_FULL;
return result;
} }
void set_phy_power_mode(uint8_t pmode) { void set_phy_power_mode(uint8_t pmode) {
@@ -1168,20 +1063,24 @@ void set_phy_power_mode(uint8_t pmode) {
mdio_write(PHYRAR_BMCR, tmp); mdio_write(PHYRAR_BMCR, tmp);
} }
void set_net_info(net_info* p) { void set_net_info(const net_info& p) {
setSHAR(p->mac.data()); setGAR(p->gw.data()); setSUBR(p->sn.data()); setSIPR(p->ip.data()); setSHAR(const_cast<uint8_t*>(p.mac.data())); setGAR(const_cast<uint8_t*>(p.gw.data()));
setGA6R(p->gw6.data()); setSUB6R(p->sn6.data()); setLLAR(p->lla.data()); setGUAR(p->gua.data()); setSUBR(const_cast<uint8_t*>(p.sn.data())); setSIPR(const_cast<uint8_t*>(p.ip.data()));
memcpy(dns_, p->dns.data(), 4); setGA6R(const_cast<uint8_t*>(p.gw6.data())); setSUB6R(const_cast<uint8_t*>(p.sn6.data()));
memcpy(dns6_, p->dns6.data(), 16); setLLAR(const_cast<uint8_t*>(p.lla.data())); setGUAR(const_cast<uint8_t*>(p.gua.data()));
ipmode_ = p->ipmode; memcpy(dns_, p.dns.data(), 4);
memcpy(dns6_, p.dns6.data(), 16);
ipmode_ = p.ipmode;
} }
void get_net_info(net_info* p) { net_info get_net_info() {
getSHAR(p->mac.data()); getGAR(p->gw.data()); getSUBR(p->sn.data()); getSIPR(p->ip.data()); net_info p = {};
getGA6R(p->gw6.data()); getSUB6R(p->sn6.data()); getLLAR(p->lla.data()); getGUAR(p->gua.data()); getSHAR(p.mac.data()); getGAR(p.gw.data()); getSUBR(p.sn.data()); getSIPR(p.ip.data());
memcpy(p->dns.data(), dns_, 4); getGA6R(p.gw6.data()); getSUB6R(p.sn6.data()); getLLAR(p.lla.data()); getGUAR(p.gua.data());
memcpy(p->dns6.data(), dns6_, 16); memcpy(p.dns.data(), dns_, 4);
p->ipmode = ipmode_; memcpy(p.dns6.data(), dns6_, 16);
p.ipmode = ipmode_;
return p;
} }
void set_net_mode(netmode_type netmode) { void set_net_mode(netmode_type netmode) {
@@ -1200,32 +1099,31 @@ netmode_type get_net_mode() {
return (netmode_type)ret; return (netmode_type)ret;
} }
void set_timeout(net_timeout* t) { void set_timeout(const net_timeout& t) {
setRCR(t->s_retry_cnt); setRTR(t->s_time_100us); setRCR(t.s_retry_cnt); setRTR(t.s_time_100us);
setSLRCR(t->sl_retry_cnt); setSLRTR(t->sl_time_100us); setSLRCR(t.sl_retry_cnt); setSLRTR(t.sl_time_100us);
} }
void get_timeout(net_timeout* t) { net_timeout get_timeout() {
t->s_retry_cnt = getRCR(); t->s_time_100us = getRTR(); return {getRCR(), getRTR(), getSLRCR(), getSLRTR()};
t->sl_retry_cnt = getSLRCR(); t->sl_time_100us = getSLRTR();
} }
int8_t send_arp(arp_request* arp) { int8_t send_arp(arp_request& arp) {
uint8_t tmp; uint8_t tmp;
if (arp->destinfo.len == 16) { setSLDIP6R(arp->destinfo.ip.data()); setSLCR(SLCR_ARP6); } if (arp.destinfo.len == 16) { setSLDIP6R(const_cast<uint8_t*>(arp.destinfo.ip.data())); setSLCR(SLCR_ARP6); }
else { setSLDIP4R(arp->destinfo.ip.data()); setSLCR(SLCR_ARP4); } else { setSLDIP4R(const_cast<uint8_t*>(arp.destinfo.ip.data())); setSLCR(SLCR_ARP4); }
while (getSLCR()); while (getSLCR());
while ((tmp = getSLIR()) == 0x00); while ((tmp = getSLIR()) == 0x00);
setSLIRCLR(~SLIR_RA); setSLIRCLR(~SLIR_RA);
if (tmp & (SLIR_ARP4 | SLIR_ARP6)) { getSLDHAR(arp->dha.data()); return 0; } if (tmp & (SLIR_ARP4 | SLIR_ARP6)) { getSLDHAR(arp.dha.data()); return 0; }
return -1; return -1;
} }
int8_t send_ping(ping_request* ping) { int8_t send_ping(const ping_request& ping) {
uint8_t tmp; uint8_t tmp;
setPINGIDR(ping->id); setPINGSEQR(ping->seq); setPINGIDR(ping.id); setPINGSEQR(ping.seq);
if (ping->destinfo.len == 16) { setSLDIP6R(ping->destinfo.ip.data()); setSLCR(SLCR_PING6); } if (ping.destinfo.len == 16) { setSLDIP6R(const_cast<uint8_t*>(ping.destinfo.ip.data())); setSLCR(SLCR_PING6); }
else { setSLDIP4R(ping->destinfo.ip.data()); setSLCR(SLCR_PING4); } else { setSLDIP4R(const_cast<uint8_t*>(ping.destinfo.ip.data())); setSLCR(SLCR_PING4); }
while (getSLCR()); while (getSLCR());
while ((tmp = getSLIR()) == 0x00); while ((tmp = getSLIR()) == 0x00);
setSLIRCLR(~SLIR_RA); setSLIRCLR(~SLIR_RA);
@@ -1233,9 +1131,9 @@ int8_t send_ping(ping_request* ping) {
return -1; return -1;
} }
int8_t send_dad(uint8_t* ipv6) { int8_t send_dad(std::span<const uint8_t, 16> ipv6) {
uint8_t tmp; uint8_t tmp;
setSLDIP6R(ipv6); setSLCR(SLCR_NS); setSLDIP6R(const_cast<uint8_t*>(ipv6.data())); setSLCR(SLCR_NS);
while (getSLCR()); while (getSLCR());
while ((tmp = getSLIR()) == 0x00); while ((tmp = getSLIR()) == 0x00);
setSLIRCLR(~SLIR_RA); setSLIRCLR(~SLIR_RA);
@@ -1243,16 +1141,16 @@ int8_t send_dad(uint8_t* ipv6) {
return -1; return -1;
} }
int8_t send_slaac(prefix* prefix) { int8_t send_slaac(prefix& pfx) {
uint8_t tmp; uint8_t tmp;
setSLCR(SLCR_RS); setSLCR(SLCR_RS);
while (getSLCR()); while (getSLCR());
while ((tmp = getSLIR()) == 0x00); while ((tmp = getSLIR()) == 0x00);
setSLIRCLR(~SLIR_RA); setSLIRCLR(~SLIR_RA);
if (tmp & SLIR_RS) { if (tmp & SLIR_RS) {
prefix->len = getPLR(); prefix->flag = getPFR(); pfx.len = getPLR(); pfx.flag = getPFR();
prefix->valid_lifetime = getVLTR(); prefix->preferred_lifetime = getPLTR(); pfx.valid_lifetime = getVLTR(); pfx.preferred_lifetime = getPLTR();
getPAR(prefix->prefix.data()); getPAR(pfx.prefix.data());
return 0; return 0;
} }
return -1; return -1;
@@ -1268,11 +1166,11 @@ int8_t send_unsolicited() {
return -1; return -1;
} }
int8_t get_prefix(prefix* prefix) { int8_t get_prefix(prefix& pfx) {
if (getSLIR() & SLIR_RA) { if (getSLIR() & SLIR_RA) {
prefix->len = getPLR(); prefix->flag = getPFR(); pfx.len = getPLR(); pfx.flag = getPFR();
prefix->valid_lifetime = getVLTR(); prefix->preferred_lifetime = getPLTR(); pfx.valid_lifetime = getVLTR(); pfx.preferred_lifetime = getPLTR();
getPAR(prefix->prefix.data()); getPAR(pfx.prefix.data());
setSLIRCLR(SLIR_RA); setSLIRCLR(SLIR_RA);
} }
return -1; return -1;
@@ -1395,26 +1293,27 @@ std::expected<void, sock_error> listen(socket_id sid) {
return {}; return {};
} }
std::expected<void, sock_error> connect(socket_id sid, uint8_t * addr, port_num port, uint8_t addrlen) { std::expected<void, sock_error> connect(socket_id sid, const ip_address& addr, port_num port) {
uint8_t sn = static_cast<uint8_t>(sid); uint8_t sn = static_cast<uint8_t>(sid);
uint16_t p = static_cast<uint16_t>(port); uint16_t p = static_cast<uint16_t>(port);
uint8_t addrlen = addr.len;
CHECK_SOCKNUM(); CHECK_SOCKNUM();
CHECK_TCPMODE(); CHECK_TCPMODE();
CHECK_SOCKINIT(); CHECK_SOCKINIT();
CHECK_IPZERO(addr, addrlen); CHECK_IPZERO(addr.ip.data(), addrlen);
if (p == 0) FAIL(port_zero); if (p == 0) FAIL(port_zero);
setSn_DPORTR(sn, p); setSn_DPORTR(sn, p);
if (addrlen == 16) { if (addrlen == 16) {
if (getSn_MR(sn) & 0x08) { if (getSn_MR(sn) & 0x08) {
setSn_DIP6R(sn, addr); setSn_DIP6R(sn, const_cast<uint8_t*>(addr.ip.data()));
setSn_CR(sn, Sn_CR_CONNECT6); setSn_CR(sn, Sn_CR_CONNECT6);
} else { } else {
FAIL(sock_mode); FAIL(sock_mode);
} }
} else { } else {
if (getSn_MR(sn) == Sn_MR_TCP6) FAIL(sock_mode); if (getSn_MR(sn) == Sn_MR_TCP6) FAIL(sock_mode);
setSn_DIPR(sn, addr); setSn_DIPR(sn, const_cast<uint8_t*>(addr.ip.data()));
setSn_CR(sn, Sn_CR_CONNECT); setSn_CR(sn, Sn_CR_CONNECT);
} }
while (getSn_CR(sn)); while (getSn_CR(sn));
@@ -1523,10 +1422,11 @@ std::expected<uint16_t, sock_error> recv(socket_id sid, std::span<uint8_t> buf)
return len; return len;
} }
std::expected<uint16_t, sock_error> sendto(socket_id sid, std::span<const uint8_t> buf, uint8_t * addr, port_num port, uint8_t addrlen) { std::expected<uint16_t, sock_error> sendto(socket_id sid, std::span<const uint8_t> buf, const ip_address& addr, port_num port) {
uint8_t sn = static_cast<uint8_t>(sid); uint8_t sn = static_cast<uint8_t>(sid);
uint16_t p = static_cast<uint16_t>(port); uint16_t p = static_cast<uint16_t>(port);
uint16_t len = buf.size(); uint16_t len = buf.size();
uint8_t addrlen = addr.len;
uint8_t tmp = 0; uint8_t tmp = 0;
uint8_t tcmd = Sn_CR_SEND; uint8_t tcmd = Sn_CR_SEND;
uint16_t freesize = 0; uint16_t freesize = 0;
@@ -1545,14 +1445,14 @@ std::expected<uint16_t, sock_error> sendto(socket_id sid, std::span<const uint8_
if (tmp != Sn_MR_MACRAW) { if (tmp != Sn_MR_MACRAW) {
if (addrlen == 16) { if (addrlen == 16) {
if (tmp & 0x08) { if (tmp & 0x08) {
setSn_DIP6R(sn, addr); setSn_DIP6R(sn, const_cast<uint8_t*>(addr.ip.data()));
tcmd = Sn_CR_SEND6; tcmd = Sn_CR_SEND6;
} else { } else {
FAIL(sock_mode); FAIL(sock_mode);
} }
} else if (addrlen == 4) { } else if (addrlen == 4) {
if (tmp == Sn_MR_UDP6 || tmp == Sn_MR_IPRAW6) FAIL(sock_mode); if (tmp == Sn_MR_UDP6 || tmp == Sn_MR_IPRAW6) FAIL(sock_mode);
setSn_DIPR(sn, addr); setSn_DIPR(sn, const_cast<uint8_t*>(addr.ip.data()));
tcmd = Sn_CR_SEND; tcmd = Sn_CR_SEND;
} else { } else {
FAIL(ip_invalid); FAIL(ip_invalid);
@@ -1590,7 +1490,7 @@ std::expected<uint16_t, sock_error> sendto(socket_id sid, std::span<const uint8_
return len; return len;
} }
std::expected<uint16_t, sock_error> recvfrom(socket_id sid, std::span<uint8_t> buf, uint8_t * addr, port_num *port, uint8_t *addrlen) { std::expected<uint16_t, sock_error> recvfrom(socket_id sid, std::span<uint8_t> buf, ip_address& addr, port_num& port) {
uint8_t sn = static_cast<uint8_t>(sid); uint8_t sn = static_cast<uint8_t>(sid);
uint16_t len = buf.size(); uint16_t len = buf.size();
uint8_t mr; uint8_t mr;
@@ -1632,11 +1532,10 @@ std::expected<uint16_t, sock_error> recvfrom(socket_id sid, std::span<uint8_t> b
case Sn_MR_UDP4: case Sn_MR_UDP4:
case Sn_MR_UDP6: case Sn_MR_UDP6:
case Sn_MR_UDPD: case Sn_MR_UDPD:
if (addr == 0) FAIL(arg);
sock_pack_info[sn] = head[0] & 0xF8; sock_pack_info[sn] = head[0] & 0xF8;
if (sock_pack_info[sn] & PACK_IPv6) *addrlen = 16; if (sock_pack_info[sn] & PACK_IPv6) addr.len = 16;
else *addrlen = 4; else addr.len = 4;
recv_data(sn, addr, *addrlen); recv_data(sn, addr.ip.data(), addr.len);
setSn_CR(sn, Sn_CR_RECV); setSn_CR(sn, Sn_CR_RECV);
while (getSn_CR(sn)); while (getSn_CR(sn));
break; break;
@@ -1657,11 +1556,10 @@ std::expected<uint16_t, sock_error> recvfrom(socket_id sid, std::span<uint8_t> b
case Sn_MR_IPRAW6: case Sn_MR_IPRAW6:
case Sn_MR_IPRAW4: case Sn_MR_IPRAW4:
if (sock_remained_size[sn] == 0) { if (sock_remained_size[sn] == 0) {
if (*addr == 0) FAIL(arg);
sock_pack_info[sn] = head[0] & 0xF8; sock_pack_info[sn] = head[0] & 0xF8;
if (sock_pack_info[sn] & PACK_IPv6) *addrlen = 16; if (sock_pack_info[sn] & PACK_IPv6) addr.len = 16;
else *addrlen = 4; else addr.len = 4;
recv_data(sn, addr, *addrlen); recv_data(sn, addr.ip.data(), addr.len);
setSn_CR(sn, Sn_CR_RECV); setSn_CR(sn, Sn_CR_RECV);
while (getSn_CR(sn)); while (getSn_CR(sn));
} }
@@ -1675,9 +1573,8 @@ std::expected<uint16_t, sock_error> recvfrom(socket_id sid, std::span<uint8_t> b
sock_remained_size[sn] = pack_len; sock_remained_size[sn] = pack_len;
sock_pack_info[sn] |= PACK_FIRST; sock_pack_info[sn] |= PACK_FIRST;
if ((getSn_MR(sn) & 0x03) == 0x02) { if ((getSn_MR(sn) & 0x03) == 0x02) {
if (port == 0) FAIL(arg);
recv_data(sn, head, 2); recv_data(sn, head, 2);
*port = static_cast<port_num>((((uint16_t)head[0]) << 8) + head[1]); port = static_cast<port_num>((((uint16_t)head[0]) << 8) + head[1]);
setSn_CR(sn, Sn_CR_RECV); setSn_CR(sn, Sn_CR_RECV);
while (getSn_CR(sn)); while (getSn_CR(sn));
} }
@@ -1736,18 +1633,19 @@ void init_critical_section() {
void init() { void init() {
pio_frame_end(); pio_frame_end();
uint8_t memsize[2][8] = {{4, 4, 4, 4, 4, 4, 4, 4}, {4, 4, 4, 4, 4, 4, 4, 4}}; std::array<uint8_t, 8> txsize = {4, 4, 4, 4, 4, 4, 4, 4};
ctl_chip(CW_INIT_WIZCHIP, (void *)memsize); std::array<uint8_t, 8> rxsize = {4, 4, 4, 4, 4, 4, 4, 4};
init_buffers(txsize, rxsize);
} }
bool check() { bool check() {
return getCIDR() == 0x6300; return getCIDR() == 0x6300;
} }
void init_net(net_info net_info) { void init_net(const net_info& info) {
uint8_t syslock = SYS_NET_LOCK; CHIPUNLOCK();
ctl_chip(CW_SYS_UNLOCK, &syslock); NETUNLOCK();
ctl_net(CN_SET_NETINFO, (void *)&net_info); set_net_info(info);
} }
std::expected<void, sock_error> set_socket_io_mode(socket_id sid, sock_io_mode mode) { std::expected<void, sock_error> set_socket_io_mode(socket_id sid, sock_io_mode mode) {
+22 -22
View File
@@ -188,36 +188,36 @@ void init_critical_section();
void reset(); void reset();
void init(); void init();
bool check(); bool check();
void init_net(net_info info); void init_net(const net_info& info);
void soft_reset(); void soft_reset();
int8_t init_buffers(uint8_t* txsize, uint8_t* rxsize); int8_t init_buffers(std::span<const uint8_t> txsize, std::span<const uint8_t> rxsize);
void clear_interrupt(intr_kind intr); void clear_interrupt(intr_kind intr);
intr_kind get_interrupt(); intr_kind get_interrupt();
void set_interrupt_mask(intr_kind intr); void set_interrupt_mask(intr_kind intr);
intr_kind get_interrupt_mask(); intr_kind get_interrupt_mask();
int8_t get_phy_link(); int8_t get_phy_link();
int8_t get_phy_power_mode(); int8_t get_phy_power_mode();
void reset_phy(); void reset_phy();
void set_phy_conf(phy_conf* conf); void set_phy_conf(const phy_conf& conf);
void get_phy_conf(phy_conf* conf); phy_conf get_phy_conf();
void get_phy_status(phy_conf* conf); phy_conf get_phy_status();
void set_phy_power_mode(uint8_t pmode); void set_phy_power_mode(uint8_t pmode);
void set_net_info(net_info* info); void set_net_info(const net_info& info);
void get_net_info(net_info* info); net_info get_net_info();
void set_net_mode(netmode_type mode); void set_net_mode(netmode_type mode);
netmode_type get_net_mode(); netmode_type get_net_mode();
void set_timeout(net_timeout* timeout); void set_timeout(const net_timeout& timeout);
void get_timeout(net_timeout* timeout); net_timeout get_timeout();
int8_t send_arp(arp_request* arp); int8_t send_arp(arp_request& arp);
int8_t send_ping(ping_request* ping); int8_t send_ping(const ping_request& ping);
int8_t send_dad(uint8_t* ipv6); int8_t send_dad(std::span<const uint8_t, 16> ipv6);
int8_t send_slaac(prefix* pfx); int8_t send_slaac(prefix& pfx);
int8_t send_unsolicited(); int8_t send_unsolicited();
int8_t get_prefix(prefix* pfx); int8_t get_prefix(prefix& pfx);
std::expected<socket_id, sock_error> open_socket(socket_id sn, protocol proto, port_num port, sock_flag flag); std::expected<socket_id, sock_error> open_socket(socket_id sn, protocol proto, port_num port, sock_flag flag);
std::expected<void, sock_error> close(socket_id sn); std::expected<void, sock_error> close(socket_id sn);
@@ -226,9 +226,9 @@ std::expected<void, sock_error> disconnect(socket_id sn);
std::expected<uint16_t, sock_error> send(socket_id sn, std::span<const uint8_t> buf); std::expected<uint16_t, sock_error> send(socket_id sn, std::span<const uint8_t> buf);
std::expected<uint16_t, sock_error> recv(socket_id sn, std::span<uint8_t> buf); std::expected<uint16_t, sock_error> recv(socket_id sn, std::span<uint8_t> buf);
std::expected<void, sock_error> connect(socket_id sn, uint8_t* addr, port_num port, uint8_t addrlen); std::expected<void, sock_error> connect(socket_id sn, const ip_address& addr, port_num port);
std::expected<uint16_t, sock_error> sendto(socket_id sn, std::span<const uint8_t> buf, uint8_t* addr, port_num port, uint8_t addrlen); std::expected<uint16_t, sock_error> sendto(socket_id sn, std::span<const uint8_t> buf, const ip_address& addr, port_num port);
std::expected<uint16_t, sock_error> recvfrom(socket_id sn, std::span<uint8_t> buf, uint8_t* addr, port_num* port, uint8_t* addrlen); std::expected<uint16_t, sock_error> recvfrom(socket_id sn, std::span<uint8_t> buf, ip_address& addr, port_num& port);
std::expected<void, sock_error> set_socket_io_mode(socket_id sn, sock_io_mode mode); std::expected<void, sock_error> set_socket_io_mode(socket_id sn, sock_io_mode mode);
sock_io_mode get_socket_io_mode(socket_id sn); sock_io_mode get_socket_io_mode(socket_id sn);