state-master.c 5 KB
Newer Older
1
/*
2 3
 * Aurelio Colosimo for CERN, 2011 -- GNU LGPL v2.1 or later
 * Based on PTPd project v. 2.1.0 (see AUTHORS for details)
4
 */
5

6
#include <pptp/pptp.h>
7
#include <pptp/diag.h>
8
#include "common-fun.h"
9

10
int pp_master(struct pp_instance *ppi, unsigned char *pkt, int plen)
11
{
12
	TimeInternal *time;
13 14 15 16
	TimeInternal req_rec_tstamp;
	TimeInternal correction_field;
	TimeInternal resp_orig_tstamp;

17
	int e = 0; /* error var, to check errors in msg handling */
18 19
	MsgHeader *hdr = &ppi->msg_tmp_header;

20 21
	time = &ppi->last_rcv_time;

22 23
	if (ppi->is_new_state) {
		pp_timer_start(1 << DSPOR(ppi)->logSyncInterval,
24
			ppi->timers[PP_TIMER_SYNC]);
25 26

		pp_timer_start(1 << DSPOR(ppi)->logAnnounceInterval,
27
			ppi->timers[PP_TIMER_ANN_INTERVAL]);
28 29

		pp_timer_start(1 << DSPOR(ppi)->logMinPdelayReqInterval,
30
			ppi->timers[PP_TIMER_PDELAYREQ]);
31 32 33 34 35
	}

	if (st_com_check_record_update(ppi))
		goto state_updated;

36
	if (pp_timer_expired(ppi->timers[PP_TIMER_SYNC])) {
37
		/* FIXME diag
38
		DBGV("TODO: event SYNC_INTERVAL_TIMEOUT_EXPIRES\n");
39 40 41
		*/
		if (msg_issue_sync(ppi) < 0)
			goto failure;
42 43
	}

44
	if (pp_timer_expired(ppi->timers[PP_TIMER_ANN_INTERVAL])) {
45 46 47 48 49
		/* FIXME diag
		DBGV("event ANNOUNCE_INTERVAL_TIMEOUT_EXPIRES\n");
		*/
		if (msg_issue_announce(ppi) < 0)
			goto failure;
50 51 52
	}

	if (!ppi->rt_opts->e2e_mode) {
53
		if (pp_timer_expired(ppi->timers[PP_TIMER_PDELAYREQ])) {
54 55 56 57 58
			/* FIXME diag
			DBGV("event PDELAYREQ_INTERVAL_TOUT_EXPIRES\n");
			*/
			if (msg_issue_pdelay_req(ppi) < 0)
				goto failure;
59 60 61 62 63 64
		}
	}

	switch (ppi->msg_tmp_header.messageType) {

	case PPM_ANNOUNCE:
65
		e = st_com_master_handle_announce(ppi, pkt, plen);
66 67 68
		break;

	case PPM_SYNC:
69
		e = st_com_master_handle_sync(ppi, pkt, plen);
70 71 72 73
		break;

	case PPM_DELAY_REQ:
		msg_copy_header(&ppi->delay_req_hdr, hdr);
74
		msg_issue_delay_resp(ppi, time);
75 76 77
		break;

	case PPM_PDELAY_REQ:
78
		e = st_com_handle_pdelay_req(ppi, pkt, plen);
79 80 81 82 83 84 85 86 87
		break;

	case PPM_PDELAY_RESP:
		/* Loopback Timestamp */
		if (ppi->rt_opts->e2e_mode)
			break;

		if (ppi->is_from_self) {
			/*Add latency*/
88
			add_TimeInternal(time, time,
89 90
					 &ppi->rt_opts->outbound_latency);

91
			e = msg_issue_pdelay_resp_follow_up(ppi, time);
92 93 94 95 96 97 98 99 100 101 102 103 104
			break;
		}
		msg_unpack_pdelay_resp(pkt, &ppi->msg_tmp.presp);

		if (!((ppi->sent_seq_id[PPM_PDELAY_REQ] ==
		       hdr->sequenceId)
			&& (!pp_memcmp(DSPOR(ppi)->portIdentity.clockIdentity,
			       ppi->msg_tmp.presp.requestingPortIdentity.
				       clockIdentity,
			       PP_CLOCK_IDENTITY_LENGTH))
			&& (DSPOR(ppi)->portIdentity.portNumber ==
			ppi->msg_tmp.presp.requestingPortIdentity.portNumber))
		   ) {
105
			if ((hdr->flagField[0] & PP_TWO_STEP_FLAG) != 0) {
106 107 108
				/* Two Step Clock */
				/* Store t4 (Fig 35) */
				ppi->pdelay_resp_receive_time.seconds =
109
					time->seconds;
110
				ppi->pdelay_resp_receive_time.nanoseconds =
111
					time->nanoseconds;
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
				/* Store t2 (Fig 35) */
				to_TimeInternal(&req_rec_tstamp,
					       &ppi->msg_tmp.presp.
					       requestReceiptTimestamp);
				ppi->pdelay_req_receive_time.seconds =
					req_rec_tstamp.seconds;
				ppi->pdelay_req_receive_time.nanoseconds =
					req_rec_tstamp.nanoseconds;
				int64_to_TimeInternal(
					hdr->correctionfield,
					&correction_field);
				ppi->last_pdelay_resp_corr_field.seconds =
					correction_field.seconds;
				ppi->last_pdelay_resp_corr_field.nanoseconds =
					correction_field.nanoseconds;
				break;
128
			} else {
129 130 131
				/* One step Clock */
				/* Store t4 (Fig 35)*/
				ppi->pdelay_resp_receive_time.seconds =
132
					time->seconds;
133
				ppi->pdelay_resp_receive_time.nanoseconds =
134
					time->nanoseconds;
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183

				int64_to_TimeInternal(
					hdr->correctionfield,
					&correction_field);
				/* TODO servo
				updatePeerDelay(&ptpClock->owd_filt,
						rtOpts,ptpClock,
						&correctionField,FALSE);
				*/
				break;
			}
		}
		break; /* XXX added by gnn for safety */


	case PPM_PDELAY_RESP_FOLLOW_UP:
		if (hdr->sequenceId == ppi->sent_seq_id[PPM_PDELAY_REQ] - 1) {
			msg_unpack_pdelay_resp_followup(pkt,
				&ppi->msg_tmp.prespfollow);
			to_TimeInternal(&resp_orig_tstamp,
				       &ppi->msg_tmp.prespfollow.
					       responseOriginTimestamp);
			ppi->pdelay_resp_send_time.seconds =
				resp_orig_tstamp.seconds;
			ppi->pdelay_resp_send_time.nanoseconds =
				resp_orig_tstamp.nanoseconds;

			int64_to_TimeInternal(
				ppi->msg_tmp_header.correctionfield,
				&correction_field);
			add_TimeInternal(&correction_field,
				&correction_field,
				&ppi->last_pdelay_resp_corr_field);

			/* TODO servo
			updatePeerDelay(&ptpClock->owd_filt,
					rtOpts, ptpClock,
					&correctionField,TRUE);
			*/
			break;
		}

		break;

	default:
		/* disreguard, nothing to do */
		break;
	}

184
failure:
185 186 187 188
	if (e == 0) {
		if (DSDEF(ppi)->slaveOnly ||
			DSDEF(ppi)->clockQuality.clockClass == 255)
			ppi->next_state = PPS_LISTENING;
189
	} else {
190
		ppi->next_state = PPS_FAULTY;
191
	}
192 193 194 195

state_updated:
	/* Leaving this state */
	if (ppi->next_state != ppi->state) {
196 197 198
		pp_timer_stop(ppi->timers[PP_TIMER_SYNC]);
		pp_timer_stop(ppi->timers[PP_TIMER_ANN_INTERVAL]);
		pp_timer_stop(ppi->timers[PP_TIMER_PDELAYREQ]);
199 200 201 202
	}

	ppi->next_delay = PP_DEFAULT_NEXT_DELAY_MS;

203
	return 0;
204

205
}