1 /******************************************************************************
2  *
3  * Copyright (C) 1999-2012 Broadcom Corporation
4  * Copyright (C) 2016 ST Microelectronics S.A.
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at:
9  *
10  *  http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  ******************************************************************************/
19 #include "android_logmsg.h"
20 #include <pthread.h>
21 #include <stdio.h>
22 
23 void DispHal(const char* title, const void* data, size_t length);
24 unsigned char hal_trace_level = STNFC_TRACE_LEVEL_DEBUG;
25 unsigned char hal_conf_trace_level = STNFC_TRACE_LEVEL_DEBUG;
26 uint16_t hal_log_cnt = 0;
27 pthread_mutex_t halLogMutex;
28 
29 /*******************************************************************************
30 **
31 ** Function:        InitializeGlobalAppLogLevel
32 **
33 ** Description:     Initialize and get global logging level from
34 **                  Android property nfc.app_log_level.
35 **
36 ** Returns:         Global log level:
37 **                  STNFC_TRACE_LEVEL_NONE    0     * No trace messages to be
38 *                                                     generated
39 **                  STNFC_TRACE_LEVEL_ERROR   1     * Error condition trace
40 *                                                     messages
41 **                  STNFC_TRACE_LEVEL_WARNING 2     * Warning condition trace
42 *                                                     messages
43 **                  STNFC_TRACE_LEVEL_DEBUG   3     * Debug messages (general)
44 **
45 **                  STNFC_TRACE_LEVEL_VERBOSE 4     * Verbose messages
46 **
47 *******************************************************************************/
InitializeSTLogLevel()48 unsigned char InitializeSTLogLevel() {
49   unsigned long num = 0;
50   int ret;
51 
52   num = 1;
53   if (GetNumValue(NAME_STNFC_HAL_LOGLEVEL, &num, sizeof(num))) {
54     hal_conf_trace_level = (unsigned char)num;
55     hal_trace_level = hal_conf_trace_level;
56   }
57 
58   STLOG_HAL_D("%s: HAL log level=%u, hal_log_cnt (before reset): #%04X",
59               __func__, hal_trace_level, hal_log_cnt);
60 
61   hal_log_cnt = 0x00;
62 
63   ret = pthread_mutex_init(&halLogMutex, NULL);
64   if (ret != 0) {
65     STLOG_HAL_E("HAL: %s pthread_mutex_init failed", __func__);
66   }
67 
68   return hal_trace_level;
69 }
70 
DispHal(const char * title,const void * data,size_t length)71 void DispHal(const char* title, const void* data, size_t length) {
72   uint8_t* d = (uint8_t*)data;
73   char line[100];
74   size_t i, k;
75   bool first_line = true;
76   bool privacy = false;
77   uint16_t frame_nb;
78 
79   pthread_mutex_lock(&halLogMutex);
80   frame_nb = hal_log_cnt;
81 
82   if (hal_log_cnt == 0xFFFF) {
83     hal_log_cnt = 0;
84   } else {
85     hal_log_cnt++;
86   }
87 
88   if (hal_trace_level & STNFC_TRACE_FLAG_PRIVACY) {
89     if ((length > 3) &&
90         // DATA message
91         (((d[0] & 0xE0) == 0) ||
92          // routing table contains the AIDs
93          ((d[0] == 0x21) && (d[1] == 0x01)) ||
94          // NTF showing which AID was selected
95          ((d[0] == 0x61) && (d[1] == 0x09)))) {
96       // We hide the payload for GSMA TS27 15.9.3.2.*
97       privacy = true;
98     }
99   }
100 
101   line[0] = 0;
102   if (length == 0) {
103     STLOG_HAL_D("%s", title);
104     pthread_mutex_unlock(&halLogMutex);
105     return;
106   }
107   for (i = 0, k = 0; i < (privacy ? 3 : length); i++, k++) {
108     if (k > 31) {
109       k = 0;
110       if (first_line == true) {
111         first_line = false;
112         if (title[0] == 'R') {
113           STLOG_HAL_D("(#0%04X) Rx %s\n", frame_nb, line);
114         } else if (title[0] == 'T') {
115           STLOG_HAL_D("(#0%04X) Tx %s\n", frame_nb, line);
116         } else {
117           STLOG_HAL_D("%s\n", line);
118         }
119         pthread_mutex_unlock(&halLogMutex);
120       } else {
121         if (title[0] == 'R') {
122           STLOG_HAL_D("(#0%04X) rx %s\n", frame_nb, line);
123         } else if (title[0] == 'T') {
124           STLOG_HAL_D("(#0%04X) tx %s\n", frame_nb, line);
125         } else {
126           STLOG_HAL_D("%s\n", line);
127         }
128       }
129       if (k < 100) {
130         line[k] = 0;
131       }
132     }
133     snprintf(&line[k * 3], sizeof(line) - (k * 3), "%02x ", d[i]);
134   }
135 
136   if (privacy) {
137     snprintf(&line[k * 3], sizeof(line) - (k * 3), "(hidden)");
138   }
139 
140   if (first_line == true) {
141     if (title[0] == 'R') {
142       STLOG_HAL_D("(#0%04X) Rx %s\n", frame_nb, line);
143     } else if (title[0] == 'T') {
144       STLOG_HAL_D("(#0%04X) Tx %s\n", frame_nb, line);
145     } else {
146       STLOG_HAL_D("%s\n", line);
147     }
148     pthread_mutex_unlock(&halLogMutex);
149   } else {
150     if (title[0] == 'R') {
151       STLOG_HAL_D("(#0%04X) rx %s\n", frame_nb, line);
152     } else if (title[0] == 'T') {
153       STLOG_HAL_D("(#0%04X) tx %s\n", frame_nb, line);
154     } else {
155       STLOG_HAL_D("%s\n", line);
156     }
157   }
158 }
159 
deInitializeHalLog()160 void deInitializeHalLog() { pthread_mutex_destroy(&halLogMutex); }
161