link_misc.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*----------------------------------------------------------------------------
  2. * Copyright (c) <2018>, <Huawei Technologies Co., Ltd>
  3. * All rights reserved.
  4. * Redistribution and use in source and binary forms, with or without modification,
  5. * are permitted provided that the following conditions are met:
  6. * 1. Redistributions of source code must retain the above copyright notice, this list of
  7. * conditions and the following disclaimer.
  8. * 2. Redistributions in binary form must reproduce the above copyright notice, this list
  9. * of conditions and the following disclaimer in the documentation and/or other materials
  10. * provided with the distribution.
  11. * 3. Neither the name of the copyright holder nor the names of its contributors may be used
  12. * to endorse or promote products derived from this software without specific prior written
  13. * permission.
  14. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  16. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  17. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  18. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  19. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  20. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  21. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  22. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  23. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  24. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. *---------------------------------------------------------------------------*/
  26. /*----------------------------------------------------------------------------
  27. * Notice of Export Control Law
  28. * ===============================================
  29. * Huawei LiteOS may be subject to applicable export control laws and regulations, which might
  30. * include those applicable to Huawei LiteOS of U.S. and the country in which you are located.
  31. * Import, export and usage of Huawei LiteOS in any manner by you shall be in compliance with such
  32. * applicable export control laws and regulations.
  33. *---------------------------------------------------------------------------*/
  34. /**
  35. * DATE AUTHOR INSTRUCTION
  36. * 2019-04-29 10:10 zhangqianfu The first version
  37. *
  38. */
  39. #ifndef LITEOS_LAB_IOT_LINK_LINK_MISC_LINK_MISC_H_
  40. #define LITEOS_LAB_IOT_LINK_LINK_MISC_LINK_MISC_H_
  41. #include <stdint.h>
  42. #include <stddef.h>
  43. /**
  44. * @brief:use this function to make a string to argc and args mode
  45. *
  46. * @param[in/out]:argc, how many buffer in the argc
  47. *
  48. * @param[in/out]:argv, the buffer for the args
  49. *
  50. * @param[in]:string, the string to be format
  51. *
  52. *
  53. * @return:how many parameters in the string
  54. *
  55. * */
  56. int string_buffer_to_arg(int *argc, const char *argv[],char *string);
  57. /**
  58. *
  59. * @brief: use this function to clone a string
  60. *
  61. * @param[in]: source string
  62. *
  63. * @return: the clone string
  64. *
  65. * */
  66. char *osal_strdup(const char *ch);
  67. /**
  68. *
  69. * dataoff
  70. * ring buffer map: | ----datalen-------
  71. * | / \
  72. * |/ \
  73. * ----------------------------------------------------------
  74. * | empty space |//////valid data///////| |
  75. * ----------------------------------------------------------
  76. * | \ /
  77. * | \ /
  78. * | \ /
  79. * | ---------------buflen--------------------------
  80. * buf
  81. *
  82. * attention that do dump read or write means no data will really write or read,
  83. * it only changed the dataoff or datalen, and you make sure that the dump read
  84. * or write length is legal
  85. *
  86. * */
  87. /**
  88. * @brief: this is the ring buffer data structure, implement as the common function
  89. *
  90. * */
  91. typedef struct
  92. {
  93. unsigned char *buf; ///< which means the buffer
  94. int buflen; ///< which means the buffer limit
  95. int datalen; ///< which means how many data in the buffer
  96. int dataoff; ///< which means the valid data offset in the buffer
  97. }tag_ring_buffer_t;
  98. typedef tag_ring_buffer_t ring_buffer_t;
  99. /**
  100. * @brief:use this function to make a new ring handle
  101. *
  102. * @param[in]:ring,which will be initialized
  103. *
  104. * @param[in]:buf, which will be used as the cache for the ring
  105. *
  106. * @param[in]:buflen, the length of buf
  107. *
  108. * @param[in]:offset, the initialize data offset
  109. *
  110. * @param[in]:datalen, the initialize data length
  111. *
  112. * @return: 0 success while -1 failed
  113. *
  114. * */
  115. int ring_buffer_init(tag_ring_buffer_t *ring,unsigned char *buf, int buflen,int offset,int datalen);
  116. /**
  117. * @brief:use this function to write data to the ring buffer
  118. *
  119. * @param[in]:ring,which will be written
  120. *
  121. * @param[in]:buf, the data buffer
  122. *
  123. * @param[in]:len, the buffer length
  124. *
  125. * @return: how many bytes has been written while -1 means failed
  126. *
  127. * */
  128. int ring_buffer_write(tag_ring_buffer_t *ring,unsigned char *buf, int len);
  129. /**
  130. * @brief:use this function to read data from the ring buffer
  131. *
  132. * @param[in]:ring,which will be read
  133. *
  134. * @param[in]:buf, the data buffer
  135. *
  136. * @param[in]:len, the buffer length
  137. *
  138. * @return: how many bytes has been read while -1 failed
  139. *
  140. * */
  141. int ring_buffer_read(tag_ring_buffer_t *ring,unsigned char *buf, int len);
  142. #define ring_buffer_buf(x) (x->buf)
  143. #define ring_buffer_buflen(x) (x->buflen)
  144. #define ring_buffer_data(x) (x->buf + x->dataoff)
  145. //#define ring_buffer_datalen(x) (x->datalen)
  146. //#define ring_buffer_freespace(x) (x->buflen - x->datalen)
  147. #define ring_buffer_dataoff(x) (x->dataoff)
  148. #define ring_buffer_dumpread(x,y) do{x->dataoff = (x->dataoff + y)%x->buflen; x->datalen -= y;}while(0)
  149. #define ring_buffer_dumpwrite(x,y) do{x->datalen += y;)while(0)
  150. /**
  151. * @brief:use this function check out how many data in the ring buffer
  152. *
  153. * @param[in]:ring,which will be checked
  154. * @return: how many data bytes while -1 failed
  155. *
  156. * */
  157. int ring_buffer_datalen(tag_ring_buffer_t *ring);
  158. /**
  159. * @brief:use this function check out how many free space in the ring buffer
  160. *
  161. * @param[in]:ring,which will be checked
  162. * @return: how many free space bytes while -1 failed
  163. *
  164. * */
  165. int ring_buffer_freespace(tag_ring_buffer_t *ring);
  166. /**
  167. * @brief:use this function to reset the ring buffer
  168. *
  169. * @param[in]:ring,which will be reset
  170. * @return: 0 success while -1 failed
  171. *
  172. * */
  173. int ring_buffer_reset(tag_ring_buffer_t *ring);
  174. /**
  175. * @brief:use this function to deinit the ring buffer
  176. *
  177. * @param[in]:ring,which will be deinit
  178. * @return: 0 success while -1 failed
  179. *
  180. * */
  181. int ring_buffer_deinit(tag_ring_buffer_t *ring);
  182. ///< random
  183. /**
  184. * @brief: use this function to generate the random ,could be reprogramed by yourelf
  185. *
  186. * @param[in]:output, this is the random data storage buffer
  187. * @param[in]:len, the output buffer length
  188. *
  189. * @retrun:0 success while -1 failed
  190. *
  191. * */
  192. int link_random(void* output, int len);
  193. /**
  194. * @brief: use this function to translate the string to argv mode
  195. *
  196. * @param[in]:string , the string to translate
  197. * @param[inout]:argc, the sizeof argv and how many prarameters has been splited
  198. * @param[inout]:argv, used to storage the parameters splited
  199. *
  200. * @retrun:how many parameters geted
  201. *
  202. * */
  203. int string_to_arg(int *argc, const char *argv[],char *string);
  204. /**
  205. * @brief: use this function to add the string list to one string
  206. *
  207. * @param[in]:str[] the array to add, end with NULL
  208. *
  209. * @retrun:the str added, NULL if failed
  210. *
  211. * */
  212. char *osal_strcat(char *str[]);
  213. int hexstr2byte(const char *bufin, int len, char *bufout);
  214. int byte2hexstr(uint8_t *bufin, int len, char *bufout);
  215. #endif /* LITEOS_LAB_IOT_LINK_LINK_MISC_LINK_MISC_H_ */