1 /* Copyright (c) 2014, 2020 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 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 #include <stddef.h>
40 #include <stdint.h>
41 /*===========================================================================
42 FUNCTION loc_split_string
43 
44 DESCRIPTION:
45     This function is used to split a delimiter separated string into
46     sub-strings. This function does not allocate new memory to store the split
47     strings. Instead, it places '\0' in places of delimiters and assings the
48     starting address of the substring within the raw string as the string address
49     The input raw_string no longer remains to be a collection of sub-strings
50     after this function is executed.
51     Please make a copy of the input string before calling this function if
52     necessary
53 
54 PARAMETERS:
55     char *raw_string: is the original string with delimiter separated substrings
56     char **split_strings_ptr: is the arraw of pointers which will hold the addresses
57                               of individual substrings
58     int max_num_substrings: is the maximum number of substrings that are expected
59                             by the caller. The array of pointers in the above parameter
60                             is usually this long
61     char delimiter: is the delimiter that separates the substrings. Examples: ' ', ';'
62 
63 DEPENDENCIES
64     N/A
65 
66 RETURN VALUE
67     int Number of split strings
68 
69 SIDE EFFECTS
70     The input raw_string no longer remains a delimiter separated single string.
71 
72 EXAMPLE
73     delimiter = ' ' //space
74     raw_string = "hello new user" //delimiter is space ' '
75     addresses  =  0123456789abcd
76     split_strings_ptr[0] = &raw_string[0]; //split_strings_ptr[0] contains "hello"
77     split_strings_ptr[1] = &raw_string[6]; //split_strings_ptr[1] contains "new"
78     split_strings_ptr[2] = &raw_string[a]; //split_strings_ptr[2] contains "user"
79 
80 ===========================================================================*/
81 int loc_util_split_string(char *raw_string, char **split_strings_ptr, int max_num_substrings,
82                      char delimiter);
83 
84 /*===========================================================================
85 FUNCTION trim_space
86 
87 DESCRIPTION
88    Removes leading and trailing spaces of the string
89 
90 DEPENDENCIES
91    N/A
92 
93 RETURN VALUE
94    None
95 
96 SIDE EFFECTS
97    N/A
98 ===========================================================================*/
99 void loc_util_trim_space(char *org_string);
100 
101 /*===========================================================================
102 FUNCTION dlGetSymFromLib
103 
104 DESCRIPTION
105    Handy function to get a pointer to a symbol from a library.
106 
107    If libHandle is not null, it will be used as the handle to the library. In
108    that case libName wll not be used;
109    libHandle is an in / out parameter.
110    If libHandle is null, libName will be used to dlopen.
111    Either libHandle or libName must not be nullptr.
112    symName must not be null.
113 
114 DEPENDENCIES
115    N/A
116 
117 RETURN VALUE
118    pointer to symName. Could be nullptr if
119        Parameters are incorrect; or
120        libName can not be opened; or
121        symName can not be found.
122 
123 SIDE EFFECTS
124    N/A
125 ===========================================================================*/
126 void* dlGetSymFromLib(void*& libHandle, const char* libName, const char* symName);
127 
128 /*===========================================================================
129 FUNCTION getQTimerTickCount
130 
131 DESCRIPTION
132    This function is used to read the QTimer ticks count. This value is globally maintained and
133    must be the same across all processors on a target.
134 
135 DEPENDENCIES
136    N/A
137 
138 RETURN VALUE
139     uint64_t QTimer tick count
140 
141 SIDE EFFECTS
142    N/A
143 ===========================================================================*/
144 uint64_t getQTimerTickCount();
145 
146 /*===========================================================================
147 FUNCTION getQTimerDeltaNanos
148 
149 DESCRIPTION
150 This function is used to read the the difference in nanoseconds between
151 Qtimer on AP side and Qtimer on MP side for dual-SoC architectures such as Kona
152 
153 DEPENDENCIES
154 N/A
155 
156 RETURN VALUE
157 uint64_t QTimer difference in nanoseconds
158 
159 SIDE EFFECTS
160 N/A
161 ===========================================================================*/
162 uint64_t getQTimerDeltaNanos();
163 
164 /*===========================================================================
165 FUNCTION getQTimerFreq
166 
167 DESCRIPTION
168    This function is used to read the QTimer frequency in hz. This value is globally maintained and
169    must be the same across all processors on a target.
170 
171 DEPENDENCIES
172    N/A
173 
174 RETURN VALUE
175     uint64_t QTimer frequency
176 
177 SIDE EFFECTS
178    N/A
179 ===========================================================================*/
180 uint64_t getQTimerFreq();
181 
182 /*===========================================================================
183 FUNCTION getBootTimeMilliSec
184 
185 DESCRIPTION
186    This function is used to get boot time in milliseconds.
187 
188 DEPENDENCIES
189    N/A
190 
191 RETURN VALUE
192     uint64_t boot time in milliseconds
193 
194 SIDE EFFECTS
195    N/A
196 ===========================================================================*/
197 uint64_t getBootTimeMilliSec();
198 
199 #ifdef __cplusplus
200 }
201 #endif
202 
203 using std::hex;
204 using std::string;
205 using std::stringstream;
206 
207 /*===========================================================================
208 FUNCTION to_string_hex
209 
210 DESCRIPTION
211    This function works similar to std::to_string, but puts only in hex format.
212 
213 DEPENDENCIES
214    N/A
215 
216 RETURN VALUE
217    string, of input val in hex format
218 
219 SIDE EFFECTS
220    N/A
221 ===========================================================================*/
222 template <typename T>
to_string_hex(T val)223 string to_string_hex(T val) {
224     stringstream ss;
225     if (val < 0) {
226         val = -val;
227         ss << "-";
228     }
229     ss << hex << "0x" << val;
230     return ss.str();
231 }
232 
233 /*===========================================================================
234 FUNCTION loc_prim_arr_to_string
235 
236 DESCRIPTION
237    This function puts out primitive array in DEC or EHX format.
238 
239 DEPENDENCIES
240    N/A
241 
242 RETURN VALUE
243     string, space separated string of values in the input array, either
244             in decimal or hex format, depending on the value of decIfTrue
245 
246 SIDE EFFECTS
247    N/A
248 ===========================================================================*/
249 template <typename T>
250 static string loc_prim_arr_to_string(T* arr, uint32_t size, bool decIfTrue = true) {
251     stringstream ss;
252     for (uint32_t i = 0; i < size; i++) {
253         ss << (decIfTrue ? to_string(arr[i]) : to_string_hex(arr[i]));
254         if (i != size - 1) {
255             ss << " ";
256         }
257     }
258     return ss.str();
259 }
260 
261 /*===========================================================================
262 FUNCTION qTimerTicksToNanos
263 
264 DESCRIPTION
265     Transform from ticks to nanoseconds, clock is 19.2 MHz
266     so the formula would be qtimer(ns) = (ticks * 1000000000) / 19200000
267     or simplified qtimer(ns) = (ticks * 10000) / 192.
268 
269 DEPENDENCIES
270     N/A
271 
272 RETURN VALUE
273     Qtimer value in nanoseconds
274 
275 SIDE EFFECTS
276     N/A
277 ===========================================================================*/
qTimerTicksToNanos(double qTimer)278 inline uint64_t qTimerTicksToNanos(double qTimer) {
279     return (uint64_t((qTimer * double(10000ull)) / (double)192ull));
280 }
281 
282 /*===========================================================================
283 FUNCTION loc_convert_lla_gnss_to_vrp
284 
285 DESCRIPTION
286    This function converts lat/long/altitude from GNSS antenna based
287    to vehicle reference point based.
288 
289 DEPENDENCIES
290    N/A
291 
292 RETURN VALUE
293     The converted lat/long/altitude will be stored in the parameter of llaInfo.
294 
295 SIDE EFFECTS
296    N/A
297 ===========================================================================*/
298 void loc_convert_lla_gnss_to_vrp(double lla[3], float rollPitchYaw[3],
299                                  float leverArm[3]);
300 
301 /*===========================================================================
302 FUNCTION loc_convert_velocity_gnss_to_vrp
303 
304 DESCRIPTION
305    This function converts east/north/up velocity from GNSS antenna based
306    to vehicle reference point based.
307 
308 DEPENDENCIES
309    N/A
310 
311 RETURN VALUE
312     The converted east/north/up velocity will be stored in the parameter of
313     enuVelocity.
314 
315 SIDE EFFECTS
316    N/A
317 ===========================================================================*/
318 void loc_convert_velocity_gnss_to_vrp(float enuVelocity[3], float rollPitchYaw[3],
319                                       float rollPitchYawRate[3], float leverArm[3]);
320 
321 #endif //_LOC_MISC_UTILS_H_
322