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