• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <cstdint>
22 #include <memory>
23 #include <string>
24 
25 #include "gd/common/circular_buffer.h"
26 #include "osi/include/allocator.h"
27 #include "osi/include/fixed_queue.h"
28 #include "osi/include/list.h"
29 #include "stack/acl/acl.h"
30 #include "stack/btm/btm_ble_int_types.h"
31 #include "stack/btm/btm_sco.h"
32 #include "stack/btm/neighbor_inquiry.h"
33 #include "stack/btm/security_device_record.h"
34 #include "stack/include/bt_octets.h"
35 #include "stack/include/btm_ble_api_types.h"
36 #include "stack/include/security_client_callbacks.h"
37 #include "types/raw_address.h"
38 
39 #define BTM_MAX_SCN_ 31  // PORT_MAX_RFC_PORTS packages/modules/Bluetooth/system/stack/include/rfcdefs.h
40 
41 constexpr size_t kMaxLogSize = 255;
42 constexpr size_t kBtmLogHistoryBufferSize = 200;
43 constexpr size_t kMaxInquiryScanHistory = 10;
44 
45 extern bluetooth::common::TimestamperInMilliseconds timestamper_in_milliseconds;
46 
47 class TimestampedStringCircularBuffer
48     : public bluetooth::common::TimestampedCircularBuffer<std::string> {
49  public:
TimestampedStringCircularBuffer(size_t size)50   explicit TimestampedStringCircularBuffer(size_t size)
51       : bluetooth::common::TimestampedCircularBuffer<std::string>(size) {}
52 
Push(const std::string & s)53   void Push(const std::string& s) {
54     bluetooth::common::TimestampedCircularBuffer<std::string>::Push(
55         s.substr(0, kMaxLogSize));
56   }
57 
58   template <typename... Args>
Push(Args...args)59   void Push(Args... args) {
60     char buf[kMaxLogSize];
61     std::snprintf(buf, sizeof(buf), args...);
62     bluetooth::common::TimestampedCircularBuffer<std::string>::Push(
63         std::string(buf));
64   }
65 };
66 
67 /*
68  * Local device configuration
69  */
70 typedef struct {
71   tBTM_LOC_BD_NAME bd_name;  /* local Bluetooth device name */
72   bool pin_type;             /* true if PIN type is fixed */
73   uint8_t pin_code_len;      /* Bonding information */
74   PIN_CODE pin_code;         /* PIN CODE if pin type is fixed */
75 } tBTM_CFG;
76 
77 /* Pairing State */
78 enum {
79   BTM_PAIR_STATE_IDLE, /* Idle                                         */
80   BTM_PAIR_STATE_GET_REM_NAME, /* Getting the remote name (to check for SM4) */
81   BTM_PAIR_STATE_WAIT_PIN_REQ, /* Started authentication, waiting for PIN req
82                                   (PIN is pre-fetched) */
83   BTM_PAIR_STATE_WAIT_LOCAL_PIN,       /* Waiting for local PIN code */
84   BTM_PAIR_STATE_WAIT_NUMERIC_CONFIRM, /* Waiting user 'yes' to numeric
85                                           confirmation   */
86   BTM_PAIR_STATE_KEY_ENTRY, /* Key entry state (we are a keyboard)          */
87   BTM_PAIR_STATE_WAIT_LOCAL_OOB_RSP, /* Waiting for local response to peer OOB
88                                         data  */
89   BTM_PAIR_STATE_WAIT_LOCAL_IOCAPS, /* Waiting for local IO capabilities and OOB
90                                        data */
91   BTM_PAIR_STATE_INCOMING_SSP, /* Incoming SSP (got peer IO caps when idle) */
92   BTM_PAIR_STATE_WAIT_AUTH_COMPLETE, /* All done, waiting authentication
93                                         cpmplete    */
94   BTM_PAIR_STATE_WAIT_DISCONNECT     /* Waiting to disconnect the ACL */
95 };
96 typedef uint8_t tBTM_PAIRING_STATE;
97 
98 #define BTM_PAIR_FLAGS_WE_STARTED_DD \
99   0x01 /* We want to do dedicated bonding              */
100 #define BTM_PAIR_FLAGS_PEER_STARTED_DD \
101   0x02 /* Peer initiated dedicated bonding             */
102 #define BTM_PAIR_FLAGS_DISC_WHEN_DONE 0x04 /* Disconnect when done     */
103 #define BTM_PAIR_FLAGS_PIN_REQD \
104   0x08 /* set this bit when pin_callback is called     */
105 #define BTM_PAIR_FLAGS_PRE_FETCH_PIN \
106   0x10 /* set this bit when pre-fetch pin     */
107 #define BTM_PAIR_FLAGS_REJECTED_CONNECT \
108   0x20 /* set this bit when rejected incoming connection  */
109 #define BTM_PAIR_FLAGS_WE_CANCEL_DD \
110   0x40 /* set this bit when cancelling a bonding procedure */
111 #define BTM_PAIR_FLAGS_LE_ACTIVE \
112   0x80 /* use this bit when SMP pairing is active */
113 
114 typedef struct {
115   bool is_mux;
116   RawAddress bd_addr;
117   uint16_t psm;
118   bool is_orig;
119   tBTM_SEC_CALLBACK* p_callback;
120   void* p_ref_data;
121   uint16_t rfcomm_security_requirement;
122   tBT_TRANSPORT transport;
123   tBTM_BLE_SEC_ACT sec_act;
124 } tBTM_SEC_QUEUE_ENTRY;
125 
126 /* Define a structure to hold all the BTM data
127 */
128 
129 #define BTM_STATE_BUFFER_SIZE 5 /* size of state buffer */
130 
131 /* Define the Device Management control structure
132  */
133 typedef struct tBTM_DEVCB {
134   tBTM_VS_EVT_CB* p_vend_spec_cb[BTM_MAX_VSE_CALLBACKS]; /* Register for vendor
135                                                             specific events  */
136 
137   tBTM_CMPL_CB*
138       p_stored_link_key_cmpl_cb; /* Read/Write/Delete stored link key    */
139 
140   alarm_t* read_local_name_timer; /* Read local name timer */
141   tBTM_CMPL_CB* p_rln_cmpl_cb;    /* Callback function to be called when  */
142                                   /* read local name function complete    */
143 
144   alarm_t* read_rssi_timer;     /* Read RSSI timer */
145   tBTM_CMPL_CB* p_rssi_cmpl_cb; /* Callback function to be called when  */
146                                 /* read RSSI function completes */
147 
148   alarm_t* read_failed_contact_counter_timer; /* Read Failed Contact Counter */
149                                               /* timer */
150   tBTM_CMPL_CB* p_failed_contact_counter_cmpl_cb; /* Callback function to be */
151   /* called when read Failed Contact Counter function completes */
152 
153   alarm_t*
154       read_automatic_flush_timeout_timer; /* Read Automatic Flush Timeout */
155                                           /* timer */
156   tBTM_CMPL_CB* p_automatic_flush_timeout_cmpl_cb; /* Callback function to be */
157   /* called when read Automatic Flush Timeout function completes */
158 
159   alarm_t* read_link_quality_timer;
160   tBTM_CMPL_CB* p_link_qual_cmpl_cb; /* Callback function to be called when  */
161                                      /* read link quality function completes */
162 
163   alarm_t* read_tx_power_timer;     /* Read tx power timer */
164   tBTM_CMPL_CB* p_tx_power_cmpl_cb; /* Callback function to be called       */
165 
166   DEV_CLASS dev_class; /* Local device class                   */
167 
168   RawAddress read_tx_pwr_addr; /* read TX power target address     */
169 
170   tBTM_BLE_LOCAL_ID_KEYS id_keys;   /* local BLE ID keys */
171   Octet16 ble_encryption_key_value; /* BLE encryption key */
172 
173 #if (BTM_BLE_CONFORMANCE_TESTING == TRUE)
174   bool no_disc_if_pair_fail;
175   bool enable_test_mac_val;
176   BT_OCTET8 test_mac;
177   bool enable_test_local_sign_cntr;
178   uint32_t test_local_sign_cntr;
179 #endif
180 
181   tBTM_IO_CAP loc_io_caps;    /* IO capability of the local device */
182   tBTM_AUTH_REQ loc_auth_req; /* the auth_req flag  */
183 
InittBTM_DEVCB184   void Init() {
185     read_local_name_timer = alarm_new("btm.read_local_name_timer");
186     read_rssi_timer = alarm_new("btm.read_rssi_timer");
187     read_failed_contact_counter_timer =
188         alarm_new("btm.read_failed_contact_counter_timer");
189     read_automatic_flush_timeout_timer =
190         alarm_new("btm.read_automatic_flush_timeout_timer");
191     read_link_quality_timer = alarm_new("btm.read_link_quality_timer");
192     read_tx_power_timer = alarm_new("btm.read_tx_power_timer");
193   }
194 
FreetBTM_DEVCB195   void Free() {
196     alarm_free(read_local_name_timer);
197     alarm_free(read_rssi_timer);
198     alarm_free(read_failed_contact_counter_timer);
199     alarm_free(read_automatic_flush_timeout_timer);
200     alarm_free(read_link_quality_timer);
201     alarm_free(read_tx_power_timer);
202   }
203 } tBTM_DEVCB;
204 
205 typedef struct tBTM_CB {
206   tBTM_CFG cfg; /* Device configuration */
207 
208   /*****************************************************
209   **      Device control
210   *****************************************************/
211   tBTM_DEVCB devcb;
212 
213   /*****************************************************
214   **      BLE Device controllers
215   *****************************************************/
216   tBTM_BLE_CB ble_ctr_cb;
217 
218  private:
219   friend void btm_ble_ltk_request_reply(const RawAddress& bda, bool use_stk,
220                                         const Octet16& stk);
221   friend tBTM_STATUS btm_ble_start_encrypt(const RawAddress& bda, bool use_stk,
222                                            Octet16* p_stk);
223   friend void btm_ble_ltk_request_reply(const RawAddress& bda, bool use_stk,
224                                         const Octet16& stk);
225   uint16_t enc_handle{0};
226 
227   friend void btm_ble_ltk_request(uint16_t handle, uint8_t rand[8],
228                                   uint16_t ediv);
229   BT_OCTET8 enc_rand; /* received rand value from LTK request*/
230 
231   uint16_t ediv{0}; /* received ediv value from LTK request */
232 
233   uint8_t key_size{0};
234 
235  public:
236   tBTM_BLE_VSC_CB cmn_ble_vsc_cb;
237 
238   /* Packet types supported by the local device */
239   uint16_t btm_sco_pkt_types_supported{0};
240 
241   /*****************************************************
242   **      Inquiry
243   *****************************************************/
244   tBTM_INQUIRY_VAR_ST btm_inq_vars;
245 
246   /*****************************************************
247   **      SCO Management
248   *****************************************************/
249   tSCO_CB sco_cb;
250 
251   /*****************************************************
252   **      Security Management
253   *****************************************************/
254   tBTM_APPL_INFO api;
255 
256 #define BTM_SEC_MAX_RMT_NAME_CALLBACKS 2
257   tBTM_RMT_NAME_CALLBACK* p_rmt_name_callback[BTM_SEC_MAX_RMT_NAME_CALLBACKS];
258 
259   tBTM_SEC_DEV_REC* p_collided_dev_rec{nullptr};
260   alarm_t* sec_collision_timer{nullptr};
261   uint64_t collision_start_time{0};
262   uint32_t dev_rec_count{0}; /* Counter used for device record timestamp */
263   uint8_t security_mode{0};
264   bool pairing_disabled{false};
265   bool security_mode_changed{false}; /* mode changed during bonding */
266   bool pin_type_changed{false};      /* pin type changed during bonding */
267   bool sec_req_pending{false};       /*   true if a request is pending */
268 
269   uint8_t pin_code_len{0};          /* for legacy devices */
270   PIN_CODE pin_code;                /* for legacy devices */
271   tBTM_PAIRING_STATE pairing_state{
272       BTM_PAIR_STATE_IDLE};         /* The current pairing state    */
273   uint8_t pairing_flags{0};         /* The current pairing flags    */
274   RawAddress pairing_bda;           /* The device currently pairing */
275   alarm_t* pairing_timer{nullptr};  /* Timer for pairing process    */
276   alarm_t* execution_wait_timer{nullptr}; /* To avoid concurrent auth request */
277   uint16_t disc_handle{0};          /* for legacy devices */
278   uint8_t disc_reason{0};           /* for legacy devices */
279   tBTM_SEC_SERV_REC sec_serv_rec[BTM_SEC_MAX_SERVICE_RECORDS];
280   list_t* sec_dev_rec{nullptr}; /* list of tBTM_SEC_DEV_REC */
281   tBTM_SEC_SERV_REC* p_out_serv{nullptr};
282   tBTM_MKEY_CALLBACK* mkey_cback{nullptr};
283 
284   RawAddress connecting_bda;
285   DEV_CLASS connecting_dc;
286   uint8_t trace_level;
287   bool is_paging{false};  /* true, if paging is in progess */
288   bool is_inquiry{false}; /* true, if inquiry is in progess */
289   fixed_queue_t* page_queue{nullptr};
290 
291   bool paging{false};
set_pagingtBTM_CB292   void set_paging() { paging = true; }
reset_pagingtBTM_CB293   void reset_paging() { paging = false; }
is_paging_activetBTM_CB294   bool is_paging_active() const {
295     return paging;
296   }  // TODO remove all this paging state
297 
298   fixed_queue_t* sec_pending_q{nullptr}; /* pending sequrity requests in
299                                             tBTM_SEC_QUEUE_ENTRY format */
300 
301   // BQR Receiver
302   tBTM_BT_QUALITY_REPORT_RECEIVER* p_bqr_report_receiver{nullptr};
303 
304 #define BTM_CODEC_TYPE_MAX_RECORDS 32
305   tBTM_BT_DYNAMIC_AUDIO_BUFFER_CB
306       dynamic_audio_buffer_cb[BTM_CODEC_TYPE_MAX_RECORDS];
307 
308   tACL_CB acl_cb_;
309 
310   std::shared_ptr<TimestampedStringCircularBuffer> history_{nullptr};
311 
312   struct {
313     struct {
314       long long start_time_ms;
315       unsigned long results;
316     } classic_inquiry, le_scan, le_inquiry, le_observe, le_legacy_scan;
317     std::unique_ptr<
318         bluetooth::common::TimestampedCircularBuffer<tBTM_INQUIRY_CMPL>>
319         inquiry_history_ = std::make_unique<
320             bluetooth::common::TimestampedCircularBuffer<tBTM_INQUIRY_CMPL>>(
321             kMaxInquiryScanHistory);
322   } neighbor;
323 
InittBTM_CB324   void Init(uint8_t initial_security_mode) {
325     memset(&cfg, 0, sizeof(cfg));
326     memset(&devcb, 0, sizeof(devcb));
327     memset(&ble_ctr_cb, 0, sizeof(ble_ctr_cb));
328     memset(&enc_rand, 0, sizeof(enc_rand));
329     memset(&cmn_ble_vsc_cb, 0, sizeof(cmn_ble_vsc_cb));
330     memset(&btm_inq_vars, 0, sizeof(btm_inq_vars));
331     memset(&sco_cb, 0, sizeof(sco_cb));
332     memset(&api, 0, sizeof(api));
333     memset(p_rmt_name_callback, 0, sizeof(p_rmt_name_callback));
334     memset(&pin_code, 0, sizeof(pin_code));
335     memset(sec_serv_rec, 0, sizeof(sec_serv_rec));
336 
337     connecting_bda = RawAddress::kEmpty;
338     memset(&connecting_dc, 0, sizeof(connecting_dc));
339 
340     acl_cb_ = {};
341     neighbor = {};
342 
343     page_queue = fixed_queue_new(SIZE_MAX);
344     sec_pending_q = fixed_queue_new(SIZE_MAX);
345     sec_collision_timer = alarm_new("btm.sec_collision_timer");
346     pairing_timer = alarm_new("btm.pairing_timer");
347     execution_wait_timer = alarm_new("btm.execution_wait_timer");
348 
349 #if defined(BTM_INITIAL_TRACE_LEVEL)
350     trace_level = BTM_INITIAL_TRACE_LEVEL;
351 #else
352     trace_level = BT_TRACE_LEVEL_NONE; /* No traces */
353 #endif
354     security_mode = initial_security_mode;
355     pairing_bda = RawAddress::kAny;
356     sec_dev_rec = list_new([](void* ptr) {
357       // Invoke destructor for all record objects and reset to default
358       // initialized value so memory may be properly freed
359       *((tBTM_SEC_DEV_REC*)ptr) = {};
360       osi_free(ptr);
361     });
362 
363     /* Initialize BTM component structures */
364     btm_inq_vars.Init(); /* Inquiry Database and Structures */
365     sco_cb.Init();       /* SCO Database and Structures (If included) */
366     devcb.Init();
367 
368     history_ = std::make_shared<TimestampedStringCircularBuffer>(
369         kBtmLogHistoryBufferSize);
370     CHECK(history_ != nullptr);
371     history_->Push(std::string("Initialized btm history"));
372     btm_available_index = 1;
373   }
374 
FreetBTM_CB375   void Free() {
376     history_.reset();
377 
378     devcb.Free();
379     sco_cb.Free();
380     btm_inq_vars.Free();
381 
382     fixed_queue_free(page_queue, nullptr);
383     page_queue = nullptr;
384 
385     fixed_queue_free(sec_pending_q, nullptr);
386     sec_pending_q = nullptr;
387 
388     list_free(sec_dev_rec);
389     sec_dev_rec = nullptr;
390 
391     alarm_free(sec_collision_timer);
392     sec_collision_timer = nullptr;
393 
394     alarm_free(pairing_timer);
395     pairing_timer = nullptr;
396 
397     alarm_free(execution_wait_timer);
398     execution_wait_timer = nullptr;
399   }
400 
401  private:
402   friend uint8_t BTM_AllocateSCN(void);
403   friend bool BTM_TryAllocateSCN(uint8_t scn);
404   friend bool BTM_FreeSCN(uint8_t scn);
405   uint8_t btm_scn[BTM_MAX_SCN_];
406   uint8_t btm_available_index;
407 } tBTM_CB;
408 
409 /* security action for L2CAP COC channels */
410 #define BTM_SEC_OK 1
411 #define BTM_SEC_ENCRYPT 2         /* encrypt the link with current key */
412 #define BTM_SEC_ENCRYPT_NO_MITM 3 /* unauthenticated encryption or better */
413 #define BTM_SEC_ENCRYPT_MITM 4    /* authenticated encryption */
414 #define BTM_SEC_ENC_PENDING 5     /* wait for link encryption pending */
415 
416 typedef uint8_t tBTM_SEC_ACTION;
417 
418 #endif  // BTM_INT_TYPES_H
419