test-session.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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_session.h>
  22. #include <libdvben50221/en50221_app_utils.h>
  23. #include <libdvbapi/dvbca.h>
  24. #include <pthread.h>
  25. void *stackthread_func(void* arg);
  26. int test_lookup_callback(void *arg, uint8_t slot_id, uint32_t requested_resource_id,
  27. en50221_sl_resource_callback *callback_out, void **arg_out, uint32_t *connected_resource_id);
  28. int test_session_callback(void *arg, int reason, uint8_t slot_id, uint16_t session_number, uint32_t resource_id);
  29. int shutdown_stackthread = 0;
  30. #define DEFAULT_SLOT 0
  31. int main(int argc, char * argv[])
  32. {
  33. (void)argc;
  34. (void)argv;
  35. int i;
  36. pthread_t stackthread;
  37. // create transport layer
  38. struct en50221_transport_layer *tl = en50221_tl_create(5, 32);
  39. if (tl == NULL) {
  40. fprintf(stderr, "Failed to create transport layer\n");
  41. exit(1);
  42. }
  43. // find CAMs
  44. int slot_count = 0;
  45. int cafd= -1;
  46. for(i=0; i<20; i++) {
  47. if ((cafd = dvbca_open(i, 0)) > 0) {
  48. if (dvbca_get_cam_state(cafd, DEFAULT_SLOT) == DVBCA_CAMSTATE_MISSING) {
  49. close(cafd);
  50. continue;
  51. }
  52. // reset it and wait
  53. dvbca_reset(cafd, DEFAULT_SLOT);
  54. printf("Found a CAM on adapter%i... waiting...\n", i);
  55. while(dvbca_get_cam_state(cafd, DEFAULT_SLOT) != DVBCA_CAMSTATE_READY) {
  56. usleep(1000);
  57. }
  58. // register it with the CA stack
  59. int slot_id = 0;
  60. if ((slot_id = en50221_tl_register_slot(tl, cafd, DEFAULT_SLOT, 1000, 100)) < 0) {
  61. fprintf(stderr, "Slot registration failed\n");
  62. exit(1);
  63. }
  64. printf("slotid: %i\n", slot_id);
  65. slot_count++;
  66. }
  67. }
  68. // create session layer
  69. struct en50221_session_layer *sl = en50221_sl_create(tl, 256);
  70. if (sl == NULL) {
  71. fprintf(stderr, "Failed to create session layer\n");
  72. exit(1);
  73. }
  74. // start another thread running the stack
  75. pthread_create(&stackthread, NULL, stackthread_func, tl);
  76. // register callbacks
  77. en50221_sl_register_lookup_callback(sl, test_lookup_callback, sl);
  78. en50221_sl_register_session_callback(sl, test_session_callback, sl);
  79. // create a new connection
  80. for(i=0; i<slot_count; i++) {
  81. int tc = en50221_tl_new_tc(tl, i);
  82. printf("tcid: %i\n", tc);
  83. }
  84. // wait
  85. printf("Press a key to exit\n");
  86. getchar();
  87. // destroy slots
  88. for(i=0; i<slot_count; i++) {
  89. en50221_tl_destroy_slot(tl, i);
  90. }
  91. shutdown_stackthread = 1;
  92. pthread_join(stackthread, NULL);
  93. // destroy session layer
  94. en50221_sl_destroy(sl);
  95. // destroy transport layer
  96. en50221_tl_destroy(tl);
  97. return 0;
  98. }
  99. int test_lookup_callback(void *arg, uint8_t slot_id, uint32_t requested_resource_id,
  100. en50221_sl_resource_callback *callback_out, void **arg_out, uint32_t *connected_resource_id)
  101. {
  102. (void)arg;
  103. (void)callback_out;
  104. (void)arg_out;
  105. (void)connected_resource_id;
  106. struct en50221_app_public_resource_id resid;
  107. if (en50221_app_decode_public_resource_id(&resid, requested_resource_id)) {
  108. printf("Public resource lookup callback %i %i %i %i\n", slot_id,
  109. resid.resource_class, resid.resource_type, resid.resource_version);
  110. } else {
  111. printf("Private resource lookup callback %i %08x\n", slot_id, requested_resource_id);
  112. }
  113. return -1;
  114. }
  115. int test_session_callback(void *arg, int reason, uint8_t slot_id, uint16_t session_number, uint32_t resource_id)
  116. {
  117. (void)arg;
  118. printf("Session callback %i %i %i %04x\n", slot_id, session_number, reason, resource_id);
  119. return -1;
  120. }
  121. void *stackthread_func(void* arg) {
  122. struct en50221_transport_layer *tl = arg;
  123. int lasterror = 0;
  124. while(!shutdown_stackthread) {
  125. int error;
  126. if ((error = en50221_tl_poll(tl)) != 0) {
  127. if (error != lasterror) {
  128. fprintf(stderr, "Error reported by stack slot:%i error:%i\n",
  129. en50221_tl_get_error_slot(tl),
  130. en50221_tl_get_error(tl));
  131. }
  132. lasterror = error;
  133. }
  134. }
  135. shutdown_stackthread = 0;
  136. return 0;
  137. }