dvbnet.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * dvbnet.c
  3. *
  4. * Copyright (C) 2003 TV Files S.p.A
  5. * L.Y.Mesentsev <lymes@tiscalinet.it>
  6. *
  7. * Ported to use new DVB libraries:
  8. * Copyright (C) 2006 Andrew de Quincey <adq_dvb@lidskialf.net>
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version 2
  13. * of the License, or (at your option) any later version.
  14. *
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  25. * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
  26. *
  27. */
  28. #include <stdio.h>
  29. #include <fcntl.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <unistd.h>
  33. #include <errno.h>
  34. #include <sys/stat.h>
  35. #include <sys/types.h>
  36. #include <sys/ioctl.h>
  37. #include <libdvbapi/dvbnet.h>
  38. #define OK 0
  39. #define FAIL -1
  40. enum Mode {
  41. UNKNOWN,
  42. LST_INTERFACE,
  43. ADD_INTERFACE,
  44. DEL_INTERFACE
  45. } op_mode;
  46. static int adapter = 0;
  47. static int netdev = 0;
  48. static void hello(void);
  49. static void usage(char *);
  50. static void parse_args(int, char **);
  51. static void queryInterface(int);
  52. int ifnum;
  53. int pid;
  54. int encapsulation;
  55. int main(int argc, char **argv)
  56. {
  57. int fd_net;
  58. hello();
  59. parse_args(argc, argv);
  60. if ((fd_net = dvbnet_open(adapter, netdev)) < 0) {
  61. fprintf(stderr, "Error: couldn't open device %d: %d %m\n",
  62. netdev, errno);
  63. return FAIL;
  64. }
  65. switch (op_mode) {
  66. case DEL_INTERFACE:
  67. if (dvbnet_remove_interface(fd_net, ifnum))
  68. fprintf(stderr,
  69. "Error: couldn't remove interface %d: %d %m.\n",
  70. ifnum, errno);
  71. else
  72. printf("Status: device %d removed successfully.\n",
  73. ifnum);
  74. break;
  75. case ADD_INTERFACE:
  76. if ((ifnum = dvbnet_add_interface(fd_net, pid, encapsulation)) < 0)
  77. fprintf(stderr,
  78. "Error: couldn't add interface for pid %d: %d %m.\n",
  79. pid, errno);
  80. else
  81. printf
  82. ("Status: device dvb%d_%d for pid %d created successfully.\n",
  83. adapter, ifnum, pid);
  84. break;
  85. case LST_INTERFACE:
  86. queryInterface(fd_net);
  87. break;
  88. default:
  89. usage(argv[0]);
  90. return FAIL;
  91. }
  92. close(fd_net);
  93. return OK;
  94. }
  95. void queryInterface(int fd_net)
  96. {
  97. int IF, nIFaces = 0;
  98. char *encap;
  99. printf("Query DVB network interfaces:\n");
  100. printf("-----------------------------\n");
  101. for (IF = 0; IF < DVBNET_MAX_INTERFACES; IF++) {
  102. uint16_t _pid;
  103. enum dvbnet_encap _encapsulation;
  104. if (dvbnet_get_interface(fd_net, IF, &_pid, &_encapsulation))
  105. continue;
  106. encap = "???";
  107. switch(_encapsulation) {
  108. case DVBNET_ENCAP_MPE:
  109. encap = "MPE";
  110. break;
  111. case DVBNET_ENCAP_ULE:
  112. encap = "ULE";
  113. break;
  114. }
  115. printf("Found device %d: interface dvb%d_%d, "
  116. "listening on PID %d, encapsulation %s\n",
  117. IF, adapter, IF, _pid, encap);
  118. nIFaces++;
  119. }
  120. printf("-----------------------------\n");
  121. printf("Found %d interface(s).\n\n", nIFaces);
  122. }
  123. void parse_args(int argc, char **argv)
  124. {
  125. int c;
  126. char *s;
  127. op_mode = UNKNOWN;
  128. encapsulation = DVBNET_ENCAP_MPE;
  129. while ((c = getopt(argc, argv, "a:n:p:d:lUvh")) != EOF) {
  130. switch (c) {
  131. case 'a':
  132. adapter = strtol(optarg, NULL, 0);
  133. break;
  134. case 'n':
  135. netdev = strtol(optarg, NULL, 0);
  136. break;
  137. case 'p':
  138. pid = strtol(optarg, NULL, 0);
  139. op_mode = ADD_INTERFACE;
  140. break;
  141. case 'd':
  142. ifnum = strtol(optarg, NULL, 0);
  143. op_mode = DEL_INTERFACE;
  144. break;
  145. case 'l':
  146. op_mode = LST_INTERFACE;
  147. break;
  148. case 'U':
  149. encapsulation = DVBNET_ENCAP_ULE;
  150. break;
  151. case 'v':
  152. exit(OK);
  153. case 'h':
  154. default:
  155. s = strrchr(argv[0], '/') + 1;
  156. usage((s) ? s : argv[0]);
  157. exit(FAIL);
  158. }
  159. }
  160. }
  161. void usage(char *prog_name)
  162. {
  163. fprintf(stderr, "Usage: %s [options]\n", prog_name);
  164. fprintf(stderr, "Where options are:\n");
  165. fprintf(stderr, "\t-a AD : Adapter card (default 0)\n");
  166. fprintf(stderr, "\t-n DD : Demux (default 0)\n");
  167. fprintf(stderr, "\t-p PID : Add interface listening on PID\n");
  168. fprintf(stderr, "\t-d NUM : Remove interface NUM\n");
  169. fprintf(stderr, "\t-l : List currently available interfaces\n");
  170. fprintf(stderr, "\t-U : use ULE framing (default: MPE)\n" );
  171. fprintf(stderr, "\t-v : Print current version\n\n");
  172. }
  173. void hello(void)
  174. {
  175. printf("\nDVB Network Interface Manager\n");
  176. printf("Copyright (C) 2003, TV Files S.p.A\n\n");
  177. }