Home | History | Annotate | Download | only in include
      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 <vector>
     23 #include <unordered_map>
     24 
     25 #include "base/json/json_value_converter.h"
     26 #include "base/time/time.h"
     27 #include "vendor_libs/test_vendor_lib/include/command_packet.h"
     28 #include "vendor_libs/test_vendor_lib/include/hci_transport.h"
     29 #include "vendor_libs/test_vendor_lib/include/test_channel_transport.h"
     30 
     31 namespace test_vendor_lib {
     32 
     33 // Emulates a dual mode BR/EDR + LE controller by maintaining the link layer
     34 // state machine detailed in the Bluetooth Core Specification Version 4.2,
     35 // Volume 6, Part B, Section 1.1 (page 30). Provides methods corresponding to
     36 // commands sent by the HCI. These methods will be registered as callbacks from
     37 // a controller instance with the HciHandler. To implement a new Bluetooth
     38 // command, simply add the method declaration below, with return type void and a
     39 // single const std::vector<uint8_t>& argument. After implementing the
     40 // method, simply register it with the HciHandler using the SET_HANDLER macro in
     41 // the controller's default constructor. Be sure to name your method after the
     42 // corresponding Bluetooth command in the Core Specification with the prefix
     43 // "Hci" to distinguish it as a controller command.
     44 class DualModeController {
     45  public:
     46   class Properties {
     47    public:
     48     // TODO(dennischeng): Add default initialization and use that to instantiate
     49     // a default configured controller if the config file is invalid or not
     50     // provided.
     51     Properties(const std::string& file_name);
     52 
     53     // Aggregates and returns the result for the Read Local Extended Features
     54     // command. This result contains the |maximum_page_number_| property (among
     55     // other things not in the Properties object). See the Bluetooth Core
     56     // Specification Version 4.2, Volume 2, Part E, Section 7.4.4 (page 792).
     57     const std::vector<uint8_t> GetBdAddress();
     58 
     59     // Aggregates and returns the result for the Read Buffer Size command. This
     60     // result consists of the |acl_data_packet_size_|, |sco_data_packet_size_|,
     61     // |num_acl_data_packets_|, and |num_sco_data_packets_| properties. See the
     62     // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.4.5
     63     // (page 794).
     64     const std::vector<uint8_t> GetBufferSize();
     65 
     66     // Returns the result for the Read BD_ADDR command. This result is the
     67     // |bd_address_| property. See the Bluetooth Core Specification Version
     68     // 4.2, Volume 2, Part E, Section 7.4.6 (page 796).
     69     const std::vector<uint8_t> GetLocalExtendedFeatures(uint8_t page_number);
     70 
     71     // Returns the result for the Read Local Name command. See the Bluetooth
     72     // Core Specification Version 4.2, Volume 2, Part E, Section 7.3.12
     73     // (page 664).
     74     const std::vector<uint8_t> GetLocalName();
     75 
     76     // Returns the result for the Read Local Supported Commands command. See the
     77     // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.4.2
     78     // (page 790).
     79     const std::vector<uint8_t> GetLocalSupportedCommands();
     80 
     81     // Aggregates and returns the Read Local Version Information result. This
     82     // consists of the |version_|, |revision_|, |lmp_pal_version_|,
     83     // |manufacturer_name_|, and |lmp_pal_subversion_|. See the Bluetooth Core
     84     // Specification Version 4.2, Volume 2, Part E, Section 7.4.1 (page 788).
     85     const std::vector<uint8_t> GetLocalVersionInformation();
     86 
     87     static void RegisterJSONConverter(
     88         base::JSONValueConverter<Properties>* converter);
     89 
     90    private:
     91     uint16_t acl_data_packet_size_;
     92     uint8_t sco_data_packet_size_;
     93     uint16_t num_acl_data_packets_;
     94     uint16_t num_sco_data_packets_;
     95     uint8_t version_;
     96     uint16_t revision_;
     97     uint8_t lmp_pal_version_;
     98     uint16_t manufacturer_name_;
     99     uint16_t lmp_pal_subversion_;
    100     uint8_t maximum_page_number_;
    101     uint8_t local_supported_commands_size_;
    102     uint8_t local_name_size_;
    103     std::vector<uint8_t> bd_address_;
    104   };
    105 
    106   // Sets all of the methods to be used as callbacks in the HciHandler.
    107   DualModeController();
    108 
    109   ~DualModeController() = default;
    110 
    111   // Preprocesses the command, primarily checking testh channel hooks. If
    112   // possible, dispatches the corresponding controller method corresponding to
    113   // carry out the command.
    114   void HandleCommand(std::unique_ptr<CommandPacket> command_packet);
    115 
    116   // Dispatches the test channel action corresponding to the command specified
    117   // by |name|.
    118   void HandleTestChannelCommand(const std::string& name,
    119                                 const std::vector<std::string>& args);
    120 
    121   // Sets the controller Handle* methods as callbacks for the transport to call
    122   // when data is received.
    123   void RegisterHandlersWithHciTransport(HciTransport& transport);
    124 
    125   // Sets the test channel handler with the transport dedicated to test channel
    126   // communications.
    127   void RegisterHandlersWithTestChannelTransport(
    128       TestChannelTransport& transport);
    129 
    130   // Sets the callback to be used for sending events back to the HCI.
    131   // TODO(dennischeng): Once PostDelayedTask works, get rid of this and only use
    132   // |RegisterDelayedEventChannel|.
    133   void RegisterEventChannel(
    134       std::function<void(std::unique_ptr<EventPacket>)> send_event);
    135 
    136   void RegisterDelayedEventChannel(
    137       std::function<void(std::unique_ptr<EventPacket>, base::TimeDelta)>
    138           send_event);
    139 
    140   // Controller commands. For error codes, see the Bluetooth Core Specification,
    141   // Version 4.2, Volume 2, Part D (page 370).
    142 
    143   // OGF: 0x0003
    144   // OCF: 0x0003
    145   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.3.2
    146   void HciReset(const std::vector<uint8_t>& args);
    147 
    148   // OGF: 0x0004
    149   // OGF: 0x0005
    150   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.4.5
    151   void HciReadBufferSize(const std::vector<uint8_t>& args);
    152 
    153   // OGF: 0x0003
    154   // OCF: 0x0033
    155   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.3.39
    156   void HciHostBufferSize(const std::vector<uint8_t>& args);
    157 
    158   // OGF: 0x0004
    159   // OCF: 0x0001
    160   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.4.1
    161   void HciReadLocalVersionInformation(const std::vector<uint8_t>& args);
    162 
    163   // OGF: 0x0004
    164   // OCF: 0x0009
    165   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.4.6
    166   void HciReadBdAddr(const std::vector<uint8_t>& args);
    167 
    168   // OGF: 0x0004
    169   // OCF: 0x0002
    170   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.4.2
    171   void HciReadLocalSupportedCommands(const std::vector<uint8_t>& args);
    172 
    173   // OGF: 0x0004
    174   // OCF: 0x0004
    175   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.4.4
    176   void HciReadLocalExtendedFeatures(const std::vector<uint8_t>& args);
    177 
    178   // OGF: 0x0003
    179   // OCF: 0x0056
    180   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.3.59
    181   void HciWriteSimplePairingMode(const std::vector<uint8_t>& args);
    182 
    183   // OGF: 0x0003
    184   // OCF: 0x006D
    185   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.3.79
    186   void HciWriteLeHostSupport(const std::vector<uint8_t>& args);
    187 
    188   // OGF: 0x0003
    189   // OCF: 0x0001
    190   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.3.1
    191   void HciSetEventMask(const std::vector<uint8_t>& args);
    192 
    193   // OGF: 0x0003
    194   // OCF: 0x0045
    195   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.3.50
    196    void HciWriteInquiryMode(const std::vector<uint8_t>& args);
    197 
    198   // OGF: 0x0003
    199   // OCF: 0x0047
    200   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.3.52
    201    void HciWritePageScanType(const std::vector<uint8_t>& args);
    202 
    203   // OGF: 0x0003
    204   // OCF: 0x0043
    205   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.3.48
    206   void HciWriteInquiryScanType(const std::vector<uint8_t>& args);
    207 
    208   // OGF: 0x0003
    209   // OCF: 0x0024
    210   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.3.26
    211   void HciWriteClassOfDevice(const std::vector<uint8_t>& args);
    212 
    213   // OGF: 0x0003
    214   // OCF: 0x0018
    215   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.3.16
    216   void HciWritePageTimeout(const std::vector<uint8_t>& args);
    217 
    218   // OGF: 0x0002
    219   // OCF: 0x000F
    220   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.2.12
    221   void HciWriteDefaultLinkPolicySettings(const std::vector<uint8_t>& args);
    222 
    223   // OGF: 0x0003
    224   // OCF: 0x0014
    225   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.3.12
    226   void HciReadLocalName(const std::vector<uint8_t>& args);
    227 
    228   // OGF: 0x0003
    229   // OCF: 0x0013
    230   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.3.11
    231   void HciWriteLocalName(const std::vector<uint8_t>& args);
    232 
    233   // OGF: 0x0003
    234   // OCF: 0x0052
    235   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.3.56
    236   void HciWriteExtendedInquiryResponse(const std::vector<uint8_t>& args);
    237 
    238   // OGF: 0x0003
    239   // OCF: 0x0026
    240   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.3.28
    241   void HciWriteVoiceSetting(const std::vector<uint8_t>& args);
    242 
    243   // OGF: 0x0003
    244   // OCF: 0x003A
    245   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.3.45
    246     void HciWriteCurrentIacLap(const std::vector<uint8_t>& args);
    247 
    248   // OGF: 0x0003
    249   // OCF: 0x001E
    250   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.3.22
    251    void HciWriteInquiryScanActivity(const std::vector<uint8_t>& args);
    252 
    253   // OGF: 0x0003
    254   // OCF: 0x001A
    255   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.3.18
    256   void HciWriteScanEnable(const std::vector<uint8_t>& args);
    257 
    258   // OGF: 0x0003
    259   // OCF: 0x0005
    260   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.3.3
    261   void HciSetEventFilter(const std::vector<uint8_t>& args);
    262 
    263   // OGF: 0x0001
    264   // OCF: 0x0001
    265   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.1.1
    266   void HciInquiry(const std::vector<uint8_t>& args);
    267 
    268   // OGF: 0x0001
    269   // OCF: 0x0002
    270   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.1.2
    271   void HciInquiryCancel(const std::vector<uint8_t>& args);
    272 
    273   // OGF: 0x0003
    274   // OCF: 0x0012
    275   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.3.10
    276   void HciDeleteStoredLinkKey(const std::vector<uint8_t>& args);
    277 
    278   // OGF: 0x0001
    279   // OCF: 0x0019
    280   // Bluetooth Core Specification Version 4.2 Volume 2 Part E 7.1.19
    281   void HciRemoteNameRequest(const std::vector<uint8_t>& args);
    282 
    283   // Test Channel commands:
    284 
    285   // Clears all test channel modifications.
    286   void TestChannelClear(const std::vector<std::string>& args);
    287 
    288   // Sets the response delay for events to 0.
    289   void TestChannelClearEventDelay(const std::vector<std::string>& args);
    290 
    291   // Discovers a fake device.
    292   void TestChannelDiscover(const std::vector<std::string>& args);
    293 
    294   // Causes events to be sent after a delay.
    295   void TestChannelSetEventDelay(const std::vector<std::string>& args);
    296 
    297   // Causes all future HCI commands to timeout.
    298   void TestChannelTimeoutAll(const std::vector<std::string>& args);
    299 
    300  private:
    301   // Current link layer state of the controller.
    302   enum State {
    303     kStandby,  // Not receiving/transmitting any packets from/to other devices.
    304     kInquiry,  // The controller is discovering other nearby devices.
    305   };
    306 
    307   enum TestChannelState {
    308     kNone,  // The controller is running normally.
    309     kTimeoutAll,  // All commands should time out, i.e. send no response.
    310     kDelayedResponse,  // Event responses are sent after a delay.
    311   };
    312 
    313   // Creates a command complete event and sends it back to the HCI.
    314   void SendCommandComplete(uint16_t command_opcode,
    315                            const std::vector<uint8_t>& return_parameters) const;
    316 
    317   // Sends a command complete event with no return parameters. This event is
    318   // typically sent for commands that can be completed immediately.
    319   void SendCommandCompleteSuccess(uint16_t command_opcode) const;
    320 
    321   // Creates a command status event and sends it back to the HCI.
    322   void SendCommandStatus(uint8_t status, uint16_t command_opcode) const;
    323 
    324   // Sends a command status event with default event parameters.
    325   void SendCommandStatusSuccess(uint16_t command_opcode) const;
    326 
    327   // Sends an inquiry response for a fake device.
    328   void SendInquiryResult() const;
    329 
    330   // Sends an extended inquiry response for a fake device.
    331   void SendExtendedInquiryResult(const std::string& name,
    332                                  const std::string& address) const;
    333 
    334   void SetEventDelay(int64_t delay);
    335 
    336   // Callback provided to send events from the controller back to the HCI.
    337   std::function<void(std::unique_ptr<EventPacket>)> send_event_;
    338 
    339   std::function<void(std::unique_ptr<EventPacket>, base::TimeDelta)>
    340       send_delayed_event_;
    341 
    342   // Maintains the commands to be registered and used in the HciHandler object.
    343   // Keys are command opcodes and values are the callbacks to handle each
    344   // command.
    345   std::unordered_map<uint16_t, std::function<void(const std::vector<uint8_t>&)>>
    346       active_hci_commands_;
    347 
    348   std::unordered_map<std::string,
    349                      std::function<void(const std::vector<std::string>&)>>
    350       active_test_channel_commands_;
    351 
    352   // Specifies the format of Inquiry Result events to be returned during the
    353   // Inquiry command.
    354   // 0x00: Standard Inquiry Result event format (default).
    355   // 0x01: Inquiry Result format with RSSI.
    356   // 0x02 Inquiry Result with RSSI format or Extended Inquiry Result format.
    357   // 0x03-0xFF: Reserved.
    358   uint8_t inquiry_mode_;
    359 
    360   State state_;
    361 
    362   Properties properties_;
    363 
    364   TestChannelState test_channel_state_;
    365 
    366   DISALLOW_COPY_AND_ASSIGN(DualModeController);
    367 };
    368 
    369 }  // namespace test_vendor_lib
    370