1 /*
2 * Copyright 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "facade/grpc_root_server.h"
18
19 #include <string>
20
21 #include "facade/read_only_property_server.h"
22 #include "facade/rootservice.grpc.pb.h"
23 #include "grpc/grpc_module.h"
24 #include "hal/facade.h"
25 #include "hci/facade/acl_manager_facade.h"
26 #include "hci/facade/controller_facade.h"
27 #include "hci/facade/facade.h"
28 #include "hci/facade/le_acl_manager_facade.h"
29 #include "hci/facade/le_advertising_manager_facade.h"
30 #include "hci/facade/le_initiator_address_facade.h"
31 #include "hci/facade/le_scanning_manager_facade.h"
32 #include "hci/hci_layer.h"
33 #include "hci/le_advertising_manager.h"
34 #include "hci/le_scanning_manager.h"
35 #include "iso/facade.h"
36 #include "l2cap/classic/facade.h"
37 #include "l2cap/le/facade.h"
38 #include "neighbor/connectability.h"
39 #include "neighbor/discoverability.h"
40 #include "neighbor/facade/facade.h"
41 #include "neighbor/inquiry.h"
42 #include "neighbor/name.h"
43 #include "neighbor/page.h"
44 #include "os/log.h"
45 #include "os/thread.h"
46 #include "security/facade.h"
47 #include "security/security_module.h"
48 #include "shim/dumpsys.h"
49 #include "shim/facade/facade.h"
50 #include "stack_manager.h"
51 #include "storage/storage_module.h"
52
53 namespace bluetooth {
54 namespace facade {
55
56 using ::bluetooth::grpc::GrpcModule;
57 using ::bluetooth::os::Thread;
58
59 namespace {
60 class RootFacadeService : public ::bluetooth::facade::RootFacade::Service {
61 public:
RootFacadeService(int grpc_port)62 RootFacadeService(int grpc_port) : grpc_port_(grpc_port) {}
63
StartStack(::grpc::ServerContext * context,const::bluetooth::facade::StartStackRequest * request,::bluetooth::facade::StartStackResponse * response)64 ::grpc::Status StartStack(
65 ::grpc::ServerContext* context,
66 const ::bluetooth::facade::StartStackRequest* request,
67 ::bluetooth::facade::StartStackResponse* response) override {
68 if (is_running_) {
69 return ::grpc::Status(::grpc::StatusCode::INVALID_ARGUMENT, "stack is running");
70 }
71
72 ModuleList modules;
73 modules.add<::bluetooth::grpc::GrpcModule>();
74
75 BluetoothModule module_under_test = request->module_under_test();
76 switch (module_under_test) {
77 case BluetoothModule::HAL:
78 modules.add<::bluetooth::hal::HciHalFacadeModule>();
79 break;
80 case BluetoothModule::HCI:
81 modules.add<::bluetooth::facade::ReadOnlyPropertyServerModule>();
82 modules.add<::bluetooth::hci::facade::HciFacadeModule>();
83 break;
84 case BluetoothModule::HCI_INTERFACES:
85 modules.add<::bluetooth::facade::ReadOnlyPropertyServerModule>();
86 modules.add<::bluetooth::hci::facade::HciFacadeModule>();
87 modules.add<::bluetooth::hci::facade::AclManagerFacadeModule>();
88 modules.add<::bluetooth::hci::facade::ControllerFacadeModule>();
89 modules.add<::bluetooth::hci::facade::LeAclManagerFacadeModule>();
90 modules.add<::bluetooth::hci::facade::LeAdvertisingManagerFacadeModule>();
91 modules.add<::bluetooth::hci::facade::LeInitiatorAddressFacadeModule>();
92 modules.add<::bluetooth::hci::facade::LeScanningManagerFacadeModule>();
93 modules.add<::bluetooth::neighbor::facade::NeighborFacadeModule>();
94 modules.add<::bluetooth::iso::IsoModuleFacadeModule>();
95 break;
96 case BluetoothModule::L2CAP:
97 modules.add<::bluetooth::hci::facade::ControllerFacadeModule>();
98 modules.add<::bluetooth::hci::facade::LeAdvertisingManagerFacadeModule>();
99 modules.add<::bluetooth::hci::facade::LeInitiatorAddressFacadeModule>();
100 modules.add<::bluetooth::neighbor::facade::NeighborFacadeModule>();
101 modules.add<::bluetooth::facade::ReadOnlyPropertyServerModule>();
102 modules.add<::bluetooth::l2cap::classic::L2capClassicModuleFacadeModule>();
103 modules.add<::bluetooth::l2cap::le::L2capLeModuleFacadeModule>();
104 modules.add<::bluetooth::hci::facade::HciFacadeModule>();
105 modules.add<::bluetooth::iso::IsoModuleFacadeModule>();
106 break;
107 case BluetoothModule::SECURITY:
108 modules.add<::bluetooth::facade::ReadOnlyPropertyServerModule>();
109 modules.add<::bluetooth::hci::facade::ControllerFacadeModule>();
110 modules.add<::bluetooth::security::SecurityModuleFacadeModule>();
111 modules.add<::bluetooth::neighbor::facade::NeighborFacadeModule>();
112 modules.add<::bluetooth::l2cap::classic::L2capClassicModuleFacadeModule>();
113 modules.add<::bluetooth::hci::facade::HciFacadeModule>();
114 modules.add<::bluetooth::hci::facade::ControllerFacadeModule>();
115 modules.add<::bluetooth::hci::facade::LeAdvertisingManagerFacadeModule>();
116 modules.add<::bluetooth::hci::facade::LeScanningManagerFacadeModule>();
117 break;
118 case BluetoothModule::SHIM:
119 modules.add<::bluetooth::shim::facade::ShimFacadeModule>();
120 break;
121 default:
122 return ::grpc::Status(::grpc::StatusCode::INVALID_ARGUMENT, "invalid module under test");
123 }
124
125 stack_thread_ = new Thread("stack_thread", Thread::Priority::NORMAL);
126 stack_manager_.StartUp(&modules, stack_thread_);
127
128 GrpcModule* grpc_module = stack_manager_.GetInstance<GrpcModule>();
129 grpc_module->StartServer("0.0.0.0", grpc_port_);
130
131 grpc_loop_thread_ = new std::thread([grpc_module] { grpc_module->RunGrpcLoop(); });
132 is_running_ = true;
133
134 return ::grpc::Status::OK;
135 }
136
StopStack(::grpc::ServerContext * context,const::bluetooth::facade::StopStackRequest * request,::bluetooth::facade::StopStackResponse * response)137 ::grpc::Status StopStack(
138 ::grpc::ServerContext* context,
139 const ::bluetooth::facade::StopStackRequest* request,
140 ::bluetooth::facade::StopStackResponse* response) override {
141 if (!is_running_) {
142 return ::grpc::Status(::grpc::StatusCode::INVALID_ARGUMENT, "stack is not running");
143 }
144
145 stack_manager_.GetInstance<GrpcModule>()->StopServer();
146 grpc_loop_thread_->join();
147 delete grpc_loop_thread_;
148
149 stack_manager_.ShutDown();
150 delete stack_thread_;
151 is_running_ = false;
152 return ::grpc::Status::OK;
153 }
154
155 private:
156 Thread* stack_thread_ = nullptr;
157 bool is_running_ = false;
158 std::thread* grpc_loop_thread_ = nullptr;
159 StackManager stack_manager_;
160 int grpc_port_ = 8898;
161 };
162
163 RootFacadeService* root_facade_service;
164 } // namespace
165
StartServer(const std::string & address,int grpc_root_server_port,int grpc_port)166 void GrpcRootServer::StartServer(const std::string& address, int grpc_root_server_port, int grpc_port) {
167 ASSERT(!started_);
168 started_ = true;
169
170 std::string listening_port = address + ":" + std::to_string(grpc_root_server_port);
171 ::grpc::ServerBuilder builder;
172 root_facade_service = new RootFacadeService(grpc_port);
173 builder.RegisterService(root_facade_service);
174 builder.AddListeningPort(listening_port, ::grpc::InsecureServerCredentials());
175 server_ = builder.BuildAndStart();
176
177 ASSERT(server_ != nullptr);
178 }
179
StopServer()180 void GrpcRootServer::StopServer() {
181 ASSERT(started_);
182 server_->Shutdown();
183 started_ = false;
184 server_.reset();
185 delete root_facade_service;
186 }
187
RunGrpcLoop()188 void GrpcRootServer::RunGrpcLoop() {
189 ASSERT(started_);
190 server_->Wait();
191 }
192
193 } // namespace facade
194 } // namespace bluetooth
195