1 /******************************************************************************
2 *
3 * Copyright 2002-2012 Broadcom Corporation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19 /******************************************************************************
20 *
21 * This file contains the definition of the btm control block.
22 *
23 ******************************************************************************/
24
25 #include <bluetooth/log.h>
26
27 #include <memory>
28 #include <string>
29
30 #include "common/strings.h"
31 #include "main/shim/dumpsys.h"
32 #include "stack/btm/btm_int_types.h"
33 #include "stack/include/security_client_callbacks.h"
34 #include "types/raw_address.h"
35
36 // TODO(b/369381361) Enfore -Wmissing-prototypes
37 #pragma GCC diagnostic ignored "-Wmissing-prototypes"
38
39 using namespace bluetooth;
40
41 /* Global BTM control block structure
42 */
43 tBTM_CB btm_cb;
44
45 /*******************************************************************************
46 *
47 * Function btm_init
48 *
49 * Description This function is called at BTM startup to allocate the
50 * control block (if using dynamic memory), and initializes the
51 * tracing level. It then initializes the various components
52 * of btm.
53 *
54 * Returns void
55 *
56 ******************************************************************************/
btm_init(void)57 void btm_init(void) {
58 btm_cb.Init();
59 get_security_client_interface().BTM_Sec_Init();
60 }
61
62 /** This function is called to free dynamic memory and system resource allocated by btm_init */
btm_free(void)63 void btm_free(void) {
64 get_security_client_interface().BTM_Sec_Free();
65 btm_cb.Free();
66 }
67
68 constexpr size_t kMaxLogHistoryTagLength = 6;
69 constexpr size_t kMaxLogHistoryMsgLength = 25;
70
btm_log_history(const std::string & tag,const char * addr,const std::string & msg,const std::string & extra)71 static void btm_log_history(const std::string& tag, const char* addr, const std::string& msg,
72 const std::string& extra) {
73 if (btm_cb.history_ == nullptr) {
74 log::error("BTM_LogHistory has not been constructed or already destroyed !");
75 return;
76 }
77
78 btm_cb.history_->Push("%-6s %-25s: %s %s", tag.substr(0, kMaxLogHistoryTagLength).c_str(),
79 msg.substr(0, kMaxLogHistoryMsgLength).c_str(), addr, extra.c_str());
80 }
81
BTM_LogHistory(const std::string & tag,const RawAddress & bd_addr,const std::string & msg,const std::string & extra)82 void BTM_LogHistory(const std::string& tag, const RawAddress& bd_addr, const std::string& msg,
83 const std::string& extra) {
84 btm_log_history(tag, ADDRESS_TO_LOGGABLE_CSTR(bd_addr), msg, extra);
85 }
86
BTM_LogHistory(const std::string & tag,const RawAddress & bd_addr,const std::string & msg)87 void BTM_LogHistory(const std::string& tag, const RawAddress& bd_addr, const std::string& msg) {
88 BTM_LogHistory(tag, bd_addr, msg, std::string());
89 }
90
BTM_LogHistory(const std::string & tag,const tBLE_BD_ADDR & ble_bd_addr,const std::string & msg,const std::string & extra)91 void BTM_LogHistory(const std::string& tag, const tBLE_BD_ADDR& ble_bd_addr, const std::string& msg,
92 const std::string& extra) {
93 btm_log_history(tag, ADDRESS_TO_LOGGABLE_CSTR(ble_bd_addr), msg, extra);
94 }
95
BTM_LogHistory(const std::string & tag,const tBLE_BD_ADDR & ble_bd_addr,const std::string & msg)96 void BTM_LogHistory(const std::string& tag, const tBLE_BD_ADDR& ble_bd_addr,
97 const std::string& msg) {
98 BTM_LogHistory(tag, ble_bd_addr, msg, std::string());
99 }
100
101 bluetooth::common::TimestamperInMilliseconds timestamper_in_milliseconds;
102
103 using Record = common::TimestampedEntry<std::string>;
104 const std::string kTimeFormat("%Y-%m-%d %H:%M:%S");
105
106 #define DUMPSYS_TAG "shim::btm"
DumpsysBtm(int fd)107 void DumpsysBtm(int fd) {
108 LOG_DUMPSYS_TITLE(fd, DUMPSYS_TAG);
109 if (btm_cb.history_ != nullptr) {
110 std::vector<Record> history = btm_cb.history_->Pull();
111 for (auto& record : history) {
112 time_t then = record.timestamp / 1000;
113 struct tm tm;
114 localtime_r(&then, &tm);
115 auto s2 = common::StringFormatTime(kTimeFormat, tm);
116 LOG_DUMPSYS(fd, " %s.%03u %s", s2.c_str(), static_cast<unsigned int>(record.timestamp % 1000),
117 record.entry.c_str());
118 }
119 }
120 }
121 #undef DUMPSYS_TAG
122