aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/test/mc_nextgen_test.c
blob: 210044dd733458c67664de4e6f752ba194061182 (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
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
/*
 * Media controller Next Generation test app
 *
 * Copyright (C) 2015 Mauro Carvalho Chehab <mchehab@osg.samsung.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation version 2
 * of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 * Or, point your browser to http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * */

#include <config.h>

#include <linux/media.h>

#include <argp.h>

#include <syslog.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <stdarg.h>
#include <errno.h>
#include <string.h>

#define PROGRAM_NAME	"mc_nextgen_test"

const char *argp_program_version = PROGRAM_NAME " version " V4L_UTILS_VERSION;
const char *argp_program_bug_address = "Mauro Carvalho Chehab <m.chehab@samsung.com>";

static const char doc[] = "\nA testing tool for the MC next geneneration API\n";

static const struct argp_option options[] = {
	{"entities",	'e',	0,		0,	"show entities", 0},
	{"interfaces",	'i',	0,		0,	"show pads", 0},
	{"data-links",	'l',	0,		0,	"show data links", 0},
	{"intf-links",	'I',	0,		0,	"show interface links", 0},
	{"dot",		'D',	0,		0,	"show in Graphviz format", 0},
	{"max_tsout",	't',	"NUM_TSOUT",	0,	"max number of DTV TS out entities/interfaces in graphviz output (default: 5)", 0},
	{"device",	'd',	"DEVICE",	0,	"media controller device (default: /dev/media0", 0},

	{"help",        '?',	0,		0,	"Give this help list", -1},
	{"usage",	-3,	0,		0,	"Give a short usage message"},
	{"version",	'V',	0,		0,	"Print program version", -1},
	{ 0, 0, 0, 0, 0, 0 }
};

static int show_entities = 0;
static int show_interfaces = 0;
static int show_data_links = 0;
static int show_intf_links = 0;
static int show_dot = 0;
static int max_tsout = 5;
static char media_device[256] = "/dev/media0";

static error_t parse_opt(int k, char *arg, struct argp_state *state)
{
	switch (k) {
	case 'e':
		show_entities++;
		break;
	case 'i':
		show_interfaces++;
		break;
	case 'l':
		show_data_links++;
		break;
	case 'I':
		show_intf_links++;
		break;
	case 'D':
		show_dot++;
		break;

	case 'd':
		strncpy(media_device, arg, sizeof(media_device) - 1);
		media_device[sizeof(media_device)-1] = '\0';
		break;

	case 't':
		max_tsout = atoi(arg);
		break;

	case '?':
		argp_state_help(state, state->out_stream,
				ARGP_HELP_SHORT_USAGE | ARGP_HELP_LONG
				| ARGP_HELP_DOC);
		fprintf(state->out_stream, "\nReport bugs to %s.\n",
			argp_program_bug_address);
		exit(0);
	case 'V':
		fprintf (state->out_stream, "%s\n", argp_program_version);
		exit(0);
	case -3:
		argp_state_help(state, state->out_stream, ARGP_HELP_USAGE);
		exit(0);
	default:
		return ARGP_ERR_UNKNOWN;
	}
	return 0;
}

static struct argp argp = {
	.options = options,
	.parser = parse_opt,
	.doc = doc,
};


/*
 * Those came from media-entity.h and bitops.h
 * They should actually be added at the media.h UAPI if we decide to keep
 * both ID and TYPE fields together - as current proposal
 */

enum media_gobj_type {
	MEDIA_GRAPH_ENTITY,
	MEDIA_GRAPH_PAD,
	MEDIA_GRAPH_LINK,
	MEDIA_GRAPH_INTF_DEVNODE,
};

static uint32_t media_type(uint32_t id)
{
	return id >> 24;
}

static inline uint32_t media_localid(uint32_t id)
{
	return id & 0xffffff;
}

static inline const char *gobj_type(uint32_t id)
{
	switch (media_type(id)) {
	case MEDIA_GRAPH_ENTITY:
		return "entity";
	case MEDIA_GRAPH_PAD:
		return "pad";
	case MEDIA_GRAPH_LINK:
		return "link";
	case MEDIA_GRAPH_INTF_DEVNODE:
		return "intf_devnode";
	default:
		return "unknown intf type";
	}
}

static inline const char *intf_type(uint32_t intf_type)
{
	switch (intf_type) {
	case MEDIA_INTF_T_DVB_FE:
		return "frontend";
	case MEDIA_INTF_T_DVB_DEMUX:
		return "demux";
	case MEDIA_INTF_T_DVB_DVR:
		return "DVR";
	case MEDIA_INTF_T_DVB_CA:
		return  "CA";
	case MEDIA_INTF_T_DVB_NET:
		return "dvbnet";
	case MEDIA_INTF_T_V4L_VIDEO:
		return "video";
	case MEDIA_INTF_T_V4L_VBI:
		return "vbi";
	case MEDIA_INTF_T_V4L_RADIO:
		return "radio";
	case MEDIA_INTF_T_V4L_SUBDEV:
		return "v4l2-subdev";
	case MEDIA_INTF_T_V4L_SWRADIO:
		return "swradio";
#if 0
	case MEDIA_INTF_T_ALSA_PCM_CAPTURE:
		return "pcm-capture";
	case MEDIA_INTF_T_ALSA_PCM_PLAYBACK:
		return "pcm-playback";
	case MEDIA_INTF_T_ALSA_CONTROL:
		return "alsa-control";
	case MEDIA_INTF_T_ALSA_COMPRESS:
		return "compress";
	case MEDIA_INTF_T_ALSA_RAWMIDI:
		return "rawmidi";
	case MEDIA_INTF_T_ALSA_HWDEP:
		return "hwdep";
#endif
	default:
		return "unknown_intf";
	}
};

static inline const char *ent_function(uint32_t function)
{
	switch (function) {
	case MEDIA_ENT_F_DTV_DEMOD:
		return "DTV demod";
	case MEDIA_ENT_F_TS_DEMUX:
		return "MPEG-TS demux";
	case MEDIA_ENT_F_DTV_CA:
		return "DTV CA";
	case MEDIA_ENT_F_DTV_NET_DECAP:
		return "DTV Network decap";
	case MEDIA_ENT_F_CONN_RF:
		return "RF connector";
	case MEDIA_ENT_F_CONN_SVIDEO:
		return "S-Video connector";
	case MEDIA_ENT_F_CONN_COMPOSITE:
		return "Composite connector";
	case MEDIA_ENT_F_CONN_TEST:
		return "Test connector";
	case MEDIA_ENT_F_IO_V4L:
		return "V4L I/O";
	case MEDIA_ENT_F_IO_DTV:
		return "DTV I/O";
	case MEDIA_ENT_F_IO_SWRADIO:
		return "SDR I/O";
	case MEDIA_ENT_F_IO_VBI:
		return "VBI I/O";
	case MEDIA_ENT_F_CAM_SENSOR:
		return "Camera Sensor";
	case MEDIA_ENT_F_FLASH:
		return "Flash LED/light";
	case MEDIA_ENT_F_LENS:
		return "Lens";
	case MEDIA_ENT_F_ATV_DECODER:
		return "ATV decoder";
	case MEDIA_ENT_F_TUNER:
		return "tuner";
	case MEDIA_ENT_F_IF_VID_DECODER:
		return "IF video decoder";
	case MEDIA_ENT_F_IF_AUD_DECODER:
		return "IF sound decoder";
	case MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN:
	default:
		return "unknown entity type";
	}
}

/* Ancilary function to produce an human readable ID for an object */

static char *objname(uint32_t id, char delimiter)
{
	char *name;
	int ret;

	ret = asprintf(&name, "%s%c%d", gobj_type(id), delimiter, media_localid(id));
	if (ret < 0)
		return NULL;

	return name;
}

enum ansi_colors {
	BLACK = 30,
	RED,
	GREEN,
	YELLOW,
	BLUE,
	MAGENTA,
	CYAN,
	WHITE
};

#define NORMAL_COLOR "\033[0;%dm"
#define BRIGHT_COLOR "\033[1;%dm"

void show(int color, int bright, const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);

	if (isatty(STDOUT_FILENO)) {
		if (bright)
			printf(BRIGHT_COLOR, color);
		else
			printf(NORMAL_COLOR, color);
	}

	vprintf(fmt, ap);

	va_end(ap);
}

#define logperror(msg) do {\
       show(RED, 0, "%s: %s", msg, strerror(errno)); \
       printf("\n"); \
} while (0)

/*
 * Code to convert devnode major, minor into a name
 *
 * This code was imported from the Media controller interface library (libmediactl.c) under LGPL v2.1
 *      Copyright (C) 2010-2014 Ideas on board SPRL
 *      Contact: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
 */
#ifdef HAVE_LIBUDEV

#include <libudev.h>

static int media_open_ifname(void **priv)
{
	struct udev **udev = (struct udev **)priv;

	*udev = udev_new();
	if (*udev == NULL)
		return -ENOMEM;
	return 0;
}

static void media_close_ifname(void *priv)
{
	struct udev *udev = (struct udev *)priv;

	if (udev != NULL)
		udev_unref(udev);
}

static char *media_get_ifname_udev(struct media_v2_intf_devnode *devnode, struct udev *udev)
{
	struct udev_device *device;
	dev_t devnum;
	const char *p;
	char *name = NULL;
	int ret;

	if (udev == NULL)
		return NULL;

	devnum = makedev(devnode->major, devnode->minor);
	device = udev_device_new_from_devnum(udev, 'c', devnum);
	if (device) {
		p = udev_device_get_devnode(device);
		if (p) {
			ret = asprintf(&name, "%s", p);
			if (ret < 0)
				return NULL;
		}
	}

	udev_device_unref(device);

	return name;
}
#else
static inline char *media_open_ifname(void **priv) { return NULL; } ;
static void media_close_ifname(void *priv) {};
#endif	/* HAVE_LIBUDEV */

char *media_get_ifname(struct media_v2_interface *intf, void *priv)
{
	struct media_v2_intf_devnode *devnode;
	struct stat devstat;
	char devname[32];
	char sysname[32];
	char target[1024];
	char *p, *name = NULL;
	int ret;

	/* Only handles Devnode interfaces */
	if (media_type(intf->id) != MEDIA_GRAPH_INTF_DEVNODE)
		return NULL;

	devnode = &intf->devnode;

#ifdef HAVE_LIBUDEV
	/* Try first to convert using udev */
	name = media_get_ifname_udev(devnode, priv);
	if (name)
		return name;
#endif

	/* Failed. Let's fallback to get it via sysfs */

	sprintf(sysname, "/sys/dev/char/%u:%u",
		devnode->major, devnode->minor);

	ret = readlink(sysname, target, sizeof(target) - 1);
	if (ret < 0)
		return NULL;

	target[ret] = '\0';
	p = strrchr(target, '/');
	if (p == NULL)
		return NULL;

	sprintf(devname, "/dev/%s", p + 1);
	if (strstr(p + 1, "dvb")) {
		char *s = p + 1;

		if (strncmp(s, "dvb", 3))
			return NULL;
		s += 3;
		p = strchr(s, '.');
		if (!p)
			return NULL;
		*p = '/';
		sprintf(devname, "/dev/dvb/adapter%s", s);
	} else {
		sprintf(devname, "/dev/%s", p + 1);
	}
	ret = stat(devname, &devstat);
	if (ret < 0)
		return NULL;

	/* Sanity check: udev might have reordered the device nodes.
	 * Make sure the major/minor match. We should really use
	 * libudev.
	 */
	if (major(devstat.st_rdev) == intf->devnode.major &&
	    minor(devstat.st_rdev) == intf->devnode.minor) {
		ret = asprintf(&name, "%s", devname);
		if (ret < 0)
			return NULL;
	}
	return name;
}

/*
 * The real code starts here
 */

struct graph_obj {
	uint32_t id;
	union {
		struct media_v2_entity *entity;
		struct media_v2_pad *pad;
		struct media_v2_interface *intf;
		struct media_v2_link *link;
	};
	/* Currently, used only for pads->entity */
	struct graph_obj *parent;
	/* Used only for entities */
	int num_pads, num_pad_sinks, num_pad_sources;
};

struct media_controller {
	int fd;
	struct media_v2_topology topo;
	struct media_device_info info;

	int num_gobj;
	struct graph_obj *gobj;
};

static inline
struct graph_obj *find_gobj(struct media_controller *mc, uint32_t id)
{
	int i;

	/*
	 * If we were concerned about performance, we could use bsearch
	 */
	for (i = 0; i < mc->num_gobj; i++) {
		if (mc->gobj[i].id == id)
			return &mc->gobj[i];
	}
	return NULL;
}

static int media_init_graph_obj(struct media_controller *mc)
{
	struct media_v2_topology *topo = &mc->topo;
	int i, j, num_gobj;
	int idx = 0;
	struct media_v2_entity *entities = media_get_uptr(topo->ptr_entities);
	struct media_v2_interface *interfaces = media_get_uptr(topo->ptr_interfaces);
	struct media_v2_pad *pads = media_get_uptr(topo->ptr_pads);
	struct media_v2_link *links = media_get_uptr(topo->ptr_links);

	num_gobj = topo->num_entities + topo->num_interfaces
		   + topo->num_pads + topo->num_links;

	mc->gobj = calloc(num_gobj, sizeof(*mc->gobj));
	if (!mc->gobj) {
		logperror("couldn't allocate space for graph_obj");
		return -ENOMEM;
	}

	for (i = 0; i < topo->num_pads; i++) {
		mc->gobj[idx].id = pads[i].id;
		mc->gobj[idx].pad = &pads[i];
		idx++;
	}

	mc->num_gobj = num_gobj;

	for (i = 0; i < topo->num_entities; i++) {
		struct graph_obj *gobj;

		mc->gobj[idx].id = entities[i].id;
		mc->gobj[idx].entity = &entities[i];

		/* Set the parent object for the pads */
		for (j = 0; j < topo->num_pads; j++) {
			if (pads[j].entity_id != entities[i].id)
				continue;

			/* The data below is useful for Graphviz generation */
			mc->gobj[idx].num_pads++;
			if (pads[j].flags & MEDIA_PAD_FL_SINK)
				mc->gobj[idx].num_pad_sinks++;
			if (pads[j].flags & MEDIA_PAD_FL_SOURCE)
				mc->gobj[idx].num_pad_sources++;

			gobj = find_gobj(mc, pads[j].id);
			if (gobj)
				gobj->parent = &mc->gobj[idx];
		}
		idx++;
	}
	for (i = 0; i < topo->num_interfaces; i++) {
		mc->gobj[idx].id = interfaces[i].id;
		mc->gobj[idx].intf = &interfaces[i];
		idx++;
	}
	for (i = 0; i < topo->num_links; i++) {
		mc->gobj[idx].id = links[i].id;
		mc->gobj[idx].link = &links[i];
		idx++;
	}

	mc->num_gobj = num_gobj;

	/*
	 * If we were concerned about performance, we could now sort
	 * the objects and use binary search at the find routine
	 *
	 * We could also add some logic here to create per-interfaces
	 * and per-entities linked lists with pads and links.
	 *
	 * However, this is just a test program, so let's keep it simple
	 */
	return 0;
}

static void media_show_entities(struct media_controller *mc)
{
	struct media_v2_topology *topo = &mc->topo;
	struct media_v2_entity *entities = media_get_uptr(topo->ptr_entities);
	struct media_v2_pad *pads = media_get_uptr(topo->ptr_pads);
	int i, j;

	for (i = 0; i < topo->num_entities; i++) {
		struct media_v2_entity *entity = &entities[i];
		char *obj;
		int num_pads = 0;
		int num_sinks = 0;
		int num_sources = 0;

		/*
		 * Count the number of patches - If this would be a
		 * real application/library, we would likely be creating
		 * either a list of an array to associate entities/pads/links
		 *
		 * However, we just want to test the API, so we don't care
		 * about performance.
		 */
		for (j = 0; j < topo->num_pads; j++) {
			if (pads[j].entity_id != entity->id)
				continue;

			num_pads++;
			if (pads[j].flags & MEDIA_PAD_FL_SINK)
				num_sinks++;
			if (pads[j].flags & MEDIA_PAD_FL_SOURCE)
				num_sources++;
		}

		obj = objname(entity->id, '#');
		show(YELLOW, 0, "entity %s: '%s' %s, %d pad(s)",
		     obj, ent_function(entity->function),
		     entity->name, num_pads);
		if (num_sinks)
			show(YELLOW, 0,", %d sink(s)", num_sinks);
		if (num_sources)
			show(YELLOW, 0,", %d source(s)", num_sources);
		printf("\n");

		free(obj);
	}
}

static void media_show_interfaces(struct media_controller *mc)
{
	struct media_v2_topology *topo = &mc->topo;
	struct media_v2_interface *interfaces = media_get_uptr(topo->ptr_interfaces);
	void *priv = NULL;
	int i;

	media_open_ifname(&priv);
	for (i = 0; i < topo->num_interfaces; i++) {
		char *obj, *devname;
		struct media_v2_interface *intf = &interfaces[i];

		obj = objname(intf->id, '#');
		devname = media_get_ifname(intf, priv);
		show(GREEN, 0, "interface %s: %s %s\n",
		     obj, intf_type(intf->intf_type),
		     devname);
		free(obj);
		free(devname);
	}
	media_close_ifname(priv);
}

static void media_show_links(struct media_controller *mc)
{
	struct media_v2_topology *topo = &mc->topo;
	struct media_v2_link *links = media_get_uptr(topo->ptr_links);
	int i, color;

	for (i = 0; i < topo->num_links; i++) {
		struct media_v2_link *link = &links[i];
		char *obj, *source_obj, *sink_obj;

		color = BLUE;
		if (media_type(link->source_id) == MEDIA_GRAPH_PAD) {
			if (!show_data_links)
				continue;
			color = CYAN;
		}

		if (media_type(link->source_id) == MEDIA_GRAPH_INTF_DEVNODE) {
			if (!show_intf_links)
				continue;
		}

		obj = objname(link->id, '#');
		source_obj = objname(link->source_id, '#');
		sink_obj = objname(link->sink_id, '#');

		if ((link->flags & MEDIA_LNK_FL_LINK_TYPE) == MEDIA_LNK_FL_INTERFACE_LINK)
			show(color, 0, "interface ");
		else
			show(color, 0, "data ");
		show(color, 0, "link %s: %s %s %s",
		     obj, source_obj,
		     ((link->flags & MEDIA_LNK_FL_LINK_TYPE) == MEDIA_LNK_FL_INTERFACE_LINK) ? "<=>" : "=>",
		     sink_obj);
		if (link->flags & MEDIA_LNK_FL_IMMUTABLE)
			show(color, 0, " [IMMUTABLE]");
		if (link->flags & MEDIA_LNK_FL_DYNAMIC)
			show(color, 0, " [DYNAMIC]");
		if (link->flags & MEDIA_LNK_FL_ENABLED)
			show(color, 1, " [ENABLED]");

		printf("\n");

		free(obj);
		free(source_obj);
		free(sink_obj);
	}
}

static void media_get_device_info(struct media_controller *mc)
{
	struct media_device_info *info = &mc->info;
	int ret = 0;

	ret = ioctl(mc->fd, MEDIA_IOC_DEVICE_INFO, info);
	if (ret < 0) {
		logperror("MEDIA_IOC_DEVICE_INFO failed");
		return;
	}
	if (show_dot)
		return;

	show(WHITE, 0, "Device: %s (driver %s)\n",
	     info->model, info->driver);
	if (info->serial[0])
		show(WHITE, 0, "Serial: %s\n", info->serial);
	show(WHITE, 0, "Bus: %s\n", info->bus_info);
}

static int media_get_topology(struct media_controller *mc)
{
	struct media_v2_topology *topo = &mc->topo;
	int ret = 0, topology_version;

	media_get_device_info(mc);

	/* First call: get the amount of elements */
	memset(topo, 0, sizeof(*topo));
	ret = ioctl(mc->fd, MEDIA_IOC_G_TOPOLOGY, topo);
	if (ret < 0) {
		logperror("MEDIA_IOC_G_TOPOLOGY faled to get numbers");
		goto error;
	}

	topology_version = topo->topology_version;

	if (!show_dot) {
		show(WHITE, 0, "version: %d\n", topology_version);
		show(WHITE, 0, "number of entities: %d\n", topo->num_entities);
		show(WHITE, 0, "number of interfaces: %d\n", topo->num_interfaces);
		show(WHITE, 0, "number of pads: %d\n", topo->num_pads);
		show(WHITE, 0, "number of links: %d\n", topo->num_links);
	}

	do {
		topo->ptr_entities = (__u64)calloc(topo->num_entities,
					sizeof(struct media_v2_entity));
		if (topo->num_entities && !topo->ptr_entities)
			goto error;

		topo->ptr_interfaces = (__u64)calloc(topo->num_interfaces,
					  sizeof(struct media_v2_interface));
		if (topo->num_interfaces && !topo->ptr_interfaces)
			goto error;

		topo->ptr_pads = (__u64)calloc(topo->num_pads,
				   sizeof(struct media_v2_pad));
		if (topo->num_pads && !topo->ptr_pads)
			goto error;

		topo->ptr_links = (__u64)calloc(topo->num_links,
				     sizeof(struct media_v2_link));
		if (topo->num_links && !topo->ptr_links)
			goto error;

		ret = ioctl(mc->fd, MEDIA_IOC_G_TOPOLOGY, topo);
		if (ret < 0) {
			if (topo->topology_version != topology_version) {
				show(WHITE, 0, "Topology changed from version %d to %d. trying again.\n",
					  topology_version,
					  topo->topology_version);
				/*
				 * The logic here could be smarter, but, as
				 * topology changes should be rare, this
				 * should do the work
				 */
				free((void *)topo->ptr_entities);
				free((void *)topo->ptr_interfaces);
				free((void *)topo->ptr_pads);
				free((void *)topo->ptr_links);
				topology_version = topo->topology_version;
				continue;
			}
			logperror("MEDIA_IOC_G_TOPOLOGY faled");
			goto error;
		}
	} while (ret < 0);

	media_init_graph_obj(mc);

	return 0;

error:
	if (topo->ptr_entities)
		free((void *)topo->ptr_entities);
	if (topo->ptr_interfaces)
		free((void *)topo->ptr_interfaces);
	if (topo->ptr_pads)
		free((void *)topo->ptr_pads);
	if (topo->ptr_links)
		free((void *)topo->ptr_links);

	topo->ptr_entities = 0;
	topo->ptr_interfaces = 0;
	topo->ptr_pads = 0;
	topo->ptr_links = 0;

	return ret;
}

static struct media_controller *mc_open(char *devname)
{
	struct media_controller *mc;

	mc = calloc(1, sizeof(*mc));

	mc->fd = open(devname, O_RDWR);
	if (mc->fd < 0) {
		logperror("Can't open media device");
		return NULL;
	}

	return mc;
}

static int mc_close(struct media_controller *mc)
{
	int ret;

	ret = close(mc->fd);

	if (mc->gobj)
		free(mc->gobj);
	if (mc->topo.ptr_entities)
		free((void *)mc->topo.ptr_entities);
	if (mc->topo.ptr_interfaces)
		free((void *)mc->topo.ptr_interfaces);
	if (mc->topo.ptr_pads)
		free((void *)mc->topo.ptr_pads);
	if (mc->topo.ptr_links)
		free((void *)mc->topo.ptr_links);
	free(mc);

	return ret;
}

/* Graphviz styles */
#define DOT_HEADER	"digraph board {\n\trankdir=TB\n\tcolorscheme=x11\n"
#define STYLE_INTF	"shape=box, style=filled, fillcolor=yellow"
#define STYLE_ENTITY	"shape=Mrecord, style=filled, fillcolor=lightblue"
#define STYLE_ENT_SRC	"shape=Mrecord, style=filled, fillcolor=cadetblue"
#define STYLE_ENT_SINK	"shape=Mrecord, style=filled, fillcolor=aquamarine"
#define STYLE_DATA_LINK	"color=blue"
#define STYLE_INTF_LINK	"dir=\"none\" color=\"orange\""

static void media_show_graphviz(struct media_controller *mc)
{
	struct media_v2_topology *topo = &mc->topo;
	struct media_v2_entity *entities = media_get_uptr(topo->ptr_entities);
	struct media_v2_interface *interfaces = media_get_uptr(topo->ptr_interfaces);
	struct media_v2_pad *pads = media_get_uptr(topo->ptr_pads);
	struct media_v2_link *links = media_get_uptr(topo->ptr_links);

	int i, j;
	char *obj;
	void *priv = NULL;

	printf("%s", DOT_HEADER);

	if (mc->info.model[0])
		printf("\tlabelloc=\"t\"\n\tlabel=\"%s\n driver:%s, bus: %s\n\"\n",
		       mc->info.model, mc->info.driver, mc->info.bus_info);

	media_open_ifname(&priv);
	for (i = 0; i < topo->num_interfaces; i++) {
		struct media_v2_interface *intf = &interfaces[i];
		char *devname;

		obj = objname(intf->id, '_');
		devname = media_get_ifname(intf, priv);
		printf("\t%s [label=\"%s\\n%s\\n%s\", " STYLE_INTF"]\n",
		       obj, obj, intf_type(intf->intf_type),
		     devname);
		free(obj);
		free(devname);
	}
	media_close_ifname(priv);

#define DEMUX_TSOUT	"demux-tsout #"
#define DVR_TSOUT	"dvr-tsout #"

	for (i = 0; i < topo->num_entities; i++) {
		struct media_v2_entity *entity = &entities[i];
		struct graph_obj *gobj;
		int first, idx;

		if (max_tsout) {
			int i = 0;

			if (!strncmp(entity->name, DEMUX_TSOUT, strlen(DEMUX_TSOUT)))
				i = atoi(&entity->name[strlen(DEMUX_TSOUT)]);
			else if (!strncmp(entity->name, DVR_TSOUT, strlen(DVR_TSOUT)))
				i = atoi(&entity->name[strlen(DVR_TSOUT)]);

			if (i >= max_tsout)
				continue;
		}

		gobj = find_gobj(mc, entity->id);
		obj = objname(entity->id, '_');
		printf("\t%s [label=\"{", obj);
		free(obj);

		/* Print the sink pads */
		if (!gobj || gobj->num_pad_sinks) {
			first = 1;
			idx = 0;
			printf("{");
			for (j = 0; j < topo->num_pads; j++) {
				if (pads[j].entity_id != entity->id)
					continue;

				if (pads[j].flags & MEDIA_PAD_FL_SINK) {
					if (first)
						first = 0;
					else
						printf (" | ");

					obj = objname(pads[j].id, '_');
					printf("<%s> %d", obj, idx);
					free(obj);
				}
				idx++;
			}
			printf("} | ");
		}
		obj = objname(entity->id, '_');
		printf("%s\\n%s\\n%s", obj, ent_function(entity->function), entity->name);
		free(obj);

		/* Print the source pads */
		if (!gobj || gobj->num_pad_sources) {
			int pad_count = 0;
			first = 1;
			idx = 0;
			printf(" | {");

			for (j = 0; j < topo->num_pads; j++) {
				if (pads[j].entity_id != entity->id)
					continue;

				if (entity->function == MEDIA_ENT_F_TS_DEMUX && pad_count > max_tsout)
					continue;
				pad_count++;

				if (pads[j].flags & MEDIA_PAD_FL_SOURCE) {
					if (first)
						first = 0;
					else
						printf (" | ");

					obj = objname(pads[j].id, '_');
					printf("<%s> %d", obj, idx);
					free(obj);
				}
				idx++;
			}
			printf("}");
		}
		printf("}\", ");
		if (!gobj || (gobj->num_pad_sources && gobj->num_pad_sinks))
			printf(STYLE_ENTITY"]\n");
		else if (gobj->num_pad_sinks)
			printf(STYLE_ENT_SINK"]\n");
		else
			printf(STYLE_ENT_SRC"]\n");
	}

	for (i = 0; i < topo->num_links; i++) {
		struct media_v2_link *link = &links[i];
		char *source_pad_obj, *sink_pad_obj;
		char *source_ent_obj, *sink_ent_obj;

		if (media_type(link->source_id) == MEDIA_GRAPH_PAD) {
			struct media_v2_entity *source, *sink;
			struct graph_obj *gobj, *parent;

			source_pad_obj = objname(link->source_id, '_');
			gobj = find_gobj(mc, link->source_id);
			if (!gobj) {
				show(RED, 0, "Graph object for %s not found\n",
				     source_pad_obj);
				free(source_pad_obj);
				continue;
			}
			parent = gobj->parent;
			if (!parent) {
				show(RED, 0, "Sink entity for %s not found\n",
				     source_pad_obj);
				free(source_pad_obj);
				continue;
			}
			source = parent->entity;

			sink_pad_obj = objname(link->sink_id, '_');

			gobj = find_gobj(mc, link->sink_id);
			if (!gobj) {
				show(RED, 0, "Graph object for %s not found\n",
				     sink_pad_obj);
				free(source_pad_obj);
				free(sink_pad_obj);
				continue;
			}
			parent = gobj->parent;
			if (!parent) {
				show(RED, 0, "Sink entity for %s not found\n",
				     sink_pad_obj);
				free(source_pad_obj);
				free(sink_pad_obj);
				continue;
			}
			sink = parent->entity;

			if (max_tsout) {
				int i = 0;

				if (!strncmp(sink->name, DEMUX_TSOUT, strlen(DEMUX_TSOUT)))
					i = atoi(&sink->name[strlen(DEMUX_TSOUT)]);
				else if (!strncmp(sink->name, DVR_TSOUT, strlen(DVR_TSOUT)))
					i = atoi(&sink->name[strlen(DVR_TSOUT)]);
				if (i >= max_tsout)
					continue;
			}

			source_ent_obj = objname(source->id, '_');
			sink_ent_obj = objname(sink->id, '_');

			printf("\t%s:%s -> %s:%s [" STYLE_DATA_LINK,
			       source_ent_obj, source_pad_obj,
			       sink_ent_obj, sink_pad_obj);
			if (!(link->flags & MEDIA_LNK_FL_ENABLED))
				printf(" style=\"dashed\"");
			printf("]\n");

			free(source_pad_obj);
			free(sink_pad_obj);
			free(source_ent_obj);
			free(sink_ent_obj);
		}

		if (media_type(link->source_id) == MEDIA_GRAPH_INTF_DEVNODE) {
			struct media_v2_entity *sink;
			struct graph_obj *gobj;

			sink_ent_obj = objname(link->sink_id, '_');
			gobj = find_gobj(mc, link->sink_id);
			if (!gobj) {
				show(RED, 0, "Graph object for %s not found\n",
				     sink_ent_obj);
				free(sink_ent_obj);
				continue;
			}
			sink = gobj->entity;

			if (max_tsout) {
				int i = 0;

				if (!strncmp(sink->name, DEMUX_TSOUT, strlen(DEMUX_TSOUT)))
					i = atoi(&sink->name[strlen(DEMUX_TSOUT)]);
				else if (!strncmp(sink->name, DVR_TSOUT, strlen(DVR_TSOUT)))
					i = atoi(&sink->name[strlen(DVR_TSOUT)]);
				if (i >= max_tsout)
					continue;
			}

			source_ent_obj = objname(link->source_id, '_');

			printf("\t%s -> %s [" STYLE_INTF_LINK,
			       source_ent_obj, sink_ent_obj);
			if (!(link->flags & MEDIA_LNK_FL_ENABLED))
				printf(" style=\"dashed\"");
			printf("]\n");

			free(source_ent_obj);
			free(sink_ent_obj);
		}
	}

	printf ("}\n");
}

int main(int argc, char *argv[])
{
	struct media_controller *mc;
	int rc;

	argp_parse(&argp, argc, argv, ARGP_NO_HELP | ARGP_NO_EXIT, 0, 0);

	mc = mc_open(media_device);
	if (!mc)
	return -1;

	rc = media_get_topology(mc);
	if (rc) {
		mc_close(mc);
		return -2;
	}

	if (show_dot) {
		media_show_graphviz(mc);
	} else {
		if (show_entities)
			media_show_entities(mc);

		if (show_interfaces)
			media_show_interfaces(mc);

		if (show_data_links || show_intf_links)
			media_show_links(mc);
	}

	mc_close(mc);

	return 0;
}

Privacy Policy