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