Home | History | Annotate | Download | only in src
      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 #define LOG_TAG "event_packet"
     18 
     19 #include "event_packet.h"
     20 
     21 #include "osi/include/log.h"
     22 #include "stack/include/hcidefs.h"
     23 
     24 using std::vector;
     25 
     26 namespace test_vendor_lib {
     27 
     28 EventPacket::EventPacket(uint8_t event_code)
     29     : Packet(DATA_TYPE_EVENT, {event_code}) {}
     30 
     31 uint8_t EventPacket::GetEventCode() const { return GetHeader()[0]; }
     32 
     33 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.7.1
     34 std::unique_ptr<EventPacket> EventPacket::CreateInquiryCompleteEvent(
     35     uint8_t status) {
     36   std::unique_ptr<EventPacket> evt_ptr =
     37       std::unique_ptr<EventPacket>(new EventPacket(HCI_INQUIRY_COMP_EVT));
     38   CHECK(evt_ptr->AddPayloadOctets1(status));
     39 
     40   return evt_ptr;
     41 }
     42 
     43 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.7.14
     44 std::unique_ptr<EventPacket> EventPacket::CreateCommandCompleteEvent(
     45     uint16_t command_opcode, const vector<uint8_t>& event_return_parameters) {
     46   std::unique_ptr<EventPacket> evt_ptr =
     47       std::unique_ptr<EventPacket>(new EventPacket(HCI_COMMAND_COMPLETE_EVT));
     48 
     49   CHECK(evt_ptr->AddPayloadOctets1(1));  // num_hci_command_packets
     50   CHECK(evt_ptr->AddPayloadOctets2(command_opcode));
     51   CHECK(evt_ptr->AddPayloadOctets(event_return_parameters.size(),
     52                                   event_return_parameters));
     53 
     54   return evt_ptr;
     55 }
     56 
     57 std::unique_ptr<EventPacket> EventPacket::CreateCommandCompleteOnlyStatusEvent(
     58     uint16_t command_opcode, uint8_t status) {
     59   std::unique_ptr<EventPacket> evt_ptr =
     60       std::unique_ptr<EventPacket>(new EventPacket(HCI_COMMAND_COMPLETE_EVT));
     61 
     62   CHECK(evt_ptr->AddPayloadOctets1(1));  // num_hci_command_packets
     63   CHECK(evt_ptr->AddPayloadOctets2(command_opcode));
     64   CHECK(evt_ptr->AddPayloadOctets1(status));
     65 
     66   return evt_ptr;
     67 }
     68 
     69 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.7.15
     70 std::unique_ptr<EventPacket> EventPacket::CreateCommandStatusEvent(
     71     uint8_t status, uint16_t command_opcode) {
     72   std::unique_ptr<EventPacket> evt_ptr =
     73       std::unique_ptr<EventPacket>(new EventPacket(HCI_COMMAND_STATUS_EVT));
     74 
     75   CHECK(evt_ptr->AddPayloadOctets1(status));
     76   CHECK(evt_ptr->AddPayloadOctets1(1));  // num_hci_command_packets
     77   CHECK(evt_ptr->AddPayloadOctets2(command_opcode));
     78 
     79   return evt_ptr;
     80 }
     81 
     82 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.3.12
     83 std::unique_ptr<EventPacket> EventPacket::CreateCommandCompleteReadLocalName(
     84     uint8_t status, const std::string& local_name) {
     85   std::unique_ptr<EventPacket> evt_ptr =
     86       EventPacket::CreateCommandCompleteOnlyStatusEvent(HCI_READ_LOCAL_NAME,
     87                                                         status);
     88 
     89   for (size_t i = 0; i < local_name.length(); i++)
     90     CHECK(evt_ptr->AddPayloadOctets1(local_name[i]));
     91   CHECK(evt_ptr->AddPayloadOctets1(0));  // Null terminated
     92   for (size_t i = 0; i < 248 - local_name.length() - 1; i++)
     93     CHECK(evt_ptr->AddPayloadOctets1(0xFF));
     94 
     95   return evt_ptr;
     96 }
     97 
     98 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.4.1
     99 std::unique_ptr<EventPacket>
    100 EventPacket::CreateCommandCompleteReadLocalVersionInformation(
    101     uint8_t status, uint8_t hci_version, uint16_t hci_revision,
    102     uint8_t lmp_pal_version, uint16_t manufacturer_name,
    103     uint16_t lmp_pal_subversion) {
    104   std::unique_ptr<EventPacket> evt_ptr =
    105       EventPacket::CreateCommandCompleteOnlyStatusEvent(
    106           HCI_READ_LOCAL_VERSION_INFO, status);
    107 
    108   CHECK(evt_ptr->AddPayloadOctets1(hci_version));
    109   CHECK(evt_ptr->AddPayloadOctets2(hci_revision));
    110   CHECK(evt_ptr->AddPayloadOctets1(lmp_pal_version));
    111   CHECK(evt_ptr->AddPayloadOctets2(manufacturer_name));
    112   CHECK(evt_ptr->AddPayloadOctets2(lmp_pal_subversion));
    113 
    114   return evt_ptr;
    115 }
    116 
    117 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.4.2
    118 std::unique_ptr<EventPacket>
    119 EventPacket::CreateCommandCompleteReadLocalSupportedCommands(
    120     uint8_t status, const vector<uint8_t>& supported_commands) {
    121   std::unique_ptr<EventPacket> evt_ptr =
    122       EventPacket::CreateCommandCompleteOnlyStatusEvent(
    123           HCI_READ_LOCAL_SUPPORTED_CMDS, status);
    124 
    125   CHECK(evt_ptr->AddPayloadOctets(64, supported_commands));
    126 
    127   return evt_ptr;
    128 }
    129 
    130 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.4.4
    131 std::unique_ptr<EventPacket>
    132 EventPacket::CreateCommandCompleteReadLocalExtendedFeatures(
    133     uint8_t status, uint8_t page_number, uint8_t maximum_page_number,
    134     uint64_t extended_lmp_features) {
    135   std::unique_ptr<EventPacket> evt_ptr =
    136       EventPacket::CreateCommandCompleteOnlyStatusEvent(
    137           HCI_READ_LOCAL_EXT_FEATURES, status);
    138 
    139   CHECK(evt_ptr->AddPayloadOctets1(page_number));
    140   CHECK(evt_ptr->AddPayloadOctets1(maximum_page_number));
    141   CHECK(evt_ptr->AddPayloadOctets8(extended_lmp_features));
    142 
    143   return evt_ptr;
    144 }
    145 
    146 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.4.5
    147 std::unique_ptr<EventPacket> EventPacket::CreateCommandCompleteReadBufferSize(
    148     uint8_t status, uint16_t hc_acl_data_packet_length,
    149     uint8_t hc_synchronous_data_packet_length,
    150     uint16_t hc_total_num_acl_data_packets,
    151     uint16_t hc_total_synchronous_data_packets) {
    152   std::unique_ptr<EventPacket> evt_ptr =
    153       EventPacket::CreateCommandCompleteOnlyStatusEvent(HCI_READ_BUFFER_SIZE,
    154                                                         status);
    155 
    156   CHECK(evt_ptr->AddPayloadOctets2(hc_acl_data_packet_length));
    157   CHECK(evt_ptr->AddPayloadOctets1(hc_synchronous_data_packet_length));
    158   CHECK(evt_ptr->AddPayloadOctets2(hc_total_num_acl_data_packets));
    159   CHECK(evt_ptr->AddPayloadOctets2(hc_total_synchronous_data_packets));
    160 
    161   return evt_ptr;
    162 }
    163 
    164 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.4.6
    165 std::unique_ptr<EventPacket> EventPacket::CreateCommandCompleteReadBdAddr(
    166     uint8_t status, const BtAddress& address) {
    167   std::unique_ptr<EventPacket> evt_ptr =
    168       EventPacket::CreateCommandCompleteOnlyStatusEvent(HCI_READ_BD_ADDR,
    169                                                         status);
    170 
    171   CHECK(evt_ptr->AddPayloadBtAddress(address));
    172 
    173   return evt_ptr;
    174 }
    175 
    176 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.4.8
    177 std::unique_ptr<EventPacket>
    178 EventPacket::CreateCommandCompleteReadLocalSupportedCodecs(
    179     uint8_t status, const vector<uint8_t>& supported_codecs,
    180     const vector<uint32_t>& vendor_specific_codecs) {
    181   std::unique_ptr<EventPacket> evt_ptr =
    182       EventPacket::CreateCommandCompleteOnlyStatusEvent(
    183           HCI_READ_LOCAL_SUPPORTED_CODECS, status);
    184 
    185   CHECK(evt_ptr->AddPayloadOctets(supported_codecs.size(), supported_codecs));
    186   for (size_t i = 0; i < vendor_specific_codecs.size(); i++)
    187     CHECK(evt_ptr->AddPayloadOctets4(vendor_specific_codecs[i]));
    188 
    189   return evt_ptr;
    190 }
    191 
    192 std::unique_ptr<EventPacket> EventPacket::CreateInquiryResultEvent(
    193     const BtAddress& address,
    194     const PageScanRepetitionMode page_scan_repetition_mode,
    195     uint32_t class_of_device, uint16_t clock_offset) {
    196   std::unique_ptr<EventPacket> evt_ptr =
    197       std::unique_ptr<EventPacket>(new EventPacket(HCI_INQUIRY_RESULT_EVT));
    198 
    199   CHECK(evt_ptr->AddPayloadOctets1(1));  // Start with a single response
    200 
    201   CHECK(evt_ptr->AddPayloadBtAddress(address));
    202   CHECK(evt_ptr->AddPayloadOctets1(page_scan_repetition_mode));
    203   CHECK(evt_ptr->AddPayloadOctets2(kReservedZero));
    204   CHECK(evt_ptr->AddPayloadOctets3(class_of_device));
    205   CHECK(!(clock_offset & 0x8000));
    206   CHECK(evt_ptr->AddPayloadOctets2(clock_offset));
    207 
    208   return evt_ptr;
    209 }
    210 
    211 void EventPacket::AddInquiryResult(
    212     const BtAddress& address,
    213     const PageScanRepetitionMode page_scan_repetition_mode,
    214     uint32_t class_of_device, uint16_t clock_offset) {
    215   CHECK(GetEventCode() == HCI_INQUIRY_RESULT_EVT);
    216 
    217   CHECK(IncrementPayloadCounter(1));  // Increment the number of responses
    218 
    219   CHECK(AddPayloadBtAddress(address));
    220   CHECK(AddPayloadOctets1(page_scan_repetition_mode));
    221   CHECK(AddPayloadOctets2(kReservedZero));
    222   CHECK(AddPayloadOctets3(class_of_device));
    223   CHECK(!(clock_offset & 0x8000));
    224   CHECK(AddPayloadOctets2(clock_offset));
    225 }
    226 
    227 std::unique_ptr<EventPacket> EventPacket::CreateExtendedInquiryResultEvent(
    228     const BtAddress& address,
    229     const PageScanRepetitionMode page_scan_repetition_mode,
    230     uint32_t class_of_device, uint16_t clock_offset, uint8_t rssi,
    231     const vector<uint8_t>& extended_inquiry_response) {
    232   std::unique_ptr<EventPacket> evt_ptr = std::unique_ptr<EventPacket>(
    233       new EventPacket(HCI_EXTENDED_INQUIRY_RESULT_EVT));
    234 
    235   CHECK(evt_ptr->AddPayloadOctets1(1));  // Always contains a single response
    236 
    237   CHECK(evt_ptr->AddPayloadBtAddress(address));
    238   CHECK(evt_ptr->AddPayloadOctets1(page_scan_repetition_mode));
    239   CHECK(evt_ptr->AddPayloadOctets1(kReservedZero));
    240   CHECK(evt_ptr->AddPayloadOctets3(class_of_device));
    241   CHECK(!(clock_offset & 0x8000));
    242   CHECK(evt_ptr->AddPayloadOctets2(clock_offset));
    243   CHECK(evt_ptr->AddPayloadOctets1(rssi));
    244   CHECK(evt_ptr->AddPayloadOctets(extended_inquiry_response.size(),
    245                                   extended_inquiry_response));
    246   while (evt_ptr->AddPayloadOctets1(0xff))
    247     ;  // Fill packet
    248   return evt_ptr;
    249 }
    250 
    251 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.8.2
    252 std::unique_ptr<EventPacket> EventPacket::CreateCommandCompleteLeReadBufferSize(
    253     uint8_t status, uint16_t hc_le_data_packet_length,
    254     uint8_t hc_total_num_le_data_packets) {
    255   std::unique_ptr<EventPacket> evt_ptr =
    256       EventPacket::CreateCommandCompleteOnlyStatusEvent(
    257           HCI_BLE_READ_BUFFER_SIZE, status);
    258 
    259   CHECK(evt_ptr->AddPayloadOctets2(hc_le_data_packet_length));
    260   CHECK(evt_ptr->AddPayloadOctets1(hc_total_num_le_data_packets));
    261 
    262   return evt_ptr;
    263 }
    264 
    265 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.8.3
    266 std::unique_ptr<EventPacket>
    267 EventPacket::CreateCommandCompleteLeReadLocalSupportedFeatures(
    268     uint8_t status, uint64_t le_features) {
    269   std::unique_ptr<EventPacket> evt_ptr =
    270       EventPacket::CreateCommandCompleteOnlyStatusEvent(
    271           HCI_BLE_READ_LOCAL_SPT_FEAT, status);
    272 
    273   CHECK(evt_ptr->AddPayloadOctets8(le_features));
    274 
    275   return evt_ptr;
    276 }
    277 
    278 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.8.14
    279 std::unique_ptr<EventPacket>
    280 EventPacket::CreateCommandCompleteLeReadWhiteListSize(uint8_t status,
    281                                                       uint8_t white_list_size) {
    282   std::unique_ptr<EventPacket> evt_ptr =
    283       EventPacket::CreateCommandCompleteOnlyStatusEvent(
    284           HCI_BLE_READ_WHITE_LIST_SIZE, status);
    285 
    286   CHECK(evt_ptr->AddPayloadOctets8(white_list_size));
    287 
    288   return evt_ptr;
    289 }
    290 
    291 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.8.23
    292 std::unique_ptr<EventPacket> EventPacket::CreateCommandCompleteLeRand(
    293     uint8_t status, uint64_t random_val) {
    294   std::unique_ptr<EventPacket> evt_ptr =
    295       EventPacket::CreateCommandCompleteOnlyStatusEvent(HCI_BLE_RAND, status);
    296 
    297   CHECK(evt_ptr->AddPayloadOctets8(random_val));
    298 
    299   return evt_ptr;
    300 }
    301 
    302 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.8.27
    303 std::unique_ptr<EventPacket>
    304 EventPacket::CreateCommandCompleteLeReadSupportedStates(uint8_t status,
    305                                                         uint64_t le_states) {
    306   std::unique_ptr<EventPacket> evt_ptr =
    307       EventPacket::CreateCommandCompleteOnlyStatusEvent(
    308           HCI_BLE_READ_SUPPORTED_STATES, status);
    309 
    310   CHECK(evt_ptr->AddPayloadOctets8(le_states));
    311 
    312   return evt_ptr;
    313 }
    314 
    315 // Vendor-specific commands (see hcidefs.h)
    316 
    317 std::unique_ptr<EventPacket> EventPacket::CreateCommandCompleteLeVendorCap(
    318     uint8_t status, const vector<uint8_t>& vendor_cap) {
    319   std::unique_ptr<EventPacket> evt_ptr =
    320       EventPacket::CreateCommandCompleteOnlyStatusEvent(HCI_BLE_VENDOR_CAP_OCF,
    321                                                         status);
    322 
    323   CHECK(evt_ptr->AddPayloadOctets(vendor_cap.size(), vendor_cap));
    324 
    325   return evt_ptr;
    326 }
    327 
    328 }  // namespace test_vendor_lib
    329