btstack_util.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * Copyright (C) 2014 BlueKitchen GmbH
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the copyright holders nor the names of
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. * 4. Any redistribution, use, or modification is done solely for
  17. * personal benefit and not for any commercial purpose or for
  18. * monetary gain.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
  21. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
  24. * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  26. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  27. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  28. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  30. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. *
  33. * Please inquire about commercial licensing options at
  34. * contact@bluekitchen-gmbh.com
  35. *
  36. */
  37. /*
  38. * btstack_util.h
  39. *
  40. * General utility functions
  41. *
  42. * Created by Matthias Ringwald on 7/23/09.
  43. */
  44. #ifndef BTSTACK_UTIL_H
  45. #define BTSTACK_UTIL_H
  46. #if defined __cplusplus
  47. extern "C" {
  48. #endif
  49. #include <stdint.h>
  50. #include <string.h>
  51. #include "bluetooth.h"
  52. #include "btstack_defines.h"
  53. #include "btstack_linked_list.h"
  54. // hack: compilation with the android ndk causes an error as there's a reverse_64 macro
  55. #ifdef reverse_64
  56. #undef reverse_64
  57. #endif
  58. // will be moved to daemon/btstack_device_name_db.h
  59. /**
  60. * @brief The device name type
  61. */
  62. #define DEVICE_NAME_LEN 248
  63. typedef uint8_t device_name_t[DEVICE_NAME_LEN+1];
  64. /* API_START */
  65. /**
  66. * @brief Minimum function for uint32_t
  67. * @param a
  68. * @param b
  69. * @return value
  70. */
  71. uint32_t btstack_min(uint32_t a, uint32_t b);
  72. /**
  73. * @brief Maximum function for uint32_t
  74. * @param a
  75. * @param b
  76. * @return value
  77. */
  78. uint32_t btstack_max(uint32_t a, uint32_t b);
  79. /**
  80. * @brief Calculate delta between two points in time
  81. * @returns time_a - time_b - result > 0 if time_a is newer than time_b
  82. */
  83. int32_t btstack_time_delta(uint32_t time_a, uint32_t time_b);
  84. /**
  85. * @brief Read 16/24/32 bit little endian value from buffer
  86. * @param buffer
  87. * @param position in buffer
  88. * @return value
  89. */
  90. uint16_t little_endian_read_16(const uint8_t * buffer, int position);
  91. uint32_t little_endian_read_24(const uint8_t * buffer, int position);
  92. uint32_t little_endian_read_32(const uint8_t * buffer, int position);
  93. /**
  94. * @brief Write 16/32 bit little endian value into buffer
  95. * @param buffer
  96. * @param position in buffer
  97. * @param value
  98. */
  99. void little_endian_store_16(uint8_t *buffer, uint16_t position, uint16_t value);
  100. void little_endian_store_24(uint8_t *buffer, uint16_t position, uint32_t value);
  101. void little_endian_store_32(uint8_t *buffer, uint16_t position, uint32_t value);
  102. /**
  103. * @brief Read 16/24/32 bit big endian value from buffer
  104. * @param buffer
  105. * @param position in buffer
  106. * @return value
  107. */
  108. uint32_t big_endian_read_16( const uint8_t * buffer, int pos);
  109. uint32_t big_endian_read_24( const uint8_t * buffer, int pos);
  110. uint32_t big_endian_read_32( const uint8_t * buffer, int pos);
  111. /**
  112. * @brief Write 16/32 bit big endian value into buffer
  113. * @param buffer
  114. * @param position in buffer
  115. * @param value
  116. */
  117. void big_endian_store_16(uint8_t *buffer, uint16_t pos, uint16_t value);
  118. void big_endian_store_24(uint8_t *buffer, uint16_t pos, uint32_t value);
  119. void big_endian_store_32(uint8_t *buffer, uint16_t pos, uint32_t value);
  120. /**
  121. * @brief Swap bytes in 16 bit integer
  122. */
  123. static inline uint16_t btstack_flip_16(uint16_t value){
  124. return (uint16_t)((value & 0xffu) << 8) | (value >> 8);
  125. }
  126. /**
  127. * @brief Check for big endian system
  128. * @returns 1 if on big endian
  129. */
  130. static inline int btstack_is_big_endian(void){
  131. uint16_t sample = 0x0100;
  132. return (int) *(uint8_t*) &sample;
  133. }
  134. /**
  135. * @brief Check for little endian system
  136. * @returns 1 if on little endian
  137. */
  138. static inline int btstack_is_little_endian(void){
  139. uint16_t sample = 0x0001;
  140. return (int) *(uint8_t*) &sample;
  141. }
  142. /**
  143. * @brief Copy from source to destination and reverse byte order
  144. * @param src
  145. * @param dest
  146. * @param len
  147. */
  148. void reverse_bytes (const uint8_t *src, uint8_t * dest, int len);
  149. /**
  150. * @brief Wrapper around reverse_bytes for common buffer sizes
  151. * @param src
  152. * @param dest
  153. */
  154. void reverse_24 (const uint8_t *src, uint8_t * dest);
  155. void reverse_48 (const uint8_t *src, uint8_t * dest);
  156. void reverse_56 (const uint8_t *src, uint8_t * dest);
  157. void reverse_64 (const uint8_t *src, uint8_t * dest);
  158. void reverse_128(const uint8_t *src, uint8_t * dest);
  159. void reverse_256(const uint8_t *src, uint8_t * dest);
  160. void reverse_bd_addr(const bd_addr_t src, bd_addr_t dest);
  161. /**
  162. * @brief ASCII character for 4-bit nibble
  163. * @return character
  164. */
  165. char char_for_nibble(int nibble);
  166. /**
  167. * @brif 4-bit nibble from ASCII character
  168. * @return value
  169. */
  170. int nibble_for_char(char c);
  171. /**
  172. * @brief Compare two Bluetooth addresses
  173. * @param a
  174. * @param b
  175. * @return 0 if equal
  176. */
  177. int bd_addr_cmp(const bd_addr_t a, const bd_addr_t b);
  178. /**
  179. * @brief Copy Bluetooth address
  180. * @param dest
  181. * @param src
  182. */
  183. void bd_addr_copy(bd_addr_t dest, const bd_addr_t src);
  184. /**
  185. * @brief Use printf to write hexdump as single line of data
  186. */
  187. void printf_hexdump(const void *data, int size);
  188. /**
  189. * @brief Create human readable representation for UUID128
  190. * @note uses fixed global buffer
  191. * @return pointer to UUID128 string
  192. */
  193. char * uuid128_to_str(const uint8_t * uuid);
  194. /**
  195. * @brief Create human readable represenationt of Bluetooth address
  196. * @note uses fixed global buffer
  197. * @return pointer to Bluetooth address string
  198. */
  199. char * bd_addr_to_str(const bd_addr_t addr);
  200. /**
  201. * @brief Replace address placeholder '00:00:00:00:00:00' with Bluetooth address
  202. * @param buffer
  203. * @param size
  204. * @param address
  205. */
  206. void btstack_replace_bd_addr_placeholder(uint8_t * buffer, uint16_t size, const bd_addr_t address);
  207. /**
  208. * @brief Parse Bluetooth address
  209. * @param address_string
  210. * @param buffer for parsed address
  211. * @return 1 if string was parsed successfully
  212. */
  213. int sscanf_bd_addr(const char * addr_string, bd_addr_t addr);
  214. /**
  215. * @brief Constructs UUID128 from 16 or 32 bit UUID using Bluetooth base UUID
  216. * @param uuid128 output buffer
  217. * @param short_uuid
  218. */
  219. void uuid_add_bluetooth_prefix(uint8_t * uuid128, uint32_t short_uuid);
  220. /**
  221. * @brief Checks if UUID128 has Bluetooth base UUID prefix
  222. * @param uui128 to test
  223. * @return 1 if it can be expressed as UUID32
  224. */
  225. int uuid_has_bluetooth_prefix(const uint8_t * uuid128);
  226. /**
  227. * @brief Parse unsigned number
  228. * @param str to parse
  229. * @return value
  230. */
  231. uint32_t btstack_atoi(const char *str);
  232. /**
  233. * @brief Return number of digits of a uint32 number
  234. * @param uint32_number
  235. * @return num_digits
  236. */
  237. int string_len_for_uint32(uint32_t i);
  238. /**
  239. * @brief Return number of set bits in a uint32 number
  240. * @param uint32_number
  241. * @return num_set_bits
  242. */
  243. int count_set_bits_uint32(uint32_t x);
  244. /**
  245. * CRC8 functions using ETSI TS 101 369 V6.3.0.
  246. * Only used by RFCOMM
  247. */
  248. uint8_t btstack_crc8_check(uint8_t *data, uint16_t len, uint8_t check_sum);
  249. uint8_t btstack_crc8_calc(uint8_t *data, uint16_t len);
  250. /* API_END */
  251. #if defined __cplusplus
  252. }
  253. #endif
  254. #endif // BTSTACK_UTIL_H