From 262e777f3a26e95bc368e62af971bbb30a8d2544 Mon Sep 17 00:00:00 2001 From: Jukka Rissanen Date: Tue, 17 Jun 2025 17:05:07 +0300 Subject: [PATCH] net: if: Add operational state change time information Make sure network interface contains information when the operational state was changed. After boot, the value is set to 0. Signed-off-by: Jukka Rissanen --- include/zephyr/net/net_if.h | 45 +++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/include/zephyr/net/net_if.h b/include/zephyr/net/net_if.h index 67747d6e806..bbc6f80e77f 100644 --- a/include/zephyr/net/net_if.h +++ b/include/zephyr/net/net_if.h @@ -703,6 +703,17 @@ struct net_if_dev { /** RFC 2863 operational status */ enum net_if_oper_state oper_state; + + /** Last time the operational state was changed. + * This is used to determine how long the interface has been in the + * current operational state. + * + * This value is set to 0 when the interface is created, and then + * updated whenever the operational state changes. + * + * The value is in milliseconds since boot. + */ + int64_t oper_state_change_time; }; /** @@ -906,6 +917,12 @@ static inline enum net_if_oper_state net_if_oper_state_set( iface->if_dev->oper_state = oper_state; } + net_if_lock(iface); + + iface->if_dev->oper_state_change_time = k_uptime_get(); + + net_if_unlock(iface); + return iface->if_dev->oper_state; } @@ -925,6 +942,34 @@ static inline enum net_if_oper_state net_if_oper_state(struct net_if *iface) return iface->if_dev->oper_state; } +/** + * @brief Get an operational state change time of an interface + * + * @param iface Pointer to network interface + * @param change_time Pointer to store the change time. Time the operational + * state of an interface was last changed, in milliseconds since boot. + * If the interface operational state has not been changed yet, + * then the value is 0. + * + * @return 0 if ok, -EINVAL if operational state change time could not be + * retrieved (for example if iface or change_time is NULL). + */ +static inline int net_if_oper_state_change_time(struct net_if *iface, + int64_t *change_time) +{ + if (iface == NULL || iface->if_dev == NULL || change_time == NULL) { + return -EINVAL; + } + + net_if_lock(iface); + + *change_time = iface->if_dev->oper_state_change_time; + + net_if_unlock(iface); + + return 0; +} + /** * @brief Try sending a packet through a net iface *