osi_hdlc.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /* Copyright (C) 2018 RDA Technologies Limited and/or its affiliates("RDA").
  2. * All rights reserved.
  3. *
  4. * This software is supplied "AS IS" without any warranties.
  5. * RDA assumes no responsibility or liability for the use of the software,
  6. * conveys no license or title under any patent, copyright, or mask work
  7. * right to the product. RDA reserves the right to make changes in the
  8. * software without notification. RDA also make no representation or
  9. * warranty that such application will be suitable for the specified use
  10. * without further testing or modification.
  11. */
  12. #ifndef _OSI_HDLC_H_
  13. #define _OSI_HDLC_H_
  14. #include "osi_compiler.h"
  15. OSI_EXTERN_C_BEGIN
  16. /**
  17. * \brief hdlc decoding state
  18. */
  19. typedef enum
  20. {
  21. /** decode on going */
  22. OSI_HDLC_DEC_ST_DECODING,
  23. /** complete packet decoded */
  24. OSI_HDLC_DEC_ST_PACKET,
  25. /** buffer overflow */
  26. OSI_HDLC_DEC_ST_OVERFLOW,
  27. } osiHdlcDecodeState_t;
  28. /**
  29. * \brief hdlc decoding flags
  30. */
  31. typedef enum
  32. {
  33. /** report buffer overflow */
  34. OSI_HDLC_DEC_CHECK_OVERFLOW = (1 << 0),
  35. } osiHdlcDecFlag_t;
  36. /**
  37. * \brief hdlc decoding data struct
  38. */
  39. typedef struct
  40. {
  41. //! @cond Doxygen_Suppress
  42. char *buf;
  43. unsigned size;
  44. unsigned len;
  45. uint16_t flags;
  46. uint16_t state;
  47. //! @endcond
  48. } osiHdlcDecode_t;
  49. /**
  50. * \brief initialize hdlc decoding
  51. *
  52. * \param d hdlc decoder instance
  53. * \param buf buffer for decoded packet
  54. * \param size buffer size
  55. * \param flags refer to \p osiHdlcDecFlag_t
  56. * \return
  57. * - true on success
  58. * - false on error, invalid parameters
  59. */
  60. bool osiHdlcDecodeInit(osiHdlcDecode_t *d, void *buf, unsigned size, uint16_t flags);
  61. /**
  62. * \brief reset hdlc decoding buffer and state
  63. *
  64. * \param d hdlc decoder instance, must be valid
  65. */
  66. void osiHdlcDecodeReset(osiHdlcDecode_t *d);
  67. /**
  68. * \brief reset hdlc decoding buffer and state
  69. *
  70. * This can be used
  71. *
  72. * \param d hdlc decoder instance, must be valid
  73. * \param buf buffer for decoded packet
  74. * \param size buffer size
  75. * \param len valid data length in \p buf
  76. * \param resync whether resync is needed
  77. * \return
  78. * - true on success
  79. * - false on error, invalid parameter
  80. */
  81. bool osiHdlcDecodeResetBuf(osiHdlcDecode_t *d, void *buf, unsigned size, unsigned len, bool resync);
  82. /**
  83. * \brief put hdlc encoded stream
  84. *
  85. * When there is complete packet inside, or overflow, it won't accept any
  86. * input data. Caller should avoid infinite loop.
  87. *
  88. * \param d hdlc decoder instance, must be valid
  89. * \param data memory for hdlc encoded stream
  90. * \param size size of hdlc encoded stream
  91. * \return
  92. * - consumed encoded data size, 0 is possible
  93. * - -1 on error, invalid parameters
  94. */
  95. int osiHdlcDecodePush(osiHdlcDecode_t *d, const void *data, unsigned size);
  96. /**
  97. * \brief get hdlc decoding state
  98. *
  99. * \param d hdlc decoder instance, must be valid
  100. * \return hdlc decoding state
  101. */
  102. osiHdlcDecodeState_t osiHdlcDecodeGetState(osiHdlcDecode_t *d);
  103. /**
  104. * \brief fetch decoded hdlc packet
  105. *
  106. * When there doesn't exist complete decoded hdlc packet, the returned
  107. * buffer size is 0.
  108. *
  109. * When there is a complete packet, it will clear the packet, and it can
  110. * accept input data after that.
  111. *
  112. * \param d hdlc decoder instance, must be valid
  113. * \return decoded hdlc packet
  114. */
  115. osiBuffer_t osiHdlcDecodeFetchPacket(osiHdlcDecode_t *d);
  116. /**
  117. * \brief get hdlc decoding buffer data
  118. *
  119. * It will return the buffer data, no matter what state is. And it won't
  120. * change hdlc decoding state.
  121. *
  122. * \param d hdlc decoder instance, must be valid
  123. * \return decoded hdlc data
  124. */
  125. osiBuffer_t osiHdlcDecodeGetData(osiHdlcDecode_t *d);
  126. /**
  127. * \brief estimate hdlc encoding size
  128. *
  129. * The return value is the same as \p osiHdlcEncode, and it won't
  130. * ensure the alignment of returned size.
  131. *
  132. * \param data memory to be encoded
  133. * \param size memory size
  134. * \return
  135. * - encoded size
  136. * - -1 on error, invalid parameter
  137. */
  138. int osiHdlcEncodeLen(const void *data, unsigned size);
  139. /**
  140. * \brief hdlc encoding to output buffer
  141. *
  142. * \p dst should be large enough to hold the whole encoded stream.
  143. *
  144. * \param dst output buffer, must be valid
  145. * \param data memory to be encoded
  146. * \param size memory size
  147. * \return
  148. * - encoded size
  149. * - -1 on error, invalid parameter
  150. */
  151. int osiHdlcEncode(void *dst, const void *data, unsigned size);
  152. /**
  153. * \brief estimate hdlc encoding size
  154. *
  155. * The return value is the same as \p osiHdlcEncodeMulti, and it won't
  156. * ensure the alignment of returned size.
  157. *
  158. * \param bufs buffer array
  159. * \param count buffer count
  160. * \return
  161. * - encoded size
  162. * - -1 on error, invalid parameter
  163. */
  164. int osiHdlcEncodeMultiLen(const osiBuffer_t *bufs, unsigned count);
  165. /**
  166. * \brief hdlc encoding to output buffer
  167. *
  168. * \p dst should be large enough to hold the whole encoded stream.
  169. *
  170. * \param dst output buffer, must be valid
  171. * \param bufs buffer array
  172. * \param count buffer count
  173. * \return
  174. * - encoded size
  175. * - -1 on error, invalid parameter
  176. */
  177. int osiHdlcEncodeMulti(void *dst, const osiBuffer_t *bufs, unsigned count);
  178. OSI_EXTERN_C_END
  179. #endif