state-slave.c 6.38 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_slave(struct pp_instance *ppi, unsigned char *pkt, int plen)
11
{
12
	int e = 0; /* error var, to check errors in msg handling */
13
	TimeInternal *time;
14 15 16 17
	TimeInternal req_rec_tstamp;
	TimeInternal correction_field;
	TimeInternal resp_orig_tstamp;
	MsgHeader *hdr = &ppi->msg_tmp_header;
18

19 20
	time = &ppi->last_rcv_time;

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
	if (ppi->is_new_state) {
		pp_init_clock(ppi);

		ppi->waiting_for_follow = FALSE;

		ppi->pdelay_req_send_time.seconds = 0;
		ppi->pdelay_req_send_time.nanoseconds = 0;
		ppi->pdelay_req_receive_time.seconds = 0;
		ppi->pdelay_req_receive_time.nanoseconds = 0;
		ppi->pdelay_resp_send_time.seconds = 0;
		ppi->pdelay_resp_send_time.nanoseconds = 0;
		ppi->pdelay_resp_receive_time.seconds = 0;
		ppi->pdelay_resp_receive_time.nanoseconds = 0;

		st_com_restart_annrec_timer(ppi);

		if (ppi->rt_opts->e2e_mode)
			pp_timer_start(1 << DSPOR(ppi)->logMinDelayReqInterval,
39
				ppi->timers[PP_TIMER_DELAYREQ]);
40 41
		else
			pp_timer_start(1 << DSPOR(ppi)->logMinPdelayReqInterval,
42
				ppi->timers[PP_TIMER_PDELAYREQ]);
43 44 45 46 47 48 49 50
	}

	if (st_com_check_record_update(ppi))
		goto state_updated;

	switch (ppi->msg_tmp_header.messageType) {

	case PPM_ANNOUNCE:
51
		e = st_com_slave_handle_announce(ppi, pkt, plen);
52 53 54
		break;

	case PPM_SYNC:
55
		e = st_com_slave_handle_sync(ppi, pkt, plen);
56 57 58
		break;

	case PPM_FOLLOW_UP:
59
		e = st_com_slave_handle_followup(ppi, pkt, plen);
60 61 62
		break;

	case PPM_DELAY_REQ:
63 64 65 66 67 68 69 70

		e = (plen < PP_DELAY_REQ_LENGTH);

		if (!e && ppi->is_from_self) {
			/* Get sending timestamp from IP stack
			 * with So_TIMESTAMP */

			ppi->delay_req_send_time.seconds =
71
				time->seconds;
72
			ppi->delay_req_send_time.nanoseconds =
73
				time->nanoseconds;
74 75 76 77 78 79

			/* Add latency */
			add_TimeInternal(&ppi->delay_req_send_time,
					 &ppi->delay_req_send_time,
					 &ppi->rt_opts->outbound_latency);
		}
80 81 82
		break;

	case PPM_DELAY_RESP:
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122

		e = (plen < PP_DELAY_RESP_LENGTH);

		if (e)
			break;

		if (!ppi->rt_opts->e2e_mode)
			break;

		msg_unpack_delay_resp(pkt, &ppi->msg_tmp.resp);

		if ((pp_memcmp(DSPOR(ppi)->portIdentity.clockIdentity,
			ppi->msg_tmp.resp.requestingPortIdentity.clockIdentity,
			PP_CLOCK_IDENTITY_LENGTH) == 0) &&
			((ppi->sent_seq_id[PPM_DELAY_REQ] - 1) ==
				hdr->sequenceId) &&
			(DSPOR(ppi)->portIdentity.portNumber ==
			ppi->msg_tmp.resp.requestingPortIdentity.portNumber)
			&& ppi->is_from_cur_par) {

			to_TimeInternal(&req_rec_tstamp,
				       &ppi->msg_tmp.resp.receiveTimestamp);
			ppi->delay_req_receive_time.seconds =
				req_rec_tstamp.seconds;
			ppi->delay_req_receive_time.nanoseconds =
				req_rec_tstamp.nanoseconds;

			int64_to_TimeInternal(
				hdr->correctionfield,
				&correction_field);

			/* TODO servo
			updateDelay(&ptpClock->owd_filt,
				    rtOpts,ptpClock, &correctionField);
			*/

			ppi->log_min_delay_req_interval =
				hdr->logMessageInterval;

		} else {
123 124
			DBGV("TODO: HandledelayResp : "
			     "delayResp doesn't match delayReq.\n");
125 126
		}

127 128 129
		break;

	case PPM_PDELAY_REQ:
130
		e = st_com_handle_pdelay_req(ppi, pkt, plen);
131 132 133
		break;

	case PPM_PDELAY_RESP:
134 135 136 137 138 139 140 141 142
		if (ppi->rt_opts->e2e_mode)
			break;

		e = (plen < PP_PDELAY_RESP_LENGTH);

		if (e)
			break;

		if (ppi->is_from_self)	{
143
			add_TimeInternal(time, time,
144
					&ppi->rt_opts->outbound_latency);
145
			msg_issue_pdelay_resp_follow_up(ppi, time);
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
			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))) {

			if ((hdr->flagField[0] & 0x02) ==
			    PP_TWO_STEP_FLAG) {
				/* Two Step Clock */
				/* Store t4 (Fig 35) */
				ppi->pdelay_resp_receive_time.seconds =
165
					time->seconds;
166
				ppi->pdelay_resp_receive_time.nanoseconds =
167
					time->nanoseconds;
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
				/* 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;
			} else {
				/* One step Clock */
				/* Store t4 (Fig 35) */
				ppi->pdelay_resp_receive_time.seconds =
187
					time->seconds;
188
				ppi->pdelay_resp_receive_time.nanoseconds =
189
					time->nanoseconds;
190 191 192 193 194 195 196 197 198

				int64_to_TimeInternal(hdr->correctionfield,
					&correction_field);
				/* TODO
				updatePeerDelay(&ptpClock->owd_filt,rtOpts,
					ptpClock,&correctionField,FALSE);
				*/
			}
		} else {
199 200
			DBGV("TODO: HandlePdelayResp : Pdelayresp doesn't "
			     "match PdelayReq.\n");
201
		}
202 203 204
		break;

	case PPM_PDELAY_RESP_FOLLOW_UP:
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242

		if (ppi->rt_opts->e2e_mode)
			break;

		e = (plen < PP_PDELAY_RESP_FOLLOW_UP_LENGTH);

		if (e)
			break;

		if (hdr->sequenceId ==
		    ppi->sent_seq_id[PPM_PDELAY_REQ]) {

			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(
				hdr->correctionfield,
				&correction_field);
			add_TimeInternal(&correction_field, &correction_field,
				&ppi->last_pdelay_req_corr_field);

			/* TODO servo
			updatePeerDelay (&ptpClock->owd_filt,
					 rtOpts, ptpClock,
					 &correctionField,TRUE);
			*/
		}
243 244 245 246 247 248 249 250 251
		break;

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

	}

	if (e == 0)
252 253 254
		e = st_com_execute_slave(ppi);

	if (e != 0)
255 256 257 258 259 260
		ppi->next_state = PPS_FAULTY;

state_updated:

	/* Leaving this state */
	if (ppi->next_state != ppi->state) {
261
		pp_timer_stop(ppi->timers[PP_TIMER_ANN_RECEIPT]);
262 263

		if (ppi->rt_opts->e2e_mode)
264
			pp_timer_stop(ppi->timers[PP_TIMER_DELAYREQ]);
265
		else
266
			pp_timer_stop(ppi->timers[PP_TIMER_PDELAYREQ]);
267 268 269 270 271 272

		pp_init_clock(ppi);
	}

	ppi->next_delay = PP_DEFAULT_NEXT_DELAY_MS;

273 274
	return 0;
}