Skip to content

Commit 9652679

Browse files
theadibfabiobaltieri
authored andcommitted
ptp: clock.c: revise offset calculation
cast differences to signed int to allow negative numbers Signed-off-by: Adib Taraben <[email protected]>
1 parent 5a2e612 commit 9652679

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

subsys/net/lib/ptp/clock.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ int ptp_clock_management_msg_process(struct ptp_port *port, struct ptp_msg *msg)
509509
void ptp_clock_synchronize(uint64_t ingress, uint64_t egress)
510510
{
511511
int64_t offset;
512-
uint64_t delay = ptp_clk.current_ds.mean_delay >> 16;
512+
int64_t delay = ptp_clk.current_ds.mean_delay >> 16;
513513

514514
ptp_clk.timestamp.t1 = egress;
515515
ptp_clk.timestamp.t2 = ingress;
@@ -518,7 +518,7 @@ void ptp_clock_synchronize(uint64_t ingress, uint64_t egress)
518518
return;
519519
}
520520

521-
offset = ptp_clk.timestamp.t2 - ptp_clk.timestamp.t1 - delay;
521+
offset = (int64_t)(ptp_clk.timestamp.t2 - ptp_clk.timestamp.t1) - delay;
522522

523523
/* If diff is too big, ptp_clk needs to be set first. */
524524
if ((offset > (int64_t)NSEC_PER_SEC) || (offset < -(int64_t)NSEC_PER_SEC)) {
@@ -559,8 +559,9 @@ void ptp_clock_delay(uint64_t egress, uint64_t ingress)
559559
ptp_clk.timestamp.t3 = egress;
560560
ptp_clk.timestamp.t4 = ingress;
561561

562-
delay = ((ptp_clk.timestamp.t2 - ptp_clk.timestamp.t3) +
563-
(ptp_clk.timestamp.t4 - ptp_clk.timestamp.t1)) / 2;
562+
delay = ((int64_t)(ptp_clk.timestamp.t2 - ptp_clk.timestamp.t3) +
563+
(int64_t)(ptp_clk.timestamp.t4 - ptp_clk.timestamp.t1)) /
564+
2LL;
564565

565566
LOG_DBG("Delay %lldns", delay);
566567
ptp_clk.current_ds.mean_delay = clock_ns_to_timeinterval(delay);

0 commit comments

Comments
 (0)