cell_list_descriptor.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * section and descriptor parser
  3. *
  4. * Copyright (C) 2005 Kenneth Aafloy (kenneth@linuxtv.org)
  5. * Copyright (C) 2005 Andrew de Quincey (adq_dvb@lidskialf.net)
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  20. */
  21. #ifndef _UCSI_DVB_CELL_LIST_DESCRIPTOR
  22. #define _UCSI_DVB_CELL_LIST_DESCRIPTOR 1
  23. #ifdef __cplusplus
  24. extern "C"
  25. {
  26. #endif
  27. #include <libucsi/descriptor.h>
  28. #include <libucsi/endianops.h>
  29. /**
  30. * dvb_cell_list_descriptor structure.
  31. */
  32. struct dvb_cell_list_descriptor {
  33. struct descriptor d;
  34. /* struct dvb_cell_list_entry cells[] */
  35. } __ucsi_packed;
  36. /**
  37. * An entry in the cells field of a dvb_cell_list_descriptor.
  38. */
  39. struct dvb_cell_list_entry {
  40. uint16_t cell_id;
  41. uint16_t cell_latitude;
  42. uint16_t cell_longitude;
  43. EBIT3(uint32_t cell_extend_of_latitude :12; ,
  44. uint32_t cell_extend_of_longitude :12; ,
  45. uint32_t subcell_info_loop_length : 8; );
  46. /* struct dvb_subcell_list_entry subcells[] */
  47. } __ucsi_packed;
  48. /**
  49. * An entry in the subcells field of a dvb_cell_list_entry.
  50. */
  51. struct dvb_subcell_list_entry {
  52. uint8_t cell_id_extension;
  53. uint16_t subcell_latitude;
  54. uint16_t subcell_longitude;
  55. EBIT2(uint32_t subcell_extend_of_latitude :12; ,
  56. uint32_t subcell_extend_of_longitude :12; );
  57. } __ucsi_packed;
  58. /**
  59. * Process a dvb_cell_list_descriptor.
  60. *
  61. * @param d Generic descriptor pointer.
  62. * @return dvb_cell_list_descriptor pointer, or NULL on error.
  63. */
  64. static inline struct dvb_cell_list_descriptor*
  65. dvb_cell_list_descriptor_codec(struct descriptor* d)
  66. {
  67. uint32_t pos = 0;
  68. uint32_t pos2 = 0;
  69. uint8_t* buf = (uint8_t*) d + 2;
  70. uint32_t len = d->len;
  71. while(pos < len) {
  72. struct dvb_cell_list_entry *e =
  73. (struct dvb_cell_list_entry*) (buf+pos);
  74. if ((pos + sizeof(struct dvb_cell_list_entry)) > len)
  75. return NULL;
  76. bswap16(buf+pos);
  77. bswap16(buf+pos+2);
  78. bswap16(buf+pos+4);
  79. bswap32(buf+pos+6);
  80. pos += sizeof(struct dvb_cell_list_entry);
  81. if ((pos + e->subcell_info_loop_length) > len)
  82. return NULL;
  83. if (e->subcell_info_loop_length % sizeof(struct dvb_subcell_list_entry))
  84. return NULL;
  85. pos2 = 0;
  86. while(pos2 < e->subcell_info_loop_length) {
  87. bswap16(buf+pos+pos2+1);
  88. bswap16(buf+pos+pos2+3);
  89. bswap24(buf+pos+pos2+5);
  90. pos2 += sizeof(struct dvb_subcell_list_entry);
  91. }
  92. pos += e->subcell_info_loop_length;
  93. }
  94. return (struct dvb_cell_list_descriptor*) d;
  95. }
  96. /**
  97. * Iterator for the cells field of a dvb_cell_list_descriptor.
  98. *
  99. * @param d dvb_cell_list_descriptor pointer.
  100. * @param pos Variable holding a pointer to the current dvb_cell_list_entry.
  101. */
  102. #define dvb_cell_list_descriptor_cells_for_each(d, pos) \
  103. for ((pos) = dvb_cell_list_descriptor_cells_first(d); \
  104. (pos); \
  105. (pos) = dvb_cell_list_descriptor_cells_next(d, pos))
  106. /**
  107. * Iterator for the subcells field of a dvb_cell_list_entry.
  108. *
  109. * @param cell dvb_cell_list_entry pointer.
  110. * @param pos Variable holding a pointer to the current dvb_subcell_list_entry.
  111. */
  112. #define dvb_cell_list_entry_subcells_for_each(cell, pos) \
  113. for ((pos) = dvb_cell_list_entry_subcells_first(cell); \
  114. (pos); \
  115. (pos) = dvb_cell_list_entry_subcells_next(cell, pos))
  116. /******************************** PRIVATE CODE ********************************/
  117. static inline struct dvb_cell_list_entry*
  118. dvb_cell_list_descriptor_cells_first(struct dvb_cell_list_descriptor *d)
  119. {
  120. if (d->d.len == 0)
  121. return NULL;
  122. return (struct dvb_cell_list_entry *)
  123. ((uint8_t*) d + sizeof(struct dvb_cell_list_descriptor));
  124. }
  125. static inline struct dvb_cell_list_entry*
  126. dvb_cell_list_descriptor_cells_next(struct dvb_cell_list_descriptor *d,
  127. struct dvb_cell_list_entry *pos)
  128. {
  129. uint8_t *end = (uint8_t*) d + 2 + d->d.len;
  130. uint8_t *next = (uint8_t *) pos +
  131. sizeof(struct dvb_cell_list_entry) +
  132. pos->subcell_info_loop_length;
  133. if (next >= end)
  134. return NULL;
  135. return (struct dvb_cell_list_entry *) next;
  136. }
  137. static inline struct dvb_subcell_list_entry*
  138. dvb_cell_list_entry_subcells_first(struct dvb_cell_list_entry *d)
  139. {
  140. if (d->subcell_info_loop_length == 0)
  141. return NULL;
  142. return (struct dvb_subcell_list_entry*)
  143. ((uint8_t*) d + sizeof(struct dvb_cell_list_entry));
  144. }
  145. static inline struct dvb_subcell_list_entry*
  146. dvb_cell_list_entry_subcells_next(struct dvb_cell_list_entry *d,
  147. struct dvb_subcell_list_entry *pos)
  148. {
  149. uint8_t *next = (uint8_t*) pos + sizeof(struct dvb_subcell_list_entry);
  150. uint8_t *end = (uint8_t*) d +
  151. sizeof(struct dvb_cell_list_entry) +
  152. d->subcell_info_loop_length;
  153. if (next >= end)
  154. return NULL;
  155. return (struct dvb_subcell_list_entry *) next;
  156. }
  157. #ifdef __cplusplus
  158. }
  159. #endif
  160. #endif