• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <algorithm>
20 #include <chrono>
21 #include <map>
22 #include <vector>
23 
24 #include "hci/address.h"
25 #include "hci/hci_packets.h"
26 #include "include/phy.h"
27 #include "model/controller/acl_connection_handler.h"
28 #include "model/controller/controller_properties.h"
29 #include "model/controller/le_advertiser.h"
30 #include "model/setup/async_manager.h"
31 #include "packets/link_layer_packets.h"
32 
33 #ifdef ROOTCANAL_LMP
34 extern "C" {
35 struct LinkManager;
36 }
37 #include "lmp.h"
38 #else
39 #include "security_manager.h"
40 #endif /* ROOTCANAL_LMP */
41 
42 namespace rootcanal {
43 
44 using ::bluetooth::hci::Address;
45 using ::bluetooth::hci::AddressType;
46 using ::bluetooth::hci::AuthenticationEnable;
47 using ::bluetooth::hci::ClassOfDevice;
48 using ::bluetooth::hci::ErrorCode;
49 using ::bluetooth::hci::FilterAcceptListAddressType;
50 using ::bluetooth::hci::OpCode;
51 using ::bluetooth::hci::PageScanRepetitionMode;
52 
53 // Create an address with type Public Device Address or Random Device Address.
54 AddressWithType PeerDeviceAddress(Address address,
55                                   PeerAddressType peer_address_type);
56 // Create an address with type Public Identity Address or Random Identity
57 // address.
58 AddressWithType PeerIdentityAddress(Address address,
59                                     PeerAddressType peer_address_type);
60 
61 class LinkLayerController {
62  public:
63   static constexpr size_t kIrkSize = 16;
64 
65   LinkLayerController(const Address& address,
66                       const ControllerProperties& properties);
67 
68   ErrorCode SendCommandToRemoteByAddress(
69       OpCode opcode, bluetooth::packet::PacketView<true> args,
70       const Address& own_address, const Address& peer_address);
71   ErrorCode SendLeCommandToRemoteByAddress(OpCode opcode,
72                                            const Address& own_address,
73                                            const Address& peer_address);
74   ErrorCode SendCommandToRemoteByHandle(
75       OpCode opcode, bluetooth::packet::PacketView<true> args, uint16_t handle);
76   ErrorCode SendScoToRemote(bluetooth::hci::ScoView sco_packet);
77   ErrorCode SendAclToRemote(bluetooth::hci::AclView acl_packet);
78 
79 #ifdef ROOTCANAL_LMP
80   void ForwardToLm(bluetooth::hci::CommandView command);
81 #else
82   void StartSimplePairing(const Address& address);
83   void AuthenticateRemoteStage1(const Address& address,
84                                 PairingType pairing_type);
85   void AuthenticateRemoteStage2(const Address& address);
86   void SaveKeyAndAuthenticate(uint8_t key_type, const Address& peer);
87   ErrorCode LinkKeyRequestReply(const Address& address,
88                                 const std::array<uint8_t, 16>& key);
89   ErrorCode LinkKeyRequestNegativeReply(const Address& address);
90   ErrorCode IoCapabilityRequestReply(const Address& peer, uint8_t io_capability,
91                                      uint8_t oob_data_present_flag,
92                                      uint8_t authentication_requirements);
93   ErrorCode IoCapabilityRequestNegativeReply(const Address& peer,
94                                              ErrorCode reason);
95   ErrorCode PinCodeRequestReply(const Address& peer, std::vector<uint8_t> pin);
96   ErrorCode PinCodeRequestNegativeReply(const Address& peer);
97   ErrorCode UserConfirmationRequestReply(const Address& peer);
98   ErrorCode UserConfirmationRequestNegativeReply(const Address& peer);
99   ErrorCode UserPasskeyRequestReply(const Address& peer,
100                                     uint32_t numeric_value);
101   ErrorCode UserPasskeyRequestNegativeReply(const Address& peer);
102   ErrorCode RemoteOobDataRequestReply(const Address& peer,
103                                       const std::array<uint8_t, 16>& c,
104                                       const std::array<uint8_t, 16>& r);
105   ErrorCode RemoteOobDataRequestNegativeReply(const Address& peer);
106   ErrorCode RemoteOobExtendedDataRequestReply(
107       const Address& peer, const std::array<uint8_t, 16>& c_192,
108       const std::array<uint8_t, 16>& r_192,
109       const std::array<uint8_t, 16>& c_256,
110       const std::array<uint8_t, 16>& r_256);
111   ErrorCode SendKeypressNotification(
112       const Address& peer,
113       bluetooth::hci::KeypressNotificationType notification_type);
114   void HandleSetConnectionEncryption(const Address& address, uint16_t handle,
115                                      uint8_t encryption_enable);
116   ErrorCode SetConnectionEncryption(uint16_t handle, uint8_t encryption_enable);
117   void HandleAuthenticationRequest(const Address& address, uint16_t handle);
118   ErrorCode AuthenticationRequested(uint16_t handle);
119 #endif /* ROOTCANAL_LMP */
120 
121   std::vector<bluetooth::hci::Lap> const& ReadCurrentIacLap() const;
122   void WriteCurrentIacLap(std::vector<bluetooth::hci::Lap> iac_lap);
123 
124   ErrorCode AcceptConnectionRequest(const Address& addr, bool try_role_switch);
125   void MakePeripheralConnection(const Address& addr, bool try_role_switch);
126   ErrorCode RejectConnectionRequest(const Address& addr, uint8_t reason);
127   void RejectPeripheralConnection(const Address& addr, uint8_t reason);
128   ErrorCode CreateConnection(const Address& addr, uint16_t packet_type,
129                              uint8_t page_scan_mode, uint16_t clock_offset,
130                              uint8_t allow_role_switch);
131   ErrorCode CreateConnectionCancel(const Address& addr);
132   ErrorCode Disconnect(uint16_t handle, ErrorCode reason);
133 
134  private:
135   void SendDisconnectionCompleteEvent(uint16_t handle, ErrorCode reason);
136 
137   void IncomingPacketWithRssi(model::packets::LinkLayerPacketView incoming,
138                               uint8_t rssi);
139 
140  public:
141   const Address& GetAddress() const;
142 
143   void IncomingPacket(model::packets::LinkLayerPacketView incoming);
144 
145   void TimerTick();
146 
147   void Close();
148 
149   AsyncTaskId ScheduleTask(std::chrono::milliseconds delay_ms,
150                            const TaskCallback& task);
151 
152   AsyncTaskId SchedulePeriodicTask(std::chrono::milliseconds delay_ms,
153                                    std::chrono::milliseconds period_ms,
154                                    const TaskCallback& callback);
155 
156   void CancelScheduledTask(AsyncTaskId task);
157 
158   // Set the callbacks for sending packets to the HCI.
159   void RegisterEventChannel(
160       const std::function<void(std::shared_ptr<bluetooth::hci::EventBuilder>)>&
161           send_event);
162 
163   void RegisterAclChannel(
164       const std::function<void(std::shared_ptr<bluetooth::hci::AclBuilder>)>&
165           send_acl);
166 
167   void RegisterScoChannel(
168       const std::function<void(std::shared_ptr<bluetooth::hci::ScoBuilder>)>&
169           send_sco);
170 
171   void RegisterIsoChannel(
172       const std::function<void(std::shared_ptr<bluetooth::hci::IsoBuilder>)>&
173           send_iso);
174 
175   void RegisterRemoteChannel(
176       const std::function<void(
177           std::shared_ptr<model::packets::LinkLayerPacketBuilder>, Phy::Type)>&
178           send_to_remote);
179 
180   // Set the callbacks for scheduling tasks.
181   void RegisterTaskScheduler(
182       std::function<AsyncTaskId(std::chrono::milliseconds, const TaskCallback&)>
183           event_scheduler);
184 
185   void RegisterPeriodicTaskScheduler(
186       std::function<AsyncTaskId(std::chrono::milliseconds,
187                                 std::chrono::milliseconds, const TaskCallback&)>
188           periodic_event_scheduler);
189 
190   void RegisterTaskCancel(std::function<void(AsyncTaskId)> cancel);
191   void Reset();
192 
193   void LeAdvertising();
194   void LeScanning();
195 
196   void LeConnectionUpdateComplete(uint16_t handle, uint16_t interval_min,
197                                   uint16_t interval_max, uint16_t latency,
198                                   uint16_t supervision_timeout);
199   ErrorCode LeConnectionUpdate(uint16_t handle, uint16_t interval_min,
200                                uint16_t interval_max, uint16_t latency,
201                                uint16_t supervision_timeout);
202   ErrorCode LeRemoteConnectionParameterRequestReply(
203       uint16_t connection_handle, uint16_t interval_min, uint16_t interval_max,
204       uint16_t timeout, uint16_t latency, uint16_t minimum_ce_length,
205       uint16_t maximum_ce_length);
206   ErrorCode LeRemoteConnectionParameterRequestNegativeReply(
207       uint16_t connection_handle, bluetooth::hci::ErrorCode reason);
208   uint16_t HandleLeConnection(AddressWithType addr, AddressWithType own_addr,
209                               bluetooth::hci::Role role,
210                               uint16_t connection_interval,
211                               uint16_t connection_latency,
212                               uint16_t supervision_timeout,
213                               bool send_le_channel_selection_algorithm_event);
214 
215   bool ResolvingListBusy();
216   bool FilterAcceptListBusy();
217 
218   bool LeFilterAcceptListContainsDevice(
219       FilterAcceptListAddressType address_type, Address address);
220   bool LeFilterAcceptListContainsDevice(AddressWithType address);
221 
222   enum IrkSelection {
223     Peer,  // Use Peer IRK for RPA resolution or generation.
224     Local  // Use Local IRK for RPA resolution or generation.
225   };
226 
227   // If the selected address is a Resolvable Private Address, then
228   // resolve the address using the resolving list. If the address cannot
229   // be resolved none is returned. If the address is not a Resolvable
230   // Private Address, the original address is returned.
231   std::optional<AddressWithType> ResolvePrivateAddress(AddressWithType address,
232                                                        IrkSelection irk);
233 
234   // Generate a Resolvable Private for the selected peer.
235   // If the address is not found in the resolving list none is returned.
236   // `local` indicates whether to use the local (true) or peer (false) IRK when
237   // generating the Resolvable Private Address.
238   std::optional<AddressWithType> GenerateResolvablePrivateAddress(
239       AddressWithType address, IrkSelection irk);
240 
241   // Check if the selected address matches one of the controller's device
242   // addresses (public or random static).
IsLocalPublicOrRandomAddress(AddressWithType address)243   bool IsLocalPublicOrRandomAddress(AddressWithType address) {
244     switch (address.GetAddressType()) {
245       case AddressType::PUBLIC_DEVICE_ADDRESS:
246         return address.GetAddress() == address_;
247       case AddressType::RANDOM_DEVICE_ADDRESS:
248         return address.GetAddress() == random_address_;
249       default:
250         return false;
251     }
252   }
253 
254   void LeReadIsoTxSync(uint16_t handle);
255   void LeSetCigParameters(
256       uint8_t cig_id, uint32_t sdu_interval_m_to_s,
257       uint32_t sdu_interval_s_to_m,
258       bluetooth::hci::ClockAccuracy clock_accuracy,
259       bluetooth::hci::Packing packing, bluetooth::hci::Enable framing,
260       uint16_t max_transport_latency_m_to_s,
261       uint16_t max_transport_latency_s_to_m,
262       std::vector<bluetooth::hci::CisParametersConfig> cis_config);
263   bluetooth::hci::ErrorCode LeCreateCis(
264       std::vector<bluetooth::hci::CreateCisConfig> cis_config);
265   bluetooth::hci::ErrorCode LeRemoveCig(uint8_t cig_id);
266   bluetooth::hci::ErrorCode LeAcceptCisRequest(uint16_t handle);
267   bluetooth::hci::ErrorCode LeRejectCisRequest(
268       uint16_t handle, bluetooth::hci::ErrorCode reason);
269   bluetooth::hci::ErrorCode LeCreateBig(
270       uint8_t big_handle, uint8_t advertising_handle, uint8_t num_bis,
271       uint32_t sdu_interval, uint16_t max_sdu, uint16_t max_transport_latency,
272       uint8_t rtn, bluetooth::hci::SecondaryPhyType phy,
273       bluetooth::hci::Packing packing, bluetooth::hci::Enable framing,
274       bluetooth::hci::Enable encryption,
275       std::array<uint8_t, 16> broadcast_code);
276   bluetooth::hci::ErrorCode LeTerminateBig(uint8_t big_handle,
277                                            bluetooth::hci::ErrorCode reason);
278   bluetooth::hci::ErrorCode LeBigCreateSync(
279       uint8_t big_handle, uint16_t sync_handle,
280       bluetooth::hci::Enable encryption, std::array<uint8_t, 16> broadcast_code,
281       uint8_t mse, uint16_t big_syunc_timeout, std::vector<uint8_t> bis);
282   void LeBigTerminateSync(uint8_t big_handle);
283   bluetooth::hci::ErrorCode LeRequestPeerSca(uint16_t request_handle);
284   void LeSetupIsoDataPath(uint16_t connection_handle,
285                           bluetooth::hci::DataPathDirection data_path_direction,
286                           uint8_t data_path_id, uint64_t codec_id,
287                           uint32_t controller_Delay,
288                           std::vector<uint8_t> codec_configuration);
289   void LeRemoveIsoDataPath(
290       uint16_t connection_handle,
291       bluetooth::hci::RemoveDataPathDirection remove_data_path_direction);
292 
293   void HandleLeEnableEncryption(uint16_t handle, std::array<uint8_t, 8> rand,
294                                 uint16_t ediv, std::array<uint8_t, 16> ltk);
295 
296   ErrorCode LeEnableEncryption(uint16_t handle, std::array<uint8_t, 8> rand,
297                                uint16_t ediv, std::array<uint8_t, 16> ltk);
298 
299   ErrorCode LeLongTermKeyRequestReply(uint16_t handle,
300                                       std::array<uint8_t, 16> ltk);
301 
302   ErrorCode LeLongTermKeyRequestNegativeReply(uint16_t handle);
303 
304   uint8_t LeReadNumberOfSupportedAdvertisingSets();
305 
306   // Classic
307   void StartInquiry(std::chrono::milliseconds timeout);
308   void InquiryCancel();
309   void InquiryTimeout();
310   void SetInquiryMode(uint8_t mode);
311   void SetInquiryLAP(uint64_t lap);
312   void SetInquiryMaxResponses(uint8_t max);
313   void Inquiry();
314 
GetInquiryScanEnable()315   bool GetInquiryScanEnable() { return inquiry_scan_enable_; }
316   void SetInquiryScanEnable(bool enable);
317 
GetPageScanEnable()318   bool GetPageScanEnable() { return page_scan_enable_; }
319   void SetPageScanEnable(bool enable);
320 
321   uint16_t GetPageTimeout();
322   void SetPageTimeout(uint16_t page_timeout);
323 
324   ErrorCode ChangeConnectionPacketType(uint16_t handle, uint16_t types);
325   ErrorCode ChangeConnectionLinkKey(uint16_t handle);
326   ErrorCode CentralLinkKey(uint8_t key_flag);
327   ErrorCode HoldMode(uint16_t handle, uint16_t hold_mode_max_interval,
328                      uint16_t hold_mode_min_interval);
329   ErrorCode SniffMode(uint16_t handle, uint16_t sniff_max_interval,
330                       uint16_t sniff_min_interval, uint16_t sniff_attempt,
331                       uint16_t sniff_timeout);
332   ErrorCode ExitSniffMode(uint16_t handle);
333   ErrorCode QosSetup(uint16_t handle, uint8_t service_type, uint32_t token_rate,
334                      uint32_t peak_bandwidth, uint32_t latency,
335                      uint32_t delay_variation);
336   ErrorCode RoleDiscovery(uint16_t handle, bluetooth::hci::Role* role);
337   ErrorCode SwitchRole(Address bd_addr, bluetooth::hci::Role role);
338   ErrorCode ReadLinkPolicySettings(uint16_t handle, uint16_t* settings);
339   ErrorCode WriteLinkPolicySettings(uint16_t handle, uint16_t settings);
340   ErrorCode FlowSpecification(uint16_t handle, uint8_t flow_direction,
341                               uint8_t service_type, uint32_t token_rate,
342                               uint32_t token_bucket_size,
343                               uint32_t peak_bandwidth, uint32_t access_latency);
344   ErrorCode WriteLinkSupervisionTimeout(uint16_t handle, uint16_t timeout);
345   ErrorCode WriteDefaultLinkPolicySettings(uint16_t settings);
346   void CheckExpiringConnection(uint16_t handle);
347   uint16_t ReadDefaultLinkPolicySettings();
348 
349   void ReadLocalOobData();
350   void ReadLocalOobExtendedData();
351 
352   ErrorCode AddScoConnection(uint16_t connection_handle, uint16_t packet_type,
353                              ScoDatapath datapath);
354   ErrorCode SetupSynchronousConnection(
355       uint16_t connection_handle, uint32_t transmit_bandwidth,
356       uint32_t receive_bandwidth, uint16_t max_latency, uint16_t voice_setting,
357       uint8_t retransmission_effort, uint16_t packet_types,
358       ScoDatapath datapath);
359   ErrorCode AcceptSynchronousConnection(
360       Address bd_addr, uint32_t transmit_bandwidth, uint32_t receive_bandwidth,
361       uint16_t max_latency, uint16_t voice_setting,
362       uint8_t retransmission_effort, uint16_t packet_types);
363   ErrorCode RejectSynchronousConnection(Address bd_addr, uint16_t reason);
364 
365   bool HasAclConnection();
366 
367   void HandleIso(bluetooth::hci::IsoView iso);
368 
369   // LE Commands
370 
371   // HCI LE Set Random Address command (Vol 4, Part E § 7.8.4).
372   ErrorCode LeSetRandomAddress(Address random_address);
373 
374   // HCI LE Set Resolvable Private Address Timeout command
375   // (Vol 4, Part E § 7.8.45).
376   ErrorCode LeSetResolvablePrivateAddressTimeout(uint16_t rpa_timeout);
377 
378   // HCI LE Set Host Feature command (Vol 4, Part E § 7.8.115).
379   ErrorCode LeSetHostFeature(uint8_t bit_number, uint8_t bit_value);
380 
381   // LE Filter Accept List
382 
383   // HCI command LE_Clear_Filter_Accept_List (Vol 4, Part E § 7.8.15).
384   ErrorCode LeClearFilterAcceptList();
385 
386   // HCI command LE_Add_Device_To_Filter_Accept_List (Vol 4, Part E § 7.8.16).
387   ErrorCode LeAddDeviceToFilterAcceptList(
388       FilterAcceptListAddressType address_type, Address address);
389 
390   // HCI command LE_Remove_Device_From_Filter_Accept_List (Vol 4, Part E
391   // § 7.8.17).
392   ErrorCode LeRemoveDeviceFromFilterAcceptList(
393       FilterAcceptListAddressType address_type, Address address);
394 
395   // LE Address Resolving
396 
397   // HCI command LE_Add_Device_To_Resolving_List (Vol 4, Part E § 7.8.38).
398   ErrorCode LeAddDeviceToResolvingList(
399       PeerAddressType peer_identity_address_type, Address peer_identity_address,
400       std::array<uint8_t, kIrkSize> peer_irk,
401       std::array<uint8_t, kIrkSize> local_irk);
402 
403   // HCI command LE_Remove_Device_From_Resolving_List (Vol 4, Part E § 7.8.39).
404   ErrorCode LeRemoveDeviceFromResolvingList(
405       PeerAddressType peer_identity_address_type,
406       Address peer_identity_address);
407 
408   // HCI command LE_Clear_Resolving_List (Vol 4, Part E § 7.8.40).
409   ErrorCode LeClearResolvingList();
410 
411   // HCI command LE_Set_Address_Resolution_Enable (Vol 4, Part E § 7.8.44).
412   ErrorCode LeSetAddressResolutionEnable(bool enable);
413 
414   // HCI command LE_Set_Privacy_Mode (Vol 4, Part E § 7.8.77).
415   ErrorCode LeSetPrivacyMode(PeerAddressType peer_identity_address_type,
416                              Address peer_identity_address,
417                              bluetooth::hci::PrivacyMode privacy_mode);
418 
419   // Legacy Advertising
420 
421   // HCI command LE_Set_Advertising_Parameters (Vol 4, Part E § 7.8.5).
422   ErrorCode LeSetAdvertisingParameters(
423       uint16_t advertising_interval_min, uint16_t advertising_interval_max,
424       bluetooth::hci::AdvertisingType advertising_type,
425       bluetooth::hci::OwnAddressType own_address_type,
426       bluetooth::hci::PeerAddressType peer_address_type, Address peer_address,
427       uint8_t advertising_channel_map,
428       bluetooth::hci::AdvertisingFilterPolicy advertising_filter_policy);
429 
430   // HCI command LE_Set_Advertising_Data (Vol 4, Part E § 7.8.7).
431   ErrorCode LeSetAdvertisingData(const std::vector<uint8_t>& advertising_data);
432 
433   // HCI command LE_Set_Scan_Response_Data (Vol 4, Part E § 7.8.8).
434   ErrorCode LeSetScanResponseData(
435       const std::vector<uint8_t>& scan_response_data);
436 
437   // HCI command LE_Advertising_Enable (Vol 4, Part E § 7.8.9).
438   ErrorCode LeSetAdvertisingEnable(bool advertising_enable);
439 
440   // Extended Advertising
441 
442   // HCI command LE_Set_Advertising_Set_Random_Address (Vol 4, Part E § 7.8.52).
443   ErrorCode LeSetAdvertisingSetRandomAddress(uint8_t advertising_handle,
444                                              Address random_address);
445 
446   // HCI command LE_Set_Advertising_Parameters (Vol 4, Part E § 7.8.53).
447   ErrorCode LeSetExtendedAdvertisingParameters(
448       uint8_t advertising_handle,
449       AdvertisingEventProperties advertising_event_properties,
450       uint16_t primary_advertising_interval_min,
451       uint16_t primary_advertising_interval_max,
452       uint8_t primary_advertising_channel_map,
453       bluetooth::hci::OwnAddressType own_address_type,
454       bluetooth::hci::PeerAddressType peer_address_type, Address peer_address,
455       bluetooth::hci::AdvertisingFilterPolicy advertising_filter_policy,
456       uint8_t advertising_tx_power,
457       bluetooth::hci::PrimaryPhyType primary_advertising_phy,
458       uint8_t secondary_max_skip,
459       bluetooth::hci::SecondaryPhyType secondary_advertising_phy,
460       uint8_t advertising_sid, bool scan_request_notification_enable);
461 
462   // HCI command LE_Set_Extended_Advertising_Data (Vol 4, Part E § 7.8.54).
463   ErrorCode LeSetExtendedAdvertisingData(
464       uint8_t advertising_handle, bluetooth::hci::Operation operation,
465       bluetooth::hci::FragmentPreference fragment_preference,
466       const std::vector<uint8_t>& advertising_data);
467 
468   // HCI command LE_Set_Extended_Scan_Response_Data (Vol 4, Part E § 7.8.55).
469   ErrorCode LeSetExtendedScanResponseData(
470       uint8_t advertising_handle, bluetooth::hci::Operation operation,
471       bluetooth::hci::FragmentPreference fragment_preference,
472       const std::vector<uint8_t>& scan_response_data);
473 
474   // HCI command LE_Set_Extended_Advertising_Enable (Vol 4, Part E § 7.8.56).
475   ErrorCode LeSetExtendedAdvertisingEnable(
476       bool enable, const std::vector<bluetooth::hci::EnabledSet>& sets);
477 
478   // HCI command LE_Remove_Advertising_Set (Vol 4, Part E § 7.8.59).
479   ErrorCode LeRemoveAdvertisingSet(uint8_t advertising_handle);
480 
481   // HCI command LE_Clear_Advertising_Sets (Vol 4, Part E § 7.8.60).
482   ErrorCode LeClearAdvertisingSets();
483 
484   // Legacy Scanning
485 
486   // HCI command LE_Set_Scan_Parameters (Vol 4, Part E § 7.8.10).
487   ErrorCode LeSetScanParameters(
488       bluetooth::hci::LeScanType scan_type, uint16_t scan_interval,
489       uint16_t scan_window, bluetooth::hci::OwnAddressType own_address_type,
490       bluetooth::hci::LeScanningFilterPolicy scanning_filter_policy);
491 
492   // HCI command LE_Set_Scan_Enable (Vol 4, Part E § 7.8.11).
493   ErrorCode LeSetScanEnable(bool enable, bool filter_duplicates);
494 
495   // Extended Scanning
496 
497   // HCI command LE_Set_Extended_Scan_Parameters (Vol 4, Part E § 7.8.64).
498   ErrorCode LeSetExtendedScanParameters(
499       bluetooth::hci::OwnAddressType own_address_type,
500       bluetooth::hci::LeScanningFilterPolicy scanning_filter_policy,
501       uint8_t scanning_phys,
502       std::vector<bluetooth::hci::PhyScanParameters> scanning_phy_parameters);
503 
504   // HCI command LE_Set_Extended_Scan_Enable (Vol 4, Part E § 7.8.65).
505   ErrorCode LeSetExtendedScanEnable(
506       bool enable, bluetooth::hci::FilterDuplicates filter_duplicates,
507       uint16_t duration, uint16_t period);
508 
509   // Legacy Connection
510 
511   // HCI LE Create Connection command (Vol 4, Part E § 7.8.12).
512   ErrorCode LeCreateConnection(
513       uint16_t scan_interval, uint16_t scan_window,
514       bluetooth::hci::InitiatorFilterPolicy initiator_filter_policy,
515       AddressWithType peer_address,
516       bluetooth::hci::OwnAddressType own_address_type,
517       uint16_t connection_interval_min, uint16_t connection_interval_max,
518       uint16_t max_latency, uint16_t supervision_timeout,
519       uint16_t min_ce_length, uint16_t max_ce_length);
520 
521   // HCI LE Create Connection Cancel command (Vol 4, Part E § 7.8.12).
522   ErrorCode LeCreateConnectionCancel();
523 
524   // Extended Connection
525 
526   // HCI LE Extended Create Connection command (Vol 4, Part E § 7.8.66).
527   ErrorCode LeExtendedCreateConnection(
528       bluetooth::hci::InitiatorFilterPolicy initiator_filter_policy,
529       bluetooth::hci::OwnAddressType own_address_type,
530       AddressWithType peer_address, uint8_t initiating_phys,
531       std::vector<bluetooth::hci::LeCreateConnPhyScanParameters>
532           initiating_phy_parameters);
533 
534  protected:
535   void SendLinkLayerPacket(
536       std::unique_ptr<model::packets::LinkLayerPacketBuilder> packet);
537   void SendLeLinkLayerPacket(
538       std::unique_ptr<model::packets::LinkLayerPacketBuilder> packet);
539   void SendLeLinkLayerPacketWithRssi(
540       Address source_address, Address destination_address, uint8_t rssi,
541       std::unique_ptr<model::packets::LinkLayerPacketBuilder> packet);
542 
543   void IncomingAclPacket(model::packets::LinkLayerPacketView packet);
544   void IncomingScoPacket(model::packets::LinkLayerPacketView packet);
545   void IncomingDisconnectPacket(model::packets::LinkLayerPacketView packet);
546   void IncomingEncryptConnection(model::packets::LinkLayerPacketView packet);
547   void IncomingEncryptConnectionResponse(
548       model::packets::LinkLayerPacketView packet);
549   void IncomingInquiryPacket(model::packets::LinkLayerPacketView packet,
550                              uint8_t rssi);
551   void IncomingInquiryResponsePacket(
552       model::packets::LinkLayerPacketView packet);
553 #ifdef ROOTCANAL_LMP
554   void IncomingLmpPacket(model::packets::LinkLayerPacketView packet);
555 #else
556   void IncomingIoCapabilityRequestPacket(
557       model::packets::LinkLayerPacketView packet);
558   void IncomingIoCapabilityResponsePacket(
559       model::packets::LinkLayerPacketView packet);
560   void IncomingIoCapabilityNegativeResponsePacket(
561       model::packets::LinkLayerPacketView packet);
562   void IncomingKeypressNotificationPacket(
563       model::packets::LinkLayerPacketView packet);
564   void IncomingPasskeyPacket(model::packets::LinkLayerPacketView packet);
565   void IncomingPasskeyFailedPacket(model::packets::LinkLayerPacketView packet);
566   void IncomingPinRequestPacket(model::packets::LinkLayerPacketView packet);
567   void IncomingPinResponsePacket(model::packets::LinkLayerPacketView packet);
568 #endif /* ROOTCANAL_LMP */
569   void IncomingIsoPacket(model::packets::LinkLayerPacketView packet);
570   void IncomingIsoConnectionRequestPacket(
571       model::packets::LinkLayerPacketView packet);
572   void IncomingIsoConnectionResponsePacket(
573       model::packets::LinkLayerPacketView packet);
574 
575   void ScanIncomingLeLegacyAdvertisingPdu(
576       model::packets::LeLegacyAdvertisingPduView& pdu, uint8_t rssi);
577   void ScanIncomingLeExtendedAdvertisingPdu(
578       model::packets::LeExtendedAdvertisingPduView& pdu, uint8_t rssi);
579   void ConnectIncomingLeLegacyAdvertisingPdu(
580       model::packets::LeLegacyAdvertisingPduView& pdu);
581   void ConnectIncomingLeExtendedAdvertisingPdu(
582       model::packets::LeExtendedAdvertisingPduView& pdu);
583 
584   void IncomingLeLegacyAdvertisingPdu(
585       model::packets::LinkLayerPacketView packet, uint8_t rssi);
586   void IncomingLeExtendedAdvertisingPdu(
587       model::packets::LinkLayerPacketView packet, uint8_t rssi);
588 
589   void IncomingLeConnectPacket(model::packets::LinkLayerPacketView packet);
590   void IncomingLeConnectCompletePacket(
591       model::packets::LinkLayerPacketView packet);
592   void IncomingLeConnectionParameterRequest(
593       model::packets::LinkLayerPacketView packet);
594   void IncomingLeConnectionParameterUpdate(
595       model::packets::LinkLayerPacketView packet);
596   void IncomingLeEncryptConnection(model::packets::LinkLayerPacketView packet);
597   void IncomingLeEncryptConnectionResponse(
598       model::packets::LinkLayerPacketView packet);
599   void IncomingLeReadRemoteFeatures(model::packets::LinkLayerPacketView packet);
600   void IncomingLeReadRemoteFeaturesResponse(
601       model::packets::LinkLayerPacketView packet);
602 
603   void ProcessIncomingLegacyScanRequest(
604       AddressWithType scanning_address,
605       AddressWithType resolved_scanning_address,
606       AddressWithType advertising_address);
607   void ProcessIncomingExtendedScanRequest(
608       ExtendedAdvertiser const& advertiser, AddressWithType scanning_address,
609       AddressWithType resolved_scanning_address,
610       AddressWithType advertising_address);
611 
612   bool ProcessIncomingLegacyConnectRequest(
613       model::packets::LeConnectView const& connect_ind);
614   bool ProcessIncomingExtendedConnectRequest(
615       ExtendedAdvertiser& advertiser,
616       model::packets::LeConnectView const& connect_ind);
617 
618   void IncomingLeScanPacket(model::packets::LinkLayerPacketView packet);
619 
620   void IncomingLeScanResponsePacket(model::packets::LinkLayerPacketView packet,
621                                     uint8_t rssi);
622   void IncomingPagePacket(model::packets::LinkLayerPacketView packet);
623   void IncomingPageRejectPacket(model::packets::LinkLayerPacketView packet);
624   void IncomingPageResponsePacket(model::packets::LinkLayerPacketView packet);
625   void IncomingReadRemoteLmpFeatures(
626       model::packets::LinkLayerPacketView packet);
627   void IncomingReadRemoteLmpFeaturesResponse(
628       model::packets::LinkLayerPacketView packet);
629   void IncomingReadRemoteSupportedFeatures(
630       model::packets::LinkLayerPacketView packet);
631   void IncomingReadRemoteSupportedFeaturesResponse(
632       model::packets::LinkLayerPacketView packet);
633   void IncomingReadRemoteExtendedFeatures(
634       model::packets::LinkLayerPacketView packet);
635   void IncomingReadRemoteExtendedFeaturesResponse(
636       model::packets::LinkLayerPacketView packet);
637   void IncomingReadRemoteVersion(model::packets::LinkLayerPacketView packet);
638   void IncomingReadRemoteVersionResponse(
639       model::packets::LinkLayerPacketView packet);
640   void IncomingReadClockOffset(model::packets::LinkLayerPacketView packet);
641   void IncomingReadClockOffsetResponse(
642       model::packets::LinkLayerPacketView packet);
643   void IncomingRemoteNameRequest(model::packets::LinkLayerPacketView packet);
644   void IncomingRemoteNameRequestResponse(
645       model::packets::LinkLayerPacketView packet);
646 
647   void IncomingScoConnectionRequest(model::packets::LinkLayerPacketView packet);
648   void IncomingScoConnectionResponse(
649       model::packets::LinkLayerPacketView packet);
650   void IncomingScoDisconnect(model::packets::LinkLayerPacketView packet);
651 
652   void IncomingPingRequest(model::packets::LinkLayerPacketView packet);
653 
654  public:
655   bool IsEventUnmasked(bluetooth::hci::EventCode event) const;
656   bool IsLeEventUnmasked(bluetooth::hci::SubeventCode subevent) const;
657 
658   // TODO
659   // The Clock Offset should be specific to an ACL connection.
660   // Returning a proper value is not that important.
GetClockOffset()661   uint32_t GetClockOffset() const { return 0; }
662 
663   // TODO
664   // The Page Scan Repetition Mode should be specific to an ACL connection or
665   // a paging session.
GetPageScanRepetitionMode()666   PageScanRepetitionMode GetPageScanRepetitionMode() const {
667     return page_scan_repetition_mode_;
668   }
669 
670   // TODO
671   // The Encryption Key Size should be specific to an ACL connection.
GetEncryptionKeySize()672   uint8_t GetEncryptionKeySize() const { return min_encryption_key_size_; }
673 
GetScoFlowControlEnable()674   bool GetScoFlowControlEnable() const { return sco_flow_control_enable_; }
675 
GetAuthenticationEnable()676   AuthenticationEnable GetAuthenticationEnable() {
677     return authentication_enable_;
678   }
679 
GetLocalName()680   std::array<uint8_t, 248> const& GetLocalName() { return local_name_; }
681 
GetLeSupportedFeatures()682   uint64_t GetLeSupportedFeatures() const {
683     return properties_.le_features | le_host_supported_features_;
684   }
685 
GetConnectionAcceptTimeout()686   uint16_t GetConnectionAcceptTimeout() const {
687     return connection_accept_timeout_;
688   }
689 
GetVoiceSetting()690   uint16_t GetVoiceSetting() const { return voice_setting_; }
GetClassOfDevice()691   const ClassOfDevice& GetClassOfDevice() const { return class_of_device_; }
692 
GetMaxLmpFeaturesPageNumber()693   uint8_t GetMaxLmpFeaturesPageNumber() {
694     return properties_.lmp_features.size() - 1;
695   }
696 
697   uint64_t GetLmpFeatures(uint8_t page_number = 0) {
698     return page_number == 1 ? host_supported_features_
699                             : properties_.lmp_features[page_number];
700   }
701 
702   void SetLocalName(std::vector<uint8_t> const& local_name);
703   void SetLocalName(std::array<uint8_t, 248> const& local_name);
704   void SetExtendedInquiryResponse(
705       std::vector<uint8_t> const& extended_inquiry_response);
706 
SetClassOfDevice(ClassOfDevice class_of_device)707   void SetClassOfDevice(ClassOfDevice class_of_device) {
708     class_of_device_ = class_of_device;
709   }
710 
SetClassOfDevice(uint32_t class_of_device)711   void SetClassOfDevice(uint32_t class_of_device) {
712     class_of_device_.cod[0] = class_of_device & 0xff;
713     class_of_device_.cod[1] = (class_of_device >> 8) & 0xff;
714     class_of_device_.cod[2] = (class_of_device >> 16) & 0xff;
715   }
716 
SetAuthenticationEnable(AuthenticationEnable enable)717   void SetAuthenticationEnable(AuthenticationEnable enable) {
718     authentication_enable_ = enable;
719   }
720 
SetScoFlowControlEnable(bool enable)721   void SetScoFlowControlEnable(bool enable) {
722     sco_flow_control_enable_ = enable;
723   }
SetVoiceSetting(uint16_t voice_setting)724   void SetVoiceSetting(uint16_t voice_setting) {
725     voice_setting_ = voice_setting;
726   }
SetEventMask(uint64_t event_mask)727   void SetEventMask(uint64_t event_mask) { event_mask_ = event_mask; }
728 
SetEventMaskPage2(uint64_t event_mask)729   void SetEventMaskPage2(uint64_t event_mask) {
730     event_mask_page_2_ = event_mask;
731   }
SetLeEventMask(uint64_t le_event_mask)732   void SetLeEventMask(uint64_t le_event_mask) {
733     le_event_mask_ = le_event_mask;
734   }
735 
736   void SetLeHostSupport(bool enable);
737   void SetSecureSimplePairingSupport(bool enable);
738   void SetSecureConnectionsSupport(bool enable);
739 
SetConnectionAcceptTimeout(uint16_t timeout)740   void SetConnectionAcceptTimeout(uint16_t timeout) {
741     connection_accept_timeout_ = timeout;
742   }
743 
LegacyAdvertising()744   bool LegacyAdvertising() const { return legacy_advertising_in_use_; }
ExtendedAdvertising()745   bool ExtendedAdvertising() const { return extended_advertising_in_use_; }
746 
SelectLegacyAdvertising()747   bool SelectLegacyAdvertising() {
748     if (extended_advertising_in_use_) {
749       return false;
750     } else {
751       legacy_advertising_in_use_ = true;
752       return true;
753     }
754   }
755 
SelectExtendedAdvertising()756   bool SelectExtendedAdvertising() {
757     if (legacy_advertising_in_use_) {
758       return false;
759     } else {
760       extended_advertising_in_use_ = true;
761       return true;
762     }
763   }
764 
GetLeSuggestedMaxTxOctets()765   uint16_t GetLeSuggestedMaxTxOctets() const {
766     return le_suggested_max_tx_octets_;
767   }
GetLeSuggestedMaxTxTime()768   uint16_t GetLeSuggestedMaxTxTime() const { return le_suggested_max_tx_time_; }
769 
SetLeSuggestedMaxTxOctets(uint16_t max_tx_octets)770   void SetLeSuggestedMaxTxOctets(uint16_t max_tx_octets) {
771     le_suggested_max_tx_octets_ = max_tx_octets;
772   }
SetLeSuggestedMaxTxTime(uint16_t max_tx_time)773   void SetLeSuggestedMaxTxTime(uint16_t max_tx_time) {
774     le_suggested_max_tx_time_ = max_tx_time;
775   }
776 
777   AsyncTaskId StartScoStream(Address address);
778 
779  private:
780   const Address& address_;
781   const ControllerProperties& properties_;
782 
783   // Host Supported Features (Vol 2, Part C § 3.3 Feature Mask Definition).
784   // Page 1 of the LMP feature mask.
785   uint64_t host_supported_features_{0};
786   bool le_host_support_{false};
787   bool secure_simple_pairing_host_support_{false};
788   bool secure_connections_host_support_{false};
789 
790   // Le Host Supported Features (Vol 4, Part E § 7.8.3).
791   // Specifies the bits indicating Host support.
792   uint64_t le_host_supported_features_{0};
793   bool connected_isochronous_stream_host_support_{false};
794   bool connection_subrating_host_support_{false};
795 
796   // LE Random Address (Vol 4, Part E § 7.8.4).
797   Address random_address_{Address::kEmpty};
798 
799   // HCI configuration parameters.
800   //
801   // Provide the current HCI Configuration Parameters as defined in section
802   // Vol 4, Part E § 6 of the core specification.
803 
804   // Scan Enable (Vol 4, Part E § 6.1).
805   bool page_scan_enable_{false};
806   bool inquiry_scan_enable_{false};
807 
808   // Inquiry Scan Interval and Window
809   // (Vol 4, Part E § 6.2, 6.3).
810   uint16_t inquiry_scan_interval_{0x1000};
811   uint16_t inquiry_scan_window_{0x0012};
812 
813   // Page Timeout (Vol 4, Part E § 6.6).
814   uint16_t page_timeout_{0x2000};
815 
816   // Connection Accept Timeout (Vol 4, Part E § 6.7).
817   uint16_t connection_accept_timeout_{0x1FA0};
818 
819   // Page Scan Interval and Window
820   // (Vol 4, Part E § 6.8, 6.9).
821   uint16_t page_scan_interval_{0x0800};
822   uint16_t page_scan_window_{0x0012};
823 
824   // Voice Setting (Vol 4, Part E § 6.12).
825   uint16_t voice_setting_{0x0060};
826 
827   // Authentication Enable (Vol 4, Part E § 6.16).
828   AuthenticationEnable authentication_enable_{
829       AuthenticationEnable::NOT_REQUIRED};
830 
831   // Default Link Policy Settings (Vol 4, Part E § 6.18).
832   uint8_t default_link_policy_settings_{0x0000};
833 
834   // Synchronous Flow Control Enable (Vol 4, Part E § 6.22).
835   bool sco_flow_control_enable_{false};
836 
837   // Local Name (Vol 4, Part E § 6.23).
838   std::array<uint8_t, 248> local_name_{};
839 
840   // Extended Inquiry Response (Vol 4, Part E § 6.24).
841   std::array<uint8_t, 240> extended_inquiry_response_{};
842 
843   // Class of Device (Vol 4, Part E § 6.26).
844   ClassOfDevice class_of_device_{{0, 0, 0}};
845 
846   // Other configuration parameters.
847 
848   // Current IAC LAP (Vol 4, Part E § 7.3.44).
849   std::vector<bluetooth::hci::Lap> current_iac_lap_list_{};
850 
851   // Min Encryption Key Size (Vol 4, Part E § 7.3.102).
852   uint8_t min_encryption_key_size_{16};
853 
854   // Event Mask (Vol 4, Part E § 7.3.1) and
855   // Event Mask Page 2 (Vol 4, Part E § 7.3.69) and
856   // LE Event Mask (Vol 4, Part E § 7.8.1).
857   uint64_t event_mask_{0x00001fffffffffff};
858   uint64_t event_mask_page_2_{0x0};
859   uint64_t le_event_mask_{0x01f};
860 
861   // Suggested Default Data Length (Vol 4, Part E § 7.8.34).
862   uint16_t le_suggested_max_tx_octets_{0x001b};
863   uint16_t le_suggested_max_tx_time_{0x0148};
864 
865   // Resolvable Private Address Timeout (Vol 4, Part E § 7.8.45).
866   std::chrono::seconds resolvable_private_address_timeout_{0x0384};
867 
868   // Page Scan Repetition Mode (Vol 2 Part B § 8.3.1 Page Scan substate).
869   // The Page Scan Repetition Mode depends on the selected Page Scan Interval.
870   PageScanRepetitionMode page_scan_repetition_mode_{PageScanRepetitionMode::R0};
871 
872   AclConnectionHandler connections_;
873 
874   // Callbacks to schedule tasks.
875   std::function<AsyncTaskId(std::chrono::milliseconds, const TaskCallback&)>
876       schedule_task_;
877   std::function<AsyncTaskId(std::chrono::milliseconds,
878                             std::chrono::milliseconds, const TaskCallback&)>
879       schedule_periodic_task_;
880   std::function<void(AsyncTaskId)> cancel_task_;
881 
882   // Callbacks to send packets back to the HCI.
883   std::function<void(std::shared_ptr<bluetooth::hci::AclBuilder>)> send_acl_;
884   std::function<void(std::shared_ptr<bluetooth::hci::EventBuilder>)>
885       send_event_;
886   std::function<void(std::shared_ptr<bluetooth::hci::ScoBuilder>)> send_sco_;
887   std::function<void(std::shared_ptr<bluetooth::hci::IsoBuilder>)> send_iso_;
888 
889   // Callback to send packets to remote devices.
890   std::function<void(std::shared_ptr<model::packets::LinkLayerPacketBuilder>,
891                      Phy::Type phy_type)>
892       send_to_remote_;
893 
894   uint32_t oob_id_{1};
895   uint32_t key_id_{1};
896 
897   struct FilterAcceptListEntry {
898     FilterAcceptListAddressType address_type;
899     Address address;
900   };
901 
902   std::vector<FilterAcceptListEntry> le_filter_accept_list_;
903 
904   struct ResolvingListEntry {
905     PeerAddressType peer_identity_address_type;
906     Address peer_identity_address;
907     std::array<uint8_t, kIrkSize> peer_irk;
908     std::array<uint8_t, kIrkSize> local_irk;
909     bluetooth::hci::PrivacyMode privacy_mode;
910   };
911 
912   std::vector<ResolvingListEntry> le_resolving_list_;
913   bool le_resolving_list_enabled_{false};
914 
915   // Flag set when any legacy advertising command has been received
916   // since the last power-on-reset.
917   // From Vol 4, Part E § 3.1.1 Legacy and extended advertising,
918   // extended advertising are rejected when this bit is set.
919   bool legacy_advertising_in_use_{false};
920 
921   // Flag set when any extended advertising command has been received
922   // since the last power-on-reset.
923   // From Vol 4, Part E § 3.1.1 Legacy and extended advertising,
924   // legacy advertising are rejected when this bit is set.
925   bool extended_advertising_in_use_{false};
926 
927   // Legacy advertising state.
928   LegacyAdvertiser legacy_advertiser_{};
929 
930   // Extended advertising sets.
931   std::unordered_map<uint8_t, ExtendedAdvertiser> extended_advertisers_{};
932 
933   struct Scanner {
934     bool scan_enable;
935     std::chrono::steady_clock::duration period;
936     std::chrono::steady_clock::duration duration;
937     bluetooth::hci::FilterDuplicates filter_duplicates;
938     bluetooth::hci::OwnAddressType own_address_type;
939     bluetooth::hci::LeScanningFilterPolicy scan_filter_policy;
940 
941     struct PhyParameters {
942       bool enabled;
943       bluetooth::hci::LeScanType scan_type;
944       uint16_t scan_interval;
945       uint16_t scan_window;
946     };
947 
948     PhyParameters le_1m_phy;
949     PhyParameters le_coded_phy;
950 
951     // Save information about the advertising PDU being scanned.
952     bool connectable_scan_response;
953     std::optional<AddressWithType> pending_scan_request{};
954 
955     // Time keeping
956     std::optional<std::chrono::steady_clock::time_point> timeout;
957     std::optional<std::chrono::steady_clock::time_point> periodical_timeout;
958 
959     // Packet History
960     std::vector<model::packets::LinkLayerPacketView> history;
961 
IsEnabledScanner962     bool IsEnabled() const { return scan_enable; }
963 
IsPacketInHistoryScanner964     bool IsPacketInHistory(model::packets::LinkLayerPacketView packet) const {
965       return std::any_of(
966           history.begin(), history.end(),
967           [packet](model::packets::LinkLayerPacketView const& a) {
968             return a.size() == packet.size() &&
969                    std::equal(a.begin(), a.end(), packet.begin());
970           });
971     }
AddPacketToHistoryScanner972     void AddPacketToHistory(model::packets::LinkLayerPacketView packet) {
973       history.push_back(packet);
974     }
975   };
976 
977   // Legacy and extended scanning properties.
978   // Legacy and extended scanning are disambiguated by the use
979   // of legacy_advertising_in_use_ and extended_advertising_in_use_ flags.
980   // Only one type of advertising may be used during a controller session.
981   Scanner scanner_{};
982 
983   struct Initiator {
984     bool connect_enable;
985     bluetooth::hci::InitiatorFilterPolicy initiator_filter_policy;
986     bluetooth::hci::AddressWithType peer_address{};
987     bluetooth::hci::OwnAddressType own_address_type;
988 
989     struct PhyParameters {
990       bool enabled;
991       uint16_t scan_interval;
992       uint16_t scan_window;
993       uint16_t connection_interval_min;
994       uint16_t connection_interval_max;
995       uint16_t max_latency;
996       uint16_t supervision_timeout;
997       uint16_t min_ce_length;
998       uint16_t max_ce_length;
999     };
1000 
1001     PhyParameters le_1m_phy;
1002     PhyParameters le_2m_phy;
1003     PhyParameters le_coded_phy;
1004 
1005     // Save information about the ongoing connection.
1006     Address initiating_address{};  // TODO: AddressWithType
1007     std::optional<AddressWithType> pending_connect_request{};
1008 
IsEnabledInitiator1009     bool IsEnabled() const { return connect_enable; }
DisableInitiator1010     void Disable() { connect_enable = false; }
1011   };
1012 
1013   // Legacy and extended initiating properties.
1014   // Legacy and extended initiating are disambiguated by the use
1015   // of legacy_advertising_in_use_ and extended_advertising_in_use_ flags.
1016   // Only one type of advertising may be used during a controller session.
1017   Initiator initiator_{};
1018 
1019   // Classic state
1020 #ifdef ROOTCANAL_LMP
1021   std::unique_ptr<const LinkManager, void (*)(const LinkManager*)> lm_;
1022   struct LinkManagerOps ops_;
1023 #else
1024   SecurityManager security_manager_{10};
1025 #endif /* ROOTCANAL_LMP */
1026 
1027   AsyncTaskId page_timeout_task_id_ = kInvalidTaskId;
1028 
1029   std::chrono::steady_clock::time_point last_inquiry_;
1030   model::packets::InquiryType inquiry_mode_{
1031       model::packets::InquiryType::STANDARD};
1032   AsyncTaskId inquiry_timer_task_id_ = kInvalidTaskId;
1033   uint64_t inquiry_lap_{};
1034   uint8_t inquiry_max_responses_{};
1035 };
1036 
1037 }  // namespace rootcanal
1038