Extract the repeated switch-on-clockid pattern used for printing
system timestamps into a reusable helper function. This removes
code duplication in the -x (PTP_SYS_OFFSET_EXTENDED) output path
and prepares for additional callers.

No functional change.

Signed-off-by: Arthur Kiyanovski <[email protected]>
---
 tools/testing/selftests/ptp/testptp.c | 70 ++++++++++++---------------
 1 file changed, 32 insertions(+), 38 deletions(-)

diff --git a/tools/testing/selftests/ptp/testptp.c 
b/tools/testing/selftests/ptp/testptp.c
index ed1e288..d3bcfd0 100644
--- a/tools/testing/selftests/ptp/testptp.c
+++ b/tools/testing/selftests/ptp/testptp.c
@@ -153,6 +153,28 @@ static void usage(char *progname)
                progname, PTP_MAX_SAMPLES);
 }
 
+static void print_system_timestamp(int sample_num, __kernel_clockid_t clockid,
+                                  long long sec, unsigned int nsec,
+                                  const char *when)
+{
+       switch (clockid) {
+       case CLOCK_REALTIME:
+               printf("sample #%2d: real time %s: %lld.%09u\n",
+                      sample_num, when, sec, nsec);
+               break;
+       case CLOCK_MONOTONIC:
+               printf("sample #%2d: monotonic time %s: %lld.%09u\n",
+                      sample_num, when, sec, nsec);
+               break;
+       case CLOCK_MONOTONIC_RAW:
+               printf("sample #%2d: monotonic-raw time %s: %lld.%09u\n",
+                      sample_num, when, sec, nsec);
+               break;
+       default:
+               break;
+       }
+}
+
 int main(int argc, char *argv[])
 {
        struct ptp_clock_caps caps;
@@ -608,46 +630,18 @@ int main(int argc, char *argv[])
                               getextended);
 
                        for (i = 0; i < getextended; i++) {
-                               switch (ext_clockid) {
-                               case CLOCK_REALTIME:
-                                       printf("sample #%2d: real time before: 
%lld.%09u\n",
-                                              i, soe->ts[i][0].sec,
-                                              soe->ts[i][0].nsec);
-                                       break;
-                               case CLOCK_MONOTONIC:
-                                       printf("sample #%2d: monotonic time 
before: %lld.%09u\n",
-                                              i, soe->ts[i][0].sec,
-                                              soe->ts[i][0].nsec);
-                                       break;
-                               case CLOCK_MONOTONIC_RAW:
-                                       printf("sample #%2d: monotonic-raw time 
before: %lld.%09u\n",
-                                              i, soe->ts[i][0].sec,
-                                              soe->ts[i][0].nsec);
-                                       break;
-                               default:
-                                       break;
-                               }
+                               print_system_timestamp(i, ext_clockid,
+                                                      soe->ts[i][0].sec,
+                                                      soe->ts[i][0].nsec,
+                                                      "before");
+
                                printf("            phc time: %lld.%09u\n",
                                       soe->ts[i][1].sec, soe->ts[i][1].nsec);
-                               switch (ext_clockid) {
-                               case CLOCK_REALTIME:
-                                       printf("            real time after: 
%lld.%09u\n",
-                                              soe->ts[i][2].sec,
-                                              soe->ts[i][2].nsec);
-                                       break;
-                               case CLOCK_MONOTONIC:
-                                       printf("            monotonic time 
after: %lld.%09u\n",
-                                              soe->ts[i][2].sec,
-                                              soe->ts[i][2].nsec);
-                                       break;
-                               case CLOCK_MONOTONIC_RAW:
-                                       printf("            monotonic-raw time 
after: %lld.%09u\n",
-                                              soe->ts[i][2].sec,
-                                              soe->ts[i][2].nsec);
-                                       break;
-                               default:
-                                       break;
-                               }
+
+                               print_system_timestamp(i, ext_clockid,
+                                                      soe->ts[i][2].sec,
+                                                      soe->ts[i][2].nsec,
+                                                      "after");
                        }
                }
 
-- 
2.47.3


Reply via email to