• 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 
21 #include <cstdint>
22 #include <memory>
23 #include <string>
24 #include <unordered_map>
25 #include <vector>
26 
27 #include "controller_properties.h"
28 #include "hci/address.h"
29 #include "hci/hci_packets.h"
30 #include "link_layer_controller.h"
31 #include "model/controller/vendor_commands/csr.h"
32 #include "model/devices/device.h"
33 
34 namespace rootcanal {
35 
36 using ::bluetooth::hci::Address;
37 using ::bluetooth::hci::CommandView;
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  public:
52   DualModeController(ControllerProperties properties = ControllerProperties());
53   DualModeController(DualModeController&&) = delete;
54   DualModeController(const DualModeController&) = delete;
55   ~DualModeController() = default;
56 
57   DualModeController& operator=(const DualModeController&) = delete;
58 
59   // Device methods.
60   std::string GetTypeString() const override;
61 
62   void ReceiveLinkLayerPacket(model::packets::LinkLayerPacketView incoming,
63                               Phy::Type type, int8_t rssi) override;
64 
65   void Tick() override;
66   void Close() override;
67 
68   // Route commands and data from the stack.
69   void HandleAcl(std::shared_ptr<std::vector<uint8_t>> acl_packet);
70   void HandleCommand(std::shared_ptr<std::vector<uint8_t>> command_packet);
71   void HandleSco(std::shared_ptr<std::vector<uint8_t>> sco_packet);
72   void HandleIso(std::shared_ptr<std::vector<uint8_t>> iso_packet);
73 
74   // Set the callbacks for sending packets to the HCI.
75   void RegisterEventChannel(
76       const std::function<void(std::shared_ptr<std::vector<uint8_t>>)>&
77           send_event);
78 
79   void RegisterAclChannel(
80       const std::function<void(std::shared_ptr<std::vector<uint8_t>>)>&
81           send_acl);
82 
83   void RegisterScoChannel(
84       const std::function<void(std::shared_ptr<std::vector<uint8_t>>)>&
85           send_sco);
86 
87   void RegisterIsoChannel(
88       const std::function<void(std::shared_ptr<std::vector<uint8_t>>)>&
89           send_iso);
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 Inquiry(CommandView command);
99 
100   // 7.1.2
101   void InquiryCancel(CommandView command);
102 
103   // 7.1.5
104   void CreateConnection(CommandView command);
105 
106   // 7.1.6
107   void Disconnect(CommandView command);
108 
109   // Deprecated
110   void AddScoConnection(CommandView command);
111 
112   // 7.1.7
113   void CreateConnectionCancel(CommandView command);
114 
115   // 7.1.8
116   void AcceptConnectionRequest(CommandView command);
117 
118   // 7.1.9
119   void RejectConnectionRequest(CommandView command);
120 
121   // 7.1.14
122   void ChangeConnectionPacketType(CommandView command);
123 
124   // 7.1.17
125   void ChangeConnectionLinkKey(CommandView command);
126 
127   // 7.1.18
128   void CentralLinkKey(CommandView command);
129 
130   // 7.1.19
131   void RemoteNameRequest(CommandView command);
132 
133   // 7.2.8
134   void SwitchRole(CommandView command);
135 
136   // 7.1.21
137   void ReadRemoteSupportedFeatures(CommandView command);
138 
139   // 7.1.22
140   void ReadRemoteExtendedFeatures(CommandView command);
141 
142   // 7.1.23
143   void ReadRemoteVersionInformation(CommandView command);
144 
145   // 7.1.24
146   void ReadClockOffset(CommandView command);
147 
148   // 7.1.26
149   void SetupSynchronousConnection(CommandView command);
150 
151   // 7.1.27
152   void AcceptSynchronousConnection(CommandView command);
153 
154   // 7.1.28
155   void RejectSynchronousConnection(CommandView command);
156 
157   // 7.1.45
158   void EnhancedSetupSynchronousConnection(CommandView command);
159 
160   // 7.1.46
161   void EnhancedAcceptSynchronousConnection(CommandView command);
162 
163   // Link Policy Commands
164   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.2
165 
166   // 7.2.1
167   void HoldMode(CommandView command);
168 
169   // 7.2.2
170   void SniffMode(CommandView command);
171 
172   // 7.2.3
173   void ExitSniffMode(CommandView command);
174 
175   // 7.2.6
176   void QosSetup(CommandView command);
177 
178   // 7.2.7
179   void RoleDiscovery(CommandView command);
180 
181   // 7.2.9
182   void ReadLinkPolicySettings(CommandView command);
183 
184   // 7.2.10
185   void WriteLinkPolicySettings(CommandView command);
186 
187   // 7.2.11
188   void ReadDefaultLinkPolicySettings(CommandView command);
189 
190   // 7.2.12
191   void WriteDefaultLinkPolicySettings(CommandView command);
192 
193   // 7.2.13
194   void FlowSpecification(CommandView command);
195 
196   // 7.2.14
197   void SniffSubrating(CommandView command);
198 
199   // Link Controller Commands
200   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.3
201 
202   // 7.3.1
203   void SetEventMask(CommandView command);
204 
205   // 7.3.2
206   void Reset(CommandView command);
207 
208   // 7.3.3
209   void SetEventFilter(CommandView command);
210 
211   // 7.3.10
212   void DeleteStoredLinkKey(CommandView command);
213 
214   // 7.3.11
215   void WriteLocalName(CommandView command);
216 
217   // 7.3.12
218   void ReadLocalName(CommandView command);
219 
220   // 7.3.15
221   void ReadPageTimeout(CommandView command);
222 
223   // 7.3.16
224   void WritePageTimeout(CommandView command);
225 
226   // 7.3.17
227   void ReadScanEnable(CommandView command);
228 
229   // 7.3.18
230   void WriteScanEnable(CommandView command);
231 
232   // 7.3.19
233   void ReadPageScanActivity(CommandView command);
234 
235   // 7.3.20
236   void WritePageScanActivity(CommandView command);
237 
238   // 7.3.21
239   void ReadInquiryScanActivity(CommandView command);
240 
241   // 7.3.22
242   void WriteInquiryScanActivity(CommandView command);
243 
244   // 7.3.23
245   void ReadAuthenticationEnable(CommandView command);
246 
247   // 7.3.24
248   void WriteAuthenticationEnable(CommandView command);
249 
250   // 7.3.26
251   void WriteClassOfDevice(CommandView command);
252 
253   // 7.3.28
254   void WriteVoiceSetting(CommandView command);
255 
256   // 7.3.36
257   void ReadSynchronousFlowControlEnable(CommandView command);
258 
259   // 7.3.37
260   void WriteSynchronousFlowControlEnable(CommandView command);
261 
262   // 7.3.39
263   void HostBufferSize(CommandView command);
264 
265   // 7.3.42
266   void WriteLinkSupervisionTimeout(CommandView command);
267 
268   // 7.3.43
269   void ReadNumberOfSupportedIac(CommandView command);
270 
271   // 7.3.44
272   void ReadCurrentIacLap(CommandView command);
273 
274   // 7.3.45
275   void WriteCurrentIacLap(CommandView command);
276 
277   // 7.3.47
278   void ReadInquiryScanType(CommandView command);
279 
280   // 7.3.48
281   void WriteInquiryScanType(CommandView command);
282 
283   // 7.3.49
284   void ReadInquiryMode(CommandView command);
285 
286   // 7.3.50
287   void WriteInquiryMode(CommandView command);
288 
289   // 7.3.52
290   void ReadPageScanType(CommandView command);
291 
292   // 7.3.52
293   void WritePageScanType(CommandView command);
294 
295   // 7.3.56
296   void WriteExtendedInquiryResponse(CommandView command);
297 
298   // 7.3.57
299   void RefreshEncryptionKey(CommandView command);
300 
301   // 7.3.59
302   void WriteSimplePairingMode(CommandView command);
303 
304   // 7.3.60
305   void ReadLocalOobData(CommandView command);
306 
307   // 7.3.61
308   void ReadInquiryResponseTransmitPowerLevel(CommandView command);
309 
310   // 7.3.66
311   void EnhancedFlush(CommandView command);
312 
313   // 7.3.69
314   void SetEventMaskPage2(CommandView command);
315 
316   // 7.3.79
317   void WriteLeHostSupport(CommandView command);
318 
319   // 7.3.92
320   void WriteSecureConnectionsHostSupport(CommandView command);
321 
322   // 7.3.95
323   void ReadLocalOobExtendedData(CommandView command);
324 
325   // Informational Parameters Commands
326   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.4
327 
328   // 7.4.5
329   void ReadBufferSize(CommandView command);
330 
331   // 7.4.1
332   void ReadLocalVersionInformation(CommandView command);
333 
334   // 7.4.6
335   void ReadBdAddr(CommandView command);
336 
337   // 7.4.2
338   void ReadLocalSupportedCommands(CommandView command);
339 
340   // 7.4.3
341   void ReadLocalSupportedFeatures(CommandView command);
342 
343   // 7.4.4
344   void ReadLocalExtendedFeatures(CommandView command);
345 
346   // 7.4.8
347   void ReadLocalSupportedCodecsV1(CommandView command);
348 
349   // Status Parameters Commands
350   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.5
351 
352   // 7.5.4
353   void ReadRssi(CommandView command);
354 
355   // 7.5.7
356   void ReadEncryptionKeySize(CommandView command);
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(CommandView command);
363 
364   // 7.7.2
365   void WriteLoopbackMode(CommandView command);
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(CommandView command);
372 
373   // 7.8.2 and 7.8.93
374   void LeReadBufferSizeV1(CommandView command);
375   void LeReadBufferSizeV2(CommandView command);
376 
377   // 7.8.3
378   void LeReadLocalSupportedFeatures(CommandView command);
379 
380   // 7.8.4
381   void LeSetRandomAddress(CommandView command);
382 
383   // 7.8.5
384   void LeSetAdvertisingParameters(CommandView command);
385 
386   // 7.8.6
387   void LeReadAdvertisingPhysicalChannelTxPower(CommandView command);
388 
389   // 7.8.7
390   void LeSetAdvertisingData(CommandView command);
391 
392   // 7.8.8
393   void LeSetScanResponseData(CommandView command);
394 
395   // 7.8.9
396   void LeSetAdvertisingEnable(CommandView command);
397 
398   // 7.8.10
399   void LeSetScanParameters(CommandView command);
400 
401   // 7.8.11
402   void LeSetScanEnable(CommandView command);
403 
404   // 7.8.12
405   void LeCreateConnection(CommandView command);
406 
407   // 7.8.13
408   void LeCreateConnectionCancel(CommandView command);
409 
410   // 7.8.14
411   void LeReadFilterAcceptListSize(CommandView command);
412 
413   // 7.8.15
414   void LeClearFilterAcceptList(CommandView command);
415 
416   // 7.8.16
417   void LeAddDeviceToFilterAcceptList(CommandView command);
418 
419   // 7.8.17
420   void LeRemoveDeviceFromFilterAcceptList(CommandView command);
421 
422   // 7.8.18
423   void LeConnectionUpdate(CommandView command);
424 
425   // 7.8.21
426   void LeReadRemoteFeatures(CommandView command);
427 
428   // 7.8.22
429   void LeEncrypt(CommandView command);
430 
431   // 7.8.23
432   void LeRand(CommandView command);
433 
434   // 7.8.24
435   void LeStartEncryption(CommandView command);
436 
437   // 7.8.25
438   void LeLongTermKeyRequestReply(CommandView command);
439 
440   // 7.8.26
441   void LeLongTermKeyRequestNegativeReply(CommandView command);
442 
443   // 7.8.27
444   void LeReadSupportedStates(CommandView command);
445 
446   // 7.8.31
447   void LeRemoteConnectionParameterRequestReply(CommandView command);
448 
449   // 7.8.32
450   void LeRemoteConnectionParameterRequestNegativeReply(CommandView command);
451 
452   // 7.8.34
453   void LeReadSuggestedDefaultDataLength(CommandView command);
454 
455   // 7.8.35
456   void LeWriteSuggestedDefaultDataLength(CommandView command);
457 
458   // 7.8.38 - 7.8.41
459   void LeAddDeviceToResolvingList(CommandView command);
460   void LeRemoveDeviceFromResolvingList(CommandView command);
461   void LeClearResolvingList(CommandView command);
462   void LeReadResolvingListSize(CommandView command);
463 
464   // 7.8.42 - 7.8.43
465   void LeReadPeerResolvableAddress(CommandView command);
466   void LeReadLocalResolvableAddress(CommandView command);
467 
468   // 7.8.44
469   void LeSetAddressResolutionEnable(CommandView command);
470 
471   // 7.8.45
472   void LeSetResolvablePrivateAddressTimeout(CommandView command);
473 
474   // 7.8.46
475   void LeReadMaximumDataLength(CommandView command);
476 
477   void LeReadPhy(CommandView command);
478   void LeSetDefaultPhy(CommandView command);
479   void LeSetPhy(CommandView command);
480 
481   // 7.8.52
482   void LeSetAdvertisingSetRandomAddress(CommandView command);
483 
484   // 7.8.53
485   void LeSetExtendedAdvertisingParameters(CommandView command);
486 
487   // 7.8.54
488   void LeSetExtendedAdvertisingData(CommandView command);
489 
490   // 7.8.55
491   void LeSetExtendedScanResponseData(CommandView command);
492 
493   // 7.8.56
494   void LeSetExtendedAdvertisingEnable(CommandView command);
495 
496   // 7.8.57
497   void LeReadMaximumAdvertisingDataLength(CommandView command);
498 
499   // 7.8.58
500   void LeReadNumberOfSupportedAdvertisingSets(CommandView command);
501 
502   // 7.8.59
503   void LeRemoveAdvertisingSet(CommandView command);
504 
505   // 7.8.60
506   void LeClearAdvertisingSets(CommandView command);
507 
508   // 7.8.61 - 7.8.63
509   void LeSetPeriodicAdvertisingParameters(CommandView command);
510   void LeSetPeriodicAdvertisingData(CommandView command);
511   void LeSetPeriodicAdvertisingEnable(CommandView command);
512 
513   // 7.8.67 - 7.8.69
514   void LePeriodicAdvertisingCreateSync(CommandView command);
515   void LePeriodicAdvertisingCreateSyncCancel(CommandView command);
516   void LePeriodicAdvertisingTerminateSync(CommandView command);
517 
518   // 7.8.70 - 7.8.73
519   void LeAddDeviceToPeriodicAdvertiserList(CommandView command);
520   void LeRemoveDeviceFromPeriodicAdvertiserList(CommandView command);
521   void LeClearPeriodicAdvertiserList(CommandView command);
522   void LeReadPeriodicAdvertiserListSize(CommandView command);
523 
524   // 7.8.64
525   void LeSetExtendedScanParameters(CommandView command);
526 
527   // 7.8.65
528   void LeSetExtendedScanEnable(CommandView command);
529 
530   // 7.8.66
531   void LeExtendedCreateConnection(CommandView command);
532 
533   // 7.8.77
534   void LeSetPrivacyMode(CommandView command);
535 
536   // 7.8.96 - 7.8.110
537   void LeReadIsoTxSync(CommandView command);
538   void LeSetCigParameters(CommandView command);
539   void LeCreateCis(CommandView command);
540   void LeRemoveCig(CommandView command);
541   void LeAcceptCisRequest(CommandView command);
542   void LeRejectCisRequest(CommandView command);
543   void LeCreateBig(CommandView command);
544   void LeTerminateBig(CommandView command);
545   void LeBigCreateSync(CommandView command);
546   void LeBigTerminateSync(CommandView command);
547   void LeRequestPeerSca(CommandView command);
548   void LeSetupIsoDataPath(CommandView command);
549   void LeRemoveIsoDataPath(CommandView command);
550 
551   // 7.8.115
552   void LeSetHostFeature(CommandView command);
553 
554   // Required commands for handshaking with hci driver
555   void ReadClassOfDevice(CommandView command);
556   void ReadVoiceSetting(CommandView command);
557   void ReadConnectionAcceptTimeout(CommandView command);
558   void WriteConnectionAcceptTimeout(CommandView command);
559 
560   // Vendor-specific Commands
561 
562   void LeGetVendorCapabilities(CommandView command);
563   void LeEnergyInfo(CommandView command);
564   void LeMultiAdv(CommandView command);
565   void LeAdvertisingFilter(CommandView command);
566   void LeExtendedScanParams(CommandView command);
567 
568   // CSR vendor command.
569   // Implement the command specific to the CSR controller
570   // used specifically by the PTS tool to pass certification tests.
571   void CsrVendorCommand(CommandView command);
572   void CsrReadVarid(CsrVarid varid, std::vector<uint8_t>& value);
573   void CsrWriteVarid(CsrVarid varid, std::vector<uint8_t> const& value);
574   void CsrReadPskey(CsrPskey pskey, std::vector<uint8_t>& value);
575   void CsrWritePskey(CsrPskey pskey, std::vector<uint8_t> const& value);
576 
577   // Command pass-through.
578   void ForwardToLm(CommandView command);
579 
580  protected:
581   // Controller configuration.
582   ControllerProperties properties_;
583 
584   // Link Layer state.
585   LinkLayerController link_layer_controller_{address_, properties_};
586 
587  private:
588   // Send a HCI_Command_Complete event for the specified op_code with
589   // the error code UNKNOWN_OPCODE.
590   void SendCommandCompleteUnknownOpCodeEvent(
591       bluetooth::hci::OpCode op_code) const;
592 
593   // Callbacks to send packets back to the HCI.
594   std::function<void(std::shared_ptr<bluetooth::hci::AclBuilder>)> send_acl_;
595   std::function<void(std::shared_ptr<bluetooth::hci::EventBuilder>)>
596       send_event_;
597   std::function<void(std::shared_ptr<bluetooth::hci::ScoBuilder>)> send_sco_;
598   std::function<void(std::shared_ptr<bluetooth::hci::IsoBuilder>)> send_iso_;
599 
600   // Loopback mode (Vol 4, Part E § 7.6.1).
601   // The local loopback mode is used to pass the android Vendor Test Suite
602   // with RootCanal.
603   bluetooth::hci::LoopbackMode loopback_mode_{LoopbackMode::NO_LOOPBACK};
604 
605   // Flag set to true after the HCI Reset command has been received
606   // the first time.
607   bool controller_reset_{false};
608 
609   // Map command opcodes to the corresponding bit index in the
610   // supported command mask.
611   static const std::unordered_map<OpCode, OpCodeIndex>
612       hci_command_op_code_to_index_;
613 
614   // Map all implemented opcodes to the function implementing the handler
615   // for the associated command. The map should be a subset of the
616   // supported_command field in the properties_ object. Commands
617   // that are supported but not implemented will raise a fatal assert.
618   using CommandHandler =
619       std::function<void(DualModeController*, bluetooth::hci::CommandView)>;
620   static const std::unordered_map<OpCode, CommandHandler> hci_command_handlers_;
621 };
622 
623 }  // namespace rootcanal
624