test_sec_ne.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. #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. static unsigned int version_number;
  34. void usage(void)
  35. {
  36. fprintf(stderr, "usage: test_sec_ne pid [tid [tid_ext]]\n");
  37. fprintf(stderr, " Read the version_number from the first section\n");
  38. fprintf(stderr, " and sec a filter to wait for it to change.\n");
  39. fprintf(stderr, " The default demux device used can be changed\n");
  40. fprintf(stderr, " using the DEMUX environment variable;\n");
  41. exit(1);
  42. }
  43. void process_section(int fd)
  44. {
  45. uint8_t buf[MAX_SECTION_SIZE];
  46. int bytes;
  47. bytes = read(fd, buf, sizeof(buf));
  48. if (bytes < 0) {
  49. perror("read");
  50. if (bytes != ETIMEDOUT)
  51. exit(1);
  52. }
  53. hex_dump(buf, bytes);
  54. version_number = ((buf[5] >> 1) & 0x1f);
  55. printf("current version_number: %u\n\n", version_number);
  56. }
  57. int set_filter(int fd, unsigned int pid, unsigned int tid,
  58. unsigned int tid_ext, unsigned int _version_number)
  59. {
  60. struct dmx_sct_filter_params f;
  61. unsigned long bufsz;
  62. if (getenv("BUFFER")) {
  63. bufsz=strtoul(getenv("BUFFER"), NULL, 0);
  64. if (bufsz > 0 && bufsz <= MAX_SECTION_SIZE) {
  65. fprintf(stderr, "DMX_SET_BUFFER_SIZE %lu\n", bufsz);
  66. if (ioctl(fd, DMX_SET_BUFFER_SIZE, bufsz) == -1) {
  67. perror("DMX_SET_BUFFER_SIZE");
  68. return 1;
  69. }
  70. }
  71. }
  72. memset(&f.filter, 0, sizeof(struct dmx_filter));
  73. f.pid = (uint16_t) pid;
  74. if (tid < 0x100) {
  75. f.filter.filter[0] = (uint8_t) tid;
  76. f.filter.mask[0] = 0xff;
  77. }
  78. if (tid_ext < 0x10000) {
  79. f.filter.filter[1] = (uint8_t) (tid_ext >> 8);
  80. f.filter.filter[2] = (uint8_t) (tid_ext & 0xff);
  81. f.filter.mask[1] = 0xff;
  82. f.filter.mask[2] = 0xff;
  83. }
  84. if (_version_number < 0x20) {
  85. f.filter.filter[3] = (uint8_t) (_version_number << 1);
  86. f.filter.mask[3] = 0x3e;
  87. f.filter.mode[3] = 0x3e;
  88. }
  89. f.timeout = 0;
  90. f.flags = DMX_IMMEDIATE_START;
  91. if (ioctl(fd, DMX_SET_FILTER, &f) == -1) {
  92. perror("DMX_SET_FILTER");
  93. return 1;
  94. }
  95. return 0;
  96. }
  97. int main(int argc, char *argv[])
  98. {
  99. int dmxfd;
  100. unsigned long pid, tid, tid_ext;
  101. char * dmxdev = "/dev/dvb/adapter0/demux0";
  102. if (argc < 2 || argc > 4)
  103. usage();
  104. pid = strtoul(argv[1], NULL, 0);
  105. if (pid > 0x1fff)
  106. usage();
  107. if (argc > 2) {
  108. tid = strtoul(argv[2], NULL, 0);
  109. if (tid > 0xff)
  110. usage();
  111. } else
  112. tid = 0x100;
  113. if (argc > 3) {
  114. tid_ext = strtoul(argv[3], NULL, 0);
  115. if (tid_ext > 0xffff)
  116. usage();
  117. } else
  118. tid_ext = 0x10000;
  119. if (getenv("DEMUX"))
  120. dmxdev = getenv("DEMUX");
  121. fprintf(stderr, "test_sec_ne: using '%s'\n", dmxdev);
  122. fprintf(stderr, " PID 0x%04lx\n", pid);
  123. if (tid < 0x100)
  124. fprintf(stderr, " TID 0x%02lx\n", tid);
  125. if (tid_ext < 0x10000)
  126. fprintf(stderr, " TID EXT 0x%04lx\n", tid_ext);
  127. if ((dmxfd = open(dmxdev, O_RDWR)) < 0){
  128. perror("open");
  129. return 1;
  130. }
  131. if (set_filter(dmxfd, pid, tid, tid_ext, 0xff) != 0)
  132. return 1;
  133. process_section(dmxfd);
  134. while (1) {
  135. if (set_filter(dmxfd, pid, tid, tid_ext, version_number) != 0)
  136. return 1;
  137. process_section(dmxfd);
  138. }
  139. for (;;) {
  140. process_section(dmxfd);
  141. }
  142. close(dmxfd);
  143. return 0;
  144. }