1 /*
2  * Copyright 2012-2018, 2023 NXP
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_TAG "NxpUwbHal"
18 #include "phNxpLog.h"
19 #include "phNxpConfig.h"
20 #include <cutils/properties.h>
21 #include <log/log.h>
22 #include <stdio.h>
23 #include <string.h>
24 
25 const char* NXPLOG_ITEM_EXTNS = "NxpExtns";
26 const char* NXPLOG_ITEM_UCIHAL = "NxpUwbHal";
27 const char* NXPLOG_ITEM_UCIX = "NxpUciX";
28 const char* NXPLOG_ITEM_UCIR = "NxpUciR";
29 const char* NXPLOG_ITEM_FWDNLD = "NxpFwDnld";
30 const char* NXPLOG_ITEM_TML = "NxpUwbTml";
31 
32 #ifdef NXP_HCI_REQ
33 const char* NXPLOG_ITEM_HCPX = "NxpHcpX";
34 const char* NXPLOG_ITEM_HCPR = "NxpHcpR";
35 #endif /*NXP_HCI_REQ*/
36 
37 /* global log level structure */
38 uci_log_level_t gLog_level;
39 
40 /*******************************************************************************
41  *
42  * Function         phNxpLog_SetGlobalLogLevel
43  *
44  * Description      Sets the global log level for all modules.
45  *                  This value is set by Android property
46  *uwb.nxp_log_level_global.
47  *                  If value can be overridden by module log level.
48  *
49  * Returns          The value of global log level
50  *
51  ******************************************************************************/
phNxpLog_SetGlobalLogLevel(void)52 static uint8_t phNxpLog_SetGlobalLogLevel(void) {
53   uint8_t level = NXPLOG_DEFAULT_LOGLEVEL;
54   unsigned long num = 0;
55   char valueStr[PROPERTY_VALUE_MAX] = {0};
56 
57   int len = property_get(PROP_NAME_NXPLOG_GLOBAL_LOGLEVEL, valueStr, "");
58   if (len > 0) {
59     // let Android property override .conf variable
60     sscanf(valueStr, "%lu", &num);
61     level = (unsigned char)num;
62   }
63   memset(&gLog_level, level, sizeof(uci_log_level_t));
64   return level;
65 }
66 
67 /*******************************************************************************
68  *
69  * Function         phNxpLog_SetHALLogLevel
70  *
71  * Description      Sets the HAL layer log level.
72  *
73  * Returns          void
74  *
75  ******************************************************************************/
phNxpLog_SetHALLogLevel(uint8_t level)76 static void phNxpLog_SetHALLogLevel(uint8_t level) {
77   unsigned long num = 0;
78   int len;
79   char valueStr[PROPERTY_VALUE_MAX] = {0};
80 
81   if (NxpConfig_GetNum(NAME_NXPLOG_HAL_LOGLEVEL, &num, sizeof(num))) {
82     gLog_level.hal_log_level =
83         (level > (unsigned char)num) ? level : (unsigned char)num;
84     ;
85   }
86 
87   len = property_get(PROP_NAME_NXPLOG_HAL_LOGLEVEL, valueStr, "");
88   if (len > 0) {
89     /* let Android property override .conf variable */
90     sscanf(valueStr, "%lu", &num);
91 
92     gLog_level.hal_log_level = (unsigned char)num;
93   }
94 }
95 
96 /*******************************************************************************
97  *
98  * Function         phNxpLog_SetExtnsLogLevel
99  *
100  * Description      Sets the Extensions layer log level.
101  *
102  * Returns          void
103  *
104  ******************************************************************************/
phNxpLog_SetExtnsLogLevel(uint8_t level)105 static void phNxpLog_SetExtnsLogLevel(uint8_t level) {
106   unsigned long num = 0;
107   int len;
108   char valueStr[PROPERTY_VALUE_MAX] = {0};
109   if (NxpConfig_GetNum(NAME_NXPLOG_EXTNS_LOGLEVEL, &num, sizeof(num))) {
110     gLog_level.extns_log_level =
111         (level > (unsigned char)num) ? level : (unsigned char)num;
112     ;
113   }
114 
115   len = property_get(PROP_NAME_NXPLOG_EXTNS_LOGLEVEL, valueStr, "");
116   if (len > 0) {
117     /* let Android property override .conf variable */
118     sscanf(valueStr, "%lu", &num);
119     gLog_level.extns_log_level = (unsigned char)num;
120   }
121 }
122 
123 /*******************************************************************************
124  *
125  * Function         phNxpLog_SetTmlLogLevel
126  *
127  * Description      Sets the Tml layer log level.
128  *
129  * Returns          void
130  *
131  ******************************************************************************/
phNxpLog_SetTmlLogLevel(uint8_t level)132 static void phNxpLog_SetTmlLogLevel(uint8_t level) {
133   unsigned long num = 0;
134   int len;
135   char valueStr[PROPERTY_VALUE_MAX] = {0};
136   if (NxpConfig_GetNum(NAME_NXPLOG_TML_LOGLEVEL, &num, sizeof(num))) {
137     gLog_level.tml_log_level =
138         (level > (unsigned char)num) ? level : (unsigned char)num;
139     ;
140   }
141 
142   len = property_get(PROP_NAME_NXPLOG_TML_LOGLEVEL, valueStr, "");
143   if (len > 0) {
144     /* let Android property override .conf variable */
145     sscanf(valueStr, "%lu", &num);
146     gLog_level.tml_log_level = (unsigned char)num;
147   }
148 }
149 
150 /*******************************************************************************
151  *
152  * Function         phNxpLog_SetDnldLogLevel
153  *
154  * Description      Sets the FW download layer log level.
155  *
156  * Returns          void
157  *
158  ******************************************************************************/
phNxpLog_SetDnldLogLevel(uint8_t level)159 static void phNxpLog_SetDnldLogLevel(uint8_t level) {
160   unsigned long num = 0;
161   int len;
162   char valueStr[PROPERTY_VALUE_MAX] = {0};
163   if (NxpConfig_GetNum(NAME_NXPLOG_FWDNLD_LOGLEVEL, &num, sizeof(num))) {
164     gLog_level.dnld_log_level =
165         (level > (unsigned char)num) ? level : (unsigned char)num;
166     ;
167   }
168 
169   len = property_get(PROP_NAME_NXPLOG_FWDNLD_LOGLEVEL, valueStr, "");
170   if (len > 0) {
171     /* let Android property override .conf variable */
172     sscanf(valueStr, "%lu", &num);
173     gLog_level.dnld_log_level = (unsigned char)num;
174   }
175 }
176 
177 /*******************************************************************************
178  *
179  * Function         phNxpLog_SetUciTxLogLevel
180  *
181  * Description      Sets the UCI transaction layer log level.
182  *
183  * Returns          void
184  *
185  ******************************************************************************/
phNxpLog_SetUciTxLogLevel(uint8_t level)186 static void phNxpLog_SetUciTxLogLevel(uint8_t level) {
187   unsigned long num = 0;
188   int len;
189   char valueStr[PROPERTY_VALUE_MAX] = {0};
190   if (NxpConfig_GetNum(NAME_NXPLOG_UCIX_LOGLEVEL, &num, sizeof(num))) {
191     gLog_level.ucix_log_level =
192         (level > (unsigned char)num) ? level : (unsigned char)num;
193   }
194   if (NxpConfig_GetNum(NAME_NXPLOG_UCIR_LOGLEVEL, &num, sizeof(num))) {
195     gLog_level.ucir_log_level =
196         (level > (unsigned char)num) ? level : (unsigned char)num;
197     ;
198   }
199 
200   len = property_get(PROP_NAME_NXPLOG_UCI_LOGLEVEL, valueStr, "");
201   if (len > 0) {
202     /* let Android property override .conf variable */
203     sscanf(valueStr, "%lu", &num);
204     gLog_level.ucix_log_level = (unsigned char)num;
205     gLog_level.ucir_log_level = (unsigned char)num;
206   }
207 }
208 
209 /******************************************************************************
210  * Function         phNxpLog_InitializeLogLevel
211  *
212  * Description      Initialize and get log level of module from libuwb-nxp.conf
213  *or
214  *                  Android runtime properties.
215  *                  The Android property uwb.nxp_global_log_level is to
216  *                  define log level for all modules. Modules log level will
217  *overwide global level.
218  *                  The Android property will overwide the level
219  *                  in libuwb-nxp.conf
220  *
221  *                  Android property names:
222  *                      uwb.nxp_log_level_global    * defines log level for all
223  *modules
224  *                      uwb.nxp_log_level_extns     * extensions module log
225  *                      uwb.nxp_log_level_hal       * Hal module log
226  *                      uwb.nxp_log_level_dnld      * firmware download module
227  *log
228  *                      uwb.nxp_log_level_tml       * TML module log
229  *                      uwb.nxp_log_level_uci       * UCI transaction log
230  *
231  *                  Log Level values:
232  *                      NXPLOG_LOG_SILENT_LOGLEVEL  0        * No trace to show
233  *                      NXPLOG_LOG_ERROR_LOGLEVEL   1        * Show Error trace
234  *only
235  *                      NXPLOG_LOG_WARN_LOGLEVEL    2        * Show Warning
236  *trace and Error trace
237  *                      NXPLOG_LOG_DEBUG_LOGLEVEL   3        * Show all traces
238  *
239  * Returns          void
240  *
241  ******************************************************************************/
phNxpLog_InitializeLogLevel(void)242 void phNxpLog_InitializeLogLevel(void) {
243   uint8_t level = phNxpLog_SetGlobalLogLevel();
244   phNxpLog_SetHALLogLevel(level);
245   phNxpLog_SetExtnsLogLevel(level);
246   phNxpLog_SetTmlLogLevel(level);
247   phNxpLog_SetDnldLogLevel(level);
248   phNxpLog_SetUciTxLogLevel(level);
249 
250   ALOGV("%s: global =%u, Fwdnld =%u, extns =%u, \
251                 hal =%u, tml =%u, ucir =%u, \
252                 ucix =%u",
253            __func__, gLog_level.global_log_level, gLog_level.dnld_log_level,
254            gLog_level.extns_log_level, gLog_level.hal_log_level,
255            gLog_level.tml_log_level, gLog_level.ucir_log_level,
256            gLog_level.ucix_log_level);
257 }
258