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 24 #include "base/logging.h" 25 #include "bt_address.h" 26 #include "packet.h" 27 28 namespace test_vendor_lib { 29 30 // Event Packets are specified in the Bluetooth Core Specification Version 4.2, 31 // Volume 2, Part E, Section 5.4.4 32 class EventPacket : public Packet { 33 public: 34 virtual ~EventPacket() override = default; 35 36 uint8_t GetEventCode() const; 37 38 // Static functions for creating event packets: 39 40 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.7.1 41 static std::unique_ptr<EventPacket> CreateInquiryCompleteEvent( 42 uint8_t status); 43 44 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.7.14 45 // This should only be used for testing to send non-standard packets 46 // Most code should use the more specific functions that follow 47 static std::unique_ptr<EventPacket> CreateCommandCompleteEvent( 48 uint16_t command_opcode, 49 const std::vector<uint8_t>& event_return_parameters); 50 51 static std::unique_ptr<EventPacket> CreateCommandCompleteOnlyStatusEvent( 52 uint16_t command_opcode, uint8_t status); 53 54 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.7.15 55 static std::unique_ptr<EventPacket> CreateCommandStatusEvent( 56 uint8_t status, uint16_t command_opcode); 57 58 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.3.12 59 static std::unique_ptr<EventPacket> CreateCommandCompleteReadLocalName( 60 uint8_t status, const std::string& local_name); 61 62 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.4.1 63 static std::unique_ptr<EventPacket> 64 CreateCommandCompleteReadLocalVersionInformation(uint8_t status, 65 uint8_t hci_version, 66 uint16_t hci_revision, 67 uint8_t lmp_pal_version, 68 uint16_t manufacturer_name, 69 uint16_t lmp_pal_subversion); 70 71 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.4.2 72 static std::unique_ptr<EventPacket> 73 CreateCommandCompleteReadLocalSupportedCommands( 74 uint8_t status, const std::vector<uint8_t>& supported_commands); 75 76 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.4.4 77 static std::unique_ptr<EventPacket> 78 CreateCommandCompleteReadLocalExtendedFeatures( 79 uint8_t status, uint8_t page_number, uint8_t maximum_page_number, 80 uint64_t extended_lmp_features); 81 82 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.4.5 83 static std::unique_ptr<EventPacket> CreateCommandCompleteReadBufferSize( 84 uint8_t status, uint16_t hc_acl_data_packet_length, 85 uint8_t hc_synchronous_data_packet_length, 86 uint16_t hc_total_num_acl_data_packets, 87 uint16_t hc_total_synchronous_data_packets); 88 89 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.4.6 90 static std::unique_ptr<EventPacket> CreateCommandCompleteReadBdAddr( 91 uint8_t status, const BtAddress& bt_address); 92 93 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.4.8 94 static std::unique_ptr<EventPacket> 95 CreateCommandCompleteReadLocalSupportedCodecs( 96 uint8_t status, const std::vector<uint8_t>& supported_codecs, 97 const std::vector<uint32_t>& vendor_specific_codecs); 98 99 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.7.2 100 enum PageScanRepetitionMode { 101 kR0 = 0, 102 kR1 = 1, 103 kR2 = 2, 104 }; 105 106 static std::unique_ptr<EventPacket> CreateInquiryResultEvent( 107 const BtAddress& bt_address, 108 const PageScanRepetitionMode page_scan_repetition_mode, 109 uint32_t class_of_device, uint16_t clock_offset); 110 111 void AddInquiryResult(const BtAddress& bt_address, 112 const PageScanRepetitionMode page_scan_repetition_mode, 113 uint32_t class_of_device, uint16_t clock_offset); 114 115 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.7.38 116 static std::unique_ptr<EventPacket> CreateExtendedInquiryResultEvent( 117 const BtAddress& bt_address, 118 const PageScanRepetitionMode page_scan_repetition_mode, 119 uint32_t class_of_device, uint16_t clock_offset, uint8_t rssi, 120 const std::vector<uint8_t>& extended_inquiry_response); 121 122 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.8.2 123 static std::unique_ptr<EventPacket> CreateCommandCompleteLeReadBufferSize( 124 uint8_t status, uint16_t hc_le_data_packet_length, 125 uint8_t hc_total_num_le_data_packets); 126 127 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.8.3 128 static std::unique_ptr<EventPacket> 129 CreateCommandCompleteLeReadLocalSupportedFeatures(uint8_t status, 130 uint64_t le_features); 131 132 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.8.14 133 static std::unique_ptr<EventPacket> CreateCommandCompleteLeReadWhiteListSize( 134 uint8_t status, uint8_t white_list_size); 135 136 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.8.23 137 static std::unique_ptr<EventPacket> CreateCommandCompleteLeRand( 138 uint8_t status, uint64_t random_val); 139 140 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.8.27 141 static std::unique_ptr<EventPacket> 142 CreateCommandCompleteLeReadSupportedStates(uint8_t status, 143 uint64_t le_states); 144 145 // Vendor-specific commands (see hcidefs.h) 146 147 static std::unique_ptr<EventPacket> CreateCommandCompleteLeVendorCap( 148 uint8_t status, const std::vector<uint8_t>& vendor_cap); 149 150 // Size of a data packet header, which consists of a 1 octet event code 151 static const size_t kEventHeaderSize = 1; 152 153 private: 154 explicit EventPacket(uint8_t event_code); 155 EventPacket(uint8_t event_code, const std::vector<uint8_t>& payload); 156 }; 157 158 } // namespace test_vendor_lib 159