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 "hci/address.h"
28 #include "hci/hci_packets.h"
29 #include "link_layer_controller.h"
30 #include "model/devices/device.h"
31 #include "model/setup/async_manager.h"
32 #include "security_manager.h"
33 
34 namespace test_vendor_lib {
35 
36 using ::bluetooth::hci::Address;
37 using ::bluetooth::hci::CommandPacketView;
38 
39 // Emulates a dual mode BR/EDR + LE controller by maintaining the link layer
40 // state machine detailed in the Bluetooth Core Specification Version 4.2,
41 // Volume 6, Part B, Section 1.1 (page 30). Provides methods corresponding to
42 // commands sent by the HCI. These methods will be registered as callbacks from
43 // a controller instance with the HciHandler. To implement a new Bluetooth
44 // command, simply add the method declaration below, with return type void and a
45 // single const std::vector<uint8_t>& argument. After implementing the
46 // method, simply register it with the HciHandler using the SET_HANDLER macro in
47 // the controller's default constructor. Be sure to name your method after the
48 // corresponding Bluetooth command in the Core Specification with the prefix
49 // "Hci" to distinguish it as a controller command.
50 class DualModeController : public Device {
51   // The location of the config file loaded to populate controller attributes.
52   static constexpr char kControllerPropertiesFile[] = "/etc/bluetooth/controller_properties.json";
53   static constexpr uint16_t kSecurityManagerNumKeys = 15;
54 
55  public:
56   // Sets all of the methods to be used as callbacks in the HciHandler.
57   DualModeController(const std::string& properties_filename = std::string(kControllerPropertiesFile),
58                      uint16_t num_keys = kSecurityManagerNumKeys);
59 
60   ~DualModeController() = default;
61 
62   // Device methods.
63   virtual void Initialize(const std::vector<std::string>& args) override;
64 
65   virtual std::string GetTypeString() const override;
66 
67   virtual void IncomingPacket(
68       model::packets::LinkLayerPacketView incoming) override;
69 
70   virtual void TimerTick() override;
71 
72   // Route commands and data from the stack.
73   void HandleAcl(std::shared_ptr<std::vector<uint8_t>> acl_packet);
74   void HandleCommand(std::shared_ptr<std::vector<uint8_t>> command_packet);
75   void HandleSco(std::shared_ptr<std::vector<uint8_t>> sco_packet);
76   void HandleIso(std::shared_ptr<std::vector<uint8_t>> iso_packet);
77 
78   // Set the callbacks for scheduling tasks.
79   void RegisterTaskScheduler(std::function<AsyncTaskId(std::chrono::milliseconds, const TaskCallback&)> evtScheduler);
80 
81   void RegisterPeriodicTaskScheduler(
82       std::function<AsyncTaskId(std::chrono::milliseconds, std::chrono::milliseconds, const TaskCallback&)>
83           periodicEvtScheduler);
84 
85   void RegisterTaskCancel(std::function<void(AsyncTaskId)> cancel);
86 
87   // Set the callbacks for sending packets to the HCI.
88   void RegisterEventChannel(
89       const std::function<void(std::shared_ptr<std::vector<uint8_t>>)>&
90           send_event);
91 
92   void RegisterAclChannel(const std::function<void(std::shared_ptr<std::vector<uint8_t>>)>& send_acl);
93 
94   void RegisterScoChannel(const std::function<void(std::shared_ptr<std::vector<uint8_t>>)>& send_sco);
95 
96   void RegisterIsoChannel(
97       const std::function<void(std::shared_ptr<std::vector<uint8_t>>)>&
98           send_iso);
99 
100   // Set the device's address.
101   void SetAddress(Address address) override;
102 
103   // Controller commands. For error codes, see the Bluetooth Core Specification,
104   // Version 4.2, Volume 2, Part D (page 370).
105 
106   // Link Control Commands
107   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.1
108 
109   // 7.1.1
110   void Inquiry(CommandPacketView args);
111 
112   // 7.1.2
113   void InquiryCancel(CommandPacketView args);
114 
115   // 7.1.5
116   void CreateConnection(CommandPacketView args);
117 
118   // 7.1.6
119   void Disconnect(CommandPacketView args);
120 
121   // 7.1.8
122   void AcceptConnectionRequest(CommandPacketView args);
123 
124   // 7.1.9
125   void RejectConnectionRequest(CommandPacketView args);
126 
127   // 7.1.10
128   void LinkKeyRequestReply(CommandPacketView args);
129 
130   // 7.1.11
131   void LinkKeyRequestNegativeReply(CommandPacketView args);
132 
133   // 7.1.14
134   void ChangeConnectionPacketType(CommandPacketView args);
135 
136   // 7.1.15
137   void AuthenticationRequested(CommandPacketView args);
138 
139   // 7.1.16
140   void SetConnectionEncryption(CommandPacketView args);
141 
142   // 7.1.17
143   void ChangeConnectionLinkKey(CommandPacketView args);
144 
145   // 7.1.18
146   void MasterLinkKey(CommandPacketView args);
147 
148   // 7.1.19
149   void RemoteNameRequest(CommandPacketView args);
150 
151   // 7.2.8
152   void SwitchRole(CommandPacketView args);
153 
154   // 7.1.21
155   void ReadRemoteSupportedFeatures(CommandPacketView args);
156 
157   // 7.1.22
158   void ReadRemoteExtendedFeatures(CommandPacketView args);
159 
160   // 7.1.23
161   void ReadRemoteVersionInformation(CommandPacketView args);
162 
163   // 7.1.24
164   void ReadClockOffset(CommandPacketView args);
165 
166   // 7.1.29
167   void IoCapabilityRequestReply(CommandPacketView args);
168 
169   // 7.1.30
170   void UserConfirmationRequestReply(CommandPacketView args);
171 
172   // 7.1.31
173   void UserConfirmationRequestNegativeReply(CommandPacketView args);
174 
175   // 7.1.32
176   void UserPasskeyRequestReply(CommandPacketView args);
177 
178   // 7.1.33
179   void UserPasskeyRequestNegativeReply(CommandPacketView args);
180 
181   // 7.1.34
182   void RemoteOobDataRequestReply(CommandPacketView args);
183 
184   // 7.1.35
185   void RemoteOobDataRequestNegativeReply(CommandPacketView args);
186 
187   // 7.1.36
188   void IoCapabilityRequestNegativeReply(CommandPacketView args);
189 
190   // Link Policy Commands
191   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.2
192 
193   // 7.2.1
194   void HoldMode(CommandPacketView args);
195 
196   // 7.2.2
197   void SniffMode(CommandPacketView args);
198 
199   // 7.2.3
200   void ExitSniffMode(CommandPacketView args);
201 
202   // 7.2.6
203   void QosSetup(CommandPacketView args);
204 
205   // 7.2.10
206   void WriteLinkPolicySettings(CommandPacketView args);
207 
208   // 7.2.11
209   void ReadDefaultLinkPolicySettings(CommandPacketView args);
210 
211   // 7.2.12
212   void WriteDefaultLinkPolicySettings(CommandPacketView args);
213 
214   // 7.2.13
215   void FlowSpecification(CommandPacketView args);
216 
217   // 7.2.14
218   void SniffSubrating(CommandPacketView args);
219 
220   // Link Controller Commands
221   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.3
222 
223   // 7.3.1
224   void SetEventMask(CommandPacketView args);
225 
226   // 7.3.2
227   void Reset(CommandPacketView args);
228 
229   // 7.3.3
230   void SetEventFilter(CommandPacketView args);
231 
232   // 7.3.10
233   void DeleteStoredLinkKey(CommandPacketView args);
234 
235   // 7.3.11
236   void WriteLocalName(CommandPacketView args);
237 
238   // 7.3.12
239   void ReadLocalName(CommandPacketView args);
240 
241   // 7.3.15
242   void ReadPageTimeout(CommandPacketView args);
243 
244   // 7.3.16
245   void WritePageTimeout(CommandPacketView args);
246 
247   // 7.3.17
248   void ReadScanEnable(CommandPacketView args);
249 
250   // 7.3.18
251   void WriteScanEnable(CommandPacketView args);
252 
253   // 7.3.19
254   void ReadPageScanActivity(CommandPacketView args);
255 
256   // 7.3.20
257   void WritePageScanActivity(CommandPacketView args);
258 
259   // 7.3.21
260   void ReadInquiryScanActivity(CommandPacketView args);
261 
262   // 7.3.22
263   void WriteInquiryScanActivity(CommandPacketView args);
264 
265   // 7.3.23
266   void ReadAuthenticationEnable(CommandPacketView args);
267 
268   // 7.3.24
269   void WriteAuthenticationEnable(CommandPacketView args);
270 
271   // 7.3.26
272   void WriteClassOfDevice(CommandPacketView args);
273 
274   // 7.3.28
275   void WriteVoiceSetting(CommandPacketView args);
276 
277   // 7.3.39
278   void HostBufferSize(CommandPacketView args);
279 
280   // 7.3.42
281   void WriteLinkSupervisionTimeout(CommandPacketView args);
282 
283   // 7.3.43
284   void ReadNumberOfSupportedIac(CommandPacketView args);
285 
286   // 7.3.44
287   void ReadCurrentIacLap(CommandPacketView args);
288 
289   // 7.3.45
290   void WriteCurrentIacLap(CommandPacketView args);
291 
292   // 7.3.47
293   void ReadInquiryScanType(CommandPacketView args);
294 
295   // 7.3.48
296   void WriteInquiryScanType(CommandPacketView args);
297 
298   // 7.3.49
299   void ReadInquiryMode(CommandPacketView args);
300 
301   // 7.3.50
302   void WriteInquiryMode(CommandPacketView args);
303 
304   // 7.3.52
305   void ReadPageScanType(CommandPacketView args);
306 
307   // 7.3.52
308   void WritePageScanType(CommandPacketView args);
309 
310   // 7.3.56
311   void WriteExtendedInquiryResponse(CommandPacketView args);
312 
313   // 7.3.57
314   void RefreshEncryptionKey(CommandPacketView args);
315 
316   // 7.3.59
317   void WriteSimplePairingMode(CommandPacketView args);
318 
319   // 7.3.61
320   void ReadInquiryResponseTransmitPowerLevel(CommandPacketView args);
321 
322   // 7.3.79
323   void WriteLeHostSupport(CommandPacketView args);
324 
325   // 7.3.92
326   void WriteSecureConnectionsHostSupport(CommandPacketView args);
327 
328   // Informational Parameters Commands
329   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.4
330 
331   // 7.4.5
332   void ReadBufferSize(CommandPacketView args);
333 
334   // 7.4.1
335   void ReadLocalVersionInformation(CommandPacketView args);
336 
337   // 7.4.6
338   void ReadBdAddr(CommandPacketView args);
339 
340   // 7.4.2
341   void ReadLocalSupportedCommands(CommandPacketView args);
342 
343   // 7.4.3
344   void ReadLocalSupportedFeatures(CommandPacketView args);
345 
346   // 7.4.4
347   void ReadLocalExtendedFeatures(CommandPacketView args);
348 
349   // 7.4.8
350   void ReadLocalSupportedCodecs(CommandPacketView args);
351 
352   // Status Parameters Commands
353   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.5
354 
355   // 7.5.7
356   void ReadEncryptionKeySize(CommandPacketView args);
357 
358   // Test Commands
359   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.7
360 
361   // 7.7.1
362   void ReadLoopbackMode(CommandPacketView args);
363 
364   // 7.7.2
365   void WriteLoopbackMode(CommandPacketView args);
366 
367   // LE Controller Commands
368   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.8
369 
370   // 7.8.1
371   void LeSetEventMask(CommandPacketView args);
372 
373   // 7.8.2
374   void LeReadBufferSize(CommandPacketView args);
375 
376   // 7.8.3
377   void LeReadLocalSupportedFeatures(CommandPacketView args);
378 
379   // 7.8.4
380   void LeSetRandomAddress(CommandPacketView args);
381 
382   // 7.8.5
383   void LeSetAdvertisingParameters(CommandPacketView args);
384 
385   // 7.8.7
386   void LeSetAdvertisingData(CommandPacketView args);
387 
388   // 7.8.8
389   void LeSetScanResponseData(CommandPacketView args);
390 
391   // 7.8.9
392   void LeSetAdvertisingEnable(CommandPacketView args);
393 
394   // 7.8.10
395   void LeSetScanParameters(CommandPacketView args);
396 
397   // 7.8.11
398   void LeSetScanEnable(CommandPacketView args);
399 
400   // 7.8.12
401   void LeCreateConnection(CommandPacketView args);
402 
403   // 7.8.18
404   void LeConnectionUpdate(CommandPacketView args);
405 
406   // 7.8.13
407   void LeConnectionCancel(CommandPacketView args);
408 
409   // 7.8.14
410   void LeReadConnectListSize(CommandPacketView args);
411 
412   // 7.8.15
413   void LeClearConnectList(CommandPacketView args);
414 
415   // 7.8.16
416   void LeAddDeviceToConnectList(CommandPacketView args);
417 
418   // 7.8.17
419   void LeRemoveDeviceFromConnectList(CommandPacketView args);
420 
421   // 7.8.21
422   void LeReadRemoteFeatures(CommandPacketView args);
423 
424   // 7.8.23
425   void LeRand(CommandPacketView args);
426 
427   // 7.8.24
428   void LeStartEncryption(CommandPacketView args);
429 
430   // 7.8.27
431   void LeReadSupportedStates(CommandPacketView args);
432 
433   // 7.8.38
434   void LeAddDeviceToResolvingList(CommandPacketView args);
435 
436   // 7.8.39
437   void LeRemoveDeviceFromResolvingList(CommandPacketView args);
438 
439   // 7.8.40
440   void LeClearResolvingList(CommandPacketView args);
441 
442   // 7.8.41
443   void LeReadResolvingListSize(CommandPacketView args);
444 
445   // 7.8.46
446   void LeReadMaximumDataLength(CommandPacketView args);
447 
448   // 7.8.52
449   void LeSetExtendedAdvertisingRandomAddress(CommandPacketView args);
450 
451   // 7.8.53
452   void LeSetExtendedAdvertisingParameters(CommandPacketView args);
453 
454   // 7.8.54
455   void LeSetExtendedAdvertisingData(CommandPacketView args);
456 
457   // 7.8.55
458   void LeSetExtendedAdvertisingScanResponse(CommandPacketView args);
459 
460   // 7.8.56
461   void LeSetExtendedAdvertisingEnable(CommandPacketView args);
462 
463   // 7.8.57
464   void LeReadMaximumAdvertisingDataLength(CommandPacketView args);
465 
466   // 7.8.58
467   void LeReadNumberOfSupportedAdvertisingSets(CommandPacketView args);
468 
469   // 7.8.59
470   void LeRemoveAdvertisingSet(CommandPacketView args);
471 
472   // 7.8.60
473   void LeClearAdvertisingSets(CommandPacketView args);
474 
475   // 7.8.64
476   void LeSetExtendedScanParameters(CommandPacketView args);
477 
478   // 7.8.65
479   void LeSetExtendedScanEnable(CommandPacketView args);
480 
481   // 7.8.66
482   void LeExtendedCreateConnection(CommandPacketView args);
483 
484   // 7.8.77
485   void LeSetPrivacyMode(CommandPacketView args);
486 
487   // Vendor-specific Commands
488 
489   void LeVendorSleepMode(CommandPacketView args);
490   void LeVendorCap(CommandPacketView args);
491   void LeVendorMultiAdv(CommandPacketView args);
492   void LeVendor155(CommandPacketView args);
493   void LeVendor157(CommandPacketView args);
494   void LeEnergyInfo(CommandPacketView args);
495   void LeAdvertisingFilter(CommandPacketView args);
496   void LeExtendedScanParams(CommandPacketView args);
497 
498   void SetTimerPeriod(std::chrono::milliseconds new_period);
499   void StartTimer();
500   void StopTimer();
501 
502  protected:
503   LinkLayerController link_layer_controller_{properties_};
504 
505  private:
506   // Set a timer for a future action
507   void AddControllerEvent(std::chrono::milliseconds, const TaskCallback& callback);
508 
509   void AddConnectionAction(const TaskCallback& callback, uint16_t handle);
510 
511   void SendCommandCompleteUnknownOpCodeEvent(uint16_t command_opcode) const;
512 
513   // Callbacks to send packets back to the HCI.
514   std::function<void(std::shared_ptr<bluetooth::hci::AclPacketBuilder>)>
515       send_acl_;
516   std::function<void(std::shared_ptr<bluetooth::hci::EventPacketBuilder>)>
517       send_event_;
518   std::function<void(std::shared_ptr<std::vector<uint8_t>>)> send_sco_;
519   std::function<void(std::shared_ptr<std::vector<uint8_t>>)> send_iso_;
520 
521   // Maintains the commands to be registered and used in the HciHandler object.
522   // Keys are command opcodes and values are the callbacks to handle each
523   // command.
524   std::unordered_map<bluetooth::hci::OpCode,
525                      std::function<void(bluetooth::hci::CommandPacketView)>>
526       active_hci_commands_;
527 
528   bluetooth::hci::LoopbackMode loopback_mode_;
529 
530   SecurityManager security_manager_;
531 
532   DualModeController(const DualModeController& cmdPckt) = delete;
533   DualModeController& operator=(const DualModeController& cmdPckt) = delete;
534 };
535 
536 }  // namespace test_vendor_lib
537