1 /******************************************************************************
2  *
3  *  Copyright 1999-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 #pragma once
19 
20 #include <bluetooth/log.h>
21 
22 #include <cstdint>
23 #include <memory>
24 #include <string>
25 
26 #include "common/circular_buffer.h"
27 #include "osi/include/fixed_queue.h"
28 #include "stack/acl/acl.h"
29 #include "stack/btm/btm_ble_int_types.h"
30 #include "stack/btm/btm_sco.h"
31 #include "stack/btm/neighbor_inquiry.h"
32 #include "stack/include/btm_ble_api_types.h"
33 #include "stack/include/security_client_callbacks.h"
34 #include "stack/rnr/remote_name_request.h"
35 #include "types/raw_address.h"
36 
37 constexpr size_t kMaxLogSize = 255;
38 constexpr size_t kBtmLogHistoryBufferSize = 200;
39 constexpr size_t kMaxInquiryScanHistory = 10;
40 
41 extern bluetooth::common::TimestamperInMilliseconds timestamper_in_milliseconds;
42 
43 class TimestampedStringCircularBuffer
44     : public bluetooth::common::TimestampedCircularBuffer<std::string> {
45 public:
TimestampedStringCircularBuffer(size_t size)46   explicit TimestampedStringCircularBuffer(size_t size)
47       : bluetooth::common::TimestampedCircularBuffer<std::string>(size) {}
48 
Push(const std::string & s)49   void Push(const std::string& s) {
50     bluetooth::common::TimestampedCircularBuffer<std::string>::Push(s.substr(0, kMaxLogSize));
51   }
52 
53   template <typename... Args>
Push(Args...args)54   void Push(Args... args) {
55     char buf[kMaxLogSize];
56     std::snprintf(buf, sizeof(buf), args...);
57     bluetooth::common::TimestampedCircularBuffer<std::string>::Push(std::string(buf));
58   }
59 };
60 
61 /* Define a structure to hold all the BTM data
62  */
63 
64 /* Define the Device Management control structure
65  */
66 typedef struct tBTM_DEVCB {
67   tBTM_CMPL_CB* p_rln_cmpl_cb;    /* Callback function to be called when  */
68                                   /* read local name function complete    */
69 
70   alarm_t* read_rssi_timer;     /* Read RSSI timer */
71   tBTM_CMPL_CB* p_rssi_cmpl_cb; /* Callback function to be called when  */
72                                 /* read RSSI function completes */
73 
74   alarm_t* read_failed_contact_counter_timer;     /* Read Failed Contact Counter */
75                                                   /* timer */
76   tBTM_CMPL_CB* p_failed_contact_counter_cmpl_cb; /* Callback function to be */
77   /* called when read Failed Contact Counter function completes */
78 
79   alarm_t* read_automatic_flush_timeout_timer;     /* Read Automatic Flush Timeout */
80                                                    /* timer */
81   tBTM_CMPL_CB* p_automatic_flush_timeout_cmpl_cb; /* Callback function to be */
82   /* called when read Automatic Flush Timeout function completes */
83 
84   alarm_t* read_tx_power_timer;     /* Read tx power timer */
85   tBTM_CMPL_CB* p_tx_power_cmpl_cb; /* Callback function to be called       */
86 
87   DEV_CLASS dev_class; /* Local device class                   */
88 
89   tBTM_CMPL_CB* p_le_test_cmd_cmpl_cb; /* Callback function to be called when
90                                        LE test mode command has been sent successfully */
91 
92   RawAddress read_tx_pwr_addr; /* read TX power target address     */
93 
InittBTM_DEVCB94   void Init() {
95     read_rssi_timer = alarm_new("btm.read_rssi_timer");
96     read_failed_contact_counter_timer = alarm_new("btm.read_failed_contact_counter_timer");
97     read_automatic_flush_timeout_timer = alarm_new("btm.read_automatic_flush_timeout_timer");
98     read_tx_power_timer = alarm_new("btm.read_tx_power_timer");
99   }
100 
FreetBTM_DEVCB101   void Free() {
102     alarm_free(read_rssi_timer);
103     alarm_free(read_failed_contact_counter_timer);
104     alarm_free(read_automatic_flush_timeout_timer);
105     alarm_free(read_tx_power_timer);
106   }
107 } tBTM_DEVCB;
108 
109 typedef struct tBTM_CB {
110   /*****************************************************
111   **      Control block for local device
112   *****************************************************/
113   tBTM_DEVCB devcb;
114 
115   /*****************************************************
116   **      Control block for local LE device
117   *****************************************************/
118   tBTM_BLE_CB ble_ctr_cb;
119 
120 public:
121   tBTM_BLE_VSC_CB cmn_ble_vsc_cb;
122 
123   /* Packet types supported by the local device */
124   uint16_t btm_sco_pkt_types_supported{0};
125 
126   /*****************************************************
127   **      Inquiry
128   *****************************************************/
129   tBTM_INQUIRY_VAR_ST btm_inq_vars;
130 
131   /*****************************************************
132   **      SCO Management
133   *****************************************************/
134   tSCO_CB sco_cb;
135 
136   uint16_t disc_handle{0}; /* for legacy devices */
137   uint8_t disc_reason{0};  /* for legacy devices */
138 
139   fixed_queue_t* sec_pending_q{nullptr}; /* pending sequrity requests in
140                                             tBTM_SEC_QUEUE_ENTRY format */
141 
142 #define BTM_CODEC_TYPE_MAX_RECORDS 32
143   tBTM_BT_DYNAMIC_AUDIO_BUFFER_CB dynamic_audio_buffer_cb[BTM_CODEC_TYPE_MAX_RECORDS];
144 
145   tACL_CB acl_cb_;
146 
147   std::shared_ptr<TimestampedStringCircularBuffer> history_{nullptr};
148 
149   struct {
150     struct {
151       uint64_t start_time_ms;
152       uint64_t results;
153     } classic_inquiry, le_scan, le_inquiry, le_observe, le_legacy_scan;
154     std::unique_ptr<bluetooth::common::TimestampedCircularBuffer<tBTM_INQUIRY_CMPL>>
155             inquiry_history_ = std::make_unique<
156                     bluetooth::common::TimestampedCircularBuffer<tBTM_INQUIRY_CMPL>>(
157                     kMaxInquiryScanHistory);
158   } neighbor;
159 
160   bluetooth::stack::rnr::RemoteNameRequest rnr;
161 
InittBTM_CB162   void Init() {
163     memset(&devcb, 0, sizeof(devcb));
164     memset(&ble_ctr_cb, 0, sizeof(ble_ctr_cb));
165     memset(&cmn_ble_vsc_cb, 0, sizeof(cmn_ble_vsc_cb));
166     memset(&btm_inq_vars, 0, sizeof(btm_inq_vars));
167     memset(&sco_cb, 0, sizeof(sco_cb));
168 
169     acl_cb_ = {};
170     neighbor = {};
171     rnr = {};
172     rnr.remote_name_timer = alarm_new("rnr.remote_name_timer");
173 
174     /* Initialize BTM component structures */
175     btm_inq_vars.Init(); /* Inquiry Database and Structures */
176     sco_cb.Init();       /* SCO Database and Structures (If included) */
177     devcb.Init();
178 
179     history_ = std::make_shared<TimestampedStringCircularBuffer>(kBtmLogHistoryBufferSize);
180     bluetooth::log::assert_that(history_ != nullptr, "assert failed: history_ != nullptr");
181     history_->Push(std::string("Initialized btm history"));
182   }
183 
FreetBTM_CB184   void Free() {
185     alarm_free(rnr.remote_name_timer);
186     history_.reset();
187 
188     devcb.Free();
189     sco_cb.Free();
190     btm_inq_vars.Free();
191   }
192 } tBTM_CB;
193