zap.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. ZAP utility
  3. Copyright (C) 2004, 2005 Manu Abraham <abraham.manu@gmail.com>
  4. Copyright (C) 2006 Andrew de Quincey (adq_dvb@lidskialf.net)
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU Lesser General Public License as
  7. published by the Free Software Foundation; either version 2.1 of
  8. the License, or (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with this library; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. */
  17. #include <stdio.h>
  18. #include <unistd.h>
  19. #include <limits.h>
  20. #include <string.h>
  21. #include <fcntl.h>
  22. #include <signal.h>
  23. #include <pthread.h>
  24. #include <sys/poll.h>
  25. #include <libdvbapi/dvbdemux.h>
  26. #include <libdvbapi/dvbaudio.h>
  27. #include <libdvbsec/dvbsec_cfg.h>
  28. #include <libucsi/mpeg/section.h>
  29. #include "zap_dvb.h"
  30. #include "zap_ca.h"
  31. static void signal_handler(int _signal);
  32. static int quit_app = 0;
  33. void usage(void)
  34. {
  35. static const char *_usage = "\n"
  36. " ZAP: A zapping application\n"
  37. " Copyright (C) 2004, 2005, 2006 Manu Abraham (manu@kromtek.com)\n"
  38. " Copyright (C) 2006 Andrew de Quincey (adq_dvb@lidskialf.net)\n\n"
  39. " usage: zap <options> as follows:\n"
  40. " -h help\n"
  41. " -adapter <id> adapter to use (default 0)\n"
  42. " -frontend <id> frontend to use (default 0)\n"
  43. " -demux <id> demux to use (default 0)\n"
  44. " -caslotnum <id> ca slot number to use (default 0)\n"
  45. " -channels <filename> channels.conf file.\n"
  46. " -secfile <filename> Optional sec.conf file.\n"
  47. " -secid <secid> ID of the SEC configuration to use, one of:\n"
  48. " -nomoveca Do not attempt to move CA descriptors from stream to programme level\n"
  49. " <channel name>\n";
  50. fprintf(stderr, "%s\n", _usage);
  51. exit(1);
  52. }
  53. int find_channel(struct dvbcfg_zapchannel *channel, void *private_data)
  54. {
  55. struct dvbcfg_zapchannel *tmpchannel = private_data;
  56. if (strcmp(channel->name, tmpchannel->name) == 0) {
  57. memcpy(tmpchannel, channel, sizeof(struct dvbcfg_zapchannel));
  58. return 1;
  59. }
  60. return 0;
  61. }
  62. int main(int argc, char *argv[])
  63. {
  64. int adapter_id = 0;
  65. int frontend_id = 0;
  66. int demux_id = 0;
  67. int caslot_num = 0;
  68. char *chanfile = "/etc/channels.conf";
  69. char *secfile = NULL;
  70. char *secid = NULL;
  71. char *channel_name = NULL;
  72. int moveca = 1;
  73. int argpos = 1;
  74. struct zap_dvb_params zap_dvb_params;
  75. struct zap_ca_params zap_ca_params;
  76. while(argpos != argc) {
  77. if (!strcmp(argv[argpos], "-h")) {
  78. usage();
  79. } else if (!strcmp(argv[argpos], "-adapter")) {
  80. if ((argc - argpos) < 2)
  81. usage();
  82. if (sscanf(argv[argpos+1], "%i", &adapter_id) != 1)
  83. usage();
  84. argpos+=2;
  85. } else if (!strcmp(argv[argpos], "-frontend")) {
  86. if ((argc - argpos) < 2)
  87. usage();
  88. if (sscanf(argv[argpos+1], "%i", &frontend_id) != 1)
  89. usage();
  90. argpos+=2;
  91. } else if (!strcmp(argv[argpos], "-demux")) {
  92. if ((argc - argpos) < 2)
  93. usage();
  94. if (sscanf(argv[argpos+1], "%i", &demux_id) != 1)
  95. usage();
  96. argpos+=2;
  97. } else if (!strcmp(argv[argpos], "-caslotnum")) {
  98. if ((argc - argpos) < 2)
  99. usage();
  100. if (sscanf(argv[argpos+1], "%i", &caslot_num) != 1)
  101. usage();
  102. argpos+=2;
  103. } else if (!strcmp(argv[argpos], "-channels")) {
  104. if ((argc - argpos) < 2)
  105. usage();
  106. chanfile = argv[argpos+1];
  107. argpos+=2;
  108. } else if (!strcmp(argv[argpos], "-secfile")) {
  109. if ((argc - argpos) < 2)
  110. usage();
  111. secfile = argv[argpos+1];
  112. argpos+=2;
  113. } else if (!strcmp(argv[argpos], "-secid")) {
  114. if ((argc - argpos) < 2)
  115. usage();
  116. secid = argv[argpos+1];
  117. argpos+=2;
  118. } else if (!strcmp(argv[argpos], "-nomoveca")) {
  119. moveca = 0;
  120. argpos++;
  121. } else {
  122. if ((argc - argpos) != 1)
  123. usage();
  124. channel_name = argv[argpos];
  125. argpos++;
  126. }
  127. }
  128. // the user didn't select anything!
  129. if (channel_name == NULL)
  130. usage();
  131. // setup any signals
  132. signal(SIGINT, signal_handler);
  133. signal(SIGPIPE, SIG_IGN);
  134. // start the CA stuff
  135. zap_ca_params.adapter_id = adapter_id;
  136. zap_ca_params.caslot_num = caslot_num;
  137. zap_ca_params.moveca = moveca;
  138. zap_ca_start(&zap_ca_params);
  139. // find the requested channel
  140. if (strlen(channel_name) >= sizeof(zap_dvb_params.channel.name)) {
  141. fprintf(stderr, "Channel name is too long %s\n", channel_name);
  142. exit(1);
  143. }
  144. FILE *channel_file = fopen(chanfile, "r");
  145. if (channel_file == NULL) {
  146. fprintf(stderr, "Could open channel file %s\n", chanfile);
  147. exit(1);
  148. }
  149. memcpy(zap_dvb_params.channel.name, channel_name, strlen(channel_name) + 1);
  150. if (dvbcfg_zapchannel_parse(channel_file, find_channel, &zap_dvb_params.channel) != 1) {
  151. fprintf(stderr, "Unable to find requested channel %s\n", channel_name);
  152. exit(1);
  153. }
  154. fclose(channel_file);
  155. // default SEC with a DVBS card
  156. if ((secid == NULL) && (zap_dvb_params.channel.fe_type == DVBFE_TYPE_DVBS))
  157. secid = "UNIVERSAL";
  158. // look it up if one were supplied
  159. zap_dvb_params.valid_sec = 0;
  160. if (secid != NULL) {
  161. if (dvbsec_cfg_find(secfile, secid,
  162. &zap_dvb_params.sec)) {
  163. fprintf(stderr, "Unable to find suitable sec/lnb configuration for channel\n");
  164. exit(1);
  165. }
  166. zap_dvb_params.valid_sec = 1;
  167. }
  168. // open the frontend
  169. zap_dvb_params.fe = dvbfe_open(adapter_id, frontend_id, 0);
  170. if (zap_dvb_params.fe == NULL) {
  171. fprintf(stderr, "Failed to open frontend\n");
  172. exit(1);
  173. }
  174. // start the DVB stuff
  175. zap_dvb_params.adapter_id = adapter_id;
  176. zap_dvb_params.frontend_id = frontend_id;
  177. zap_dvb_params.demux_id = demux_id;
  178. zap_dvb_start(&zap_dvb_params);
  179. // the UI
  180. while(!quit_app) {
  181. sleep(1);
  182. }
  183. // shutdown DVB stuff
  184. if (channel_name != NULL)
  185. zap_dvb_stop();
  186. // shutdown CA stuff
  187. zap_ca_stop();
  188. // done
  189. exit(0);
  190. }
  191. static void signal_handler(int _signal)
  192. {
  193. (void) _signal;
  194. if (!quit_app) {
  195. quit_app = 1;
  196. }
  197. }