state-wr-m-lock.c 1.57 KB
Newer Older
1
/*
2 3
 * Copyright (C) 2012 CERN (www.cern.ch)
 * Author: Aurelio Colosimo
4
 * Based on ptp-noposix project (see AUTHORS for details)
5 6
 *
 * Released to the public domain
7 8 9 10
 */

#include <ppsi/ppsi.h>

11 12 13 14
/*
 * This the entry point for a WR master: send "LOCK" and wait
 * for "LOCKED". On timeout retry sending, for WR_STATE_RETRY times.
 */
15 16 17 18
#define WR_TMO_NAME "WR_MLOCK"
#define WR_TMO_MS WR_M_LOCK_TIMEOUT_MS

int wr_m_lock(struct pp_instance *ppi, void *buf, int len, int new_state)
19
{
20
	int sendmsg = 0;
21
	struct wr_dsport *wrp = WR_DSPOR(ppi);
22

23 24
	if (new_state) {
		wr_reset_process(ppi,WR_MASTER);
25
		wrp->wrStateRetry = WR_STATE_RETRY;
26
		pp_timeout_set_rename(ppi, wrTmoIdx, WR_TMO_MS*(WR_STATE_RETRY+1),WR_TMO_NAME);
27
		sendmsg = 1;
28
	} else {
29
		if (ppi->received_ptp_header.messageType == PPM_SIGNALING) {
30 31
			Enumeration16 wrMsgId;
			MsgSignaling wrsig_msg;
32

33
			if ( msg_unpack_wrsig(ppi, buf, &wrsig_msg,&wrMsgId) ) {
34 35 36
				if (wrMsgId == LOCKED) {
					wrp->next_state =  WRS_CALIBRATION;
				} else {
37
					pp_diag(ppi, ext, 1, "WR: Invalid msgId(x%04x) received. LOCKED was expected\n",wrMsgId);
38 39
					wr_handshake_fail(ppi);
				}
40 41
				return 0;
			}
42
		}
43

44 45
		{ /* Check remaining time */
			int rms=pp_next_delay_1(ppi, wrTmoIdx);
46 47
			if ( rms<=(wrp->wrStateRetry*WR_TMO_MS)) {
				if ( !rms ) {
48
					pp_diag(ppi, time, 1, "timeout expired: "WR_TMO_NAME"\n");
49
					wr_handshake_fail(ppi);
50 51
					return 0; /* non-wr already */
				}
52 53
				if (wr_handshake_retry(ppi))
					sendmsg = 1;
54 55
			}
		}
56 57
	}

58 59
	if (sendmsg) {
		msg_issue_wrsig(ppi, LOCK);
60
	}
61

62
	return pp_next_delay_1(ppi,wrTmoIdx)-wrp->wrStateRetry*WR_TMO_MS;
63
}