summaryrefslogtreecommitdiff
path: root/libhw_generic/include
diff options
context:
space:
mode:
Diffstat (limited to 'libhw_generic/include')
-rw-r--r--libhw_generic/include/libhw/generic/alarmclock.h83
-rw-r--r--libhw_generic/include/libhw/generic/io.h225
-rw-r--r--libhw_generic/include/libhw/generic/net.h210
-rw-r--r--libhw_generic/include/libhw/generic/spi.h30
4 files changed, 347 insertions, 201 deletions
diff --git a/libhw_generic/include/libhw/generic/alarmclock.h b/libhw_generic/include/libhw/generic/alarmclock.h
index a9d816b..e7e66a1 100644
--- a/libhw_generic/include/libhw/generic/alarmclock.h
+++ b/libhw_generic/include/libhw/generic/alarmclock.h
@@ -1,17 +1,16 @@
/* 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
*/
#ifndef _LIBHW_GENERIC_ALARMCLOCK_H_
#define _LIBHW_GENERIC_ALARMCLOCK_H_
-#include <stdbool.h> /* for bool */
-#include <stdint.h> /* for uint{n}_t and UINT{n}_C */
+#include <stdint.h> /* for uint{n}_t and UINT{n}_C */
+#include <libmisc/obj.h>
#include <libmisc/private.h>
-#include <libmisc/vcall.h>
/* Useful constants ***********************************************************/
@@ -23,71 +22,51 @@
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. \
+ * \
+ * If fire_at_ns is in the past, then it will fire \
+ * immediately. \
+ */ \
+ LO_FUNC(void, 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);
-
-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_us(implements_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) {
- alarmclock_sleep_for_ns(clock, delta_ms * (NS_PER_S/MS_PER_S));
-}
+void alarmclock_sleep_until_ns(lo_interface alarmclock clock, uint64_t abstime_ns);
-static inline void alarmclock_sleep_for_s(implements_alarmclock *clock, uint64_t delta_s) {
- alarmclock_sleep_for_ns(clock, delta_s * NS_PER_S);
-}
+void alarmclock_sleep_for_ns(lo_interface alarmclock clock, uint64_t delta_ns);
+void alarmclock_sleep_for_us(lo_interface alarmclock clock, uint64_t delta_us);
+void alarmclock_sleep_for_ms(lo_interface alarmclock clock, uint64_t delta_ms);
+void alarmclock_sleep_for_s(lo_interface alarmclock clock, uint64_t delta_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..8c9ff53
--- /dev/null
+++ b/libhw_generic/include/libhw/generic/io.h
@@ -0,0 +1,225 @@
+/* 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, uint{n}_t, UINT{n}_MAX */
+
+#include <libmisc/error.h>
+#include <libmisc/obj.h>
+
+/* structs ********************************************************************/
+
+/* uoff_t ==========================================================*/
+
+typedef uint64_t uoff_t;
+#define UOFF_MAX UINT64_MAX
+DECLARE_ERROR_OR(uoff_t);
+DECLARE_ERROR_AND(uoff_t);
+
+/* rd_iovec ========================================================*/
+
+struct rd_iovec {
+ void *iov_read_to;
+ size_t iov_len;
+};
+DECLARE_ERROR_OR_(struct rd_iovec, rd_iovec);
+DECLARE_ERROR_AND_(struct rd_iovec, rd_iovec);
+
+/* wr_iovec ========================================================*/
+
+struct wr_iovec {
+ const void *iov_write_from;
+ size_t iov_len;
+};
+DECLARE_ERROR_OR_(struct wr_iovec, wr_iovec);
+DECLARE_ERROR_AND_(struct wr_iovec, wr_iovec);
+
+/* duplex_iovec ====================================================*/
+
+#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;
+ const void *iov_write_from;
+ size_t iov_len;
+};
+DECLARE_ERROR_OR_(struct duplex_iovec, duplex_iovec);
+DECLARE_ERROR_AND_(struct duplex_iovec, duplex_iovec);
+
+/* iovec utilities ************************************************************/
+
+/* If byte_max_cnt == 0, then there is no maximum. */
+
+/* slice iovec lists */
+int io_rd_slice_cnt ( const struct rd_iovec *src, int src_cnt, size_t byte_start, size_t byte_max_cnt);
+void io_rd_slice (struct rd_iovec *dst, const struct rd_iovec *src, int src_cnt, size_t byte_start, size_t byte_max_cnt);
+int io_wr_slice_cnt ( const struct wr_iovec *src, int src_cnt, size_t byte_start, size_t byte_max_cnt);
+void io_wr_slice (struct wr_iovec *dst, const struct wr_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 rd_iovec *src, int iovcnt);
+void io_wr_to_duplex(struct duplex_iovec *dst, const struct wr_iovec *src, int iovcnt);
+
+/* slice and convert in one go */
+void io_slice_rd_to_duplex(struct duplex_iovec *dst, const struct rd_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 wr_iovec *src, int src_cnt, size_t byte_start, size_t byte_max_cnt);
+
+/* basic interfaces ***********************************************************/
+
+/*
+ * Conventions:
+ *
+ * - Naming:
+ * + The "p"[osition] prefix means an explicit `uoff_t offset`,
+ * instead of using an internal cursor.
+ * + The "v"[ec] suffix means a sequence of iovecs, instead of a
+ * simple buf+len.
+ *
+ * - Errors:
+ * + Short *reads* are *not* errors (and so return size_t *or*
+ * error).
+ * + Short *writes* *are* errors (and so return size_t *and*
+ * (possibly-null) error).
+ */
+
+/* read ============================================================*/
+
+/**
+ * Return bytes-read on success. A short read is *not* an error
+ * (unlike `write` methods).
+ *
+ * It is invalid to call readv when the sum length of iovecs is 0.
+ */
+#define io_reader_LO_IFACE \
+ LO_FUNC(size_t_or_error, readv, const struct rd_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 rd_iovec){.iov_read_to = buf, .iov_len = count}), 1)
+
+/**
+ * Returns bytes-read on success. A short read is *not* an error
+ * (unlike `write` methods).
+ *
+ * It is invalid to call preadv when the sum length of iovecs is 0.
+ */
+#define io_preader_LO_IFACE \
+ LO_FUNC(size_t_or_error, preadv, const struct rd_iovec *iov, int iovcnt, uoff_t offset)
+LO_INTERFACE(io_preader);
+#define io_preadv(r, iov, iovcnt, off) LO_CALL(r, preadv, iov, iovcnt, off)
+#define io_pread(r, buf, count, off) LO_CALL(r, preadv, &((struct rd_iovec){.iov_read_to = buf, .iov_len = count}), 1, off)
+
+/* write ===========================================================*/
+
+/**
+ * Return bytes-written. A short write *is* an error (unlike `read`
+ * methods).
+ *
+ * Writes are *not* guaranteed to be atomic, so if you have concurrent
+ * writers then you should arrange for a mutex to protect the writer.
+ *
+ * It is invalid to call writev when the sum length of iovecs is 0.
+ */
+#define io_writer_LO_IFACE \
+ LO_FUNC(size_t_and_error, writev, const struct wr_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 wr_iovec){.iov_write_from = buf, .iov_len = count}), 1)
+
+/**
+ * Returns bytes-written. A short write *is* an error (unlike `read`
+ * methods).
+ *
+ * Writes are *not* guaranteed to be atomic, so if you have concurrent
+ * writers then you should arrange for a mutex to protect the writer.
+ *
+ * It is invalid to call writev when the sum length of iovecs is 0.
+ */
+#define io_pwriter_LO_IFACE \
+ LO_FUNC(size_t_and_error, pwritev, const struct wr_iovec *iov, int iovcnt, uoff_t offset)
+LO_INTERFACE(io_pwriter);
+#define io_pwritev(r, iov, iovcnt, off) LO_CALL(r, pwritev, iov, iovcnt, off)
+#define io_pwrite(r, buf, count, off) LO_CALL(r, pwritev, &((struct wr_iovec){.iov_write_from = buf, .iov_len = count}), 1, off)
+
+/* readwrite =======================================================*/
+
+/**
+ * A short read/write *is* an error (like `write` methods and unlike
+ * `read` methods).
+ *
+ * Reads/writes are *not* guaranteed to be atomic, so if you have
+ * concurrent readers/writers then you should arrange for a mutex to
+ * protect the duplex_readwriter.
+ *
+ * It is invalid to call readwritev when the sum length of iovecs is 0.
+ */
+#define io_duplex_readwriter_LO_IFACE \
+ LO_FUNC(size_t_and_error, 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)
+
+/* read then write =================================================*/
+
+/**
+ * LO_CALL(src, pread_to, dst, src_offset, count) is functionally:
+ *
+ * size_t_and_error copy(lo_interface io_writer dst, lo_interface io_preader src, uoff_t src_offset, size_t count) {
+ * buf = malloc(count);
+ * size_t_or_error read_r = io_pread(src, buf, count, src_offset);
+ * if (read_r.is_err)
+ * return ERROR_AND(size_t, 0, read_r.err);
+ * size_t_and_error write_r = io_write(dst, buf, read_r.size_t);
+ * free(buf);
+ * return write_r;
+ * }
+ *
+ * except that it does not need to allocate a buffer. That is: It
+ * allows the reader to dump its data directly to the writer. This
+ * allows us to save a copy when the reader already has an in-memory
+ * buffer containing the data.
+ *
+ * The above code defines when a short read/write is an error or not.
+ *
+ * It must call writev() exactly 0-or-1 times, and if it does call it,
+ * then it must be called with exactly 1 iovec.
+ */
+#define io_preader_to_LO_IFACE \
+ LO_FUNC(size_t_and_error, pread_to, lo_interface io_writer dst, uoff_t src_offset, size_t count)
+LO_INTERFACE(io_preader_to);
+
+/* close ===========================================================*/
+
+#define io_closer_LO_IFACE \
+ LO_FUNC(error, close)
+LO_INTERFACE(io_closer);
+#define io_close(c) LO_CALL(c, close)
+
+#define io_bidi_closer_LO_IFACE \
+ LO_NEST(io_closer) \
+ LO_FUNC(error, close_read) \
+ LO_FUNC(error, 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)
+
+/* flush ===========================================================*/
+
+#define io_flusher_LO_IFACE \
+ LO_FUNC(error, flush)
+LO_INTERFACE(io_flusher);
+#define io_flush(c) LO_CALL(c, flush)
+
+#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..de94a98 100644
--- a/libhw_generic/include/libhw/generic/net.h
+++ b/libhw_generic/include/libhw/generic/net.h
@@ -1,27 +1,17 @@
/* 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
*/
#ifndef _LIBHW_GENERIC_NET_H_
#define _LIBHW_GENERIC_NET_H_
-#include <inttypes.h> /* for PRI{u,x}{n} */
-#include <stdbool.h> /* for bool */
#include <stddef.h> /* for size_t */
#include <stdint.h> /* for uint{n}_t} */
-#include <sys/types.h> /* for ssize_t */
-/* Errnos *********************************************************************/
-
-#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
+#include <libhw/generic/io.h>
+#include <libmisc/fmt.h>
/* Address types **************************************************************/
@@ -31,118 +21,83 @@ struct net_ip4_addr {
static const struct net_ip4_addr net_ip4_addr_broadcast = {{255, 255, 255, 255}};
static const struct net_ip4_addr net_ip4_addr_zero = {{0, 0, 0, 0}};
-
-#define PRI_net_ip4_addr "%"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8
-#define ARG_net_ip4_addr(addr) (addr).octets[0], \
- (addr).octets[1], \
- (addr).octets[2], \
- (addr).octets[3]
+void fmt_print_net_ip4_addr(lo_interface fmt_dest, struct net_ip4_addr);
struct net_eth_addr {
unsigned char octets[6];
};
-#define PRI_net_eth_addr "%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8
-#define ARG_net_eth_addr(addr) (addr).octets[0], \
- (addr).octets[1], \
- (addr).octets[2], \
- (addr).octets[3], \
- (addr).octets[4], \
- (addr).octets[5]
+void fmt_print_net_eth_addr(lo_interface fmt_dest, 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);
-};
+#define net_stream_conn_LO_IFACE \
+ LO_NEST(io_reader) \
+ LO_NEST(io_writer) \
+ 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);
+
+DECLARE_ERROR_OR_(lo_interface net_stream_conn, 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(net_stream_conn_or_error, 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);
/* 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(error, 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(size_t_or_error, 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 +107,17 @@ 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(net_stream_conn_or_error , 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) \
+ \
+ /** FIXME: arp_ping should probably have an explicit timeout or something. */ \
+ LO_FUNC(bool , arp_ping , struct net_ip4_addr)
+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..c0d9d91 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,25 +9,15 @@
#include <stddef.h> /* for size_t */
-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) */
- SPI_MODE_2 = 2, /* clk_polarity=1 (idle high), clk_phase=0 (sample on rise) */
- SPI_MODE_3 = 3, /* clk_polarity=1 (idle high), clk_phase=1 (sample on fall) */
-};
+#include <libhw/generic/io.h>
-struct bidi_iovec {
- void *iov_read_dst;
- void *iov_write_src;
- size_t iov_len;
+enum spi_mode {
+ SPI_MODE_0 = 0, /* clk_polarity=0 (idle low), clk_phase=0 (sample on transition-from-idle (rise)) ; shift on clock fall and CS fall */
+ SPI_MODE_1 = 1, /* clk_polarity=0 (idle low), clk_phase=1 (sample on transition-to-idle (fall)) ; shift on clock rise */
+ SPI_MODE_2 = 2, /* clk_polarity=1 (idle high), clk_phase=0 (sample on transition-from-idle (fall)) ; shift on clock rise and CS fall */
+ SPI_MODE_3 = 3, /* clk_polarity=1 (idle high), clk_phase=1 (sample on transition-to-idle (rise)) ; shift on clock fall */
};
-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_ */