1 /* Copyright (c) 2011-2012, 2015, 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 <sys/time.h>
35 #include "log_util.h"
36 #include "loc_log.h"
37 #include "msg_q.h"
38 #include <loc_pla.h>
39
40 #define BUFFER_SIZE 120
41
42 // Logging Improvements
43 const char *loc_logger_boolStr[]={"False","True"};
44 const char VOID_RET[] = "None";
45 const char FROM_AFW[] = "===>";
46 const char TO_MODEM[] = "--->";
47 const char FROM_MODEM[] = "<---";
48 const char TO_AFW[] = "<===";
49 const char EXIT_TAG[] = "Exiting";
50 const char ENTRY_TAG[] = "Entering";
51 const char EXIT_ERROR_TAG[] = "Exiting with error";
52
53 /* Logging Mechanism */
54 loc_logger_s_type loc_logger;
55
56 /* Get names from value */
loc_get_name_from_mask(const loc_name_val_s_type table[],size_t table_size,long mask)57 const char* loc_get_name_from_mask(const loc_name_val_s_type table[], size_t table_size, long mask)
58 {
59 size_t i;
60 for (i = 0; i < table_size; i++)
61 {
62 if (table[i].val & (long) mask)
63 {
64 return table[i].name;
65 }
66 }
67 return UNKNOWN_STR;
68 }
69
70 /* Get names from value */
loc_get_name_from_val(const loc_name_val_s_type table[],size_t table_size,long value)71 const char* loc_get_name_from_val(const loc_name_val_s_type table[], size_t table_size, long value)
72 {
73 size_t i;
74 for (i = 0; i < table_size; i++)
75 {
76 if (table[i].val == (long) value)
77 {
78 return table[i].name;
79 }
80 }
81 return UNKNOWN_STR;
82 }
83
84 static const loc_name_val_s_type loc_msg_q_status[] =
85 {
86 NAME_VAL( eMSG_Q_SUCCESS ),
87 NAME_VAL( eMSG_Q_FAILURE_GENERAL ),
88 NAME_VAL( eMSG_Q_INVALID_PARAMETER ),
89 NAME_VAL( eMSG_Q_INVALID_HANDLE ),
90 NAME_VAL( eMSG_Q_UNAVAILABLE_RESOURCE ),
91 NAME_VAL( eMSG_Q_INSUFFICIENT_BUFFER )
92 };
93 static const size_t loc_msg_q_status_num = LOC_TABLE_SIZE(loc_msg_q_status);
94
95 /* Find msg_q status name */
loc_get_msg_q_status(int status)96 const char* loc_get_msg_q_status(int status)
97 {
98 return loc_get_name_from_val(loc_msg_q_status, loc_msg_q_status_num, (long) status);
99 }
100
log_succ_fail_string(int is_succ)101 const char* log_succ_fail_string(int is_succ)
102 {
103 return is_succ? "successful" : "failed";
104 }
105
106 //Target names
107 static const loc_name_val_s_type target_name[] =
108 {
109 NAME_VAL(GNSS_NONE),
110 NAME_VAL(GNSS_MSM),
111 NAME_VAL(GNSS_GSS),
112 NAME_VAL(GNSS_MDM),
113 NAME_VAL(GNSS_AUTO),
114 NAME_VAL(GNSS_UNKNOWN)
115 };
116
117 static const size_t target_name_num = LOC_TABLE_SIZE(target_name);
118
119 /*===========================================================================
120
121 FUNCTION loc_get_target_name
122
123 DESCRIPTION
124 Returns pointer to a string that contains name of the target
125
126 XX:XX:XX.000\0
127
128 RETURN VALUE
129 The target name string
130
131 ===========================================================================*/
loc_get_target_name(unsigned int target)132 const char *loc_get_target_name(unsigned int target)
133 {
134 int index = 0;
135 static char ret[BUFFER_SIZE];
136
137 index = getTargetGnssType(target);
138 if( index < 0 || (unsigned)index >= target_name_num )
139 index = target_name_num - 1;
140
141 if( (target & HAS_SSC) == HAS_SSC ) {
142 snprintf(ret, sizeof(ret), " %s with SSC",
143 loc_get_name_from_val(target_name, target_name_num, (long)index) );
144 }
145 else {
146 snprintf(ret, sizeof(ret), " %s without SSC",
147 loc_get_name_from_val(target_name, target_name_num, (long)index) );
148 }
149 return ret;
150 }
151
152
153 /*===========================================================================
154
155 FUNCTION loc_get_time
156
157 DESCRIPTION
158 Logs a callback event header.
159 The pointer time_string should point to a buffer of at least 13 bytes:
160
161 XX:XX:XX.000\0
162
163 RETURN VALUE
164 The time string
165
166 ===========================================================================*/
loc_get_time(char * time_string,size_t buf_size)167 char *loc_get_time(char *time_string, size_t buf_size)
168 {
169 struct timeval now; /* sec and usec */
170 struct tm now_tm; /* broken-down time */
171 char hms_string[80]; /* HH:MM:SS */
172
173 gettimeofday(&now, NULL);
174 localtime_r(&now.tv_sec, &now_tm);
175
176 strftime(hms_string, sizeof hms_string, "%H:%M:%S", &now_tm);
177 snprintf(time_string, buf_size, "%s.%03d", hms_string, (int) (now.tv_usec / 1000));
178
179 return time_string;
180 }
181
182
183 /*===========================================================================
184 FUNCTION loc_logger_init
185
186 DESCRIPTION
187 Initializes the state of DEBUG_LEVEL and TIMESTAMP
188
189 DEPENDENCIES
190 N/A
191
192 RETURN VALUE
193 None
194
195 SIDE EFFECTS
196 N/A
197 ===========================================================================*/
loc_logger_init(unsigned long debug,unsigned long timestamp)198 void loc_logger_init(unsigned long debug, unsigned long timestamp)
199 {
200 loc_logger.DEBUG_LEVEL = debug;
201 #ifdef TARGET_BUILD_VARIANT_USER
202 // force user builds to 2 or less
203 if (loc_logger.DEBUG_LEVEL > 2) {
204 loc_logger.DEBUG_LEVEL = 2;
205 }
206 #endif
207 loc_logger.TIMESTAMP = timestamp;
208 }
209
210
211 /*===========================================================================
212 FUNCTION get_timestamp
213
214 DESCRIPTION
215 Generates a timestamp using the current system time
216
217 DEPENDENCIES
218 N/A
219
220 RETURN VALUE
221 Char pointer to the parameter str
222
223 SIDE EFFECTS
224 N/A
225 ===========================================================================*/
get_timestamp(char * str,unsigned long buf_size)226 char * get_timestamp(char *str, unsigned long buf_size)
227 {
228 struct timeval tv;
229 struct timezone tz;
230 int hh, mm, ss;
231 gettimeofday(&tv, &tz);
232 hh = tv.tv_sec/3600%24;
233 mm = (tv.tv_sec%3600)/60;
234 ss = tv.tv_sec%60;
235 snprintf(str, buf_size, "%02d:%02d:%02d.%06ld", hh, mm, ss, tv.tv_usec);
236 return str;
237 }
238
239