diff --git a/firmware/lib/net.cpp b/firmware/lib/net.cpp index 79b6a8b..7d96d7e 100644 --- a/firmware/lib/net.cpp +++ b/firmware/lib/net.cpp @@ -108,9 +108,7 @@ static bool ip_match_or_broadcast(const ip4_addr& dst) { static void send_raw(const void* data, size_t len) { dlog_if_slow("send_raw", 1000, [&]{ - w6300::ip_address dummy = {}; - w6300::sendto(raw_socket, std::span{static_cast(data), len}, - dummy, w6300::port_num{0}); + w6300::send(raw_socket, std::span{static_cast(data), len}); }); } @@ -280,7 +278,7 @@ bool net_init() { state.ip[2] = state.mac[4]; state.ip[3] = state.mac[5]; - w6300::open_socket(raw_socket, w6300::protocol::macraw, w6300::port_num{0}, w6300::sock_flag::none); + w6300::open_socket(raw_socket, w6300::protocol::macraw, w6300::sock_flag::none); w6300::set_socket_io_mode(raw_socket, w6300::sock_io_mode::nonblock); w6300::set_interrupt_mask(w6300::ik_sock_0); @@ -299,9 +297,7 @@ void net_poll() { w6300::clear_interrupt(w6300::ik_int_all); if (w6300::get_socket_recv_buf(raw_socket) == 0) return; static uint8_t rx_buf[1518]; - w6300::ip_address dummy_addr = {}; - w6300::port_num dummy_port{0}; - auto result = w6300::recvfrom(raw_socket, std::span{rx_buf}, dummy_addr, dummy_port); + auto result = w6300::recv(raw_socket, std::span{rx_buf}); if (!result) return; process_frame(rx_buf, *result); } diff --git a/firmware/test.cpp b/firmware/test.cpp index a45921a..a564458 100644 --- a/firmware/test.cpp +++ b/firmware/test.cpp @@ -29,44 +29,36 @@ 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{req}, dest, - w6300::port_num{PICOMAP_DISCOVERY_PORT}); + auto send_result = w6300::send(test_socket, std::span{req}); if (!send_result) { resp.pass = false; - resp.messages.push_back("sendto: error " + std::to_string(static_cast(send_result.error()))); + resp.messages.push_back("send: error " + std::to_string(static_cast(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 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); + 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("recvfrom: timed out after 5s"); + resp.messages.push_back("recv: timed out after 5s"); } else { - resp.messages.push_back("recvfrom: error " + std::to_string(static_cast(recv_result.error()))); + resp.messages.push_back("recv: error " + std::to_string(static_cast(recv_result.error()))); } return resp; } - resp.messages.push_back("received " + std::to_string(*recv_result) + " bytes from port " + - std::to_string(static_cast(src_port))); + resp.messages.push_back("received " + std::to_string(*recv_result) + " bytes"); auto info = decode_response(rx_buf, *recv_result); if (!info) { diff --git a/firmware/w6300/w6300.cpp b/firmware/w6300/w6300.cpp index 5ec661d..8c706cb 100644 --- a/firmware/w6300/w6300.cpp +++ b/firmware/w6300/w6300.cpp @@ -236,7 +236,6 @@ constexpr uint8_t PACK_NONE = 0x00; constexpr uint8_t PACK_FIRST = 1 << 1; constexpr uint8_t PACK_REMAINED = 1 << 2; constexpr uint8_t PACK_COMPLETED = 1 << 3; -constexpr uint8_t PACK_IPv6 = 1 << 7; constexpr uint8_t SPI_READ = (0x00 << 5); constexpr uint8_t SPI_WRITE = (0x01 << 5); @@ -273,11 +272,7 @@ constexpr uint32_t REG_SN_CR(uint8_t n) { return (0x0010 << 8) + SREG_BLOCK constexpr uint32_t REG_SN_IR(uint8_t n) { return (0x0020 << 8) + SREG_BLOCK(n); } constexpr uint32_t REG_SN_IRCLR(uint8_t n) { return (0x0028 << 8) + SREG_BLOCK(n); } constexpr uint32_t REG_SN_SR(uint8_t n) { return (0x0030 << 8) + SREG_BLOCK(n); } -constexpr uint32_t REG_SN_PORTR(uint8_t n) { return (0x0114 << 8) + SREG_BLOCK(n); } constexpr uint32_t REG_SN_DHAR(uint8_t n) { return (0x0118 << 8) + SREG_BLOCK(n); } -constexpr uint32_t REG_SN_DIPR(uint8_t n) { return (0x0120 << 8) + SREG_BLOCK(n); } -constexpr uint32_t REG_SN_DIP6R(uint8_t n) { return (0x0130 << 8) + SREG_BLOCK(n); } -constexpr uint32_t REG_SN_DPORTR(uint8_t n) { return (0x0140 << 8) + SREG_BLOCK(n); } constexpr uint32_t REG_SN_MR2(uint8_t n) { return (0x0144 << 8) + SREG_BLOCK(n); } constexpr uint32_t REG_SN_TX_BSR(uint8_t n) { return (0x0200 << 8) + SREG_BLOCK(n); } constexpr uint32_t REG_SN_TX_FSR(uint8_t n) { return (0x0204 << 8) + SREG_BLOCK(n); } @@ -289,26 +284,10 @@ constexpr uint32_t REG_SN_RX_RD(uint8_t n) { return (0x0228 << 8) + SREG_BLOCK constexpr uint8_t SYSR_CHPL = 1 << 7; constexpr uint8_t SYSR_NETL = 1 << 6; constexpr uint8_t SYCR0_RST = 0x00; -constexpr uint8_t SN_MR_MULTI = 1 << 7; -constexpr uint8_t SN_MR_UNIB = 1 << 4; -constexpr uint8_t SN_MR_TCP = 0x01; -constexpr uint8_t SN_MR_TCP4 = SN_MR_TCP; -constexpr uint8_t SN_MR_UDP = 0x02; -constexpr uint8_t SN_MR_UDP4 = SN_MR_UDP; -constexpr uint8_t SN_MR_IPRAW = 0x03; -constexpr uint8_t SN_MR_IPRAW4 = SN_MR_IPRAW; constexpr uint8_t SN_MR_MACRAW = 0x07; -constexpr uint8_t SN_MR_TCP6 = 0x09; -constexpr uint8_t SN_MR_UDP6 = 0x0A; -constexpr uint8_t SN_MR_IPRAW6 = 0x0B; -constexpr uint8_t SN_MR_TCPD = 0x0D; -constexpr uint8_t SN_MR_UDPD = 0x0E; -constexpr uint8_t SN_MR2_DHAM = 1 << 1; -constexpr uint8_t SN_MR2_FARP = 1 << 0; constexpr uint8_t SN_CR_OPEN = 0x01; constexpr uint8_t SN_CR_CLOSE = 0x10; constexpr uint8_t SN_CR_SEND = 0x20; -constexpr uint8_t SN_CR_SEND6 = 0xA0; constexpr uint8_t SN_CR_RECV = 0x40; constexpr uint8_t SN_IR_SENDOK = 0x10; constexpr uint8_t SN_IR_TIMEOUT = 0x08; @@ -354,24 +333,13 @@ void net_lock() { set_netlckr(0xC5); } void net_unlock() { set_netlckr(0x3A); } void set_sn_mr(uint8_t sn, uint8_t v) { reg_write(REG_SN_MR(sn), v); } -uint8_t get_sn_mr(uint8_t sn) { return reg_read(REG_SN_MR(sn)); } void set_sn_cr(uint8_t sn, uint8_t v) { reg_write(REG_SN_CR(sn), v); } uint8_t get_sn_cr(uint8_t sn) { return reg_read(REG_SN_CR(sn)); } uint8_t get_sn_ir(uint8_t sn) { return reg_read(REG_SN_IR(sn)); } void set_sn_irclr(uint8_t sn, uint8_t v) { reg_write(REG_SN_IRCLR(sn), v); } void set_sn_ir(uint8_t sn, uint8_t v) { set_sn_irclr(sn, v); } uint8_t get_sn_sr(uint8_t sn) { return reg_read(REG_SN_SR(sn)); } -void set_sn_portr(uint8_t sn, uint16_t v) { - reg_write(REG_SN_PORTR(sn), (uint8_t)(v >> 8)); - reg_write(offset_inc(REG_SN_PORTR(sn), 1), (uint8_t)v); -} void set_sn_dhar(uint8_t sn, uint8_t* v) { reg_write_buf(REG_SN_DHAR(sn), v, 6); } -void set_sn_dipr(uint8_t sn, uint8_t* v) { reg_write_buf(REG_SN_DIPR(sn), v, 4); } -void set_sn_dip6r(uint8_t sn, uint8_t* v) { reg_write_buf(REG_SN_DIP6R(sn), v, 16); } -void set_sn_dportr(uint8_t sn, uint16_t v) { - reg_write(REG_SN_DPORTR(sn), (uint8_t)(v >> 8)); - reg_write(offset_inc(REG_SN_DPORTR(sn), 1), (uint8_t)v); -} void set_sn_mr2(uint8_t sn, uint8_t v) { reg_write(REG_SN_MR2(sn), v); } void set_sn_tx_bsr(uint8_t sn, uint8_t v) { reg_write(REG_SN_TX_BSR(sn), v); } void set_sn_txbuf_size(uint8_t sn, uint8_t v) { set_sn_tx_bsr(sn, v); } @@ -460,11 +428,6 @@ void recv_data(uint8_t sn, uint8_t *data, uint16_t len) { set_sn_rx_rd(sn, ptr); } -void recv_ignore(uint8_t sn, uint16_t len) { - set_sn_rx_rd(sn, get_sn_rx_rd(sn) + len); -} - - void soft_reset() { uint8_t gw[4], sn[4], sip[4], mac[6]; uint8_t gw6[16], sn6[16], lla[16], gua[16]; @@ -505,9 +468,6 @@ int8_t init_buffers(std::span txsize, std::span rx return 0; } -constexpr uint16_t SOCK_ANY_PORT_NUM = 0xC000; - -uint16_t sock_any_port = SOCK_ANY_PORT_NUM; uint16_t sock_io_mode_bits = 0; uint16_t sock_is_sending = 0; uint16_t sock_remained_size[sock_count] = {0,}; @@ -550,66 +510,15 @@ void set_interrupt_mask(intr_kind intr) { } -std::expected open_socket(socket_id sid, protocol proto, port_num port, sock_flag flag) { +std::expected open_socket(socket_id sid, protocol proto, sock_flag flag) { uint8_t sn = static_cast(sid); - uint16_t p = static_cast(port); uint8_t pr = static_cast(proto); uint8_t fl = static_cast(flag); - uint8_t taddr[16]; CHECK_SOCKNUM(); - switch (pr & 0x0F) { - case SN_MR_TCP4: - get_sipr(taddr); - CHECK_IPZERO(taddr, 4); - break; - case SN_MR_TCP6: - get_llar(taddr); - CHECK_IPZERO(taddr, 16); - break; - case SN_MR_TCPD: - get_sipr(taddr); - CHECK_IPZERO(taddr, 4); - get_llar(taddr); - CHECK_IPZERO(taddr, 16); - break; - case SN_MR_UDP: - case SN_MR_UDP6: - case SN_MR_UDPD: - case SN_MR_MACRAW: - case SN_MR_IPRAW4: - case SN_MR_IPRAW6: - break; - default: - FAIL(sock_mode); - } - if ((fl & 0x04) != 0) FAIL(sock_flag); - - if (fl != 0) { - switch (pr) { - case SN_MR_MACRAW: - if ((fl & (SN_MR2_DHAM | SN_MR2_FARP)) != 0) FAIL(sock_flag); - break; - case SN_MR_TCP4: - case SN_MR_TCP6: - case SN_MR_TCPD: - if ((fl & (SN_MR_MULTI | SN_MR_UNIB)) != 0) FAIL(sock_flag); - break; - case SN_MR_IPRAW4: - case SN_MR_IPRAW6: - if (fl != 0) FAIL(sock_flag); - break; - default: - break; - } - } + if ((pr & 0x0F) != SN_MR_MACRAW) FAIL(sock_mode); close(sid); set_sn_mr(sn, (pr | (fl & 0xF0))); set_sn_mr2(sn, fl & 0x03); - if (!p) { - p = sock_any_port++; - if (sock_any_port == 0xFFF0) sock_any_port = SOCK_ANY_PORT_NUM; - } - set_sn_portr(sn, p); set_sn_cr(sn, SN_CR_OPEN); while (get_sn_cr(sn)); sock_io_mode_bits &= ~(1 << sn); @@ -621,49 +530,13 @@ std::expected open_socket(socket_id sid, protocol proto, return sid; } -std::expected sendto(socket_id sid, std::span buf, const ip_address& addr, port_num port) { +std::expected send(socket_id sid, std::span buf) { uint8_t sn = static_cast(sid); - uint16_t p = static_cast(port); uint16_t len = buf.size(); - uint8_t addrlen = addr.len; uint8_t tmp = 0; - uint8_t tcmd = SN_CR_SEND; uint16_t freesize = 0; CHECK_SOCKNUM(); - switch (get_sn_mr(sn) & 0x0F) { - case SN_MR_UDP: - case SN_MR_MACRAW: - case SN_MR_IPRAW: - case SN_MR_IPRAW6: - break; - default: - FAIL(sock_mode); - } - tmp = get_sn_mr(sn); - if (tmp != SN_MR_MACRAW) { - if (addrlen == 16) { - if (tmp & 0x08) { - set_sn_dip6r(sn, const_cast(addr.ip.data())); - tcmd = SN_CR_SEND6; - } else { - FAIL(sock_mode); - } - } else if (addrlen == 4) { - if (tmp == SN_MR_UDP6 || tmp == SN_MR_IPRAW6) FAIL(sock_mode); - set_sn_dipr(sn, const_cast(addr.ip.data())); - tcmd = SN_CR_SEND; - } else { - FAIL(ip_invalid); - } - } - if ((tmp & 0x03) == 0x02) { - if (p) { - set_sn_dportr(sn, p); - } else { - FAIL(port_zero); - } - } freesize = get_sn_tx_max(sn); if (len > freesize) len = freesize; @@ -674,7 +547,7 @@ std::expected sendto(socket_id sid, std::span(buf.data()), len); - set_sn_cr(sn, tcmd); + set_sn_cr(sn, SN_CR_SEND); while (get_sn_cr(sn)); while (1) { tmp = get_sn_ir(sn); @@ -689,26 +562,15 @@ std::expected sendto(socket_id sid, std::span recvfrom(socket_id sid, std::span buf, ip_address& addr, port_num& port) { +std::expected recv(socket_id sid, std::span buf) { uint8_t sn = static_cast(sid); uint16_t len = buf.size(); - uint8_t mr; - uint8_t head[8]; + uint8_t head[2]; uint16_t pack_len = 0; CHECK_SOCKNUM(); CHECK_SOCKDATA(); - switch ((mr = get_sn_mr(sn)) & 0x0F) { - case SN_MR_UDP: - case SN_MR_IPRAW: - case SN_MR_IPRAW6: - case SN_MR_MACRAW: - break; - default: - FAIL(sock_mode); - } - if (sock_remained_size[sn] == 0) { while (1) { pack_len = get_sn_rx_rsr(sn); @@ -724,59 +586,22 @@ std::expected recvfrom(socket_id sid, std::span b recv_data(sn, head, 2); set_sn_cr(sn, SN_CR_RECV); while (get_sn_cr(sn)); - pack_len = head[0] & 0x07; - pack_len = (pack_len << 8) + head[1]; - switch (mr & 0x07) { - case SN_MR_UDP4: - case SN_MR_UDP6: - case SN_MR_UDPD: - sock_pack_info[sn] = head[0] & 0xF8; - if (sock_pack_info[sn] & PACK_IPv6) addr.len = 16; - else addr.len = 4; - recv_data(sn, addr.ip.data(), addr.len); - set_sn_cr(sn, SN_CR_RECV); - while (get_sn_cr(sn)); - break; - case SN_MR_MACRAW: - if (sock_remained_size[sn] == 0) { - sock_remained_size[sn] = head[0]; - sock_remained_size[sn] = (sock_remained_size[sn] << 8) + head[1] - 2; - if (sock_remained_size[sn] > 1514) { - close(sid); - FAIL(fatal_packlen); - } - sock_pack_info[sn] = PACK_FIRST; + if (sock_remained_size[sn] == 0) { + sock_remained_size[sn] = head[0]; + sock_remained_size[sn] = (sock_remained_size[sn] << 8) + head[1] - 2; + if (sock_remained_size[sn] > 1514) { + close(sid); + FAIL(fatal_packlen); } - if (len < sock_remained_size[sn]) pack_len = len; - else pack_len = sock_remained_size[sn]; - recv_data(sn, buf.data(), pack_len); - break; - case SN_MR_IPRAW6: - case SN_MR_IPRAW4: - if (sock_remained_size[sn] == 0) { - sock_pack_info[sn] = head[0] & 0xF8; - if (sock_pack_info[sn] & PACK_IPv6) addr.len = 16; - else addr.len = 4; - recv_data(sn, addr.ip.data(), addr.len); - set_sn_cr(sn, SN_CR_RECV); - while (get_sn_cr(sn)); - } - break; - default: - recv_ignore(sn, pack_len); - sock_remained_size[sn] = pack_len; - break; + sock_pack_info[sn] = PACK_FIRST; } + if (len < sock_remained_size[sn]) pack_len = len; + else pack_len = sock_remained_size[sn]; + recv_data(sn, buf.data(), pack_len); sock_remained_size[sn] = pack_len; sock_pack_info[sn] |= PACK_FIRST; - if ((get_sn_mr(sn) & 0x03) == 0x02) { - recv_data(sn, head, 2); - port = static_cast((((uint16_t)head[0]) << 8) + head[1]); - set_sn_cr(sn, SN_CR_RECV); - while (get_sn_cr(sn)); - } if (len < sock_remained_size[sn]) pack_len = len; else pack_len = sock_remained_size[sn]; diff --git a/firmware/w6300/w6300.h b/firmware/w6300/w6300.h index eb9d5af..60c1a0d 100644 --- a/firmware/w6300/w6300.h +++ b/firmware/w6300/w6300.h @@ -9,7 +9,6 @@ namespace w6300 { constexpr int sock_count = 8; enum class socket_id : uint8_t {}; -enum class port_num : uint16_t {}; enum class sock_error : int16_t { busy = 0, @@ -81,11 +80,6 @@ enum intr_kind : uint32_t { ik_int_all = 0x00FFFF97 }; -struct ip_address { - std::array ip; - uint8_t len; -}; - void init_spi(); void reset(); void init(); @@ -94,9 +88,9 @@ bool check(); void clear_interrupt(intr_kind intr); void set_interrupt_mask(intr_kind intr); -std::expected open_socket(socket_id sn, protocol proto, port_num port, sock_flag flag); -std::expected sendto(socket_id sn, std::span buf, const ip_address& addr, port_num port); -std::expected recvfrom(socket_id sn, std::span buf, ip_address& addr, port_num& port); +std::expected open_socket(socket_id sn, protocol proto, sock_flag flag); +std::expected send(socket_id sn, std::span buf); +std::expected recv(socket_id sn, std::span buf); std::expected set_socket_io_mode(socket_id sn, sock_io_mode mode); void set_socket_dest_mac(socket_id sn, const std::array& mac);