• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2015 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 <unistd.h>
20 #include <cstdint>
21 #include <memory>
22 #include <string>
23 #include <unordered_map>
24 #include <vector>
25 
26 #include "base/time/time.h"
27 #include "link_layer_controller.h"
28 #include "model/devices/device.h"
29 #include "model/setup/async_manager.h"
30 #include "security_manager.h"
31 #include "types/address.h"
32 
33 namespace test_vendor_lib {
34 
35 // Emulates a dual mode BR/EDR + LE controller by maintaining the link layer
36 // state machine detailed in the Bluetooth Core Specification Version 4.2,
37 // Volume 6, Part B, Section 1.1 (page 30). Provides methods corresponding to
38 // commands sent by the HCI. These methods will be registered as callbacks from
39 // a controller instance with the HciHandler. To implement a new Bluetooth
40 // command, simply add the method declaration below, with return type void and a
41 // single const std::vector<uint8_t>& argument. After implementing the
42 // method, simply register it with the HciHandler using the SET_HANDLER macro in
43 // the controller's default constructor. Be sure to name your method after the
44 // corresponding Bluetooth command in the Core Specification with the prefix
45 // "Hci" to distinguish it as a controller command.
46 class DualModeController : public Device {
47   // The location of the config file loaded to populate controller attributes.
48   static constexpr char kControllerPropertiesFile[] = "/etc/bluetooth/controller_properties.json";
49   static constexpr uint16_t kSecurityManagerNumKeys = 15;
50 
51  public:
52   // Sets all of the methods to be used as callbacks in the HciHandler.
53   DualModeController(const std::string& properties_filename = std::string(kControllerPropertiesFile),
54                      uint16_t num_keys = kSecurityManagerNumKeys);
55 
56   ~DualModeController() = default;
57 
58   // Device methods.
59   virtual void Initialize(const std::vector<std::string>& args) override;
60 
61   virtual std::string GetTypeString() const override;
62 
63   virtual void IncomingPacket(packets::LinkLayerPacketView incoming) override;
64 
65   virtual void TimerTick() override;
66 
67   // Send packets to remote devices
68   void SendLinkLayerPacket(std::shared_ptr<packets::LinkLayerPacketBuilder> to_send, Phy::Type phy_type);
69 
70   // Route commands and data from the stack.
71   void HandleAcl(std::shared_ptr<std::vector<uint8_t>> acl_packet);
72   void HandleCommand(std::shared_ptr<std::vector<uint8_t>> command_packet);
73   void HandleSco(std::shared_ptr<std::vector<uint8_t>> sco_packet);
74 
75   // Set the callbacks for scheduling tasks.
76   void RegisterTaskScheduler(std::function<AsyncTaskId(std::chrono::milliseconds, const TaskCallback&)> evtScheduler);
77 
78   void RegisterPeriodicTaskScheduler(
79       std::function<AsyncTaskId(std::chrono::milliseconds, std::chrono::milliseconds, const TaskCallback&)>
80           periodicEvtScheduler);
81 
82   void RegisterTaskCancel(std::function<void(AsyncTaskId)> cancel);
83 
84   // Set the callbacks for sending packets to the HCI.
85   void RegisterEventChannel(const std::function<void(std::shared_ptr<std::vector<uint8_t>>)>& send_event);
86 
87   void RegisterAclChannel(const std::function<void(std::shared_ptr<std::vector<uint8_t>>)>& send_acl);
88 
89   void RegisterScoChannel(const std::function<void(std::shared_ptr<std::vector<uint8_t>>)>& send_sco);
90 
91   // Controller commands. For error codes, see the Bluetooth Core Specification,
92   // Version 4.2, Volume 2, Part D (page 370).
93 
94   // Link Control Commands
95   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.1
96 
97   // 7.1.1
98   void HciInquiry(packets::PacketView<true> args);
99 
100   // 7.1.2
101   void HciInquiryCancel(packets::PacketView<true> args);
102 
103   // 7.1.5
104   void HciCreateConnection(packets::PacketView<true> args);
105 
106   // 7.1.6
107   void HciDisconnect(packets::PacketView<true> args);
108 
109   // 7.1.8
110   void HciAcceptConnectionRequest(packets::PacketView<true> args);
111 
112   // 7.1.9
113   void HciRejectConnectionRequest(packets::PacketView<true> args);
114 
115   // 7.1.10
116   void HciLinkKeyRequestReply(packets::PacketView<true> args);
117 
118   // 7.1.11
119   void HciLinkKeyRequestNegativeReply(packets::PacketView<true> args);
120 
121   // 7.1.14
122   void HciChangeConnectionPacketType(packets::PacketView<true> args);
123 
124   // 7.1.15
125   void HciAuthenticationRequested(packets::PacketView<true> args);
126 
127   // 7.1.16
128   void HciSetConnectionEncryption(packets::PacketView<true> args);
129 
130   // 7.1.19
131   void HciRemoteNameRequest(packets::PacketView<true> args);
132 
133   // 7.1.21
134   void HciReadRemoteSupportedFeatures(packets::PacketView<true> args);
135 
136   // 7.1.22
137   void HciReadRemoteExtendedFeatures(packets::PacketView<true> args);
138 
139   // 7.1.23
140   void HciReadRemoteVersionInformation(packets::PacketView<true> args);
141 
142   // 7.1.24
143   void HciReadClockOffset(packets::PacketView<true> args);
144 
145   // 7.1.29
146   void HciIoCapabilityRequestReply(packets::PacketView<true> args);
147 
148   // 7.1.30
149   void HciUserConfirmationRequestReply(packets::PacketView<true> args);
150 
151   // 7.1.31
152   void HciUserConfirmationRequestNegativeReply(packets::PacketView<true> args);
153 
154   // 7.1.32
155   void HciUserPasskeyRequestReply(packets::PacketView<true> args);
156 
157   // 7.1.33
158   void HciUserPasskeyRequestNegativeReply(packets::PacketView<true> args);
159 
160   // 7.1.34
161   void HciRemoteOobDataRequestReply(packets::PacketView<true> args);
162 
163   // 7.1.35
164   void HciRemoteOobDataRequestNegativeReply(packets::PacketView<true> args);
165 
166   // 7.1.36
167   void HciIoCapabilityRequestNegativeReply(packets::PacketView<true> args);
168 
169   // Link Policy Commands
170   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.2
171 
172   // 7.2.10
173   void HciWriteLinkPolicySettings(packets::PacketView<true> args);
174 
175   // 7.2.12
176   void HciWriteDefaultLinkPolicySettings(packets::PacketView<true> args);
177 
178   // 7.2.14
179   void HciSniffSubrating(packets::PacketView<true> args);
180 
181   // Link Controller Commands
182   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.3
183 
184   // 7.3.1
185   void HciSetEventMask(packets::PacketView<true> args);
186 
187   // 7.3.2
188   void HciReset(packets::PacketView<true> args);
189 
190   // 7.3.3
191   void HciSetEventFilter(packets::PacketView<true> args);
192 
193   // 7.3.10
194   void HciDeleteStoredLinkKey(packets::PacketView<true> args);
195 
196   // 7.3.11
197   void HciWriteLocalName(packets::PacketView<true> args);
198 
199   // 7.3.12
200   void HciReadLocalName(packets::PacketView<true> args);
201 
202   // 7.3.16
203   void HciWritePageTimeout(packets::PacketView<true> args);
204 
205   // 7.3.18
206   void HciWriteScanEnable(packets::PacketView<true> args);
207 
208   // 7.3.22
209   void HciWriteInquiryScanActivity(packets::PacketView<true> args);
210 
211   // 7.3.23
212   void HciReadAuthenticationEnable(packets::PacketView<true> args);
213 
214   // 7.3.24
215   void HciWriteAuthenticationEnable(packets::PacketView<true> args);
216 
217   // 7.3.26
218   void HciWriteClassOfDevice(packets::PacketView<true> args);
219 
220   // 7.3.28
221   void HciWriteVoiceSetting(packets::PacketView<true> args);
222 
223   // 7.3.39
224   void HciHostBufferSize(packets::PacketView<true> args);
225 
226   // 7.3.42
227   void HciWriteLinkSupervisionTimeout(packets::PacketView<true> args);
228 
229   // 7.3.45
230   void HciWriteCurrentIacLap(packets::PacketView<true> args);
231 
232   // 7.3.48
233   void HciWriteInquiryScanType(packets::PacketView<true> args);
234 
235   // 7.3.50
236   void HciWriteInquiryMode(packets::PacketView<true> args);
237 
238   // 7.3.52
239   void HciWritePageScanType(packets::PacketView<true> args);
240 
241   // 7.3.56
242   void HciWriteExtendedInquiryResponse(packets::PacketView<true> args);
243 
244   // 7.3.59
245   void HciWriteSimplePairingMode(packets::PacketView<true> args);
246 
247   // 7.3.79
248   void HciWriteLeHostSupport(packets::PacketView<true> args);
249 
250   // Informational Parameters Commands
251   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.4
252 
253   // 7.4.5
254   void HciReadBufferSize(packets::PacketView<true> args);
255 
256   // 7.4.1
257   void HciReadLocalVersionInformation(packets::PacketView<true> args);
258 
259   // 7.4.6
260   void HciReadBdAddr(packets::PacketView<true> args);
261 
262   // 7.4.2
263   void HciReadLocalSupportedCommands(packets::PacketView<true> args);
264 
265   // 7.4.4
266   void HciReadLocalExtendedFeatures(packets::PacketView<true> args);
267 
268   // 7.4.8
269   void HciReadLocalSupportedCodecs(packets::PacketView<true> args);
270 
271   // Status Parameters Commands
272   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.5
273 
274   // Test Commands
275   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.7
276 
277   // 7.7.1
278   void HciReadLoopbackMode(packets::PacketView<true> args);
279 
280   // 7.7.2
281   void HciWriteLoopbackMode(packets::PacketView<true> args);
282 
283   // LE Controller Commands
284   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.8
285 
286   // 7.8.1
287   void HciLeSetEventMask(packets::PacketView<true> args);
288 
289   // 7.8.2
290   void HciLeReadBufferSize(packets::PacketView<true> args);
291 
292   // 7.8.3
293   void HciLeReadLocalSupportedFeatures(packets::PacketView<true> args);
294 
295   // 7.8.4
296   void HciLeSetRandomAddress(packets::PacketView<true> args);
297 
298   // 7.8.5
299   void HciLeSetAdvertisingParameters(packets::PacketView<true> args);
300 
301   // 7.8.7
302   void HciLeSetAdvertisingData(packets::PacketView<true> args);
303 
304   // 7.8.10
305   void HciLeSetScanParameters(packets::PacketView<true> args);
306 
307   // 7.8.11
308   void HciLeSetScanEnable(packets::PacketView<true> args);
309 
310   // 7.8.12
311   void HciLeCreateConnection(packets::PacketView<true> args);
312 
313   // 7.8.18
314   void HciLeConnectionUpdate(packets::PacketView<true> args);
315 
316   // 7.8.13
317   void HciLeConnectionCancel(packets::PacketView<true> args);
318 
319   // 7.8.14
320   void HciLeReadWhiteListSize(packets::PacketView<true> args);
321 
322   // 7.8.15
323   void HciLeClearWhiteList(packets::PacketView<true> args);
324 
325   // 7.8.16
326   void HciLeAddDeviceToWhiteList(packets::PacketView<true> args);
327 
328   // 7.8.17
329   void HciLeRemoveDeviceFromWhiteList(packets::PacketView<true> args);
330 
331   // 7.8.21
332   void HciLeReadRemoteFeatures(packets::PacketView<true> args);
333 
334   // 7.8.23
335   void HciLeRand(packets::PacketView<true> args);
336 
337   // 7.8.24
338   void HciLeStartEncryption(packets::PacketView<true> args);
339 
340   // 7.8.27
341   void HciLeReadSupportedStates(packets::PacketView<true> args);
342 
343   // Vendor-specific Commands
344 
345   void HciLeVendorSleepMode(packets::PacketView<true> args);
346   void HciLeVendorCap(packets::PacketView<true> args);
347   void HciLeVendorMultiAdv(packets::PacketView<true> args);
348   void HciLeVendor155(packets::PacketView<true> args);
349   void HciLeVendor157(packets::PacketView<true> args);
350   void HciLeEnergyInfo(packets::PacketView<true> args);
351   void HciLeAdvertisingFilter(packets::PacketView<true> args);
352   void HciLeExtendedScanParams(packets::PacketView<true> args);
353 
354   void SetTimerPeriod(std::chrono::milliseconds new_period);
355   void StartTimer();
356   void StopTimer();
357 
358  protected:
359   LinkLayerController link_layer_controller_{properties_};
360 
361  private:
362   // Set a timer for a future action
363   void AddControllerEvent(std::chrono::milliseconds, const TaskCallback& callback);
364 
365   void AddConnectionAction(const TaskCallback& callback, uint16_t handle);
366 
367   // Creates a command complete event and sends it back to the HCI.
368   void SendCommandComplete(hci::OpCode command_opcode, const std::vector<uint8_t>& return_parameters) const;
369 
370   // Sends a command complete event with no return parameters.
371   void SendCommandCompleteSuccess(hci::OpCode command_opcode) const;
372 
373   void SendCommandCompleteUnknownOpCodeEvent(uint16_t command_opcode) const;
374 
375   // Sends a command complete event with no return parameters.
376   void SendCommandCompleteOnlyStatus(hci::OpCode command_opcode, hci::Status status) const;
377 
378   void SendCommandCompleteStatusAndAddress(hci::OpCode command_opcode, hci::Status status,
379                                            const Address& address) const;
380 
381   // Creates a command status event and sends it back to the HCI.
382   void SendCommandStatus(hci::Status status, hci::OpCode command_opcode) const;
383 
384   // Sends a command status event with default event parameters.
385   void SendCommandStatusSuccess(hci::OpCode command_opcode) const;
386 
387   // Callbacks to send packets back to the HCI.
388   std::function<void(std::shared_ptr<std::vector<uint8_t>>)> send_acl_;
389   std::function<void(std::shared_ptr<std::vector<uint8_t>>)> send_event_;
390   std::function<void(std::shared_ptr<std::vector<uint8_t>>)> send_sco_;
391 
392   // Maintains the commands to be registered and used in the HciHandler object.
393   // Keys are command opcodes and values are the callbacks to handle each
394   // command.
395   std::unordered_map<uint16_t, std::function<void(packets::PacketView<true>)>> active_hci_commands_;
396 
397   hci::LoopbackMode loopback_mode_;
398 
399   SecurityManager security_manager_;
400 
401   DualModeController(const DualModeController& cmdPckt) = delete;
402   DualModeController& operator=(const DualModeController& cmdPckt) = delete;
403 };
404 
405 }  // namespace test_vendor_lib
406