dvbdate.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*
  2. dvbdate - a program to set the system date and time from a TDT multiplex
  3. Copyright (C) Laurence Culhane 2002 <dvbdate@holmes.demon.co.uk>
  4. Mercilessly ripped off from dvbtune, Copyright (C) Dave Chapman 2001
  5. Revamped by Johannes Stezenbach <js@convergence.de>
  6. and Michael Hunold <hunold@convergence.de>
  7. Ported to use the standard dvb libraries and add ATSC STT
  8. support Andrew de Quincey <adq_dvb@lidskialf.net>
  9. This program is free software; you can redistribute it and/or
  10. modify it under the terms of the GNU General Public License
  11. as published by the Free Software Foundation; either version 2
  12. of the License, or (at your option) any later version.
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU General Public License for more details.
  17. You should have received a copy of the GNU 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. Or, point your browser to http://www.gnu.org/copyleft/gpl.html
  21. Copyright (C) Laurence Culhane 2002 <dvbdate@holmes.demon.co.uk>
  22. */
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <unistd.h>
  26. #include <string.h>
  27. #include <sys/types.h>
  28. #include <sys/stat.h>
  29. #include <fcntl.h>
  30. #include <sys/ioctl.h>
  31. #include <sys/poll.h>
  32. #include <errno.h>
  33. #include <getopt.h>
  34. #include <stdarg.h>
  35. #include <libdvbapi/dvbfe.h>
  36. #include <libdvbapi/dvbdemux.h>
  37. #include <libucsi/dvb/section.h>
  38. #include <libucsi/atsc/section.h>
  39. /* How many seconds can the system clock be out before we get warned? */
  40. #define ALLOWABLE_DELTA 30*60
  41. char *ProgName;
  42. int do_print;
  43. int do_set;
  44. int do_force;
  45. int do_quiet;
  46. int timeout = 25;
  47. int adapter = 0;
  48. void errmsg(char *message, ...)
  49. {
  50. va_list ap;
  51. va_start(ap, message);
  52. fprintf(stderr, "%s: ", ProgName);
  53. vfprintf(stderr, message, ap);
  54. va_end(ap);
  55. }
  56. void usage(void)
  57. {
  58. fprintf(stderr, "usage: %s [-a] [-p] [-s] [-f] [-q] [-h]\n", ProgName);
  59. _exit(1);
  60. }
  61. void help(void)
  62. {
  63. fprintf(stderr,
  64. "\nhelp:\n"
  65. "%s [-a] [-p] [-s] [-f] [-q] [-h] [-t n]\n"
  66. " --adapter (adapter to use, default: 0)\n"
  67. " --print (print current time, received time and delta)\n"
  68. " --set (set the system clock to received time)\n"
  69. " --force (force the setting of the clock)\n"
  70. " --quiet (be silent)\n"
  71. " --help (display this message)\n"
  72. " --timeout n (max seconds to wait, default: 25)\n", ProgName);
  73. _exit(1);
  74. }
  75. int do_options(int arg_count, char **arg_strings)
  76. {
  77. static struct option Long_Options[] = {
  78. {"print", 0, 0, 'p'},
  79. {"set", 0, 0, 's'},
  80. {"force", 0, 0, 'f'},
  81. {"quiet", 0, 0, 'q'},
  82. {"help", 0, 0, 'h'},
  83. {"timeout", 1, 0, 't'},
  84. {"adapter", 1, 0, 'a'},
  85. {0, 0, 0, 0}
  86. };
  87. int c;
  88. int Option_Index = 0;
  89. while (1) {
  90. c = getopt_long(arg_count, arg_strings, "a:psfqht:", Long_Options, &Option_Index);
  91. if (c == EOF)
  92. break;
  93. switch (c) {
  94. case 't':
  95. timeout = atoi(optarg);
  96. if (0 == timeout) {
  97. fprintf(stderr, "%s: invalid timeout value\n", ProgName);
  98. usage();
  99. }
  100. break;
  101. case 'a':
  102. adapter = atoi(optarg);
  103. break;
  104. case 'p':
  105. do_print = 1;
  106. break;
  107. case 's':
  108. do_set = 1;
  109. break;
  110. case 'f':
  111. do_force = 1;
  112. break;
  113. case 'q':
  114. do_quiet = 1;
  115. break;
  116. case 'h':
  117. help();
  118. break;
  119. case '?':
  120. usage();
  121. break;
  122. case 0:
  123. /*
  124. * Which long option has been selected? We only need this extra switch
  125. * to cope with the case of wanting to assign two long options the same
  126. * short character code.
  127. */
  128. printf("long option index %d\n", Option_Index);
  129. switch (Option_Index) {
  130. case 0: /* Print */
  131. case 1: /* Set */
  132. case 2: /* Force */
  133. case 3: /* Quiet */
  134. case 4: /* Help */
  135. case 5: /* timeout */
  136. case 6: /* adapter */
  137. break;
  138. default:
  139. fprintf(stderr, "%s: unknown long option %d\n", ProgName, Option_Index);
  140. usage();
  141. }
  142. break;
  143. /*
  144. * End of Special Long-opt handling code
  145. */
  146. default:
  147. fprintf(stderr, "%s: unknown getopt error - returned code %02x\n", ProgName, c);
  148. _exit(1);
  149. }
  150. }
  151. return 0;
  152. }
  153. /*
  154. * Get the next UTC date packet from the TDT section
  155. */
  156. int dvb_scan_date(time_t *rx_time, unsigned int to)
  157. {
  158. int tdt_fd;
  159. uint8_t filter[18];
  160. uint8_t mask[18];
  161. unsigned char sibuf[4096];
  162. int size;
  163. // open the demuxer
  164. if ((tdt_fd = dvbdemux_open_demux(adapter, 0, 0)) < 0) {
  165. return -1;
  166. }
  167. // create a section filter for the TDT
  168. memset(filter, 0, sizeof(filter));
  169. memset(mask, 0, sizeof(mask));
  170. filter[0] = stag_dvb_time_date;
  171. mask[0] = 0xFF;
  172. if (dvbdemux_set_section_filter(tdt_fd, TRANSPORT_TDT_PID, filter, mask, 1, 1)) {
  173. close(tdt_fd);
  174. return -1;
  175. }
  176. // poll for data
  177. struct pollfd pollfd;
  178. pollfd.fd = tdt_fd;
  179. pollfd.events = POLLIN|POLLERR|POLLPRI;
  180. if (poll(&pollfd, 1, to * 1000) != 1) {
  181. close(tdt_fd);
  182. return -1;
  183. }
  184. // read it
  185. if ((size = read(tdt_fd, sibuf, sizeof(sibuf))) < 0) {
  186. close(tdt_fd);
  187. return -1;
  188. }
  189. // parse section
  190. struct section *section = section_codec(sibuf, size);
  191. if (section == NULL) {
  192. close(tdt_fd);
  193. return -1;
  194. }
  195. // parse TDT
  196. struct dvb_tdt_section *tdt = dvb_tdt_section_codec(section);
  197. if (tdt == NULL) {
  198. close(tdt_fd);
  199. return -1;
  200. }
  201. // done
  202. *rx_time = dvbdate_to_unixtime(tdt->utc_time);
  203. close(tdt_fd);
  204. return 0;
  205. }
  206. /*
  207. * Get the next date packet from the STT section
  208. */
  209. int atsc_scan_date(time_t *rx_time, unsigned int to)
  210. {
  211. int stt_fd;
  212. uint8_t filter[18];
  213. uint8_t mask[18];
  214. unsigned char sibuf[4096];
  215. int size;
  216. // open the demuxer
  217. if ((stt_fd = dvbdemux_open_demux(adapter, 0, 0)) < 0) {
  218. return -1;
  219. }
  220. // create a section filter for the STT
  221. memset(filter, 0, sizeof(filter));
  222. memset(mask, 0, sizeof(mask));
  223. filter[0] = stag_atsc_system_time;
  224. mask[0] = 0xFF;
  225. if (dvbdemux_set_section_filter(stt_fd, ATSC_BASE_PID, filter, mask, 1, 1)) {
  226. close(stt_fd);
  227. return -1;
  228. }
  229. // poll for data
  230. struct pollfd pollfd;
  231. pollfd.fd = stt_fd;
  232. pollfd.events = POLLIN|POLLERR|POLLPRI;
  233. if (poll(&pollfd, 1, to * 1000) != 1) {
  234. close(stt_fd);
  235. return -1;
  236. }
  237. // read it
  238. if ((size = read(stt_fd, sibuf, sizeof(sibuf))) < 0) {
  239. close(stt_fd);
  240. return -1;
  241. }
  242. // parse section
  243. struct section *section = section_codec(sibuf, size);
  244. if (section == NULL) {
  245. close(stt_fd);
  246. return -1;
  247. }
  248. struct section_ext *section_ext = section_ext_decode(section, 0);
  249. if (section_ext == NULL) {
  250. close(stt_fd);
  251. return -1;
  252. }
  253. struct atsc_section_psip *psip = atsc_section_psip_decode(section_ext);
  254. if (psip == NULL) {
  255. close(stt_fd);
  256. return -1;
  257. }
  258. // parse STT
  259. struct atsc_stt_section *stt = atsc_stt_section_codec(psip);
  260. if (stt == NULL) {
  261. close(stt_fd);
  262. return -1;
  263. }
  264. // done
  265. *rx_time = atsctime_to_unixtime(stt->system_time);
  266. close(stt_fd);
  267. return 0;
  268. }
  269. /*
  270. * Set the system time
  271. */
  272. int set_time(time_t * new_time)
  273. {
  274. if (stime(new_time)) {
  275. perror("Unable to set time");
  276. return -1;
  277. }
  278. return 0;
  279. }
  280. int main(int argc, char **argv)
  281. {
  282. time_t rx_time;
  283. time_t real_time;
  284. time_t offset;
  285. int ret;
  286. struct dvbfe_handle *fe;
  287. struct dvbfe_info fe_info;
  288. do_print = 0;
  289. do_force = 0;
  290. do_set = 0;
  291. do_quiet = 0;
  292. ProgName = argv[0];
  293. /*
  294. * Process command line arguments
  295. */
  296. do_options(argc, argv);
  297. if (do_quiet && do_print) {
  298. errmsg("quiet and print options are mutually exclusive.\n");
  299. exit(1);
  300. }
  301. /*
  302. * Find the frontend type
  303. */
  304. if ((fe = dvbfe_open(adapter, 0, 1)) == NULL) {
  305. errmsg("Unable to open frontend.\n");
  306. exit(1);
  307. }
  308. dvbfe_get_info(fe, 0, &fe_info, DVBFE_INFO_QUERYTYPE_IMMEDIATE, 0);
  309. /*
  310. * Get the date from the currently tuned multiplex
  311. */
  312. switch(fe_info.type) {
  313. case DVBFE_TYPE_DVBS:
  314. case DVBFE_TYPE_DVBC:
  315. case DVBFE_TYPE_DVBT:
  316. ret = dvb_scan_date(&rx_time, timeout);
  317. break;
  318. case DVBFE_TYPE_ATSC:
  319. ret = atsc_scan_date(&rx_time, timeout);
  320. break;
  321. default:
  322. errmsg("Unsupported frontend type.\n");
  323. exit(1);
  324. }
  325. if (ret != 0) {
  326. errmsg("Unable to get time from multiplex.\n");
  327. exit(1);
  328. }
  329. time(&real_time);
  330. offset = rx_time - real_time;
  331. if (do_print) {
  332. fprintf(stdout, "System time: %s", ctime(&real_time));
  333. fprintf(stdout, " RX time: %s", ctime(&rx_time));
  334. fprintf(stdout, " Offset: %ld seconds\n", offset);
  335. } else if (!do_quiet) {
  336. fprintf(stdout, "%s", ctime(&rx_time));
  337. }
  338. if (do_set) {
  339. if (labs(offset) > ALLOWABLE_DELTA) {
  340. if (do_force) {
  341. if (0 != set_time(&rx_time)) {
  342. errmsg("setting the time failed\n");
  343. }
  344. } else {
  345. errmsg("multiplex time differs by more than %d from system.\n", ALLOWABLE_DELTA);
  346. errmsg("use -f to force system clock to new time.\n");
  347. exit(1);
  348. }
  349. } else {
  350. set_time(&rx_time);
  351. }
  352. } /* #end if (do_set) */
  353. return (0);
  354. }