Loading system/gd/hal/cert/cert.cc +3 −11 Original line number Diff line number Diff line Loading @@ -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 { Loading @@ -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); Loading Loading @@ -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); } Loading system/gd/hal/facade.cc +7 −17 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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); } Loading @@ -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); } Loading @@ -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); } Loading system/gd/hal/hci_hal_host_rootcanal_test.cc +7 −0 Original line number Diff line number Diff line Loading @@ -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> Loading @@ -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; Loading Loading @@ -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 system/gd/hal/serialize_packet.h 0 → 100644 +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 Loading
system/gd/hal/cert/cert.cc +3 −11 Original line number Diff line number Diff line Loading @@ -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 { Loading @@ -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); Loading Loading @@ -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); } Loading
system/gd/hal/facade.cc +7 −17 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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); } Loading @@ -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); } Loading @@ -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); } Loading
system/gd/hal/hci_hal_host_rootcanal_test.cc +7 −0 Original line number Diff line number Diff line Loading @@ -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> Loading @@ -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; Loading Loading @@ -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
system/gd/hal/serialize_packet.h 0 → 100644 +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