1 /* Copyright (c) 2011-2012, 2015, 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
30 #define LOG_NDEBUG 0
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include <sys/time.h>
36 #include "log_util.h"
37 #include "loc_log.h"
38 #include "msg_q.h"
39 #include <loc_pla.h>
40 #include "LogBuffer.h"
41
42 #define BUFFER_SIZE 120
43
44 // Logging Improvements
45 const char *loc_logger_boolStr[]={"False","True"};
46 const char VOID_RET[] = "None";
47 const char FROM_AFW[] = "===>";
48 const char TO_MODEM[] = "--->";
49 const char FROM_MODEM[] = "<---";
50 const char TO_AFW[] = "<===";
51 const char EXIT_TAG[] = "Exiting";
52 const char ENTRY_TAG[] = "Entering";
53 const char EXIT_ERROR_TAG[] = "Exiting with error";
54
55 int build_type_prop = BUILD_TYPE_PROP_NA;
56
57 const string gEmptyStr = "";
58 const string gUnknownStr = "UNKNOWN";
59 /* Logging Mechanism */
60 loc_logger_s_type loc_logger;
61
62 /* returns the least signification bit that is set in the mask
63 Param
64 mask - bit mask.
65 clearTheBit - if true, mask gets modified upon return.
66 returns 0 if mask is 0.
67 */
loc_get_least_bit(uint64_t & mask,bool clearTheBit)68 uint64_t loc_get_least_bit(uint64_t& mask, bool clearTheBit) {
69 uint64_t bit = 0;
70
71 if (mask > 0) {
72 uint64_t less1 = mask - 1;
73 bit = mask & ~(less1);
74 if (clearTheBit) {
75 mask &= less1;
76 }
77 }
78
79 return bit;
80 }
81
loc_get_bit_defs(uint64_t mask,const NameValTbl & tbl)82 string loc_get_bit_defs(uint64_t mask, const NameValTbl& tbl) {
83 string out;
84 while (mask > 0) {
85 out += loc_get_name_from_tbl(tbl, loc_get_least_bit(mask));
86 if (mask > 0) {
87 out += " | ";
88 }
89 }
90 return out;
91 }
92
93 DECLARE_TBL(loc_msg_q_status) =
94 {
95 NAME_VAL( eMSG_Q_SUCCESS ),
96 NAME_VAL( eMSG_Q_FAILURE_GENERAL ),
97 NAME_VAL( eMSG_Q_INVALID_PARAMETER ),
98 NAME_VAL( eMSG_Q_INVALID_HANDLE ),
99 NAME_VAL( eMSG_Q_UNAVAILABLE_RESOURCE ),
100 NAME_VAL( eMSG_Q_INSUFFICIENT_BUFFER )
101 };
102
103 /* Find msg_q status name */
loc_get_msg_q_status(int status)104 const char* loc_get_msg_q_status(int status)
105 {
106 return loc_get_name_from_val(loc_msg_q_status_tbl, (int64_t) status);
107 }
108
109 //Target names
110 DECLARE_TBL(target_name) =
111 {
112 NAME_VAL(GNSS_NONE),
113 NAME_VAL(GNSS_MSM),
114 NAME_VAL(GNSS_GSS),
115 NAME_VAL(GNSS_MDM),
116 NAME_VAL(GNSS_AUTO),
117 NAME_VAL(GNSS_UNKNOWN)
118 };
119
120 /*===========================================================================
121
122 FUNCTION loc_get_target_name
123
124 DESCRIPTION
125 Returns pointer to a string that contains name of the target
126
127 XX:XX:XX.000\0
128
129 RETURN VALUE
130 The target name string
131
132 ===========================================================================*/
loc_get_target_name(unsigned int target)133 const char *loc_get_target_name(unsigned int target)
134 {
135 int64_t index = 0;
136 static char ret[BUFFER_SIZE];
137
138 snprintf(ret, sizeof(ret), " %s with%s SSC",
139 loc_get_name_from_val(target_name_tbl, getTargetGnssType(target)),
140 ((target & HAS_SSC) == HAS_SSC) ? gEmptyStr.c_str() : "out");
141
142 return ret;
143 }
144
145
146 /*===========================================================================
147
148 FUNCTION loc_get_time
149
150 DESCRIPTION
151 Logs a callback event header.
152 The pointer time_string should point to a buffer of at least 13 bytes:
153
154 XX:XX:XX.000\0
155
156 RETURN VALUE
157 The time string
158
159 ===========================================================================*/
loc_get_time(char * time_string,size_t buf_size)160 char *loc_get_time(char *time_string, size_t buf_size)
161 {
162 struct timeval now; /* sec and usec */
163 struct tm now_tm; /* broken-down time */
164 char hms_string[80]; /* HH:MM:SS */
165
166 gettimeofday(&now, NULL);
167 localtime_r(&now.tv_sec, &now_tm);
168
169 strftime(hms_string, sizeof hms_string, "%H:%M:%S", &now_tm);
170 snprintf(time_string, buf_size, "%s.%03d", hms_string, (int) (now.tv_usec / 1000));
171
172 return time_string;
173 }
174
175 /*===========================================================================
176 FUNCTION get_timestamp
177
178 DESCRIPTION
179 Generates a timestamp using the current system time
180
181 DEPENDENCIES
182 N/A
183
184 RETURN VALUE
185 Char pointer to the parameter str
186
187 SIDE EFFECTS
188 N/A
189 ===========================================================================*/
get_timestamp(char * str,unsigned long buf_size)190 char * get_timestamp(char *str, unsigned long buf_size)
191 {
192 struct timeval tv;
193 struct timezone tz;
194 int hh, mm, ss;
195 gettimeofday(&tv, &tz);
196 hh = tv.tv_sec/3600%24;
197 mm = (tv.tv_sec%3600)/60;
198 ss = tv.tv_sec%60;
199 snprintf(str, buf_size, "%02d:%02d:%02d.%06ld", hh, mm, ss, tv.tv_usec);
200 return str;
201 }
202
203 /*===========================================================================
204
205 FUNCTION log_buffer_insert
206
207 DESCRIPTION
208 Insert a log sentence with specific level to the log buffer.
209
210 RETURN VALUE
211 N/A
212
213 ===========================================================================*/
log_buffer_insert(char * str,unsigned long buf_size,int level)214 void log_buffer_insert(char *str, unsigned long buf_size, int level)
215 {
216 timespec tv;
217 clock_gettime(CLOCK_BOOTTIME, &tv);
218 uint64_t elapsedTime = (uint64_t)tv.tv_sec + (uint64_t)tv.tv_nsec/1000000000;
219 string ss = str;
220 loc_util::LogBuffer::getInstance()->append(ss, level, elapsedTime);
221 }
222