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 uint16_t hal_log_cnt = 0;
26 pthread_mutex_t halLogMutex;
27 
28 /*******************************************************************************
29 **
30 ** Function:        InitializeGlobalAppLogLevel
31 **
32 ** Description:     Initialize and get global logging level from
33 **                  Android property nfc.app_log_level.
34 **
35 ** Returns:         Global log level:
36 **                  STNFC_TRACE_LEVEL_NONE    0     * No trace messages to be
37 *                                                     generated
38 **                  STNFC_TRACE_LEVEL_ERROR   1     * Error condition trace
39 *                                                     messages
40 **                  STNFC_TRACE_LEVEL_WARNING 2     * Warning condition trace
41 *                                                     messages
42 **                  STNFC_TRACE_LEVEL_DEBUG   3     * Debug messages (general)
43 **
44 **                  STNFC_TRACE_LEVEL_VERBOSE 4     * Verbose messages
45 **
46 *******************************************************************************/
InitializeSTLogLevel()47 unsigned char InitializeSTLogLevel() {
48   unsigned long num = 0;
49   int ret;
50 
51   num = 1;
52   if (GetNumValue(NAME_STNFC_HAL_LOGLEVEL, &num, sizeof(num)))
53     hal_trace_level = (unsigned char)num;
54 
55   STLOG_HAL_D("%s: HAL log level=%u, hal_log_cnt (before reset): #%04X",
56               __func__, hal_trace_level, hal_log_cnt);
57 
58   hal_log_cnt = 0x00;
59 
60   ret = pthread_mutex_init(&halLogMutex, NULL);
61   if (ret != 0) {
62     STLOG_HAL_E("HAL: %s pthread_mutex_init failed", __func__);
63   }
64 
65   return hal_trace_level;
66 }
67 
DispHal(const char * title,const void * data,size_t length)68 void DispHal(const char* title, const void* data, size_t length) {
69   uint8_t* d = (uint8_t*)data;
70   char line[100];
71   size_t i, k;
72   bool first_line = true;
73   bool privacy = false;
74   uint16_t frame_nb;
75 
76   pthread_mutex_lock(&halLogMutex);
77   frame_nb = hal_log_cnt;
78 
79   if (hal_log_cnt == 0xFFFF) {
80     hal_log_cnt = 0;
81   } else {
82     hal_log_cnt++;
83   }
84 
85   if (hal_trace_level & STNFC_TRACE_FLAG_PRIVACY) {
86     if ((length > 3) &&
87         // DATA message
88         (((d[0] & 0xE0) == 0) ||
89          // routing table contains the AIDs
90          ((d[0] == 0x21) && (d[1] == 0x01)) ||
91          // NTF showing which AID was selected
92          ((d[0] == 0x61) && (d[1] == 0x09)))) {
93       // We hide the payload for GSMA TS27 15.9.3.2.*
94       privacy = true;
95     }
96   }
97 
98   line[0] = 0;
99   if (length == 0) {
100     STLOG_HAL_D("%s", title);
101     pthread_mutex_unlock(&halLogMutex);
102     return;
103   }
104   for (i = 0, k = 0; i < (privacy ? 3 : length); i++, k++) {
105     if (k > 31) {
106       k = 0;
107       if (first_line == true) {
108         first_line = false;
109         if (title[0] == 'R') {
110           STLOG_HAL_D("(#0%04X) Rx %s\n", frame_nb, line);
111         } else if (title[0] == 'T') {
112           STLOG_HAL_D("(#0%04X) Tx %s\n", frame_nb, line);
113         } else {
114           STLOG_HAL_D("%s\n", line);
115         }
116         pthread_mutex_unlock(&halLogMutex);
117       } else {
118         if (title[0] == 'R') {
119           STLOG_HAL_D("(#0%04X) rx %s\n", frame_nb, line);
120         } else if (title[0] == 'T') {
121           STLOG_HAL_D("(#0%04X) tx %s\n", frame_nb, line);
122         } else {
123           STLOG_HAL_D("%s\n", line);
124         }
125       }
126       line[k] = 0;
127     }
128     sprintf(&line[k * 3], "%02x ", d[i]);
129   }
130 
131   if (privacy) {
132     sprintf(&line[k * 3], "(hidden)");
133   }
134 
135   if (first_line == true) {
136     if (title[0] == 'R') {
137       STLOG_HAL_D("(#0%04X) Rx %s\n", frame_nb, line);
138     } else if (title[0] == 'T') {
139       STLOG_HAL_D("(#0%04X) Tx %s\n", frame_nb, line);
140     } else {
141       STLOG_HAL_D("%s\n", line);
142     }
143     pthread_mutex_unlock(&halLogMutex);
144   } else {
145     if (title[0] == 'R') {
146       STLOG_HAL_D("(#0%04X) rx %s\n", frame_nb, line);
147     } else if (title[0] == 'T') {
148       STLOG_HAL_D("(#0%04X) tx %s\n", frame_nb, line);
149     } else {
150       STLOG_HAL_D("%s\n", line);
151     }
152   }
153 }
154 
deInitializeHalLog()155 void deInitializeHalLog() { pthread_mutex_destroy(&halLogMutex); }
156