1 /*
2  * Copyright (C) 2015 The Android Open Source Project
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 #include <string.h>
18 #if !defined (NXPLOG__H_INCLUDED)
19 #    include "phNxpLog.h"
20 #    include "phNxpConfig.h"
21 #endif
22 #include <cutils/properties.h>
23 
24 const char * NXPLOG_ITEM_EXTNS   = "NxpExtns";
25 const char * NXPLOG_ITEM_NCIHAL  = "NxpHal";
26 const char * NXPLOG_ITEM_NCIX    = "NxpNciX";
27 const char * NXPLOG_ITEM_NCIR    = "NxpNciR";
28 const char * NXPLOG_ITEM_FWDNLD  = "NxpFwDnld";
29 const char * NXPLOG_ITEM_TML     = "NxpTml";
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 nci_log_level_t gLog_level;
38 
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 nfc.nxp_log_level_global.
46  *                  If value can be overridden by module log level.
47  *
48  * Returns          The value of global log level
49  *
50  ******************************************************************************/
phNxpLog_SetGlobalLogLevel(void)51 static uint8_t phNxpLog_SetGlobalLogLevel (void)
52 {
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     {
60         /* let Android property override .conf variable */
61         sscanf (valueStr, "%lu", &num);
62         level = (unsigned char) num;
63     }
64     memset(&gLog_level, level, sizeof(nci_log_level_t));
65     return level;
66 }
67 
68 /*******************************************************************************
69  *
70  * Function         phNxpLog_SetHALLogLevel
71  *
72  * Description      Sets the HAL layer log level.
73  *
74  * Returns          void
75  *
76  ******************************************************************************/
phNxpLog_SetHALLogLevel(uint8_t level)77 static void phNxpLog_SetHALLogLevel (uint8_t level)
78 {
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     {
85         gLog_level.hal_log_level = (level > (unsigned char) num) ? level : (unsigned char) num;;
86     }
87 
88     len = property_get (PROP_NAME_NXPLOG_HAL_LOGLEVEL, valueStr, "");
89     if (len > 0)
90     {
91         /* let Android property override .conf variable */
92         sscanf (valueStr, "%lu", &num);
93         gLog_level.hal_log_level = (unsigned char) num;
94     }
95 
96     return;
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 {
110     unsigned long num = 0;
111     int len;
112     char valueStr [PROPERTY_VALUE_MAX] = {0};
113     if (GetNxpNumValue (NAME_NXPLOG_EXTNS_LOGLEVEL, &num, sizeof(num)))
114     {
115         gLog_level.extns_log_level = (level > (unsigned char) num) ? level : (unsigned char) num;;
116     }
117 
118     len = property_get (PROP_NAME_NXPLOG_EXTNS_LOGLEVEL, valueStr, "");
119     if (len > 0)
120     {
121         /* let Android property override .conf variable */
122         sscanf (valueStr, "%lu", &num);
123         gLog_level.extns_log_level = (unsigned char) num;
124     }
125 
126     return;
127 }
128 
129 /*******************************************************************************
130  *
131  * Function         phNxpLog_SetTmlLogLevel
132  *
133  * Description      Sets the TML layer log level.
134  *
135  * Returns          void
136  *
137  ******************************************************************************/
phNxpLog_SetTmlLogLevel(uint8_t level)138 static void phNxpLog_SetTmlLogLevel (uint8_t level)
139 {
140     unsigned long num = 0;
141     int len;
142     char valueStr [PROPERTY_VALUE_MAX] = {0};
143     if (GetNxpNumValue (NAME_NXPLOG_TML_LOGLEVEL, &num, sizeof(num)))
144     {
145         gLog_level.tml_log_level = (level > (unsigned char) num) ? level : (unsigned char) num;;
146     }
147 
148     len = property_get (PROP_NAME_NXPLOG_TML_LOGLEVEL, valueStr, "");
149     if (len > 0)
150     {
151         /* let Android property override .conf variable */
152         sscanf (valueStr, "%lu", &num);
153         gLog_level.tml_log_level = (unsigned char) num;
154     }
155 
156     return;
157 }
158 
159 /*******************************************************************************
160  *
161  * Function         phNxpLog_SetDnldLogLevel
162  *
163  * Description      Sets the FW download layer log level.
164  *
165  * Returns          void
166  *
167  ******************************************************************************/
phNxpLog_SetDnldLogLevel(uint8_t level)168 static void phNxpLog_SetDnldLogLevel (uint8_t level)
169 {
170     unsigned long num = 0;
171     int len;
172     char valueStr [PROPERTY_VALUE_MAX] = {0};
173     if (GetNxpNumValue (NAME_NXPLOG_FWDNLD_LOGLEVEL, &num, sizeof(num)))
174     {
175         gLog_level.dnld_log_level = (level > (unsigned char) num) ? level : (unsigned char) num;;
176     }
177 
178     len = property_get (PROP_NAME_NXPLOG_FWDNLD_LOGLEVEL, valueStr, "");
179     if (len > 0)
180     {
181         /* let Android property override .conf variable */
182         sscanf (valueStr, "%lu", &num);
183         gLog_level.dnld_log_level = (unsigned char) num;
184     }
185 
186     return;
187 }
188 
189 /*******************************************************************************
190  *
191  * Function         phNxpLog_SetNciTxLogLevel
192  *
193  * Description      Sets the NCI transaction layer log level.
194  *
195  * Returns          void
196  *
197  ******************************************************************************/
phNxpLog_SetNciTxLogLevel(uint8_t level)198 static void phNxpLog_SetNciTxLogLevel (uint8_t level)
199 {
200     unsigned long num = 0;
201     int len;
202     char valueStr [PROPERTY_VALUE_MAX] = {0};
203     if (GetNxpNumValue (NAME_NXPLOG_NCIX_LOGLEVEL, &num, sizeof(num)))
204     {
205         gLog_level.ncix_log_level = (level > (unsigned char) num) ? level : (unsigned char) num;
206     }
207     if (GetNxpNumValue (NAME_NXPLOG_NCIR_LOGLEVEL, &num, sizeof(num)))
208     {
209         gLog_level.ncir_log_level = (level > (unsigned char) num) ? level : (unsigned char) num;;
210     }
211 
212     len = property_get (PROP_NAME_NXPLOG_NCI_LOGLEVEL, valueStr, "");
213     if (len > 0)
214     {
215         /* let Android property override .conf variable */
216         sscanf (valueStr, "%lu", &num);
217         gLog_level.ncix_log_level = (unsigned char) num;
218         gLog_level.ncir_log_level = (unsigned char) num;
219     }
220 
221     return;
222 }
223 
224 /******************************************************************************
225  * Function         phNxpLog_InitializeLogLevel
226  *
227  * Description      Initialize and get log level of module from libnfc-nxp.conf or
228  *                  Android runtime properties.
229  *                  The Android property nfc.nxp_global_log_level is to
230  *                  define log level for all modules. Modules log level will override global level.
231  *                  The Android property will override the level
232  *                  in libnfc-nxp.conf
233  *
234  *                  Android property names:
235  *                      nfc.nxp_log_level_global    * defines log level for all modules
236  *                      nfc.nxp_log_level_extns     * extensions module log
237  *                      nfc.nxp_log_level_hal       * Hal module log
238  *                      nfc.nxp_log_level_dnld      * firmware download module log
239  *                      nfc.nxp_log_level_tml       * TML module log
240  *                      nfc.nxp_log_level_nci       * NCI transaction log
241  *
242  *                  Log Level values:
243  *                      NXPLOG_LOG_SILENT_LOGLEVEL  0        * No trace to show
244  *                      NXPLOG_LOG_ERROR_LOGLEVEL   1        * Show Error trace only
245  *                      NXPLOG_LOG_WARN_LOGLEVEL    2        * Show Warning trace and Error trace
246  *                      NXPLOG_LOG_DEBUG_LOGLEVEL   3        * Show all traces
247  *
248  * Returns          void
249  *
250  ******************************************************************************/
phNxpLog_InitializeLogLevel(void)251 void phNxpLog_InitializeLogLevel(void)
252 {
253     uint8_t level = phNxpLog_SetGlobalLogLevel ();
254     phNxpLog_SetHALLogLevel (level);
255     phNxpLog_SetExtnsLogLevel (level);
256     phNxpLog_SetTmlLogLevel (level);
257     phNxpLog_SetDnldLogLevel (level);
258     phNxpLog_SetNciTxLogLevel (level);
259 
260     ALOGD ("%s: global =%u, Fwdnld =%u, extns =%u, \
261                 hal =%u, tml =%u, ncir =%u, \
262                 ncix =%u", \
263                 __FUNCTION__, gLog_level.global_log_level, gLog_level.dnld_log_level,
264                     gLog_level.extns_log_level, gLog_level.hal_log_level, gLog_level.tml_log_level,
265                     gLog_level.ncir_log_level, gLog_level.ncix_log_level);
266 
267     return;
268 }
269