loc_misc_utils.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /* Copyright (c) 2014, 2020 - 2021 The Linux Foundation. All rights reserved.
  2. *
  3. * Redistribution and use in source and binary forms, with or without
  4. * modification, are permitted provided that the following conditions are
  5. * met:
  6. * * Redistributions of source code must retain the above copyright
  7. * notice, this list of conditions and the following disclaimer.
  8. * * Redistributions in binary form must reproduce the above
  9. * copyright notice, this list of conditions and the following
  10. * disclaimer in the documentation and/or other materials provided
  11. * with the distribution.
  12. * * Neither the name of The Linux Foundation, nor the names of its
  13. * contributors may be used to endorse or promote products derived
  14. * from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
  20. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  21. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  22. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  23. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  24. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  25. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  26. * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. *
  28. */
  29. #ifndef _LOC_MISC_UTILS_H_
  30. #define _LOC_MISC_UTILS_H_
  31. #include <stdint.h>
  32. #include <ios>
  33. #include <string>
  34. #include <sstream>
  35. #ifdef __cplusplus
  36. extern "C" {
  37. #endif
  38. #include <stddef.h>
  39. #include <stdint.h>
  40. /*===========================================================================
  41. FUNCTION loc_split_string
  42. DESCRIPTION:
  43. This function is used to split a delimiter separated string into
  44. sub-strings. This function does not allocate new memory to store the split
  45. strings. Instead, it places '\0' in places of delimiters and assings the
  46. starting address of the substring within the raw string as the string address
  47. The input raw_string no longer remains to be a collection of sub-strings
  48. after this function is executed.
  49. Please make a copy of the input string before calling this function if
  50. necessary
  51. PARAMETERS:
  52. char *raw_string: is the original string with delimiter separated substrings
  53. char **split_strings_ptr: is the arraw of pointers which will hold the addresses
  54. of individual substrings
  55. int max_num_substrings: is the maximum number of substrings that are expected
  56. by the caller. The array of pointers in the above parameter
  57. is usually this long
  58. char delimiter: is the delimiter that separates the substrings. Examples: ' ', ';'
  59. DEPENDENCIES
  60. N/A
  61. RETURN VALUE
  62. int Number of split strings
  63. SIDE EFFECTS
  64. The input raw_string no longer remains a delimiter separated single string.
  65. EXAMPLE
  66. delimiter = ' ' //space
  67. raw_string = "hello new user" //delimiter is space ' '
  68. addresses = 0123456789abcd
  69. split_strings_ptr[0] = &raw_string[0]; //split_strings_ptr[0] contains "hello"
  70. split_strings_ptr[1] = &raw_string[6]; //split_strings_ptr[1] contains "new"
  71. split_strings_ptr[2] = &raw_string[a]; //split_strings_ptr[2] contains "user"
  72. ===========================================================================*/
  73. int loc_util_split_string(char *raw_string, char **split_strings_ptr, int max_num_substrings,
  74. char delimiter);
  75. /*===========================================================================
  76. FUNCTION trim_space
  77. DESCRIPTION
  78. Removes leading and trailing spaces of the string
  79. DEPENDENCIES
  80. N/A
  81. RETURN VALUE
  82. None
  83. SIDE EFFECTS
  84. N/A
  85. ===========================================================================*/
  86. void loc_util_trim_space(char *org_string);
  87. /*===========================================================================
  88. FUNCTION dlGetSymFromLib
  89. DESCRIPTION
  90. Handy function to get a pointer to a symbol from a library.
  91. If libHandle is not null, it will be used as the handle to the library. In
  92. that case libName wll not be used;
  93. libHandle is an in / out parameter.
  94. If libHandle is null, libName will be used to dlopen.
  95. Either libHandle or libName must not be nullptr.
  96. symName must not be null.
  97. DEPENDENCIES
  98. N/A
  99. RETURN VALUE
  100. pointer to symName. Could be nullptr if
  101. Parameters are incorrect; or
  102. libName can not be opened; or
  103. symName can not be found.
  104. SIDE EFFECTS
  105. N/A
  106. ===========================================================================*/
  107. void* dlGetSymFromLib(void*& libHandle, const char* libName, const char* symName);
  108. /*===========================================================================
  109. FUNCTION getQTimerTickCount
  110. DESCRIPTION
  111. This function is used to read the QTimer ticks count. This value is globally maintained and
  112. must be the same across all processors on a target.
  113. DEPENDENCIES
  114. N/A
  115. RETURN VALUE
  116. uint64_t QTimer tick count
  117. SIDE EFFECTS
  118. N/A
  119. ===========================================================================*/
  120. uint64_t getQTimerTickCount();
  121. /*===========================================================================
  122. FUNCTION getQTimerDeltaNanos
  123. DESCRIPTION
  124. This function is used to read the the difference in nanoseconds between
  125. Qtimer on AP side and Qtimer on MP side for dual-SoC architectures such as Kona
  126. DEPENDENCIES
  127. N/A
  128. RETURN VALUE
  129. uint64_t QTimer difference in nanoseconds
  130. SIDE EFFECTS
  131. N/A
  132. ===========================================================================*/
  133. uint64_t getQTimerDeltaNanos();
  134. /*===========================================================================
  135. FUNCTION getQTimerFreq
  136. DESCRIPTION
  137. This function is used to read the QTimer frequency in hz. This value is globally maintained and
  138. must be the same across all processors on a target.
  139. DEPENDENCIES
  140. N/A
  141. RETURN VALUE
  142. uint64_t QTimer frequency
  143. SIDE EFFECTS
  144. N/A
  145. ===========================================================================*/
  146. uint64_t getQTimerFreq();
  147. /*===========================================================================
  148. FUNCTION getBootTimeMilliSec
  149. DESCRIPTION
  150. This function is used to get boot time in milliseconds.
  151. DEPENDENCIES
  152. N/A
  153. RETURN VALUE
  154. uint64_t boot time in milliseconds
  155. SIDE EFFECTS
  156. N/A
  157. ===========================================================================*/
  158. uint64_t getBootTimeMilliSec();
  159. #ifdef __cplusplus
  160. }
  161. #endif
  162. using std::hex;
  163. using std::string;
  164. using std::stringstream;
  165. /*===========================================================================
  166. FUNCTION to_string_hex
  167. DESCRIPTION
  168. This function works similar to std::to_string, but puts only in hex format.
  169. DEPENDENCIES
  170. N/A
  171. RETURN VALUE
  172. string, of input val in hex format
  173. SIDE EFFECTS
  174. N/A
  175. ===========================================================================*/
  176. template <typename T>
  177. string to_string_hex(T val) {
  178. stringstream ss;
  179. if (val < 0) {
  180. val = -val;
  181. ss << "-";
  182. }
  183. ss << hex << "0x" << val;
  184. return ss.str();
  185. }
  186. /*===========================================================================
  187. FUNCTION loc_prim_arr_to_string
  188. DESCRIPTION
  189. This function puts out primitive array in DEC or EHX format.
  190. DEPENDENCIES
  191. N/A
  192. RETURN VALUE
  193. string, space separated string of values in the input array, either
  194. in decimal or hex format, depending on the value of decIfTrue
  195. SIDE EFFECTS
  196. N/A
  197. ===========================================================================*/
  198. template <typename T>
  199. static string loc_prim_arr_to_string(T* arr, uint32_t size, bool decIfTrue = true) {
  200. stringstream ss;
  201. for (uint32_t i = 0; i < size; i++) {
  202. ss << (decIfTrue ? to_string(arr[i]) : to_string_hex(arr[i]));
  203. if (i != size - 1) {
  204. ss << " ";
  205. }
  206. }
  207. return ss.str();
  208. }
  209. /*===========================================================================
  210. FUNCTION qTimerTicksToNanos
  211. DESCRIPTION
  212. Transform from ticks to nanoseconds, clock is 19.2 MHz
  213. so the formula would be qtimer(ns) = (ticks * 1000000000) / 19200000
  214. or simplified qtimer(ns) = (ticks * 10000) / 192.
  215. DEPENDENCIES
  216. N/A
  217. RETURN VALUE
  218. Qtimer value in nanoseconds
  219. SIDE EFFECTS
  220. N/A
  221. ===========================================================================*/
  222. inline uint64_t qTimerTicksToNanos(double qTimer) {
  223. return (uint64_t((qTimer * double(10000ull)) / (double)192ull));
  224. }
  225. /*===========================================================================
  226. FUNCTION loc_convert_lla_gnss_to_vrp
  227. DESCRIPTION
  228. This function converts lat/long/altitude from GNSS antenna based
  229. to vehicle reference point based.
  230. DEPENDENCIES
  231. N/A
  232. RETURN VALUE
  233. The converted lat/long/altitude will be stored in the parameter of llaInfo.
  234. SIDE EFFECTS
  235. N/A
  236. ===========================================================================*/
  237. void loc_convert_lla_gnss_to_vrp(double lla[3], float rollPitchYaw[3],
  238. float leverArm[3]);
  239. /*===========================================================================
  240. FUNCTION loc_convert_velocity_gnss_to_vrp
  241. DESCRIPTION
  242. This function converts east/north/up velocity from GNSS antenna based
  243. to vehicle reference point based.
  244. DEPENDENCIES
  245. N/A
  246. RETURN VALUE
  247. The converted east/north/up velocity will be stored in the parameter of
  248. enuVelocity.
  249. SIDE EFFECTS
  250. N/A
  251. ===========================================================================*/
  252. void loc_convert_velocity_gnss_to_vrp(float enuVelocity[3], float rollPitchYaw[3],
  253. float rollPitchYawRate[3], float leverArm[3]);
  254. /*===========================================================================
  255. FUNCTION locUtilWaitForDir
  256. DESCRIPTION
  257. This function will block until the directory specified in
  258. dirName has been created
  259. DEPENDENCIES
  260. N/A
  261. RETURN VALUE
  262. N/A
  263. SIDE EFFECTS
  264. N/A
  265. ===========================================================================*/
  266. void locUtilWaitForDir(const char* dirName);
  267. #endif //_LOC_MISC_UTILS_H_