From 4d185da010aea1d307c8ff7807ef3d4359083ed0 Mon Sep 17 00:00:00 2001 From: "Luke T. Shumaker" Date: Mon, 28 Oct 2024 23:51:15 -0600 Subject: wip host net timeout --- libhw/common_include/libhw/generic/bootclock.h | 22 ++++++++++++++++++++++ libhw/common_include/libhw/generic/net.h | 16 ++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 libhw/common_include/libhw/generic/bootclock.h (limited to 'libhw/common_include') diff --git a/libhw/common_include/libhw/generic/bootclock.h b/libhw/common_include/libhw/generic/bootclock.h new file mode 100644 index 0000000..bea5338 --- /dev/null +++ b/libhw/common_include/libhw/generic/bootclock.h @@ -0,0 +1,22 @@ +/* libhw/generic/bootclock.h - Device-independent clock definitions + * + * Copyright (C) 2024 Luke T. Shumaker + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +#ifndef _LIBHW_GENERIC_BOOTCLOCK_H_ +#define _LIBHW_GENERIC_BOOTCLOCK_H_ + +#include /* for uint{n}_t and UINT{n}_C */ + +#define NS_PER_S UINT64_C(1000000000) /* needs at least log₂(10⁹) ≈ 29.9 bits */ +#define US_PER_S UINT64_C(1000000) /* needs at least log₂(10⁶) ≈ 19.9 bits */ +#define MS_PER_S UINT64_C(1000) /* needs at least log₂(10³) ≈ 9.9 bits */ + +/** + * (2⁶⁴-1 nanoseconds is more than 500 years; there is little + * risk of this overflowing) + */ +uint64_t bootclock_get_ns(void); + +#endif /* _LIBHW_GENERIC_BOOTCLOCK_H_ */ diff --git a/libhw/common_include/libhw/generic/net.h b/libhw/common_include/libhw/generic/net.h index 419a8f2..af8844f 100644 --- a/libhw/common_include/libhw/generic/net.h +++ b/libhw/common_include/libhw/generic/net.h @@ -60,6 +60,20 @@ struct net_stream_conn_vtable { 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. @@ -93,6 +107,8 @@ struct net_packet_conn_vtable { 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); }; -- cgit v1.2.3-2-g168b