Commit 6d7bf7e3 authored by Alessandro Rubini's avatar Alessandro Rubini

timeout: add helpers for next delay

This kills the ambiguously-named pp_ms_to_timeout(), and
adds pp_next_delay_{1,2,3}.

This commit meanwhile fixes the timeouts in the pdelay master (who didn't
consider the request timeout), and in the pdelay listener (because
LISTEN mode must send pdelay requests).
Signed-off-by: Alessandro Rubini's avatarAlessandro Rubini <rubini@gnudd.com>
parent 94d45b3c
......@@ -227,7 +227,9 @@ extern void pp_timeout_set(struct pp_instance *ppi, int index);
extern void pp_timeout_setall(struct pp_instance *ppi);
extern int pp_timeout(struct pp_instance *ppi, int index)
__attribute__((warn_unused_result));
extern int pp_ms_to_timeout(struct pp_instance *ppi, int index);
extern int pp_next_delay_1(struct pp_instance *ppi, int i1);
extern int pp_next_delay_2(struct pp_instance *ppi, int i1, int i2);
extern int pp_next_delay_3(struct pp_instance *ppi, int i1, int i2, int i3);
/* The channel for an instance must be created and possibly destroyed. */
extern int pp_init_globals(struct pp_globals *ppg, struct pp_runtime_opts *opts);
......
......@@ -19,6 +19,6 @@ int pp_faulty(struct pp_instance *ppi, unsigned char *pkt, int plen)
ppi->next_state = PPS_INITIALIZING;
return 0;
}
ppi->next_delay = pp_ms_to_timeout(ppi, PP_TO_FAULTY);
ppi->next_delay = pp_next_delay_1(ppi, PP_TO_FAULTY);
return 0;
}
......@@ -90,7 +90,7 @@ out:
if (e != 0)
ppi->next_state = PPS_FAULTY;
ppi->next_delay = pp_ms_to_timeout(ppi, PP_TO_ANN_RECEIPT);
ppi->next_delay = pp_next_delay_1(ppi, PP_TO_ANN_RECEIPT);
return 0;
}
......@@ -11,7 +11,7 @@
int pp_master(struct pp_instance *ppi, unsigned char *pkt, int plen)
{
int msgtype, d1, d2;
int msgtype;
int e = 0; /* error var, to check errors in msg handling */
MsgHeader *hdr = &ppi->received_ptp_header;
MsgPDelayRespFollowUp respFllw;
......@@ -23,6 +23,8 @@ int pp_master(struct pp_instance *ppi, unsigned char *pkt, int plen)
/* when the clock is using peer-delay, the muster mast send it too */
if (ppi->glbs->delay_mech == PP_P2P_MECH)
pp_lib_may_issue_request(ppi);
else
pp_timeout_set(ppi, PP_TO_REQUEST);
if (plen == 0)
goto out;
......@@ -119,9 +121,8 @@ out:
break;
}
d1 = pp_ms_to_timeout(ppi, PP_TO_ANN_SEND);
d2 = pp_ms_to_timeout(ppi, PP_TO_SYNC_SEND);
ppi->next_delay = d1 < d2 ? d1 : d2;
ppi->next_delay = pp_next_delay_3(ppi,
PP_TO_ANN_SEND, PP_TO_SYNC_SEND, PP_TO_REQUEST);
return e;
out_fault:
......
......@@ -17,7 +17,6 @@ int pp_slave(struct pp_instance *ppi, unsigned char *pkt, int plen)
MsgHeader *hdr = &ppi->received_ptp_header;
MsgDelayResp resp;
MsgPDelayRespFollowUp respFllw;
int d1, d2;
if (ppi->is_new_state) {
memset(&ppi->t1, 0, sizeof(ppi->t1));
......@@ -157,9 +156,7 @@ out:
pp_servo_init(ppi);
return e;
}
d1 = d2 = pp_ms_to_timeout(ppi, PP_TO_ANN_RECEIPT);
if (ppi->timeouts[PP_TO_REQUEST])
d2 = pp_ms_to_timeout(ppi, PP_TO_REQUEST);
ppi->next_delay = d1 < d2 ? d1 : d2;
ppi->next_delay = pp_next_delay_2(ppi,
PP_TO_ANN_RECEIPT, PP_TO_REQUEST);
return e;
}
......@@ -113,12 +113,42 @@ int pp_timeout(struct pp_instance *ppi, int index)
return ret;
}
/* how many ms to wait for the timeout to happen, for ppi->next_delay */
int pp_ms_to_timeout(struct pp_instance *ppi, int index)
/*
* How many ms to wait for the timeout to happen, for ppi->next_delay.
* It is not allowed for a timeout to not be pending
*/
int pp_next_delay_1(struct pp_instance *ppi, int i1)
{
signed long ret;
unsigned long now = ppi->t_ops->calc_timeout(ppi, 0);
signed long r1;
ret = ppi->timeouts[index] - ppi->t_ops->calc_timeout(ppi, 0);
return ret <= 0 ? 0 : ret;
r1 = ppi->timeouts[i1] - now;
return r1 < 0 ? 0 : r1;
}
int pp_next_delay_2(struct pp_instance *ppi, int i1, int i2)
{
unsigned long now = ppi->t_ops->calc_timeout(ppi, 0);
signed long r1, r2;
r1 = ppi->timeouts[i1] - now;
r2 = ppi->timeouts[i2] - now;
if (r2 < r1)
r1 = r2;
return r1 < 0 ? 0 : r1;
}
int pp_next_delay_3(struct pp_instance *ppi, int i1, int i2, int i3)
{
unsigned long now = ppi->t_ops->calc_timeout(ppi, 0);
signed long r1, r2, r3;
r1 = ppi->timeouts[i1] - now;
r2 = ppi->timeouts[i2] - now;
r3 = ppi->timeouts[i3] - now;
if (r2 < r1)
r1 = r2;
if (r3 < r1)
r1 = r3;
return r1 < 0 ? 0 : r1;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment