• 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 "acl_connection_handler.h"
20 #include "include/hci.h"
21 #include "include/inquiry.h"
22 #include "include/link.h"
23 #include "include/phy.h"
24 #include "model/devices/device_properties.h"
25 #include "model/setup/async_manager.h"
26 #include "packets/hci/acl_packet_view.h"
27 #include "packets/hci/sco_packet_view.h"
28 #include "packets/link_layer/link_layer_packet_builder.h"
29 #include "packets/link_layer/link_layer_packet_view.h"
30 #include "security_manager.h"
31 #include "types/address.h"
32 
33 namespace test_vendor_lib {
34 
35 class LinkLayerController {
36  public:
LinkLayerController(const DeviceProperties & properties)37   LinkLayerController(const DeviceProperties& properties) : properties_(properties) {}
38   hci::Status SendCommandToRemoteByAddress(hci::OpCode opcode, packets::PacketView<true> args, const Address& remote,
39                                            bool use_public_address);
40   hci::Status SendCommandToRemoteByHandle(hci::OpCode opcode, packets::PacketView<true> args, uint16_t handle);
41   hci::Status SendScoToRemote(packets::ScoPacketView sco_packet);
42   hci::Status SendAclToRemote(packets::AclPacketView acl_packet);
43 
44   void WriteSimplePairingMode(bool enabled);
45   void StartSimplePairing(const Address& address);
46   void AuthenticateRemoteStage1(const Address& address, PairingType pairing_type);
47   void AuthenticateRemoteStage2(const Address& address);
48   hci::Status LinkKeyRequestReply(const Address& address, packets::PacketView<true> key);
49   hci::Status LinkKeyRequestNegativeReply(const Address& address);
50   hci::Status IoCapabilityRequestReply(const Address& peer, uint8_t io_capability, uint8_t oob_data_present_flag,
51                                        uint8_t authentication_requirements);
52   hci::Status IoCapabilityRequestNegativeReply(const Address& peer, hci::Status reason);
53   hci::Status UserConfirmationRequestReply(const Address& peer);
54   hci::Status UserConfirmationRequestNegativeReply(const Address& peer);
55   hci::Status UserPasskeyRequestReply(const Address& peer, uint32_t numeric_value);
56   hci::Status UserPasskeyRequestNegativeReply(const Address& peer);
57   hci::Status RemoteOobDataRequestReply(const Address& peer, const std::vector<uint8_t>& c,
58                                         const std::vector<uint8_t>& r);
59   hci::Status RemoteOobDataRequestNegativeReply(const Address& peer);
60   void HandleSetConnectionEncryption(const Address& address, uint16_t handle, uint8_t encryption_enable);
61   hci::Status SetConnectionEncryption(uint16_t handle, uint8_t encryption_enable);
62   void HandleAuthenticationRequest(const Address& address, uint16_t handle);
63   hci::Status AuthenticationRequested(uint16_t handle);
64 
65   hci::Status AcceptConnectionRequest(const Address& addr, bool try_role_switch);
66   void MakeSlaveConnection(const Address& addr, bool try_role_switch);
67   hci::Status RejectConnectionRequest(const Address& addr, uint8_t reason);
68   void RejectSlaveConnection(const Address& addr, uint8_t reason);
69   hci::Status CreateConnection(const Address& addr, uint16_t packet_type, uint8_t page_scan_mode, uint16_t clock_offset,
70                                uint8_t allow_role_switch);
71   hci::Status CreateConnectionCancel(const Address& addr);
72   hci::Status Disconnect(uint16_t handle, uint8_t reason);
73 
74  private:
75   void DisconnectCleanup(uint16_t handle, uint8_t reason);
76 
77  public:
78   void IncomingPacket(packets::LinkLayerPacketView incoming);
79 
80   void TimerTick();
81 
82   // Set the callbacks for sending packets to the HCI.
83   void RegisterEventChannel(const std::function<void(std::shared_ptr<std::vector<uint8_t>>)>& send_event);
84 
85   void RegisterAclChannel(const std::function<void(std::shared_ptr<std::vector<uint8_t>>)>& send_acl);
86 
87   void RegisterScoChannel(const std::function<void(std::shared_ptr<std::vector<uint8_t>>)>& send_sco);
88 
89   void RegisterRemoteChannel(
90       const std::function<void(std::shared_ptr<packets::LinkLayerPacketBuilder>, Phy::Type)>& send_to_remote);
91 
92   // Set the callbacks for scheduling tasks.
93   void RegisterTaskScheduler(
94       std::function<AsyncTaskId(std::chrono::milliseconds, const TaskCallback&)> event_scheduler);
95 
96   void RegisterPeriodicTaskScheduler(
97       std::function<AsyncTaskId(std::chrono::milliseconds, std::chrono::milliseconds, const TaskCallback&)>
98           periodic_event_scheduler);
99 
100   void RegisterTaskCancel(std::function<void(AsyncTaskId)> cancel);
101   void Reset();
102   void AddControllerEvent(std::chrono::milliseconds delay, const TaskCallback& task);
103 
104   void PageScan();
105   void Connections();
106 
107   void LeWhiteListClear();
108   void LeWhiteListAddDevice(Address addr, uint8_t addr_type);
109   void LeWhiteListRemoveDevice(Address addr, uint8_t addr_type);
110   bool LeWhiteListContainsDevice(Address addr, uint8_t addr_type);
111   bool LeWhiteListFull();
112 
SetLeScanEnable(uint8_t le_scan_enable)113   void SetLeScanEnable(uint8_t le_scan_enable) {
114     le_scan_enable_ = le_scan_enable;
115   }
SetLeScanType(uint8_t le_scan_type)116   void SetLeScanType(uint8_t le_scan_type) {
117     le_scan_type_ = le_scan_type;
118   }
SetLeScanInterval(uint16_t le_scan_interval)119   void SetLeScanInterval(uint16_t le_scan_interval) {
120     le_scan_interval_ = le_scan_interval;
121   }
SetLeScanWindow(uint16_t le_scan_window)122   void SetLeScanWindow(uint16_t le_scan_window) {
123     le_scan_window_ = le_scan_window;
124   }
SetLeScanFilterPolicy(uint8_t le_scan_filter_policy)125   void SetLeScanFilterPolicy(uint8_t le_scan_filter_policy) {
126     le_scan_filter_policy_ = le_scan_filter_policy;
127   }
SetLeFilterDuplicates(uint8_t le_scan_filter_duplicates)128   void SetLeFilterDuplicates(uint8_t le_scan_filter_duplicates) {
129     le_scan_filter_duplicates_ = le_scan_filter_duplicates;
130   }
SetLeAddressType(uint8_t le_address_type)131   void SetLeAddressType(uint8_t le_address_type) {
132     le_address_type_ = le_address_type;
133   }
SetLeConnect(bool le_connect)134   hci::Status SetLeConnect(bool le_connect) {
135     le_connect_ = le_connect;
136     return hci::Status::SUCCESS;
137   }
SetLeConnectionIntervalMin(uint16_t min)138   void SetLeConnectionIntervalMin(uint16_t min) {
139     le_connection_interval_min_ = min;
140   }
SetLeConnectionIntervalMax(uint16_t max)141   void SetLeConnectionIntervalMax(uint16_t max) {
142     le_connection_interval_max_ = max;
143   }
SetLeConnectionLatency(uint16_t latency)144   void SetLeConnectionLatency(uint16_t latency) {
145     le_connection_latency_ = latency;
146   }
SetLeSupervisionTimeout(uint16_t timeout)147   void SetLeSupervisionTimeout(uint16_t timeout) {
148     le_connection_supervision_timeout_ = timeout;
149   }
SetLeMinimumCeLength(uint16_t min)150   void SetLeMinimumCeLength(uint16_t min) {
151     le_connection_minimum_ce_length_ = min;
152   }
SetLeMaximumCeLength(uint16_t max)153   void SetLeMaximumCeLength(uint16_t max) {
154     le_connection_maximum_ce_length_ = max;
155   }
SetLeInitiatorFilterPolicy(uint8_t le_initiator_filter_policy)156   void SetLeInitiatorFilterPolicy(uint8_t le_initiator_filter_policy) {
157     le_initiator_filter_policy_ = le_initiator_filter_policy;
158   }
SetLePeerAddressType(uint8_t peer_address_type)159   void SetLePeerAddressType(uint8_t peer_address_type) {
160     le_peer_address_type_ = peer_address_type;
161   }
SetLePeerAddress(const Address & peer_address)162   void SetLePeerAddress(const Address& peer_address) {
163     le_peer_address_ = peer_address;
164   }
165 
166   // Classic
167   void StartInquiry(std::chrono::milliseconds timeout);
168   void InquiryCancel();
169   void InquiryTimeout();
170   void SetInquiryMode(uint8_t mode);
171   void SetInquiryLAP(uint64_t lap);
172   void SetInquiryMaxResponses(uint8_t max);
173   void Inquiry();
174 
175   void SetInquiryScanEnable(bool enable);
176   void SetPageScanEnable(bool enable);
177 
178   hci::Status ChangeConnectionPacketType(uint16_t handle, uint16_t types);
179   hci::Status WriteLinkPolicySettings(uint16_t handle, uint16_t settings);
180   hci::Status WriteLinkSupervisionTimeout(uint16_t handle, uint16_t timeout);
181 
182  protected:
183   void SendLELinkLayerPacket(std::shared_ptr<packets::LinkLayerPacketBuilder> packet);
184   void SendLinkLayerPacket(std::shared_ptr<packets::LinkLayerPacketBuilder> packet);
185   void IncomingAclPacket(packets::LinkLayerPacketView packet);
186   void IncomingAclAckPacket(packets::LinkLayerPacketView packet);
187   void IncomingCommandPacket(packets::LinkLayerPacketView packet);
188   void IncomingCreateConnectionPacket(packets::LinkLayerPacketView packet);
189   void IncomingDisconnectPacket(packets::LinkLayerPacketView packet);
190   void IncomingEncryptConnection(packets::LinkLayerPacketView packet);
191   void IncomingEncryptConnectionResponse(packets::LinkLayerPacketView packet);
192   void IncomingInquiryPacket(packets::LinkLayerPacketView packet);
193   void IncomingInquiryResponsePacket(packets::LinkLayerPacketView packet);
194   void IncomingIoCapabilityRequestPacket(packets::LinkLayerPacketView packet);
195   void IncomingIoCapabilityResponsePacket(packets::LinkLayerPacketView packet);
196   void IncomingIoCapabilityNegativeResponsePacket(packets::LinkLayerPacketView packet);
197   void IncomingLeAdvertisementPacket(packets::LinkLayerPacketView packet);
198   void IncomingLeScanPacket(packets::LinkLayerPacketView packet);
199   void IncomingLeScanResponsePacket(packets::LinkLayerPacketView packet);
200   void IncomingPagePacket(packets::LinkLayerPacketView packet);
201   void IncomingPageResponsePacket(packets::LinkLayerPacketView packet);
202   void IncomingResponsePacket(packets::LinkLayerPacketView packet);
203 
204  private:
205   const DeviceProperties& properties_;
206   AclConnectionHandler classic_connections_;
207   // Add timestamps?
208   std::vector<std::shared_ptr<packets::LinkLayerPacketBuilder>> commands_awaiting_responses_;
209 
210   // Timing related state
211   std::vector<AsyncTaskId> controller_events_;
212   AsyncTaskId timer_tick_task_;
213   std::chrono::milliseconds timer_period_ = std::chrono::milliseconds(100);
214 
215   // Callbacks to schedule tasks.
216   std::function<AsyncTaskId(std::chrono::milliseconds, const TaskCallback&)> schedule_task_;
217   std::function<AsyncTaskId(std::chrono::milliseconds, std::chrono::milliseconds, const TaskCallback&)>
218       schedule_periodic_task_;
219   std::function<void(AsyncTaskId)> cancel_task_;
220 
221   // Callbacks to send packets back to the HCI.
222   std::function<void(std::shared_ptr<std::vector<uint8_t>>)> send_acl_;
223   std::function<void(std::shared_ptr<std::vector<uint8_t>>)> send_event_;
224   std::function<void(std::shared_ptr<std::vector<uint8_t>>)> send_sco_;
225 
226   // Callback to send packets to remote devices.
227   std::function<void(std::shared_ptr<packets::LinkLayerPacketBuilder>, Phy::Type phy_type)> send_to_remote_;
228 
229   // LE state
230   std::vector<uint8_t> le_event_mask_;
231 
232   std::vector<std::tuple<Address, uint8_t>> le_white_list_;
233 
234   uint8_t le_scan_enable_;
235   uint8_t le_scan_type_;
236   uint16_t le_scan_interval_;
237   uint16_t le_scan_window_;
238   uint8_t le_scan_filter_policy_;
239   uint8_t le_scan_filter_duplicates_;
240   uint8_t le_address_type_;
241 
242   bool le_connect_;
243   uint16_t le_connection_interval_min_;
244   uint16_t le_connection_interval_max_;
245   uint16_t le_connection_latency_;
246   uint16_t le_connection_supervision_timeout_;
247   uint16_t le_connection_minimum_ce_length_;
248   uint16_t le_connection_maximum_ce_length_;
249   uint8_t le_initiator_filter_policy_;
250 
251   Address le_peer_address_;
252   uint8_t le_peer_address_type_;
253 
254   // Classic state
255 
256   SecurityManager security_manager_{10};
257   std::chrono::steady_clock::time_point last_inquiry_;
258   Inquiry::InquiryType inquiry_mode_;
259   Inquiry::InquiryState inquiry_state_;
260   uint64_t inquiry_lap_;
261   uint8_t inquiry_max_responses_;
262 
263   bool page_scans_enabled_{false};
264   bool inquiry_scans_enabled_{false};
265 
266   bool simple_pairing_mode_enabled_{false};
267 };
268 
269 }  // namespace test_vendor_lib
270