#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 {
::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);
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);
}
#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;
::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);
}
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);
}
::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);
}
#include "hal/hci_hal_host_rootcanal.h"
#include "hal/hci_hal.h"
+#include "hal/serialize_packet.h"
#include <fcntl.h>
#include <netdb.h>
#include "os/log.h"
#include "os/thread.h"
#include "os/utils.h"
+#include "packet/raw_builder.h"
using ::bluetooth::os::Thread;
}
}
+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
--- /dev/null
+/*
+ * 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