1 /* Copyright (c) 2011-2012, 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 
55 /* Logging Mechanism */
56 loc_logger_s_type loc_logger;
57 
58 /* Get names from value */
loc_get_name_from_mask(loc_name_val_s_type table[],int table_size,long mask)59 const char* loc_get_name_from_mask(loc_name_val_s_type table[], int table_size, long mask)
60 {
61    int i;
62    for (i = 0; i < table_size; i++)
63    {
64       if (table[i].val & (long) mask)
65       {
66          return table[i].name;
67       }
68    }
69    return UNKNOWN_STR;
70 }
71 
72 /* Get names from value */
loc_get_name_from_val(loc_name_val_s_type table[],int table_size,long value)73 const char* loc_get_name_from_val(loc_name_val_s_type table[], int table_size, long value)
74 {
75    int i;
76    for (i = 0; i < table_size; i++)
77    {
78       if (table[i].val == (long) value)
79       {
80          return table[i].name;
81       }
82    }
83    return UNKNOWN_STR;
84 }
85 
86 static loc_name_val_s_type loc_msg_q_status[] =
87 {
88     NAME_VAL( eMSG_Q_SUCCESS ),
89     NAME_VAL( eMSG_Q_FAILURE_GENERAL ),
90     NAME_VAL( eMSG_Q_INVALID_PARAMETER ),
91     NAME_VAL( eMSG_Q_INVALID_HANDLE ),
92     NAME_VAL( eMSG_Q_UNAVAILABLE_RESOURCE ),
93     NAME_VAL( eMSG_Q_INSUFFICIENT_BUFFER )
94 };
95 static int loc_msg_q_status_num = sizeof(loc_msg_q_status) / sizeof(loc_name_val_s_type);
96 
97 /* Find msg_q status name */
loc_get_msg_q_status(int status)98 const char* loc_get_msg_q_status(int status)
99 {
100    return loc_get_name_from_val(loc_msg_q_status, loc_msg_q_status_num, (long) status);
101 }
102 
log_succ_fail_string(int is_succ)103 const char* log_succ_fail_string(int is_succ)
104 {
105    return is_succ? "successful" : "failed";
106 }
107 
108 //Target names
109 loc_name_val_s_type target_name[] =
110 {
111     NAME_VAL(GNSS_NONE),
112     NAME_VAL(GNSS_MSM),
113     NAME_VAL(GNSS_GSS),
114     NAME_VAL(GNSS_MDM),
115     NAME_VAL(GNSS_QCA1530),
116     NAME_VAL(GNSS_UNKNOWN)
117 };
118 
119 static int target_name_num = sizeof(target_name)/sizeof(loc_name_val_s_type);
120 
121 /*===========================================================================
122 
123 FUNCTION loc_get_target_name
124 
125 DESCRIPTION
126    Returns pointer to a string that contains name of the target
127 
128    XX:XX:XX.000\0
129 
130 RETURN VALUE
131    The target name string
132 
133 ===========================================================================*/
loc_get_target_name(unsigned int target)134 const char *loc_get_target_name(unsigned int target)
135 {
136     int index = 0;
137     static char ret[BUFFER_SIZE];
138 
139     index =  getTargetGnssType(target);
140     if( index >= target_name_num || index < 0)
141         index = target_name_num - 1;
142 
143     if( (target & HAS_SSC) == HAS_SSC ) {
144         snprintf(ret, sizeof(ret), " %s with SSC",
145            loc_get_name_from_val(target_name, target_name_num, (long)index) );
146     }
147     else {
148        snprintf(ret, sizeof(ret), " %s  without SSC",
149            loc_get_name_from_val(target_name, target_name_num, (long)index) );
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,unsigned long buf_size)169 char *loc_get_time(char *time_string, unsigned long 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 /*===========================================================================
186 FUNCTION loc_logger_init
187 
188 DESCRIPTION
189    Initializes the state of DEBUG_LEVEL and TIMESTAMP
190 
191 DEPENDENCIES
192    N/A
193 
194 RETURN VALUE
195    None
196 
197 SIDE EFFECTS
198    N/A
199 ===========================================================================*/
loc_logger_init(unsigned long debug,unsigned long timestamp)200 void loc_logger_init(unsigned long debug, unsigned long timestamp)
201 {
202    loc_logger.DEBUG_LEVEL = debug;
203 #ifdef TARGET_BUILD_VARIANT_USER
204    // force user builds to 2 or less
205    if (loc_logger.DEBUG_LEVEL > 2) {
206        loc_logger.DEBUG_LEVEL = 2;
207    }
208 #endif
209    loc_logger.TIMESTAMP   = timestamp;
210 }
211 
212 
213 /*===========================================================================
214 FUNCTION get_timestamp
215 
216 DESCRIPTION
217    Generates a timestamp using the current system time
218 
219 DEPENDENCIES
220    N/A
221 
222 RETURN VALUE
223    Char pointer to the parameter str
224 
225 SIDE EFFECTS
226    N/A
227 ===========================================================================*/
get_timestamp(char * str,unsigned long buf_size)228 char * get_timestamp(char *str, unsigned long buf_size)
229 {
230   struct timeval tv;
231   struct timezone tz;
232   int hh, mm, ss;
233   gettimeofday(&tv, &tz);
234   hh = tv.tv_sec/3600%24;
235   mm = (tv.tv_sec%3600)/60;
236   ss = tv.tv_sec%60;
237   snprintf(str, buf_size, "%02d:%02d:%02d.%06ld", hh, mm, ss, tv.tv_usec);
238   return str;
239 }
240 
241