• 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 <cstdint>
20 #include <memory>
21 #include <string>
22 #include <unordered_map>
23 #include <vector>
24 
25 #include "acl_packet.h"
26 #include "async_manager.h"
27 #include "base/time/time.h"
28 #include "bt_address.h"
29 #include "command_packet.h"
30 #include "connection.h"
31 #include "device.h"
32 #include "device_properties.h"
33 #include "event_packet.h"
34 #include "sco_packet.h"
35 #include "test_channel_transport.h"
36 
37 namespace test_vendor_lib {
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 {
51  public:
52   // Sets all of the methods to be used as callbacks in the HciHandler.
53   DualModeController();
54 
55   ~DualModeController() = default;
56 
57   // Route commands and data from the stack.
58   void HandleAcl(std::unique_ptr<AclPacket> acl_packet);
59   void HandleCommand(std::unique_ptr<CommandPacket> command_packet);
60   void HandleSco(std::unique_ptr<ScoPacket> sco_packet);
61 
62   // Dispatches the test channel action corresponding to the command specified
63   // by |name|.
64   void HandleTestChannelCommand(const std::string& name,
65                                 const std::vector<std::string>& args);
66 
67   // Set the callbacks for scheduling tasks.
68   void RegisterTaskScheduler(
69       std::function<AsyncTaskId(std::chrono::milliseconds, const TaskCallback&)>
70           evtScheduler);
71 
72   void RegisterPeriodicTaskScheduler(
73       std::function<AsyncTaskId(std::chrono::milliseconds,
74                                 std::chrono::milliseconds, const TaskCallback&)>
75           periodicEvtScheduler);
76 
77   void RegisterTaskCancel(std::function<void(AsyncTaskId)> cancel);
78 
79   // Set the callbacks for sending packets to the HCI.
80   void RegisterEventChannel(
81       const std::function<void(std::unique_ptr<EventPacket>)>& send_event);
82 
83   void RegisterAclChannel(
84       const std::function<void(std::unique_ptr<AclPacket>)>& send_acl);
85 
86   void RegisterScoChannel(
87       const std::function<void(std::unique_ptr<ScoPacket>)>& send_sco);
88 
89   // Controller commands. For error codes, see the Bluetooth Core Specification,
90   // Version 4.2, Volume 2, Part D (page 370).
91 
92   // OGF: 0x0003
93   // OCF: 0x0003
94   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.3.2
95   void HciReset(const std::vector<uint8_t>& args);
96 
97   // OGF: 0x0004
98   // OGF: 0x0005
99   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.4.5
100   void HciReadBufferSize(const std::vector<uint8_t>& args);
101 
102   // OGF: 0x0003
103   // OCF: 0x0033
104   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.3.39
105   void HciHostBufferSize(const std::vector<uint8_t>& args);
106 
107   // OGF: 0x0004
108   // OCF: 0x0001
109   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.4.1
110   void HciReadLocalVersionInformation(const std::vector<uint8_t>& args);
111 
112   // OGF: 0x0004
113   // OCF: 0x0009
114   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.4.6
115   void HciReadBdAddr(const std::vector<uint8_t>& args);
116 
117   // OGF: 0x0004
118   // OCF: 0x0002
119   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.4.2
120   void HciReadLocalSupportedCommands(const std::vector<uint8_t>& args);
121 
122   // OGF: 0x0004
123   // OCF: 0x0004
124   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.4.4
125   void HciReadLocalExtendedFeatures(const std::vector<uint8_t>& args);
126 
127   // OGF: 0x0004
128   // OCF: 0x000B
129   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.4.8
130   void HciReadLocalSupportedCodecs(const std::vector<uint8_t>& args);
131 
132   // OGF: 0x0003
133   // OCF: 0x0056
134   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.3.59
135   void HciWriteSimplePairingMode(const std::vector<uint8_t>& args);
136 
137   // OGF: 0x0003
138   // OCF: 0x006D
139   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.3.79
140   void HciWriteLeHostSupport(const std::vector<uint8_t>& args);
141 
142   // OGF: 0x0003
143   // OCF: 0x0001
144   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.3.1
145   void HciSetEventMask(const std::vector<uint8_t>& args);
146 
147   // OGF: 0x0003
148   // OCF: 0x0045
149   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.3.50
150   void HciWriteInquiryMode(const std::vector<uint8_t>& args);
151 
152   // OGF: 0x0003
153   // OCF: 0x0047
154   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.3.52
155   void HciWritePageScanType(const std::vector<uint8_t>& args);
156 
157   // OGF: 0x0003
158   // OCF: 0x0043
159   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.3.48
160   void HciWriteInquiryScanType(const std::vector<uint8_t>& args);
161 
162   // OGF: 0x0003
163   // OCF: 0x0024
164   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.3.26
165   void HciWriteClassOfDevice(const std::vector<uint8_t>& args);
166 
167   // OGF: 0x0003
168   // OCF: 0x0018
169   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.3.16
170   void HciWritePageTimeout(const std::vector<uint8_t>& args);
171 
172   // OGF: 0x0002
173   // OCF: 0x000F
174   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.2.12
175   void HciWriteDefaultLinkPolicySettings(const std::vector<uint8_t>& args);
176 
177   // OGF: 0x0003
178   // OCF: 0x0014
179   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.3.12
180   void HciReadLocalName(const std::vector<uint8_t>& args);
181 
182   // OGF: 0x0003
183   // OCF: 0x0013
184   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.3.11
185   void HciWriteLocalName(const std::vector<uint8_t>& args);
186 
187   // OGF: 0x0003
188   // OCF: 0x0052
189   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.3.56
190   void HciWriteExtendedInquiryResponse(const std::vector<uint8_t>& args);
191 
192   // OGF: 0x0003
193   // OCF: 0x0026
194   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.3.28
195   void HciWriteVoiceSetting(const std::vector<uint8_t>& args);
196 
197   // OGF: 0x0003
198   // OCF: 0x003A
199   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.3.45
200   void HciWriteCurrentIacLap(const std::vector<uint8_t>& args);
201 
202   // OGF: 0x0003
203   // OCF: 0x001E
204   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.3.22
205   void HciWriteInquiryScanActivity(const std::vector<uint8_t>& args);
206 
207   // OGF: 0x0003
208   // OCF: 0x001A
209   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.3.18
210   void HciWriteScanEnable(const std::vector<uint8_t>& args);
211 
212   // OGF: 0x0003
213   // OCF: 0x0005
214   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.3.3
215   void HciSetEventFilter(const std::vector<uint8_t>& args);
216 
217   // OGF: 0x0001
218   // OCF: 0x0001
219   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.1.1
220   void HciInquiry(const std::vector<uint8_t>& args);
221 
222   void InquiryTimeout();
223 
224   // OGF: 0x0001
225   // OCF: 0x0002
226   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.1.2
227   void HciInquiryCancel(const std::vector<uint8_t>& args);
228 
229   // OGF: 0x0003
230   // OCF: 0x0012
231   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.3.10
232   void HciDeleteStoredLinkKey(const std::vector<uint8_t>& args);
233 
234   // OGF: 0x0001
235   // OCF: 0x0019
236   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.1.19
237   void HciRemoteNameRequest(const std::vector<uint8_t>& args);
238 
239   // Test Commands
240 
241   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.7.1
242   void HciReadLoopbackMode(const std::vector<uint8_t>& args);
243 
244   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.7.2
245   void HciWriteLoopbackMode(const std::vector<uint8_t>& args);
246 
247   // LE Controller Commands
248 
249   // OGF: 0x0008
250   // OCF: 0x0001
251   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.8.1
252   void HciLeSetEventMask(const std::vector<uint8_t>& args);
253 
254   // OGF: 0x0008
255   // OCF: 0x0002
256   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.8.2
257   void HciLeReadBufferSize(const std::vector<uint8_t>& args);
258 
259   // OGF: 0x0008
260   // OCF: 0x0003
261   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.8.3
262   void HciLeReadLocalSupportedFeatures(const std::vector<uint8_t>& args);
263 
264   // OGF: 0x0008
265   // OCF: 0x0005
266   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.8.4
267   void HciLeSetRandomAddress(const std::vector<uint8_t>& args);
268 
269   // OGF: 0x0008
270   // OCF: 0x0006
271   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.8.5
272   void HciLeSetAdvertisingParameters(const std::vector<uint8_t>& args);
273 
274   // OGF: 0x0008
275   // OCF: 0x0008
276   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.8.7
277   void HciLeSetAdvertisingData(const std::vector<uint8_t>& args);
278 
279   // OGF: 0x0008
280   // OCF: 0x000B
281   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.8.10
282   void HciLeSetScanParameters(const std::vector<uint8_t>& args);
283 
284   // OGF: 0x0008
285   // OCF: 0x000C
286   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.8.11
287   void HciLeSetScanEnable(const std::vector<uint8_t>& args);
288 
289   // OGF: 0x0008
290   // OCF: 0x000D
291   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.8.12
292   void HciLeCreateConnection(const std::vector<uint8_t>& args);
293 
294   // OGF: 0x0008
295   // OCF: 0x000E
296   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.8.13
297   void HciLeConnectionCancel(const std::vector<uint8_t>& args);
298 
299   // OGF: 0x0008
300   // OCF: 0x000F
301   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.8.14
302   void HciLeReadWhiteListSize(const std::vector<uint8_t>& args);
303 
304   // OGF: 0x0008
305   // OCF: 0x0016
306   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.8.21
307   void HciLeReadRemoteUsedFeatures(const std::vector<uint8_t>& args);
308   void HciLeReadRemoteUsedFeaturesB(uint16_t handle);
309 
310   // OGF: 0x0008
311   // OCF: 0x0018
312   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.8.23
313   void HciLeRand(const std::vector<uint8_t>& args);
314 
315   // OGF: 0x0008
316   // OCF: 0x001C
317   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.8.27
318   void HciLeReadSupportedStates(const std::vector<uint8_t>& args);
319 
320   // Vendor-specific commands (see hcidefs.h)
321 
322   // OGF: 0x00FC
323   // OCF: 0x0027
324   void HciBleVendorSleepMode(const std::vector<uint8_t>& args);
325 
326   // OGF: 0x00FC
327   // OCF: 0x0153
328   void HciBleVendorCap(const std::vector<uint8_t>& args);
329 
330   // OGF: 0x00FC
331   // OCF: 0x0154
332   void HciBleVendorMultiAdv(const std::vector<uint8_t>& args);
333 
334   // OGF: 0x00FC
335   // OCF: 0x0155
336   void HciBleVendor155(const std::vector<uint8_t>& args);
337 
338   // OGF: 0x00FC
339   // OCF: 0x0157
340   void HciBleVendor157(const std::vector<uint8_t>& args);
341 
342   // OGF: 0x00FC
343   // OCF: 0x0159
344   void HciBleEnergyInfo(const std::vector<uint8_t>& args);
345 
346   // Test
347   void HciBleAdvertisingFilter(const std::vector<uint8_t>& args);
348 
349   // OGF: 0x00FC
350   // OCF: 0x015A
351   void HciBleExtendedScanParams(const std::vector<uint8_t>& args);
352 
353   // Test Channel commands:
354 
355   // Add devices
356   void TestChannelAdd(const std::vector<std::string>& args);
357 
358   // Remove devices by index
359   void TestChannelDel(const std::vector<std::string>& args);
360 
361   // List the devices that the controller knows about
362   void TestChannelList(const std::vector<std::string>& args) const;
363 
364   void Connections();
365 
366   void LeScan();
367 
368   void PageScan();
369 
370   void HandleTimerTick();
371   void SetTimerPeriod(std::chrono::milliseconds new_period);
372   void StartTimer();
373   void StopTimer();
374 
375  private:
376   // Current link layer state of the controller.
377   enum State {
378     kStandby,  // Not receiving/transmitting any packets from/to other devices.
379     kInquiry,  // The controller is discovering other nearby devices.
380   };
381 
382   // Set a timer for a future action
383   void AddControllerEvent(std::chrono::milliseconds,
384                           const TaskCallback& callback);
385 
386   void AddConnectionAction(const TaskCallback& callback, uint16_t handle);
387 
388   // Creates a command complete event and sends it back to the HCI.
389   void SendCommandComplete(uint16_t command_opcode,
390                            const std::vector<uint8_t>& return_parameters) const;
391 
392   // Sends a command complete event with no return parameters. This event is
393   // typically sent for commands that can be completed immediately.
394   void SendCommandCompleteSuccess(uint16_t command_opcode) const;
395 
396   // Sends a command complete event with no return parameters. This event is
397   // typically sent for commands that can be completed immediately.
398   void SendCommandCompleteOnlyStatus(uint16_t command_opcode,
399                                      uint8_t status) const;
400 
401   // Creates a command status event and sends it back to the HCI.
402   void SendCommandStatus(uint8_t status, uint16_t command_opcode) const;
403 
404   // Sends a command status event with default event parameters.
405   void SendCommandStatusSuccess(uint16_t command_opcode) const;
406 
407   void SetEventDelay(int64_t delay);
408 
409   // Callbacks to schedule tasks.
410   std::function<AsyncTaskId(std::chrono::milliseconds, const TaskCallback&)>
411       schedule_task_;
412   std::function<AsyncTaskId(std::chrono::milliseconds,
413                             std::chrono::milliseconds, const TaskCallback&)>
414       schedule_periodic_task_;
415 
416   std::function<void(AsyncTaskId)> cancel_task_;
417 
418   // Callbacks to send packets back to the HCI.
419   std::function<void(std::unique_ptr<AclPacket>)> send_acl_;
420   std::function<void(std::unique_ptr<EventPacket>)> send_event_;
421   std::function<void(std::unique_ptr<ScoPacket>)> send_sco_;
422 
423   // Maintains the commands to be registered and used in the HciHandler object.
424   // Keys are command opcodes and values are the callbacks to handle each
425   // command.
426   std::unordered_map<uint16_t, std::function<void(const std::vector<uint8_t>&)>>
427       active_hci_commands_;
428 
429   std::unordered_map<std::string,
430                      std::function<void(const std::vector<std::string>&)>>
431       active_test_channel_commands_;
432 
433   // Specifies the format of Inquiry Result events to be returned during the
434   // Inquiry command.
435   // 0x00: Standard Inquiry Result event format (default).
436   // 0x01: Inquiry Result format with RSSI.
437   // 0x02 Inquiry Result with RSSI format or Extended Inquiry Result format.
438   // 0x03-0xFF: Reserved.
439   uint8_t inquiry_mode_;
440 
441   bool inquiry_responses_limited_;
442   uint8_t inquiry_num_responses_;
443   uint8_t inquiry_lap_[3];
444 
445   std::vector<uint8_t> le_event_mask_;
446 
447   BtAddress le_random_address_;
448 
449   uint8_t le_scan_type_;
450   uint16_t le_scan_interval_;
451   uint16_t le_scan_window_;
452   uint8_t own_address_type_;
453   uint8_t scanning_filter_policy_;
454 
455   uint8_t le_scan_enable_;
456   uint8_t filter_duplicates_;
457 
458   bool le_connect_;
459   uint8_t initiator_filter_policy_;
460 
461   BtAddress peer_address_;
462   uint8_t peer_address_type_;
463 
464   uint8_t loopback_mode_;
465 
466   State state_;
467 
468   DeviceProperties properties_;
469 
470   std::vector<std::shared_ptr<Device>> devices_;
471 
472   std::vector<AsyncTaskId> controller_events_;
473 
474   std::vector<std::shared_ptr<Connection>> connections_;
475 
476   AsyncTaskId timer_tick_task_;
477   std::chrono::milliseconds timer_period_ = std::chrono::milliseconds(100);
478 
479   DualModeController(const DualModeController& cmdPckt) = delete;
480   DualModeController& operator=(const DualModeController& cmdPckt) = delete;
481 };
482 
483 }  // namespace test_vendor_lib
484