• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <time.h>
36 #include "loc_log.h"
37 #include "msg_q.h"
38 #include <platform_lib_includes.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_QCA1530),
114     NAME_VAL(GNSS_AUTO),
115     NAME_VAL(GNSS_UNKNOWN)
116 };
117 
118 static const size_t target_name_num = LOC_TABLE_SIZE(target_name);
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     int index = 0;
136     static char ret[BUFFER_SIZE];
137 
138     index =  getTargetGnssType(target);
139     if( index < 0 || (unsigned)index >= target_name_num )
140         index = target_name_num - 1;
141 
142     if( (target & HAS_SSC) == HAS_SSC ) {
143         snprintf(ret, sizeof(ret), " %s with SSC",
144            loc_get_name_from_val(target_name, target_name_num, (long)index) );
145     }
146     else {
147        snprintf(ret, sizeof(ret), " %s  without SSC",
148            loc_get_name_from_val(target_name, target_name_num, (long)index) );
149     }
150     return ret;
151 }
152 
153 
154 /*===========================================================================
155 
156 FUNCTION loc_get_time
157 
158 DESCRIPTION
159    Logs a callback event header.
160    The pointer time_string should point to a buffer of at least 13 bytes:
161 
162    XX:XX:XX.000\0
163 
164 RETURN VALUE
165    The time string
166 
167 ===========================================================================*/
loc_get_time(char * time_string,size_t buf_size)168 char *loc_get_time(char *time_string, size_t buf_size)
169 {
170    struct timeval now;     /* sec and usec     */
171    struct tm now_tm;       /* broken-down time */
172    char hms_string[80];    /* HH:MM:SS         */
173 
174    gettimeofday(&now, NULL);
175    localtime_r(&now.tv_sec, &now_tm);
176 
177    strftime(hms_string, sizeof hms_string, "%H:%M:%S", &now_tm);
178    snprintf(time_string, buf_size, "%s.%03d", hms_string, (int) (now.tv_usec / 1000));
179 
180    return time_string;
181 }
182 
183 
184 /*===========================================================================
185 FUNCTION loc_logger_init
186 
187 DESCRIPTION
188    Initializes the state of DEBUG_LEVEL and TIMESTAMP
189 
190 DEPENDENCIES
191    N/A
192 
193 RETURN VALUE
194    None
195 
196 SIDE EFFECTS
197    N/A
198 ===========================================================================*/
loc_logger_init(unsigned long debug,unsigned long timestamp)199 void loc_logger_init(unsigned long debug, unsigned long timestamp)
200 {
201    loc_logger.DEBUG_LEVEL = debug;
202 #ifdef TARGET_BUILD_VARIANT_USER
203    // force user builds to 2 or less
204    if (loc_logger.DEBUG_LEVEL > 2) {
205        loc_logger.DEBUG_LEVEL = 2;
206    }
207 #endif
208    loc_logger.TIMESTAMP   = timestamp;
209 }
210 
211 
212 /*===========================================================================
213 FUNCTION get_timestamp
214 
215 DESCRIPTION
216    Generates a timestamp using the current system time
217 
218 DEPENDENCIES
219    N/A
220 
221 RETURN VALUE
222    Char pointer to the parameter str
223 
224 SIDE EFFECTS
225    N/A
226 ===========================================================================*/
get_timestamp(char * str,unsigned long buf_size)227 char * get_timestamp(char *str, unsigned long buf_size)
228 {
229   struct timeval tv;
230   struct timezone tz;
231   int hh, mm, ss;
232   gettimeofday(&tv, &tz);
233   hh = tv.tv_sec/3600%24;
234   mm = (tv.tv_sec%3600)/60;
235   ss = tv.tv_sec%60;
236   snprintf(str, buf_size, "%02d:%02d:%02d.%06ld", hh, mm, ss, tv.tv_usec);
237   return str;
238 }
239 
240