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 #include <unordered_map>
42 #include <fstream>
43 #include <algorithm>
44 #include <string>
45 #include <cctype>
46 #define  BUFFER_SIZE  120
47 #define  LOG_TAG_LEVEL_CONF_FILE_PATH "/data/vendor/location/gps.prop"
48 
49 // Logging Improvements
50 const char *loc_logger_boolStr[]={"False","True"};
51 const char VOID_RET[]   = "None";
52 const char FROM_AFW[]   = "===>";
53 const char TO_MODEM[]   = "--->";
54 const char FROM_MODEM[] = "<---";
55 const char TO_AFW[]     = "<===";
56 const char EXIT_TAG[]   = "Exiting";
57 const char ENTRY_TAG[]  = "Entering";
58 const char EXIT_ERROR_TAG[]  = "Exiting with error";
59 
60 int build_type_prop = BUILD_TYPE_PROP_NA;
61 
62 const string gEmptyStr = "";
63 const string gUnknownStr = "UNKNOWN";
64 /* Logging Mechanism */
65 loc_logger_s_type loc_logger;
66 
67 /* tag base logging control map*/
68 static std::unordered_map<std::string, uint8_t> tag_level_map;
69 static bool tag_map_inited = false;
70 
71 /* returns the least signification bit that is set in the mask
72    Param
73       mask -        bit mask.
74       clearTheBit - if true, mask gets modified upon return.
75    returns 0 if mask is 0.
76 */
loc_get_least_bit(uint64_t & mask,bool clearTheBit)77 uint64_t loc_get_least_bit(uint64_t& mask, bool clearTheBit) {
78     uint64_t bit = 0;
79 
80     if (mask > 0) {
81         uint64_t less1 = mask - 1;
82         bit = mask & ~(less1);
83         if (clearTheBit) {
84             mask &= less1;
85         }
86     }
87 
88     return bit;
89 }
90 
loc_get_bit_defs(uint64_t mask,const NameValTbl & tbl)91 string loc_get_bit_defs(uint64_t mask, const NameValTbl& tbl) {
92     string out;
93     while (mask > 0) {
94         out += loc_get_name_from_tbl(tbl, loc_get_least_bit(mask));
95         if (mask > 0) {
96             out += " | ";
97         }
98     }
99     return out;
100 }
101 
102 DECLARE_TBL(loc_msg_q_status) =
103 {
104     NAME_VAL( eMSG_Q_SUCCESS ),
105     NAME_VAL( eMSG_Q_FAILURE_GENERAL ),
106     NAME_VAL( eMSG_Q_INVALID_PARAMETER ),
107     NAME_VAL( eMSG_Q_INVALID_HANDLE ),
108     NAME_VAL( eMSG_Q_UNAVAILABLE_RESOURCE ),
109     NAME_VAL( eMSG_Q_INSUFFICIENT_BUFFER )
110 };
111 
112 /* Find msg_q status name */
loc_get_msg_q_status(int status)113 const char* loc_get_msg_q_status(int status)
114 {
115    return loc_get_name_from_val(loc_msg_q_status_tbl, (int64_t) status);
116 }
117 
118 //Target names
119 DECLARE_TBL(target_name) =
120 {
121     NAME_VAL(GNSS_NONE),
122     NAME_VAL(GNSS_MSM),
123     NAME_VAL(GNSS_GSS),
124     NAME_VAL(GNSS_MDM),
125     NAME_VAL(GNSS_AUTO),
126     NAME_VAL(GNSS_UNKNOWN)
127 };
128 
129 /*===========================================================================
130 
131 FUNCTION loc_get_target_name
132 
133 DESCRIPTION
134    Returns pointer to a string that contains name of the target
135 
136    XX:XX:XX.000\0
137 
138 RETURN VALUE
139    The target name string
140 
141 ===========================================================================*/
loc_get_target_name(unsigned int target)142 const char *loc_get_target_name(unsigned int target)
143 {
144     int64_t index = 0;
145     static char ret[BUFFER_SIZE];
146 
147     snprintf(ret, sizeof(ret), " %s with%s SSC",
148              loc_get_name_from_val(target_name_tbl, getTargetGnssType(target)),
149              ((target & HAS_SSC) == HAS_SSC) ? gEmptyStr.c_str() : "out");
150 
151     return ret;
152 }
153 
154 
155 /*===========================================================================
156 
157 FUNCTION loc_get_time
158 
159 DESCRIPTION
160    Logs a callback event header.
161    The pointer time_string should point to a buffer of at least 13 bytes:
162 
163    XX:XX:XX.000\0
164 
165 RETURN VALUE
166    The time string
167 
168 ===========================================================================*/
loc_get_time(char * time_string,size_t buf_size)169 char *loc_get_time(char *time_string, size_t buf_size)
170 {
171    struct timeval now;     /* sec and usec     */
172    struct tm now_tm;       /* broken-down time */
173    char hms_string[80];    /* HH:MM:SS         */
174 
175    gettimeofday(&now, NULL);
176    localtime_r(&now.tv_sec, &now_tm);
177 
178    strftime(hms_string, sizeof hms_string, "%H:%M:%S", &now_tm);
179    snprintf(time_string, buf_size, "%s.%03d", hms_string, (int) (now.tv_usec / 1000));
180 
181    return time_string;
182 }
183 
184 /*===========================================================================
185 FUNCTION get_timestamp
186 
187 DESCRIPTION
188    Generates a timestamp using the current system time
189 
190 DEPENDENCIES
191    N/A
192 
193 RETURN VALUE
194    Char pointer to the parameter str
195 
196 SIDE EFFECTS
197    N/A
198 ===========================================================================*/
get_timestamp(char * str,unsigned long buf_size)199 char * get_timestamp(char *str, unsigned long buf_size)
200 {
201   struct timeval tv;
202   struct timezone tz;
203   int hh, mm, ss;
204   gettimeofday(&tv, &tz);
205   hh = tv.tv_sec/3600%24;
206   mm = (tv.tv_sec%3600)/60;
207   ss = tv.tv_sec%60;
208   snprintf(str, buf_size, "%02d:%02d:%02d.%06ld", hh, mm, ss, tv.tv_usec);
209   return str;
210 }
211 
212 /*===========================================================================
213 
214 FUNCTION log_buffer_insert
215 
216 DESCRIPTION
217    Insert a log sentence with specific level to the log buffer.
218 
219 RETURN VALUE
220    N/A
221 
222 ===========================================================================*/
log_buffer_insert(char * str,unsigned long buf_size,int level)223 void log_buffer_insert(char *str, unsigned long buf_size, int level)
224 {
225     timespec tv;
226     clock_gettime(CLOCK_BOOTTIME, &tv);
227     uint64_t elapsedTime = (uint64_t)tv.tv_sec + (uint64_t)tv.tv_nsec/1000000000;
228     string ss = str;
229     loc_util::LogBuffer::getInstance()->append(ss, level, elapsedTime);
230 }
231 
log_tag_level_map_init()232 void log_tag_level_map_init()
233 {
234     if (tag_map_inited) {
235         return;
236     }
237 
238     std::string filename = LOG_TAG_LEVEL_CONF_FILE_PATH;
239 
240     std::ifstream s(filename);
241     if (!s.is_open()) {
242         ALOGE("cannot open file:%s", LOG_TAG_LEVEL_CONF_FILE_PATH);
243     } else {
244         std::string line;
245         while (std::getline(s, line)) {
246             line.erase(std::remove(line.begin(), line.end(), ' '), line.end());
247             int pos = line.find('=');
248             if (pos <= 0 || pos >= (line.size() - 1)) {
249                 ALOGE("wrong format in gps.prop");
250                 continue;
251             }
252             std::string tag = line.substr(0, pos);
253             std::string level = line.substr(pos+1, 1);
254             if (!std::isdigit(*(level.begin()))) {
255                 ALOGE("wrong format in gps.prop");
256                 continue;
257             }
258             tag_level_map[tag] = (uint8_t)std::stoul(level);
259         }
260     }
261     tag_map_inited = true;
262 }
263 
get_tag_log_level(const char * tag)264 int get_tag_log_level(const char* tag)
265 {
266     if (!tag_map_inited) {
267         return -1;
268     }
269 
270     // in case LOG_TAG isn't defined in a source file, use the global log level
271     if (tag == NULL) {
272         return loc_logger.DEBUG_LEVEL;
273     }
274     int log_level;
275     auto search = tag_level_map.find(std::string(tag));
276     if (tag_level_map.end() != search) {
277         log_level = search->second;
278     } else {
279         log_level = loc_logger.DEBUG_LEVEL;
280     }
281     return log_level;
282 }
283