Commit 8cd4bda8 authored by Alessandro Rubini's avatar Alessandro Rubini

proto-standard: move handle_announce() to fsm-lib.c

This is another step in a general cleanup of the state machine.  To
help me knowning what is audited and what is not, I rename the "common
functions" to fsm-lib. This is the management of announce.

- master and slave functions were the same, so they are unified now

- hooks->handle_annunce returns int (it may fail)
Signed-off-by: Alessandro Rubini's avatarAlessandro Rubini <rubini@gnudd.com>
parent cc014e83
......@@ -63,6 +63,8 @@ extern struct pp_msgtype_info pp_msgtype_info[16];
extern int pp_lib_may_issue_sync(struct pp_instance *ppi);
extern int pp_lib_may_issue_announce(struct pp_instance *ppi);
extern int pp_lib_may_issue_request(struct pp_instance *ppi);
extern int pp_lib_handle_announce(struct pp_instance *ppi,
unsigned char *buf, int len);
/* We use data sets a lot, so have these helpers */
static inline struct pp_globals *GLBS(struct pp_instance *ppi)
......@@ -151,7 +153,7 @@ struct pp_ext_hooks {
int (*handle_resp)(struct pp_instance *ppi);
void (*s1)(struct pp_instance *ppi, MsgHeader *hdr, MsgAnnounce *ann);
int (*execute_slave)(struct pp_instance *ppi);
void (*handle_announce)(struct pp_instance *ppi);
int (*handle_announce)(struct pp_instance *ppi);
int (*handle_followup)(struct pp_instance *ppi, TimeInternal *orig,
TimeInternal *correction_field);
int (*handle_preq) (struct pp_instance * ppi);
......
......@@ -192,7 +192,7 @@ static int wr_execute_slave(struct pp_instance *ppi)
return 1; /* the caller returns too */
}
static void wr_handle_announce(struct pp_instance *ppi)
static int wr_handle_announce(struct pp_instance *ppi)
{
pp_diag(ppi, ext, 2, "hook: %s\n", __func__);
if ((WR_DSPOR(ppi)->wrConfig & WR_S_ONLY) &&
......@@ -202,6 +202,7 @@ static void wr_handle_announce(struct pp_instance *ppi)
/* We must start the handshake as a WR slave */
wr_handshake_init(ppi, PPS_SLAVE);
}
return 0;
}
static int wr_handle_followup(struct pp_instance *ppi,
......
......@@ -100,63 +100,6 @@ int st_com_execute_slave(struct pp_instance *ppi)
return 0;
}
/* Called by this file, basically when an announce is got, all states */
static void st_com_add_foreign(struct pp_instance *ppi, unsigned char *buf)
{
int i;
MsgHeader *hdr = &ppi->received_ptp_header;
/* Check if foreign master is already known */
for (i = 0; i < ppi->frgn_rec_num; i++) {
if (!memcmp(&hdr->sourcePortIdentity,
&ppi->frgn_master[i].port_id,
sizeof(hdr->sourcePortIdentity))) {
/* already in Foreign master data set, update info */
msg_copy_header(&ppi->frgn_master[i].hdr, hdr);
msg_unpack_announce(buf, &ppi->frgn_master[i].ann);
return;
}
}
/* New foreign master */
if (ppi->frgn_rec_num < PP_NR_FOREIGN_RECORDS)
ppi->frgn_rec_num++;
/* FIXME: replace the worst */
i = ppi->frgn_rec_num - 1;
/* Copy new foreign master data set from announce message */
memcpy(&ppi->frgn_master[i].port_id,
&hdr->sourcePortIdentity, sizeof(hdr->sourcePortIdentity));
/*
* header and announce field of each Foreign Master are
* useful to run Best Master Clock Algorithm
*/
msg_copy_header(&ppi->frgn_master[i].hdr, hdr);
msg_unpack_announce(buf, &ppi->frgn_master[i].ann);
pp_diag(ppi, bmc, 1, "New foreign Master %i added\n", i);
}
/* Called by slave and uncalibrated */
int st_com_slave_handle_announce(struct pp_instance *ppi, unsigned char *buf,
int len)
{
/* st_com_add_foreign takes care of announce unpacking */
st_com_add_foreign(ppi, buf);
/*Reset Timer handling Announce receipt timeout*/
pp_timeout_set(ppi, PP_TO_ANN_RECEIPT);
ppi->next_state = bmc(ppi); /* got a new announce: run bmc */
if (pp_hooks.handle_announce)
pp_hooks.handle_announce(ppi);
return 0;
}
/* Called by slave and uncalibrated */
int st_com_slave_handle_sync(struct pp_instance *ppi, unsigned char *buf,
......@@ -349,21 +292,6 @@ int st_com_slave_handle_followup(struct pp_instance *ppi, unsigned char *buf,
return 0;
}
/* Called by master, listenting, passive. */
int st_com_master_handle_announce(struct pp_instance *ppi, unsigned char *buf,
int len)
{
pp_diag(ppi, bmc, 2, "Announce message from another foreign master\n");
st_com_add_foreign(ppi, buf);
ppi->next_state = bmc(ppi); /* got a new announce: run bmc */
if (pp_hooks.handle_announce)
pp_hooks.handle_announce(ppi);
return 0;
}
/*
* Called by master, listenting, passive.
* FIXME: this must be implemented to support one-step masters
......
......@@ -20,12 +20,6 @@ int st_com_execute_slave(struct pp_instance *ppi);
* message, -1 in case the message contained in buf is not proper (e.g. size
* is not the expected one
*/
int st_com_slave_handle_announce(struct pp_instance *ppi, unsigned char *buf,
int len);
int st_com_master_handle_announce(struct pp_instance *ppi, unsigned char *buf,
int len);
int st_com_slave_handle_sync(struct pp_instance *ppi, unsigned char *buf,
int len);
......
......@@ -2,13 +2,12 @@
* Copyright (C) 2011 CERN (www.cern.ch)
* Author: Aurelio Colosimo
* Copyright (C) 2014 GSI (www.gsi.de)
* Author: Alessandro Rubin
* Author: Alessandro Rubini
*
* Released according to the GNU LGPL, version 2.1 or any later version.
*/
#include <ppsi/ppsi.h>
/* Local functions that build to nothing when Kconfig selects 0/1 vlans */
static int pp_vlan_issue_announce(struct pp_instance *ppi)
{
......@@ -108,3 +107,54 @@ int pp_lib_may_issue_request(struct pp_instance *ppi)
ppi->t3 = ppi->last_snt_time;
return 0;
}
/* Called by this file, basically when an announce is got, all states */
static void __lib_add_foreign(struct pp_instance *ppi, unsigned char *buf)
{
int i;
MsgHeader *hdr = &ppi->received_ptp_header;
/* Check if foreign master is already known */
for (i = 0; i < ppi->frgn_rec_num; i++) {
if (!memcmp(&hdr->sourcePortIdentity,
&ppi->frgn_master[i].port_id,
sizeof(hdr->sourcePortIdentity))) {
/* already in Foreign master data set, update info */
msg_copy_header(&ppi->frgn_master[i].hdr, hdr);
msg_unpack_announce(buf, &ppi->frgn_master[i].ann);
return;
}
}
/* New foreign master */
if (ppi->frgn_rec_num < PP_NR_FOREIGN_RECORDS)
ppi->frgn_rec_num++;
/* FIXME: replace the worst */
i = ppi->frgn_rec_num - 1;
/* Copy new foreign master data set from announce message */
memcpy(&ppi->frgn_master[i].port_id,
&hdr->sourcePortIdentity, sizeof(hdr->sourcePortIdentity));
/*
* header and announce field of each Foreign Master are
* useful to run Best Master Clock Algorithm
*/
msg_copy_header(&ppi->frgn_master[i].hdr, hdr);
msg_unpack_announce(buf, &ppi->frgn_master[i].ann);
pp_diag(ppi, bmc, 1, "New foreign Master %i added\n", i);
}
int pp_lib_handle_announce(struct pp_instance *ppi, unsigned char *buf, int len)
{
__lib_add_foreign(ppi, buf);
ppi->next_state = bmc(ppi); /* got a new announce: run bmc */
pp_timeout_set(ppi, PP_TO_ANN_RECEIPT);
if (pp_hooks.handle_announce)
return pp_hooks.handle_announce(ppi);
return 0;
}
......@@ -25,7 +25,7 @@ int pp_listening(struct pp_instance *ppi, unsigned char *pkt, int plen)
switch (ppi->received_ptp_header.messageType) {
case PPM_ANNOUNCE:
e = st_com_master_handle_announce(ppi, pkt, plen);
e = pp_lib_handle_announce(ppi, pkt, plen);
break;
case PPM_SYNC:
......
......@@ -22,7 +22,7 @@ static pp_action *actions[] = {
#endif
[PPM_FOLLOW_UP] = 0,
[PPM_DELAY_RESP] = 0,
[PPM_ANNOUNCE] = st_com_master_handle_announce,
[PPM_ANNOUNCE] = pp_lib_handle_announce,
/* skip signaling and management, for binary size */
};
......
......@@ -20,7 +20,7 @@ int pp_passive(struct pp_instance *ppi, unsigned char *pkt, int plen)
switch (ppi->received_ptp_header.messageType) {
case PPM_ANNOUNCE:
e = st_com_master_handle_announce(ppi, pkt, plen);
e = pp_lib_handle_announce(ppi, pkt, plen);
break;
case PPM_SYNC:
......
......@@ -24,7 +24,7 @@ static pp_action *actions[] = {
#endif
[PPM_FOLLOW_UP] = st_com_slave_handle_followup,
[PPM_DELAY_RESP] = slave_handle_response,
[PPM_ANNOUNCE] = st_com_slave_handle_announce,
[PPM_ANNOUNCE] = pp_lib_handle_announce,
/* skip signaling and management, for binary size */
};
......
......@@ -16,7 +16,7 @@ int pp_uncalibrated(struct pp_instance *ppi, unsigned char *pkt, int plen)
switch (ppi->received_ptp_header.messageType) {
case PPM_ANNOUNCE:
e = st_com_slave_handle_announce(ppi, pkt, plen);
e = pp_lib_handle_announce(ppi, pkt, plen);
break;
case PPM_SYNC:
......
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