*
* Commercial licensing is also available.
*/
-#include <string.h>
#include <errno.h>
+#include <string.h>
#include <unistd.h>
+#include "debug.h"
+#include "general-utilities.h"
#include "nqptp-message-handlers.h"
#include "nqptp-ptp-definitions.h"
#include "nqptp-utilities.h"
-#include "debug.h"
-#include "general-utilities.h"
void update_master_old(clock_source_private_data *clock_private_info) {
int old_master = -1;
}
void handle_sync(char *buf, __attribute__((unused)) ssize_t recv_len,
- clock_source_private_data *clock_private_info, uint64_t reception_time, SOCKADDR *from_sock_addr, int socket_number) {
+ clock_source_private_data *clock_private_info, uint64_t reception_time,
+ SOCKADDR *from_sock_addr, int socket_number) {
struct ptp_sync_message *msg = (struct ptp_sync_message *)buf;
// this is just to see if anything interesting comes in the SYNC package
// a non-zero origin timestamp
struct ptp_delay_req_message m;
memset(&m, 0, sizeof(m));
m.header.transportSpecificAndMessageID = 0x11; // Table 19, pp 125, 1 byte field
- m.header.reservedAndVersionPTP = 0x02; // 1 byte field
+ m.header.reservedAndVersionPTP = 0x02; // 1 byte field
m.header.messageLength = htons(44);
m.header.flags = htons(0x608);
m.header.sourcePortID = htons(1);
m.header.controlOtherMessage = 5; // 1 byte field
m.header.sequenceId = htons(clock_private_info->sequence_number);
uint64_t sid = get_self_clock_id();
- memcpy(&m.header.clockIdentity,&sid,sizeof(uint64_t));
+ memcpy(&m.header.clockIdentity, &sid, sizeof(uint64_t));
struct msghdr header;
struct iovec io;
memset(&header, 0, sizeof(header));
header.msg_iovlen = 1;
clock_private_info->t3 = get_time_now(); // in case nothing better works
if ((sendmsg(socket_number, &header, 0)) == -1) {
- //debug(1, "Error in sendmsg [errno = %d] to socket %d.", errno, socket_number);
- //debug_print_buffer(1,(char *)&m, sizeof(m));
+ // debug(1, "Error in sendmsg [errno = %d] to socket %d.", errno, socket_number);
+ // debug_print_buffer(1,(char *)&m, sizeof(m));
} else {
- //debug(1, "Success in sendmsg to socket %d.", socket_number);
+ // debug(1, "Success in sendmsg to socket %d.", socket_number);
}
}
}
preciseOriginTimestamp = preciseOriginTimestamp * 1000000000L;
preciseOriginTimestamp = preciseOriginTimestamp + nanoseconds;
-
// this result is called "t1" in the IEEE spec.
clock_private_info->t1 = preciseOriginTimestamp;
clock_private_info->current_stage = follow_up_seen;
-
} else {
debug(3,
"Follow_Up %u expecting to be in state sync_seen (%u). Stage error -- "
}
void handle_delay_resp(char *buf, __attribute__((unused)) 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) {
struct ptp_delay_resp_message *msg = (struct ptp_delay_resp_message *)buf;
if ((clock_private_info->current_stage == follow_up_seen) &&
// this is t4 in the IEEE doc and should be close to t1
// on some systems, it is identical to t1.
- //uint64_t delay_req_turnaround_time = reception_time - clock_private_info->t3;
+ // uint64_t delay_req_turnaround_time = reception_time - clock_private_info->t3;
uint64_t t4t1diff = receiveTimestamp - clock_private_info->t1;
- //uint64_t t3t2diff = clock_private_info->t3 - clock_private_info->t2;
- //debug(1,"t4t1diff: %f, delay_req_turnaround_time: %f, t3t2diff: %f.", t4t1diff * 0.000000001, delay_req_turnaround_time * 0.000000001, t3t2diff * 0.000000001);
-
+ // uint64_t t3t2diff = clock_private_info->t3 - clock_private_info->t2;
+ // debug(1,"t4t1diff: %f, delay_req_turnaround_time: %f, t3t2diff: %f.", t4t1diff * 0.000000001,
+ // delay_req_turnaround_time * 0.000000001, t3t2diff * 0.000000001);
if (t4t1diff < 20000000) {
- // update the shared clock information
- uint64_t offset = clock_private_info->t1 - clock_private_info->t2;
-
- // update our sample information
-
- clock_private_info->samples[clock_private_info->next_sample_goes_here].local =
- clock_private_info->t2; // this is when the Sync message arrived.
- clock_private_info->samples[clock_private_info->next_sample_goes_here].local_to_remote_offset =
- offset;
- clock_private_info->samples[clock_private_info->next_sample_goes_here].sequence_number =
- clock_private_info->sequence_number;
-
- // if this is the very first...
- if (clock_private_info->vacant_samples == MAX_TIMING_SAMPLES) {
- clock_private_info->previous_offset = offset;
- clock_private_info->previous_estimated_offset = offset;
- }
+ // update the shared clock information
+ uint64_t offset = clock_private_info->t1 - clock_private_info->t2;
+
+ // update our sample information
+
+ clock_private_info->samples[clock_private_info->next_sample_goes_here].local =
+ clock_private_info->t2; // this is when the Sync message arrived.
+ clock_private_info->samples[clock_private_info->next_sample_goes_here]
+ .local_to_remote_offset = offset;
+ clock_private_info->samples[clock_private_info->next_sample_goes_here].sequence_number =
+ clock_private_info->sequence_number;
+
+ // if this is the very first...
+ if (clock_private_info->vacant_samples == MAX_TIMING_SAMPLES) {
+ clock_private_info->previous_offset = offset;
+ clock_private_info->previous_estimated_offset = offset;
+ }
- if (clock_private_info->vacant_samples > 0)
- clock_private_info->vacant_samples--;
+ if (clock_private_info->vacant_samples > 0)
+ clock_private_info->vacant_samples--;
- int sample_count = MAX_TIMING_SAMPLES - clock_private_info->vacant_samples;
- int64_t divergence = 0;
- uint64_t estimated_offset = offset;
+ int sample_count = MAX_TIMING_SAMPLES - clock_private_info->vacant_samples;
+ int64_t divergence = 0;
+ uint64_t estimated_offset = offset;
+ if (sample_count > 1) {
+ int e;
+ long double offsets = 0;
+ for (e = 0; e < sample_count; e++) {
+ uint64_t ho = clock_private_info->samples[e].local_to_remote_offset;
- if (sample_count > 1) {
- int e;
- long double offsets = 0;
- for (e = 0; e < sample_count; e++) {
- uint64_t ho = clock_private_info->samples[e].local_to_remote_offset;
+ offsets = offsets + 1.0 * ho;
+ }
- offsets = offsets + 1.0 * ho;
+ offsets = offsets / sample_count;
+ estimated_offset = (uint64_t)offsets;
}
- offsets = offsets / sample_count;
- estimated_offset = (uint64_t)offsets;
- }
+ clock_private_info->previous_estimated_offset = estimated_offset;
- clock_private_info->previous_estimated_offset = estimated_offset;
+ clock_private_info->clock_id = packet_clock_id;
+ clock_private_info->flags |= (1 << clock_is_valid);
+ clock_private_info->local_time = clock_private_info->t2;
+ clock_private_info->local_to_source_time_offset = estimated_offset;
- clock_private_info->clock_id = packet_clock_id;
- clock_private_info->flags |= (1 << clock_is_valid);
- clock_private_info->local_time = clock_private_info->t2;
- clock_private_info->local_to_source_time_offset = estimated_offset;
-
- if ((clock_private_info->flags & (1 << clock_is_master)) != 0) {
- update_master_clock_info(clock_private_info->clock_id, clock_private_info->local_time,
- clock_private_info->local_to_source_time_offset);
- }
+ if ((clock_private_info->flags & (1 << clock_is_master)) != 0) {
+ update_master_clock_info(clock_private_info->clock_id, clock_private_info->local_time,
+ clock_private_info->local_to_source_time_offset);
+ }
- clock_private_info->next_sample_goes_here++;
+ clock_private_info->next_sample_goes_here++;
- // if we have need to wrap.
- if (clock_private_info->next_sample_goes_here == MAX_TIMING_SAMPLES)
- clock_private_info->next_sample_goes_here = 0;
+ // if we have need to wrap.
+ if (clock_private_info->next_sample_goes_here == MAX_TIMING_SAMPLES)
+ clock_private_info->next_sample_goes_here = 0;
} else {
- debug(2,"Dropping an apparently slow timing exchange with a disparity of %f milliseconds on clock: %" PRIx64 ".", t4t1diff * 0.000001, clock_private_info->clock_id);
+ debug(2,
+ "Dropping an apparently slow timing exchange with a disparity of %f milliseconds on "
+ "clock: %" PRIx64 ".",
+ t4t1diff * 0.000001, clock_private_info->clock_id);
}
- } else {
+ } else {
debug(3,
"Delay_Resp %u expecting to be in state follow_up_seen (%u). Stage error -- "
"current state is %u, sequence %u. Ignoring it. %s",