en50221_app_ai.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 "en50221_app_ai.h"
  23. #include "en50221_app_tags.h"
  24. #include "asn_1.h"
  25. struct en50221_app_ai {
  26. struct en50221_app_send_functions *funcs;
  27. en50221_app_ai_callback callback;
  28. void *callback_arg;
  29. pthread_mutex_t lock;
  30. };
  31. static int en50221_app_ai_parse_app_info(struct en50221_app_ai *ai,
  32. uint8_t slot_id,
  33. uint16_t session_number,
  34. uint8_t * data,
  35. uint32_t data_length);
  36. struct en50221_app_ai *en50221_app_ai_create(struct en50221_app_send_functions *funcs)
  37. {
  38. struct en50221_app_ai *ai = NULL;
  39. // create structure and set it up
  40. ai = malloc(sizeof(struct en50221_app_ai));
  41. if (ai == NULL) {
  42. return NULL;
  43. }
  44. ai->funcs = funcs;
  45. ai->callback = NULL;
  46. pthread_mutex_init(&ai->lock, NULL);
  47. // done
  48. return ai;
  49. }
  50. void en50221_app_ai_destroy(struct en50221_app_ai *ai)
  51. {
  52. pthread_mutex_destroy(&ai->lock);
  53. free(ai);
  54. }
  55. void en50221_app_ai_register_callback(struct en50221_app_ai *ai,
  56. en50221_app_ai_callback callback,
  57. void *arg)
  58. {
  59. pthread_mutex_lock(&ai->lock);
  60. ai->callback = callback;
  61. ai->callback_arg = arg;
  62. pthread_mutex_unlock(&ai->lock);
  63. }
  64. int en50221_app_ai_enquiry(struct en50221_app_ai *ai,
  65. uint16_t session_number)
  66. {
  67. uint8_t data[4];
  68. data[0] = (TAG_APP_INFO_ENQUIRY >> 16) & 0xFF;
  69. data[1] = (TAG_APP_INFO_ENQUIRY >> 8) & 0xFF;
  70. data[2] = TAG_APP_INFO_ENQUIRY & 0xFF;
  71. data[3] = 0;
  72. return ai->funcs->send_data(ai->funcs->arg, session_number, data, 4);
  73. }
  74. int en50221_app_ai_entermenu(struct en50221_app_ai *ai,
  75. uint16_t session_number)
  76. {
  77. uint8_t data[4];
  78. data[0] = (TAG_ENTER_MENU >> 16) & 0xFF;
  79. data[1] = (TAG_ENTER_MENU >> 8) & 0xFF;
  80. data[2] = TAG_ENTER_MENU & 0xFF;
  81. data[3] = 0;
  82. return ai->funcs->send_data(ai->funcs->arg, session_number, data, 4);
  83. }
  84. int en50221_app_ai_message(struct en50221_app_ai *ai,
  85. uint8_t slot_id,
  86. uint16_t session_number,
  87. uint32_t resource_id,
  88. uint8_t * data, uint32_t data_length)
  89. {
  90. (void) resource_id;
  91. // get the tag
  92. if (data_length < 3) {
  93. print(LOG_LEVEL, ERROR, 1, "Received short data\n");
  94. return -1;
  95. }
  96. uint32_t tag = (data[0] << 16) | (data[1] << 8) | data[2];
  97. switch (tag) {
  98. case TAG_APP_INFO:
  99. return en50221_app_ai_parse_app_info(ai, slot_id,
  100. session_number,
  101. data + 3,
  102. data_length - 3);
  103. }
  104. print(LOG_LEVEL, ERROR, 1, "Received unexpected tag %x\n", tag);
  105. return -1;
  106. }
  107. static int en50221_app_ai_parse_app_info(struct en50221_app_ai *ai,
  108. uint8_t slot_id,
  109. uint16_t session_number,
  110. uint8_t * data,
  111. uint32_t data_length)
  112. {
  113. // parse the length field
  114. int length_field_len;
  115. uint16_t asn_data_length;
  116. if ((length_field_len = asn_1_decode(&asn_data_length, data, data_length)) < 0) {
  117. print(LOG_LEVEL, ERROR, 1,
  118. "Received data with invalid length from module on slot %02x\n",
  119. slot_id);
  120. return -1;
  121. }
  122. // check it
  123. if (asn_data_length < 6) {
  124. print(LOG_LEVEL, ERROR, 1, "Received short data\n");
  125. return -1;
  126. }
  127. if (asn_data_length > (data_length - length_field_len)) {
  128. print(LOG_LEVEL, ERROR, 1, "Received short data\n");
  129. return -1;
  130. }
  131. uint8_t *app_info = data + length_field_len;
  132. // parse the fields
  133. uint8_t application_type = app_info[0];
  134. uint16_t application_manufacturer = (app_info[1] << 8) | app_info[2];
  135. uint16_t manufacturer_code = (app_info[3] << 8) | app_info[4];
  136. uint8_t menu_string_length = app_info[5];
  137. uint8_t *menu_string = app_info + 6;
  138. // check the menu_string_length
  139. if (menu_string_length > (asn_data_length - 6)) {
  140. print(LOG_LEVEL, ERROR, 1,
  141. "Received bad menu string length - adjusting\n");
  142. menu_string_length = asn_data_length - 6;
  143. }
  144. // tell the app
  145. pthread_mutex_lock(&ai->lock);
  146. en50221_app_ai_callback cb = ai->callback;
  147. void *cb_arg = ai->callback_arg;
  148. pthread_mutex_unlock(&ai->lock);
  149. if (cb) {
  150. return cb(cb_arg, slot_id, session_number,
  151. application_type, application_manufacturer,
  152. manufacturer_code, menu_string_length,
  153. menu_string);
  154. }
  155. return 0;
  156. }