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