OSDN Git Service

GD: HAL: Factor out packet serialization
authorMyles Watson <mylesgw@google.com>
Wed, 4 Sep 2019 22:37:23 +0000 (15:37 -0700)
committerMyles Watson <mylesgw@google.com>
Fri, 6 Sep 2019 17:55:25 +0000 (10:55 -0700)
Test: ./cert/run_cert.sh
Change-Id: I640a91462bb728252218f0b4c8109e1a0e10f510

gd/hal/cert/cert.cc
gd/hal/facade.cc
gd/hal/hci_hal_host_rootcanal_test.cc
gd/hal/serialize_packet.h [new file with mode: 0644]

index a50b1f7..12240d3 100644 (file)
@@ -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);
     }
index 7e486ac..329865c 100644 (file)
@@ -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);
     }
index e06c803..9783e84 100644 (file)
@@ -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
diff --git a/gd/hal/serialize_packet.h b/gd/hal/serialize_packet.h
new file mode 100644 (file)
index 0000000..621dcec
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+ * 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