en50221_app_rm.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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/endianops.h>
  23. #include "en50221_app_rm.h"
  24. #include "en50221_app_tags.h"
  25. #include "asn_1.h"
  26. struct en50221_app_rm {
  27. struct en50221_app_send_functions *funcs;
  28. en50221_app_rm_enq_callback enqcallback;
  29. void *enqcallback_arg;
  30. en50221_app_rm_reply_callback replycallback;
  31. void *replycallback_arg;
  32. en50221_app_rm_changed_callback changedcallback;
  33. void *changedcallback_arg;
  34. pthread_mutex_t lock;
  35. };
  36. static int en50221_app_rm_parse_profile_enq(struct en50221_app_rm *rm,
  37. uint8_t slot_id,
  38. uint16_t session_number,
  39. uint8_t * data,
  40. uint32_t data_length);
  41. static int en50221_app_rm_parse_profile_reply(struct en50221_app_rm *rm,
  42. uint8_t slot_id,
  43. uint16_t session_number,
  44. uint8_t * data,
  45. uint32_t data_length);
  46. static int en50221_app_rm_parse_profile_change(struct en50221_app_rm *rm,
  47. uint8_t slot_id,
  48. uint16_t session_number,
  49. uint8_t * data,
  50. uint32_t data_length);
  51. struct en50221_app_rm *en50221_app_rm_create(struct
  52. en50221_app_send_functions
  53. *funcs)
  54. {
  55. struct en50221_app_rm *rm = NULL;
  56. // create structure and set it up
  57. rm = malloc(sizeof(struct en50221_app_rm));
  58. if (rm == NULL) {
  59. return NULL;
  60. }
  61. rm->funcs = funcs;
  62. rm->enqcallback = NULL;
  63. rm->replycallback = NULL;
  64. rm->changedcallback = NULL;
  65. pthread_mutex_init(&rm->lock, NULL);
  66. // done
  67. return rm;
  68. }
  69. void en50221_app_rm_destroy(struct en50221_app_rm *rm)
  70. {
  71. pthread_mutex_destroy(&rm->lock);
  72. free(rm);
  73. }
  74. void en50221_app_rm_register_enq_callback(struct en50221_app_rm *rm,
  75. en50221_app_rm_enq_callback
  76. callback, void *arg)
  77. {
  78. pthread_mutex_lock(&rm->lock);
  79. rm->enqcallback = callback;
  80. rm->enqcallback_arg = arg;
  81. pthread_mutex_unlock(&rm->lock);
  82. }
  83. void en50221_app_rm_register_reply_callback(struct en50221_app_rm *rm,
  84. en50221_app_rm_reply_callback
  85. callback, void *arg)
  86. {
  87. pthread_mutex_lock(&rm->lock);
  88. rm->replycallback = callback;
  89. rm->replycallback_arg = arg;
  90. pthread_mutex_unlock(&rm->lock);
  91. }
  92. void en50221_app_rm_register_changed_callback(struct en50221_app_rm *rm,
  93. en50221_app_rm_changed_callback
  94. callback, void *arg)
  95. {
  96. pthread_mutex_lock(&rm->lock);
  97. rm->changedcallback = callback;
  98. rm->changedcallback_arg = arg;
  99. pthread_mutex_unlock(&rm->lock);
  100. }
  101. int en50221_app_rm_enq(struct en50221_app_rm *rm, uint16_t session_number)
  102. {
  103. uint8_t buf[4];
  104. // set up the tag
  105. buf[0] = (TAG_PROFILE_ENQUIRY >> 16) & 0xFF;
  106. buf[1] = (TAG_PROFILE_ENQUIRY >> 8) & 0xFF;
  107. buf[2] = TAG_PROFILE_ENQUIRY & 0xFF;
  108. buf[3] = 0;
  109. // create the data and send it
  110. return rm->funcs->send_data(rm->funcs->arg, session_number, buf, 4);
  111. }
  112. int en50221_app_rm_reply(struct en50221_app_rm *rm,
  113. uint16_t session_number,
  114. uint32_t resource_id_count,
  115. uint32_t * resource_ids)
  116. {
  117. uint8_t buf[10];
  118. // set up the tag
  119. buf[0] = (TAG_PROFILE >> 16) & 0xFF;
  120. buf[1] = (TAG_PROFILE >> 8) & 0xFF;
  121. buf[2] = TAG_PROFILE & 0xFF;
  122. // encode the length field
  123. int length_field_len;
  124. if ((length_field_len = asn_1_encode(resource_id_count * 4, buf + 3, 3)) < 0) {
  125. return -1;
  126. }
  127. // copy the data and byteswap it
  128. uint32_t *copy_resource_ids = alloca(4 * resource_id_count);
  129. if (copy_resource_ids == NULL) {
  130. return -1;
  131. }
  132. uint8_t *data = (uint8_t *) copy_resource_ids;
  133. memcpy(data, resource_ids, resource_id_count * 4);
  134. uint32_t i;
  135. for (i = 0; i < resource_id_count; i++) {
  136. bswap32(data);
  137. data += 4;
  138. }
  139. // build the iovecs
  140. struct iovec iov[2];
  141. iov[0].iov_base = buf;
  142. iov[0].iov_len = 3 + length_field_len;
  143. iov[1].iov_base = (uint8_t *) copy_resource_ids;
  144. iov[1].iov_len = resource_id_count * 4;
  145. // create the data and send it
  146. return rm->funcs->send_datav(rm->funcs->arg, session_number, iov, 2);
  147. }
  148. int en50221_app_rm_changed(struct en50221_app_rm *rm,
  149. uint16_t session_number)
  150. {
  151. uint8_t buf[4];
  152. // set up the tag
  153. buf[0] = (TAG_PROFILE_CHANGE >> 16) & 0xFF;
  154. buf[1] = (TAG_PROFILE_CHANGE >> 8) & 0xFF;
  155. buf[2] = TAG_PROFILE_CHANGE & 0xFF;
  156. buf[3] = 0;
  157. // create the data and send it
  158. return rm->funcs->send_data(rm->funcs->arg, session_number, buf, 4);
  159. }
  160. int en50221_app_rm_message(struct en50221_app_rm *rm,
  161. uint8_t slot_id,
  162. uint16_t session_number,
  163. uint32_t resource_id,
  164. uint8_t * data, uint32_t data_length)
  165. {
  166. (void) resource_id;
  167. // get the tag
  168. if (data_length < 3) {
  169. print(LOG_LEVEL, ERROR, 1, "Received short data\n");
  170. return -1;
  171. }
  172. uint32_t tag = (data[0] << 16) | (data[1] << 8) | data[2];
  173. // dispatch it
  174. switch (tag) {
  175. case TAG_PROFILE_ENQUIRY:
  176. return en50221_app_rm_parse_profile_enq(rm, slot_id,
  177. session_number,
  178. data + 3,
  179. data_length - 3);
  180. case TAG_PROFILE:
  181. return en50221_app_rm_parse_profile_reply(rm, slot_id,
  182. session_number,
  183. data + 3,
  184. data_length - 3);
  185. case TAG_PROFILE_CHANGE:
  186. return en50221_app_rm_parse_profile_change(rm, slot_id,
  187. session_number,
  188. data + 3,
  189. data_length - 3);
  190. }
  191. print(LOG_LEVEL, ERROR, 1, "Received unexpected tag %x\n", tag);
  192. return -1;
  193. }
  194. static int en50221_app_rm_parse_profile_enq(struct en50221_app_rm *rm,
  195. uint8_t slot_id,
  196. uint16_t session_number,
  197. uint8_t * data,
  198. uint32_t data_length)
  199. {
  200. (void) data;
  201. (void) data_length;
  202. pthread_mutex_lock(&rm->lock);
  203. en50221_app_rm_enq_callback cb = rm->enqcallback;
  204. void *cb_arg = rm->enqcallback_arg;
  205. pthread_mutex_unlock(&rm->lock);
  206. if (cb) {
  207. return cb(cb_arg, slot_id, session_number);
  208. }
  209. return 0;
  210. }
  211. static int en50221_app_rm_parse_profile_reply(struct en50221_app_rm *rm,
  212. uint8_t slot_id,
  213. uint16_t session_number,
  214. uint8_t * data,
  215. uint32_t data_length)
  216. {
  217. // first of all, decode the length field
  218. uint16_t asn_data_length;
  219. int length_field_len;
  220. if ((length_field_len = asn_1_decode(&asn_data_length, data, data_length)) < 0) {
  221. print(LOG_LEVEL, ERROR, 1, "ASN.1 decode error\n");
  222. return -1;
  223. }
  224. // check it
  225. if (asn_data_length > (data_length - length_field_len)) {
  226. print(LOG_LEVEL, ERROR, 1, "Received short data\n");
  227. return -1;
  228. }
  229. uint32_t resources_count = asn_data_length / 4;
  230. uint32_t *resource_ids = (uint32_t *) (data + length_field_len);
  231. data += length_field_len;
  232. // byteswap it
  233. uint32_t i;
  234. for (i = 0; i < resources_count; i++) {
  235. bswap32(data);
  236. data += 4;
  237. }
  238. // inform observer
  239. pthread_mutex_lock(&rm->lock);
  240. en50221_app_rm_reply_callback cb = rm->replycallback;
  241. void *cb_arg = rm->replycallback_arg;
  242. pthread_mutex_unlock(&rm->lock);
  243. if (cb) {
  244. return cb(cb_arg, slot_id, session_number, resources_count, resource_ids);
  245. }
  246. return 0;
  247. }
  248. static int en50221_app_rm_parse_profile_change(struct en50221_app_rm *rm,
  249. uint8_t slot_id,
  250. uint16_t session_number,
  251. uint8_t * data,
  252. uint32_t data_length)
  253. {
  254. (void) data;
  255. (void) data_length;
  256. pthread_mutex_lock(&rm->lock);
  257. en50221_app_rm_changed_callback cb = rm->changedcallback;
  258. void *cb_arg = rm->changedcallback_arg;
  259. pthread_mutex_unlock(&rm->lock);
  260. if (cb) {
  261. return cb(cb_arg, slot_id, session_number);
  262. }
  263. return 0;
  264. }