zap_dvb.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /*
  2. ZAP utility DVB functions
  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 <signal.h>
  22. #include <pthread.h>
  23. #include <sys/poll.h>
  24. #include <libdvbapi/dvbdemux.h>
  25. #include <libucsi/section.h>
  26. #include <libucsi/mpeg/section.h>
  27. #include <libucsi/dvb/section.h>
  28. #include "zap_dvb.h"
  29. #include "zap_ca.h"
  30. #define FE_STATUS_PARAMS (DVBFE_INFO_LOCKSTATUS|DVBFE_INFO_SIGNAL_STRENGTH|DVBFE_INFO_BER|DVBFE_INFO_SNR|DVBFE_INFO_UNCORRECTED_BLOCKS)
  31. static int dvbthread_shutdown = 0;
  32. static pthread_t dvbthread;
  33. static int pat_version = -1;
  34. static int ca_pmt_version = -1;
  35. static void *dvbthread_func(void* arg);
  36. static void process_pat(int pat_fd, struct zap_dvb_params *params, int *pmt_fd, struct pollfd *pollfd);
  37. static void process_tdt(int tdt_fd);
  38. static void process_pmt(int pmt_fd, struct zap_dvb_params *params);
  39. static int create_section_filter(int adapter, int demux, uint16_t pid, uint8_t table_id);
  40. int zap_dvb_start(struct zap_dvb_params *params)
  41. {
  42. pthread_create(&dvbthread, NULL, dvbthread_func, (void*) params);
  43. return 0;
  44. }
  45. void zap_dvb_stop(void)
  46. {
  47. dvbthread_shutdown = 1;
  48. pthread_join(dvbthread, NULL);
  49. }
  50. static void *dvbthread_func(void* arg)
  51. {
  52. int tune_state = 0;
  53. int pat_fd = -1;
  54. int pmt_fd = -1;
  55. int tdt_fd = -1;
  56. struct pollfd pollfds[3];
  57. struct zap_dvb_params *params = (struct zap_dvb_params *) arg;
  58. // create PAT filter
  59. if ((pat_fd = create_section_filter(params->adapter_id, params->demux_id,
  60. TRANSPORT_PAT_PID, stag_mpeg_program_association)) < 0) {
  61. fprintf(stderr, "Failed to create PAT section filter\n");
  62. exit(1);
  63. }
  64. pollfds[0].fd = pat_fd;
  65. pollfds[0].events = POLLIN|POLLPRI|POLLERR;
  66. // create TDT filter
  67. if ((tdt_fd = create_section_filter(params->adapter_id, params->demux_id, TRANSPORT_TDT_PID, stag_dvb_time_date)) < 0) {
  68. fprintf(stderr, "Failed to create TDT section filter\n");
  69. exit(1);
  70. }
  71. pollfds[1].fd = tdt_fd;
  72. pollfds[1].events = POLLIN|POLLPRI|POLLERR;
  73. // zero PMT filter
  74. pollfds[2].fd = 0;
  75. pollfds[2].events = 0;
  76. // the DVB loop
  77. while(!dvbthread_shutdown) {
  78. // tune frontend + monitor lock status
  79. if (tune_state == 0) {
  80. // get the type of frontend
  81. struct dvbfe_info result;
  82. char *types;
  83. memset(&result, 0, sizeof(result));
  84. dvbfe_get_info(params->fe, 0, &result, DVBFE_INFO_QUERYTYPE_IMMEDIATE, 0);
  85. switch(result.type) {
  86. case DVBFE_TYPE_DVBS:
  87. types = "DVB-S";
  88. break;
  89. case DVBFE_TYPE_DVBC:
  90. types = "DVB-C";
  91. break;
  92. case DVBFE_TYPE_DVBT:
  93. types = "DVB-T";
  94. break;
  95. case DVBFE_TYPE_ATSC:
  96. types = "ATSC";
  97. break;
  98. default:
  99. types = "Unknown";
  100. }
  101. fprintf(stderr, "Using frontend \"%s\", type %s\n", result.name, types);
  102. // do we have a valid SEC configuration?
  103. struct dvbsec_config *sec = NULL;
  104. if (params->valid_sec)
  105. sec = &params->sec;
  106. // tune!
  107. if (dvbsec_set(params->fe,
  108. sec,
  109. params->channel.polarization,
  110. (params->channel.diseqc_switch & 0x01) ? DISEQC_SWITCH_B : DISEQC_SWITCH_A,
  111. (params->channel.diseqc_switch & 0x02) ? DISEQC_SWITCH_B : DISEQC_SWITCH_A,
  112. &params->channel.fe_params,
  113. 0)) {
  114. fprintf(stderr, "Failed to set frontend\n");
  115. exit(1);
  116. }
  117. tune_state++;
  118. } else if (tune_state == 1) {
  119. struct dvbfe_info result;
  120. memset(&result, 0, sizeof(result));
  121. if (dvbfe_get_info(params->fe,
  122. FE_STATUS_PARAMS,
  123. &result,
  124. DVBFE_INFO_QUERYTYPE_IMMEDIATE,
  125. 0) != FE_STATUS_PARAMS) {
  126. fprintf(stderr, "Problem retrieving frontend information: %m\n");
  127. }
  128. fprintf(stderr, "status %c%c%c%c%c | signal %04x | snr %04x | ber %08x | unc %08x | %s\r",
  129. result.signal ? 'S' : ' ',
  130. result.carrier ? 'C' : ' ',
  131. result.viterbi ? 'V' : ' ',
  132. result.sync ? 'Y' : ' ',
  133. result.lock ? 'L' : ' ',
  134. result.signal_strength,
  135. result.snr,
  136. result.ber,
  137. result.ucblocks,
  138. result.lock ? "FE_HAS_LOCK" : "");
  139. fflush(stderr);
  140. if (result.lock) {
  141. tune_state++;
  142. fprintf(stderr, "\n");
  143. fflush(stderr);
  144. } else {
  145. usleep(500000);
  146. }
  147. }
  148. // is there SI data?
  149. int count = poll(pollfds, 3, 100);
  150. if (count < 0) {
  151. fprintf(stderr, "Poll error\n");
  152. break;
  153. }
  154. if (count == 0) {
  155. continue;
  156. }
  157. // PAT
  158. if (pollfds[0].revents & (POLLIN|POLLPRI)) {
  159. process_pat(pat_fd, params, &pmt_fd, &pollfds[2]);
  160. }
  161. // TDT
  162. if (pollfds[1].revents & (POLLIN|POLLPRI)) {
  163. process_tdt(tdt_fd);
  164. }
  165. // PMT
  166. if (pollfds[2].revents & (POLLIN|POLLPRI)) {
  167. process_pmt(pmt_fd, params);
  168. }
  169. }
  170. // close demuxers
  171. if (pat_fd != -1)
  172. close(pat_fd);
  173. if (pmt_fd != -1)
  174. close(pmt_fd);
  175. if (tdt_fd != -1)
  176. close(tdt_fd);
  177. return 0;
  178. }
  179. static void process_pat(int pat_fd, struct zap_dvb_params *params, int *pmt_fd, struct pollfd *pollfd)
  180. {
  181. int size;
  182. uint8_t sibuf[4096];
  183. // read the section
  184. if ((size = read(pat_fd, sibuf, sizeof(sibuf))) < 0) {
  185. return;
  186. }
  187. // parse section
  188. struct section *section = section_codec(sibuf, size);
  189. if (section == NULL) {
  190. return;
  191. }
  192. // parse section_ext
  193. struct section_ext *section_ext = section_ext_decode(section, 0);
  194. if (section_ext == NULL) {
  195. return;
  196. }
  197. if (pat_version == section_ext->version_number) {
  198. return;
  199. }
  200. // parse PAT
  201. struct mpeg_pat_section *pat = mpeg_pat_section_codec(section_ext);
  202. if (pat == NULL) {
  203. return;
  204. }
  205. // try and find the requested program
  206. struct mpeg_pat_program *cur_program;
  207. mpeg_pat_section_programs_for_each(pat, cur_program) {
  208. if (cur_program->program_number == params->channel.service_id) {
  209. // close old PMT fd
  210. if (*pmt_fd != -1)
  211. close(*pmt_fd);
  212. // create PMT filter
  213. if ((*pmt_fd = create_section_filter(params->adapter_id, params->demux_id,
  214. cur_program->pid, stag_mpeg_program_map)) < 0) {
  215. return;
  216. }
  217. pollfd->fd = *pmt_fd;
  218. pollfd->events = POLLIN|POLLPRI|POLLERR;
  219. // we have a new PMT pid
  220. ca_pmt_version = -1;
  221. break;
  222. }
  223. }
  224. // remember the PAT version
  225. pat_version = section_ext->version_number;
  226. }
  227. static void process_tdt(int tdt_fd)
  228. {
  229. int size;
  230. uint8_t sibuf[4096];
  231. // read the section
  232. if ((size = read(tdt_fd, sibuf, sizeof(sibuf))) < 0) {
  233. return;
  234. }
  235. // parse section
  236. struct section *section = section_codec(sibuf, size);
  237. if (section == NULL) {
  238. return;
  239. }
  240. // parse TDT
  241. struct dvb_tdt_section *tdt = dvb_tdt_section_codec(section);
  242. if (tdt == NULL) {
  243. return;
  244. }
  245. // done
  246. zap_ca_new_dvbtime(dvbdate_to_unixtime(tdt->utc_time));
  247. }
  248. static void process_pmt(int pmt_fd, struct zap_dvb_params *params)
  249. {
  250. int size;
  251. uint8_t sibuf[4096];
  252. // read the section
  253. if ((size = read(pmt_fd, sibuf, sizeof(sibuf))) < 0) {
  254. return;
  255. }
  256. // parse section
  257. struct section *section = section_codec(sibuf, size);
  258. if (section == NULL) {
  259. return;
  260. }
  261. // parse section_ext
  262. struct section_ext *section_ext = section_ext_decode(section, 0);
  263. if (section_ext == NULL) {
  264. return;
  265. }
  266. if ((section_ext->table_id_ext != params->channel.service_id) ||
  267. (section_ext->version_number == ca_pmt_version)) {
  268. return;
  269. }
  270. // parse PMT
  271. struct mpeg_pmt_section *pmt = mpeg_pmt_section_codec(section_ext);
  272. if (pmt == NULL) {
  273. return;
  274. }
  275. // do ca handling
  276. if (zap_ca_new_pmt(pmt) == 1)
  277. ca_pmt_version = pmt->head.version_number;
  278. }
  279. static int create_section_filter(int adapter, int demux, uint16_t pid, uint8_t table_id)
  280. {
  281. int demux_fd = -1;
  282. uint8_t filter[18];
  283. uint8_t mask[18];
  284. // open the demuxer
  285. if ((demux_fd = dvbdemux_open_demux(adapter, demux, 0)) < 0) {
  286. return -1;
  287. }
  288. // create a section filter
  289. memset(filter, 0, sizeof(filter));
  290. memset(mask, 0, sizeof(mask));
  291. filter[0] = table_id;
  292. mask[0] = 0xFF;
  293. if (dvbdemux_set_section_filter(demux_fd, pid, filter, mask, 1, 1)) {
  294. close(demux_fd);
  295. return -1;
  296. }
  297. // done
  298. return demux_fd;
  299. }