Skip to content

Commit 262e777

Browse files
jukkarkartben
authored andcommitted
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 <jukka.rissanen@nordicsemi.no>
1 parent 10deca2 commit 262e777

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

include/zephyr/net/net_if.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,17 @@ struct net_if_dev {
703703

704704
/** RFC 2863 operational status */
705705
enum net_if_oper_state oper_state;
706+
707+
/** Last time the operational state was changed.
708+
* This is used to determine how long the interface has been in the
709+
* current operational state.
710+
*
711+
* This value is set to 0 when the interface is created, and then
712+
* updated whenever the operational state changes.
713+
*
714+
* The value is in milliseconds since boot.
715+
*/
716+
int64_t oper_state_change_time;
706717
};
707718

708719
/**
@@ -906,6 +917,12 @@ static inline enum net_if_oper_state net_if_oper_state_set(
906917
iface->if_dev->oper_state = oper_state;
907918
}
908919

920+
net_if_lock(iface);
921+
922+
iface->if_dev->oper_state_change_time = k_uptime_get();
923+
924+
net_if_unlock(iface);
925+
909926
return iface->if_dev->oper_state;
910927
}
911928

@@ -925,6 +942,34 @@ static inline enum net_if_oper_state net_if_oper_state(struct net_if *iface)
925942
return iface->if_dev->oper_state;
926943
}
927944

945+
/**
946+
* @brief Get an operational state change time of an interface
947+
*
948+
* @param iface Pointer to network interface
949+
* @param change_time Pointer to store the change time. Time the operational
950+
* state of an interface was last changed, in milliseconds since boot.
951+
* If the interface operational state has not been changed yet,
952+
* then the value is 0.
953+
*
954+
* @return 0 if ok, -EINVAL if operational state change time could not be
955+
* retrieved (for example if iface or change_time is NULL).
956+
*/
957+
static inline int net_if_oper_state_change_time(struct net_if *iface,
958+
int64_t *change_time)
959+
{
960+
if (iface == NULL || iface->if_dev == NULL || change_time == NULL) {
961+
return -EINVAL;
962+
}
963+
964+
net_if_lock(iface);
965+
966+
*change_time = iface->if_dev->oper_state_change_time;
967+
968+
net_if_unlock(iface);
969+
970+
return 0;
971+
}
972+
928973
/**
929974
* @brief Try sending a packet through a net iface
930975
*

0 commit comments

Comments
 (0)