state-passive.c 3.03 KB
Newer Older
1
/*
2 3
 * Copyright (C) 2011 CERN (www.cern.ch)
 * Author: Aurelio Colosimo
4
 * Based on PTPd project v. 2.1.0 (see AUTHORS for details)
5 6
 *
 * Released according to the GNU LGPL, version 2.1 or any later version.
7
 */
8

Alessandro Rubini's avatar
Alessandro Rubini committed
9
#include <ppsi/ppsi.h>
10
#include "common-fun.h"
11

12
static int passive_handle_announce(struct pp_instance *ppi, void *buf, int len);
13

Sven Meier's avatar
Sven Meier committed
14 15 16 17 18 19 20 21 22 23
static pp_action *actions[] = {
	[PPM_SYNC]		= 0,
	[PPM_DELAY_REQ]		= 0,
#if CONFIG_HAS_P2P
	[PPM_PDELAY_REQ]	= st_com_peer_handle_preq,
	[PPM_PDELAY_RESP]	= st_com_peer_handle_pres,
	[PPM_PDELAY_R_FUP]	= st_com_peer_handle_pres_followup,
#endif
	[PPM_FOLLOW_UP]		= 0,
	[PPM_DELAY_RESP]	= 0,
24
	[PPM_ANNOUNCE]		= passive_handle_announce,
Sven Meier's avatar
Sven Meier committed
25 26 27
	/* skip signaling and management, for binary size */
};

28
static int passive_handle_announce(struct pp_instance *ppi, void *buf, int len)
29 30 31
{
	int ret = 0;
	MsgHeader *hdr = &ppi->received_ptp_header;
32
	struct pp_frgn_master *erbest = ppi->frgn_rec_best!=-1 ? &ppi->frgn_master[ppi->frgn_rec_best] : NULL;
33
	
34
	ret = st_com_handle_announce(ppi, buf, len);
35 36 37
	if (ret)
		return ret;
	
baujc's avatar
baujc committed
38 39 40 41 42 43 44 45 46 47 48
	/* Clause 9.2.2.2 MasterOnly PTP ports :
	 * Announce messages received on a masterOnly PTP Port shall not be considered
	 * in the operation of the best master clock algorithm or in the update of data sets.
	 */
	if ( ! DSPOR(ppi)->masterOnly) {
		struct pp_frgn_master frgn_master;

		bmc_store_frgn_master(ppi, &frgn_master, buf, len);
		bmc_add_frgn_master(ppi, &frgn_master);
	}

baujc's avatar
baujc committed
49 50 51 52 53 54 55 56 57 58 59 60
	/* Clause 17.6.5.3 : ExternalPortConfiguration enabled
	 *  - The Announce receipt timeout mechanism (see 9.2.6.12) shall not be active.
	 */
	if (! is_externalPortConfigurationEnabled(DSDEF(ppi))) {
		if (erbest!=NULL && !bmc_pidcmp(&hdr->sourcePortIdentity,
			&erbest->sourcePortIdentity)) {
			/*
			 * 9.2.6.11 d) reset timeout when an announce
			 * is received from the clock putting it into passive (erbest)
			 */
			pp_timeout_reset(ppi, PP_TO_ANN_RECEIPT);
		}
61 62 63 64 65
	}
	
	return 0;
}

66
int pp_passive(struct pp_instance *ppi, void *buf, int len)
67
{
68
	int e = 0; /* error var, to check errors in msg handling */
Sven Meier's avatar
Sven Meier committed
69 70
	MsgHeader *hdr = &ppi->received_ptp_header;

71
	/* when the clock is using peer-delay, passive must send it too */
baujc's avatar
baujc committed
72
	if ( is_delayMechanismP2P(ppi) )
73
		e  = pp_lib_may_issue_request(ppi);
74

Sven Meier's avatar
Sven Meier committed
75 76 77 78 79
	/*
	 * The management of messages is now table-driven
	 */
	if (hdr->messageType < ARRAY_SIZE(actions)
	    && actions[hdr->messageType]) {
80
		e = actions[hdr->messageType](ppi, buf, len);
Sven Meier's avatar
Sven Meier committed
81
	} else {
82
		if (len)
Sven Meier's avatar
Sven Meier committed
83 84 85
			pp_diag(ppi, frames, 1, "Ignored frame %i\n",
				hdr->messageType);
	}
86

baujc's avatar
baujc committed
87 88 89 90 91 92 93
	/* Clause 17.6.5.3 : ExternalPortConfiguration enabled
	 *  - The Announce receipt timeout mechanism (see 9.2.6.12) shall not be active.
	 */
	if ( is_externalPortConfigurationEnabled(DSDEF(ppi))) {
		ppi->next_delay = is_delayMechanismP2P(ppi) ?
				pp_next_delay_1(ppi,PP_TO_REQUEST) :
				INT_MAX;
94
	} else {
baujc's avatar
baujc committed
95 96 97 98 99
		st_com_check_announce_receive_timeout(ppi);
		ppi->next_delay = is_delayMechanismP2P(ppi) ?
				pp_next_delay_2(ppi,PP_TO_ANN_RECEIPT, PP_TO_REQUEST) :
				pp_next_delay_1(ppi,PP_TO_ANN_RECEIPT);

baujc's avatar
baujc committed
100
		if ( e !=0 )
baujc's avatar
baujc committed
101
			ppi->next_state = PPS_FAULTY;
102
	}
baujc's avatar
baujc committed
103

Sven Meier's avatar
Sven Meier committed
104
	return e;
105
}