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

Commit 1267f5c5 authored by Hansong Zhang's avatar Hansong Zhang
Browse files

Add grpc root service for test configuration

When facade_main and cert_main is started, it only starts grpc root
service, and listens for StartStack() request before brining up required
modules. Then, it listens for StopStack() request before cleaning up
modules. Defining new rootservice.proto for facade and cert.

Test: cert/run_cert.sh
Change-Id: I10fa745b9a235080be7a0c82fe7f3a297c1a112d
parent 38d66ee9
Loading
Loading
Loading
Loading
+16 −2
Original line number Diff line number Diff line
@@ -120,6 +120,7 @@ cc_binary {
    host_supported: true,
    srcs: [
        "facade/facade_main.cc",
        "facade/grpc_root_server.cc",
        "grpc/grpc_module.cc",
        ":BluetoothFacade_hci_hal",
    ],
@@ -161,6 +162,7 @@ cc_binary {
    host_supported: true,
    srcs: [
        "cert/cert_main.cc",
        "cert/grpc_root_server.cc",
        "grpc/grpc_module.cc",
        ":BluetoothCertSource_hci_hal",
    ],
@@ -299,6 +301,7 @@ filegroup {
    name: "BluetoothFacadeProto",
    srcs: [
        "facade/common.proto",
        "facade/rootservice.proto",
        "hal/facade.proto",
    ],
}
@@ -316,6 +319,8 @@ genrule {
    out: [
        "facade/common.grpc.pb.h",
        "facade/common.pb.h",
        "facade/rootservice.grpc.pb.h",
        "facade/rootservice.pb.h",
        "hal/facade.grpc.pb.h",
        "hal/facade.pb.h",
    ],
@@ -334,6 +339,8 @@ genrule {
    out: [
        "facade/common.grpc.pb.cc",
        "facade/common.pb.cc",
        "facade/rootservice.grpc.pb.cc",
        "facade/rootservice.pb.cc",
        "hal/facade.grpc.pb.cc",
        "hal/facade.pb.cc",
    ],
@@ -346,7 +353,6 @@ genrule {
        "protoc-gen-grpc-python-plugin",
    ],
    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/cert/__init__.py; ",
@@ -355,10 +361,13 @@ genrule {
        ":BluetoothCertStackProto",
    ],
    out: [
        "__init__.py",
        "cert/rootservice_pb2_grpc.py",
        "cert/rootservice_pb2.py",
        "facade/__init__.py",
        "facade/common_pb2_grpc.py",
        "facade/common_pb2.py",
        "facade/rootservice_pb2_grpc.py",
        "facade/rootservice_pb2.py",
        "hal/__init__.py",
        "hal/facade_pb2_grpc.py",
        "hal/facade_pb2.py",
@@ -371,6 +380,7 @@ genrule {
filegroup {
    name: "BluetoothCertStackProto",
    srcs: [
        "cert/rootservice.proto",
        "hal/cert/api.proto",
    ],
}
@@ -387,6 +397,8 @@ genrule {
        ":BluetoothFacadeProto",  // we need to use facade/common.proto
    ],
    out: [
        "cert/rootservice.grpc.pb.h",
        "cert/rootservice.pb.h",
        "facade/common.grpc.pb.h",
        "facade/common.pb.h",
        "hal/cert/api.grpc.pb.h",
@@ -406,6 +418,8 @@ genrule {
        ":BluetoothFacadeProto",  // we need to use facade/common.proto
    ],
    out: [
        "cert/rootservice.grpc.pb.cc",
        "cert/rootservice.pb.cc",
        "facade/common.grpc.pb.cc",
        "facade/common.pb.cc",
        "hal/cert/api.grpc.pb.cc",
+18 −27
Original line number Diff line number Diff line
@@ -17,14 +17,15 @@
#include "stack_manager.h"

#include <csignal>
#include <string>
#include <thread>

#include "cert/grpc_root_server.h"
#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;
@@ -33,25 +34,31 @@ using ::bluetooth::grpc::GrpcModule;
using ::bluetooth::os::Thread;

namespace {
StackManager* stack;
::bluetooth::cert::GrpcRootServer grpc_root_server;

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

int main(int argc, const char** argv) {
  int port = 8898;
  int root_server_port = 8897;
  int grpc_port = 8899;

  const std::string arg_grpc_port = "--port=";
  const std::string arg_grpc_root_server_port = "--root-server-port=";
  const std::string arg_grpc_server_port = "--grpc-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_grpc_root_server_port) == 0) {
      auto port_number = arg.substr(arg_grpc_root_server_port.size());
      root_server_port = std::stoi(port_number);
    }
    if (arg.find(arg_grpc_server_port) == 0) {
      auto port_number = arg.substr(arg_grpc_server_port.size());
      grpc_port = std::stoi(port_number);
    }
    if (arg.find(arg_rootcanal_port) == 0) {
      auto port_number = arg.substr(arg_rootcanal_port.size());
@@ -63,26 +70,10 @@ int main(int argc, const char** argv) {
    }
  }

  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(); });
  grpc_root_server.StartServer("0.0.0.0", root_server_port, grpc_port);
  auto wait_thread = std::thread([] { grpc_root_server.RunGrpcLoop(); });
  wait_thread.join();

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

  return 0;
}
+5 −3
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
from gd_device_base import GdDeviceBase
from gd_device_base import replace_vars

from cert import rootservice_pb2_grpc as cert_rootservice_pb2_grpc
from hal.cert import api_pb2_grpc as hal_cert_pb2_grpc

ACTS_CONTROLLER_CONFIG_NAME = "GdCertDevice"
@@ -49,11 +50,12 @@ def get_instances_with_configs(configs):
        resolved_cmd = []
        for entry in config["cmd"]:
            resolved_cmd.append(replace_vars(entry, config))
        devices.append(GdCertDevice(config["grpc_port"], resolved_cmd, config["label"]))
        devices.append(GdCertDevice(config["grpc_port"], config["grpc_root_server_port"], resolved_cmd, config["label"]))
    return devices

class GdCertDevice(GdDeviceBase):
    def __init__(self, grpc_port, cmd, label):
        super().__init__(grpc_port, cmd, label, ACTS_CONTROLLER_CONFIG_NAME)
    def __init__(self, grpc_port, grpc_root_server_port, cmd, label):
        super().__init__(grpc_port, grpc_root_server_port, cmd, label, ACTS_CONTROLLER_CONFIG_NAME)
        self.rootservice = cert_rootservice_pb2_grpc.RootCertStub(self.grpc_root_server_channel)
        self.hal = hal_cert_pb2_grpc.HciHalCertStub(self.grpc_channel)
+5 −3
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ from gd_device_base import GdDeviceBase
from gd_device_base import replace_vars

from cert.event_stream import EventStream
from facade import rootservice_pb2_grpc as facade_rootservice_pb2_grpc
from hal import facade_pb2_grpc as hal_facade_pb2_grpc

ACTS_CONTROLLER_CONFIG_NAME = "GdDevice"
@@ -50,12 +51,13 @@ def get_instances_with_configs(configs):
        resolved_cmd = []
        for entry in config["cmd"]:
            resolved_cmd.append(replace_vars(entry, config))
        devices.append(GdDevice(config["grpc_port"], resolved_cmd, config["label"]))
        devices.append(GdDevice(config["grpc_port"], config["grpc_root_server_port"], resolved_cmd, config["label"]))
    return devices

class GdDevice(GdDeviceBase):
    def __init__(self, grpc_port, cmd, label):
        super().__init__(grpc_port, cmd, label, ACTS_CONTROLLER_CONFIG_NAME)
    def __init__(self, grpc_port, grpc_root_server_port, cmd, label):
        super().__init__(grpc_port, grpc_root_server_port, cmd, label, ACTS_CONTROLLER_CONFIG_NAME)
        self.rootservice = facade_rootservice_pb2_grpc.RootFacadeStub(self.grpc_root_server_channel)
        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)
+8 −2
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ import os
from builtins import open
import signal
import subprocess
import time

from acts import error
from acts import tracelogger
@@ -31,10 +32,11 @@ ANDROID_HOST_OUT = os.environ.get('ANDROID_HOST_OUT')
def replace_vars(string, config):
    return string.replace("$ANDROID_HOST_OUT", ANDROID_HOST_OUT) \
                 .replace("$(grpc_port)", config.get("grpc_port")) \
                 .replace("$(grpc_root_server_port)", config.get("grpc_root_server_port")) \
                 .replace("$(rootcanal_port)", config.get("rootcanal_port"))

class GdDeviceBase:
    def __init__(self, grpc_port, cmd, label, type_identifier):
    def __init__(self, grpc_port, grpc_root_server_port, cmd, label, type_identifier):
        self.label = label if label is not None else grpc_port
        # logging.log_path only exists when this is used in an ACTS test run.
        log_path_base = getattr(logging, 'log_path', '/tmp/logs')
@@ -56,11 +58,15 @@ class GdDeviceBase:
            env=os.environ.copy(),
            stdout=self.backing_process_logs,
            stderr=self.backing_process_logs)

        time.sleep(1)  # We need some time for backing_process to start server
        # TODO: pass a fd that can be signaled when the process is ready
        self.grpc_root_server_channel = grpc.insecure_channel("localhost:" + grpc_root_server_port)
        self.grpc_port = int(grpc_port)
        self.grpc_channel = grpc.insecure_channel("localhost:" + grpc_port)

    def clean_up(self):
        self.grpc_channel.close()
        self.grpc_root_server_channel.close()
        self.backing_process.send_signal(signal.SIGINT)
        backing_process_return_code = self.backing_process.wait()
        self.backing_process_logs.close()
Loading