1 /*
2 * Copyright 2022 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 #define LOG_TAG "bt_bte"
18
19 #include <base/logging.h>
20 #include <stdarg.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/time.h>
25 #include <time.h>
26
27 #include "a2dp_api.h"
28 #include "avdt_api.h"
29 #include "avrc_api.h"
30 #include "bta_api.h"
31 #include "btm_api.h"
32 #include "btu.h"
33 #include "l2c_api.h"
34 #include "osi/include/config.h"
35 #include "osi/include/log.h"
36 #include "port_api.h"
37 #include "sdp_api.h"
38 #include "stack_config.h"
39 #if (BNEP_INCLUDED == TRUE)
40 #include "bnep_api.h"
41 #endif
42 #if (PAN_INCLUDED == TRUE)
43 #include "pan_api.h"
44 #endif
45 #if (HID_HOST_INCLUDED == TRUE)
46 #include "hidh_api.h"
47 #endif
48 #if (HID_DEV_INCLUDED == TRUE)
49 #include "hidd_api.h"
50 #endif
51
52 #include "gd/common/init_flags.h"
53 #include "smp_api.h"
54
55 #ifndef DEFAULT_CONF_TRACE_LEVEL
56 #define DEFAULT_CONF_TRACE_LEVEL BT_TRACE_LEVEL_WARNING
57 #endif
58
59 static uint8_t BTAPP_SetTraceLevel(uint8_t new_level);
60 static uint8_t BTIF_SetTraceLevel(uint8_t new_level);
61 static uint8_t BTU_SetTraceLevel(uint8_t new_level);
62
63 /* make sure list is order by increasing layer id!!! */
64 static tBTTRC_FUNC_MAP bttrc_set_level_map[] = {
65 {BTTRC_ID_STK_BTU, BTTRC_ID_STK_HCI, BTU_SetTraceLevel, "TRC_HCI",
66 DEFAULT_CONF_TRACE_LEVEL},
67 {BTTRC_ID_STK_L2CAP, BTTRC_ID_STK_L2CAP, L2CA_SetTraceLevel, "TRC_L2CAP",
68 DEFAULT_CONF_TRACE_LEVEL},
69 {BTTRC_ID_STK_RFCOMM, BTTRC_ID_STK_RFCOMM_DATA, PORT_SetTraceLevel,
70 "TRC_RFCOMM", DEFAULT_CONF_TRACE_LEVEL},
71 {BTTRC_ID_STK_AVCT, BTTRC_ID_STK_AVCT, AVCT_SetTraceLevel, "TRC_AVCT",
72 DEFAULT_CONF_TRACE_LEVEL},
73 {BTTRC_ID_STK_AVDT, BTTRC_ID_STK_AVDT, AVDT_SetTraceLevel, "TRC_AVDT",
74 DEFAULT_CONF_TRACE_LEVEL},
75 {BTTRC_ID_STK_AVRC, BTTRC_ID_STK_AVRC, AVRC_SetTraceLevel, "TRC_AVRC",
76 DEFAULT_CONF_TRACE_LEVEL},
77 {BTTRC_ID_STK_A2DP, BTTRC_ID_STK_A2DP, A2DP_SetTraceLevel, "TRC_A2D",
78 DEFAULT_CONF_TRACE_LEVEL},
79 #if (BNEP_INCLUDED == TRUE)
80 {BTTRC_ID_STK_BNEP, BTTRC_ID_STK_BNEP, BNEP_SetTraceLevel, "TRC_BNEP",
81 DEFAULT_CONF_TRACE_LEVEL},
82 #endif
83 {BTTRC_ID_STK_BTM_ACL, BTTRC_ID_STK_BTM_SEC, BTM_SetTraceLevel, "TRC_BTM",
84 DEFAULT_CONF_TRACE_LEVEL},
85 #if (HID_HOST_INCLUDED == TRUE)
86 {BTTRC_ID_STK_HID, BTTRC_ID_STK_HID, HID_HostSetTraceLevel, "TRC_HID_HOST",
87 DEFAULT_CONF_TRACE_LEVEL},
88 #endif
89 #if (PAN_INCLUDED == TRUE)
90 {BTTRC_ID_STK_PAN, BTTRC_ID_STK_PAN, PAN_SetTraceLevel, "TRC_PAN",
91 DEFAULT_CONF_TRACE_LEVEL},
92 #endif
93 {BTTRC_ID_STK_SDP, BTTRC_ID_STK_SDP, SDP_SetTraceLevel, "TRC_SDP",
94 DEFAULT_CONF_TRACE_LEVEL},
95 {BTTRC_ID_STK_SMP, BTTRC_ID_STK_SMP, SMP_SetTraceLevel, "TRC_SMP",
96 DEFAULT_CONF_TRACE_LEVEL},
97 #if (HID_DEV_INCLUDED == TRUE)
98 {BTTRC_ID_STK_HIDD, BTTRC_ID_STK_HIDD, HID_DevSetTraceLevel, "TRC_HID_DEV",
99 DEFAULT_CONF_TRACE_LEVEL},
100 #endif
101
102 /* LayerIDs for BTA, currently everything maps onto appl_trace_level.
103 */
104 {BTTRC_ID_BTA_ACC, BTTRC_ID_BTAPP, BTAPP_SetTraceLevel, "TRC_BTAPP",
105 DEFAULT_CONF_TRACE_LEVEL},
106 {BTTRC_ID_BTA_ACC, BTTRC_ID_BTAPP, BTIF_SetTraceLevel, "TRC_BTIF",
107 DEFAULT_CONF_TRACE_LEVEL},
108
109 {0, 0, NULL, NULL, DEFAULT_CONF_TRACE_LEVEL}};
110
111 /* this function should go into BTAPP_DM for example */
BTAPP_SetTraceLevel(uint8_t new_level)112 static uint8_t BTAPP_SetTraceLevel(uint8_t new_level) {
113 if (new_level != 0xFF) appl_trace_level = new_level;
114
115 return appl_trace_level;
116 }
117
BTIF_SetTraceLevel(uint8_t new_level)118 static uint8_t BTIF_SetTraceLevel(uint8_t new_level) {
119 if (new_level != 0xFF) btif_trace_level = new_level;
120
121 return btif_trace_level;
122 }
123
BTU_SetTraceLevel(uint8_t new_level)124 static uint8_t BTU_SetTraceLevel(uint8_t new_level) {
125 if (new_level != 0xFF) btu_trace_level = new_level;
126
127 return btu_trace_level;
128 }
129
load_levels_from_config(const config_t * config)130 void load_levels_from_config(const config_t* config) {
131 CHECK(config != NULL);
132
133 for (tBTTRC_FUNC_MAP* functions = &bttrc_set_level_map[0];
134 functions->trc_name; ++functions) {
135 int value = config_get_int(*config, CONFIG_DEFAULT_SECTION,
136 functions->trc_name, -1);
137 if (value != -1) {
138 functions->trace_level = value;
139 }
140 if (bluetooth::common::InitFlags::GetDefaultLogLevel() >= LOG_TAG_VERBOSE) {
141 LOG_INFO("Enable logging for %s because all debug logs are enabled",
142 functions->trc_name);
143 functions->trace_level = BT_TRACE_LEVEL_VERBOSE;
144 }
145 LOG_INFO("BTE_InitTraceLevels -- %s : Level %d", functions->trc_name,
146 functions->trace_level);
147 if (functions->p_f) functions->p_f(functions->trace_level);
148 }
149 }
150