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 "async_manager.h"
26#include "base/json/json_value_converter.h"
27#include "base/time/time.h"
28#include "bt_address.h"
29#include "command_packet.h"
30#include "event_packet.h"
31#include "test_channel_transport.h"
32
33namespace 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.
46class DualModeController {
47 public:
48  class Properties {
49   public:
50    explicit Properties(const std::string& file_name);
51
52    // Access private configuration data
53
54    // Specification Version 4.2, Volume 2, Part E, Section 7.4.1
55    const std::vector<uint8_t>& GetLocalVersionInformation() const;
56
57    // Specification Version 4.2, Volume 2, Part E, Section 7.4.2
58    const std::vector<uint8_t>& GetLocalSupportedCommands() const {
59      return local_supported_commands_;
60    }
61
62    // Specification Version 4.2, Volume 2, Part E, Section 7.4.3
63    uint64_t GetLocalSupportedFeatures() const {
64      return local_extended_features_[0];
65    };
66
67    // Specification Version 4.2, Volume 2, Part E, Section 7.4.4
68    uint8_t GetLocalExtendedFeaturesMaximumPageNumber() const {
69      return local_extended_features_.size() - 1;
70    };
71
72    uint64_t GetLocalExtendedFeatures(uint8_t page_number) const {
73      CHECK(page_number < local_extended_features_.size());
74      return local_extended_features_[page_number];
75    };
76
77    // Specification Version 4.2, Volume 2, Part E, Section 7.4.5
78    uint16_t GetAclDataPacketSize() const { return acl_data_packet_size_; }
79
80    uint8_t GetSynchronousDataPacketSize() const {
81      return sco_data_packet_size_;
82    }
83
84    uint16_t GetTotalNumAclDataPackets() const { return num_acl_data_packets_; }
85
86    uint16_t GetTotalNumSynchronousDataPackets() const {
87      return num_sco_data_packets_;
88    }
89
90    const BtAddress& GetAddress() const { return address_; }
91
92    // Specification Version 4.2, Volume 2, Part E, Section 7.4.8
93    const std::vector<uint8_t>& GetSupportedCodecs() const {
94      return supported_codecs_;
95    }
96
97    const std::vector<uint32_t>& GetVendorSpecificCodecs() const {
98      return vendor_specific_codecs_;
99    }
100
101    const std::string& GetLocalName() const { return local_name_; }
102
103    uint8_t GetVersion() const { return version_; }
104
105    uint16_t GetRevision() const { return revision_; }
106
107    uint8_t GetLmpPalVersion() const { return lmp_pal_version_; }
108
109    uint16_t GetLmpPalSubversion() const { return lmp_pal_subversion_; }
110
111    uint16_t GetManufacturerName() const { return manufacturer_name_; }
112
113    // Specification Version 4.2, Volume 2, Part E, Section 7.8.2
114    uint16_t GetLeDataPacketLength() const { return le_data_packet_length_; }
115
116    uint8_t GetTotalNumLeDataPackets() const { return num_le_data_packets_; }
117
118    // Specification Version 4.2, Volume 2, Part E, Section 7.8.3
119    uint64_t GetLeLocalSupportedFeatures() const {
120      return le_supported_features_;
121    }
122
123    // Specification Version 4.2, Volume 2, Part E, Section 7.8.14
124    uint8_t GetLeWhiteListSize() const { return le_white_list_size_; }
125
126    // Specification Version 4.2, Volume 2, Part E, Section 7.8.27
127    uint64_t GetLeSupportedStates() const { return le_supported_states_; }
128
129    // Vendor-specific commands (see hcidefs.h)
130    const std::vector<uint8_t>& GetLeVendorCap() const {
131      return le_vendor_cap_;
132    }
133
134    static void RegisterJSONConverter(
135        base::JSONValueConverter<Properties>* converter);
136
137   private:
138    uint16_t acl_data_packet_size_;
139    uint8_t sco_data_packet_size_;
140    uint16_t num_acl_data_packets_;
141    uint16_t num_sco_data_packets_;
142    uint8_t version_;
143    uint16_t revision_;
144    uint8_t lmp_pal_version_;
145    uint16_t manufacturer_name_;
146    uint16_t lmp_pal_subversion_;
147    std::vector<uint8_t> supported_codecs_;
148    std::vector<uint32_t> vendor_specific_codecs_;
149    std::vector<uint8_t> local_supported_commands_;
150    std::string local_name_;
151    std::vector<uint64_t> local_extended_features_;
152    BtAddress address_;
153
154    uint16_t le_data_packet_length_;
155    uint8_t num_le_data_packets_;
156    uint8_t le_white_list_size_;
157    uint64_t le_supported_features_;
158    uint64_t le_supported_states_;
159    std::vector<uint8_t> le_vendor_cap_;
160  };
161
162  // Sets all of the methods to be used as callbacks in the HciHandler.
163  DualModeController();
164
165  ~DualModeController() = default;
166
167  // Preprocesses the command, primarily checking testh channel hooks. If
168  // possible, dispatches the corresponding controller method corresponding to
169  // carry out the command.
170  void HandleCommand(std::unique_ptr<CommandPacket> command_packet);
171
172  // Dispatches the test channel action corresponding to the command specified
173  // by |name|.
174  void HandleTestChannelCommand(const std::string& name,
175                                const std::vector<std::string>& args);
176
177  // Set the callbacks for scheduling tasks.
178  void RegisterTaskScheduler(
179      std::function<AsyncTaskId(std::chrono::milliseconds, const TaskCallback&)>
180          evtScheduler);
181
182  void RegisterPeriodicTaskScheduler(
183      std::function<AsyncTaskId(std::chrono::milliseconds,
184                                std::chrono::milliseconds, const TaskCallback&)>
185          periodicEvtScheduler);
186
187  void RegisterTaskCancel(std::function<void(AsyncTaskId)> cancel);
188
189  // Sets the callback to be used for sending events back to the HCI.
190  void RegisterEventChannel(
191      const std::function<void(std::unique_ptr<EventPacket>)>& send_event);
192
193  // Controller commands. For error codes, see the Bluetooth Core Specification,
194  // Version 4.2, Volume 2, Part D (page 370).
195
196  // OGF: 0x0003
197  // OCF: 0x0003
198  // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.3.2
199  void HciReset(const std::vector<uint8_t>& args);
200
201  // OGF: 0x0004
202  // OGF: 0x0005
203  // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.4.5
204  void HciReadBufferSize(const std::vector<uint8_t>& args);
205
206  // OGF: 0x0003
207  // OCF: 0x0033
208  // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.3.39
209  void HciHostBufferSize(const std::vector<uint8_t>& args);
210
211  // OGF: 0x0004
212  // OCF: 0x0001
213  // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.4.1
214  void HciReadLocalVersionInformation(const std::vector<uint8_t>& args);
215
216  // OGF: 0x0004
217  // OCF: 0x0009
218  // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.4.6
219  void HciReadBdAddr(const std::vector<uint8_t>& args);
220
221  // OGF: 0x0004
222  // OCF: 0x0002
223  // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.4.2
224  void HciReadLocalSupportedCommands(const std::vector<uint8_t>& args);
225
226  // OGF: 0x0004
227  // OCF: 0x0004
228  // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.4.4
229  void HciReadLocalExtendedFeatures(const std::vector<uint8_t>& args);
230
231  // OGF: 0x0004
232  // OCF: 0x000B
233  // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.4.8
234  void HciReadLocalSupportedCodecs(const std::vector<uint8_t>& args);
235
236  // OGF: 0x0003
237  // OCF: 0x0056
238  // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.3.59
239  void HciWriteSimplePairingMode(const std::vector<uint8_t>& args);
240
241  // OGF: 0x0003
242  // OCF: 0x006D
243  // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.3.79
244  void HciWriteLeHostSupport(const std::vector<uint8_t>& args);
245
246  // OGF: 0x0003
247  // OCF: 0x0001
248  // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.3.1
249  void HciSetEventMask(const std::vector<uint8_t>& args);
250
251  // OGF: 0x0003
252  // OCF: 0x0045
253  // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.3.50
254  void HciWriteInquiryMode(const std::vector<uint8_t>& args);
255
256  // OGF: 0x0003
257  // OCF: 0x0047
258  // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.3.52
259  void HciWritePageScanType(const std::vector<uint8_t>& args);
260
261  // OGF: 0x0003
262  // OCF: 0x0043
263  // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.3.48
264  void HciWriteInquiryScanType(const std::vector<uint8_t>& args);
265
266  // OGF: 0x0003
267  // OCF: 0x0024
268  // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.3.26
269  void HciWriteClassOfDevice(const std::vector<uint8_t>& args);
270
271  // OGF: 0x0003
272  // OCF: 0x0018
273  // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.3.16
274  void HciWritePageTimeout(const std::vector<uint8_t>& args);
275
276  // OGF: 0x0002
277  // OCF: 0x000F
278  // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.2.12
279  void HciWriteDefaultLinkPolicySettings(const std::vector<uint8_t>& args);
280
281  // OGF: 0x0003
282  // OCF: 0x0014
283  // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.3.12
284  void HciReadLocalName(const std::vector<uint8_t>& args);
285
286  // OGF: 0x0003
287  // OCF: 0x0013
288  // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.3.11
289  void HciWriteLocalName(const std::vector<uint8_t>& args);
290
291  // OGF: 0x0003
292  // OCF: 0x0052
293  // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.3.56
294  void HciWriteExtendedInquiryResponse(const std::vector<uint8_t>& args);
295
296  // OGF: 0x0003
297  // OCF: 0x0026
298  // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.3.28
299  void HciWriteVoiceSetting(const std::vector<uint8_t>& args);
300
301  // OGF: 0x0003
302  // OCF: 0x003A
303  // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.3.45
304  void HciWriteCurrentIacLap(const std::vector<uint8_t>& args);
305
306  // OGF: 0x0003
307  // OCF: 0x001E
308  // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.3.22
309  void HciWriteInquiryScanActivity(const std::vector<uint8_t>& args);
310
311  // OGF: 0x0003
312  // OCF: 0x001A
313  // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.3.18
314  void HciWriteScanEnable(const std::vector<uint8_t>& args);
315
316  // OGF: 0x0003
317  // OCF: 0x0005
318  // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.3.3
319  void HciSetEventFilter(const std::vector<uint8_t>& args);
320
321  // OGF: 0x0001
322  // OCF: 0x0001
323  // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.1.1
324  void HciInquiry(const std::vector<uint8_t>& args);
325
326  void InquiryTimeout();
327
328  // OGF: 0x0001
329  // OCF: 0x0002
330  // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.1.2
331  void HciInquiryCancel(const std::vector<uint8_t>& args);
332
333  // OGF: 0x0003
334  // OCF: 0x0012
335  // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.3.10
336  void HciDeleteStoredLinkKey(const std::vector<uint8_t>& args);
337
338  // OGF: 0x0001
339  // OCF: 0x0019
340  // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.1.19
341  void HciRemoteNameRequest(const std::vector<uint8_t>& args);
342
343  // LE Controller Commands
344
345  // OGF: 0x0008
346  // OCF: 0x0001
347  // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.8.1
348  void HciLeSetEventMask(const std::vector<uint8_t>& args);
349
350  // OGF: 0x0008
351  // OCF: 0x0002
352  // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.8.2
353  void HciLeReadBufferSize(const std::vector<uint8_t>& args);
354
355  // OGF: 0x0008
356  // OCF: 0x0003
357  // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.8.3
358  void HciLeReadLocalSupportedFeatures(const std::vector<uint8_t>& args);
359
360  // OGF: 0x0008
361  // OCF: 0x0005
362  // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.8.4
363  void HciLeSetRandomAddress(const std::vector<uint8_t>& args);
364
365  // OGF: 0x0008
366  // OCF: 0x0006
367  // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.8.5
368  void HciLeSetAdvertisingParameters(const std::vector<uint8_t>& args);
369
370  // OGF: 0x0008
371  // OCF: 0x0008
372  // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.8.7
373  void HciLeSetAdvertisingData(const std::vector<uint8_t>& args);
374
375  // OGF: 0x0008
376  // OCF: 0x000B
377  // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.8.10
378  void HciLeSetScanParameters(const std::vector<uint8_t>& args);
379
380  // OGF: 0x0008
381  // OCF: 0x000C
382  // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.8.11
383  void HciLeSetScanEnable(const std::vector<uint8_t>& args);
384
385  // OGF: 0x0008
386  // OCF: 0x000F
387  // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.8.14
388  void HciLeReadWhiteListSize(const std::vector<uint8_t>& args);
389
390  // OGF: 0x0008
391  // OCF: 0x0018
392  // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.8.23
393  void HciLeRand(const std::vector<uint8_t>& args);
394
395  // OGF: 0x0008
396  // OCF: 0x001C
397  // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.8.27
398  void HciLeReadSupportedStates(const std::vector<uint8_t>& args);
399
400  // Vendor-specific commands (see hcidefs.h)
401
402  // OGF: 0x00FC
403  // OCF: 0x0027
404  void HciBleVendorSleepMode(const std::vector<uint8_t>& args);
405
406  // OGF: 0x00FC
407  // OCF: 0x0153
408  void HciBleVendorCap(const std::vector<uint8_t>& args);
409
410  // OGF: 0x00FC
411  // OCF: 0x0154
412  void HciBleVendorMultiAdv(const std::vector<uint8_t>& args);
413
414  // OGF: 0x00FC
415  // OCF: 0x0155
416  void HciBleVendor155(const std::vector<uint8_t>& args);
417
418  // OGF: 0x00FC
419  // OCF: 0x0157
420  void HciBleVendor157(const std::vector<uint8_t>& args);
421
422  // OGF: 0x00FC
423  // OCF: 0x0159
424  void HciBleEnergyInfo(const std::vector<uint8_t>& args);
425
426  // OGF: 0x00FC
427  // OCF: 0x015A
428  void HciBleExtendedScanParams(const std::vector<uint8_t>& args);
429
430  // Test Channel commands:
431
432  // Clears all test channel modifications.
433  void TestChannelClear(const std::vector<std::string>& args);
434
435  // Sets the response delay for events to 0.
436  void TestChannelClearEventDelay(const std::vector<std::string>& args);
437
438  // Discovers a fake device.
439  void TestChannelDiscover(const std::vector<std::string>& args);
440
441  // Causes events to be sent after a delay.
442  void TestChannelSetEventDelay(const std::vector<std::string>& args);
443
444  // Causes all future HCI commands to timeout.
445  void TestChannelTimeoutAll(const std::vector<std::string>& args);
446
447  void HandleTimerTick();
448  void SetTimerPeriod(std::chrono::milliseconds new_period);
449  void StartTimer();
450  void StopTimer();
451
452 private:
453  // Current link layer state of the controller.
454  enum State {
455    kStandby,  // Not receiving/transmitting any packets from/to other devices.
456    kInquiry,  // The controller is discovering other nearby devices.
457  };
458
459  enum TestChannelState {
460    kNone,             // The controller is running normally.
461    kTimeoutAll,       // All commands should time out, i.e. send no response.
462    kDelayedResponse,  // Event responses are sent after a delay.
463  };
464
465  // Set a timer for a future action
466  void AddControllerEvent(std::chrono::milliseconds,
467                          const TaskCallback& callback);
468
469  // Creates a command complete event and sends it back to the HCI.
470  void SendCommandComplete(uint16_t command_opcode,
471                           const std::vector<uint8_t>& return_parameters) const;
472
473  // Sends a command complete event with no return parameters. This event is
474  // typically sent for commands that can be completed immediately.
475  void SendCommandCompleteSuccess(uint16_t command_opcode) const;
476
477  // Sends a command complete event with no return parameters. This event is
478  // typically sent for commands that can be completed immediately.
479  void SendCommandCompleteOnlyStatus(uint16_t command_opcode,
480                                     uint8_t status) const;
481
482  // Creates a command status event and sends it back to the HCI.
483  void SendCommandStatus(uint8_t status, uint16_t command_opcode) const;
484
485  // Sends a command status event with default event parameters.
486  void SendCommandStatusSuccess(uint16_t command_opcode) const;
487
488  void SetEventDelay(int64_t delay);
489
490  // Callbacks to schedule tasks.
491  std::function<AsyncTaskId(std::chrono::milliseconds, const TaskCallback&)>
492      schedule_task_;
493  std::function<AsyncTaskId(std::chrono::milliseconds,
494                            std::chrono::milliseconds, const TaskCallback&)>
495      schedule_periodic_task_;
496
497  std::function<void(AsyncTaskId)> cancel_task_;
498
499  // Callback provided to send events from the controller back to the HCI.
500  std::function<void(std::unique_ptr<EventPacket>)> send_event_;
501
502  // Maintains the commands to be registered and used in the HciHandler object.
503  // Keys are command opcodes and values are the callbacks to handle each
504  // command.
505  std::unordered_map<uint16_t, std::function<void(const std::vector<uint8_t>&)>>
506      active_hci_commands_;
507
508  std::unordered_map<std::string,
509                     std::function<void(const std::vector<std::string>&)>>
510      active_test_channel_commands_;
511
512  // Specifies the format of Inquiry Result events to be returned during the
513  // Inquiry command.
514  // 0x00: Standard Inquiry Result event format (default).
515  // 0x01: Inquiry Result format with RSSI.
516  // 0x02 Inquiry Result with RSSI format or Extended Inquiry Result format.
517  // 0x03-0xFF: Reserved.
518  uint8_t inquiry_mode_;
519
520  std::vector<uint8_t> le_event_mask_;
521
522  BtAddress le_random_address_;
523
524  uint8_t le_scan_type_;
525  uint16_t le_scan_interval_;
526  uint16_t le_scan_window_;
527  uint8_t own_address_type_;
528  uint8_t scanning_filter_policy_;
529
530  uint8_t le_scan_enable_;
531  uint8_t filter_duplicates_;
532
533  State state_;
534
535  Properties properties_;
536
537  TestChannelState test_channel_state_;
538
539  std::vector<AsyncTaskId> controller_events_;
540  AsyncTaskId timer_tick_task_;
541  std::chrono::milliseconds timer_period_ = std::chrono::milliseconds(1000);
542
543  DualModeController(const DualModeController& cmdPckt) = delete;
544  DualModeController& operator=(const DualModeController& cmdPckt) = delete;
545};
546
547}  // namespace test_vendor_lib
548