Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b7f51da9 authored by Myles Watson's avatar Myles Watson Committed by android-build-merger
Browse files

Merge "GD: HAL: Factor out packet serialization" am: 13402fe4 am: 7472fb22

am: c1491f47

Change-Id: I4b78dde41157530846ee1ee2a5e6d569ac041f9c
parents 56b6deeb c1491f47
Loading
Loading
Loading
Loading
+3 −11
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@
#include "grpc/grpc_event_stream.h"
#include "hal/cert/api.grpc.pb.h"
#include "hal/hci_hal.h"
#include "hal/serialize_packet.h"
#include "hci/hci_packets.h"

namespace bluetooth {
@@ -46,11 +47,7 @@ class HciHalCertService : public HciHalCert::Service, public ::bluetooth::hal::H
                                     ::google::protobuf::Empty* response) override {
    std::unique_lock<std::mutex> lock(mutex_);
    can_send_hci_command_ = false;
    auto packet = hci::ResetBuilder::Create();
    std::shared_ptr<std::vector<uint8_t>> packet_bytes = std::make_shared<std::vector<uint8_t>>();
    hci::BitInserter it(*packet_bytes);
    packet->Serialize(it);
    hal_->sendHciCommand(*packet_bytes);
    hal_->sendHciCommand(SerializePacket(hci::ResetBuilder::Create()));
    std::this_thread::sleep_for(std::chrono::milliseconds(300));
    while (!can_send_hci_command_) {
      cv_.wait(lock);
@@ -79,12 +76,7 @@ class HciHalCertService : public HciHalCert::Service, public ::bluetooth::hal::H
        break;
    }

    auto packet = hci::WriteScanEnableBuilder::Create(scan_enable);
    std::shared_ptr<std::vector<uint8_t>> packet_bytes = std::make_shared<std::vector<uint8_t>>();
    hci::BitInserter it(*packet_bytes);
    packet->Serialize(it);
    hal_->sendHciCommand(*packet_bytes);

    hal_->sendHciCommand(SerializePacket(hci::WriteScanEnableBuilder::Create(scan_enable)));
    while (!can_send_hci_command_) {
      cv_.wait(lock);
    }
+7 −17
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@
#include "grpc/grpc_event_stream.h"
#include "hal/facade.grpc.pb.h"
#include "hal/hci_hal.h"
#include "hal/serialize_packet.h"
#include "hci/hci_packets.h"

using ::grpc::ServerAsyncResponseWriter;
@@ -53,11 +54,7 @@ class HciHalFacadeService
                                     ::google::protobuf::Empty* response) override {
    std::unique_lock<std::mutex> lock(mutex_);
    can_send_hci_command_ = false;
    auto packet = hci::ResetBuilder::Create();
    std::shared_ptr<std::vector<uint8_t>> packet_bytes = std::make_shared<std::vector<uint8_t>>();
    hci::BitInserter it(*packet_bytes);
    packet->Serialize(it);
    hal_->sendHciCommand(*packet_bytes);
    hal_->sendHciCommand(SerializePacket(hci::ResetBuilder::Create()));
    while (!can_send_hci_command_) {
      cv_.wait(lock);
    }
@@ -70,12 +67,8 @@ class HciHalFacadeService
    std::unique_lock<std::mutex> lock(mutex_);
    can_send_hci_command_ = false;
    bool enable = request->enable();
    auto packet = hci::WriteLoopbackModeBuilder::Create(enable ? hci::LoopbackMode::ENABLE_LOCAL
                                                               : hci::LoopbackMode::NO_LOOPBACK);
    std::shared_ptr<std::vector<uint8_t>> packet_bytes = std::make_shared<std::vector<uint8_t>>();
    hci::BitInserter it(*packet_bytes);
    packet->Serialize(it);
    hal_->sendHciCommand(*packet_bytes);
    hal_->sendHciCommand(SerializePacket(hci::WriteLoopbackModeBuilder::Create(
        enable ? hci::LoopbackMode::ENABLE_LOCAL : hci::LoopbackMode::NO_LOOPBACK)));
    while (!can_send_hci_command_) {
      cv_.wait(lock);
    }
@@ -86,12 +79,9 @@ class HciHalFacadeService
                            ::google::protobuf::Empty* response) override {
    std::unique_lock<std::mutex> lock(mutex_);
    can_send_hci_command_ = false;
    auto packet = hci::InquiryBuilder::Create(0x33 /* LAP=0x9e8b33 */, static_cast<uint8_t>(request->length()),
                                              static_cast<uint8_t>(request->num_responses()));
    std::shared_ptr<std::vector<uint8_t>> packet_bytes = std::make_shared<std::vector<uint8_t>>();
    hci::BitInserter it(*packet_bytes);
    packet->Serialize(it);
    hal_->sendHciCommand(*packet_bytes);
    hal_->sendHciCommand(
        SerializePacket(hci::InquiryBuilder::Create(0x33 /* LAP=0x9e8b33 */, static_cast<uint8_t>(request->length()),
                                                    static_cast<uint8_t>(request->num_responses()))));
    while (!can_send_hci_command_) {
      cv_.wait(lock);
    }
+7 −0
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

#include "hal/hci_hal_host_rootcanal.h"
#include "hal/hci_hal.h"
#include "hal/serialize_packet.h"

#include <fcntl.h>
#include <netdb.h>
@@ -35,6 +36,7 @@
#include "os/log.h"
#include "os/thread.h"
#include "os/utils.h"
#include "packet/raw_builder.h"

using ::bluetooth::os::Thread;

@@ -377,6 +379,11 @@ TEST_F(HciHalRootcanalTest, send_multiple_acl_sequential) {
  }
}

TEST(HciHalHidlTest, serialize) {
  std::vector<uint8_t> bytes = {1, 2, 3, 4, 5, 6, 7, 8, 9};
  auto packet_bytes = hal::SerializePacket(std::unique_ptr<packet::BasePacketBuilder>(new packet::RawBuilder(bytes)));
  EXPECT_EQ(bytes, packet_bytes);
}
}  // namespace
}  // namespace hal
}  // namespace bluetooth
+35 −0
Original line number Diff line number Diff line
/*
 * Copyright 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include <memory>

#include "packet/base_packet_builder.h"

namespace bluetooth {
namespace hal {

inline std::vector<uint8_t> SerializePacket(std::unique_ptr<packet::BasePacketBuilder> packet) {
  std::vector<uint8_t> packet_bytes;
  packet_bytes.reserve(packet->size());
  packet::BitInserter it(packet_bytes);
  packet->Serialize(it);
  return packet_bytes;
}

}  // namespace hal
}  // namespace bluetooth