1 /* test_sections.c - Test for section filters.
2 * usage: DEMUX=/dev/dvb/adapterX/demuxX test_sections [PID [TID]]
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
39 #if !defined(DMX_FILTER_SIZE)
40 #define DMX_FILTER_SIZE 16
41 #endif
42
parse_filter(unsigned char * filter,const char * filter_desc)43 static void parse_filter(unsigned char* filter, const char* filter_desc)
44 {
45 int filter_idx;
46 char* end_ptr;
47
48 memset(filter, '\0', DMX_FILTER_SIZE);
49
50 for (filter_idx = 1; /* Leave first byte for tid */
51 filter_idx < DMX_FILTER_SIZE-1; ++filter_idx) {
52 filter[filter_idx] = strtoul(filter_desc, &end_ptr, 0);
53
54 if (*end_ptr == '\0' || end_ptr == filter_desc)
55 break;
56
57 filter_desc = end_ptr;
58 }
59 }
60
usage(void)61 void usage(void)
62 {
63 fprintf(stderr, "usage: test_sections PID [TID] [FILTER] [MASK]\n");
64 fprintf(stderr, " The default demux device used can be changed\n");
65 fprintf(stderr, " using the DEMUX environment variable;\n");
66 fprintf(stderr, " set env BUFFER to play with DMX_SET_BUFFER_SIZE\n");
67 fprintf(stderr, "\n");
68 fprintf(stderr, " The optional filter and mask may be used to filter on\n");
69 fprintf(stderr, " additional bytes. These bytes may be given in hex or dec\n");
70 fprintf(stderr, " separated by spaces (hint: you may have to use quotes around\n");
71 fprintf(stderr, " FILTER and MASK). TID is always the first byte of the filter,\n");
72 fprintf(stderr, " and the first byte from FILTER applies to byte 3 of the section\n");
73 fprintf(stderr, " data (i.e. the first byte after the section length)\n");
74 exit(1);
75 }
76
77
process_section(int fd)78 void process_section(int fd)
79 {
80 uint8_t buf[MAX_SECTION_SIZE];
81 int bytes;
82
83 bytes = read(fd, buf, sizeof(buf));
84 if (bytes < 0) {
85 perror("read");
86 if (errno != EOVERFLOW)
87 exit(1);
88 }
89 hex_dump(buf, bytes);
90 printf("\n");
91 }
92
set_filter(int fd,unsigned int pid,const unsigned char * filter,const unsigned char * mask)93 int set_filter(int fd, unsigned int pid,
94 const unsigned char* filter, const unsigned char* mask)
95 {
96 struct dmx_sct_filter_params f;
97 unsigned long bufsz;
98
99 if (getenv("BUFFER")) {
100 bufsz=strtoul(getenv("BUFFER"), NULL, 0);
101 if (bufsz > 0 && bufsz <= MAX_SECTION_SIZE) {
102 fprintf(stderr, "DMX_SET_BUFFER_SIZE %lu\n", bufsz);
103 if (ioctl(fd, DMX_SET_BUFFER_SIZE, bufsz) == -1) {
104 perror("DMX_SET_BUFFER_SIZE");
105 return 1;
106 }
107 }
108 }
109 memset(&f.filter, 0, sizeof(struct dmx_filter));
110 f.pid = (uint16_t) pid;
111
112 memcpy(f.filter.filter, filter, DMX_FILTER_SIZE);
113 memcpy(f.filter.mask, mask, DMX_FILTER_SIZE);
114 f.timeout = 0;
115 f.flags = DMX_IMMEDIATE_START | DMX_CHECK_CRC;
116
117 if (ioctl(fd, DMX_SET_FILTER, &f) == -1) {
118 perror("DMX_SET_FILTER");
119 return 1;
120 }
121 return 0;
122 }
123
main(int argc,char * argv[])124 int main(int argc, char *argv[])
125 {
126 int dmxfd;
127 unsigned long pid, tid;
128 char * dmxdev = "/dev/dvb/adapter0/demux0";
129 unsigned char filter[DMX_FILTER_SIZE];
130 unsigned char mask[DMX_FILTER_SIZE];
131 int filter_idx;
132
133 if (argc < 2 || argc > 5)
134 usage();
135
136 pid = strtoul(argv[1], NULL, 0);
137 if (pid > 0x1fff)
138 usage();
139 if (argc > 2) {
140 tid = strtoul(argv[2], NULL, 0);
141 if (tid > 0xff)
142 usage();
143 } else
144 tid = 0x100;
145
146 if (argc > 3)
147 parse_filter(filter, argv[3]);
148 else
149 memset(filter, '\0', sizeof(filter));
150
151 if (argc > 4)
152 parse_filter(mask, argv[4]);
153 else
154 memset(mask, '\0', sizeof(mask));
155
156 if (tid < 0x100) {
157 filter[0] = tid;
158 mask[0] = 0xff;
159 }
160
161 if (getenv("DEMUX"))
162 dmxdev = getenv("DEMUX");
163 fprintf(stderr, "test_sections: using '%s'\n", dmxdev);
164 fprintf(stderr, " PID 0x%04lx\n", pid);
165 if (tid < 0x100)
166 fprintf(stderr, " TID 0x%02lx\n", tid);
167
168
169 fprintf(stderr, " Filter ");
170
171 for (filter_idx = 0; filter_idx < DMX_FILTER_SIZE; ++filter_idx)
172 fprintf(stderr, "0x%.2x ", filter[filter_idx]);
173 fprintf(stderr, "\n");
174
175 fprintf(stderr, " Mask ");
176
177 for (filter_idx = 0; filter_idx < DMX_FILTER_SIZE; ++filter_idx)
178 fprintf(stderr, "0x%.2x ", mask[filter_idx]);
179
180 fprintf(stderr, "\n");
181
182 if ((dmxfd = open(dmxdev, O_RDWR)) < 0){
183 perror("open");
184 return 1;
185 }
186
187 if (set_filter(dmxfd, pid, filter, mask) != 0)
188 return 1;
189
190 for (;;) {
191 process_section(dmxfd);
192 }
193
194 close(dmxfd);
195 return 0;
196 }
197