en50221_app_ca.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. /*
  2. en50221 encoder An implementation for libdvb
  3. an implementation for the en50221 transport layer
  4. Copyright (C) 2004, 2005 Manu Abraham <abraham.manu@gmail.com>
  5. Copyright (C) 2005 Julian Scheel (julian at jusst dot de)
  6. Copyright (C) 2006 Andrew de Quincey (adq_dvb@lidskialf.net)
  7. This library is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU Lesser General Public License as
  9. published by the Free Software Foundation; either version 2.1 of
  10. the License, or (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU Lesser General Public License for more details.
  15. You should have received a copy of the GNU Lesser General Public
  16. License along with this library; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <string.h>
  20. #include <libdvbmisc/dvbmisc.h>
  21. #include <pthread.h>
  22. #include <libucsi/mpeg/descriptor.h>
  23. #include "en50221_app_ca.h"
  24. #include "asn_1.h"
  25. // tags supported by this resource
  26. #define TAG_CA_INFO_ENQUIRY 0x9f8030
  27. #define TAG_CA_INFO 0x9f8031
  28. #define TAG_CA_PMT 0x9f8032
  29. #define TAG_CA_PMT_REPLY 0x9f8033
  30. struct en50221_app_ca {
  31. struct en50221_app_send_functions *funcs;
  32. en50221_app_ca_info_callback ca_info_callback;
  33. void *ca_info_callback_arg;
  34. en50221_app_ca_pmt_reply_callback ca_pmt_reply_callback;
  35. void *ca_pmt_reply_callback_arg;
  36. pthread_mutex_t lock;
  37. };
  38. struct ca_pmt_descriptor {
  39. uint8_t *descriptor;
  40. uint16_t length;
  41. struct ca_pmt_descriptor *next;
  42. };
  43. struct ca_pmt_stream {
  44. uint8_t stream_type;
  45. uint16_t pid;
  46. struct ca_pmt_descriptor *descriptors;
  47. uint32_t descriptors_length;
  48. uint32_t descriptors_count;
  49. struct ca_pmt_stream *next;
  50. };
  51. static int en50221_ca_extract_pmt_descriptors(struct mpeg_pmt_section *pmt,
  52. struct ca_pmt_descriptor **outdescriptors);
  53. static int en50221_ca_extract_streams(struct mpeg_pmt_section *pmt,
  54. struct ca_pmt_stream **outstreams);
  55. static void en50221_ca_try_move_pmt_descriptors(struct ca_pmt_descriptor **pmt_descriptors,
  56. struct ca_pmt_stream **pmt_streams);
  57. static uint32_t en50221_ca_calculate_length(struct ca_pmt_descriptor *pmt_descriptors,
  58. uint32_t *pmt_descriptors_length,
  59. struct ca_pmt_stream *pmt_streams);
  60. static int en50221_app_ca_parse_info(struct en50221_app_ca *ca,
  61. uint8_t slot_id,
  62. uint16_t session_number,
  63. uint8_t * data, uint32_t data_length);
  64. static int en50221_app_ca_parse_reply(struct en50221_app_ca *ca,
  65. uint8_t slot_id,
  66. uint16_t session_number,
  67. uint8_t * data,
  68. uint32_t data_length);
  69. struct en50221_app_ca *en50221_app_ca_create(struct en50221_app_send_functions *funcs)
  70. {
  71. struct en50221_app_ca *ca = NULL;
  72. // create structure and set it up
  73. ca = malloc(sizeof(struct en50221_app_ca));
  74. if (ca == NULL) {
  75. return NULL;
  76. }
  77. ca->funcs = funcs;
  78. ca->ca_info_callback = NULL;
  79. ca->ca_pmt_reply_callback = NULL;
  80. pthread_mutex_init(&ca->lock, NULL);
  81. // done
  82. return ca;
  83. }
  84. void en50221_app_ca_destroy(struct en50221_app_ca *ca)
  85. {
  86. pthread_mutex_destroy(&ca->lock);
  87. free(ca);
  88. }
  89. void en50221_app_ca_register_info_callback(struct en50221_app_ca *ca,
  90. en50221_app_ca_info_callback
  91. callback, void *arg)
  92. {
  93. pthread_mutex_lock(&ca->lock);
  94. ca->ca_info_callback = callback;
  95. ca->ca_info_callback_arg = arg;
  96. pthread_mutex_unlock(&ca->lock);
  97. }
  98. void en50221_app_ca_register_pmt_reply_callback(struct en50221_app_ca *ca,
  99. en50221_app_ca_pmt_reply_callback
  100. callback, void *arg)
  101. {
  102. pthread_mutex_lock(&ca->lock);
  103. ca->ca_pmt_reply_callback = callback;
  104. ca->ca_pmt_reply_callback_arg = arg;
  105. pthread_mutex_unlock(&ca->lock);
  106. }
  107. int en50221_app_ca_info_enq(struct en50221_app_ca *ca,
  108. uint16_t session_number)
  109. {
  110. uint8_t data[4];
  111. data[0] = (TAG_CA_INFO_ENQUIRY >> 16) & 0xFF;
  112. data[1] = (TAG_CA_INFO_ENQUIRY >> 8) & 0xFF;
  113. data[2] = TAG_CA_INFO_ENQUIRY & 0xFF;
  114. data[3] = 0;
  115. return ca->funcs->send_data(ca->funcs->arg, session_number, data, 4);
  116. }
  117. int en50221_app_ca_pmt(struct en50221_app_ca *ca,
  118. uint16_t session_number,
  119. uint8_t * ca_pmt, uint32_t ca_pmt_length)
  120. {
  121. uint8_t buf[10];
  122. // set up the tag
  123. buf[0] = (TAG_CA_PMT >> 16) & 0xFF;
  124. buf[1] = (TAG_CA_PMT >> 8) & 0xFF;
  125. buf[2] = TAG_CA_PMT & 0xFF;
  126. // encode the length field
  127. int length_field_len;
  128. if ((length_field_len = asn_1_encode(ca_pmt_length, buf + 3, 3)) < 0) {
  129. return -1;
  130. }
  131. // build the iovecs
  132. struct iovec iov[2];
  133. iov[0].iov_base = buf;
  134. iov[0].iov_len = 3 + length_field_len;
  135. iov[1].iov_base = ca_pmt;
  136. iov[1].iov_len = ca_pmt_length;
  137. // create the data and send it
  138. return ca->funcs->send_datav(ca->funcs->arg, session_number, iov, 2);
  139. }
  140. int en50221_app_ca_message(struct en50221_app_ca *ca,
  141. uint8_t slot_id,
  142. uint16_t session_number,
  143. uint32_t resource_id,
  144. uint8_t * data, uint32_t data_length)
  145. {
  146. (void) resource_id;
  147. // get the tag
  148. if (data_length < 3) {
  149. print(LOG_LEVEL, ERROR, 1, "Received short data\n");
  150. return -1;
  151. }
  152. uint32_t tag = (data[0] << 16) | (data[1] << 8) | data[2];
  153. switch (tag) {
  154. case TAG_CA_INFO:
  155. return en50221_app_ca_parse_info(ca, slot_id,
  156. session_number, data + 3,
  157. data_length - 3);
  158. case TAG_CA_PMT_REPLY:
  159. return en50221_app_ca_parse_reply(ca, slot_id,
  160. session_number, data + 3,
  161. data_length - 3);
  162. }
  163. print(LOG_LEVEL, ERROR, 1, "Received unexpected tag %x\n", tag);
  164. return -1;
  165. }
  166. int en50221_ca_format_pmt(struct mpeg_pmt_section *pmt, uint8_t * data,
  167. uint32_t data_length, int move_ca_descriptors,
  168. uint8_t ca_pmt_list_management,
  169. uint8_t ca_pmt_cmd_id)
  170. {
  171. struct ca_pmt_descriptor *pmt_descriptors = NULL;
  172. uint32_t pmt_descriptors_length = 0;
  173. struct ca_pmt_stream *pmt_streams = NULL;
  174. uint32_t total_required_length = 0;
  175. struct ca_pmt_descriptor *cur_d;
  176. struct ca_pmt_stream *cur_s;
  177. int result = -1;
  178. // extract the descriptors and streams
  179. if (en50221_ca_extract_pmt_descriptors(pmt, &pmt_descriptors))
  180. goto cleanup;
  181. if (en50221_ca_extract_streams(pmt, &pmt_streams))
  182. goto cleanup;
  183. // try and merge them if we have no PMT descriptors
  184. if ((pmt_descriptors == NULL) && move_ca_descriptors) {
  185. en50221_ca_try_move_pmt_descriptors(&pmt_descriptors,
  186. &pmt_streams);
  187. }
  188. // calculate the length of all descriptors/streams and the total length required
  189. total_required_length =
  190. en50221_ca_calculate_length(pmt_descriptors,
  191. &pmt_descriptors_length,
  192. pmt_streams);
  193. // ensure we were supplied with enough data
  194. if (total_required_length > data_length) {
  195. goto cleanup;
  196. }
  197. // format the start of the PMT
  198. uint32_t data_pos = 0;
  199. data[data_pos++] = ca_pmt_list_management;
  200. data[data_pos++] = mpeg_pmt_section_program_number(pmt) >> 8;
  201. data[data_pos++] = mpeg_pmt_section_program_number(pmt);
  202. data[data_pos++] =
  203. (pmt->head.version_number << 1) | pmt->head.
  204. current_next_indicator;
  205. data[data_pos++] = (pmt_descriptors_length >> 8) & 0x0f;
  206. data[data_pos++] = pmt_descriptors_length;
  207. // append the PMT descriptors
  208. if (pmt_descriptors_length) {
  209. data[data_pos++] = ca_pmt_cmd_id;
  210. cur_d = pmt_descriptors;
  211. while (cur_d) {
  212. memcpy(data + data_pos, cur_d->descriptor,
  213. cur_d->length);
  214. data_pos += cur_d->length;
  215. cur_d = cur_d->next;
  216. }
  217. }
  218. // now, append the streams
  219. cur_s = pmt_streams;
  220. while (cur_s) {
  221. data[data_pos++] = cur_s->stream_type;
  222. data[data_pos++] = (cur_s->pid >> 8) & 0x1f;
  223. data[data_pos++] = cur_s->pid;
  224. data[data_pos++] = (cur_s->descriptors_length >> 8) & 0x0f;
  225. data[data_pos++] = cur_s->descriptors_length;
  226. // append the stream descriptors
  227. if (cur_s->descriptors_length) {
  228. data[data_pos++] = ca_pmt_cmd_id;
  229. cur_d = cur_s->descriptors;
  230. while (cur_d) {
  231. memcpy(data + data_pos, cur_d->descriptor,
  232. cur_d->length);
  233. data_pos += cur_d->length;
  234. cur_d = cur_d->next;
  235. }
  236. }
  237. cur_s = cur_s->next;
  238. }
  239. result = data_pos;
  240. cleanup:
  241. // free the PMT descriptors
  242. cur_d = pmt_descriptors;
  243. while (cur_d) {
  244. struct ca_pmt_descriptor *next = cur_d->next;
  245. free(cur_d);
  246. cur_d = next;
  247. }
  248. // free the streams
  249. cur_s = pmt_streams;
  250. while (cur_s) {
  251. struct ca_pmt_stream *next_s = cur_s->next;
  252. // free the stream descriptors
  253. cur_d = cur_s->descriptors;
  254. while (cur_d) {
  255. struct ca_pmt_descriptor *next_d = cur_d->next;
  256. free(cur_d);
  257. cur_d = next_d;
  258. }
  259. free(cur_s);
  260. cur_s = next_s;
  261. }
  262. return result;
  263. }
  264. static int en50221_ca_extract_pmt_descriptors(struct mpeg_pmt_section *pmt,
  265. struct ca_pmt_descriptor **outdescriptors)
  266. {
  267. struct ca_pmt_descriptor *descriptors = NULL;
  268. struct ca_pmt_descriptor *descriptors_tail = NULL;
  269. struct ca_pmt_descriptor *cur_d;
  270. struct descriptor *cur_descriptor;
  271. mpeg_pmt_section_descriptors_for_each(pmt, cur_descriptor) {
  272. if (cur_descriptor->tag == dtag_mpeg_ca) {
  273. // create a new structure for this one
  274. struct ca_pmt_descriptor *new_d =
  275. malloc(sizeof(struct ca_pmt_descriptor));
  276. if (new_d == NULL) {
  277. goto error_exit;
  278. }
  279. new_d->descriptor = (uint8_t *) cur_descriptor;
  280. new_d->length = cur_descriptor->len + 2;
  281. new_d->next = NULL;
  282. // append it to the list
  283. if (descriptors == NULL) {
  284. descriptors = new_d;
  285. } else {
  286. descriptors_tail->next = new_d;
  287. }
  288. descriptors_tail = new_d;
  289. }
  290. }
  291. *outdescriptors = descriptors;
  292. return 0;
  293. error_exit:
  294. cur_d = descriptors;
  295. while (cur_d) {
  296. struct ca_pmt_descriptor *next = cur_d->next;
  297. free(cur_d);
  298. cur_d = next;
  299. }
  300. return -1;
  301. }
  302. static int en50221_ca_extract_streams(struct mpeg_pmt_section *pmt,
  303. struct ca_pmt_stream **outstreams)
  304. {
  305. struct ca_pmt_stream *streams = NULL;
  306. struct ca_pmt_stream *streams_tail = NULL;
  307. struct mpeg_pmt_stream *cur_stream;
  308. struct descriptor *cur_descriptor;
  309. struct ca_pmt_stream *cur_s;
  310. mpeg_pmt_section_streams_for_each(pmt, cur_stream) {
  311. struct ca_pmt_descriptor *descriptors_tail = NULL;
  312. // create a new structure
  313. struct ca_pmt_stream *new_s =
  314. malloc(sizeof(struct ca_pmt_stream));
  315. if (new_s == NULL) {
  316. goto exit_cleanup;
  317. }
  318. new_s->stream_type = cur_stream->stream_type;
  319. new_s->pid = cur_stream->pid;
  320. new_s->descriptors = NULL;
  321. new_s->next = NULL;
  322. new_s->descriptors_count = 0;
  323. // append it to the list
  324. if (streams == NULL) {
  325. streams = new_s;
  326. } else {
  327. streams_tail->next = new_s;
  328. }
  329. streams_tail = new_s;
  330. // now process the descriptors
  331. mpeg_pmt_stream_descriptors_for_each(cur_stream,
  332. cur_descriptor) {
  333. if (cur_descriptor->tag == dtag_mpeg_ca) {
  334. // create a new structure
  335. struct ca_pmt_descriptor *new_d =
  336. malloc(sizeof(struct ca_pmt_descriptor));
  337. if (new_d == NULL) {
  338. goto exit_cleanup;
  339. }
  340. new_d->descriptor =
  341. (uint8_t *) cur_descriptor;
  342. new_d->length = cur_descriptor->len + 2;
  343. new_d->next = NULL;
  344. // append it to the list
  345. if (new_s->descriptors == NULL) {
  346. new_s->descriptors = new_d;
  347. } else {
  348. descriptors_tail->next = new_d;
  349. }
  350. descriptors_tail = new_d;
  351. new_s->descriptors_count++;
  352. }
  353. }
  354. }
  355. *outstreams = streams;
  356. return 0;
  357. exit_cleanup:
  358. // free the streams
  359. cur_s = streams;
  360. while (cur_s) {
  361. struct ca_pmt_stream *next_s = cur_s->next;
  362. // free the stream descriptors
  363. struct ca_pmt_descriptor *cur_d = cur_s->descriptors;
  364. while (cur_d) {
  365. struct ca_pmt_descriptor *next_d = cur_d->next;
  366. free(cur_d);
  367. cur_d = next_d;
  368. }
  369. free(cur_s);
  370. cur_s = next_s;
  371. }
  372. return -1;
  373. }
  374. static void en50221_ca_try_move_pmt_descriptors(struct ca_pmt_descriptor **pmt_descriptors,
  375. struct ca_pmt_stream **pmt_streams)
  376. {
  377. // get the first stream
  378. struct ca_pmt_stream *first_stream = *pmt_streams;
  379. if (first_stream == NULL)
  380. return;
  381. // Check that all the other streams with CA descriptors have exactly the same CA descriptors
  382. struct ca_pmt_stream *cur_stream = first_stream->next;
  383. while (cur_stream) {
  384. // if there are differing numbers of descriptors, exit right now
  385. if (cur_stream->descriptors_count != first_stream->descriptors_count)
  386. return;
  387. // now verify the descriptors match
  388. struct ca_pmt_descriptor *cur_descriptor = cur_stream->descriptors;
  389. struct ca_pmt_descriptor *first_cur_descriptor = first_stream->descriptors;
  390. while (cur_descriptor) {
  391. // check the descriptors are the same length
  392. if (cur_descriptor->length != first_cur_descriptor->length)
  393. return;
  394. // check their contents match
  395. if (memcmp(cur_descriptor->descriptor,
  396. first_cur_descriptor->descriptor,
  397. cur_descriptor->length)) {
  398. return;
  399. }
  400. // move to next
  401. cur_descriptor = cur_descriptor->next;
  402. first_cur_descriptor = first_cur_descriptor->next;
  403. }
  404. // move to next
  405. cur_stream = cur_stream->next;
  406. }
  407. // if we end up here, all descriptors in all streams matched
  408. // hook the first stream's descriptors into the PMT's
  409. *pmt_descriptors = first_stream->descriptors;
  410. first_stream->descriptors = NULL;
  411. first_stream->descriptors_count = 0;
  412. // now free up all the descriptors in the other streams
  413. cur_stream = first_stream->next;
  414. while (cur_stream) {
  415. struct ca_pmt_descriptor *cur_descriptor = cur_stream->descriptors;
  416. while (cur_descriptor) {
  417. struct ca_pmt_descriptor *next = cur_descriptor->next;
  418. free(cur_descriptor);
  419. cur_descriptor = next;
  420. }
  421. cur_stream->descriptors = NULL;
  422. cur_stream->descriptors_count = 0;
  423. cur_stream = cur_stream->next;
  424. }
  425. }
  426. static uint32_t en50221_ca_calculate_length(struct ca_pmt_descriptor *pmt_descriptors,
  427. uint32_t *pmt_descriptors_length,
  428. struct ca_pmt_stream *pmt_streams)
  429. {
  430. uint32_t total_required_length = 6; // header
  431. struct ca_pmt_stream *cur_s;
  432. // calcuate the PMT descriptors length
  433. (*pmt_descriptors_length) = 0;
  434. struct ca_pmt_descriptor *cur_d = pmt_descriptors;
  435. while (cur_d) {
  436. (*pmt_descriptors_length) += cur_d->length;
  437. cur_d = cur_d->next;
  438. }
  439. // add on 1 byte for the ca_pmt_cmd_id if we have some descriptors.
  440. if (*pmt_descriptors_length)
  441. (*pmt_descriptors_length)++;
  442. // update the total required length
  443. total_required_length += *pmt_descriptors_length;
  444. // calculate the length of descriptors in the streams
  445. cur_s = pmt_streams;
  446. while (cur_s) {
  447. // calculate the size of descriptors in this stream
  448. cur_s->descriptors_length = 0;
  449. cur_d = cur_s->descriptors;
  450. while (cur_d) {
  451. cur_s->descriptors_length += cur_d->length;
  452. cur_d = cur_d->next;
  453. }
  454. // add on 1 byte for the ca_pmt_cmd_id if we have some descriptors.
  455. if (cur_s->descriptors_length)
  456. cur_s->descriptors_length++;
  457. // update the total required length;
  458. total_required_length += 5 + cur_s->descriptors_length;
  459. cur_s = cur_s->next;
  460. }
  461. // done
  462. return total_required_length;
  463. }
  464. static int en50221_app_ca_parse_info(struct en50221_app_ca *ca,
  465. uint8_t slot_id,
  466. uint16_t session_number,
  467. uint8_t * data, uint32_t data_length)
  468. {
  469. // first of all, decode the length field
  470. uint16_t asn_data_length;
  471. int length_field_len;
  472. if ((length_field_len = asn_1_decode(&asn_data_length, data, data_length)) < 0) {
  473. print(LOG_LEVEL, ERROR, 1, "ASN.1 decode error\n");
  474. return -1;
  475. }
  476. // check it
  477. if (asn_data_length > (data_length - length_field_len)) {
  478. print(LOG_LEVEL, ERROR, 1, "Received short data\n");
  479. return -1;
  480. }
  481. data += length_field_len;
  482. // parse
  483. uint32_t ca_id_count = asn_data_length / 2;
  484. // byteswap the IDs
  485. uint16_t *ids = (uint16_t *) data;
  486. uint32_t i;
  487. for (i = 0; i < ca_id_count; i++) {
  488. bswap16(data);
  489. data += 2;
  490. }
  491. // tell the app
  492. pthread_mutex_lock(&ca->lock);
  493. en50221_app_ca_info_callback cb = ca->ca_info_callback;
  494. void *cb_arg = ca->ca_info_callback_arg;
  495. pthread_mutex_unlock(&ca->lock);
  496. if (cb) {
  497. return cb(cb_arg, slot_id, session_number, ca_id_count,
  498. ids);
  499. }
  500. return 0;
  501. }
  502. static int en50221_app_ca_parse_reply(struct en50221_app_ca *ca,
  503. uint8_t slot_id,
  504. uint16_t session_number,
  505. uint8_t * data, uint32_t data_length)
  506. {
  507. // first of all, decode the length field
  508. uint16_t asn_data_length;
  509. int length_field_len;
  510. if ((length_field_len = asn_1_decode(&asn_data_length, data, data_length)) < 0) {
  511. print(LOG_LEVEL, ERROR, 1, "ASN.1 decode error\n");
  512. return -1;
  513. }
  514. // check it
  515. if (asn_data_length < 4) {
  516. print(LOG_LEVEL, ERROR, 1, "Received short data\n");
  517. return -1;
  518. }
  519. if (asn_data_length > (data_length - length_field_len)) {
  520. print(LOG_LEVEL, ERROR, 1, "Received short data\n");
  521. return -1;
  522. }
  523. data += length_field_len;
  524. data_length -= length_field_len;
  525. // process the reply table to fix endian issues
  526. uint32_t pos = 4;
  527. bswap16(data);
  528. while (pos < asn_data_length) {
  529. bswap16(data + pos);
  530. pos += 3;
  531. }
  532. // tell the app
  533. pthread_mutex_lock(&ca->lock);
  534. en50221_app_ca_pmt_reply_callback cb = ca->ca_pmt_reply_callback;
  535. void *cb_arg = ca->ca_pmt_reply_callback_arg;
  536. pthread_mutex_unlock(&ca->lock);
  537. if (cb) {
  538. return cb(cb_arg, slot_id, session_number,
  539. (struct en50221_app_pmt_reply *) data,
  540. asn_data_length);
  541. }
  542. return 0;
  543. }