section_buf.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * section and descriptor parser
  3. *
  4. * Copyright (C) 2005 Andrew de Quincey (adq_dvb@lidskialf.net)
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library 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 GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  19. */
  20. #ifndef _UCSI_SECTION_BUF_H
  21. #define _UCSI_SECTION_BUF_H 1
  22. #ifdef __cplusplus
  23. extern "C"
  24. {
  25. #endif
  26. #include <stdint.h>
  27. #define DVB_MAX_SECTION_BYTES 4096
  28. /**
  29. * Buffer used to keep track of section fragments. You should allocate an
  30. * area of memory of size (sizeof(section_buf) + <maxsectionsize>), and pass that area
  31. * to section_buf_init() to set it up.
  32. */
  33. struct section_buf {
  34. uint32_t max; /* maximum size of section - setup by section_buf_init() */
  35. uint32_t count; /* number of bytes currently accumulated */
  36. uint32_t len; /* total number of bytes expected in the complete section */
  37. uint8_t header:1; /* flag indicating the section header has been commpletely received */
  38. uint8_t wait_pdu:1;/* flag indicating to wait till the next PDU start */
  39. /* uint8_t data[] */
  40. };
  41. /**
  42. * Initialise a section_buf structure.
  43. *
  44. * @param section The section_buf to initialise.
  45. * @param max Maximum number of bytes in section (must be > 3)
  46. * @return 0 on success, nonzero on error.
  47. */
  48. extern int section_buf_init(struct section_buf *section, int max);
  49. /**
  50. * Reset a section_buf structure (e.g. if a discontinuity occurred). The
  51. * section_buf will wait for the first PDU start indicator.
  52. *
  53. * @param section The section_buf to reset.
  54. */
  55. static inline void section_buf_reset(struct section_buf *section)
  56. {
  57. int tmp = section->wait_pdu;
  58. section_buf_init(section, section->max);
  59. section->wait_pdu = tmp;
  60. }
  61. /**
  62. * Add a data fragment to a section_buf.
  63. *
  64. * @param section section_buf to add to.
  65. * @param frag Pointer to data fragment.
  66. * @param len Number of bytes of data.
  67. * @param section_status 0: nothing special. 1: section complete. -ERANGE indicates that the
  68. * section is larger than section->max.
  69. * @return Number of bytes which were consumed.
  70. */
  71. extern int section_buf_add(struct section_buf *section, uint8_t* frag, int len, int *section_status);
  72. /**
  73. * Add a transport packet PSI payload to a section_buf. This takes into account
  74. * the extra byte present in PDU_START flagged packets.
  75. *
  76. * @param section section_buf to add to.
  77. * @param payload Pointer to packet payload data.
  78. * @param len Number of bytes of data.
  79. * @param pdu_start True if the payload_unit_start_indicator flag was set in the
  80. * TS packet.
  81. * @param section_status 0: nothing special. 1: section complete. -ERANGE indicates that the
  82. * section is larger than section->max. -EINVAL indicates the pointer_field was completely
  83. * invalid (too large).
  84. */
  85. extern int section_buf_add_transport_payload(struct section_buf *section,
  86. uint8_t* payload, int len,
  87. int pdu_start, int *section_status);
  88. /**
  89. * Get the number of bytes left to be received in a section_buf.
  90. *
  91. * @param section The section_buf concerned.
  92. * @return The number of bytes.
  93. */
  94. static inline int section_buf_remaining(struct section_buf *section)
  95. {
  96. return section->len - section->count;
  97. }
  98. /**
  99. * Return a pointer to the start of the data in the section_buf.
  100. *
  101. * @param section The section_buf concerned.
  102. * @return The data.
  103. */
  104. static inline uint8_t* section_buf_data(struct section_buf *section)
  105. {
  106. return (uint8_t*) section + sizeof(struct section_buf);
  107. }
  108. #ifdef __cplusplus
  109. }
  110. #endif
  111. #endif