tzap.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. /* tzap -- DVB-T zapping utility
  2. */
  3. /*
  4. * Added recording to a file
  5. * arguments:
  6. *
  7. * -t timeout (seconds)
  8. * -o filename output filename (use -o - for stdout)
  9. * -s only print summary
  10. * -S run silently (no output)
  11. *
  12. * Bernard Hatt 24/2/04
  13. */
  14. #define _FILE_OFFSET_BITS 64
  15. #define _LARGEFILE_SOURCE 1
  16. #define _LARGEFILE64_SOURCE 1
  17. #include <sys/types.h>
  18. #include <sys/stat.h>
  19. #include <sys/ioctl.h>
  20. #include <unistd.h>
  21. #include <stdlib.h>
  22. #include <stdint.h>
  23. #include <string.h>
  24. #include <stdio.h>
  25. #include <fcntl.h>
  26. #include <ctype.h>
  27. #include <errno.h>
  28. #include <signal.h>
  29. #include <linux/dvb/frontend.h>
  30. #include <linux/dvb/dmx.h>
  31. #include "util.h"
  32. static char FRONTEND_DEV [80];
  33. static char DEMUX_DEV [80];
  34. static char DVR_DEV [80];
  35. static int timeout_flag=0;
  36. static int silent=0,timeout=0;
  37. static int exit_after_tuning;
  38. #define CHANNEL_FILE "channels.conf"
  39. #define ERROR(x...) \
  40. do { \
  41. fprintf(stderr, "ERROR: "); \
  42. fprintf(stderr, x); \
  43. fprintf (stderr, "\n"); \
  44. } while (0)
  45. #define PERROR(x...) \
  46. do { \
  47. fprintf(stderr, "ERROR: "); \
  48. fprintf(stderr, x); \
  49. fprintf (stderr, " (%s)\n", strerror(errno)); \
  50. } while (0)
  51. typedef struct {
  52. char *name;
  53. int value;
  54. } Param;
  55. static const Param inversion_list [] = {
  56. { "INVERSION_OFF", INVERSION_OFF },
  57. { "INVERSION_ON", INVERSION_ON },
  58. { "INVERSION_AUTO", INVERSION_AUTO }
  59. };
  60. static const Param bw_list [] = {
  61. { "BANDWIDTH_6_MHZ", BANDWIDTH_6_MHZ },
  62. { "BANDWIDTH_7_MHZ", BANDWIDTH_7_MHZ },
  63. { "BANDWIDTH_8_MHZ", BANDWIDTH_8_MHZ }
  64. };
  65. static const Param fec_list [] = {
  66. { "FEC_1_2", FEC_1_2 },
  67. { "FEC_2_3", FEC_2_3 },
  68. { "FEC_3_4", FEC_3_4 },
  69. { "FEC_4_5", FEC_4_5 },
  70. { "FEC_5_6", FEC_5_6 },
  71. { "FEC_6_7", FEC_6_7 },
  72. { "FEC_7_8", FEC_7_8 },
  73. { "FEC_8_9", FEC_8_9 },
  74. { "FEC_AUTO", FEC_AUTO },
  75. { "FEC_NONE", FEC_NONE }
  76. };
  77. static const Param guard_list [] = {
  78. {"GUARD_INTERVAL_1_16", GUARD_INTERVAL_1_16},
  79. {"GUARD_INTERVAL_1_32", GUARD_INTERVAL_1_32},
  80. {"GUARD_INTERVAL_1_4", GUARD_INTERVAL_1_4},
  81. {"GUARD_INTERVAL_1_8", GUARD_INTERVAL_1_8},
  82. {"GUARD_INTERVAL_AUTO", GUARD_INTERVAL_AUTO}
  83. };
  84. static const Param hierarchy_list [] = {
  85. { "HIERARCHY_1", HIERARCHY_1 },
  86. { "HIERARCHY_2", HIERARCHY_2 },
  87. { "HIERARCHY_4", HIERARCHY_4 },
  88. { "HIERARCHY_NONE", HIERARCHY_NONE },
  89. { "HIERARCHY_AUTO", HIERARCHY_AUTO }
  90. };
  91. static const Param constellation_list [] = {
  92. { "QPSK", QPSK },
  93. { "QAM_128", QAM_128 },
  94. { "QAM_16", QAM_16 },
  95. { "QAM_256", QAM_256 },
  96. { "QAM_32", QAM_32 },
  97. { "QAM_64", QAM_64 },
  98. { "QAM_AUTO", QAM_AUTO }
  99. };
  100. static const Param transmissionmode_list [] = {
  101. { "TRANSMISSION_MODE_2K", TRANSMISSION_MODE_2K },
  102. { "TRANSMISSION_MODE_8K", TRANSMISSION_MODE_8K },
  103. { "TRANSMISSION_MODE_AUTO", TRANSMISSION_MODE_AUTO }
  104. };
  105. #define LIST_SIZE(x) sizeof(x)/sizeof(Param)
  106. static int parse_param (int fd, const Param * plist, int list_size, int *param)
  107. {
  108. char c;
  109. int character = 0;
  110. int _index = 0;
  111. while (1) {
  112. if (read(fd, &c, 1) < 1)
  113. return -1; /* EOF? */
  114. if ((c == ':' || c == '\n')
  115. && plist->name[character] == '\0')
  116. break;
  117. while (toupper(c) != plist->name[character]) {
  118. _index++;
  119. plist++;
  120. if (_index >= list_size) /* parse error, no valid */
  121. return -2; /* parameter name found */
  122. }
  123. character++;
  124. }
  125. *param = plist->value;
  126. return 0;
  127. }
  128. static int parse_int(int fd, int *val)
  129. {
  130. char number[11]; /* 2^32 needs 10 digits... */
  131. int character = 0;
  132. while (1) {
  133. if (read(fd, &number[character], 1) < 1)
  134. return -1; /* EOF? */
  135. if (number[character] == ':' || number[character] == '\n') {
  136. number[character] = '\0';
  137. break;
  138. }
  139. if (!isdigit(number[character]))
  140. return -2; /* parse error, not a digit... */
  141. character++;
  142. if (character > 10) /* overflow, number too big */
  143. return -3; /* to fit in 32 bit */
  144. };
  145. errno = 0;
  146. *val = strtol(number, NULL, 10);
  147. if (errno == ERANGE)
  148. return -4;
  149. return 0;
  150. }
  151. static int find_channel(int fd, const char *channel)
  152. {
  153. int character = 0;
  154. while (1) {
  155. char c;
  156. if (read(fd, &c, 1) < 1)
  157. return -1; /* EOF! */
  158. if ( '\n' == c ) /* start of line */
  159. character = 0;
  160. else if ( character >= 0 ) { /* we are in the namefield */
  161. if (c == ':' && channel[character] == '\0')
  162. break;
  163. if (toupper(c) == toupper(channel[character]))
  164. character++;
  165. else
  166. character = -1;
  167. }
  168. };
  169. return 0;
  170. }
  171. static
  172. int try_parse_int(int fd, int *val, const char *pname)
  173. {
  174. int err;
  175. err = parse_int(fd, val);
  176. if (err)
  177. ERROR("error while parsing %s (%s)", pname,
  178. err == -1 ? "end of file" :
  179. err == -2 ? "not a number" : "number too big");
  180. return err;
  181. }
  182. static
  183. int try_parse_param(int fd, const Param * plist, int list_size, int *param,
  184. const char *pname)
  185. {
  186. int err;
  187. err = parse_param(fd, plist, list_size, param);
  188. if (err)
  189. ERROR("error while parsing %s (%s)", pname,
  190. err == -1 ? "end of file" : "syntax error");
  191. return err;
  192. }
  193. static int check_fec(fe_code_rate_t *fec)
  194. {
  195. switch (*fec)
  196. {
  197. case FEC_NONE:
  198. *fec = FEC_AUTO;
  199. case FEC_AUTO:
  200. case FEC_1_2:
  201. case FEC_2_3:
  202. case FEC_3_4:
  203. case FEC_5_6:
  204. case FEC_7_8:
  205. return 0;
  206. default:
  207. ;
  208. }
  209. return 1;
  210. }
  211. int parse(const char *fname, const char *channel,
  212. struct dvb_frontend_parameters *frontend, int *vpid, int *apid,
  213. int *sid)
  214. {
  215. int fd;
  216. int err;
  217. int tmp;
  218. if ((fd = open(fname, O_RDONLY | O_NONBLOCK)) < 0) {
  219. PERROR ("could not open file '%s'", fname);
  220. perror ("");
  221. return -1;
  222. }
  223. if (find_channel(fd, channel) < 0) {
  224. ERROR("could not find channel '%s' in channel list", channel);
  225. return -2;
  226. }
  227. if ((err = try_parse_int(fd, &tmp, "frequency")))
  228. return -3;
  229. frontend->frequency = tmp;
  230. if ((err = try_parse_param(fd,
  231. inversion_list, LIST_SIZE(inversion_list),
  232. &tmp, "inversion")))
  233. return -4;
  234. frontend->inversion = tmp;
  235. if ((err = try_parse_param(fd, bw_list, LIST_SIZE(bw_list),
  236. &tmp, "bandwidth")))
  237. return -5;
  238. frontend->u.ofdm.bandwidth = tmp;
  239. if ((err = try_parse_param(fd, fec_list, LIST_SIZE(fec_list),
  240. &tmp, "code_rate_HP")))
  241. return -6;
  242. frontend->u.ofdm.code_rate_HP = tmp;
  243. if (check_fec(&frontend->u.ofdm.code_rate_HP))
  244. return -6;
  245. if ((err = try_parse_param(fd, fec_list, LIST_SIZE(fec_list),
  246. &tmp, "code_rate_LP")))
  247. return -7;
  248. frontend->u.ofdm.code_rate_LP = tmp;
  249. if (check_fec(&frontend->u.ofdm.code_rate_LP))
  250. return -7;
  251. if ((err = try_parse_param(fd, constellation_list,
  252. LIST_SIZE(constellation_list),
  253. &tmp, "constellation")))
  254. return -8;
  255. frontend->u.ofdm.constellation = tmp;
  256. if ((err = try_parse_param(fd, transmissionmode_list,
  257. LIST_SIZE(transmissionmode_list),
  258. &tmp, "transmission_mode")))
  259. return -9;
  260. frontend->u.ofdm.transmission_mode = tmp;
  261. if ((err = try_parse_param(fd, guard_list, LIST_SIZE(guard_list),
  262. &tmp, "guard_interval")))
  263. return -10;
  264. frontend->u.ofdm.guard_interval = tmp;
  265. if ((err = try_parse_param(fd, hierarchy_list,
  266. LIST_SIZE(hierarchy_list),
  267. &tmp, "hierarchy_information")))
  268. return -11;
  269. frontend->u.ofdm.hierarchy_information = tmp;
  270. if ((err = try_parse_int(fd, vpid, "Video PID")))
  271. return -12;
  272. if ((err = try_parse_int(fd, apid, "Audio PID")))
  273. return -13;
  274. if ((err = try_parse_int(fd, sid, "Service ID")))
  275. return -14;
  276. close(fd);
  277. return 0;
  278. }
  279. static int setup_frontend (int fe_fd, struct dvb_frontend_parameters *frontend)
  280. {
  281. int ret;
  282. uint32_t mstd;
  283. if (check_frontend(fe_fd, FE_OFDM, &mstd) < 0) {
  284. close(fe_fd);
  285. return -1;
  286. }
  287. ret = dvbfe_set_delsys(fe_fd, SYS_DVBT);
  288. if (ret) {
  289. PERROR("SET Delsys failed");
  290. return -1;
  291. }
  292. if (silent < 2)
  293. fprintf (stderr,"tuning to %i Hz\n", frontend->frequency);
  294. if (ioctl(fe_fd, FE_SET_FRONTEND, frontend) < 0) {
  295. PERROR("ioctl FE_SET_FRONTEND failed");
  296. return -1;
  297. }
  298. return 0;
  299. }
  300. static void do_timeout(int x)
  301. {
  302. (void)x;
  303. if (timeout_flag == 0) {
  304. timeout_flag = 1;
  305. alarm(2);
  306. signal(SIGALRM, do_timeout);
  307. } else {
  308. /* something has gone wrong ... exit */
  309. exit(1);
  310. }
  311. }
  312. static void print_frontend_stats(int fe_fd, int human_readable)
  313. {
  314. fe_status_t status;
  315. uint16_t snr, _signal;
  316. uint32_t ber, uncorrected_blocks;
  317. ioctl(fe_fd, FE_READ_STATUS, &status);
  318. ioctl(fe_fd, FE_READ_SIGNAL_STRENGTH, &_signal);
  319. ioctl(fe_fd, FE_READ_SNR, &snr);
  320. ioctl(fe_fd, FE_READ_BER, &ber);
  321. ioctl(fe_fd, FE_READ_UNCORRECTED_BLOCKS, &uncorrected_blocks);
  322. if (human_readable) {
  323. printf ("status %02x | signal %3u%% | snr %3u%% | ber %d | unc %d | ",
  324. status, (_signal * 100) / 0xffff, (snr * 100) / 0xffff, ber, uncorrected_blocks);
  325. } else {
  326. fprintf (stderr, "status %02x | signal %04x | snr %04x | ber %08x | unc %08x | ",
  327. status, _signal, snr, ber, uncorrected_blocks);
  328. }
  329. if (status & FE_HAS_LOCK)
  330. fprintf(stderr,"FE_HAS_LOCK");
  331. fprintf(stderr,"\n");
  332. }
  333. static
  334. int monitor_frontend (int fe_fd, int human_readable)
  335. {
  336. fe_status_t status;
  337. do {
  338. ioctl(fe_fd, FE_READ_STATUS, &status);
  339. if (!silent)
  340. print_frontend_stats(fe_fd, human_readable);
  341. if (exit_after_tuning && (status & FE_HAS_LOCK))
  342. break;
  343. usleep(1000000);
  344. } while (!timeout_flag);
  345. if (silent < 2)
  346. print_frontend_stats (fe_fd, human_readable);
  347. return 0;
  348. }
  349. #define BUFLEN (188*256)
  350. static void copy_to_file(int in_fd, int out_fd)
  351. {
  352. char buf[BUFLEN];
  353. int r;
  354. long long int rc = 0LL;
  355. while (timeout_flag==0) {
  356. r=read(in_fd,buf,BUFLEN);
  357. if (r < 0) {
  358. if (errno == EOVERFLOW) {
  359. printf("buffer overrun\n");
  360. continue;
  361. }
  362. PERROR("Read failed");
  363. break;
  364. }
  365. if (write(out_fd,buf,r) < 0) {
  366. PERROR("Write failed");
  367. break;
  368. }
  369. rc+=r;
  370. }
  371. if (silent<2) {
  372. fprintf(stderr, "copied %lld bytes (%lld Kbytes/sec)\n",rc,rc/(1024*timeout));
  373. }
  374. }
  375. static char *usage =
  376. "usage:\n"
  377. " tzap [options] <channel_name>\n"
  378. " zap to channel channel_name (case insensitive)\n"
  379. " -a number : use given adapter (default 0)\n"
  380. " -f number : use given frontend (default 0)\n"
  381. " -d number : use given demux (default 0)\n"
  382. " -c file : read channels list from 'file'\n"
  383. " -x : exit after tuning\n"
  384. " -r : set up /dev/dvb/adapterX/dvr0 for TS recording\n"
  385. " -p : add pat and pmt to TS recording (implies -r)\n"
  386. " -s : only print summary\n"
  387. " -S : run silently (no output)\n"
  388. " -H : human readable output\n"
  389. " -F : set up frontend only, don't touch demux\n"
  390. " -t number : timeout (seconds)\n"
  391. " -o file : output filename (use -o - for stdout)\n"
  392. " -h -? : display this help and exit\n";
  393. int main(int argc, char **argv)
  394. {
  395. struct dvb_frontend_parameters frontend_param;
  396. char *homedir = getenv("HOME");
  397. char *confname = NULL;
  398. char *channel = NULL;
  399. int adapter = 0, frontend = 0, demux = 0, dvr = 0;
  400. int vpid, apid, sid, pmtpid = 0;
  401. int pat_fd, pmt_fd;
  402. int frontend_fd, audio_fd = 0, video_fd = 0, dvr_fd, file_fd;
  403. int opt;
  404. int record = 0;
  405. int frontend_only = 0;
  406. char *filename = NULL;
  407. int human_readable = 0, rec_psi = 0;
  408. while ((opt = getopt(argc, argv, "H?hrpxRsFSn:a:f:d:c:t:o:")) != -1) {
  409. switch (opt) {
  410. case 'a':
  411. adapter = strtoul(optarg, NULL, 0);
  412. break;
  413. case 'f':
  414. frontend = strtoul(optarg, NULL, 0);
  415. break;
  416. case 'd':
  417. demux = strtoul(optarg, NULL, 0);
  418. break;
  419. case 't':
  420. timeout = strtoul(optarg, NULL, 0);
  421. break;
  422. case 'o':
  423. filename = strdup(optarg);
  424. record=1;
  425. /* fall through */
  426. case 'r':
  427. dvr = 1;
  428. break;
  429. case 'p':
  430. rec_psi = 1;
  431. break;
  432. case 'x':
  433. exit_after_tuning = 1;
  434. break;
  435. case 'c':
  436. confname = optarg;
  437. break;
  438. case 's':
  439. silent = 1;
  440. break;
  441. case 'S':
  442. silent = 2;
  443. break;
  444. case 'F':
  445. frontend_only = 1;
  446. break;
  447. case 'H':
  448. human_readable = 1;
  449. break;
  450. case '?':
  451. case 'h':
  452. default:
  453. fprintf (stderr, usage, argv[0]);
  454. return -1;
  455. };
  456. }
  457. if (optind < argc)
  458. channel = argv[optind];
  459. if (!channel) {
  460. fprintf (stderr, usage, argv[0]);
  461. return -1;
  462. }
  463. snprintf(FRONTEND_DEV,
  464. sizeof(FRONTEND_DEV),
  465. "/dev/dvb/adapter%i/frontend%i",
  466. adapter,
  467. frontend);
  468. snprintf(DEMUX_DEV,
  469. sizeof(DEMUX_DEV),
  470. "/dev/dvb/adapter%i/demux%i",
  471. adapter,
  472. demux);
  473. snprintf(DVR_DEV,
  474. sizeof(DVR_DEV),
  475. "/dev/dvb/adapter%i/dvr%i",
  476. adapter,
  477. demux);
  478. if (silent < 2)
  479. fprintf (stderr,"using '%s' and '%s'\n", FRONTEND_DEV, DEMUX_DEV);
  480. if (!confname) {
  481. int len = strlen(homedir) + strlen(CHANNEL_FILE) + 18;
  482. if (!homedir)
  483. ERROR ("$HOME not set");
  484. confname = malloc (len);
  485. snprintf(confname, len, "%s/.tzap/%i/%s", homedir, adapter, CHANNEL_FILE);
  486. if (access (confname, R_OK))
  487. snprintf(confname, len, "%s/.tzap/%s", homedir, CHANNEL_FILE);
  488. }
  489. printf("reading channels from file '%s'\n", confname);
  490. memset(&frontend_param, 0, sizeof(struct dvb_frontend_parameters));
  491. if (parse (confname, channel, &frontend_param, &vpid, &apid, &sid))
  492. return -1;
  493. if ((frontend_fd = open(FRONTEND_DEV, O_RDWR | O_NONBLOCK)) < 0) {
  494. PERROR ("failed opening '%s'", FRONTEND_DEV);
  495. return -1;
  496. }
  497. if (setup_frontend (frontend_fd, &frontend_param) < 0)
  498. return -1;
  499. if (frontend_only)
  500. goto just_the_frontend_dude;
  501. if (rec_psi) {
  502. pmtpid = get_pmt_pid(DEMUX_DEV, sid);
  503. if (pmtpid <= 0) {
  504. fprintf(stderr,"couldn't find pmt-pid for sid %04x\n",sid);
  505. return -1;
  506. }
  507. if ((pat_fd = open(DEMUX_DEV, O_RDWR)) < 0) {
  508. perror("opening pat demux failed");
  509. return -1;
  510. }
  511. if (set_pesfilter(pat_fd, 0, DMX_PES_OTHER, dvr) < 0)
  512. return -1;
  513. if ((pmt_fd = open(DEMUX_DEV, O_RDWR)) < 0) {
  514. perror("opening pmt demux failed");
  515. return -1;
  516. }
  517. if (set_pesfilter(pmt_fd, pmtpid, DMX_PES_OTHER, dvr) < 0)
  518. return -1;
  519. }
  520. if ((video_fd = open(DEMUX_DEV, O_RDWR)) < 0) {
  521. PERROR("failed opening '%s'", DEMUX_DEV);
  522. return -1;
  523. }
  524. if (silent<2)
  525. fprintf(stderr,"video pid 0x%04x, audio pid 0x%04x\n", vpid, apid);
  526. if (set_pesfilter(video_fd, vpid, DMX_PES_VIDEO, dvr) < 0)
  527. return -1;
  528. if ((audio_fd = open(DEMUX_DEV, O_RDWR)) < 0) {
  529. PERROR("failed opening '%s'", DEMUX_DEV);
  530. return -1;
  531. }
  532. if (set_pesfilter(audio_fd, apid, DMX_PES_AUDIO, dvr) < 0)
  533. return -1;
  534. signal(SIGALRM,do_timeout);
  535. if (timeout > 0)
  536. alarm(timeout);
  537. if (record) {
  538. if (filename!=NULL) {
  539. if (strcmp(filename,"-")!=0) {
  540. file_fd = open(filename,O_WRONLY|O_LARGEFILE|O_CREAT,0644);
  541. if (file_fd<0) {
  542. PERROR("open of '%s' failed",filename);
  543. return -1;
  544. }
  545. } else {
  546. file_fd=1;
  547. }
  548. } else {
  549. PERROR("Record mode but no filename!");
  550. return -1;
  551. }
  552. if ((dvr_fd = open(DVR_DEV, O_RDONLY)) < 0) {
  553. PERROR("failed opening '%s'", DVR_DEV);
  554. return -1;
  555. }
  556. if (silent<2)
  557. print_frontend_stats(frontend_fd, human_readable);
  558. copy_to_file(dvr_fd,file_fd);
  559. if (silent<2)
  560. print_frontend_stats(frontend_fd, human_readable);
  561. } else {
  562. just_the_frontend_dude:
  563. monitor_frontend(frontend_fd, human_readable);
  564. }
  565. close(pat_fd);
  566. close(pmt_fd);
  567. close(audio_fd);
  568. close(video_fd);
  569. close(frontend_fd);
  570. return 0;
  571. }