en50221_app_datetime.c 4.5 KB

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