en50221_app_teletext.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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_teletext.h"
  23. #include "en50221_app_tags.h"
  24. #include "asn_1.h"
  25. struct en50221_app_teletext {
  26. struct en50221_app_send_functions *funcs;
  27. en50221_app_teletext_callback callback;
  28. void *callback_arg;
  29. pthread_mutex_t lock;
  30. };
  31. static int en50221_app_teletext_parse_ebu(struct en50221_app_teletext *teletext,
  32. uint8_t slot_id,
  33. uint16_t session_number,
  34. uint8_t * data,
  35. uint32_t data_length);
  36. struct en50221_app_teletext *
  37. en50221_app_teletext_create(struct en50221_app_send_functions *funcs)
  38. {
  39. struct en50221_app_teletext *teletext = NULL;
  40. // create structure and set it up
  41. teletext = malloc(sizeof(struct en50221_app_teletext));
  42. if (teletext == NULL) {
  43. return NULL;
  44. }
  45. teletext->funcs = funcs;
  46. teletext->callback = NULL;
  47. pthread_mutex_init(&teletext->lock, NULL);
  48. // done
  49. return teletext;
  50. }
  51. void en50221_app_teletext_destroy(struct en50221_app_teletext *teletext)
  52. {
  53. pthread_mutex_destroy(&teletext->lock);
  54. free(teletext);
  55. }
  56. void en50221_app_teletext_register_callback(struct en50221_app_teletext *teletext,
  57. en50221_app_teletext_callback callback, void *arg)
  58. {
  59. pthread_mutex_lock(&teletext->lock);
  60. teletext->callback = callback;
  61. teletext->callback_arg = arg;
  62. pthread_mutex_unlock(&teletext->lock);
  63. }
  64. int en50221_app_teletext_message(struct en50221_app_teletext *teletext,
  65. uint8_t slot_id,
  66. uint16_t session_number,
  67. uint32_t resource_id,
  68. uint8_t * data, uint32_t data_length)
  69. {
  70. (void) resource_id;
  71. // get the tag
  72. if (data_length < 3) {
  73. print(LOG_LEVEL, ERROR, 1, "Received short data\n");
  74. return -1;
  75. }
  76. uint32_t tag = (data[0] << 16) | (data[1] << 8) | data[2];
  77. switch (tag) {
  78. case TAG_TELETEXT_EBU:
  79. return en50221_app_teletext_parse_ebu(teletext, slot_id,
  80. session_number,
  81. data + 3,
  82. data_length - 3);
  83. }
  84. print(LOG_LEVEL, ERROR, 1, "Received unexpected tag %x\n", tag);
  85. return -1;
  86. }
  87. static int en50221_app_teletext_parse_ebu(struct en50221_app_teletext *teletext,
  88. uint8_t slot_id,
  89. uint16_t session_number,
  90. uint8_t *data,
  91. uint32_t data_length)
  92. {
  93. // first of all, decode the length field
  94. uint16_t asn_data_length;
  95. int length_field_len;
  96. if ((length_field_len = asn_1_decode(&asn_data_length, data, data_length)) < 0) {
  97. print(LOG_LEVEL, ERROR, 1, "ASN.1 decode error\n");
  98. return -1;
  99. }
  100. // check it
  101. if (asn_data_length > (data_length - length_field_len)) {
  102. print(LOG_LEVEL, ERROR, 1, "Received short data\n");
  103. return -1;
  104. }
  105. uint8_t *teletext_data = data + length_field_len;
  106. // tell the app
  107. pthread_mutex_lock(&teletext->lock);
  108. en50221_app_teletext_callback cb = teletext->callback;
  109. void *cb_arg = teletext->callback_arg;
  110. pthread_mutex_unlock(&teletext->lock);
  111. if (cb) {
  112. return cb(cb_arg, slot_id, session_number, teletext_data,
  113. asn_data_length);
  114. }
  115. return 0;
  116. }