]> git.ipfire.org Git - thirdparty/nqptp.git/commitdiff
Add version string to initial debug message
authorMike Brady <4265913+mikebrady@users.noreply.github.com>
Tue, 13 Dec 2022 09:07:02 +0000 (09:07 +0000)
committerMike Brady <4265913+mikebrady@users.noreply.github.com>
Tue, 13 Dec 2022 09:07:02 +0000 (09:07 +0000)
Work out when the clock can be relied upon not to go to sleep, i.e. is "active". This is when SPS is playing (B) or paused (P) and for brief periods after the clock is selected (T) and after play ends (E).
SPS signals Play/Pause/End/Timer Select/Timer Release with B/P/E/T messages -- B for "Begin". "T <ip>" means select the clock at that address. "T" means release the clock.
Reset clock smoothing when the clock goes "active" but not at any other time.
Clamp reductions in the offset calculated as a uint64_t remote-time-now_ns - local-time-now_ns -- and which it is assumed are due to delays in the network when the clock is "active" -- to 2.5 mS.
Turn off clock updates when the clock is not "active" -- i.e. when SPS is not paying or paused.
Remove some redundant features and code.

nqptp-clock-sources.c
nqptp-clock-sources.h
nqptp-message-handlers.c
nqptp-ptp-definitions.h
nqptp-utilities.c
nqptp.c

index 616652b68cb8d0d9c80a51c1612e8c84e77e5d02..04181b64cc20f418b26b2741fd6f14b59a42f204 100644 (file)
@@ -49,6 +49,7 @@ struct shm_structure *shared_memory;
 clock_source_private_data clocks_private[MAX_CLOCKS];
 client_record clients[MAX_CLIENTS];
 
+/*
 const char *get_client_name(int client_id) {
   if ((client_id >= 0) && (client_id < MAX_CLIENTS)) {
     return clients[client_id].shm_interface_name;
@@ -56,6 +57,7 @@ const char *get_client_name(int client_id) {
     return "";
   }
 }
+*/
 
 int get_client_id(char *client_shared_memory_interface_name) {
   int response = -1; // signify not found
@@ -140,10 +142,10 @@ int get_client_id(char *client_shared_memory_interface_name) {
           die("mutex attribute destruction failed - %s.", strerror(errno));
         }
 
-        for (i = 0; i < MAX_CLOCKS; i++) {
-          clocks_private[i].client_flags[response] =
-              0; // turn off all client flags in every clock for this client
-        }
+        // for (i = 0; i < MAX_CLOCKS; i++) {
+        //  clocks_private[i].client_flags[response] =
+        //      0; // turn off all client flags in every clock for this client
+        // }
       } else {
         debug(1, "could not create a client record for client \"%s\".",
               client_shared_memory_interface_name);
@@ -244,38 +246,8 @@ int create_clock_source_record(char *sender_string,
   return response;
 }
 
-void update_master_clock_info(int client_id, uint64_t master_clock_id, const char *ip,
-                              uint64_t local_time, uint64_t local_to_master_offset,
-                              uint64_t mastership_start_time) {
-  if (clients[client_id].shm_interface_name[0] != '\0') {
-    // debug(1,"update_master_clock_info clock: % " PRIx64 ", offset: %" PRIx64 ".",
-    // master_clock_id, local_to_master_offset);
-    int rc = pthread_mutex_lock(&clients[client_id].shared_memory->shm_mutex);
-    if (rc != 0)
-      warn("Can't acquire mutex to update master clock!");
-    clients[client_id].shared_memory->master_clock_id = master_clock_id;
-    if (ip != NULL) {
-      strncpy((char *)&clients[client_id].shared_memory->master_clock_ip, ip,
-              FIELD_SIZEOF(struct shm_structure, master_clock_ip) - 1);
-      clients[client_id].shared_memory->master_clock_start_time = mastership_start_time;
-      clients[client_id].shared_memory->local_time = local_time;
-      clients[client_id].shared_memory->local_to_master_time_offset = local_to_master_offset;
-    } else {
-      clients[client_id].shared_memory->master_clock_ip[0] = '\0';
-      clients[client_id].shared_memory->master_clock_start_time = 0;
-      clients[client_id].shared_memory->local_time = 0;
-      clients[client_id].shared_memory->local_to_master_time_offset = 0;
-    }
-    rc = pthread_mutex_unlock(&clients[client_id].shared_memory->shm_mutex);
-    if (rc != 0)
-      warn("Can't release mutex after updating master clock!");
-    // debug(1,"update_master_clock_info done");
-  }
-}
-
-void new_update_master_clock_info(uint64_t master_clock_id, const char *ip,
-                              uint64_t local_time, uint64_t local_to_master_offset,
-                              uint64_t mastership_start_time) {
+void update_master_clock_info(uint64_t master_clock_id, const char *ip, uint64_t local_time,
+                              uint64_t local_to_master_offset, uint64_t mastership_start_time) {
   // debug(1,"update_master_clock_info clock: % " PRIx64 ", offset: %" PRIx64 ".",
   // master_clock_id, local_to_master_offset);
   int rc = pthread_mutex_lock(&shared_memory->shm_mutex);
index 8f3a3dcfa2699827d97d687e1ba53c90d4ba33bc..110b1ec91c59d65b13f6e23a3aa5e25d5fb30fc8 100644 (file)
 #include "nqptp-shm-structures.h"
 #include "nqptp.h"
 
-typedef enum {
-  clock_is_in_use,
-  clock_is_master
-} clock_flags;
+typedef enum { clock_is_in_use, clock_is_master } clock_flags;
 
 // information about each clock source
 typedef struct {
@@ -43,7 +40,6 @@ typedef struct {
   uint64_t time_of_last_use; // will be taken out of use if not used for a while and not in the
                              // timing peer group
   uint8_t flags;             // stuff related specifically to the clock itself
-  uint8_t client_flags[MAX_CLIENTS]; // stuff related to membership of the clients' timing lists
 
   // these are for finding the best clock to use
   // See Figure 27 and 27 pp 89 -- 90 for the Data set comparison algorithm
@@ -57,7 +53,6 @@ typedef struct {
   uint64_t grandmasterIdentity;
   uint16_t stepsRemoved;
   int identical_previous_preciseOriginTimestamp_count;
-  int clamp_count;
   int wakeup_sent;
 
 } clock_source_private_data;
@@ -89,12 +84,7 @@ int delete_clients();
 
 extern clock_source_private_data clocks_private[MAX_CLOCKS];
 
-void update_master_clock_info(int client_id, uint64_t master_clock_id, const char *ip,
-                              uint64_t local_time, uint64_t local_to_master_offset,
-                              uint64_t mastership_start_time);
-
-void new_update_master_clock_info(uint64_t master_clock_id, const char *ip,
-                              uint64_t local_time, uint64_t local_to_master_offset,
-                              uint64_t mastership_start_time);
+void update_master_clock_info(uint64_t master_clock_id, const char *ip, uint64_t local_time,
+                              uint64_t local_to_master_offset, uint64_t mastership_start_time);
 
 #endif
index a0b6151c7c9d30e6ab798d77b37b4162c1b07009..0d2034569b8696baeb97eecf09b0a36d1496604e 100644 (file)
@@ -19,7 +19,7 @@
 #include <arpa/inet.h> // ntohl and ntohs
 #include <string.h>    //strsep
 
-#include <stdio.h> // snprintf
+#include <stdio.h>  // snprintf
 #include <stdlib.h> // drand48
 #include <unistd.h> // usleep
 
@@ -41,14 +41,15 @@ char *hex_string(void *buf, size_t buf_len) {
   for (obfc = 0; obfc < buf_len; obfc++) {
     snprintf(obfp, 3, "%02X", *tbuf);
     obfp += 2;
-    tbuf = tbuf+1;
+    tbuf = tbuf + 1;
   };
   *obfp = 0;
   return hexcharbuffer;
 }
 
 void handle_control_port_messages(char *buf, ssize_t recv_len,
-                                  clock_source_private_data *clock_private_info, uint64_t reception_time) {
+                                  clock_source_private_data *clock_private_info,
+                                  uint64_t reception_time) {
   if (recv_len != -1) {
     buf[recv_len - 1] = 0; // make sure there's a null in it!
     debug(2, "New control port message: \"%s\".", buf);
@@ -57,179 +58,123 @@ void handle_control_port_messages(char *buf, ssize_t recv_len,
     char *smi_name = strsep(&ip_list, " ");
     char *command = NULL;
     if (smi_name != NULL) {
-      int client_id = 0;
       if (ip_list != NULL)
         command = strsep(&ip_list, " ");
 
       // "B" is for play begin/resume. Assumes a "T <ip>" already
       // "E" is for play end/stop.
       // "P" is for pause (currently Buffered Audio only).
-      // 
+      //
       // "T <ip>" is for the IP address of a timer.
       // "T" means no active timer.
       // clock_is_active is made true by Play and false by Pause or End.
-      
+
       if ((strcmp(command, "B") == 0) && (ip_list == NULL)) {
-        debug(2,"Play.");
+        debug(2, "Play.");
         // We want to avoid, as much as possible, resetting the clock smoothing.
         // If we know the clock is already active or
         // if it's only been a short time since we know it was last active
         // then we will not reset the clock.
         if (clock_is_active) {
-          debug(2,"clock is already active");
+          debug(2, "clock is already active");
         } else {
           // Find out if the clock is active i.e. not sleeping.
           // We know it is active between "B" and "E" commands.
-          // We also know it is active for brief periods after the "T" and "E" commands are received.
-          // If it is not definitely active, we will reset smoothing.
+          // We also know it is active for brief periods after the "T" and "E" commands are
+          // received. If it is not definitely active, we will reset smoothing.
           int will_ask_for_a_reset = 0;
           if (clock_validity_expiration_time == 0) {
-            debug(1,"no clock_validity_expiration_time.");
+            debug(1, "no clock_validity_expiration_time.");
             will_ask_for_a_reset = 1;
           } else {
             int64_t time_to_clock_expiration = clock_validity_expiration_time - reception_time;
             // timings obtained with an iPhone Xs Max on battery save
-            
+
             // around 30 seconds at a buffered audio pause on an iphone.
             // around 1 second after a buffered audio stop on an iphone
             // 10 seconds after a "T" from an iPhone that immediately sleeps
             // more than a minute from "T" from a HomePod mini.
-            
+
             if (time_to_clock_expiration < 0) {
-              debug(2,"Clock validity may have expired, so ask for a reset.");
+              debug(2, "Clock validity may have expired, so ask for a reset.");
               will_ask_for_a_reset = 1;
             }
           }
           if (will_ask_for_a_reset != 0) {
-            debug(2,"Reset clock smoothing");
+            debug(2, "Reset clock smoothing");
             reset_clock_smoothing = 1;
           }
         }
         clock_is_active = 1;
         clock_validity_expiration_time = 0;
       } else if ((strcmp(command, "E") == 0) && (ip_list == NULL)) {
-        debug(2,"Stop");
+        debug(2, "Stop");
         if (clock_is_active) {
-          debug(2,"reset clock_validity_expiration_time to 2.25 seconds in the future.");
-          clock_validity_expiration_time = reception_time + 2250000000; // expiration time can be very soon after an "E"
+          debug(2, "reset clock_validity_expiration_time to 2.25 seconds in the future.");
+          clock_validity_expiration_time =
+              reception_time + 2250000000; // expiration time can be very soon after an "E"
           clock_is_active = 0;
         } else {
-          debug(2,"clock is already inactive.");
+          debug(2, "clock is already inactive.");
         }
       } else if ((strcmp(command, "P") == 0) && (ip_list == NULL)) {
-        debug(2,"Pause");
-        // A pause always seems to turn into a Stop in now more than a few seconds, and the clock keeps going, it seems
-        // so there is nothing to do here.
+        debug(2, "Pause");
+        // A pause always seems to turn into a Stop in now more than a few seconds, and the clock
+        // keeps going, it seems so there is nothing to do here.
       } else if ((command == NULL) || ((strcmp(command, "T") == 0) && (ip_list == NULL))) {
-        debug(2,"Stop Timing");
+        debug(2, "Stop Timing");
         clock_is_active = 0;
-        debug(2,"Clear timing peer group.");
+        debug(2, "Clear timing peer group.");
         // dirty experimental hack -- delete all the clocks
         int gc;
         for (gc = 0; gc < MAX_CLOCKS; gc++) {
           memset(&clock_private_info[gc], 0, sizeof(clock_source_private_data));
-        }        
-
-        // clear all the flags
-        debug(2, "Stop monitoring clock.");
-        int client_id = get_client_id(smi_name); // create the record if it doesn't exist
-        if (client_id != -1) {
-          /*
-          int i;
-          for (i = 0; i < MAX_CLOCKS; i++) {
-            // e.g. (obsolete)
-            clock_private_info[i].flags &= ~(1 << clock_is_master);
-            clock_private_info[i].mastership_start_time = 0;
-            clock_private_info[i].previous_offset_time = 0;
-
-            // if a clock would now stop being a master everywhere
-            // it should drop mastership history and do a sync when it becomes master again
-            if ((clock_private_info[i].client_flags[client_id] & (1 << clock_is_master)) !=
-                0) { // if clock[i] is master for this client's timing group
-              int c;
-              int this_clock_is_master_elsewhere = 0;
-              for (c = 0; c < MAX_CLIENTS; c++) {
-                if ((c != client_id) &&
-                    ((clock_private_info[i].client_flags[c] & (1 << clock_is_master)) != 0))
-                  this_clock_is_master_elsewhere = 1;
-              }
-              if (this_clock_is_master_elsewhere == 0) {
-                clock_private_info[i].mastership_start_time = 0;
-                clock_private_info[i].previous_offset_time = 0;
-              }
-            }
-            clock_private_info[i].client_flags[client_id] = 0;
-            
-          }
-          */
-          update_master_clock_info(client_id, 0, NULL, 0, 0, 0); // it may have obsolete stuff in it
         }
-        new_update_master_clock_info(0, NULL, 0, 0, 0); // it may have obsolete stuff in it
+        update_master_clock_info(0, NULL, 0, 0, 0); // the SMI may have obsolete stuff in it
       } else {
-        debug(2,"Start Timing");
-        debug(2,"Clear timing peer group.");
+        debug(2, "Start Timing");
         // dirty experimental hack -- delete all the clocks
         int gc;
         for (gc = 0; gc < MAX_CLOCKS; gc++) {
           memset(&clock_private_info[gc], 0, sizeof(clock_source_private_data));
-        }        
+        }
         debug(2, "get or create new record for \"%s\".", smi_name);
-        client_id = get_client_id(smi_name); // create the record if it doesn't exist
-        if (client_id != -1) {
-          if (strcmp(command, "T") == 0) {
-            int i;
-            for (i = 0; i < MAX_CLOCKS; i++) {
-              clock_private_info[i].announcements_without_followups =
-                  0; // to allow a possibly silent clock to be revisited when added to a timing
-                     // peer list
-              clock_private_info[i].follow_up_number = 0;
-            }
-
-            // take the first ip and make it the master, permanently
-            
-            
-            if (ip_list != NULL) {
-              char *new_ip = strsep(&ip_list, " ");
-              // look for the IP in the list of clocks, and create an inert entry if not there
-              if ((new_ip != NULL) && (new_ip[0] != 0)) {
-                int t = find_clock_source_record(new_ip, clock_private_info);
-                if (t == -1)
-                  t = create_clock_source_record(new_ip, clock_private_info);
-                if (t != -1) { // if the clock table is not full, show it's a timing peer
-                  debug(2, "Monitor clock at %s.", new_ip);
-                  clock_private_info[t].client_flags[client_id] |= (1 << clock_is_master);
-                }
-                // otherwise, drop it
-              }
-              
-              
-            }
-
+        //        client_id = get_client_id(smi_name); // create the record if it doesn't exist
+        //        if (client_id != -1) {
+        if (strcmp(command, "T") == 0) {
+          int i;
+          for (i = 0; i < MAX_CLOCKS; i++) {
+            clock_private_info[i].announcements_without_followups =
+                0; // to allow a possibly silent clock to be revisited when added to a timing
+                   // peer list
+            clock_private_info[i].follow_up_number = 0;
+          }
 
-/*
-            while (ip_list != NULL) {
-              char *new_ip = strsep(&ip_list, " ");
-              // look for the IP in the list of clocks, and create an inert entry if not there
-              if ((new_ip != NULL) && (new_ip[0] != 0)) {
-                int t = find_clock_source_record(new_ip, clock_private_info);
-                if (t == -1)
-                  t = create_clock_source_record(new_ip, clock_private_info);
-                if (t != -1) { // if the clock table is not full, show it's a timing peer
-                  clock_private_info[t].client_flags[client_id] |= (1 << clock_is_a_timing_peer);
-                }
-                // otherwise, drop it
+          // take the first ip and make it the master, permanently
+
+          if (ip_list != NULL) {
+            char *new_ip = strsep(&ip_list, " ");
+            // look for the IP in the list of clocks, and create an inert entry if not there
+            if ((new_ip != NULL) && (new_ip[0] != 0)) {
+              int t = find_clock_source_record(new_ip, clock_private_info);
+              if (t == -1)
+                t = create_clock_source_record(new_ip, clock_private_info);
+              if (t != -1) { // if the clock table is not full, okay
+                debug(2, "Monitor clock at %s.", new_ip);
               }
+              // otherwise, drop it
             }
-*/
-            // a new clock timing record will be started now
-            debug(2,"reset clock_validity_expiration_time to 5.0 seconds in the future.");
-            clock_validity_expiration_time = reception_time + 5000000000L; // clock can stop as soon as 6 seconds after a "T"
-          } else {
-            warn("Unrecognised string on the control port.");
           }
+          // a new clock timing record will be started now
+          debug(2, "reset clock_validity_expiration_time to 5.0 seconds in the future.");
+          clock_validity_expiration_time =
+              reception_time + 5000000000L; // clock can stop as soon as 6 seconds after a "T"
         } else {
-          warn("Could not find or create a record for SMI Interface \"%s\".", smi_name);
+          warn("Unrecognised string on the control port.");
         }
+        //        } else {
+        //          warn("Could not find or create a record for SMI Interface \"%s\".", smi_name);
+        //        }
       }
     } else {
       warn("SMI Interface Name not found on the control port.");
@@ -284,12 +229,11 @@ void handle_announce(char *buf, ssize_t recv_len, clock_source_private_data *clo
     clock_private_info->grandmasterPriority2 = msg->announce.grandmasterPriority2;
     clock_private_info->stepsRemoved = stepsRemoved;
     clock_private_info->clock_port_number = sourcePortID;
-    
+
     if (clock_private_info->wakeup_sent == 0) {
       send_awakening_announcement_sequence(
-                  clock_private_info->clock_id, clock_private_info->ip, clock_private_info->family,
-                  clock_private_info->grandmasterPriority1,
-                  clock_private_info->grandmasterPriority2);
+          clock_private_info->clock_id, clock_private_info->ip, clock_private_info->family,
+          clock_private_info->grandmasterPriority1, clock_private_info->grandmasterPriority2);
       clock_private_info->wakeup_sent = 1;
     }
   }
@@ -297,14 +241,25 @@ void handle_announce(char *buf, ssize_t recv_len, clock_source_private_data *clo
 
 void handle_sync(char *buf, ssize_t recv_len, clock_source_private_data *clock_private_info,
                  __attribute__((unused)) uint64_t reception_time) {
+  /*
+    // diagnostic -- decide whether to delay the processing of the follow_up to simulate a noisy
+    network if (drand48() < 0.015) {
+      // generate a random delay between 10 and 3500 milliseconds
+      int delay = (int)((3000 - 60) * drand48()) + 60;
+      debug(1,"Delay sync processing by %u milliseconds.", delay);
+      usleep(delay * 1000);
+      reception_time = get_time_now();
+    }
+  */
+
   if (clock_private_info->clock_id == 0) {
     debug(2, "Sync received before announcement -- discarded.");
   } else {
     if ((recv_len >= 0) && ((size_t)recv_len >= sizeof(struct ptp_sync_message))) {
-        // debug_print_buffer(1, buf, recv_len);
-        struct ptp_sync_message *msg = (struct ptp_sync_message *)buf;
+      // debug_print_buffer(1, buf, recv_len);
+      struct ptp_sync_message *msg = (struct ptp_sync_message *)buf;
 
-        // clang-format off
+      // clang-format off
       
       // actually the precision timestamp needs to be corrected by the Follow_Up Correction_Field contents.
       // According to IEEE Std 802.1AS-2020, paragraph 11.4.4.2.1:
@@ -316,14 +271,14 @@ void handle_sync(char *buf, ssize_t recv_len, clock_source_private_data *clock_p
       including any fractional nanoseconds.
       */
 
-        // clang-format on
+      // clang-format on
 
-        int64_t correction_field = ntoh64(msg->header.correctionField);
+      int64_t correction_field = ntoh64(msg->header.correctionField);
 
-        if (correction_field != 0)
-          debug(1, "Sync correction field is non-zero: %" PRId64 " ns.", correction_field);
+      if (correction_field != 0)
+        debug(1, "Sync correction field is non-zero: %" PRId64 " ns.", correction_field);
 
-        correction_field = correction_field / 65536; // might be signed
+      correction_field = correction_field / 65536; // might be signed
     } else {
       debug(1, "Sync message is too small to be valid.");
     }
@@ -335,65 +290,54 @@ void handle_follow_up(char *buf, ssize_t recv_len, clock_source_private_data *cl
   if (clock_private_info->clock_id == 0) {
     debug(2, "Follow_Up received before announcement -- discarded.");
   } else {
-    /*
-    // diagnostic -- decide whether to delay the processing of the follow_up to simulate a noisy network
-    if (drand48() < 0.05) {
-      // generate a random delay between 10 and 3500 milliseconds
-      int delay = (int)((3500 - 10) * drand48()) + 10;
-      debug(1,"Delay follow_up processing by %u milliseconds.", delay);
-      usleep(delay * 1000);
-      reception_time = get_time_now();    
-    }
-    */
     clock_private_info->announcements_without_followups = 0;
     if ((recv_len >= 0) && ((size_t)recv_len >= sizeof(struct ptp_follow_up_message))) {
-        // debug_print_buffer(1, buf, recv_len);
-        struct ptp_follow_up_message *msg = (struct ptp_follow_up_message *)buf;
-        uint16_t seconds_hi = nctohs(&msg->follow_up.preciseOriginTimestamp[0]);
-        uint32_t seconds_low = nctohl(&msg->follow_up.preciseOriginTimestamp[2]);
-        uint32_t nanoseconds = nctohl(&msg->follow_up.preciseOriginTimestamp[6]);
-        uint64_t preciseOriginTimestamp = seconds_hi;
-        preciseOriginTimestamp = preciseOriginTimestamp << 32;
-        preciseOriginTimestamp = preciseOriginTimestamp + seconds_low;
-        preciseOriginTimestamp = preciseOriginTimestamp * 1000000000L;
-        preciseOriginTimestamp = preciseOriginTimestamp + nanoseconds;
-
-        // update our sample information
-        int grandmasterClockIsStopped = 0;
-        if ((clock_private_info->previous_preciseOriginTimestamp == preciseOriginTimestamp) && (clock_private_info->clock_id == clock_private_info->grandmasterIdentity)) {
-          clock_private_info->identical_previous_preciseOriginTimestamp_count++;
-          grandmasterClockIsStopped = 1;
-          if (clock_private_info->identical_previous_preciseOriginTimestamp_count == 8 * 60) {
-            int64_t duration_of_mastership =
-                reception_time - clock_private_info->mastership_start_time;
-            if (clock_private_info->mastership_start_time == 0)
-              duration_of_mastership = 0;
+      // debug_print_buffer(1, buf, recv_len);
+      struct ptp_follow_up_message *msg = (struct ptp_follow_up_message *)buf;
+      uint16_t seconds_hi = nctohs(&msg->follow_up.preciseOriginTimestamp[0]);
+      uint32_t seconds_low = nctohl(&msg->follow_up.preciseOriginTimestamp[2]);
+      uint32_t nanoseconds = nctohl(&msg->follow_up.preciseOriginTimestamp[6]);
+      uint64_t preciseOriginTimestamp = seconds_hi;
+      preciseOriginTimestamp = preciseOriginTimestamp << 32;
+      preciseOriginTimestamp = preciseOriginTimestamp + seconds_low;
+      preciseOriginTimestamp = preciseOriginTimestamp * 1000000000L;
+      preciseOriginTimestamp = preciseOriginTimestamp + nanoseconds;
+
+      // update our sample information
+      int grandmasterClockIsStopped = 0;
+      if ((clock_private_info->previous_preciseOriginTimestamp == preciseOriginTimestamp) &&
+          (clock_private_info->clock_id == clock_private_info->grandmasterIdentity)) {
+        clock_private_info->identical_previous_preciseOriginTimestamp_count++;
+        grandmasterClockIsStopped = 1;
+        if (clock_private_info->identical_previous_preciseOriginTimestamp_count == 8 * 60) {
+          int64_t duration_of_mastership =
+              reception_time - clock_private_info->mastership_start_time;
+          if (clock_private_info->mastership_start_time == 0)
+            duration_of_mastership = 0;
+          debug(2,
+                "Clock %" PRIx64 "'s grandmaster clock has stopped after %f seconds of mastership.",
+                clock_private_info->clock_id, 0.000000001 * duration_of_mastership);
+          int64_t wait_limit = 62;
+          wait_limit = wait_limit * 1000000000;
+          // only try to restart a grandmaster clock on the clock itself.
+          if ((duration_of_mastership <= wait_limit) &&
+              (clock_private_info->clock_id == clock_private_info->grandmasterIdentity)) {
             debug(2,
-                  "Clock %" PRIx64
-                  "'s grandmaster clock has stopped after %f seconds of mastership.",
-                  clock_private_info->clock_id, 0.000000001 * duration_of_mastership);
-            int64_t wait_limit = 62;
-            wait_limit = wait_limit * 1000000000;
-            // only try to restart a grandmaster clock on the clock itself.
-            if ((duration_of_mastership <= wait_limit) && (clock_private_info->clock_id == clock_private_info->grandmasterIdentity)) {
-              debug(2,
-                    "Attempt to start a stopped clock %" PRIx64
-                    ", at follow_up_number %u at IP %s.",
-                    clock_private_info->clock_id, clock_private_info->follow_up_number,
-                    clock_private_info->ip);
-              send_awakening_announcement_sequence(
-                  clock_private_info->clock_id, clock_private_info->ip, clock_private_info->family,
-                  clock_private_info->grandmasterPriority1,
-                  clock_private_info->grandmasterPriority2);
-            }
+                  "Attempt to start a stopped clock %" PRIx64 ", at follow_up_number %u at IP %s.",
+                  clock_private_info->clock_id, clock_private_info->follow_up_number,
+                  clock_private_info->ip);
+            send_awakening_announcement_sequence(
+                clock_private_info->clock_id, clock_private_info->ip, clock_private_info->family,
+                clock_private_info->grandmasterPriority1, clock_private_info->grandmasterPriority2);
           }
-        } else {
-          clock_private_info->identical_previous_preciseOriginTimestamp_count = 0;
         }
+      } else {
+        clock_private_info->identical_previous_preciseOriginTimestamp_count = 0;
+      }
 
-        clock_private_info->previous_preciseOriginTimestamp = preciseOriginTimestamp;
+      clock_private_info->previous_preciseOriginTimestamp = preciseOriginTimestamp;
 
-        // clang-format off
+      // clang-format off
       
       // actually the precision timestamp needs to be corrected by the Follow_Up Correction_Field contents.
       // According to IEEE Std 802.1AS-2020, paragraph 11.4.4.2.1:
@@ -405,199 +349,185 @@ void handle_follow_up(char *buf, ssize_t recv_len, clock_source_private_data *cl
       including any fractional nanoseconds.
       */
 
-        // clang-format on
+      // clang-format on
 
-        int64_t correction_field = ntoh64(msg->header.correctionField);
+      int64_t correction_field = ntoh64(msg->header.correctionField);
 
-        // debug(1," Check ntoh64: in: %" PRIx64 ", out: %" PRIx64 ".", msg->header.correctionField,
-        // correction_field);
+      // debug(1," Check ntoh64: in: %" PRIx64 ", out: %" PRIx64 ".", msg->header.correctionField,
+      // correction_field);
 
-        correction_field = correction_field / 65536; // might be signed
-        uint64_t correctedPreciseOriginTimestamp = preciseOriginTimestamp + correction_field;
+      correction_field = correction_field / 65536; // might be signed
+      uint64_t correctedPreciseOriginTimestamp = preciseOriginTimestamp + correction_field;
 
-        if (clock_private_info->follow_up_number < 100)
-          clock_private_info->follow_up_number++;
+      if (clock_private_info->follow_up_number < 100)
+        clock_private_info->follow_up_number++;
 
-        // if (clock_private_info->announcements_without_followups < 4) // if we haven't signalled
-        // already
-        clock_private_info->announcements_without_followups = 0; // we've seen a followup
+      // if (clock_private_info->announcements_without_followups < 4) // if we haven't signalled
+      // already
+      clock_private_info->announcements_without_followups = 0; // we've seen a followup
 
-        debug(2, "FOLLOWUP from %" PRIx64 ", %s.", clock_private_info->clock_id,
-              &clock_private_info->ip);
-        uint64_t offset = correctedPreciseOriginTimestamp - reception_time;
+      debug(2, "FOLLOWUP from %" PRIx64 ", %s.", clock_private_info->clock_id,
+            &clock_private_info->ip);
+      uint64_t offset = correctedPreciseOriginTimestamp - reception_time;
 
-        int64_t jitter = 0;
+      int64_t jitter = 0;
 
-        int64_t time_since_previous_offset = 0;
-        uint64_t smoothed_offset = offset;
+      int64_t time_since_previous_offset = 0;
+      uint64_t smoothed_offset = offset;
 
-        // This is a bit hacky.
-        // Basically, the idea is that if the grandmaster has changed, then acceptance checking and
-        // smoothing should start as it it's a new clock. This is because the
-        // correctedPreciseOriginTimestamp, which is part of the data that is being smoothed, refers
-        // to the grandmaster, so when the grandmaster changes any previous calculations are no
-        // longer valid. The hacky bit is to signal this condition by zeroing the
-        // previous_offset_time.
-        if (clock_private_info->previous_offset_grandmaster !=
-            clock_private_info->grandmasterIdentity) {
-          clock_private_info->previous_offset_time = 0;
-          debug(1,"grandmaster has changed from %" PRIx64 " to %" PRIx64 ".", clock_private_info->previous_offset_grandmaster, clock_private_info->grandmasterIdentity);
-        }
+      // This is a bit hacky.
+      // Basically, the idea is that if the grandmaster has changed, then acceptance checking and
+      // smoothing should start as it it's a new clock. This is because the
+      // correctedPreciseOriginTimestamp, which is part of the data that is being smoothed, refers
+      // to the grandmaster, so when the grandmaster changes any previous calculations are no
+      // longer valid. The hacky bit is to signal this condition by zeroing the
+      // previous_offset_time.
+      if (clock_private_info->previous_offset_grandmaster !=
+          clock_private_info->grandmasterIdentity) {
+        clock_private_info->previous_offset_time = 0;
+        if (clock_private_info->previous_offset_grandmaster == 0)
+          debug(1, "grandmaster is %" PRIx64 ".", clock_private_info->grandmasterIdentity);
+        else
+          debug(1, "grandmaster has changed from %" PRIx64 " to %" PRIx64 ".",
+                clock_private_info->previous_offset_grandmaster,
+                clock_private_info->grandmasterIdentity);
+      }
 
-        // Do acceptance checking and smoothing.
-
-        // Positive changes in the offset are much more likely to be
-        // legitimate, since they could only occur due to a shorter
-        // propagation time or less of a delay sending or receiving the packet.
-        // (Actually, this is not quite true --
-        // it is possible that the remote clock could be adjusted forward
-        // and this would increase the offset too.)
-        // Anyway, when the clock is new, we give extra preferential weighting to
-        // positive changes in the offset.
-
-        // If the new offset is greater, by any amount, than the old offset,
-        // or if it is less by up to 10 mS, accept it.
-        // Otherwise, drop it if the last sample was fairly recent
-        // If the last sample was long ago, take this as a discontinuity and
-        // accept it as the start of a new period of mastership.
-
-        // This seems to be quite stable
-        
-        //if (grandmasterClockIsStopped == 0) {
-        if (reset_clock_smoothing == 0) {
-
-          if (clock_private_info->previous_offset_time != 0) {
-            time_since_previous_offset = reception_time - clock_private_info->previous_offset_time;
-            jitter = offset - clock_private_info->previous_offset;
-          }
+      // Do acceptance checking and smoothing.
 
-          // We take any positive or a limited negative jitter as a sync event in
-          // a continuous synchronisation sequence.
-          // This works well with PTP sources that sleep, as when they sleep
-          // their clock stops. When they awaken, the offset from
-          // the local clock to them must be smaller than before, triggering the
-          // timing discontinuity below and allowing an immediate readjustment.
-
-          // The full value of a positive offset jitter is accepted for a
-          // number of follow_ups at the start.
-          // After that, the weight of the jitter is reduced.
-          // Follow-ups don't always come in at 125 ms intervals, especially after a discontinuity
-          // Delays makes the offsets smaller than they should be, which is quickly
-          // allowed for.
-        
-          const int64_t clamping_limit = -2500000; // nanoseconds
-        
-          int64_t mastership_time = reception_time - clock_private_info->mastership_start_time;
-          if (clock_private_info->mastership_start_time == 0)
-            mastership_time = 0;
-          
-//          if ((clock_private_info->previous_offset_time != 0) && (clock_private_info->identical_previous_preciseOriginTimestamp_count <= 1)) {
-          if (clock_private_info->previous_offset_time != 0) {
-            if (jitter < 0) {
-              int64_t clamped_jitter = jitter;
-              if (clamped_jitter < clamping_limit) {
-                clamped_jitter = clamping_limit; // 0 means ignore a clamped value completely
-              }
-              // if (mastership_time < 1000000000) // at the beginning, if jitter is negative
-              //   smoothed_offset = clock_private_info->previous_offset + clamped_jitter / 16;
-              // else
-              
-              // ignore negative jitter for the first 10 seconds
-              if (mastership_time > 1000000000)
-                smoothed_offset = clock_private_info->previous_offset + clamped_jitter / 64; // later, if jitter is negative
-            } else if (mastership_time < 1000000000) { // at the beginning
-              smoothed_offset =
-                  clock_private_info->previous_offset + jitter / 1; // at the beginning, if jitter is positive -- accept positive changes quickly
-            } else {
-              smoothed_offset = clock_private_info->previous_offset + jitter / 64; // later, if jitter is positive
-            }
-          } else {
-            if (clock_private_info->previous_offset_time == 0)
-              debug(2, "Clock %" PRIx64 " record (re)starting at %s.", clock_private_info->clock_id,
-                    clock_private_info->ip);
-            else
-              debug(2,
-                    "Timing discontinuity on clock %" PRIx64
-                    " at %s: time_since_previous_offset: %.3f seconds%s.",
-                    clock_private_info->clock_id, clock_private_info->ip,
-                    0.000000001 * time_since_previous_offset,
-                    grandmasterClockIsStopped != 0 ? ", grandmaster clock stopped" : ""
-                    );
-            smoothed_offset = offset;
-            // clock_private_info->follow_up_number = 0;
-            clock_private_info->mastership_start_time =
-                reception_time; // mastership is reset to this time...          
-          }
+      // Positive changes in the offset are much more likely to be
+      // legitimate, since they could only occur due to a shorter
+      // propagation time or less of a delay sending or receiving the packet.
+      // (Actually, this is not quite true --
+      // it is possible that the remote clock could be adjusted forward
+      // and this would increase the offset too.)
+      // Anyway, when the clock is new, we give extra preferential weighting to
+      // positive changes in the offset.
 
-          int temp_client_id;
-          for (temp_client_id = 0; temp_client_id < MAX_CLIENTS; temp_client_id++) {
-            if ((clock_private_info->client_flags[temp_client_id] & (1 << clock_is_master)) != 0) {
-              // recalculate mastership_time because it might have been zeroed.
-              mastership_time = reception_time - clock_private_info->mastership_start_time;
-              if (clock_private_info->mastership_start_time == 0)
-                mastership_time = 0;
-              if (mastership_time > 200000000) {
-                int64_t delta = smoothed_offset - offset;
-                debug(2,
-                      "Clock %" PRIx64 ", grandmaster %" PRIx64 ". Offset: %" PRIx64
-                      ", smoothed offset: %" PRIx64 ". Smoothed Offset - Offset: %10.3f. Raw Precise Origin Timestamp: %" PRIx64
-                      ". Time since previous offset: %8.3f milliseconds. ID: %5u, Follow_Up Number: "
-                      "%u. Source: %s",
-                      clock_private_info->clock_id, clock_private_info->grandmasterIdentity, offset,
-                      smoothed_offset, 0.000001 * delta, preciseOriginTimestamp, 0.000001 * time_since_previous_offset,
-                      ntohs(msg->header.sequenceId), clock_private_info->follow_up_number,
-                      clock_private_info->ip);
-
-                debug(2, "clock_is_master -- updating master clock info for client \"%s\"",
-                      get_client_name(temp_client_id));
-                update_master_clock_info(temp_client_id, clock_private_info->grandmasterIdentity,
-                                         (const char *)&clock_private_info->ip, reception_time,
-                                         smoothed_offset, clock_private_info->mastership_start_time);
-              }
+      // If the new offset is greater, by any amount, than the old offset,
+      // or if it is less by up to 10 mS, accept it.
+      // Otherwise, drop it if the last sample was fairly recent
+      // If the last sample was long ago, take this as a discontinuity and
+      // accept it as the start of a new period of mastership.
+
+      // This seems to be quite stable
+
+      // if (grandmasterClockIsStopped == 0) {
+      if (reset_clock_smoothing == 0) {
+
+        if (clock_private_info->previous_offset_time != 0) {
+          time_since_previous_offset = reception_time - clock_private_info->previous_offset_time;
+          jitter = offset - clock_private_info->previous_offset;
+        }
+
+        // We take any positive or a limited negative jitter as a sync event in
+        // a continuous synchronisation sequence.
+
+        // The full value of a positive offset jitter is accepted for a
+        // number of follow_ups at the start.
+        // After that, the weight of the jitter is reduced.
+        // Follow-ups don't always come in at 125 ms intervals, especially after a discontinuity
+        // Delays makes the offsets smaller than they should be, which is quickly
+        // allowed for.
+
+        const int64_t clamping_limit = -2500000; // nanoseconds
+
+        int64_t mastership_time = reception_time - clock_private_info->mastership_start_time;
+        if (clock_private_info->mastership_start_time == 0)
+          mastership_time = 0;
+
+        //          if ((clock_private_info->previous_offset_time != 0) &&
+        //          (clock_private_info->identical_previous_preciseOriginTimestamp_count <= 1)) {
+        if (clock_private_info->previous_offset_time != 0) {
+          if (jitter < 0) {
+            int64_t clamped_jitter = jitter;
+            if (clamped_jitter < clamping_limit) {
+              clamped_jitter = clamping_limit; // 0 means ignore a clamped value completely
             }
+            // if (mastership_time < 1000000000) // at the beginning, if jitter is negative
+            //   smoothed_offset = clock_private_info->previous_offset + clamped_jitter / 16;
+            // else
+
+            // ignore negative jitter for the first 10 seconds
+            if (mastership_time > 1000000000)
+              smoothed_offset = clock_private_info->previous_offset +
+                                clamped_jitter / 64; // later, if jitter is negative
+          } else if (mastership_time < 1000000000) { // at the beginning
+            smoothed_offset =
+                clock_private_info->previous_offset +
+                jitter /
+                    1; // at the beginning, if jitter is positive -- accept positive changes quickly
+          } else {
+            smoothed_offset =
+                clock_private_info->previous_offset + jitter / 64; // later, if jitter is positive
           }
+        } else {
+          if (clock_private_info->previous_offset_time == 0)
+            debug(2, "Clock %" PRIx64 " record (re)starting at %s.", clock_private_info->clock_id,
+                  clock_private_info->ip);
+          else
+            debug(2,
+                  "Timing discontinuity on clock %" PRIx64
+                  " at %s: time_since_previous_offset: %.3f seconds%s.",
+                  clock_private_info->clock_id, clock_private_info->ip,
+                  0.000000001 * time_since_previous_offset,
+                  grandmasterClockIsStopped != 0 ? ", grandmaster clock stopped" : "");
+          smoothed_offset = offset;
+          // clock_private_info->follow_up_number = 0;
+          clock_private_info->mastership_start_time =
+              reception_time; // mastership is reset to this time...
+        }
 
-          int64_t delta = smoothed_offset - offset;
-          debug(2,
-                "Clock %" PRIx64 ", grandmaster %" PRIx64 ". Offset: %" PRIx64
-                ", smoothed offset: %" PRIx64 ". Smoothed Offset - Offset: %10.3f. Raw Precise Origin Timestamp: %" PRIx64
-                "%s correction_field: %" PRIx64
-                ". Time since previous offset: %8.3f milliseconds. ID: %5u, Follow_Up Number: "
-                "%u. Source: %s",
-                clock_private_info->clock_id, clock_private_info->grandmasterIdentity, offset,
-                smoothed_offset, 0.000001 * delta, preciseOriginTimestamp,
-                grandmasterClockIsStopped == 0 ? ". " : "*.",
-                correction_field,
-                0.000001 * time_since_previous_offset,
-                ntohs(msg->header.sequenceId), clock_private_info->follow_up_number,
-                clock_private_info->ip);
-
-          new_update_master_clock_info(clock_private_info->grandmasterIdentity,
-                           (const char *)&clock_private_info->ip, reception_time,
-                           smoothed_offset, clock_private_info->mastership_start_time);
-        
-          clock_private_info->previous_offset = smoothed_offset;
-          clock_private_info->previous_offset_time = reception_time;
-        
+        int64_t delta = smoothed_offset - offset;
+        debug(2,
+              "Clock %" PRIx64 ", grandmaster %" PRIx64 ". Offset: %" PRIx64
+              ", smoothed offset: %" PRIx64
+              ". Smoothed Offset - Offset: %10.3f. Raw Precise Origin Timestamp: %" PRIx64
+              "%s correction_field: %" PRIx64
+              ". Time since previous offset: %8.3f milliseconds. ID: %5u, Follow_Up Number: "
+              "%u. Source: %s",
+              clock_private_info->clock_id, clock_private_info->grandmasterIdentity, offset,
+              smoothed_offset, 0.000001 * delta, preciseOriginTimestamp,
+              grandmasterClockIsStopped == 0 ? ". " : "*.", correction_field,
+              0.000001 * time_since_previous_offset, ntohs(msg->header.sequenceId),
+              clock_private_info->follow_up_number, clock_private_info->ip);
+        if (clock_is_active) {
+          update_master_clock_info(clock_private_info->grandmasterIdentity,
+                                   (const char *)&clock_private_info->ip, reception_time,
+                                   smoothed_offset, clock_private_info->mastership_start_time);
         } else {
-          reset_clock_smoothing = 0;
-          clock_private_info->mastership_start_time = 0;
-          clock_private_info->previous_offset = 0;
-          clock_private_info->previous_offset_time = 0; // so that the first non-stopped sample will be taken as the first one in a sequence
+          update_master_clock_info(0, NULL, 0, 0, 0); // the SMI may have obsolete stuff in it
         }
 
-        clock_private_info->previous_offset_grandmaster = clock_private_info->grandmasterIdentity;
-
-       // now do some quick calculations on the possible "Universal Time"
-        // debug_print_buffer(1, "", buf, recv_len);
-        uint8_t *tlv = (uint8_t *)&msg->follow_up.tlvs[0];
-        uint8_t *lastGmPhaseChange = tlv + 16;
-        uint64_t lpt = nctoh64(lastGmPhaseChange + 4);
-        uint64_t last_tlv_clock = nctoh64((uint8_t *)buf + 86);
-        uint64_t huh = offset - lpt;
-        debug_print_buffer(2, buf, (size_t) recv_len);
-        debug(2, "%" PRIx64 ", %" PRIx64 ", %s, Origin: %016" PRIx64 ", LPT: %016" PRIx64 ", Offset: %016" PRIx64 ", Universal Offset: %016" PRIx64 ", packet length: %u.", clock_private_info->clock_id, last_tlv_clock,  hex_string(lastGmPhaseChange,12), preciseOriginTimestamp, lpt, offset, huh, recv_len);
-        // debug(1,"Clock: %" PRIx64 ", UT: %016" PRIx64 ", correctedPOT: %016" PRIx64 ", part of lastGMPhaseChange: %016" PRIx64 ".", packet_clock_id, correctedPOT - lpt, correctedPOT, lpt);
+        clock_private_info->previous_offset = smoothed_offset;
+        clock_private_info->previous_offset_time = reception_time;
+
+      } else {
+        reset_clock_smoothing = 0;
+        clock_private_info->mastership_start_time = 0;
+        clock_private_info->previous_offset = 0;
+        clock_private_info->previous_offset_time =
+            0; // so that the first non-stopped sample will be taken as the first one in a sequence
+      }
+
+      clock_private_info->previous_offset_grandmaster = clock_private_info->grandmasterIdentity;
+
+      // now do some quick calculations on the possible "Universal Time"
+      // debug_print_buffer(1, "", buf, recv_len);
+      uint8_t *tlv = (uint8_t *)&msg->follow_up.tlvs[0];
+      uint8_t *lastGmPhaseChange = tlv + 16;
+      uint64_t lpt = nctoh64(lastGmPhaseChange + 4);
+      uint64_t last_tlv_clock = nctoh64((uint8_t *)buf + 86);
+      uint64_t huh = offset - lpt;
+      debug_print_buffer(2, buf, (size_t)recv_len);
+      debug(2,
+            "%" PRIx64 ", %" PRIx64 ", %s, Origin: %016" PRIx64 ", LPT: %016" PRIx64
+            ", Offset: %016" PRIx64 ", Universal Offset: %016" PRIx64 ", packet length: %u.",
+            clock_private_info->clock_id, last_tlv_clock, hex_string(lastGmPhaseChange, 12),
+            preciseOriginTimestamp, lpt, offset, huh, recv_len);
+      // debug(1,"Clock: %" PRIx64 ", UT: %016" PRIx64 ", correctedPOT: %016" PRIx64 ", part of
+      // lastGMPhaseChange: %016" PRIx64 ".", packet_clock_id, correctedPOT - lpt, correctedPOT,
+      // lpt);
 
     } else {
       debug(1, "Follow_Up message is too small to be valid.");
index fbe143061a3c693a76e3fe86c46780a4d3ff8747..b39a1362f86e89fdf9cceb78aef86062c606de48 100644 (file)
@@ -145,16 +145,16 @@ struct __attribute__((__packed__)) ptp_common_message_header {
   uint8_t transportSpecificAndMessageID; // 0x11
   uint8_t reservedAndVersionPTP;         // 0x02
   uint16_t messageLength;
-  uint8_t domainNumber;        // 0
-  uint8_t reserved_b;          // 0
-  uint16_t flags;              // 0x0608
-  uint64_t correctionField;    // 0
-  uint32_t reserved_l;         // 0
-  uint8_t clockIdentity[8];    // MAC
-  uint16_t sourcePortID;       // 1
-  uint16_t sequenceId;         // increments
-  uint8_t controlField; // 5
-  uint8_t logMessagePeriod;    // 0
+  uint8_t domainNumber;     // 0
+  uint8_t reserved_b;       // 0
+  uint16_t flags;           // 0x0608
+  uint64_t correctionField; // 0
+  uint32_t reserved_l;      // 0
+  uint8_t clockIdentity[8]; // MAC
+  uint16_t sourcePortID;    // 1
+  uint16_t sequenceId;      // increments
+  uint8_t controlField;     // 5
+  uint8_t logMessagePeriod; // 0
 };
 
 // this is the extra part for an Announce message
index c3a5c7a623f28eb2bdbec4789d71f8f3e06b91fd..a28c9cea6c3a9822660896f46249bab954f538c7 100644 (file)
 #endif
 
 #ifdef CONFIG_FOR_FREEBSD
-#include <unistd.h>
-#include <sys/types.h>
 #include <net/if_dl.h>
 #include <net/if_types.h>
 #include <sys/socket.h>
+#include <sys/types.h>
+#include <unistd.h>
 #endif
 
 #include <netdb.h>  // getaddrinfo etc.
@@ -167,7 +167,7 @@ int get_device_id(uint8_t *id, int *int_length) {
   struct ifaddrs *ifa = NULL;
   int i = 0;
   uint8_t *t = id;
-  
+
   // clear the buffer if non zero length passed in
   for (i = 0; i < max_length; i++) {
     *t++ = 0;
@@ -177,7 +177,7 @@ int get_device_id(uint8_t *id, int *int_length) {
   if (getifaddrs(&ifaddr) != -1) {
     t = id;
     int found = 0;
-    
+
     for (ifa = ifaddr; ((ifa != NULL) && (found == 0)); ifa = ifa->ifa_next) {
 #ifdef AF_PACKET
       if ((ifa->ifa_addr) && (ifa->ifa_addr->sa_family == AF_PACKET)) {
@@ -211,7 +211,6 @@ int get_device_id(uint8_t *id, int *int_length) {
       }
 #endif
 #endif
-
     }
     if (found != 0)
       response = 0;
@@ -220,13 +219,12 @@ int get_device_id(uint8_t *id, int *int_length) {
   return response;
 }
 
-
 uint64_t get_self_clock_id() {
   // make up a clock ID based on an interface's MAC
   int local_clock_id_size = 8; // don't exceed this
   uint8_t local_clock_id[local_clock_id_size];
-  memset(local_clock_id,0,local_clock_id_size);
-  if (get_device_id(local_clock_id,&local_clock_id_size) == 0) {
+  memset(local_clock_id, 0, local_clock_id_size);
+  if (get_device_id(local_clock_id, &local_clock_id_size) == 0) {
     // if the length of the MAC address is 6 we need to doctor it a little
     // See Section 7.5.2.2.2 IEEE EUI-64 clockIdentity values, NOTE 2
 
diff --git a/nqptp.c b/nqptp.c
index 4981cf01eaa3d4a350bfc9a13b7ed0b15a2ee855..d7da8bb3af950ad1b549ac92dde19edb35241e13 100644 (file)
--- a/nqptp.c
+++ b/nqptp.c
@@ -89,11 +89,10 @@ void goodbye(void) {
   unsigned int i;
   for (i = 0; i < sockets_open_stuff.sockets_open; i++)
     close(sockets_open_stuff.sockets[i].number);
-    
 
   // close off shared memory interface
   delete_clients();
-  
+
   // close off new smi
   // mmap cleanup
   if (munmap(shared_memory, sizeof(struct shm_structure)) != 0) {
@@ -103,7 +102,7 @@ void goodbye(void) {
   if (shm_unlink(NQPTP_INTERFACE_NAME) == -1) {
     debug(1, "error unlinking shared memory \"%s\"", NQPTP_INTERFACE_NAME);
   }
-  
+
   if (shm_fd != -1)
     close(shm_fd);
 
@@ -159,18 +158,18 @@ int main(int argc, char **argv) {
       }
     }
   }
-  
+
   debug_init(debug_level, 0, 1, 1);
-  
+
 #ifdef CONFIG_USE_GIT_VERSION_STRING
   if (git_version_string[0] != '\0')
     debug(1, "Version: %s, SMI: %u. Clock ID: \"%" PRIx64 "\".", git_version_string,
-            NQPTP_SHM_STRUCTURES_VERSION, get_self_clock_id());
+          NQPTP_SHM_STRUCTURES_VERSION, get_self_clock_id());
   else
 #endif
     debug(1, "Version: %s, SMI: %u. Clock ID: \"%" PRIx64 "\".", VERSION,
-            NQPTP_SHM_STRUCTURES_VERSION, get_self_clock_id());
-  
+          NQPTP_SHM_STRUCTURES_VERSION, get_self_clock_id());
+
   // debug(1, "size of a clock entry is %u bytes.", sizeof(clock_source_private_data));
   atexit(goodbye);
 
@@ -189,7 +188,7 @@ int main(int argc, char **argv) {
   memset(&act2, 0, sizeof(struct sigaction));
   act2.sa_handler = termHandler;
   sigaction(SIGTERM, &act2, NULL);
-  
+
   // open the SMI
 
   pthread_mutexattr_t shared;
@@ -209,9 +208,8 @@ int main(int argc, char **argv) {
   }
 
 #ifdef CONFIG_FOR_FREEBSD
-  shared_memory =
-      (struct shm_structure *)mmap(NULL, sizeof(struct shm_structure), PROT_READ | PROT_WRITE,
-                                   MAP_SHARED, shm_fd, 0);
+  shared_memory = (struct shm_structure *)mmap(NULL, sizeof(struct shm_structure),
+                                               PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
 #endif
 
 #ifdef CONFIG_FOR_LINUX
@@ -338,8 +336,8 @@ int main(int argc, char **argv) {
               // check if it's a control port message before checking for the length of the
               // message.
             } else if (receiver_port == NQPTP_CONTROL_PORT) {
-              handle_control_port_messages(buf, recv_len,
-                                           (clock_source_private_data *)&clocks_private, reception_time);
+              handle_control_port_messages(
+                  buf, recv_len, (clock_source_private_data *)&clocks_private, reception_time);
             } else if (recv_len >= (ssize_t)sizeof(struct ptp_common_message_header)) {
               debug_print_buffer(2, buf, recv_len);
 
@@ -405,7 +403,7 @@ int main(int argc, char **argv) {
           }
         }
       }
-      //if (retval >= 0)
+      // if (retval >= 0)
       //  manage_clock_sources(reception_time, (clock_source_private_data *)&clocks_private);
       int i;
       for (i = 0; i < TIMED_TASKS; i++) {
@@ -520,30 +518,32 @@ uint64_t broadcasting_task(uint64_t call_time, __attribute__((unused)) void *pri
   int i;
   for (i = 0; i < MAX_CLOCKS; i++) {
 
-    int is_a_master = 0;
-    int temp_client_id;
+    /*
+        int is_a_master = 0;
+        int temp_client_id;
+
+        for (temp_client_id = 0; temp_client_id < MAX_CLIENTS; temp_client_id++)
+          if ((clocks_private->client_flags[temp_client_id] & (1 << clock_is_master)) != 0)
+            is_a_master = 1;
+        // only process it if it's a master somewhere...
+        if ((is_a_master != 0) && (clocks_private[i].announcements_without_followups == 3)) {
+    */
+    if (clocks_private[i].announcements_without_followups == 3) {
+      if (clocks_private[i].follow_up_number == 0) {
+        debug(1,
+              "Attempt to awaken a silent clock %" PRIx64
+              ", index %u, at follow_up_number %u at IP %s.",
+              clocks_private[i].clock_id, i, clocks_private[i].follow_up_number,
+              clocks_private[i].ip);
 
-    for (temp_client_id = 0; temp_client_id < MAX_CLIENTS; temp_client_id++)
-      if ((clocks_private->client_flags[temp_client_id] & (1 << clock_is_master)) != 0)
-        is_a_master = 1;
+        // send an Announce message to attempt to waken this silent PTP clock by
+        // getting it to negotiate with an apparently better clock
+        // that then immediately sends another Announce message indicating that it's inferior
 
-    // only process it if it's a master somewhere...
-    if ((is_a_master != 0) && (clocks_private[i].announcements_without_followups == 3)) {
-      if (clocks_private[i].follow_up_number == 0) {
-      debug(1,
-            "Attempt to awaken a silent clock %" PRIx64
-            ", index %u, at follow_up_number %u at IP %s.",
-            clocks_private[i].clock_id, i, clocks_private[i].follow_up_number,
-            clocks_private[i].ip);
-
-      // send an Announce message to attempt to waken this silent PTP clock by
-      // getting it to negotiate with an apparently better clock
-      // that then immediately sends another Announce message indicating that it's inferior
-
-      clocks_private[i].announcements_without_followups++; // set to 4 to indicate done/parked
-      send_awakening_announcement_sequence(
-          clocks_private[i].clock_id, clocks_private[i].ip, clocks_private[i].family,
-          clocks_private[i].grandmasterPriority1, clocks_private[i].grandmasterPriority2);
+        clocks_private[i].announcements_without_followups++; // set to 4 to indicate done/parked
+        send_awakening_announcement_sequence(
+            clocks_private[i].clock_id, clocks_private[i].ip, clocks_private[i].family,
+            clocks_private[i].grandmasterPriority1, clocks_private[i].grandmasterPriority2);
       } else {
         debug(1,
               "Silent clock %" PRIx64