state-initializing.c 2.75 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

11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
/*
 * Initialize parentDS
 */
static void init_parent_ds(struct pp_instance *ppi)
{
	/* 8.2.3.2 */
	DSPAR(ppi)->parentPortIdentity.clockIdentity =
		DSDEF(ppi)->clockIdentity;
	/* FIXME: portNumber ? */
	/* 8.2.3.3 skipped (parentStats is not used) */
	/* 8.2.3.4 */
	DSPAR(ppi)->observedParentOffsetScaledLogVariance = 0xffff;
	/* 8.2.3.5 */
	DSPAR(ppi)->observedParentClockPhaseChangeRate = 0x7fffffffUL;
	/* 8.2.3.6 */
	DSPAR(ppi)->grandmasterIdentity = DSDEF(ppi)->clockIdentity;
	/* 8.2.3.7 */
	DSPAR(ppi)->grandmasterClockQuality = DSDEF(ppi)->clockQuality;
	/* 8.2.3.8 */
	DSPAR(ppi)->grandmasterPriority1 = DSDEF(ppi)->priority1;
	/* 8.2.3.9 */
	DSPAR(ppi)->grandmasterPriority2 = DSDEF(ppi)->priority2;
}

35 36 37
/*
 * Initializes network and other stuff
 */
38

39 40
int pp_initializing(struct pp_instance *ppi, unsigned char *pkt, int plen)
{
41
	unsigned char *id, *mac;
42 43
	struct DSPort *port = DSPOR(ppi);
	struct pp_runtime_opts *opt = OPTS(ppi);
44
	int ret = 0;
45

46
	if (ppi->n_ops->init(ppi) < 0) /* it must handle being called twice */
47
		goto failure;
48

49 50
	init_parent_ds(ppi);

51
	/* Clock identity comes from mac address with 0xff:0xfe intermixed */
52
	id = (unsigned char *)&DSDEF(ppi)->clockIdentity;
53
	mac = ppi->ch[PP_NP_GEN].addr;
54 55 56 57 58 59 60 61 62
	id[0] = mac[0];
	id[1] = mac[1];
	id[2] = mac[2];
	id[3] = 0xff;
	id[4] = 0xfe;
	id[5] = mac[3];
	id[6] = mac[4];
	id[7] = mac[5];

63 64 65 66 67
	/*
	 * Initialize parent data set
	 */
	init_parent_ds(ppi);

68 69 70
	/*
	 * Initialize port data set
	 */
71
	memcpy(&port->portIdentity.clockIdentity,
72
		&DSDEF(ppi)->clockIdentity, PP_CLOCK_IDENTITY_LENGTH);
73 74
	/* 1-based port number =  index of this ppi in the global array */
	port->portIdentity.portNumber = 1 + ppi - ppi->glbs->pp_instances;
75 76 77 78 79
	port->logMinDelayReqInterval = PP_DEFAULT_DELAYREQ_INTERVAL;
	port->logAnnounceInterval = opt->announce_intvl;
	port->announceReceiptTimeout = PP_DEFAULT_ANNOUNCE_RECEIPT_TIMEOUT;
	port->logSyncInterval = opt->sync_intvl;
	port->versionNumber = PP_VERSION_PTP;
80
	pp_timeout_init(ppi);
81

82 83 84
	if (pp_hooks.init)
		ret = pp_hooks.init(ppi, pkt, plen);
	if (ret) {
85
		pp_diag(ppi, ext, 1, "%s: can't init extension\n", __func__);
86
		goto failure;
87
	}
88

89 90 91 92 93
	pp_diag(ppi, bmc, 1, "clock class = %d\n",
			DSDEF(ppi)->clockQuality.clockClass);
	pp_diag(ppi, bmc, 1, "clock accuracy = %d\n",
			DSDEF(ppi)->clockQuality.clockAccuracy);

94
	msg_init_header(ppi, ppi->tx_ptp); /* This is used for all tx */
95

96
	if (ppi->role != PPSI_ROLE_MASTER)
97 98 99
		ppi->next_state = PPS_LISTENING;
	else
		ppi->next_state = PPS_MASTER;
100 101 102
	return 0;

failure:
103 104
	ppi->next_delay = 1000; /* wait 1s before retrying */
	return 0;
105
}