dst_test.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. DST-TEST utility
  3. an implementation for the High Level Common Interface
  4. Copyright (C) 2004, 2005 Manu Abraham <abraham.manu@gmail.com>
  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 <string.h>
  20. #include <unistd.h>
  21. #include <sys/types.h>
  22. #include <sys/stat.h>
  23. #include <fcntl.h>
  24. #include <sys/ioctl.h>
  25. #include <errno.h>
  26. #include <stdint.h>
  27. #include <linux/dvb/dmx.h>
  28. #include <linux/dvb/ca.h>
  29. #include <libdvben50221/en50221_app_tags.h>
  30. #define CA_NODE "/dev/dvb/adapter0/ca0"
  31. #ifndef CA_SET_PID
  32. typedef struct ca_pid {
  33. unsigned int pid;
  34. int index;
  35. } ca_pid_t;
  36. #define CA_SET_PID 42424242
  37. #endif
  38. static int dst_comms(int cafd, uint32_t tag, uint32_t function, struct ca_msg *msg)
  39. {
  40. if (tag) {
  41. msg->msg[2] = tag & 0xff;
  42. msg->msg[1] = (tag & 0xff00) >> 8;
  43. msg->msg[0] = (tag & 0xff0000) >> 16;
  44. printf("%s: Msg=[%02x %02x %02x ]\n",__FUNCTION__, msg->msg[0], msg->msg[1], msg->msg[2]);
  45. }
  46. if ((ioctl(cafd, function, msg)) < 0) {
  47. printf("%s: ioctl failed ..\n", __FUNCTION__);
  48. return -1;
  49. }
  50. return 0;
  51. }
  52. static int dst_get_caps(int cafd, struct ca_caps *caps)
  53. {
  54. if ((ioctl(cafd, CA_GET_CAP, caps)) < 0) {
  55. printf("%s: ioctl failed ..\n", __FUNCTION__);
  56. return -1;
  57. }
  58. if (caps->slot_num < 1) {
  59. printf ("No CI Slots found\n");
  60. return -1;
  61. }
  62. printf("APP: Slots=[%d]\n", caps->slot_num);
  63. printf("APP: Type=[%d]\n", caps->slot_type);
  64. printf("APP: Descrambler keys=[%d]\n", caps->descr_num);
  65. printf("APP: Type=[%d]\n", caps->descr_type);
  66. return 0;
  67. }
  68. static int dst_get_info(int cafd, struct ca_slot_info *info)
  69. {
  70. if ((ioctl(cafd, CA_GET_SLOT_INFO, info)) < 0) {
  71. printf("%s: ioctl failed ..\n", __FUNCTION__);
  72. return -1;
  73. }
  74. if (info->num < 1) {
  75. printf("No CI Slots found\n");
  76. return -1;
  77. }
  78. printf("APP: Number=[%d]\n", info->num);
  79. printf("APP: Type=[%d]\n", info->type);
  80. printf("APP: flags=[%d]\n", info->flags);
  81. if (info->flags == 1)
  82. printf("APP: CI High level interface\n");
  83. if (info->flags == 1)
  84. printf("APP: CA/CI Module Present\n");
  85. else if (info->flags == 2)
  86. printf("APP: CA/CI Module Ready\n");
  87. else if (info->flags == 0)
  88. printf("APP: No CA/CI Module\n");
  89. return 0;
  90. }
  91. static int dst_reset(int cafd)
  92. {
  93. if ((ioctl(cafd, CA_RESET)) < 0) {
  94. printf("%s: ioctl failed ..\n", __FUNCTION__);
  95. return -1;
  96. }
  97. return 0;
  98. }
  99. static int dst_set_pid(int cafd)
  100. {
  101. if ((ioctl(cafd, CA_SET_PID)) < 0) {
  102. printf("%s: ioctl failed ..\n", __FUNCTION__);
  103. return -1;
  104. }
  105. return 0;
  106. }
  107. static int dst_get_descr(int cafd)
  108. {
  109. if ((ioctl(cafd, CA_GET_DESCR_INFO)) < 0) {
  110. printf("%s: ioctl failed ..\n", __FUNCTION__);
  111. return -1;
  112. }
  113. return 0;
  114. }
  115. static int dst_set_descr(int cafd)
  116. {
  117. if ((ioctl(cafd, CA_SET_DESCR)) < 0) {
  118. printf("%s: ioctl failed ..\n", __FUNCTION__);
  119. return -1;
  120. }
  121. return 0;
  122. }
  123. static int dst_get_app_info(int cafd, struct ca_msg *msg)
  124. {
  125. uint32_t tag = 0;
  126. /* Enquire */
  127. tag = TAG_CA_INFO_ENQUIRY;
  128. if ((dst_comms(cafd, tag, CA_SEND_MSG, msg)) < 0) {
  129. printf("%s: Dst communication failed\n", __FUNCTION__);
  130. return -1;
  131. }
  132. /* Receive */
  133. tag = TAG_CA_INFO;
  134. if ((dst_comms(cafd, tag, CA_GET_MSG, msg)) < 0) {
  135. printf("%s: Dst communication failed\n", __FUNCTION__);
  136. return -1;
  137. }
  138. /* Process */
  139. printf("%s: ================================ CI Module Application Info ======================================\n", __FUNCTION__);
  140. printf("%s: Application Type=[%d], Application Vendor=[%d], Vendor Code=[%d]\n%s: Application info=[%s]\n",
  141. __FUNCTION__, msg->msg[7], (msg->msg[8] << 8) | msg->msg[9], (msg->msg[10] << 8) | msg->msg[11], __FUNCTION__,
  142. ((char *) (&msg->msg[12])));
  143. printf("%s: ==================================================================================================\n", __FUNCTION__);
  144. return 0;
  145. }
  146. static int dst_session_test(int cafd, struct ca_msg *msg)
  147. {
  148. msg->msg[0] = 0x91;
  149. printf("Debugging open session request\n");
  150. if ((ioctl(cafd, CA_SEND_MSG, msg)) < 0) {
  151. printf("%s: ioctl failed ..\n", __FUNCTION__);
  152. return -1;
  153. }
  154. return 0;
  155. }
  156. int main(int argc, char *argv[])
  157. {
  158. int cafd;
  159. const char *usage = " DST-TEST: Twinhan DST and clones test utility\n"
  160. " an implementation for the High Level Common Interface\n"
  161. " Copyright (C) 2004, 2005 Manu Abraham (manu@kromtek.com)\n\n"
  162. "\t dst_test options:\n"
  163. "\t -c capabilities\n"
  164. "\t -i info\n"
  165. "\t -r reset\n"
  166. "\t -p pid\n"
  167. "\t -g get descr\n"
  168. "\t -s set_descr\n"
  169. "\t -a app_info\n"
  170. "\t -t session test\n";
  171. struct ca_caps *caps;
  172. caps = (struct ca_caps *) malloc(sizeof (struct ca_caps));
  173. struct ca_slot_info *info;
  174. info = (struct ca_slot_info *)malloc (sizeof (struct ca_slot_info));
  175. struct ca_msg *msg;
  176. msg = (struct ca_msg *) malloc(sizeof (struct ca_msg));
  177. if (argc < 2)
  178. printf("%s\n", usage);
  179. if (argc > 1) {
  180. if ((cafd = open(CA_NODE, O_RDONLY)) < 0) {
  181. printf("%s: Error opening %s\n", __FUNCTION__, CA_NODE);
  182. return -1;
  183. }
  184. switch (getopt(argc, argv, "cirpgsat")) {
  185. case 'c':
  186. printf("%s: Capabilities\n", __FUNCTION__);
  187. dst_get_caps(cafd, caps);
  188. break;
  189. case 'i':
  190. printf("%s: Info\n", __FUNCTION__);
  191. dst_get_info(cafd, info);
  192. break;
  193. case 'r':
  194. printf("%s: Reset\n", __FUNCTION__);
  195. dst_reset(cafd);
  196. break;
  197. case 'p':
  198. printf("%s: PID\n", __FUNCTION__);
  199. dst_set_pid(cafd);
  200. break;
  201. case 'g':
  202. printf("%s: Get Desc\n", __FUNCTION__);
  203. dst_get_descr(cafd);
  204. break;
  205. case 's':
  206. printf("%s: Set Desc\n", __FUNCTION__);
  207. dst_set_descr(cafd);
  208. break;
  209. case 'a':
  210. printf("%s: App Info\n", __FUNCTION__);
  211. dst_get_app_info(cafd, msg);
  212. break;
  213. case 't':
  214. printf("%s: Session test\n", __FUNCTION__);
  215. dst_session_test(cafd, msg);
  216. break;
  217. break;
  218. }
  219. close(cafd);
  220. }
  221. return 0;
  222. }