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 "btm_int_types.h"
31 #include "internal_include/bt_target.h"
32 #include "os/log.h"
33 #include "stack/include/security_client_callbacks.h"
34 #include "types/raw_address.h"
35 
36 using namespace bluetooth;
37 
38 /* Global BTM control block structure
39 */
40 tBTM_CB btm_cb;
41 
42 /*******************************************************************************
43  *
44  * Function         btm_init
45  *
46  * Description      This function is called at BTM startup to allocate the
47  *                  control block (if using dynamic memory), and initializes the
48  *                  tracing level.  It then initializes the various components
49  *                  of btm.
50  *
51  * Returns          void
52  *
53  ******************************************************************************/
btm_init(void)54 void btm_init(void) {
55   btm_cb.Init();
56   get_security_client_interface().BTM_Sec_Init();
57 }
58 
59 /** This function is called to free dynamic memory and system resource allocated by btm_init */
btm_free(void)60 void btm_free(void) {
61   get_security_client_interface().BTM_Sec_Free();
62   btm_cb.Free();
63 }
64 
65 constexpr size_t kMaxLogHistoryTagLength = 6;
66 constexpr size_t kMaxLogHistoryMsgLength = 25;
67 
btm_log_history(const std::string & tag,const char * addr,const std::string & msg,const std::string & extra)68 static void btm_log_history(const std::string& tag, const char* addr,
69                             const std::string& msg, const std::string& extra) {
70   if (btm_cb.history_ == nullptr) {
71     log::error(
72         "BTM_LogHistory has not been constructed or already destroyed !");
73     return;
74   }
75 
76   btm_cb.history_->Push(
77       "%-6s %-25s: %s %s", tag.substr(0, kMaxLogHistoryTagLength).c_str(),
78       msg.substr(0, kMaxLogHistoryMsgLength).c_str(), addr, extra.c_str());
79 }
80 
BTM_LogHistory(const std::string & tag,const RawAddress & bd_addr,const std::string & msg,const std::string & extra)81 void BTM_LogHistory(const std::string& tag, const RawAddress& bd_addr,
82                     const std::string& msg, const std::string& extra) {
83   btm_log_history(tag, ADDRESS_TO_LOGGABLE_CSTR(bd_addr), msg, extra);
84 }
85 
BTM_LogHistory(const std::string & tag,const RawAddress & bd_addr,const std::string & msg)86 void BTM_LogHistory(const std::string& tag, const RawAddress& bd_addr,
87                     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,
92                     const std::string& msg, 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