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