Add function to compare the current time with the time of the last
successful pmc update to determine if ptp4l is still responding.

Signed-off-by: Miroslav Lichvar <mlich...@redhat.com>
---
 pmc_agent.c | 15 +++++++++++++++
 pmc_agent.h |  8 ++++++++
 2 files changed, 23 insertions(+)

diff --git a/pmc_agent.c b/pmc_agent.c
index 59f031d..9261c57 100644
--- a/pmc_agent.c
+++ b/pmc_agent.c
@@ -421,6 +421,21 @@ int pmc_agent_update(struct pmc_agent *node)
        return 0;
 }
 
+int pmc_agent_is_subscribed(struct pmc_agent *agent)
+{
+       struct timespec tp;
+       uint64_t ts;
+
+       if (clock_gettime(CLOCK_MONOTONIC, &tp)) {
+               pr_err("failed to read clock: %m");
+               return 0;
+       }
+       ts = tp.tv_sec * NS_PER_SEC + tp.tv_nsec;
+
+       return agent->pmc_last_update > 0 &&
+               ts - agent->pmc_last_update <= agent->subscribe_duration;
+}
+
 bool pmc_agent_utc_offset_traceable(struct pmc_agent *agent)
 {
        return agent->utc_offset_traceable;
diff --git a/pmc_agent.h b/pmc_agent.h
index fa53f56..16be614 100644
--- a/pmc_agent.h
+++ b/pmc_agent.h
@@ -165,6 +165,14 @@ int pmc_agent_subscribe(struct pmc_agent *agent, int 
timeout, int duration);
  */
 int pmc_agent_update(struct pmc_agent *agent);
 
+/**
+ * Checks if last successful subscription did not run out, i.e. ptp4l is still
+ * responding (assuming pmc_agent_update() is called frequently enough).
+ * @param agent  Pointer to a PMC instance obtained via @ref 
pmc_agent_create().
+ * @return       True if subscribed, false otherwise.
+ */
+int pmc_agent_is_subscribed(struct pmc_agent *agent);
+
 /**
  * Tests whether the current UTC offset is traceable.
  * @param agent  Pointer to a PMC instance obtained via @ref 
pmc_agent_create().
-- 
2.41.0



_______________________________________________
Linuxptp-devel mailing list
Linuxptp-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linuxptp-devel

Reply via email to