1 /* test_sec_ne.c - Test for Not-Equal section filters.
2  * usage: DEMUX=/dev/dvb/adapterX/demuxX test_sec_ne pid [tid [tid_ext]]
3  *
4  * Copyright (C) 2002 convergence GmbH
5  * Johannes Stezenbach <js@convergence.de>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20  */
21 
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <fcntl.h>
29 #include <sys/ioctl.h>
30 #include <errno.h>
31 
32 #include <linux/dvb/dmx.h>
33 
34 #include "hex_dump.h"
35 
36 #define MAX_SECTION_SIZE 8192
37 
38 static unsigned int version_number;
39 
usage(void)40 void usage(void)
41 {
42 	fprintf(stderr, "usage: test_sec_ne pid [tid [tid_ext]]\n");
43 	fprintf(stderr, "       Read the version_number from the first section\n");
44 	fprintf(stderr, "       and sec a filter to wait for it to change.\n");
45 	fprintf(stderr, "       The default demux device used can be changed\n");
46 	fprintf(stderr, "       using the DEMUX environment variable;\n");
47 	exit(1);
48 }
49 
50 
process_section(int fd)51 void process_section(int fd)
52 {
53 	uint8_t buf[MAX_SECTION_SIZE];
54 	int bytes;
55 
56 	bytes = read(fd, buf, sizeof(buf));
57 	if (bytes < 0) {
58 		perror("read");
59 		if (bytes != ETIMEDOUT)
60 			exit(1);
61 	}
62 	hex_dump(buf, bytes);
63 	version_number = ((buf[5] >> 1) & 0x1f);
64 	printf("current version_number: %u\n\n", version_number);
65 }
66 
set_filter(int fd,unsigned int pid,unsigned int tid,unsigned int tid_ext,unsigned int _version_number)67 int set_filter(int fd, unsigned int pid, unsigned int tid,
68 		unsigned int tid_ext, unsigned int _version_number)
69 {
70 	struct dmx_sct_filter_params f;
71 	unsigned long bufsz;
72 
73 	if (getenv("BUFFER")) {
74 		bufsz=strtoul(getenv("BUFFER"), NULL, 0);
75 		if (bufsz > 0 && bufsz <= MAX_SECTION_SIZE) {
76 			fprintf(stderr, "DMX_SET_BUFFER_SIZE %lu\n", bufsz);
77 			if (ioctl(fd, DMX_SET_BUFFER_SIZE, bufsz) == -1) {
78 				perror("DMX_SET_BUFFER_SIZE");
79 				return 1;
80 			}
81 		}
82 	}
83 	memset(&f.filter, 0, sizeof(struct dmx_filter));
84 	f.pid = (uint16_t) pid;
85 	if (tid < 0x100) {
86 		f.filter.filter[0] = (uint8_t) tid;
87 		f.filter.mask[0]   = 0xff;
88 	}
89 	if (tid_ext < 0x10000) {
90 		f.filter.filter[1] = (uint8_t) (tid_ext >> 8);
91 		f.filter.filter[2] = (uint8_t) (tid_ext & 0xff);
92 		f.filter.mask[1]   = 0xff;
93 		f.filter.mask[2]   = 0xff;
94 	}
95 	if (_version_number < 0x20) {
96 		f.filter.filter[3] = (uint8_t) (_version_number << 1);
97 		f.filter.mask[3]   = 0x3e;
98 		f.filter.mode[3]   = 0x3e;
99 	}
100 	f.timeout = 0;
101 	f.flags = DMX_IMMEDIATE_START;
102 
103 	if (ioctl(fd, DMX_SET_FILTER, &f) == -1) {
104 		perror("DMX_SET_FILTER");
105 		return 1;
106 	}
107 	return 0;
108 }
109 
110 
main(int argc,char * argv[])111 int main(int argc, char *argv[])
112 {
113 	int dmxfd;
114 	unsigned long pid, tid, tid_ext;
115 	char * dmxdev = "/dev/dvb/adapter0/demux0";
116 
117 	if (argc < 2 || argc > 4)
118 		usage();
119 
120 	pid = strtoul(argv[1], NULL, 0);
121 	if (pid > 0x1fff)
122 		usage();
123 	if (argc > 2) {
124 		tid = strtoul(argv[2], NULL, 0);
125 		if (tid > 0xff)
126 			usage();
127 	} else
128 		tid = 0x100;
129 	if (argc > 3) {
130 		tid_ext = strtoul(argv[3], NULL, 0);
131 		if (tid_ext > 0xffff)
132 			usage();
133 	} else
134 		tid_ext = 0x10000;
135 
136 	if (getenv("DEMUX"))
137 		dmxdev = getenv("DEMUX");
138 	fprintf(stderr, "test_sec_ne: using '%s'\n", dmxdev);
139 	fprintf(stderr, "             PID 0x%04lx\n", pid);
140 	if (tid < 0x100)
141 		fprintf(stderr, "               TID 0x%02lx\n", tid);
142 	if (tid_ext < 0x10000)
143 		fprintf(stderr, "               TID EXT 0x%04lx\n", tid_ext);
144 
145 	if ((dmxfd = open(dmxdev, O_RDWR)) < 0){
146 		perror("open");
147 		return 1;
148 	}
149 
150 	if (set_filter(dmxfd, pid, tid, tid_ext, 0xff) != 0)
151 		return 1;
152 	process_section(dmxfd);
153 	while (1) {
154 		if (set_filter(dmxfd, pid, tid, tid_ext, version_number) != 0)
155 			return 1;
156 		process_section(dmxfd);
157 	}
158 	for (;;) {
159 		process_section(dmxfd);
160 	}
161 
162 	close(dmxfd);
163 	return 0;
164 }
165