test_sections.c 5.3 KB

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