test-transport.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. en50221 encoder An implementation for libdvb
  3. an implementation for the en50221 transport layer
  4. Copyright (C) 2004, 2005 Manu Abraham (manu@kromtek.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 <stdio.h>
  20. #include <unistd.h>
  21. #include <libdvben50221/en50221_transport.h>
  22. #include <libdvbapi/dvbca.h>
  23. #include <pthread.h>
  24. void *stackthread_func(void* arg);
  25. void test_callback(void *arg, int reason,
  26. uint8_t *data, uint32_t data_length,
  27. uint8_t slot_id, uint8_t connection_id);
  28. int shutdown_stackthread = 0;
  29. #define DEFAULT_SLOT 0
  30. int main(int argc, char * argv[])
  31. {
  32. (void)argc;
  33. (void)argv;
  34. int i;
  35. pthread_t stackthread;
  36. // create transport layer
  37. struct en50221_transport_layer *tl = en50221_tl_create(5, 32);
  38. if (tl == NULL) {
  39. fprintf(stderr, "Failed to create transport layer\n");
  40. exit(1);
  41. }
  42. // find CAMs
  43. int slot_count = 0;
  44. int cafd= -1;
  45. for(i=0; i<20; i++) {
  46. if ((cafd = dvbca_open(i, 0)) > 0) {
  47. if (dvbca_get_cam_state(cafd, DEFAULT_SLOT) == DVBCA_CAMSTATE_MISSING) {
  48. close(cafd);
  49. continue;
  50. }
  51. // reset it and wait
  52. dvbca_reset(cafd, DEFAULT_SLOT);
  53. printf("Found a CAM on adapter%i... waiting...\n", i);
  54. while(dvbca_get_cam_state(cafd, DEFAULT_SLOT) != DVBCA_CAMSTATE_READY) {
  55. usleep(1000);
  56. }
  57. // register it with the CA stack
  58. int slot_id = 0;
  59. if ((slot_id = en50221_tl_register_slot(tl, cafd, DEFAULT_SLOT, 1000, 100)) < 0) {
  60. fprintf(stderr, "Slot registration failed\n");
  61. exit(1);
  62. }
  63. printf("slotid: %i\n", slot_id);
  64. slot_count++;
  65. }
  66. }
  67. // start another thread to running the stack
  68. pthread_create(&stackthread, NULL, stackthread_func, tl);
  69. // register callback
  70. en50221_tl_register_callback(tl, test_callback, tl);
  71. // create a new connection
  72. for(i=0; i<slot_count; i++) {
  73. int tc = en50221_tl_new_tc(tl, i);
  74. printf("tcid: %i\n", tc);
  75. }
  76. // wait
  77. printf("Press a key to exit\n");
  78. getchar();
  79. // destroy slots
  80. for(i=0; i<slot_count; i++) {
  81. en50221_tl_destroy_slot(tl, i);
  82. }
  83. shutdown_stackthread = 1;
  84. pthread_join(stackthread, NULL);
  85. // destroy transport layer
  86. en50221_tl_destroy(tl);
  87. return 0;
  88. }
  89. void test_callback(void *arg, int reason,
  90. uint8_t *data, uint32_t data_length,
  91. uint8_t slot_id, uint8_t connection_id)
  92. {
  93. (void) arg;
  94. printf("-----------------------------------\n");
  95. printf("CALLBACK SLOTID:%i %i %i\n", slot_id, connection_id, reason);
  96. uint32_t i;
  97. for(i=0; i< data_length; i++) {
  98. printf("%02x %02x\n", i, data[i]);
  99. }
  100. }
  101. void *stackthread_func(void* arg) {
  102. struct en50221_transport_layer *tl = arg;
  103. int lasterror = 0;
  104. while(!shutdown_stackthread) {
  105. int error;
  106. if ((error = en50221_tl_poll(tl)) != 0) {
  107. if (error != lasterror) {
  108. fprintf(stderr, "Error reported by stack slot:%i error:%i\n",
  109. en50221_tl_get_error_slot(tl),
  110. en50221_tl_get_error(tl));
  111. }
  112. lasterror = error;
  113. }
  114. }
  115. shutdown_stackthread = 0;
  116. return 0;
  117. }