en50221_app_epg.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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/dvb/types.h>
  23. #include "en50221_app_epg.h"
  24. #include "en50221_app_tags.h"
  25. #include "asn_1.h"
  26. struct en50221_app_epg {
  27. struct en50221_app_send_functions *funcs;
  28. en50221_app_epg_reply_callback callback;
  29. void *callback_arg;
  30. pthread_mutex_t lock;
  31. };
  32. static int en50221_app_epg_parse_reply(struct en50221_app_epg *private,
  33. uint8_t slot_id,
  34. uint16_t session_number,
  35. uint8_t * data,
  36. uint32_t data_length);
  37. struct en50221_app_epg *en50221_app_epg_create(struct en50221_app_send_functions *funcs)
  38. {
  39. struct en50221_app_epg *epg = NULL;
  40. // create structure and set it up
  41. epg = malloc(sizeof(struct en50221_app_epg));
  42. if (epg == NULL) {
  43. return NULL;
  44. }
  45. epg->funcs = funcs;
  46. epg->callback = NULL;
  47. pthread_mutex_init(&epg->lock, NULL);
  48. // done
  49. return epg;
  50. }
  51. void en50221_app_epg_destroy(struct en50221_app_epg *epg)
  52. {
  53. pthread_mutex_destroy(&epg->lock);
  54. free(epg);
  55. }
  56. void en50221_app_epg_register_enquiry_callback(struct en50221_app_epg *epg,
  57. en50221_app_epg_reply_callback callback,
  58. void *arg)
  59. {
  60. pthread_mutex_lock(&epg->lock);
  61. epg->callback = callback;
  62. epg->callback_arg = arg;
  63. pthread_mutex_unlock(&epg->lock);
  64. }
  65. int en50221_app_epg_enquire(struct en50221_app_epg *epg,
  66. uint16_t session_number,
  67. uint8_t command_id,
  68. uint16_t network_id,
  69. uint16_t original_network_id,
  70. uint16_t transport_stream_id,
  71. uint16_t service_id, uint16_t event_id)
  72. {
  73. uint8_t data[15];
  74. data[0] = (TAG_EPG_ENQUIRY >> 16) & 0xFF;
  75. data[1] = (TAG_EPG_ENQUIRY >> 8) & 0xFF;
  76. data[2] = TAG_EPG_ENQUIRY & 0xFF;
  77. data[3] = 11;
  78. data[4] = command_id;
  79. data[5] = network_id >> 8;
  80. data[6] = network_id;
  81. data[7] = original_network_id >> 8;
  82. data[8] = original_network_id;
  83. data[9] = transport_stream_id >> 8;
  84. data[10] = transport_stream_id;
  85. data[11] = service_id >> 8;
  86. data[12] = service_id;
  87. data[13] = event_id >> 8;
  88. data[14] = event_id;
  89. return epg->funcs->send_data(epg->funcs->arg, session_number, data, 15);
  90. }
  91. int en50221_app_epg_message(struct en50221_app_epg *epg,
  92. uint8_t slot_id,
  93. uint16_t session_number,
  94. uint32_t resource_id,
  95. uint8_t * data, uint32_t data_length)
  96. {
  97. struct en50221_app_epg *private = (struct en50221_app_epg *) epg;
  98. (void) resource_id;
  99. // get the tag
  100. if (data_length < 3) {
  101. print(LOG_LEVEL, ERROR, 1, "Received short data\n");
  102. return -1;
  103. }
  104. uint32_t tag = (data[0] << 16) | (data[1] << 8) | data[2];
  105. switch (tag) {
  106. case TAG_EPG_REPLY:
  107. return en50221_app_epg_parse_reply(private, slot_id,
  108. session_number,
  109. data + 3,
  110. data_length - 3);
  111. }
  112. print(LOG_LEVEL, ERROR, 1, "Received unexpected tag %x\n", tag);
  113. return -1;
  114. }
  115. static int en50221_app_epg_parse_reply(struct en50221_app_epg *epg,
  116. uint8_t slot_id,
  117. uint16_t session_number,
  118. uint8_t * data,
  119. uint32_t data_length)
  120. {
  121. // validate data
  122. if (data_length != 2) {
  123. print(LOG_LEVEL, ERROR, 1, "Received short data\n");
  124. return -1;
  125. }
  126. if (data[0] != 1) {
  127. print(LOG_LEVEL, ERROR, 1, "Received short data\n");
  128. return -1;
  129. }
  130. uint8_t event_status = data[1];
  131. // tell the app
  132. pthread_mutex_lock(&epg->lock);
  133. en50221_app_epg_reply_callback cb = epg->callback;
  134. void *cb_arg = epg->callback_arg;
  135. pthread_mutex_unlock(&epg->lock);
  136. if (cb) {
  137. return cb(cb_arg, slot_id, session_number, event_status);
  138. }
  139. return 0;
  140. }