summaryrefslogtreecommitdiff
path: root/libhw_generic
diff options
context:
space:
mode:
Diffstat (limited to 'libhw_generic')
-rw-r--r--libhw_generic/CMakeLists.txt10
-rw-r--r--libhw_generic/alarmclock.c21
-rw-r--r--libhw_generic/include/libhw/generic/alarmclock.h74
-rw-r--r--libhw_generic/include/libhw/generic/io.h118
-rw-r--r--libhw_generic/include/libhw/generic/net.h181
-rw-r--r--libhw_generic/include/libhw/generic/spi.h22
-rw-r--r--libhw_generic/io.c98
-rw-r--r--libhw_generic/net.c31
l---------libhw_generic/tests/test.h1
-rw-r--r--libhw_generic/tests/test_io.c57
10 files changed, 432 insertions, 181 deletions
diff --git a/libhw_generic/CMakeLists.txt b/libhw_generic/CMakeLists.txt
index 9c88937..603f30b 100644
--- a/libhw_generic/CMakeLists.txt
+++ b/libhw_generic/CMakeLists.txt
@@ -1,15 +1,19 @@
# libhw_generic/CMakeLists.txt - UAPI device interfaces
#
-# Copyright (C) 2024 Luke T. Shumaker <lukeshu@lukeshu.com>
+# Copyright (C) 2024-2025 Luke T. Shumaker <lukeshu@lukeshu.com>
# SPDX-License-Identifier: AGPL-3.0-or-later
add_library(libhw_generic INTERFACE)
-target_include_directories(libhw_generic SYSTEM INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/include)
+target_include_directories(libhw_generic PUBLIC INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/include)
target_link_libraries(libhw_generic INTERFACE
libmisc
- libcr
+ libobj
)
target_sources(libhw_generic INTERFACE
alarmclock.c
+ io.c
+ net.c
)
+
+add_lib_test(libhw_generic test_io)
diff --git a/libhw_generic/alarmclock.c b/libhw_generic/alarmclock.c
index a16f2f6..31fbbaf 100644
--- a/libhw_generic/alarmclock.c
+++ b/libhw_generic/alarmclock.c
@@ -1,24 +1,9 @@
-/* libhw_generic/alarmclock.c - Device-independent <libhw/generic/alarmclock.h> utilities
+/* libhw_generic/alarmclock.c - Device-independent <libhw/generic/alarmclock.h> storage
*
- * Copyright (C) 2024 Luke T. Shumaker <lukeshu@lukeshu.com>
+ * Copyright (C) 2024-2025 Luke T. Shumaker <lukeshu@lukeshu.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
-#include <libcr/coroutine.h>
-#include <libmisc/vcall.h>
-
#include <libhw/generic/alarmclock.h>
-static void alarmclock_sleep_intrhandler(void *_arg) {
- cid_t cid = *(cid_t *)_arg;
- cr_unpause_from_intrhandler(cid);
-}
-
-void alarmclock_sleep_until_ns(implements_alarmclock *clock, uint64_t abstime_ns) {
- bool saved = cr_save_and_disable_interrupts();
- cid_t cid = cr_getcid();
- struct alarmclock_trigger trigger;
- VCALL(clock, add_trigger, &trigger, abstime_ns, alarmclock_sleep_intrhandler, &cid);
- cr_pause_and_yield();
- cr_restore_interrupts(saved);
-}
+lo_interface alarmclock bootclock = {0};
diff --git a/libhw_generic/include/libhw/generic/alarmclock.h b/libhw_generic/include/libhw/generic/alarmclock.h
index a9d816b..02789f5 100644
--- a/libhw_generic/include/libhw/generic/alarmclock.h
+++ b/libhw_generic/include/libhw/generic/alarmclock.h
@@ -1,6 +1,6 @@
/* libhw/generic/alarmclock.h - Device-independent alarmclock definitions
*
- * Copyright (C) 2024 Luke T. Shumaker <lukeshu@lukeshu.com>
+ * Copyright (C) 2024-2025 Luke T. Shumaker <lukeshu@lukeshu.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
@@ -11,7 +11,7 @@
#include <stdint.h> /* for uint{n}_t and UINT{n}_C */
#include <libmisc/private.h>
-#include <libmisc/vcall.h>
+#include <libobj/obj.h>
/* Useful constants ***********************************************************/
@@ -23,71 +23,65 @@
struct alarmclock_trigger;
struct alarmclock_trigger {
- BEGIN_PRIVATE(LIBHW_GENERIC_ALARMCLOCK_H)
+ BEGIN_PRIVATE(LIBHW_GENERIC_ALARMCLOCK_H);
void *alarmclock;
struct alarmclock_trigger *prev, *next;
uint64_t fire_at_ns;
void (*cb)(void *);
void *cb_arg;
- END_PRIVATE(LIBHW_GENERIC_ALARMCLOCK_H)
+ END_PRIVATE(LIBHW_GENERIC_ALARMCLOCK_H);
};
/* Interface ******************************************************************/
-struct alarmclock_vtable;
-
-typedef struct {
- struct alarmclock_vtable *vtable;
-} implements_alarmclock;
-
-struct alarmclock_vtable {
- /**
- * (2⁶⁴-1 nanoseconds is more than 500 years; there is little
- * risk of this overflowing)
- */
- uint64_t (*get_time_ns)(implements_alarmclock *self);
-
- /**
- * Returns true on error.
- *
- * Implementations may return an error if fire_at_ns is more
- * than UINT32_MAX µs (72 minutes) in the future.
- *
- * If fire_at_ns is in the past, then it will fire
- * immediately.
- */
- bool (*add_trigger)(implements_alarmclock *self, struct alarmclock_trigger *trigger,
- uint64_t fire_at_ns,
- void (*cb)(void *),
- void *cb_arg);
-
- void (*del_trigger)(implements_alarmclock *self, struct alarmclock_trigger *trigger);
-};
+#define alarmclock_LO_IFACE \
+ /** \
+ * (2⁶⁴-1 nanoseconds is more than 500 years; there is little \
+ * risk of this overflowing) \
+ */ \
+ LO_FUNC(uint64_t, get_time_ns) \
+ \
+ /** \
+ * Returns true on error. \
+ * \
+ * Implementations may return an error if fire_at_ns is more \
+ * than UINT32_MAX µs (72 minutes) in the future. \
+ * \
+ * If fire_at_ns is in the past, then it will fire \
+ * immediately. \
+ */ \
+ LO_FUNC(bool, add_trigger, struct alarmclock_trigger *trigger, \
+ uint64_t fire_at_ns, \
+ void (*cb)(void *), \
+ void *cb_arg) \
+ \
+ LO_FUNC(void, del_trigger, struct alarmclock_trigger *trigger)
+LO_INTERFACE(alarmclock);
/* Utilities ******************************************************************/
-void alarmclock_sleep_until_ns(implements_alarmclock *clock, uint64_t abstime_ns);
+void alarmclock_sleep_until_ns(lo_interface alarmclock clock, uint64_t abstime_ns);
-static inline void alarmclock_sleep_for_ns(implements_alarmclock *clock, uint64_t delta_ns) {
- alarmclock_sleep_until_ns(clock, VCALL(clock, get_time_ns) + delta_ns);
+static inline void alarmclock_sleep_for_ns(lo_interface alarmclock clock, uint64_t delta_ns) {
+ alarmclock_sleep_until_ns(clock, LO_CALL(clock, get_time_ns) + delta_ns);
}
-static inline void alarmclock_sleep_for_us(implements_alarmclock *clock, uint64_t delta_us) {
+static inline void alarmclock_sleep_for_us(lo_interface alarmclock clock, uint64_t delta_us) {
alarmclock_sleep_for_ns(clock, delta_us * (NS_PER_S/US_PER_S));
}
-static inline void alarmclock_sleep_for_ms(implements_alarmclock *clock, uint64_t delta_ms) {
+static inline void alarmclock_sleep_for_ms(lo_interface alarmclock clock, uint64_t delta_ms) {
alarmclock_sleep_for_ns(clock, delta_ms * (NS_PER_S/MS_PER_S));
}
-static inline void alarmclock_sleep_for_s(implements_alarmclock *clock, uint64_t delta_s) {
+static inline void alarmclock_sleep_for_s(lo_interface alarmclock clock, uint64_t delta_s) {
alarmclock_sleep_for_ns(clock, delta_s * NS_PER_S);
}
/* Globals ********************************************************************/
-extern implements_alarmclock *bootclock;
+extern lo_interface alarmclock bootclock;
#define sleep_until_ns(t) alarmclock_sleep_until_ns(bootclock, t)
#define sleep_for_ns(t) alarmclock_sleep_for_ns(bootclock, t)
diff --git a/libhw_generic/include/libhw/generic/io.h b/libhw_generic/include/libhw/generic/io.h
new file mode 100644
index 0000000..62ddbb3
--- /dev/null
+++ b/libhw_generic/include/libhw/generic/io.h
@@ -0,0 +1,118 @@
+/* libhw/generic/io.h - Device-independent I/O definitions
+ *
+ * Copyright (C) 2024-2025 Luke T. Shumaker <lukeshu@lukeshu.com>
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+#ifndef _LIBHW_GENERIC_IO_H_
+#define _LIBHW_GENERIC_IO_H_
+
+#include <stddef.h> /* for size_t */
+#include <stdint.h> /* for uintptr_t */
+#include <sys/types.h> /* for ssize_t */
+
+#include <libobj/obj.h>
+
+/* structs ********************************************************************/
+
+#if __unix__
+#include <sys/uio.h>
+#else
+struct iovec {
+ void *iov_base;
+ size_t iov_len;
+};
+#endif
+
+#define IOVEC_DISCARD ((void*)(~((uintptr_t)0)))
+
+struct duplex_iovec {
+ /**
+ * NULL is a valid pointer value in iov_read_to and
+ * iov_write_from. To skip a read or write, use the special
+ * value IOVEC_DISCARD.
+ */
+ void *iov_read_to;
+ void *iov_write_from;
+ size_t iov_len;
+};
+
+/* utilities ******************************************************************/
+
+/* slice iovec lists */
+int io_slice_cnt ( const struct iovec *src, int src_cnt, size_t byte_start, size_t byte_max_cnt);
+void io_slice (struct iovec *dst, const struct iovec *src, int src_cnt, size_t byte_start, size_t byte_max_cnt);
+int io_duplex_slice_cnt( const struct duplex_iovec *src, int src_cnt, size_t byte_start, size_t byte_max_cnt);
+void io_duplex_slice (struct duplex_iovec *dst, const struct duplex_iovec *src, int src_cnt, size_t byte_start, size_t byte_max_cnt);
+
+/* convert iovec lists */
+void io_rd_to_duplex(struct duplex_iovec *dst, const struct iovec *src, int iovcnt);
+void io_wr_to_duplex(struct duplex_iovec *dst, const struct iovec *src, int iovcnt);
+
+/* slice and convert in one go */
+void io_slice_rd_to_duplex(struct duplex_iovec *dst, const struct iovec *src, int src_cnt, size_t byte_start, size_t byte_max_cnt);
+void io_slice_wr_to_duplex(struct duplex_iovec *dst, const struct iovec *src, int src_cnt, size_t byte_start, size_t byte_max_cnt);
+
+/* basic interfaces ***********************************************************/
+
+/**
+ * Return bytes-read on success, 0 on EOF, -errno on error; a short
+ * read is *not* an error.
+ */
+#define io_reader_LO_IFACE \
+ LO_FUNC(ssize_t, readv, const struct iovec *iov, int iovcnt)
+LO_INTERFACE(io_reader);
+#define io_readv(r, iov, iovcnt) LO_CALL(r, readv, iov, iovcnt)
+#define io_read(r, buf, count) LO_CALL(r, readv, &((struct iovec){.iov_base = buf, .iov_len = count}), 1)
+
+/**
+ * Return `count` on success, -errno on error; a short write *is* an
+ * error.
+ *
+ * Writes are *not* guaranteed to be atomic, so if you have concurrent
+ * writers then you should arrange for a mutex to protect the writer.
+ */
+#define io_writer_LO_IFACE \
+ LO_FUNC(ssize_t, writev, const struct iovec *iov, int iovcnt)
+LO_INTERFACE(io_writer);
+#define io_writev(w, iov, iovcnt) LO_CALL(w, writev, iov, iovcnt)
+#define io_write(w, buf, count) LO_CALL(w, writev, &((struct iovec){.iov_base = buf, .iov_len = count}), 1)
+
+/**
+ * Return 0 on success, -errno on error.
+ */
+#define io_closer_LO_IFACE \
+ LO_FUNC(int, close)
+LO_INTERFACE(io_closer);
+#define io_close(c) LO_CALL(c, close)
+
+/**
+ * All methods return 0 on success, -errno on error.
+ */
+#define io_bidi_closer_LO_IFACE \
+ LO_NEST(io_closer) \
+ LO_FUNC(int, close_read) \
+ LO_FUNC(int, close_write)
+LO_INTERFACE(io_bidi_closer);
+#define io_close_read(c) LO_CALL(c, close_read)
+#define io_close_write(c) LO_CALL(c, close_write)
+
+/**
+ * Return bytes-read and bytes-written on success, -errno on error; a
+ * short read/write *is* an error.
+ */
+#define io_duplex_readwriter_LO_IFACE \
+ LO_FUNC(void, readwritev, const struct duplex_iovec *iov, int iovcnt)
+LO_INTERFACE(io_duplex_readwriter);
+
+#define io_readwritev(rw, iov, iovcnt) \
+ LO_CALL(rw, readwritev, iov, iovcnt)
+
+/* aggregate interfaces *******************************************************/
+
+#define io_readwriter_LO_IFACE \
+ LO_NEST(io_reader) \
+ LO_NEST(io_writer)
+LO_INTERFACE(io_readwriter);
+
+#endif /* _LIBHW_GENERIC_IO_H_ */
diff --git a/libhw_generic/include/libhw/generic/net.h b/libhw_generic/include/libhw/generic/net.h
index a016d51..4af574b 100644
--- a/libhw_generic/include/libhw/generic/net.h
+++ b/libhw_generic/include/libhw/generic/net.h
@@ -1,6 +1,6 @@
/* libhw/generic/net.h - Device-independent network definitions
*
- * Copyright (C) 2024 Luke T. Shumaker <lukeshu@lukeshu.com>
+ * Copyright (C) 2024-2025 Luke T. Shumaker <lukeshu@lukeshu.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
@@ -13,6 +13,8 @@
#include <stdint.h> /* for uint{n}_t} */
#include <sys/types.h> /* for ssize_t */
+#include <libhw/generic/io.h>
+
/* Errnos *********************************************************************/
#define NET_EOTHER 1
@@ -23,6 +25,8 @@
#define NET_ECLOSED 6
#define NET_EMSGSIZE 7
+const char *net_strerror(int net_errno);
+
/* Address types **************************************************************/
struct net_ip4_addr {
@@ -52,97 +56,72 @@ struct net_eth_addr {
/* Streams (e.g. TCP) *********************************************************/
-struct net_stream_listener_vtable;
-struct net_stream_conn_vtable;
-
-typedef struct {
- struct net_stream_listener_vtable *vtable;
-} implements_net_stream_listener;
-
-typedef struct {
- struct net_stream_conn_vtable *vtable;
-} implements_net_stream_conn;
-
-struct net_stream_listener_vtable {
- /**
- * It is invalid to accept() a new connection if an existing
- * connection is still open.
- */
- implements_net_stream_conn *(*accept)(implements_net_stream_listener *self);
-
- /**
- * The net_stream_conn returned from accept() may still be
- * valid after the listener is closed.
- *
- * Return 0 on success, -errno on error.
- */
- int (*close)(implements_net_stream_listener *self);
-};
-
-struct net_stream_conn_vtable {
- /**
- * Return bytes-read on success, 0 on EOF, -errno on error; a
- * short read is *not* an error.
- */
- ssize_t (*read)(implements_net_stream_conn *self,
- void *buf, size_t count);
-
- /**
- * Set a timestamp after which calls to read() will return
- * NET_ETIMEDOUT. The timestamp is in nanoseconds on the
- * system monotonic clock, which is usually (on pico-sdk and
- * on the Linux kernel) nanoseconds-since-boot.
- *
- * A zero value disables the deadline.
- *
- * (2⁶⁴-1 nanoseconds is more than 500 years; there is little
- * risk of this overflowing)
- */
- void (*set_read_deadline)(implements_net_stream_conn *self,
- uint64_t ns_since_boot);
-
- /**
- * Return `count` on success, -errno on error; a short write *is* an
- * error.
- *
- * Writes are *not* guaranteed to be atomic (as this would be
- * expensive to implement), so if you have concurrent writers then you
- * should arrange for a mutex to protect the connection.
- */
- ssize_t (*write)(implements_net_stream_conn *self,
- void *buf, size_t count);
-
- /**
- * Return 0 on success, -errno on error.
- */
- int (*close)(implements_net_stream_conn *self,
- bool rd, bool wr);
-};
+lo_interface net_stream_conn;
+
+#define net_stream_listener_LO_IFACE \
+ /** \
+ * It is invalid to accept() a new connection if an existing \
+ * connection is still open. \
+ */ \
+ LO_FUNC(lo_interface net_stream_conn, accept) \
+ \
+ /** \
+ * The net_stream_conn returned from accept() may still be \
+ * valid after the listener is closed. \
+ */ \
+ LO_NEST(io_closer)
+LO_INTERFACE(net_stream_listener);
+
+#define net_stream_conn_LO_IFACE \
+ LO_NEST(io_readwriter) \
+ LO_NEST(io_bidi_closer) \
+ \
+ /** \
+ * Set a timestamp after which calls to read() will return \
+ * NET_ETIMEDOUT. The timestamp is in nanoseconds on the \
+ * system monotonic clock, which is usually (on pico-sdk and \
+ * on the Linux kernel) nanoseconds-since-boot. \
+ * \
+ * A zero value disables the deadline. \
+ * \
+ * (2⁶⁴-1 nanoseconds is more than 500 years; there is little \
+ * risk of this overflowing) \
+ */ \
+ LO_FUNC(void, set_read_deadline, uint64_t ns_since_boot)
+LO_INTERFACE(net_stream_conn);
/* Packets (e.g. UDP) *********************************************************/
-struct net_packet_conn_vtable;
-
-typedef struct {
- struct net_packet_conn_vtable *vtable;
-} implements_net_packet_conn;
-
-struct net_packet_conn_vtable {
- ssize_t (*sendto )(implements_net_packet_conn *self,
- void *buf, size_t len,
- struct net_ip4_addr node, uint16_t port);
- /**
- * @return The full length of the message, which may be more
- * than the given `len` (as if the Linux MSG_TRUNC flag were
- * given).
- */
- ssize_t (*recvfrom)(implements_net_packet_conn *self,
- void *buf, size_t len,
- struct net_ip4_addr *ret_node, uint16_t *ret_port);
- void (*set_read_deadline)(implements_net_packet_conn *self,
- uint64_t ns_since_boot);
- int (*close )(implements_net_packet_conn *self);
-};
+#define net_packet_conn_LO_IFACE \
+ LO_FUNC(ssize_t, sendto, \
+ void *buf, size_t len, \
+ struct net_ip4_addr node, uint16_t port) \
+ \
+ /** \
+ * @return The full length of the message, which may be more \
+ * than the given `len` (as if the Linux MSG_TRUNC flag were \
+ * given). \
+ */ \
+ LO_FUNC(ssize_t, recvfrom, \
+ void *buf, size_t len, \
+ struct net_ip4_addr *ret_node, uint16_t *ret_port) \
+ \
+ /** \
+ * Set a timestamp after which calls to recvfrom() will return \
+ * NET_ETIMEDOUT. The timestamp is in nanoseconds on the \
+ * system monotonic clock, which is usually (on pico-sdk and \
+ * on the Linux kernel) nanoseconds-since-boot. \
+ * \
+ * A zero value disables the deadline. \
+ * \
+ * (2⁶⁴-1 nanoseconds is more than 500 years; there is little \
+ * risk of this overflowing) \
+ */ \
+ LO_FUNC(void, set_recv_deadline, \
+ uint64_t ns_since_boot) \
+ \
+ LO_NEST(io_closer)
+LO_INTERFACE(net_packet_conn);
/* Interfaces *****************************************************************/
@@ -152,20 +131,14 @@ struct net_iface_config {
struct net_ip4_addr subnet_mask;
};
-struct net_iface_vtable;
-
-typedef struct {
- struct net_iface_vtable *vtable;
-} implements_net_iface;
-
-struct net_iface_vtable {
- struct net_eth_addr (*hwaddr )(implements_net_iface *);
- void (*ifup )(implements_net_iface *, struct net_iface_config);
- void (*ifdown )(implements_net_iface *);
-
- implements_net_stream_listener *(*tcp_listen)(implements_net_iface *, uint16_t local_port);
- implements_net_stream_conn *(*tcp_dial )(implements_net_iface *, struct net_ip4_addr remote_node, uint16_t remote_port);
- implements_net_packet_conn *(*udp_conn )(implements_net_iface *, uint16_t local_port);
-};
+#define net_iface_LO_IFACE \
+ LO_FUNC(struct net_eth_addr , hwaddr ) \
+ LO_FUNC(void , ifup , struct net_iface_config) \
+ LO_FUNC(void , ifdown ) \
+ \
+ LO_FUNC(lo_interface net_stream_listener, tcp_listen, uint16_t local_port) \
+ LO_FUNC(lo_interface net_stream_conn , tcp_dial , struct net_ip4_addr remote_node, uint16_t remote_port) \
+ LO_FUNC(lo_interface net_packet_conn , udp_conn , uint16_t local_port)
+LO_INTERFACE(net_iface);
#endif /* _LIBHW_GENERIC_NET_H_ */
diff --git a/libhw_generic/include/libhw/generic/spi.h b/libhw_generic/include/libhw/generic/spi.h
index 2207a2c..4bbf649 100644
--- a/libhw_generic/include/libhw/generic/spi.h
+++ b/libhw_generic/include/libhw/generic/spi.h
@@ -1,6 +1,6 @@
/* libhw/generic/spi.h - Device-independent SPI definitions
*
- * Copyright (C) 2024 Luke T. Shumaker <lukeshu@lukeshu.com>
+ * Copyright (C) 2024-2025 Luke T. Shumaker <lukeshu@lukeshu.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
@@ -9,6 +9,8 @@
#include <stddef.h> /* for size_t */
+#include <libhw/generic/io.h>
+
enum spi_mode {
SPI_MODE_0 = 0, /* clk_polarity=0 (idle low), clk_phase=0 (sample on rise) */
SPI_MODE_1 = 1, /* clk_polarity=0 (idle low), clk_phase=1 (sample on fall) */
@@ -16,18 +18,6 @@ enum spi_mode {
SPI_MODE_3 = 3, /* clk_polarity=1 (idle high), clk_phase=1 (sample on fall) */
};
-struct bidi_iovec {
- void *iov_read_dst;
- void *iov_write_src;
- size_t iov_len;
-};
-
-struct spi_vtable;
-
-typedef struct {
- struct spi_vtable *vtable;
-} implements_spi;
-
/* This API assumes that an SPI frame is a multiple of 8-bits.
*
* It is my understanding that this is a common constraint of SPI
@@ -40,8 +30,8 @@ typedef struct {
* octets; so we have no need for an API that allows a
* non-multiple-of-8 number of bits.
*/
-struct spi_vtable {
- void (*readwritev)(implements_spi *, const struct bidi_iovec *iov, int iovcnt);
-};
+#define spi_LO_IFACE \
+ LO_NEST(io_duplex_readwriter)
+LO_INTERFACE(spi);
#endif /* _LIBHW_GENERIC_SPI_H_ */
diff --git a/libhw_generic/io.c b/libhw_generic/io.c
new file mode 100644
index 0000000..4ebff10
--- /dev/null
+++ b/libhw_generic/io.c
@@ -0,0 +1,98 @@
+/* libhw_generic/io.c - Utilities for device-independent I/O definitions
+ *
+ * Copyright (C) 2025 Luke T. Shumaker <lukeshu@lukeshu.com>
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+#include <libmisc/assert.h>
+
+#include <libhw/generic/io.h>
+
+#define IOV_ITER(ACTION) \
+ assert(src_cnt >= 0); \
+ assert(src_cnt == 0 || src); \
+ \
+ const size_t byte_end = byte_beg + byte_max_cnt; \
+ int j = 0; \
+ size_t byte_pos = 0; \
+ for (int i = 0; \
+ i < src_cnt && (byte_max_cnt == 0 || byte_pos < byte_end); \
+ i++) { \
+ size_t off = 0, len; \
+ if (byte_pos < byte_beg) { \
+ if (byte_beg - byte_pos >= src[i].iov_len) { \
+ byte_pos += src[i].iov_len; \
+ continue; \
+ } \
+ off = byte_beg-byte_pos; \
+ len = src[i].iov_len-off; \
+ byte_pos = byte_beg; \
+ } else { \
+ len = src[i].iov_len; \
+ } \
+ if (byte_max_cnt && byte_end - byte_pos < len) \
+ len = byte_end - byte_pos; \
+ do { ACTION } while (0); \
+ byte_pos += len; \
+ j++; \
+ }
+
+int io_slice_cnt(const struct iovec *src, int src_cnt, size_t byte_beg, size_t byte_max_cnt) {
+ IOV_ITER();
+ return j;
+}
+
+int io_duplex_slice_cnt(const struct duplex_iovec *src, int src_cnt, size_t byte_beg, size_t byte_max_cnt) {
+ IOV_ITER();
+ return j;
+}
+
+void io_slice(struct iovec *dst, const struct iovec *src, int src_cnt, size_t byte_beg, size_t byte_max_cnt) {
+ assert(src_cnt == 0 || dst);
+ IOV_ITER(
+ dst[j] = ((struct iovec){
+ .iov_base = src[i].iov_base+off,
+ .iov_len = len,
+ });
+ );
+}
+void io_slice_duplex(struct duplex_iovec *dst, const struct duplex_iovec *src, int src_cnt, size_t byte_beg, size_t byte_max_cnt) {
+ assert(src_cnt == 0 || dst);
+ IOV_ITER(
+ dst[j] = ((struct duplex_iovec){
+ .iov_read_to = src[i].iov_read_to+off,
+ .iov_write_from = src[i].iov_write_from+off,
+ .iov_len = len,
+ });
+ );
+}
+
+void io_slice_rd_to_duplex(struct duplex_iovec *dst, const struct iovec *src, int src_cnt, size_t byte_beg, size_t byte_max_cnt) {
+ assert(src_cnt == 0 || dst);
+ IOV_ITER(
+ dst[j] = ((struct duplex_iovec){
+ .iov_read_to = src[i].iov_base+off,
+ .iov_write_from = IOVEC_DISCARD,
+ .iov_len = len,
+ });
+ );
+}
+
+void io_slice_wr_to_duplex(struct duplex_iovec *dst, const struct iovec *src, int src_cnt, size_t byte_beg, size_t byte_max_cnt) {
+ assert(src_cnt == 0 || dst);
+ IOV_ITER(
+ dst[j] = ((struct duplex_iovec){
+ .iov_read_to = IOVEC_DISCARD,
+ .iov_write_from = src[i].iov_base+off,
+ .iov_len = len,
+ });
+ );
+}
+
+void io_rd_to_duplex(struct duplex_iovec *dst, const struct iovec *src, int iovcnt) {
+ io_slice_rd_to_duplex(dst, src, iovcnt, 0, 0);
+}
+
+void io_wr_to_duplex(struct duplex_iovec *dst, const struct iovec *src, int iovcnt) {
+ io_slice_wr_to_duplex(dst, src, iovcnt, 0, 0);
+}
diff --git a/libhw_generic/net.c b/libhw_generic/net.c
new file mode 100644
index 0000000..e2785ae
--- /dev/null
+++ b/libhw_generic/net.c
@@ -0,0 +1,31 @@
+/* libhw_generic/net.c - Device-independent <libhw/generic/net.h> utilities
+ *
+ * Copyright (C) 2024 Luke T. Shumaker <lukeshu@lukeshu.com>
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+#include <libmisc/assert.h>
+
+#include <libhw/generic/net.h>
+
+#define NET_EOTHER 1
+#define NET_EARP_TIMEOUT 2
+#define NET_EACK_TIMEOUT 3
+#define NET_ERECV_TIMEOUT 4
+#define NET_ETHREAD 5
+#define NET_ECLOSED 6
+#define NET_EMSGSIZE 7
+
+const char *net_strerror(int net_errno) {
+ switch (net_errno) {
+ case NET_EOTHER: return "unknown error";
+ case NET_EARP_TIMEOUT: return "ARP timeout";
+ case NET_EACK_TIMEOUT: return "TCP ACK timeout";
+ case NET_ERECV_TIMEOUT: return "receive timeout";
+ case NET_ETHREAD: return "thread error";
+ case NET_ECLOSED: return "already closed";
+ case NET_EMSGSIZE: return "message too long";
+ default:
+ assert_notreached("invalid net_errno");
+ }
+}
diff --git a/libhw_generic/tests/test.h b/libhw_generic/tests/test.h
new file mode 120000
index 0000000..2fb1bd5
--- /dev/null
+++ b/libhw_generic/tests/test.h
@@ -0,0 +1 @@
+../../libmisc/tests/test.h \ No newline at end of file
diff --git a/libhw_generic/tests/test_io.c b/libhw_generic/tests/test_io.c
new file mode 100644
index 0000000..0d6df11
--- /dev/null
+++ b/libhw_generic/tests/test_io.c
@@ -0,0 +1,57 @@
+/* libhw_generic/tests/test_io.c - Tests for <libmisc/io.h>
+ *
+ * Copyright (C) 2025 Luke T. Shumaker <lukeshu@lukeshu.com>
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+#include <string.h>
+
+#include <libhw/generic/io.h>
+
+#include "test.h"
+
+int main() {
+ char data[] = "abcdefghijklmnopqrstuvwxyz";
+ struct iovec src[] = {
+ {.iov_base = &data[1], .iov_len=3}, /* "bcd" */
+ {.iov_base = &data[20], .iov_len=4}, /* "uvwx" */
+ };
+ const int src_cnt = sizeof(src)/sizeof(src[0]);
+
+ struct duplex_iovec dst[2];
+
+#define TC(start, max, ...) do { \
+ char *exp[] = {__VA_ARGS__}; \
+ int exp_cnt = sizeof(exp)/sizeof(exp[0]); \
+ int act_cnt = io_slice_cnt(src, src_cnt, start, max); \
+ test_assert(act_cnt == exp_cnt); \
+ memset(dst, 0, sizeof(dst)); \
+ io_slice_wr_to_duplex(dst, src, src_cnt, start, max); \
+ for (int i = 0; i < act_cnt; i++) { \
+ test_assert(dst[i].iov_read_to == IOVEC_DISCARD); \
+ test_assert(dst[i].iov_write_from != IOVEC_DISCARD); \
+ test_assert(dst[i].iov_len == strlen(exp[i])); \
+ test_assert(memcmp(dst[i].iov_write_from, exp[i], dst[i].iov_len) == 0); \
+ } \
+ } while (0)
+
+ TC(0, 0, /* => */ "bcd", "uvwx");
+ TC(1, 0, /* => */ "cd", "uvwx");
+ TC(2, 0, /* => */ "d", "uvwx");
+ TC(3, 0, /* => */ "uvwx");
+ TC(4, 0, /* => */ "vwx");
+ TC(5, 0, /* => */ "wx");
+ TC(6, 0, /* => */ "x");
+ TC(7, 0, /* => */ );
+
+ TC(0, 2, /* => */ "bc");
+ TC(1, 2, /* => */ "cd");
+ TC(2, 2, /* => */ "d", "u");
+ TC(3, 2, /* => */ "uv");
+ TC(4, 2, /* => */ "vw");
+ TC(5, 2, /* => */ "wx");
+ TC(6, 2, /* => */ "x");
+ TC(7, 2, /* => */ );
+
+ return 0;
+}