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 #ifdef __cplusplus
147 }
148 #endif
149
150 using std::hex;
151 using std::string;
152 using std::stringstream;
153
154 /*===========================================================================
155 FUNCTION to_string_hex
156
157 DESCRIPTION
158 This function works similar to std::to_string, but puts only in hex format.
159
160 DEPENDENCIES
161 N/A
162
163 RETURN VALUE
164 string, of input val in hex format
165
166 SIDE EFFECTS
167 N/A
168 ===========================================================================*/
169 template <typename T>
to_string_hex(T val)170 string to_string_hex(T val) {
171 stringstream ss;
172 if (val < 0) {
173 val = -val;
174 ss << "-";
175 }
176 ss << hex << "0x" << val;
177 return ss.str();
178 }
179
180 /*===========================================================================
181 FUNCTION loc_prim_arr_to_string
182
183 DESCRIPTION
184 This function puts out primitive array in DEC or EHX format.
185
186 DEPENDENCIES
187 N/A
188
189 RETURN VALUE
190 string, space separated string of values in the input array, either
191 in decimal or hex format, depending on the value of decIfTrue
192
193 SIDE EFFECTS
194 N/A
195 ===========================================================================*/
196 template <typename T>
197 static string loc_prim_arr_to_string(T* arr, uint32_t size, bool decIfTrue = true) {
198 stringstream ss;
199 for (uint32_t i = 0; i < size; i++) {
200 ss << (decIfTrue ? to_string(arr[i]) : to_string_hex(arr[i]));
201 if (i != size - 1) {
202 ss << " ";
203 }
204 }
205 return ss.str();
206 }
207
208 /*===========================================================================
209 FUNCTION qTimerTicksToNanos
210
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
216 DEPENDENCIES
217 N/A
218
219 RETURN VALUE
220 Qtimer value in nanoseconds
221
222 SIDE EFFECTS
223 N/A
224 ===========================================================================*/
qTimerTicksToNanos(double qTimer)225 inline uint64_t qTimerTicksToNanos(double qTimer) {
226 return (uint64_t((qTimer * double(10000ull)) / (double)192ull));
227 }
228
229 #endif //_LOC_MISC_UTILS_H_
230