bmc.c 9.16 KB
Newer Older
1 2 3 4 5 6 7 8 9 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 35 36 37 38 39 40 41 42
/*
 * FIXME header
 */
#include <pproto/pproto.h>
#include <dep/dep.h>



/* Local clock is becoming Master. Table 13 (9.3.5) of the spec. */
void m1(struct pp_instance *ppi)
{
	/* Current data set update */
	DSCUR(ppi)->stepsRemoved = 0;
	DSCUR(ppi)->offsetFromMaster.nanoseconds = 0;
	DSCUR(ppi)->offsetFromMaster.seconds = 0;
	DSCUR(ppi)->meanPathDelay.nanoseconds = 0;
	DSCUR(ppi)->meanPathDelay.seconds = 0;

	/* Parent data set */
	pp_memcpy(DSPAR(ppi)->parentPortIdentity.clockIdentity,
		  DSDEF(ppi)->clockIdentity,PP_CLOCK_IDENTITY_LENGTH);
	DSPAR(ppi)->parentPortIdentity.portNumber = 0;
	DSPAR(ppi)->parentStats = PP_DEFAULT_PARENTS_STATS;
	DSPAR(ppi)->observedParentClockPhaseChangeRate = 0;
	DSPAR(ppi)->observedParentOffsetScaledLogVariance = 0;
	pp_memcpy(DSPAR(ppi)->grandmasterIdentity, DSDEF(ppi)->clockIdentity,
		  PP_CLOCK_IDENTITY_LENGTH);
	DSPAR(ppi)->grandmasterClockQuality.clockAccuracy =
		DSDEF(ppi)->clockQuality.clockAccuracy;
	DSPAR(ppi)->grandmasterClockQuality.clockClass =
		DSDEF(ppi)->clockQuality.clockClass;
	DSPAR(ppi)->grandmasterClockQuality.offsetScaledLogVariance =
		DSDEF(ppi)->clockQuality.offsetScaledLogVariance;
	DSPAR(ppi)->grandmasterPriority1 = DSDEF(ppi)->priority1;
	DSPAR(ppi)->grandmasterPriority2 = DSDEF(ppi)->priority2;

	/* Time Properties data set */
	DSPRO(ppi)->timeSource = INTERNAL_OSCILLATOR;
}


/* Local clock is synchronized to Ebest Table 16 (9.3.5) of the spec. */
43
void s1(MsgHeader *header, MsgAnnounce *ann, struct pp_instance *ppi)
44 45
{
	/* Current DS */
46
	DSCUR(ppi)->stepsRemoved = ann->stepsRemoved + 1;
47 48 49 50 51 52 53 54 55

	/* Parent DS */
	pp_memcpy(DSPAR(ppi)->parentPortIdentity.clockIdentity,
	       header->sourcePortIdentity.clockIdentity,
	       PP_CLOCK_IDENTITY_LENGTH);
	DSPAR(ppi)->parentPortIdentity.portNumber =
		header->sourcePortIdentity.portNumber;

	pp_memcpy(DSPAR(ppi)->grandmasterIdentity,
56
		  ann->grandmasterIdentity, PP_CLOCK_IDENTITY_LENGTH);
57 58

	DSPAR(ppi)->grandmasterClockQuality.clockAccuracy =
59
		ann->grandmasterClockQuality.clockAccuracy;
60
	DSPAR(ppi)->grandmasterClockQuality.clockClass =
61
		ann->grandmasterClockQuality.clockClass;
62
	DSPAR(ppi)->grandmasterClockQuality.offsetScaledLogVariance =
63 64 65
		ann->grandmasterClockQuality.offsetScaledLogVariance;
	DSPAR(ppi)->grandmasterPriority1 = ann->grandmasterPriority1;
	DSPAR(ppi)->grandmasterPriority2 = ann->grandmasterPriority2;
66 67

	/* Timeproperties DS */
68
	DSPRO(ppi)->currentUtcOffset = ann->currentUtcOffset;
69 70 71 72 73 74 75 76 77 78
        /* "Valid" is bit 2 in second octet of flagfield */
	DSPRO(ppi)->currentUtcOffsetValid = ((header->flagField[1] & 0x04) ==
					   0x04);

	DSPRO(ppi)->leap59 = ((header->flagField[1] & 0x02) == 0x02);
	DSPRO(ppi)->leap61 = ((header->flagField[1] & 0x01) == 0x01);
	DSPRO(ppi)->timeTraceable = ((header->flagField[1] & 0x10) == 0x10);
	DSPRO(ppi)->frequencyTraceable = ((header->flagField[1] & 0x20)
					   == 0x20);
	DSPRO(ppi)->ptpTimescale = ((header->flagField[1] & 0x08) == 0x08);
79
	DSPRO(ppi)->timeSource = ann->timeSource;
80 81 82
}


83 84
/* Copy local data set into header and ann message. 9.3.4 table 12. */
void copy_d0(MsgHeader *header, MsgAnnounce *ann, struct pp_instance *ppi)
85
{
86 87
	ann->grandmasterPriority1 = DSDEF(ppi)->priority1;
	pp_memcpy(ann->grandmasterIdentity, DSDEF(ppi)->clockIdentity,
88
	       PP_CLOCK_IDENTITY_LENGTH);
89
	ann->grandmasterClockQuality.clockClass =
90
		DSDEF(ppi)->clockQuality.clockClass;
91
	ann->grandmasterClockQuality.clockAccuracy =
92
		DSDEF(ppi)->clockQuality.clockAccuracy;
93
	ann->grandmasterClockQuality.offsetScaledLogVariance =
94
		DSDEF(ppi)->clockQuality.offsetScaledLogVariance;
95 96
	ann->grandmasterPriority2 = DSDEF(ppi)->priority2;
	ann->stepsRemoved = 0;
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 123 124 125 126 127 128 129 130 131 132 133 134 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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 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 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
	pp_memcpy(header->sourcePortIdentity.clockIdentity,
	       DSDEF(ppi)->clockIdentity, PP_CLOCK_IDENTITY_LENGTH);
}


/*
 * Data set comparison bewteen two foreign masters (9.3.4 fig 27)
 * return similar to memcmp()
 */
Integer8 bmc_dataset_cmp(MsgHeader *hdr_a, MsgAnnounce *ann_a,
		     MsgHeader *hdr_b,MsgAnnounce *ann_b,
		     struct pp_instance *ppi)
{
	/* FIXME DBGV("Data set comparison \n"); */
	short comp = 0;
	Octet *ppci;

	/* Identity comparison */
	if (!pp_memcmp(ann_a->grandmasterIdentity,
		       ann_b->grandmasterIdentity,PP_CLOCK_IDENTITY_LENGTH)) {

		/* Algorithm part2 Fig 28 */
		if (ann_a->stepsRemoved > ann_b->stepsRemoved + 1)
			return 1;

		else if (ann_b->stepsRemoved > ann_a->stepsRemoved + 1)
			return -1;

		else { /* A within 1 of B */

			ppci = DSPAR(ppi)->parentPortIdentity.clockIdentity;

			if (ann_a->stepsRemoved > ann_b->stepsRemoved) {
				if (!pp_memcmp(
					hdr_a->sourcePortIdentity.clockIdentity,
					ppci,
					PP_CLOCK_IDENTITY_LENGTH)) {
					/* FIXME
					 * DBG("Sender=Receiver : Error -1");
					 */
					return 0;
				} else
					return 1;

			} else if (ann_b->stepsRemoved > ann_a->stepsRemoved) {
				if (!pp_memcmp(
					hdr_b->sourcePortIdentity.clockIdentity,
					ppci,
					PP_CLOCK_IDENTITY_LENGTH)) {
					/* FIXME
					 * DBG("Sender=Receiver : Error -1");
					 */
					return 0;
				} else {
					return -1;
				}
			}
			else { /* steps removed A == steps removed B */
				if (!pp_memcmp(
					hdr_a->sourcePortIdentity.clockIdentity,
					hdr_b->sourcePortIdentity.clockIdentity,
					PP_CLOCK_IDENTITY_LENGTH)) {
					/* FIXME
					 * DBG("Sender=Receiver : Error -2");
					 */
					return 0;
				} else if ((pp_memcmp(
					hdr_a->sourcePortIdentity.clockIdentity,
					hdr_b->sourcePortIdentity.clockIdentity,
					PP_CLOCK_IDENTITY_LENGTH)) < 0)
					return -1;
				else
					return 1;
			}
		}
	} else { /* GrandMaster are not identical */
		/* FIXME: rewrite in a more readable way */
		if(ann_a->grandmasterPriority1 == ann_b->grandmasterPriority1) {
			if (ann_a->grandmasterClockQuality.clockClass ==
			    ann_b->grandmasterClockQuality.clockClass) {
				if (ann_a->grandmasterClockQuality.clockAccuracy == ann_b->grandmasterClockQuality.clockAccuracy) {
					if (ann_a->grandmasterClockQuality.offsetScaledLogVariance == ann_b->grandmasterClockQuality.offsetScaledLogVariance) {
						if (ann_a->grandmasterPriority2 == ann_b->grandmasterPriority2) {
							comp = pp_memcmp(ann_a->grandmasterIdentity,ann_b->grandmasterIdentity,PP_CLOCK_IDENTITY_LENGTH);
							if (comp < 0)
								return -1;
							else if (comp > 0)
								return 1;
							else
								return 0;
						} else {
						/* Priority2 are not identical */
							comp =pp_memcmp(&ann_a->grandmasterPriority2,&ann_b->grandmasterPriority2,1);
							if (comp < 0)
								return -1;
							else if (comp > 0)
								return 1;
							else
								return 0;
						}
					} else {
						/* offsetScaledLogVariance are not identical */
						comp= pp_memcmp(&ann_a->grandmasterClockQuality.clockClass,&ann_b->grandmasterClockQuality.clockClass,1);
						if (comp < 0)
							return -1;
						else if (comp > 0)
							return 1;
						else
							return 0;
					}

				} else { /*  Accuracy are not identitcal */
					comp = pp_memcmp(&ann_a->grandmasterClockQuality.clockAccuracy,&ann_b->grandmasterClockQuality.clockAccuracy,1);
					if (comp < 0)
						return -1;
					else if (comp > 0)
						return 1;
					else
						return 0;
				}
			} else { /* ClockClass are not identical */
				comp =  pp_memcmp(&ann_a->grandmasterClockQuality.clockClass,&ann_b->grandmasterClockQuality.clockClass,1);
				if (comp < 0)
					return -1;
				else if (comp > 0)
					return 1;
				else
					return 0;
			}
		} else { /*  Priority1 are not identical */
			comp =  pp_memcmp(&ann_a->grandmasterPriority1,&ann_b->grandmasterPriority1,1);
			if (comp < 0)
				return -1;
			else if (comp > 0)
				return 1;
			else
				return 0;
		}
	}
}

/* State decision algorithm 9.3.3 Fig 26 */
UInteger8 bmc_state_decision(MsgHeader *hdr, MsgAnnounce *ann,
		 struct pp_runtime_opts *rt_opts, struct pp_instance *ppi)
{
	int cmpres;

	if (rt_opts->slave_only) {
		s1(hdr, ann, ppi);
		return PPS_SLAVE;
	}

	if ((!ppi->number_foreign_records) && (ppi->state == PPS_LISTENING))
		return PPS_LISTENING;

	copy_d0(&ppi->msg_tmp_header, &ppi->msg_tmp.announce, ppi);


	cmpres = bmc_dataset_cmp(&ppi->msg_tmp_header,
				 &ppi->msg_tmp.announce,
				 hdr,ann,ppi);

	if (DSDEF(ppi)->clockQuality.clockClass < 128) {

		if (cmpres < 0) {
			m1(ppi);
			return PPS_MASTER;
		} else if (cmpres > 0) {
			s1(hdr, ann, ppi);
			return PPS_PASSIVE;
		} else {
			/* FIXME DBG("Error in bmcDataSetComparison..\n"); */
		}

	} else {

		if (cmpres < 0) {
			m1(ppi);
			return PPS_MASTER;
		} else if (cmpres > 0) {
			s1(hdr,ann,ppi);
			return PPS_SLAVE;
		} else {
			/* FIXME DBG("Error in bmcDataSetComparison..\n"); */
		}

	}

	/*  MB: Is this the return code below correct? */
	/*  Anyway, it's a valid return code. */
	return PPS_FAULTY;
}



UInteger8 bmc(struct pp_frgn_master *frgn_master,
	      struct pp_runtime_opts *rt_opts, struct pp_instance *ppi)
{
	Integer16 i, best;

	if (!ppi->number_foreign_records)
		if (ppi->state == PPS_MASTER)	{
			m1(ppi);
			return ppi->state;
		}

	for (i=1, best = 0; i < ppi->number_foreign_records; i++)
		if ((bmc_dataset_cmp(&frgn_master[i].hdr,
				     &frgn_master[i].ann,
				     &frgn_master[best].hdr,
				     &frgn_master[best].ann,
				     ppi)) < 0)
			best = i;

	/* FIXME DBGV("Best record : %d \n",best); */
	ppi->foreign_record_best = best;

	return (bmc_state_decision(&frgn_master[best].hdr,
				   &frgn_master[best].ann,
				   rt_opts,ppi));
}