azap.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <sys/ioctl.h>
  4. #include <unistd.h>
  5. #include <stdlib.h>
  6. #include <stdint.h>
  7. #include <string.h>
  8. #include <stdio.h>
  9. #include <fcntl.h>
  10. #include <ctype.h>
  11. #include <errno.h>
  12. #include <signal.h>
  13. #include <linux/dvb/frontend.h>
  14. #include <linux/dvb/dmx.h>
  15. #include "util.h"
  16. static char FRONTEND_DEV [80];
  17. static char DEMUX_DEV [80];
  18. static int timeout_flag=0;
  19. static int timeout=0;
  20. #define CHANNEL_FILE "/.azap/channels.conf"
  21. #define ERROR(x...) \
  22. do { \
  23. fprintf(stderr, "ERROR: "); \
  24. fprintf(stderr, x); \
  25. fprintf (stderr, "\n"); \
  26. } while (0)
  27. #define PERROR(x...) \
  28. do { \
  29. fprintf(stderr, "ERROR: "); \
  30. fprintf(stderr, x); \
  31. fprintf (stderr, " (%s)\n", strerror(errno)); \
  32. } while (0)
  33. typedef struct {
  34. char *name;
  35. int value;
  36. } Param;
  37. static const Param modulation_list [] = {
  38. { "8VSB", VSB_8 },
  39. { "16VSB", VSB_16 },
  40. { "QAM_64", QAM_64 },
  41. { "QAM_256", QAM_256 },
  42. };
  43. #define LIST_SIZE(x) sizeof(x)/sizeof(Param)
  44. static void do_timeout(int x)
  45. {
  46. (void)x;
  47. if (timeout_flag == 0) {
  48. timeout_flag = 1;
  49. alarm(2);
  50. signal(SIGALRM, do_timeout);
  51. } else {
  52. /* something has gone wrong ... exit */
  53. exit(1);
  54. }
  55. }
  56. static
  57. int parse_param (int fd, const Param * plist, int list_size, int *param)
  58. {
  59. char c;
  60. int character = 0;
  61. int _index = 0;
  62. while (1) {
  63. if (read(fd, &c, 1) < 1)
  64. return -1; /* EOF? */
  65. if ((c == ':' || c == '\n')
  66. && plist->name[character] == '\0')
  67. break;
  68. while (toupper(c) != plist->name[character]) {
  69. _index++;
  70. plist++;
  71. if (_index >= list_size) /* parse error, no valid */
  72. return -2; /* parameter name found */
  73. }
  74. character++;
  75. }
  76. *param = plist->value;
  77. return 0;
  78. }
  79. static
  80. int parse_int(int fd, int *val)
  81. {
  82. char number[11]; /* 2^32 needs 10 digits... */
  83. int character = 0;
  84. while (1) {
  85. if (read(fd, &number[character], 1) < 1)
  86. return -1; /* EOF? */
  87. if (number[character] == ':' || number[character] == '\n') {
  88. number[character] = '\0';
  89. break;
  90. }
  91. if (!isdigit(number[character]))
  92. return -2; /* parse error, not a digit... */
  93. character++;
  94. if (character > 10) /* overflow, number too big */
  95. return -3; /* to fit in 32 bit */
  96. };
  97. errno = 0;
  98. *val = strtol(number, NULL, 10);
  99. if (errno == ERANGE)
  100. return -4;
  101. return 0;
  102. }
  103. static
  104. int find_channel(int fd, const char *channel)
  105. {
  106. int character = 0;
  107. while (1) {
  108. char c;
  109. if (read(fd, &c, 1) < 1)
  110. return -1; /* EOF! */
  111. if (c == ':' && channel[character] == '\0')
  112. break;
  113. if (toupper(c) == toupper(channel[character]))
  114. character++;
  115. else
  116. character = 0;
  117. };
  118. return 0;
  119. }
  120. static
  121. int try_parse_int(int fd, int *val, const char *pname)
  122. {
  123. int err;
  124. err = parse_int(fd, val);
  125. if (err)
  126. ERROR("error while parsing %s (%s)", pname,
  127. err == -1 ? "end of file" :
  128. err == -2 ? "not a number" : "number too big");
  129. return err;
  130. }
  131. static
  132. int try_parse_param(int fd, const Param * plist, int list_size, int *param,
  133. const char *pname)
  134. {
  135. int err;
  136. err = parse_param(fd, plist, list_size, param);
  137. if (err)
  138. ERROR("error while parsing %s (%s)", pname,
  139. err == -1 ? "end of file" : "syntax error");
  140. return err;
  141. }
  142. int parse(const char *fname, const char *channel,
  143. struct dvb_frontend_parameters *frontend, int *vpid, int *apid, int *sid)
  144. {
  145. int fd;
  146. int err;
  147. int tmp;
  148. if ((fd = open(fname, O_RDONLY | O_NONBLOCK)) < 0) {
  149. PERROR ("could not open file '%s'", fname);
  150. perror ("");
  151. return -1;
  152. }
  153. if (find_channel(fd, channel) < 0) {
  154. ERROR("could not find channel '%s' in channel list", channel);
  155. return -2;
  156. }
  157. if ((err = try_parse_int(fd, &tmp, "frequency")))
  158. return -3;
  159. frontend->frequency = tmp;
  160. if ((err = try_parse_param(fd,
  161. modulation_list, LIST_SIZE(modulation_list),
  162. &tmp, "modulation")))
  163. return -4;
  164. frontend->u.vsb.modulation = tmp;
  165. if ((err = try_parse_int(fd, vpid, "Video PID")))
  166. return -5;
  167. if ((err = try_parse_int(fd, apid, "Audio PID")))
  168. return -6;
  169. if ((err = try_parse_int(fd, sid, "Service ID")))
  170. return -7;
  171. close(fd);
  172. return 0;
  173. }
  174. static
  175. int setup_frontend (int fe_fd, struct dvb_frontend_parameters *frontend)
  176. {
  177. uint32_t mstd;
  178. if (check_frontend(fe_fd, FE_ATSC, &mstd) < 0) {
  179. close(fe_fd);
  180. return -1;
  181. }
  182. /* TODO! Some frontends need to be explicit delivery system */
  183. printf ("tuning to %i Hz\n", frontend->frequency);
  184. if (ioctl(fe_fd, FE_SET_FRONTEND, frontend) < 0) {
  185. PERROR("ioctl FE_SET_FRONTEND failed");
  186. return -1;
  187. }
  188. return 0;
  189. }
  190. static
  191. int monitor_frontend (int fe_fd)
  192. {
  193. fe_status_t status;
  194. uint16_t snr, signal;
  195. uint32_t ber, uncorrected_blocks;
  196. do {
  197. ioctl(fe_fd, FE_READ_STATUS, &status);
  198. ioctl(fe_fd, FE_READ_SIGNAL_STRENGTH, &signal);
  199. ioctl(fe_fd, FE_READ_SNR, &snr);
  200. ioctl(fe_fd, FE_READ_BER, &ber);
  201. ioctl(fe_fd, FE_READ_UNCORRECTED_BLOCKS, &uncorrected_blocks);
  202. printf ("status %02x | signal %04x | snr %04x | "
  203. "ber %08x | unc %08x | ",
  204. status, signal, snr, ber, uncorrected_blocks);
  205. if (status & FE_HAS_LOCK)
  206. printf("FE_HAS_LOCK");
  207. usleep(1000000);
  208. printf("\n");
  209. } while (!timeout_flag);
  210. return 0;
  211. }
  212. static const char *usage = "\nusage: %s [-a adapter_num] [-f frontend_id] [-d demux_id] [-c conf_file] [-t timout_seconds] [-r] [-p] <channel name>\n\n";
  213. int main(int argc, char **argv)
  214. {
  215. struct dvb_frontend_parameters frontend_param;
  216. char *homedir = getenv ("HOME");
  217. char *confname = NULL;
  218. char *channel = NULL;
  219. int adapter = 0, frontend = 0, demux = 0, dvr = 0;
  220. int vpid, apid, sid, pmtpid = 0;
  221. int pat_fd, pmt_fd;
  222. int frontend_fd, audio_fd, video_fd;
  223. int opt;
  224. int rec_psi = 0;
  225. while ((opt = getopt(argc, argv, "hrpnt:a:f:d:c:")) != -1) {
  226. switch (opt) {
  227. case 'a':
  228. adapter = strtoul(optarg, NULL, 0);
  229. break;
  230. case 'f':
  231. frontend = strtoul(optarg, NULL, 0);
  232. break;
  233. case 'd':
  234. demux = strtoul(optarg, NULL, 0);
  235. break;
  236. case 't':
  237. timeout = strtoul(optarg, NULL, 0);
  238. break;
  239. case 'r':
  240. dvr = 1;
  241. break;
  242. case 'p':
  243. rec_psi = 1;
  244. break;
  245. case 'c':
  246. confname = optarg;
  247. break;
  248. case '?':
  249. case 'h':
  250. default:
  251. fprintf (stderr, usage, argv[0]);
  252. return -1;
  253. };
  254. }
  255. if (optind < argc)
  256. channel = argv[optind];
  257. if (!channel) {
  258. fprintf (stderr, usage, argv[0]);
  259. return -1;
  260. }
  261. snprintf (FRONTEND_DEV, sizeof(FRONTEND_DEV),
  262. "/dev/dvb/adapter%i/frontend%i", adapter, frontend);
  263. snprintf (DEMUX_DEV, sizeof(DEMUX_DEV),
  264. "/dev/dvb/adapter%i/demux%i", adapter, demux);
  265. printf ("using '%s' and '%s'\n", FRONTEND_DEV, DEMUX_DEV);
  266. if (!confname)
  267. {
  268. if (!homedir)
  269. ERROR ("$HOME not set");
  270. confname = malloc (strlen(homedir) + strlen(CHANNEL_FILE) + 1);
  271. memcpy (confname, homedir, strlen(homedir));
  272. memcpy (confname + strlen(homedir), CHANNEL_FILE,
  273. strlen(CHANNEL_FILE) + 1);
  274. }
  275. memset(&frontend_param, 0, sizeof(struct dvb_frontend_parameters));
  276. if (parse (confname, channel, &frontend_param, &vpid, &apid, &sid))
  277. return -1;
  278. if ((frontend_fd = open(FRONTEND_DEV, O_RDWR | O_NONBLOCK)) < 0) {
  279. PERROR ("failed opening '%s'", FRONTEND_DEV);
  280. return -1;
  281. }
  282. if (setup_frontend (frontend_fd, &frontend_param) < 0)
  283. return -1;
  284. signal(SIGALRM, do_timeout);
  285. if (timeout > 0)
  286. alarm(timeout);
  287. if (rec_psi) {
  288. pmtpid = get_pmt_pid(DEMUX_DEV, sid);
  289. if (pmtpid <= 0) {
  290. fprintf(stderr,"couldn't find pmt-pid for sid %04x\n",sid);
  291. return -1;
  292. }
  293. if ((pat_fd = open(DEMUX_DEV, O_RDWR)) < 0) {
  294. perror("opening pat demux failed");
  295. return -1;
  296. }
  297. if (set_pesfilter(pat_fd, 0, DMX_PES_OTHER, dvr) < 0)
  298. return -1;
  299. if ((pmt_fd = open(DEMUX_DEV, O_RDWR)) < 0) {
  300. perror("opening pmt demux failed");
  301. return -1;
  302. }
  303. if (set_pesfilter(pmt_fd, pmtpid, DMX_PES_OTHER, dvr) < 0)
  304. return -1;
  305. }
  306. if ((video_fd = open(DEMUX_DEV, O_RDWR)) < 0) {
  307. PERROR("failed opening '%s'", DEMUX_DEV);
  308. return -1;
  309. }
  310. printf ("video pid 0x%04x, audio pid 0x%04x\n", vpid, apid);
  311. if (set_pesfilter (video_fd, vpid, DMX_PES_VIDEO, dvr) < 0)
  312. return -1;
  313. if ((audio_fd = open(DEMUX_DEV, O_RDWR)) < 0) {
  314. PERROR("failed opening '%s'", DEMUX_DEV);
  315. return -1;
  316. }
  317. if (set_pesfilter (audio_fd, apid, DMX_PES_AUDIO, dvr) < 0)
  318. return -1;
  319. monitor_frontend (frontend_fd);
  320. close (pat_fd);
  321. close (pmt_fd);
  322. close (audio_fd);
  323. close (video_fd);
  324. close (frontend_fd);
  325. return 0;
  326. }