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