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