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

Commit f5068954 authored by Hansong Zhang's avatar Hansong Zhang Committed by android-build-merger
Browse files

Cert stack bring up and test Inquiry with two stacks

am: abd9110f

Change-Id: I85e10a876b52922ca0ce558292b0f54d87752405
parents 3d2db192 abd9110f
Loading
Loading
Loading
Loading
+93 −2
Original line number Diff line number Diff line
@@ -153,6 +153,47 @@ cc_binary {
    },
}

cc_binary {
    name: "bluetooth_cert_stack",
    defaults: [
        "gd_defaults",
    ],
    host_supported: true,
    srcs: [
        "cert/stack_main.cc",
        "grpc/grpc_module.cc",
        ":BluetoothCertSource_hci_hal",
    ],
    generated_headers: [
        "BluetoothGeneratedPackets_h",
        "BluetoothCertStackGeneratedStub_h",
    ],
    generated_sources: [
        "BluetoothCertStackGeneratedStub_cc",
    ],
    static_libs: [
        "libbluetooth_gd",
    ],
    shared_libs: [
        "libgrpc++_unsecure",
        "libprotobuf-cpp-full",
    ],
    target: {
        android: {
            shared_libs: [
                "android.hardware.bluetooth@1.0",
                "libhwbinder",
                "libhidlbase",
                "libhidltransport",
                "libutils",
            ],
        },
    },
    sanitize: {
        address: true,
    },
}

cc_test {
    name: "bluetooth_test_gd",
    test_suites: ["device-tests"],
@@ -299,7 +340,7 @@ genrule {
}

genrule {
    name: "BluetoothFacadeGeneratedStub_py",
    name: "BluetoothFacadeAndCertGeneratedStub_py",
    tools: [
        "aprotoc",
        "protoc-gen-grpc-python-plugin",
@@ -307,9 +348,11 @@ genrule {
    cmd: "$(location aprotoc) -Ipackages/modules/Bluetooth/system/gd -Iexternal/protobuf/src --plugin=protoc-gen-grpc=$(location protoc-gen-grpc-python-plugin) $(in) --grpc_out=$(genDir) --python_out=$(genDir); " +
        "touch $(genDir)/__init__.py; " +
        "touch $(genDir)/facade/__init__.py; " +
        "touch $(genDir)/hal/__init__.py; ",
        "touch $(genDir)/hal/__init__.py; " +
        "touch $(genDir)/hal/cert/__init__.py; ",
    srcs: [
        ":BluetoothFacadeProto",
        ":BluetoothCertStackProto",
    ],
    out: [
        "__init__.py",
@@ -319,5 +362,53 @@ genrule {
        "hal/__init__.py",
        "hal/facade_pb2_grpc.py",
        "hal/facade_pb2.py",
        "hal/cert/__init__.py",
        "hal/cert/api_pb2_grpc.py",
        "hal/cert/api_pb2.py",
    ],
}

filegroup {
    name: "BluetoothCertStackProto",
    srcs: [
        "hal/cert/api.proto",
    ],
}

genrule {
    name: "BluetoothCertStackGeneratedStub_h",
    tools: [
        "aprotoc",
        "protoc-gen-grpc-cpp-plugin",
    ],
    cmd: "$(location aprotoc) -Ipackages/modules/Bluetooth/system/gd -Iexternal/protobuf/src --plugin=protoc-gen-grpc=$(location protoc-gen-grpc-cpp-plugin) $(in) --grpc_out=$(genDir) --cpp_out=$(genDir)",
    srcs: [
        ":BluetoothCertStackProto",
        ":BluetoothFacadeProto",  // we need to use facade/common.proto
    ],
    out: [
        "facade/common.grpc.pb.h",
        "facade/common.pb.h",
        "hal/cert/api.grpc.pb.h",
        "hal/cert/api.pb.h",
    ],
}

genrule {
    name: "BluetoothCertStackGeneratedStub_cc",
    tools: [
        "aprotoc",
        "protoc-gen-grpc-cpp-plugin",
    ],
    cmd: "$(location aprotoc) -Ipackages/modules/Bluetooth/system/gd -Iexternal/protobuf/src --plugin=protoc-gen-grpc=$(location protoc-gen-grpc-cpp-plugin) $(in) --grpc_out=$(genDir) --cpp_out=$(genDir)",
    srcs: [
        ":BluetoothCertStackProto",
        ":BluetoothFacadeProto",  // we need to use facade/common.proto
    ],
    out: [
        "facade/common.grpc.pb.cc",
        "facade/common.pb.cc",
        "hal/cert/api.grpc.pb.cc",
        "hal/cert/api.pb.cc",
    ],
}
+1 −1
Original line number Diff line number Diff line
@@ -25,7 +25,7 @@ import subprocess

ANDROID_BUILD_TOP = os.environ.get('ANDROID_BUILD_TOP')

sys.path.append(ANDROID_BUILD_TOP + '/out/soong/.intermediates/packages/modules/Bluetooth/system/gd/BluetoothFacadeGeneratedStub_py/gen')
sys.path.append(ANDROID_BUILD_TOP + '/out/soong/.intermediates/packages/modules/Bluetooth/system/gd/BluetoothFacadeAndCertGeneratedStub_py/gen')

ANDROID_HOST_OUT = os.environ.get('ANDROID_HOST_OUT')
ROOTCANAL = ANDROID_HOST_OUT + "/nativetest64/root-canal/root-canal"
+25 −9
Original line number Diff line number Diff line
@@ -35,8 +35,11 @@ from acts.libs.proc import job

import grpc

from hal import facade_pb2_grpc as hal_facade_pb2_grpc
from cert.event_stream import EventStream
from hal.cert import api_pb2 as hal_cert_pb2
from hal.cert import api_pb2_grpc as hal_cert_pb2_grpc
from hal import facade_pb2 as hal_facade_pb2
from hal import facade_pb2_grpc as hal_facade_pb2_grpc

ANDROID_BUILD_TOP = os.environ.get('ANDROID_BUILD_TOP')
ANDROID_HOST_OUT = os.environ.get('ANDROID_HOST_OUT')
@@ -79,7 +82,9 @@ def get_instances_with_configs(configs):
        resolved_cmd = []
        for entry in config["cmd"]:
            resolved_cmd.append(replace_vars(entry, config))

        if config["is_cert_device"] == "true":
            device = GdCertDevice(config["grpc_port"], resolved_cmd, config["label"])
        else:
            device = GdDevice(config["grpc_port"], resolved_cmd, config["label"])
        devices.append(device)
    return devices
@@ -89,7 +94,7 @@ def replace_vars(string, config):
                 .replace("$(grpc_port)", config.get("grpc_port")) \
                 .replace("$(rootcanal_port)", config.get("rootcanal_port"))

class GdDevice:
class GdDeviceBase:
    def __init__(self, grpc_port, cmd, label):
        print(cmd)
        self.label = label if label is not None else grpc_port
@@ -114,11 +119,6 @@ class GdDevice:
            stderr=self.backing_process_logs)

        self.grpc_channel = grpc.insecure_channel("localhost:" + grpc_port)
        self.hal = hal_facade_pb2_grpc.HciHalFacadeStub(self.grpc_channel)
        self.hal.hci_event_stream = EventStream(self.hal.FetchHciEvent)
        self.hal.hci_acl_stream = EventStream(self.hal.FetchHciAcl)
        self.hal.hci_sco_stream = EventStream(self.hal.FetchHciSco)


    def clean_up(self):
        self.grpc_channel.close()
@@ -130,6 +130,22 @@ class GdDevice:
                          backing_process_return_code)
            return False


class GdDevice(GdDeviceBase):
    def __init__(self, grpc_port, cmd, label):
        super().__init__(grpc_port, cmd, label)
        self.hal = hal_facade_pb2_grpc.HciHalFacadeStub(self.grpc_channel)
        self.hal.hci_event_stream = EventStream(self.hal.FetchHciEvent)
        self.hal.hci_acl_stream = EventStream(self.hal.FetchHciAcl)
        self.hal.hci_sco_stream = EventStream(self.hal.FetchHciSco)


class GdCertDevice(GdDeviceBase):
    def __init__(self, grpc_port, cmd, label):
        super().__init__(grpc_port, cmd, label)
        self.hal = hal_cert_pb2_grpc.HciHalCertStub(self.grpc_channel)


class GdDeviceLoggerAdapter(logging.LoggerAdapter):
    def process(self, msg, kwargs):
        msg = "[GdDevice|%s] %s" % (self.extra["device"], msg)
+12 −0
Original line number Diff line number Diff line
@@ -15,12 +15,24 @@
                {
                    "grpc_port": "8899",
                    "label": "stack_under_test",
                    "is_cert_device": "false",
                    "cmd":
                    [
                        "$ANDROID_HOST_OUT/bin/stack_with_facade",
                        "--port=$(grpc_port)",
                        "--rootcanal-port=$(rootcanal_port)"
                    ]
                },
                {
                    "grpc_port": "8898",
                    "label": "cert_stack",
                    "is_cert_device": "true",
                    "cmd":
                    [
                        "$ANDROID_HOST_OUT/bin/bluetooth_cert_stack",
                        "--port=$(grpc_port)",
                        "--rootcanal-port=$(rootcanal_port)"
                    ]
                }
            ]
        }
+88 −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.
 */

#include "stack_manager.h"

#include <csignal>

#include "grpc/grpc_module.h"
#include "hal/cert/cert.h"
#include "hal/hci_hal.h"
#include "hal/hci_hal_host_rootcanal.h"
#include "hal/snoop_logger.h"
#include "module.h"
#include "os/thread.h"

using ::bluetooth::Module;
using ::bluetooth::ModuleList;
using ::bluetooth::StackManager;
using ::bluetooth::grpc::GrpcModule;
using ::bluetooth::os::Thread;

namespace {
StackManager* stack;

void interrupt_handler(int) {
  stack->GetInstance<GrpcModule>()->StopServer();
}
}  // namespace

int main(int argc, const char** argv) {
  int port = 8898;

  const std::string arg_grpc_port = "--port=";
  const std::string arg_rootcanal_port = "--rootcanal-port=";
  const std::string arg_btsnoop_path = "--btsnoop=";
  std::string btsnoop_path;
  for (int i = 1; i < argc; i++) {
    std::string arg = argv[i];
    if (arg.find(arg_grpc_port) == 0) {
      auto port_number = arg.substr(arg_grpc_port.size());
      port = std::stoi(port_number);
    }
    if (arg.find(arg_rootcanal_port) == 0) {
      auto port_number = arg.substr(arg_rootcanal_port.size());
      ::bluetooth::hal::HciHalHostRootcanalConfig::Get()->SetPort(std::stoi(port_number));
    }
    if (arg.find(arg_btsnoop_path) == 0) {
      btsnoop_path = arg.substr(arg_btsnoop_path.size());
      ::bluetooth::hal::SnoopLogger::SetFilePath(btsnoop_path);
    }
  }

  ModuleList modules;
  modules.add<::bluetooth::hal::HciHal>();
  modules.add<GrpcModule>();
  modules.add<::bluetooth::hal::cert::HalCertModule>();

  Thread* stack_thread = new Thread("cert_stack_thread", Thread::Priority::NORMAL);

  stack = new StackManager();
  stack->StartUp(&modules, stack_thread);

  GrpcModule* grpc_module = stack->GetInstance<GrpcModule>();
  grpc_module->StartServer("0.0.0.0", port);

  signal(SIGINT, interrupt_handler);
  auto wait_thread = std::thread([grpc_module] { grpc_module->RunGrpcLoop(); });
  wait_thread.join();

  stack->ShutDown();
  delete stack;
  delete stack_thread;

  return 0;
}
Loading