zap_ca.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. ZAP utility CA 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 <stdlib.h>
  19. #include <unistd.h>
  20. #include <sys/poll.h>
  21. #include <pthread.h>
  22. #include <libdvben50221/en50221_stdcam.h>
  23. #include "zap_ca.h"
  24. static int zap_ca_info_callback(void *arg, uint8_t slot_id, uint16_t session_number, uint32_t ca_id_count, uint16_t *ca_ids);
  25. static int zap_ai_callback(void *arg, uint8_t slot_id, uint16_t session_number,
  26. uint8_t application_type, uint16_t application_manufacturer,
  27. uint16_t manufacturer_code, uint8_t menu_string_length,
  28. uint8_t *menu_string);
  29. static void *camthread_func(void* arg);
  30. static struct en50221_transport_layer *tl = NULL;
  31. static struct en50221_session_layer *sl = NULL;
  32. static struct en50221_stdcam *stdcam = NULL;
  33. static int ca_resource_connected = 0;
  34. static int camthread_shutdown = 0;
  35. static pthread_t camthread;
  36. static int seenpmt = 0;
  37. static int moveca = 0;
  38. void zap_ca_start(struct zap_ca_params *params)
  39. {
  40. // create transport layer
  41. tl = en50221_tl_create(1, 16);
  42. if (tl == NULL) {
  43. fprintf(stderr, "Failed to create transport layer\n");
  44. return;
  45. }
  46. // create session layer
  47. sl = en50221_sl_create(tl, 16);
  48. if (sl == NULL) {
  49. fprintf(stderr, "Failed to create session layer\n");
  50. en50221_tl_destroy(tl);
  51. return;
  52. }
  53. // create the stdcam instance
  54. stdcam = en50221_stdcam_create(params->adapter_id, params->caslot_num, tl, sl);
  55. if (stdcam == NULL) {
  56. en50221_sl_destroy(sl);
  57. en50221_tl_destroy(tl);
  58. return;
  59. }
  60. // hook up the AI callbacks
  61. if (stdcam->ai_resource) {
  62. en50221_app_ai_register_callback(stdcam->ai_resource, zap_ai_callback, stdcam);
  63. }
  64. // hook up the CA callbacks
  65. if (stdcam->ca_resource) {
  66. en50221_app_ca_register_info_callback(stdcam->ca_resource, zap_ca_info_callback, stdcam);
  67. }
  68. // any other stuff
  69. moveca = params->moveca;
  70. // start the cam thread
  71. pthread_create(&camthread, NULL, camthread_func, NULL);
  72. }
  73. void zap_ca_stop(void)
  74. {
  75. if (stdcam == NULL)
  76. return;
  77. // shutdown the cam thread
  78. camthread_shutdown = 1;
  79. pthread_join(camthread, NULL);
  80. // destroy session layer
  81. en50221_sl_destroy(sl);
  82. // destroy transport layer
  83. en50221_tl_destroy(tl);
  84. // destroy the stdcam
  85. if (stdcam->destroy)
  86. stdcam->destroy(stdcam, 1);
  87. }
  88. int zap_ca_new_pmt(struct mpeg_pmt_section *pmt)
  89. {
  90. uint8_t capmt[4096];
  91. int size;
  92. if (stdcam == NULL)
  93. return -1;
  94. if (ca_resource_connected) {
  95. fprintf(stderr, "Received new PMT - sending to CAM...\n");
  96. // translate it into a CA PMT
  97. int listmgmt = CA_LIST_MANAGEMENT_ONLY;
  98. if (seenpmt) {
  99. listmgmt = CA_LIST_MANAGEMENT_UPDATE;
  100. }
  101. seenpmt = 1;
  102. if ((size = en50221_ca_format_pmt(pmt, capmt, sizeof(capmt), moveca, listmgmt,
  103. CA_PMT_CMD_ID_OK_DESCRAMBLING)) < 0) {
  104. fprintf(stderr, "Failed to format PMT\n");
  105. return -1;
  106. }
  107. // set it
  108. if (en50221_app_ca_pmt(stdcam->ca_resource, stdcam->ca_session_number, capmt, size)) {
  109. fprintf(stderr, "Failed to send PMT\n");
  110. return -1;
  111. }
  112. // we've seen this PMT
  113. return 1;
  114. }
  115. return 0;
  116. }
  117. void zap_ca_new_dvbtime(time_t dvb_time)
  118. {
  119. if (stdcam == NULL)
  120. return;
  121. if (stdcam->dvbtime)
  122. stdcam->dvbtime(stdcam, dvb_time);
  123. }
  124. static void *camthread_func(void* arg)
  125. {
  126. (void) arg;
  127. while(!camthread_shutdown) {
  128. stdcam->poll(stdcam);
  129. }
  130. return 0;
  131. }
  132. static int zap_ai_callback(void *arg, uint8_t slot_id, uint16_t session_number,
  133. uint8_t application_type, uint16_t application_manufacturer,
  134. uint16_t manufacturer_code, uint8_t menu_string_length,
  135. uint8_t *menu_string)
  136. {
  137. (void) arg;
  138. (void) slot_id;
  139. (void) session_number;
  140. printf("CAM Application type: %02x\n", application_type);
  141. printf("CAM Application manufacturer: %04x\n", application_manufacturer);
  142. printf("CAM Manufacturer code: %04x\n", manufacturer_code);
  143. printf("CAM Menu string: %.*s\n", menu_string_length, menu_string);
  144. return 0;
  145. }
  146. static int zap_ca_info_callback(void *arg, uint8_t slot_id, uint16_t session_number, uint32_t ca_id_count, uint16_t *ca_ids)
  147. {
  148. (void) arg;
  149. (void) slot_id;
  150. (void) session_number;
  151. printf("CAM supports the following ca system ids:\n");
  152. uint32_t i;
  153. for(i=0; i< ca_id_count; i++) {
  154. printf(" 0x%04x\n", ca_ids[i]);
  155. }
  156. ca_resource_connected = 1;
  157. return 0;
  158. }