PMC agents such as phc2sys tend to suppress uninteresting port state
transitions, to avoid running useless code when nothing really changed
in ptp4l.

With the addition of a PMC agent in ts2phc, it becomes desirable to
place that logic in a common implementation file, to avoid duplication.
Choose util.c for lack of a better place. Also add some comments and
rewrite the implementation so that it is a bit more clear. No change
intended in the produced code.

Signed-off-by: Vladimir Oltean <olte...@gmail.com>
---
v5->v6: patch is new

 phc2sys.c | 18 +++---------------
 util.c    | 13 +++++++++++++
 util.h    |  8 ++++++++
 3 files changed, 24 insertions(+), 15 deletions(-)

diff --git a/phc2sys.c b/phc2sys.c
index 63ec6d56ba3b..2c8e905e7c2c 100644
--- a/phc2sys.c
+++ b/phc2sys.c
@@ -117,8 +117,6 @@ static int clock_handle_leap(struct phc2sys_private *priv,
                             struct clock *clock,
                             int64_t offset, uint64_t ts);
 
-static int normalize_state(int state);
-
 static struct servo *servo_add(struct phc2sys_private *priv,
                               struct clock *clock)
 {
@@ -316,7 +314,7 @@ static void clock_reinit(struct phc2sys_private *priv, 
struct clock *clock,
                                                      &timestamping, &phc_index,
                                                      iface);
                if (!err) {
-                       p->state = normalize_state(state);
+                       p->state = port_state_normalize(state);
                }
                break;
        }
@@ -770,16 +768,6 @@ static int do_loop(struct phc2sys_private *priv)
        return 0;
 }
 
-static int normalize_state(int state)
-{
-       if (state != PS_MASTER && state != PS_SLAVE &&
-           state != PS_PRE_MASTER && state != PS_UNCALIBRATED) {
-               /* treat any other state as "not a master nor a slave" */
-               state = PS_DISABLED;
-       }
-       return state;
-}
-
 static int clock_compute_state(struct phc2sys_private *priv,
                               struct clock *clock)
 {
@@ -820,7 +808,7 @@ static int phc2sys_recv_subscribed(void *context, struct 
ptp_message *msg,
                                pid2str(&pds->portIdentity));
                        return 1;
                }
-               state = normalize_state(pds->portState);
+               state = port_state_normalize(pds->portState);
                if (port->state != state) {
                        pr_info("port %s changed state",
                                pid2str(&pds->portIdentity));
@@ -892,7 +880,7 @@ static int auto_init_ports(struct phc2sys_private *priv, 
int add_rt)
                port = port_add(priv, i, iface, phc_index);
                if (!port)
                        return -1;
-               port->state = normalize_state(state);
+               port->state = port_state_normalize(state);
        }
        if (LIST_EMPTY(&priv->clocks)) {
                pr_err("no suitable ports available");
diff --git a/util.c b/util.c
index a59b559ddf1c..e204c9cdb02a 100644
--- a/util.c
+++ b/util.c
@@ -207,6 +207,19 @@ const char *ustate2str(enum unicast_state ustate)
        return "???";
 }
 
+enum port_state port_state_normalize(enum port_state state)
+{
+       switch (state) {
+       case PS_MASTER:
+       case PS_SLAVE:
+       case PS_PRE_MASTER:
+       case PS_UNCALIBRATED:
+               return state;
+       default:
+               return PS_DISABLED;
+       }
+}
+
 void posix_clock_close(clockid_t clock)
 {
        if (clock == CLOCK_REALTIME) {
diff --git a/util.h b/util.h
index 558a6757131e..542f3b544ddb 100644
--- a/util.h
+++ b/util.h
@@ -26,6 +26,7 @@
 #include "address.h"
 #include "ddt.h"
 #include "ether.h"
+#include "fsm.h"
 #include "transport.h"
 #include "unicast_fsm.h"
 
@@ -113,6 +114,13 @@ char *portaddr2str(struct PortAddress *addr);
 
 const char *ustate2str(enum unicast_state ustate);
 
+/**
+ * Reduce all port states for which the sync direction isn't known to
+ * PS_DISABLED, and report the given port state otherwise. This minimizes port
+ * state transitions for PMC agents when nothing interesting happened.
+ */
+enum port_state port_state_normalize(enum port_state state);
+
 /**
  * Closes a dynamic posix clock.
  * @param clock  A clock ID obtained via posix_clock_close().
-- 
2.34.1



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

Reply via email to