• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 #include <stddef.h>
36 #include <stdint.h>
37 /*===========================================================================
38 FUNCTION loc_split_string
39 
40 DESCRIPTION:
41     This function is used to split a delimiter separated string into
42     sub-strings. This function does not allocate new memory to store the split
43     strings. Instead, it places '\0' in places of delimiters and assings the
44     starting address of the substring within the raw string as the string address
45     The input raw_string no longer remains to be a collection of sub-strings
46     after this function is executed.
47     Please make a copy of the input string before calling this function if
48     necessary
49 
50 PARAMETERS:
51     char *raw_string: is the original string with delimiter separated substrings
52     char **split_strings_ptr: is the arraw of pointers which will hold the addresses
53                               of individual substrings
54     int max_num_substrings: is the maximum number of substrings that are expected
55                             by the caller. The array of pointers in the above parameter
56                             is usually this long
57     char delimiter: is the delimiter that separates the substrings. Examples: ' ', ';'
58 
59 DEPENDENCIES
60     N/A
61 
62 RETURN VALUE
63     int Number of split strings
64 
65 SIDE EFFECTS
66     The input raw_string no longer remains a delimiter separated single string.
67 
68 EXAMPLE
69     delimiter = ' ' //space
70     raw_string = "hello new user" //delimiter is space ' '
71     addresses  =  0123456789abcd
72     split_strings_ptr[0] = &raw_string[0]; //split_strings_ptr[0] contains "hello"
73     split_strings_ptr[1] = &raw_string[6]; //split_strings_ptr[1] contains "new"
74     split_strings_ptr[2] = &raw_string[a]; //split_strings_ptr[2] contains "user"
75 
76 ===========================================================================*/
77 int loc_util_split_string(char *raw_string, char **split_strings_ptr, int max_num_substrings,
78                      char delimiter);
79 
80 /*===========================================================================
81 FUNCTION trim_space
82 
83 DESCRIPTION
84    Removes leading and trailing spaces of the string
85 
86 DEPENDENCIES
87    N/A
88 
89 RETURN VALUE
90    None
91 
92 SIDE EFFECTS
93    N/A
94 ===========================================================================*/
95 void loc_util_trim_space(char *org_string);
96 
97 /*===========================================================================
98 FUNCTION dlGetSymFromLib
99 
100 DESCRIPTION
101    Handy function to get a pointer to a symbol from a library.
102 
103    If libHandle is not null, it will be used as the handle to the library. In
104    that case libName wll not be used;
105    libHandle is an in / out parameter.
106    If libHandle is null, libName will be used to dlopen.
107    Either libHandle or libName must not be nullptr.
108    symName must not be null.
109 
110 DEPENDENCIES
111    N/A
112 
113 RETURN VALUE
114    pointer to symName. Could be nullptr if
115        Parameters are incorrect; or
116        libName can not be opened; or
117        symName can not be found.
118 
119 SIDE EFFECTS
120    N/A
121 ===========================================================================*/
122 void* dlGetSymFromLib(void*& libHandle, const char* libName, const char* symName);
123 
124 /*===========================================================================
125 FUNCTION getQTimerTickCount
126 
127 DESCRIPTION
128    This function is used to read the QTimer ticks count. This value is globally maintained and
129    must be the same across all processors on a target.
130 
131 DEPENDENCIES
132    N/A
133 
134 RETURN VALUE
135     uint64_t QTimer tick count
136 
137 SIDE EFFECTS
138    N/A
139 ===========================================================================*/
140 
141 
142 uint64_t getQTimerTickCount();
143 
144 
145 #ifdef __cplusplus
146 }
147 #endif
148 
149 /*===========================================================================
150 FUNCTION qTimerTicksToNanos
151 
152 DESCRIPTION
153     Transform from ticks to nanoseconds, clock is 19.2 MHz
154     so the formula would be qtimer(ns) = (ticks * 1000000000) / 19200000
155     or simplified qtimer(ns) = (ticks * 10000) / 192.
156 
157 DEPENDENCIES
158     N/A
159 
160 RETURN VALUE
161     Qtimer value in nanoseconds
162 
163 SIDE EFFECTS
164     N/A
165 ===========================================================================*/
qTimerTicksToNanos(double qTimer)166 inline uint64_t qTimerTicksToNanos(double qTimer) {
167     return (uint64_t((qTimer * double(10000ull)) / (double)192ull));
168 }
169 
170 #endif //_LOC_MISC_UTILS_H_
171