summaryrefslogtreecommitdiff
path: root/lib/avtp_pipeline/openavb_common/mrp_client.c
blob: c7b28e63b5d44dfb67aa87c0c7068f0dbad1b743 (plain)
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
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
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
/******************************************************************************

  Copyright (c) 2012, Intel Corporation
  Copyright (c) 2014, Parrot SA
  All rights reserved.
  
  Redistribution and use in source and binary forms, with or without 
  modification, are permitted provided that the following conditions are met:
  
   1. Redistributions of source code must retain the above copyright notice, 
      this list of conditions and the following disclaimer.
  
   2. Redistributions in binary form must reproduce the above copyright 
      notice, this list of conditions and the following disclaimer in the 
      documentation and/or other materials provided with the distribution.
  
   3. Neither the name of the Intel Corporation nor the names of its 
      contributors may be used to endorse or promote products derived from 
      this software without specific prior written permission.
  
  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  POSSIBILITY OF SUCH DAMAGE.

******************************************************************************/

#include "mrp_client.h"

#define AVB_LOG_COMPONENT "MRP"
#include "openavb_log.h"

/* global variables */

int control_socket = -1;

volatile int halt_tx = 0;
volatile int listeners = 0;
volatile int mrp_okay;
volatile int mrp_error = 0;;

volatile int domain_a_valid = 0;
int domain_class_a_id = 0;
int domain_class_a_priority = 0;
u_int16_t domain_class_a_vid = 0;

volatile int domain_b_valid = 0;
int domain_class_b_id = 0;
int domain_class_b_priority = 0;
u_int16_t domain_class_b_vid = 0;

pthread_t monitor_thread;
pthread_attr_t monitor_attr;
unsigned char monitor_stream_id[] = { 0, 0, 0, 0, 0, 0, 0, 0 };

/*
 * private
 */

int send_mrp_msg(char *notify_data, int notify_len)
{
	struct sockaddr_in addr;
	socklen_t addr_len;

	if (control_socket == -1)
		return -1;
	if (notify_data == NULL)
		return -1;

	memset(&addr, 0, sizeof(addr));
	addr.sin_family = AF_INET;
	addr.sin_port = htons(MRPD_PORT_DEFAULT);
	inet_aton("127.0.0.1", &addr.sin_addr);
	addr_len = sizeof(addr);
	return sendto(control_socket, notify_data, notify_len, 0,
			 (struct sockaddr *)&addr, addr_len);
}

int process_mrp_msg(char *buf, int buflen)
{
	/*
	 * 1st character indicates application
	 * [MVS] - MAC, VLAN or STREAM
	 */
	unsigned int id;
	unsigned int priority;
	unsigned int vid;
	unsigned int max_frame_size;
	unsigned int max_interval_frames;
	unsigned int priority_and_rank;
	unsigned int latency;
	int i, j;
	unsigned int substate;
	unsigned char recovered_streamid[8];
	unsigned char dest_addr[6];
	int offset = 0;

#if (AVB_LOG_LEVEL_DEBUG <= AVB_LOG_LEVEL)
	// Display this response in a user-friendly format.
	static char szMrpData[MAX_MRPD_CMDSZ];
	int nMrpDataLength = (buflen < MAX_MRPD_CMDSZ - 1 ? buflen : MAX_MRPD_CMDSZ - 1);
	strncpy(szMrpData, buf, nMrpDataLength);
	szMrpData[nMrpDataLength] = '\0';
	while (nMrpDataLength > 0 &&
			(buf[nMrpDataLength - 1] == '\0' || buf[nMrpDataLength - 1] == '\r' || buf[nMrpDataLength - 1] == '\n')) {
		// Remove the trailing whitespace.
		szMrpData[--nMrpDataLength] = '\0';
	}
	AVB_LOGF_DEBUG("MRP Response:  %s", szMrpData);
#endif

	while (offset < buflen && buf[offset] != '\0') {

		switch (buf[offset]) {
		case 'E':
			mrp_error = 1;
			break;

		case 'O':
			mrp_okay = 1;
			break;

		case 'M':
		case 'V':
			/* unhandled for now */
			break;

		case 'L':

			/* parse a listener attribute - see if it matches our monitor_stream_id */
			i = offset;
			while (buf[i] != 'D')
				i++;
			i += 2;		/* skip the ':' */
			sscanf(&(buf[i]), "%d", &substate);
			while (buf[i] != 'S')
				i++;
			i += 2;		/* skip the ':' */
			for (j = 0; j < 8; j++) {
				sscanf(&(buf[i + 2 * j]), "%02x", &id);
				recovered_streamid[j] = (unsigned char)id;
			}
			AVB_LOGF_DEBUG
				("FOUND STREAM ID=%02x%02x%02x%02x%02x%02x%02x%02x ",
				 recovered_streamid[0], recovered_streamid[1],
				 recovered_streamid[2], recovered_streamid[3],
				 recovered_streamid[4], recovered_streamid[5],
				 recovered_streamid[6], recovered_streamid[7]);
			switch (substate) {
			case 0:
				AVB_LOG_DEBUG("with state ignore");
				break;
			case 1:
				AVB_LOG_DEBUG("with state askfailed");
				break;
			case 2:
				AVB_LOG_DEBUG("with state ready");
				break;
			case 3:
				AVB_LOG_DEBUG("with state readyfail");
				break;
			default:
				AVB_LOGF_DEBUG("with state UNKNOWN (%d)", substate);
				break;
			}
			mrp_attach_cb(recovered_streamid, substate);
			if (substate > MSRP_LISTENER_ASKFAILED) {
				if (memcmp
					(recovered_streamid, monitor_stream_id,
					 sizeof(recovered_streamid)) == 0) {
					listeners = 1;
					AVB_LOG_DEBUG("added listener");
				}
			}
			break;

		case 'D':
			i = offset + 4;

			/* save the domain attribute */
			sscanf(&(buf[i]), "%d", &id);
			while (buf[i] != 'P')
				i++;
			i += 2;		/* skip the ':' */
			sscanf(&(buf[i]), "%d", &priority);
			while (buf[i] != 'V')
				i++;
			i += 2;		/* skip the ':' */
			sscanf(&(buf[i]), "%x", &vid);
			if (id == 6) {
				domain_class_a_id = id;
				domain_class_a_priority = priority;
				domain_class_a_vid = vid;
				domain_a_valid = 1;
			} else {
				domain_class_b_id = id;
				domain_class_b_priority = priority;
				domain_class_b_vid = vid;
				domain_b_valid = 1;
			}
			break;

		case 'T':
			i = offset;
			while (buf[i] != 'S')
				i++;
			// skip S=
			i += 2;
			for (j = 0; j < 8; j++) {
				sscanf(&(buf[i + 2 * j]), "%02x", &id);
				recovered_streamid[j] = (unsigned char)id;
			}
			while (buf[i] != 'A')
				i++;
			// skip A=
			i += 2;
			for (j = 0; j < 6; j++) {
				sscanf(&(buf[i + 2 * j]), "%02x", &id);
				dest_addr[j] = (unsigned char)id;
			}
			i+= j*2 + 1;

			sscanf(&(buf[i]), "V=%d,Z=%d,I=%d,P=%d,L=%d",
				   &vid,
				   &max_frame_size,
				   &max_interval_frames,
				   &priority_and_rank,
				   &latency);

			mrp_register_cb(recovered_streamid, 0, dest_addr, max_frame_size, max_interval_frames, vid, latency);
			break;

		case 'S':

			/* handle the leave/join events */
			switch (buf[offset + 4]) {
			case 'L':
				i = offset + 5;
				while (buf[i] != 'D')
					i++;
				i += 2;	/* skip the ':' */
				sscanf(&(buf[i]), "%d", &substate);
				while (buf[i] != 'S')
					i++;
				i += 2;	/* skip the ':' */
				for (j = 0; j < 8; j++) {
					sscanf(&(buf[i + 2 * j]), "%02x", &id);
					recovered_streamid[j] = (unsigned char)id;
				}
				AVB_LOGF_DEBUG
					("EVENT on STREAM ID=%02x%02x%02x%02x%02x%02x%02x%02x ",
					 recovered_streamid[0], recovered_streamid[1],
					 recovered_streamid[2], recovered_streamid[3],
					 recovered_streamid[4], recovered_streamid[5],
					 recovered_streamid[6], recovered_streamid[7]);
				switch (substate) {
				case 0:
					AVB_LOG_DEBUG("with state ignore");
					break;
				case 1:
					AVB_LOG_DEBUG("with state askfailed");
					break;
				case 2:
					AVB_LOG_DEBUG("with state ready");
					break;
				case 3:
					AVB_LOG_DEBUG("with state readyfail");
					break;
				default:
					AVB_LOGF_DEBUG("with state UNKNOWN (%d)", substate);
					break;
				}
				switch (buf[offset + 1]) {
				case 'L':
					mrp_attach_cb(recovered_streamid, substate);
					AVB_LOGF_DEBUG("got a leave indication substate %d", substate);
					if (memcmp
						(recovered_streamid, monitor_stream_id,
						 sizeof(recovered_streamid)) == 0) {
						listeners = 0;
						AVB_LOG_DEBUG("listener left");
					}
					break;
				case 'J':
				case 'N':
					AVB_LOGF_DEBUG("got a new/join indication substate %d", substate);
					mrp_attach_cb(recovered_streamid, substate);
					if (substate > MSRP_LISTENER_ASKFAILED) {
						if (memcmp
							(recovered_streamid,
							 monitor_stream_id,
							 sizeof(recovered_streamid)) == 0)
							listeners = 1;
					}
					break;
				}
				break;

			case 'T':
				i = offset + 5;
				while (buf[i] != 'S')
					i++;
				// skip S=
				i += 2;
				for (j = 0; j < 8; j++) {
					sscanf(&(buf[i + 2 * j]), "%02x", &id);
					recovered_streamid[j] = (unsigned char)id;
				}
				while (buf[i] != 'A')
					i++;
				// skip A=
				i += 2;
				for (j = 0; j < 6; j++) {
					sscanf(&(buf[i + 2 * j]), "%02x", &id);
					dest_addr[j] = (unsigned char)id;
				}
				i+= j*2 + 1;

				sscanf(&(buf[i]), "V=%d,Z=%d,I=%d,P=%d,L=%d",
					   &vid,
					   &max_frame_size,
					   &max_interval_frames,
					   &priority_and_rank,
					   &latency);

				mrp_register_cb(recovered_streamid, buf[offset+1] == 'J' || buf[offset+1] == 'N', dest_addr, max_frame_size, max_interval_frames, vid, latency);
				break;

			default:
				break;
			}
			break;

		case '\0':
			break;
		}

		// Proceed to the next line.
		while (offset < buflen && buf[offset] != '\n' && buf[offset] != '\0') {
			offset++;
		}
		if (offset < buflen && buf[offset] == '\n') {
			offset++;
		}
	}
	return 0;
}

void *mrp_monitor_thread(void *arg)
{
	char *msgbuf;
	struct sockaddr_in client_addr;
	struct msghdr msg;
	struct iovec iov;
	int bytes = 0;
	struct pollfd fds;
	int rc;
	(void) arg; /* unused */

	msgbuf = (char *)malloc(MAX_MRPD_CMDSZ);
	if (NULL == msgbuf)
		return NULL;
	while (!halt_tx) {
		fds.fd = control_socket;
		fds.events = POLLIN;
		fds.revents = 0;
		rc = poll(&fds, 1, 100);
		if (rc < 0) {
			free(msgbuf);
			pthread_exit(NULL);
		}
		if (rc == 0)
			continue;
		if ((fds.revents & POLLIN) == 0) {
			free(msgbuf);
			pthread_exit(NULL);
		}
		memset(&msg, 0, sizeof(msg));
		memset(&client_addr, 0, sizeof(client_addr));
		memset(msgbuf, 0, MAX_MRPD_CMDSZ);
		iov.iov_len = MAX_MRPD_CMDSZ;
		iov.iov_base = msgbuf;
		msg.msg_name = &client_addr;
		msg.msg_namelen = sizeof(client_addr);
		msg.msg_iov = &iov;
		msg.msg_iovlen = 1;
		bytes = recvmsg(control_socket, &msg, 0);
		if (bytes < 0)
			continue;
		AVB_LOGF_VERBOSE("Msg: %s", msgbuf);
		process_mrp_msg(msgbuf, bytes);
	}
	free(msgbuf);
	pthread_exit(NULL);
}

/*
 * public
 */

int mrp_connect(void)
{
	struct sockaddr_in addr;
	int sock_fd = -1;
	sock_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
	if (sock_fd < 0)
		goto out;
	memset(&addr, 0, sizeof(addr));
	addr.sin_family = AF_INET;
	addr.sin_port = htons(MRPD_PORT_DEFAULT);
	inet_aton("127.0.0.1", &addr.sin_addr);
	memset(&addr, 0, sizeof(addr));
	control_socket = sock_fd;
	return 0;
 out:	if (sock_fd != -1)
		close(sock_fd);
	sock_fd = -1;
	return -1;
}

int mrp_disconnect(void)
{
	char *msgbuf;
	int rc;

	msgbuf = malloc(1500);
	if (NULL == msgbuf)
		return -1;
	memset(msgbuf, 0, 1500);
	sprintf(msgbuf, "BYE");
	mrp_okay = 0;
	AVB_LOGF_DEBUG("MRP Command (Disconnect):  %s", msgbuf);
	rc = send_mrp_msg(msgbuf, 1500);
	free(msgbuf);

	if (rc != 1500)
		return -1;
	else
		return 0;
}

int mrp_monitor(void)
{
	int rc;
	rc = pthread_attr_init(&monitor_attr);
	rc |= pthread_create(&monitor_thread, NULL, mrp_monitor_thread, NULL);
	return rc;
}

int mrp_register_domain(int *class_id, int *priority, u_int16_t * vid)
{
	char *msgbuf;
	int rc;

	msgbuf = malloc(1500);
	if (NULL == msgbuf)
		return -1;
	memset(msgbuf, 0, 1500);

	sprintf(msgbuf, "S+D:C=%d,P=%d,V=%04x", *class_id, *priority, *vid);
	mrp_okay = 0;
	AVB_LOGF_DEBUG("MRP Command (Register Domain):  %s", msgbuf);
	rc = send_mrp_msg(msgbuf, 1500);
	free(msgbuf);

	if (rc != 1500)
		return -1;
	else
		return 0;
}


int
mrp_advertise_stream(uint8_t * streamid,
		     uint8_t * destaddr,
		     u_int16_t vlan,
		     int pktsz, int interval, int priority, int latency)
{
	char *msgbuf;
	int rc;

	msgbuf = malloc(1500);
	if (NULL == msgbuf)
		return -1;
	memset(msgbuf, 0, 1500);

	sprintf(msgbuf, "S++:S=%02X%02X%02X%02X%02X%02X%02X%02X"
		",A=%02X%02X%02X%02X%02X%02X"
		",V=%04X"
		",Z=%d"
		",I=%d"
		",P=%d"
		",L=%d", streamid[0], streamid[1], streamid[2],
		streamid[3], streamid[4], streamid[5], streamid[6],
		streamid[7], destaddr[0], destaddr[1], destaddr[2],
		destaddr[3], destaddr[4], destaddr[5], vlan, pktsz,
		interval, priority << 5, latency);
	mrp_okay = 0;
	AVB_LOGF_DEBUG("MRP Command (Advertise Stream):  %s", msgbuf);
	rc = send_mrp_msg(msgbuf, 1500);
	free(msgbuf);

	if (rc != 1500)
		return -1;
	else
		return 0;
}

int
mrp_unadvertise_stream(uint8_t * streamid,
		       uint8_t * destaddr,
		       u_int16_t vlan,
		       int pktsz, int interval, int priority, int latency)
{
	char *msgbuf;
	int rc;
	msgbuf = malloc(1500);
	if (NULL == msgbuf)
		return -1;
	memset(msgbuf, 0, 1500);
	sprintf(msgbuf, "S--:S=%02X%02X%02X%02X%02X%02X%02X%02X"
		",A=%02X%02X%02X%02X%02X%02X"
		",V=%04X"
		",Z=%d"
		",I=%d"
		",P=%d"
		",L=%d", streamid[0], streamid[1], streamid[2],
		streamid[3], streamid[4], streamid[5], streamid[6],
		streamid[7], destaddr[0], destaddr[1], destaddr[2],
		destaddr[3], destaddr[4], destaddr[5], vlan, pktsz,
		interval, priority << 5, latency);
	mrp_okay = 0;
	AVB_LOGF_DEBUG("MRP Command (Unadvertise Stream):  %s", msgbuf);
	rc = send_mrp_msg(msgbuf, 1500);
	free(msgbuf);

	if (rc != 1500)
		return -1;
	else
		return 0;
}


int mrp_await_listener(unsigned char *streamid)
{
	char *msgbuf;
	int rc;

	memcpy(monitor_stream_id, streamid, sizeof(monitor_stream_id));
	msgbuf = malloc(1500);
	if (NULL == msgbuf)
		return -1;
	memset(msgbuf, 0, 1500);
	sprintf(msgbuf, "S??");
	AVB_LOGF_DEBUG("MRP Command (Await Listener):  %s", msgbuf);
	rc = send_mrp_msg(msgbuf, 1500);
	free(msgbuf);
	if (rc != 1500)
		return -1;

	/* either already there ... or need to wait ... */
	while (!halt_tx && (listeners == 0))
		usleep(20000);

	return 0;
}

/*
 * actually not used
 */

int mrp_get_domain(int *class_a_id, int *a_priority, u_int16_t * a_vid,
		   int *class_b_id, int *b_priority, u_int16_t * b_vid)
{
	char *msgbuf;
	int ret;
	int tries = 5;

	/* we may not get a notification if we are joining late,
	 * so query for what is already there ...
	 */
	msgbuf = malloc(1500);
	if (NULL == msgbuf)
		return -1;
	memset(msgbuf, 0, 1500);
	sprintf(msgbuf, "S??");
	AVB_LOGF_DEBUG("MRP Command (Get Domain):  %s", msgbuf);
	ret = send_mrp_msg(msgbuf, 1500);
	free(msgbuf);
	if (ret != 1500)
		return -1;
	while (!halt_tx && (domain_a_valid == 0) && (domain_b_valid == 0) && tries--) {
		usleep(20000);
	}
	*class_a_id = 0;
	*a_priority = 0;
	*a_vid = 0;
	*class_b_id = 0;
	*b_priority = 0;
	*b_vid = 0;
	if (domain_a_valid) {
		*class_a_id = domain_class_a_id;
		*a_priority = domain_class_a_priority;
		*a_vid = domain_class_a_vid;
	}
	if (domain_b_valid) {
		*class_b_id = domain_class_b_id;
		*b_priority = domain_class_b_priority;
		*b_vid = domain_class_b_vid;
	}
	return domain_a_valid || domain_b_valid ? 0 : -1;
}

int mrp_join_vlan()
{
	char *msgbuf;
	int rc;

	msgbuf = malloc(1500);
	if (NULL == msgbuf)
		return -1;
	memset(msgbuf, 0, 1500);
	sprintf(msgbuf, "V++:I=0002");
	AVB_LOGF_DEBUG("MRP Command (Join VLAN):  %s", msgbuf);
	rc = send_mrp_msg(msgbuf, 1500);
	free(msgbuf);

	if (rc != 1500)
		return -1;
	else
		return 0;
}

int mrp_join_listener(uint8_t * streamid)
{
	char *msgbuf;
	int rc;

	msgbuf = malloc(1500);
	if (NULL == msgbuf)
		return -1;
	memset(msgbuf, 0, 1500);
	sprintf(msgbuf, "S+L:S=%02X%02X%02X%02X%02X%02X%02X%02X"
		",D=2", streamid[0], streamid[1], streamid[2], streamid[3],
		streamid[4], streamid[5], streamid[6], streamid[7]);
	mrp_okay = 0;
	AVB_LOGF_DEBUG("MRP Command (Join Listener):  %s", msgbuf);
	rc = send_mrp_msg(msgbuf, 1500);
	free(msgbuf);

	if (rc != 1500)
		return -1;
	else
		return 0;
}

// TODO remove
int recv_mrp_okay()
{
	while ((mrp_okay == 0) && (mrp_error == 0))
		usleep(20000);
	return 0;
}

int mrp_send_ready(uint8_t *stream_id)
{
	char *databuf;
	int rc;

	databuf = malloc(1500);
	if (NULL == databuf)
		return -1;
	memset(databuf, 0, 1500);
	sprintf(databuf, "S+L:L=%02x%02x%02x%02x%02x%02x%02x%02x, D=2",
		     stream_id[0], stream_id[1],
		     stream_id[2], stream_id[3],
		     stream_id[4], stream_id[5],
		     stream_id[6], stream_id[7]);
	AVB_LOGF_DEBUG("MRP Command (Send Ready):  %s", databuf);
	rc = send_mrp_msg(databuf, 1500);
	free(databuf);

	if (rc != 1500)
		return -1;
	else
		return 0;
}

int mrp_send_leave(uint8_t *stream_id)
{
	char *databuf;
	int rc;

	databuf = malloc(1500);
	if (NULL == databuf)
		return -1;
	memset(databuf, 0, 1500);
	sprintf(databuf, "S-L:L=%02x%02x%02x%02x%02x%02x%02x%02x, D=3",
		     stream_id[0], stream_id[1],
		     stream_id[2], stream_id[3],
		     stream_id[4], stream_id[5],
		     stream_id[6], stream_id[7]);

	AVB_LOGF_DEBUG("MRP Command (Send Leave):  %s", databuf);
	rc = send_mrp_msg(databuf, 1500);
	free(databuf);

	if (rc != 1500)
		return -1;
	else
		return 0;
}