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 #define VECTOR_COPY_TO_END(source, destination) \
     20   std::copy(source.begin(), source.end(), std::back_inserter(destination));
     21 
     22 #include "vendor_libs/test_vendor_lib/include/event_packet.h"
     23 
     24 extern "C" {
     25 #include "osi/include/log.h"
     26 #include "stack/include/hcidefs.h"
     27 }  // extern "C"
     28 
     29 namespace test_vendor_lib {
     30 
     31 EventPacket::EventPacket(uint8_t event_code,
     32                          const std::vector<uint8_t>& payload)
     33     : Packet(DATA_TYPE_EVENT) {
     34   Encode({event_code, static_cast<uint8_t>(payload.size())}, payload);
     35 }
     36 
     37 uint8_t EventPacket::GetEventCode() const {
     38   return GetHeader()[0];
     39 }
     40 
     41 // static
     42 std::unique_ptr<EventPacket> EventPacket::CreateCommandCompleteEvent(
     43     uint8_t num_hci_command_packets, uint16_t command_opcode,
     44     const std::vector<uint8_t>& event_return_parameters) {
     45   size_t payload_size = sizeof(num_hci_command_packets) +
     46                         sizeof(command_opcode) + event_return_parameters.size();
     47 
     48   std::vector<uint8_t> payload;
     49   payload.reserve(payload_size);
     50   payload.push_back(num_hci_command_packets);
     51   payload.push_back(command_opcode);
     52   payload.push_back(command_opcode >> 8);
     53   VECTOR_COPY_TO_END(event_return_parameters, payload);
     54 
     55   return std::unique_ptr<EventPacket>(
     56       new EventPacket(HCI_COMMAND_COMPLETE_EVT, payload));
     57 }
     58 
     59 // static
     60 std::unique_ptr<EventPacket> EventPacket::CreateCommandStatusEvent(
     61     uint8_t status, uint8_t num_hci_command_packets, uint16_t command_opcode) {
     62   size_t payload_size =
     63       sizeof(status) + sizeof(num_hci_command_packets) + sizeof(command_opcode);
     64 
     65   std::vector<uint8_t> payload;
     66   payload.reserve(payload_size);
     67   payload.push_back(status);
     68   payload.push_back(num_hci_command_packets);
     69   payload.push_back(command_opcode);
     70   payload.push_back(command_opcode >> 8);
     71 
     72   return std::unique_ptr<EventPacket>(
     73       new EventPacket(HCI_COMMAND_STATUS_EVT, payload));
     74 }
     75 
     76 //static
     77 std::unique_ptr<EventPacket> EventPacket::CreateInquiryResultEvent(
     78     uint8_t num_responses, const std::vector<uint8_t>& bd_addresses,
     79     const std::vector<uint8_t>& page_scan_repetition_mode,
     80     const std::vector<uint8_t>& page_scan_period_mode,
     81     const std::vector<uint8_t>& page_scan_mode,
     82     const std::vector<uint8_t>& class_of_device,
     83     const std::vector<uint8_t>& clock_offset) {
     84   size_t payload_size = sizeof(num_responses) + bd_addresses.size() +
     85                         page_scan_repetition_mode.size() +
     86                         page_scan_period_mode.size() + page_scan_mode.size() +
     87                         class_of_device.size() + clock_offset.size();
     88 
     89   std::vector<uint8_t> payload;
     90   payload.reserve(payload_size);
     91   payload.push_back(num_responses);
     92   VECTOR_COPY_TO_END(bd_addresses, payload);
     93   VECTOR_COPY_TO_END(page_scan_repetition_mode, payload);
     94   VECTOR_COPY_TO_END(page_scan_mode, payload);
     95   VECTOR_COPY_TO_END(class_of_device, payload);
     96   VECTOR_COPY_TO_END(clock_offset, payload);
     97 
     98   return std::unique_ptr<EventPacket>(
     99       new EventPacket(HCI_INQUIRY_RESULT_EVT, payload));
    100 }
    101 
    102 //static
    103 std::unique_ptr<EventPacket> EventPacket::CreateExtendedInquiryResultEvent(
    104     const std::vector<uint8_t>& bd_address,
    105     const std::vector<uint8_t>& page_scan_repetition_mode,
    106     const std::vector<uint8_t>& page_scan_period_mode,
    107     const std::vector<uint8_t>& class_of_device,
    108     const std::vector<uint8_t>& clock_offset,
    109     const std::vector<uint8_t>& rssi,
    110     const std::vector<uint8_t>& extended_inquiry_response) {
    111   size_t payload_size =
    112       1 + bd_address.size() + page_scan_repetition_mode.size() +
    113       page_scan_period_mode.size() + class_of_device.size() +
    114       clock_offset.size() + rssi.size() + extended_inquiry_response.size();
    115 
    116   std::vector<uint8_t> payload;
    117   payload.reserve(payload_size);
    118   payload.push_back(1);  // Each extended inquiry result contains one device.
    119   VECTOR_COPY_TO_END(bd_address, payload);
    120   VECTOR_COPY_TO_END(page_scan_repetition_mode, payload);
    121   VECTOR_COPY_TO_END(page_scan_period_mode, payload);
    122   VECTOR_COPY_TO_END(class_of_device, payload);
    123   VECTOR_COPY_TO_END(clock_offset, payload);
    124   VECTOR_COPY_TO_END(rssi, payload);
    125   VECTOR_COPY_TO_END(extended_inquiry_response, payload);
    126 
    127   return std::unique_ptr<EventPacket>(
    128       new EventPacket(HCI_EXTENDED_INQUIRY_RESULT_EVT, payload));
    129 }
    130 
    131 }  // namespace test_vendor_lib
    132