1 /*
2 * Copyright 2010-2019, 2022-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 #define LOG_TAG "NxpNfcHal"
17 #include <stdio.h>
18 #include <string.h>
19 #if !defined(NXPLOG__H_INCLUDED)
20 #include "phNxpConfig.h"
21 #include "phNxpLog.h"
22 #endif
23 #include <log/log.h>
24 #include "phNxpNciHal_IoctlOperations.h"
25
26 const char* NXPLOG_ITEM_EXTNS = "NxpExtns";
27 const char* NXPLOG_ITEM_NCIHAL = "NxpHal";
28 const char* NXPLOG_ITEM_NCIX = "NxpNciX";
29 const char* NXPLOG_ITEM_NCIR = "NxpNciR";
30 const char* NXPLOG_ITEM_FWDNLD = "NxpFwDnld";
31 const char* NXPLOG_ITEM_TML = "NxpTml";
32 const char* NXPLOG_ITEM_ONEBIN = "NxpOneBinary";
33
34 #ifdef NXP_HCI_REQ
35 const char* NXPLOG_ITEM_HCPX = "NxpHcpX";
36 const char* NXPLOG_ITEM_HCPR = "NxpHcpR";
37 #endif /*NXP_HCI_REQ*/
38
39 /* global log level structure */
40 nci_log_level_t gLog_level;
41
42 extern bool nfc_debug_enabled;
43
44 /*******************************************************************************
45 *
46 * Function phNxpLog_SetGlobalLogLevel
47 *
48 * Description Sets the global log level for all modules.
49 * This value is set by Android property
50 *nfc.nxp_log_level_global.
51 * If value can be overridden by module log level.
52 *
53 * Returns The value of global log level
54 *
55 ******************************************************************************/
phNxpLog_SetGlobalLogLevel(void)56 static uint8_t phNxpLog_SetGlobalLogLevel(void) {
57 uint8_t level = NXPLOG_DEFAULT_LOGLEVEL;
58 unsigned long num = 0;
59 char valueStr[PROPERTY_VALUE_MAX] = {0};
60
61 int len = property_get(PROP_NAME_NXPLOG_GLOBAL_LOGLEVEL, valueStr, "");
62 if (len > 0) {
63 /* let Android property override .conf variable */
64 int ret = sscanf(valueStr, "%lu", &num);
65 if (ret) level = (unsigned char)num;
66 }
67 memset(&gLog_level, level, sizeof(nci_log_level_t));
68 return level;
69 }
70
71 /*******************************************************************************
72 *
73 * Function phNxpLog_SetHALLogLevel
74 *
75 * Description Sets the HAL layer log level.
76 *
77 * Returns void
78 *
79 ******************************************************************************/
phNxpLog_SetHALLogLevel(uint8_t level)80 static void phNxpLog_SetHALLogLevel(uint8_t level) {
81 unsigned long num = 0;
82 int len;
83 char valueStr[PROPERTY_VALUE_MAX] = {0};
84
85 if (GetNxpNumValue(NAME_NXPLOG_NCIHAL_LOGLEVEL, &num, sizeof(num))) {
86 gLog_level.hal_log_level =
87 (level > (unsigned char)num) ? level : (unsigned char)num;
88 ;
89 }
90
91 len = property_get(PROP_NAME_NXPLOG_NCIHAL_LOGLEVEL, valueStr, "");
92 if (len > 0) {
93 /* let Android property override .conf variable */
94 int ret = sscanf(valueStr, "%lu", &num);
95 if (ret) gLog_level.hal_log_level = (unsigned char)num;
96 }
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 int ret = sscanf(valueStr, "%lu", &num);
122 if (ret) gLog_level.extns_log_level = (unsigned char)num;
123 }
124 }
125
126 /*******************************************************************************
127 *
128 * Function phNxpLog_SetTmlLogLevel
129 *
130 * Description Sets the Tml layer log level.
131 *
132 * Returns void
133 *
134 ******************************************************************************/
phNxpLog_SetTmlLogLevel(uint8_t level)135 static void phNxpLog_SetTmlLogLevel(uint8_t level) {
136 unsigned long num = 0;
137 int len;
138 char valueStr[PROPERTY_VALUE_MAX] = {0};
139 if (GetNxpNumValue(NAME_NXPLOG_TML_LOGLEVEL, &num, sizeof(num))) {
140 gLog_level.tml_log_level =
141 (level > (unsigned char)num) ? level : (unsigned char)num;
142 ;
143 }
144
145 len = property_get(PROP_NAME_NXPLOG_TML_LOGLEVEL, valueStr, "");
146 if (len > 0) {
147 /* let Android property override .conf variable */
148 int ret = sscanf(valueStr, "%lu", &num);
149 if (ret) gLog_level.tml_log_level = (unsigned char)num;
150 }
151 }
152
153 /*******************************************************************************
154 *
155 * Function phNxpLog_SetDnldLogLevel
156 *
157 * Description Sets the FW download layer log level.
158 *
159 * Returns void
160 *
161 ******************************************************************************/
phNxpLog_SetDnldLogLevel(uint8_t level)162 static void phNxpLog_SetDnldLogLevel(uint8_t level) {
163 unsigned long num = 0;
164 int len;
165 char valueStr[PROPERTY_VALUE_MAX] = {0};
166 if (GetNxpNumValue(NAME_NXPLOG_FWDNLD_LOGLEVEL, &num, sizeof(num))) {
167 gLog_level.dnld_log_level =
168 (level > (unsigned char)num) ? level : (unsigned char)num;
169 ;
170 }
171
172 len = property_get(PROP_NAME_NXPLOG_FWDNLD_LOGLEVEL, valueStr, "");
173 if (len > 0) {
174 /* let Android property override .conf variable */
175 int ret = sscanf(valueStr, "%lu", &num);
176 if (ret) gLog_level.dnld_log_level = (unsigned char)num;
177 }
178 }
179
180 /*******************************************************************************
181 *
182 * Function phNxpLog_SetNciTxLogLevel
183 *
184 * Description Sets the NCI transaction layer log level.
185 *
186 * Returns void
187 *
188 ******************************************************************************/
phNxpLog_SetNciTxLogLevel(uint8_t level)189 static void phNxpLog_SetNciTxLogLevel(uint8_t level) {
190 unsigned long num = 0;
191 int len;
192 char valueStr[PROPERTY_VALUE_MAX] = {0};
193 if (GetNxpNumValue(NAME_NXPLOG_NCIX_LOGLEVEL, &num, sizeof(num))) {
194 gLog_level.ncix_log_level =
195 (level > (unsigned char)num) ? level : (unsigned char)num;
196 }
197 if (GetNxpNumValue(NAME_NXPLOG_NCIR_LOGLEVEL, &num, sizeof(num))) {
198 gLog_level.ncir_log_level =
199 (level > (unsigned char)num) ? level : (unsigned char)num;
200 ;
201 }
202
203 len = property_get(PROP_NAME_NXPLOG_NCI_LOGLEVEL, valueStr, "");
204 if (len > 0) {
205 /* let Android property override .conf variable */
206 int ret = sscanf(valueStr, "%lu", &num);
207 if (ret) {
208 gLog_level.ncix_log_level = (unsigned char)num;
209 gLog_level.ncir_log_level = (unsigned char)num;
210 }
211 }
212 }
213
214 /******************************************************************************
215 * Function phNxpLog_InitializeLogLevel
216 *
217 * Description Initialize and get log level of module from libnfc-nxp.conf
218 *or
219 * Android runtime properties.
220 * The Android property nfc.nxp_global_log_level is to
221 * define log level for all modules. Modules log level will
222 *overwide global level.
223 * The Android property will overwide the level
224 * in libnfc-nxp.conf
225 *
226 * Android property names:
227 * nfc.nxp_log_level_global * defines log level for all
228 *modules
229 * nfc.nxp_log_level_extns * extensions module log
230 * nfc.nxp_log_level_hal * Hal module log
231 * nfc.nxp_log_level_dnld * firmware download module
232 *log
233 * nfc.nxp_log_level_tml * TML module log
234 * nfc.nxp_log_level_nci * NCI transaction log
235 *
236 * Log Level values:
237 * NXPLOG_LOG_SILENT_LOGLEVEL 0 * No trace to show
238 * NXPLOG_LOG_ERROR_LOGLEVEL 1 * Show Error trace
239 *only
240 * NXPLOG_LOG_WARN_LOGLEVEL 2 * Show Warning
241 *trace and Error trace
242 * NXPLOG_LOG_DEBUG_LOGLEVEL 3 * Show all traces
243 *
244 * Returns void
245 *
246 ******************************************************************************/
phNxpLog_InitializeLogLevel(void)247 void phNxpLog_InitializeLogLevel(void) {
248 uint8_t level = phNxpLog_SetGlobalLogLevel();
249 phNxpLog_SetHALLogLevel(level);
250 phNxpLog_SetExtnsLogLevel(level);
251 phNxpLog_SetTmlLogLevel(level);
252 phNxpLog_SetDnldLogLevel(level);
253 phNxpLog_SetNciTxLogLevel(level);
254
255 ALOGD_IF(nfc_debug_enabled,
256 "%s: global =%u, Fwdnld =%u, extns =%u, \
257 hal =%u, tml =%u, ncir =%u, \
258 ncix =%u",
259 __func__, gLog_level.global_log_level, gLog_level.dnld_log_level,
260 gLog_level.extns_log_level, gLog_level.hal_log_level,
261 gLog_level.tml_log_level, gLog_level.ncir_log_level,
262 gLog_level.ncix_log_level);
263 }
264 /******************************************************************************
265 * Function phNxpLog_EnableDisableLogLevel
266 *
267 * Description This function can be called to enable/disable the log levels
268 *
269 *
270 * Log Level values:
271 * NXPLOG_LOG_SILENT_LOGLEVEL 0 * No trace to show
272 * NXPLOG_LOG_ERROR_LOGLEVEL 1 * Show Error trace
273 *only
274 * NXPLOG_LOG_WARN_LOGLEVEL 2 * Show Warning
275 *trace and Error trace
276 * NXPLOG_LOG_DEBUG_LOGLEVEL 3 * Show all traces
277 *
278 * Returns void
279 *
280 ******************************************************************************/
phNxpLog_EnableDisableLogLevel(uint8_t enable)281 uint8_t phNxpLog_EnableDisableLogLevel(uint8_t enable) {
282 static nci_log_level_t prevTraceLevel = {0, 0, 0, 0, 0, 0, 0};
283 static uint8_t currState = 0x01;
284 static bool prev_debug_enabled = true;
285 uint8_t status = NFCSTATUS_FAILED;
286
287 if (0x01 == enable && currState != 0x01) {
288 memcpy(&gLog_level, &prevTraceLevel, sizeof(nci_log_level_t));
289 nfc_debug_enabled = prev_debug_enabled;
290 currState = 0x01;
291 status = NFCSTATUS_SUCCESS;
292 } else if (0x00 == enable && currState != 0x00) {
293 prev_debug_enabled = nfc_debug_enabled;
294 memcpy(&prevTraceLevel, &gLog_level, sizeof(nci_log_level_t));
295 gLog_level.hal_log_level = 0;
296 gLog_level.extns_log_level = 0;
297 gLog_level.tml_log_level = 0;
298 gLog_level.ncix_log_level = 0;
299 gLog_level.ncir_log_level = 0;
300 nfc_debug_enabled = false;
301 currState = 0x00;
302 status = NFCSTATUS_SUCCESS;
303 }
304
305 return status;
306 }
307