msg.c 17.8 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 8

/* Unpack header from in buffer to msg_tmp_header field */
9
void msg_unpack_header(struct pp_instance *ppi, void *buf)
10
{
11 12
	MsgHeader *hdr = &ppi->msg_tmp_header;

13 14 15
	hdr->transportSpecific = (*(Nibble *) (buf + 0)) >> 4;
	hdr->messageType = (*(Enumeration4 *) (buf + 0)) & 0x0F;
	hdr->versionPTP = (*(UInteger4 *) (buf + 1)) & 0x0F;
16

17
	/* force reserved bit to zero if not */
18
	hdr->messageLength = htons(*(UInteger16 *) (buf + 2));
19 20 21 22 23 24
	hdr->domainNumber = (*(UInteger8 *) (buf + 4));

	/* pp_memcpy(hdr->flagField, (buf + 6), FIXME:FLAG_FIELD_LENGTH); */

	pp_memcpy(&hdr->correctionfield.msb, (buf + 8), 4);
	pp_memcpy(&hdr->correctionfield.lsb, (buf + 12), 4);
25 26
	hdr->correctionfield.msb = htonl(hdr->correctionfield.msb);
	hdr->correctionfield.lsb = htonl(hdr->correctionfield.lsb);
27 28 29
	pp_memcpy(hdr->sourcePortIdentity.clockIdentity, (buf + 20),
	       PP_CLOCK_IDENTITY_LENGTH);
	hdr->sourcePortIdentity.portNumber =
30 31
		htons(*(UInteger16 *) (buf + 28));
	hdr->sequenceId = htons(*(UInteger16 *) (buf + 30));
32 33 34
	hdr->controlField = (*(UInteger8 *) (buf + 32));
	hdr->logMessageInterval = (*(Integer8 *) (buf + 33));

35 36 37 38 39 40 41 42 43
	if (DSPOR(ppi)->portIdentity.portNumber ==
	    ppi->msg_tmp_header.sourcePortIdentity.portNumber
	    && !pp_memcmp(ppi->msg_tmp_header.sourcePortIdentity.clockIdentity,
			DSPOR(ppi)->portIdentity.clockIdentity,
			PP_CLOCK_IDENTITY_LENGTH))
		ppi->is_from_self = 1;
	else
		ppi->is_from_self = 0;

44 45 46 47 48 49 50 51
	if (!pp_memcmp(DSPAR(ppi)->parentPortIdentity.clockIdentity,
			hdr->sourcePortIdentity.clockIdentity,
			PP_CLOCK_IDENTITY_LENGTH) &&
			(DSPAR(ppi)->parentPortIdentity.portNumber ==
			hdr->sourcePortIdentity.portNumber))
		ppi->is_from_cur_par = 1;
	else
		ppi->is_from_cur_par = 0;
52

53
/* FIXME: diag
54 55
#ifdef PTPD_DBG
	msgHeader_display(header);
56 57
#endif
*/
58 59
}

60
/* Pack header message into out buffer of ppi */
61
void msg_pack_header(struct pp_instance *ppi, void *buf)
62 63 64 65 66
{
	Nibble transport = 0x80;

	/* (spec annex D) */
	*(UInteger8 *) (buf + 0) = transport;
67 68
	*(UInteger4 *) (buf + 1) = DSPOR(ppi)->versionNumber;
	*(UInteger8 *) (buf + 4) = DSDEF(ppi)->domainNumber;
69

70 71
	if (DSDEF(ppi)->twoStepFlag)
		*(UInteger8 *) (buf + 6) = PP_TWO_STEP_FLAG;
72

73 74 75 76
	pp_memset((buf + 8), 0, 8);
	pp_memcpy((buf + 20), DSPOR(ppi)->portIdentity.clockIdentity,
	       PP_CLOCK_IDENTITY_LENGTH);
	*(UInteger16 *) (buf + 28) =
77
				htons(DSPOR(ppi)->portIdentity.portNumber);
78 79 80 81
	*(UInteger8 *) (buf + 33) = 0x7F;
	/* Default value(spec Table 24) */
}

82
void *msg_copy_header(MsgHeader *dest, MsgHeader *src)
83
{
84
	return pp_memcpy(dest, src, sizeof(MsgHeader));
85 86
}

87

88
/* Pack Sync message into out buffer of ppi */
89
void msg_pack_sync(struct pp_instance *ppi, Timestamp *orig_tstamp)
90
{
91 92 93 94
	void *buf;

	buf = ppi->buf_out;

95 96 97 98
	/* changes in header */
	*(char *)(buf + 0) = *(char *)(buf + 0) & 0xF0;
	/* RAZ messageType */
	*(char *)(buf + 0) = *(char *)(buf + 0) | 0x00;
99

100
	/* Table 19 */
101 102
	*(UInteger16 *) (buf + 2) = htons(PP_SYNC_LENGTH);
	*(UInteger16 *) (buf + 30) = htons(ppi->sent_seq_id[PPM_SYNC]);
103
	*(UInteger8 *) (buf + 32) = 0x00;
104

105
	/* Table 23 */
106 107
	*(Integer8 *) (buf + 33) = DSPOR(ppi)->logSyncInterval;
	pp_memset((buf + 8), 0, 8);
108 109

	/* Sync message */
110 111 112
	*(UInteger16 *) (buf + 34) = htons(orig_tstamp->secondsField.msb);
	*(UInteger32 *) (buf + 36) = htonl(orig_tstamp->secondsField.lsb);
	*(UInteger32 *) (buf + 40) = htonl(orig_tstamp->nanosecondsField);
113 114
}

115 116
/* Unpack Sync message from in buffer */
void msg_unpack_sync(void *buf, MsgSync *sync)
117
{
118
	sync->originTimestamp.secondsField.msb =
119
		htons(*(UInteger16 *) (buf + 34));
120
	sync->originTimestamp.secondsField.lsb =
121
		htonl(*(UInteger32 *) (buf + 36));
122
	sync->originTimestamp.nanosecondsField =
123
		htonl(*(UInteger32 *) (buf + 40));
124

125
/* FIXME: diag
126 127
#ifdef PTPD_DBG
	msgSync_display(sync);
128 129
#endif
*/
130 131
}

132
/* Pack Announce message into out buffer of ppi */
133
void msg_pack_announce(struct pp_instance *ppi)
134
{
135 136 137
	void *buf;

	buf = ppi->buf_out;
138 139 140 141 142
	/* changes in header */
	*(char *)(buf + 0) = *(char *)(buf + 0) & 0xF0;
	/* RAZ messageType */
	*(char *)(buf + 0) = *(char *)(buf + 0) | 0x0B;
	/* Table 19 */
143 144
	*(UInteger16 *) (buf + 2) = htons(PP_ANNOUNCE_LENGTH);
	*(UInteger16 *) (buf + 30) = htons(ppi->sent_seq_id[PPM_ANNOUNCE]);
145 146
	*(UInteger8 *) (buf + 32) = 0x05;
	/* Table 23 */
147
	*(Integer8 *) (buf + 33) = DSPOR(ppi)->logAnnounceInterval;
148 149

	/* Announce message */
150
	pp_memset((buf + 34), 0, 10);
151
	*(Integer16 *) (buf + 44) = htons(DSPRO(ppi)->currentUtcOffset);
152 153 154
	*(UInteger8 *) (buf + 47) = DSPAR(ppi)->grandmasterPriority1;
	*(UInteger8 *) (buf + 48) = DSDEF(ppi)->clockQuality.clockClass;
	*(Enumeration8 *) (buf + 49) = DSDEF(ppi)->clockQuality.clockAccuracy;
155
	*(UInteger16 *) (buf + 50) =
156
		htons(DSDEF(ppi)->clockQuality.offsetScaledLogVariance);
157 158 159
	*(UInteger8 *) (buf + 52) = DSPAR(ppi)->grandmasterPriority2;
	pp_memcpy((buf + 53), DSPAR(ppi)->grandmasterIdentity,
	       PP_CLOCK_IDENTITY_LENGTH);
160
	*(UInteger16 *) (buf + 61) = htons(DSCUR(ppi)->stepsRemoved);
161
	*(Enumeration8 *) (buf + 63) = DSPRO(ppi)->timeSource;
162 163
}

164 165
/* Unpack Announce message from in buffer of ppi to msgtmp. Announce */
void msg_unpack_announce(void *buf, MsgAnnounce *ann)
166
{
167
	ann->originTimestamp.secondsField.msb =
168
		htons(*(UInteger16 *) (buf + 34));
169
	ann->originTimestamp.secondsField.lsb =
170
		htonl(*(UInteger32 *) (buf + 36));
171
	ann->originTimestamp.nanosecondsField =
172 173
		htonl(*(UInteger32 *) (buf + 40));
	ann->currentUtcOffset = htons(*(UInteger16 *) (buf + 44));
174 175
	ann->grandmasterPriority1 = *(UInteger8 *) (buf + 47);
	ann->grandmasterClockQuality.clockClass =
176
		*(UInteger8 *) (buf + 48);
177
	ann->grandmasterClockQuality.clockAccuracy =
178
		*(Enumeration8 *) (buf + 49);
179
	ann->grandmasterClockQuality.offsetScaledLogVariance =
180
		htons(*(UInteger16 *) (buf + 50));
181 182 183
	ann->grandmasterPriority2 = *(UInteger8 *) (buf + 52);
	pp_memcpy(ann->grandmasterIdentity, (buf + 53),
	       PP_CLOCK_IDENTITY_LENGTH);
184
	ann->stepsRemoved = htons(*(UInteger16 *) (buf + 61));
185 186 187
	ann->timeSource = *(Enumeration8 *) (buf + 63);

/* FIXME: diag
188 189
#ifdef PTPD_DBG
	msgAnnounce_display(announce);
190 191
#endif
*/
192 193
}

194 195

/* Pack Follow Up message into out buffer of ppi*/
196
void msg_pack_follow_up(struct pp_instance *ppi, Timestamp *prec_orig_tstamp)
197
{
198 199 200 201
	void *buf;

	buf = ppi->buf_out;

202 203 204 205
	/* changes in header */
	*(char *)(buf + 0) = *(char *)(buf + 0) & 0xF0;
	/* RAZ messageType */
	*(char *)(buf + 0) = *(char *)(buf + 0) | 0x08;
206

207
	/* Table 19 */
208 209
	*(UInteger16 *) (buf + 2) = htons(PP_FOLLOW_UP_LENGTH);
	*(UInteger16 *) (buf + 30) = htons(ppi->sent_seq_id[PPM_SYNC] - 1);
210

211
	/* sentSyncSequenceId has already been incremented in msg_issue_sync */
212
	*(UInteger8 *) (buf + 32) = 0x02;
213

214
	/* Table 23 */
215
	*(Integer8 *) (buf + 33) = DSPOR(ppi)->logSyncInterval;
216

217
	/* Follow Up message */
218
	*(UInteger16 *) (buf + 34) =
219
		htons(prec_orig_tstamp->secondsField.msb);
220
	*(UInteger32 *) (buf + 36) =
221
		htonl(prec_orig_tstamp->secondsField.lsb);
222
	*(UInteger32 *) (buf + 40) =
223
		htonl(prec_orig_tstamp->nanosecondsField);
224 225
}

226 227
/* Unpack FollowUp message from in buffer of ppi to msgtmp.follow */
void msg_unpack_follow_up(void *buf, MsgFollowUp *flwup)
228
{
229
	flwup->preciseOriginTimestamp.secondsField.msb =
230
		htons(*(UInteger16 *) (buf + 34));
231
	flwup->preciseOriginTimestamp.secondsField.lsb =
232
		htonl(*(UInteger32 *) (buf + 36));
233
	flwup->preciseOriginTimestamp.nanosecondsField =
234
		htonl(*(UInteger32 *) (buf + 40));
235 236

/* FIXME: diag
237 238
#ifdef PTPD_DBG
	msgFollowUp_display(follow);
239 240
#endif
*/
241 242
}

243
/* pack PdelayReq message into out buffer of ppi */
244
void msg_pack_pdelay_req(struct pp_instance *ppi, Timestamp *orig_tstamp)
245
{
246 247 248 249
	void *buf;

	buf = ppi->buf_out;

250 251 252
	/* changes in header */
	*(char *)(buf + 0) = *(char *)(buf + 0) & 0xF0;
	/* RAZ messageType */
253

254
	*(char *)(buf + 0) = *(char *)(buf + 0) | 0x02;
255

256
	/* Table 19 */
257 258
	*(UInteger16 *) (buf + 2) = htons(PP_PDELAY_REQ_LENGTH);
	*(UInteger16 *) (buf + 30) = htons(ppi->sent_seq_id[PPM_PDELAY_REQ]);
259
	*(UInteger8 *) (buf + 32) = 0x05;
260

261 262
	/* Table 23 */
	*(Integer8 *) (buf + 33) = 0x7F;
263

264
	/* Table 24 */
265
	pp_memset((buf + 8), 0, 8);
266 267

	/* Pdelay_req message */
268 269 270
	*(UInteger16 *) (buf + 34) = htons(orig_tstamp->secondsField.msb);
	*(UInteger32 *) (buf + 36) = htonl(orig_tstamp->secondsField.lsb);
	*(UInteger32 *) (buf + 40) = htonl(orig_tstamp->nanosecondsField);
271

272
	pp_memset((buf + 44), 0, 10);
273 274 275
	/* RAZ reserved octets */
}

276

277 278
/* pack DelayReq message into out buffer of ppi */
void msg_pack_delay_req(struct pp_instance *ppi, Timestamp *orig_tstamp)
279
{
280 281 282 283
	void *buf;

	buf = ppi->buf_out;

284 285 286 287
	/* changes in header */
	*(char *)(buf + 0) = *(char *)(buf + 0) & 0xF0;
	/* RAZ messageType */
	*(char *)(buf + 0) = *(char *)(buf + 0) | 0x01;
288

289
	/* Table 19 */
290 291
	*(UInteger16 *) (buf + 2) = htons(PP_DELAY_REQ_LENGTH);
	*(UInteger16 *) (buf + 30) = htons(ppi->sent_seq_id[PPM_DELAY_REQ]);
292
	*(UInteger8 *) (buf + 32) = 0x01;
293

294 295
	/* Table 23 */
	*(Integer8 *) (buf + 33) = 0x7F;
296

297
	/* Table 24 */
298
	pp_memset((buf + 8), 0, 8);
299 300

	/* Pdelay_req message */
301 302 303
	*(UInteger16 *) (buf + 34) = htons(orig_tstamp->secondsField.msb);
	*(UInteger32 *) (buf + 36) = htonl(orig_tstamp->secondsField.lsb);
	*(UInteger32 *) (buf + 40) = htonl(orig_tstamp->nanosecondsField);
304 305
}

306

307 308
/* pack DelayResp message into OUT buffer of ppi */
void msg_pack_delay_resp(struct pp_instance *ppi,
309
			 MsgHeader *hdr, Timestamp *rcv_tstamp)
310
{
311 312 313 314
	void *buf;

	buf = ppi->buf_out;

315 316 317 318
	/* changes in header */
	*(char *)(buf + 0) = *(char *)(buf + 0) & 0xF0;
	/* RAZ messageType */
	*(char *)(buf + 0) = *(char *)(buf + 0) | 0x09;
319

320
	/* Table 19 */
321
	*(UInteger16 *) (buf + 2) = htons(PP_DELAY_RESP_LENGTH);
322 323
	*(UInteger8 *) (buf + 4) = hdr->domainNumber;
	pp_memset((buf + 8), 0, 8);
324 325

	/* Copy correctionField of PdelayReqMessage */
326 327
	*(Integer32 *) (buf + 8) = htonl(hdr->correctionfield.msb);
	*(Integer32 *) (buf + 12) = htonl(hdr->correctionfield.lsb);
328

329
	*(UInteger16 *) (buf + 30) = htons(hdr->sequenceId);
330

331
	*(UInteger8 *) (buf + 32) = 0x03;
332

333
	/* Table 23 */
334 335
	*(Integer8 *) (buf + 33) = DSPOR(ppi)->logMinDelayReqInterval;

336 337 338
	/* Table 24 */

	/* Pdelay_resp message */
339
	*(UInteger16 *) (buf + 34) =
340 341 342
		htons(rcv_tstamp->secondsField.msb);
	*(UInteger32 *) (buf + 36) = htonl(rcv_tstamp->secondsField.lsb);
	*(UInteger32 *) (buf + 40) = htonl(rcv_tstamp->nanosecondsField);
343 344
	pp_memcpy((buf + 44), hdr->sourcePortIdentity.clockIdentity,
		  PP_CLOCK_IDENTITY_LENGTH);
345
	*(UInteger16 *) (buf + 52) =
346
		htons(hdr->sourcePortIdentity.portNumber);
347 348 349 350
}



351
/* Pack PdelayResp message into out buffer of ppi */
352
void msg_pack_pdelay_resp(struct pp_instance *ppi, MsgHeader *hdr,
353
			  Timestamp *req_rec_tstamp)
354
{
355 356 357 358
	void *buf;

	buf = ppi->buf_out;

359 360 361 362 363
	/* changes in header */
	*(char *)(buf + 0) = *(char *)(buf + 0) & 0xF0;
	/* RAZ messageType */
	*(char *)(buf + 0) = *(char *)(buf + 0) | 0x03;
	/* Table 19 */
364
	*(UInteger16 *) (buf + 2) = htons(PP_PDELAY_RESP_LENGTH);
365 366
	*(UInteger8 *) (buf + 4) = hdr->domainNumber;
	pp_memset((buf + 8), 0, 8);
367

368
	*(UInteger16 *) (buf + 30) = htons(hdr->sequenceId);
369 370 371 372 373 374 375

	*(UInteger8 *) (buf + 32) = 0x05;
	/* Table 23 */
	*(Integer8 *) (buf + 33) = 0x7F;
	/* Table 24 */

	/* Pdelay_resp message */
376 377 378
	*(UInteger16 *) (buf + 34) = htons(req_rec_tstamp->secondsField.msb);
	*(UInteger32 *) (buf + 36) = htonl(req_rec_tstamp->secondsField.lsb);
	*(UInteger32 *) (buf + 40) = htonl(req_rec_tstamp->nanosecondsField);
379 380 381
	pp_memcpy((buf + 44), hdr->sourcePortIdentity.clockIdentity,
		  PP_CLOCK_IDENTITY_LENGTH);
	*(UInteger16 *) (buf + 52) =
382
		htons(hdr->sourcePortIdentity.portNumber);
383 384 385 386

}


387 388
/* Unpack delayReq message from in buffer of ppi to msgtmp.req */
void msg_unpack_delay_req(void *buf, MsgDelayReq *delay_req)
389
{
390
	delay_req->originTimestamp.secondsField.msb =
391
		htons(*(UInteger16 *) (buf + 34));
392
	delay_req->originTimestamp.secondsField.lsb =
393
		htonl(*(UInteger32 *) (buf + 36));
394
	delay_req->originTimestamp.nanosecondsField =
395
		htonl(*(UInteger32 *) (buf + 40));
396
/* FIXME: diag
397 398
#ifdef PTPD_DBG
	msgDelayReq_display(delayreq);
399 400
#endif
*/
401 402 403 404
}



405 406 407 408
/* Unpack PdelayReq message from IN buffer of ppi to msgtmp.req */
void msg_unpack_pdelay_req(void *buf, MsgPDelayReq *pdelay_req)
{
	pdelay_req->originTimestamp.secondsField.msb =
409
		htons(*(UInteger16 *) (buf + 34));
410
	pdelay_req->originTimestamp.secondsField.lsb =
411
		htonl(*(UInteger32 *) (buf + 36));
412
	pdelay_req->originTimestamp.nanosecondsField =
413
		htonl(*(UInteger32 *) (buf + 40));
414
/* FIXME: diag
415 416
#ifdef PTPD_DBG
	msgPDelayReq_display(pdelayreq);
417 418
#endif
*/
419 420
}

421 422
/* Unpack delayResp message from IN buffer of ppi to msgtmp.presp */
void msg_unpack_delay_resp(void *buf, MsgDelayResp *resp)
423
{
424
	resp->receiveTimestamp.secondsField.msb =
425
		htons(*(UInteger16 *) (buf + 34));
426
	resp->receiveTimestamp.secondsField.lsb =
427
		htonl(*(UInteger32 *) (buf + 36));
428
	resp->receiveTimestamp.nanosecondsField =
429
		htonl(*(UInteger32 *) (buf + 40));
430
	pp_memcpy(resp->requestingPortIdentity.clockIdentity,
431
	       (buf + 44), PP_CLOCK_IDENTITY_LENGTH);
432
	resp->requestingPortIdentity.portNumber =
433
		htons(*(UInteger16 *) (buf + 52));
434

435
/* FIXME: diag
436 437
#ifdef PTPD_DBG
	msgDelayResp_display(resp);
438 439
#endif
*/
440 441
}

442 443
/* Unpack PdelayResp message from IN buffer of ppi to msgtmp.presp */
void msg_unpack_pdelay_resp(void *buf, MsgPDelayResp *presp)
444
{
445
	presp->requestReceiptTimestamp.secondsField.msb =
446
		htons(*(UInteger16 *) (buf + 34));
447
	presp->requestReceiptTimestamp.secondsField.lsb =
448
		htonl(*(UInteger32 *) (buf + 36));
449
	presp->requestReceiptTimestamp.nanosecondsField =
450
		htonl(*(UInteger32 *) (buf + 40));
451 452 453
	pp_memcpy(presp->requestingPortIdentity.clockIdentity,
	       (buf + 44), PP_CLOCK_IDENTITY_LENGTH);
	presp->requestingPortIdentity.portNumber =
454
		htons(*(UInteger16 *) (buf + 52));
455 456

/* FIXME: diag
457 458
#ifdef PTPD_DBG
	msgPDelayResp_display(presp);
459 460
#endif
*/
461 462
}

463
/* Pack PdelayRespFollowUp message into out buffer of ppi */
464
void msg_pack_pdelay_resp_followup(struct pp_instance *ppi,
465 466
				   MsgHeader *hdr,
				   Timestamp *resp_orig_tstamp)
467
{
468 469 470 471
	void *buf;

	buf = ppi->buf_out;

472 473 474 475
	/* changes in header */
	*(char *)(buf + 0) = *(char *)(buf + 0) & 0xF0;
	/* RAZ messageType */
	*(char *)(buf + 0) = *(char *)(buf + 0) | 0x0A;
476

477
	/* Table 19 */
478 479
	*(UInteger16 *) (buf + 2) = htons(PP_PDELAY_RESP_FOLLOW_UP_LENGTH);
	*(UInteger16 *) (buf + 30) = htons(ppi->pdelay_req_hdr.sequenceId);
480
	*(UInteger8 *) (buf + 32) = 0x05;
481

482 483
	/* Table 23 */
	*(Integer8 *) (buf + 33) = 0x7F;
484

485 486 487
	/* Table 24 */

	/* Copy correctionField of PdelayReqMessage */
488 489
	*(Integer32 *) (buf + 8) = htonl(hdr->correctionfield.msb);
	*(Integer32 *) (buf + 12) = htonl(hdr->correctionfield.lsb);
490 491

	/* Pdelay_resp_follow_up message */
492
	*(UInteger16 *) (buf + 34) =
493
		htons(resp_orig_tstamp->secondsField.msb);
494
	*(UInteger32 *) (buf + 36) =
495
		htonl(resp_orig_tstamp->secondsField.lsb);
496
	*(UInteger32 *) (buf + 40) =
497
		htonl(resp_orig_tstamp->nanosecondsField);
498 499 500
	pp_memcpy((buf + 44), hdr->sourcePortIdentity.clockIdentity,
	       PP_CLOCK_IDENTITY_LENGTH);
	*(UInteger16 *) (buf + 52) =
501
		htons(hdr->sourcePortIdentity.portNumber);
502 503
}

504 505 506
/* Unpack PdelayResp message from in buffer of ppi to msgtmp.presp */
void msg_unpack_pdelay_resp_followup(void *buf,
	MsgPDelayRespFollowUp *presp_follow)
507
{
508
	presp_follow->responseOriginTimestamp.secondsField.msb =
509
		htons(*(UInteger16 *) (buf + 34));
510
	presp_follow->responseOriginTimestamp.secondsField.lsb =
511
		htonl(*(UInteger32 *) (buf + 36));
512
	presp_follow->responseOriginTimestamp.nanosecondsField =
513
		htonl(*(UInteger32 *) (buf + 40));
514 515 516
	pp_memcpy(presp_follow->requestingPortIdentity.clockIdentity,
	       (buf + 44), PP_CLOCK_IDENTITY_LENGTH);
	presp_follow->requestingPortIdentity.portNumber =
517
		htons(*(UInteger16 *) (buf + 52));
518
}
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619

/* FIXME diag in the following macro */
#define MSG_SEND_AND_RET(x,y)\
	if (pp_send_packet(ppi, ppi->buf_out, PP_## x ##_LENGTH, PP_NP_## y) <\
		PP_## x ##_LENGTH) {\
		return -1;\
	}\
	ppi->sent_seq_id[PPM_## x]++;\
	return 0;

/* Pack and send on general multicast ip adress an Announce message */
int msg_issue_announce(struct pp_instance *ppi)
{
	msg_pack_announce(ppi);

	MSG_SEND_AND_RET(ANNOUNCE, GEN);
}

/* Pack and send on event multicast ip adress a Sync message */
int msg_issue_sync(struct pp_instance *ppi)
{
	Timestamp orig_tstamp;
	TimeInternal now;
	pp_get_tstamp(&now);
	from_TimeInternal(&now, &orig_tstamp);

	msg_pack_sync(ppi,&orig_tstamp);

	MSG_SEND_AND_RET(SYNC, EVT);
}

/* Pack and send on general multicast ip adress a FollowUp message */
int msg_issue_followup(struct pp_instance *ppi, TimeInternal *time)
{
	Timestamp prec_orig_tstamp;
	from_TimeInternal(time, &prec_orig_tstamp);

	msg_pack_follow_up(ppi, &prec_orig_tstamp);

	MSG_SEND_AND_RET(FOLLOW_UP, GEN);
}

/* Pack and send on event multicast ip adress a DelayReq message */
int msg_issue_delay_req(struct pp_instance *ppi)
{
	Timestamp orig_tstamp;
	TimeInternal now;
	pp_get_tstamp(&now);
	from_TimeInternal(&now, &orig_tstamp);

	msg_pack_delay_req(ppi, &orig_tstamp);

	MSG_SEND_AND_RET(DELAY_REQ, EVT);
}

/* Pack and send on event multicast ip adress a PDelayReq message */
int msg_issue_pdelay_req(struct pp_instance *ppi)
{
	Timestamp orig_tstamp;
	TimeInternal now;
	pp_get_tstamp(&now);
	from_TimeInternal(&now, &orig_tstamp);

	msg_pack_pdelay_req(ppi, &orig_tstamp);

	MSG_SEND_AND_RET(PDELAY_REQ, EVT);
}

/* Pack and send on event multicast ip adress a PDelayResp message */
int msg_issue_pdelay_resp(struct pp_instance *ppi, TimeInternal *time,
			MsgHeader *hdr)
{
	Timestamp req_rec_tstamp;
	from_TimeInternal(time, &req_rec_tstamp);
	msg_pack_pdelay_resp(ppi, hdr, &req_rec_tstamp);

	MSG_SEND_AND_RET(PDELAY_RESP, EVT);
}

/* Pack and send on event multicast ip adress a DelayResp message */
int msg_issue_delay_resp(struct pp_instance *ppi, TimeInternal *time)
{
	Timestamp rcv_tstamp;
	from_TimeInternal(time, &rcv_tstamp);

	msg_pack_delay_resp(ppi, &ppi->delay_req_hdr, &rcv_tstamp);

	MSG_SEND_AND_RET(PDELAY_RESP, GEN);
}

/* Pack and send on event multicast ip adress a DelayResp message */
int msg_issue_pdelay_resp_follow_up(struct pp_instance *ppi, TimeInternal *time)
{
	Timestamp resp_orig_tstamp;
	from_TimeInternal(time, &resp_orig_tstamp);

	msg_pack_pdelay_resp_followup(ppi, &ppi->pdelay_req_hdr,
		&resp_orig_tstamp);

	MSG_SEND_AND_RET(PDELAY_RESP_FOLLOW_UP, GEN);
}