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 "l2cap/classic/facade.h"
36 #include "l2cap/le/facade.h"
37 #include "neighbor/connectability.h"
38 #include "neighbor/discoverability.h"
39 #include "neighbor/facade/facade.h"
40 #include "neighbor/inquiry.h"
41 #include "neighbor/name.h"
42 #include "neighbor/page.h"
43 #include "os/log.h"
44 #include "os/thread.h"
45 #include "security/facade.h"
46 #include "security/security_module.h"
47 #include "shim/dumpsys.h"
48 #include "shim/facade/facade.h"
49 #include "shim/l2cap.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::HciLayerFacadeModule>();
83 break;
84 case BluetoothModule::HCI_INTERFACES:
85 modules.add<::bluetooth::facade::ReadOnlyPropertyServerModule>();
86 modules.add<::bluetooth::hci::facade::HciLayerFacadeModule>();
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 break;
95 case BluetoothModule::L2CAP:
96 modules.add<::bluetooth::hci::facade::ControllerFacadeModule>();
97 modules.add<::bluetooth::hci::facade::LeAdvertisingManagerFacadeModule>();
98 modules.add<::bluetooth::hci::facade::LeInitiatorAddressFacadeModule>();
99 modules.add<::bluetooth::neighbor::facade::NeighborFacadeModule>();
100 modules.add<::bluetooth::facade::ReadOnlyPropertyServerModule>();
101 modules.add<::bluetooth::l2cap::classic::L2capClassicModuleFacadeModule>();
102 modules.add<::bluetooth::l2cap::le::L2capLeModuleFacadeModule>();
103 modules.add<::bluetooth::hci::facade::HciLayerFacadeModule>();
104 break;
105 case BluetoothModule::SECURITY:
106 modules.add<::bluetooth::facade::ReadOnlyPropertyServerModule>();
107 modules.add<::bluetooth::hci::facade::ControllerFacadeModule>();
108 modules.add<::bluetooth::security::SecurityModuleFacadeModule>();
109 modules.add<::bluetooth::neighbor::facade::NeighborFacadeModule>();
110 modules.add<::bluetooth::l2cap::classic::L2capClassicModuleFacadeModule>();
111 modules.add<::bluetooth::hci::facade::HciLayerFacadeModule>();
112 modules.add<::bluetooth::hci::facade::ControllerFacadeModule>();
113 modules.add<::bluetooth::hci::facade::LeAdvertisingManagerFacadeModule>();
114 modules.add<::bluetooth::hci::facade::LeScanningManagerFacadeModule>();
115 break;
116 case BluetoothModule::SHIM:
117 modules.add<::bluetooth::shim::facade::ShimFacadeModule>();
118 modules.add<::bluetooth::shim::L2cap>();
119 break;
120 default:
121 return ::grpc::Status(::grpc::StatusCode::INVALID_ARGUMENT, "invalid module under test");
122 }
123
124 stack_thread_ = new Thread("stack_thread", Thread::Priority::NORMAL);
125 stack_manager_.StartUp(&modules, stack_thread_);
126
127 GrpcModule* grpc_module = stack_manager_.GetInstance<GrpcModule>();
128 grpc_module->StartServer("0.0.0.0", grpc_port_);
129
130 grpc_loop_thread_ = new std::thread([grpc_module] { grpc_module->RunGrpcLoop(); });
131 is_running_ = true;
132
133 return ::grpc::Status::OK;
134 }
135
StopStack(::grpc::ServerContext * context,const::bluetooth::facade::StopStackRequest * request,::bluetooth::facade::StopStackResponse * response)136 ::grpc::Status StopStack(
137 ::grpc::ServerContext* context,
138 const ::bluetooth::facade::StopStackRequest* request,
139 ::bluetooth::facade::StopStackResponse* response) override {
140 if (!is_running_) {
141 return ::grpc::Status(::grpc::StatusCode::INVALID_ARGUMENT, "stack is not running");
142 }
143
144 stack_manager_.GetInstance<GrpcModule>()->StopServer();
145 grpc_loop_thread_->join();
146 delete grpc_loop_thread_;
147
148 stack_manager_.ShutDown();
149 delete stack_thread_;
150 is_running_ = false;
151 return ::grpc::Status::OK;
152 }
153
154 private:
155 Thread* stack_thread_ = nullptr;
156 bool is_running_ = false;
157 std::thread* grpc_loop_thread_ = nullptr;
158 StackManager stack_manager_;
159 int grpc_port_ = 8898;
160 };
161
162 RootFacadeService* root_facade_service;
163 } // namespace
164
StartServer(const std::string & address,int grpc_root_server_port,int grpc_port)165 void GrpcRootServer::StartServer(const std::string& address, int grpc_root_server_port, int grpc_port) {
166 ASSERT(!started_);
167 started_ = true;
168
169 std::string listening_port = address + ":" + std::to_string(grpc_root_server_port);
170 ::grpc::ServerBuilder builder;
171 root_facade_service = new RootFacadeService(grpc_port);
172 builder.RegisterService(root_facade_service);
173 builder.AddListeningPort(listening_port, ::grpc::InsecureServerCredentials());
174 server_ = builder.BuildAndStart();
175
176 ASSERT(server_ != nullptr);
177 }
178
StopServer()179 void GrpcRootServer::StopServer() {
180 ASSERT(started_);
181 server_->Shutdown();
182 started_ = false;
183 server_.reset();
184 delete root_facade_service;
185 }
186
RunGrpcLoop()187 void GrpcRootServer::RunGrpcLoop() {
188 ASSERT(started_);
189 server_->Wait();
190 }
191
192 } // namespace facade
193 } // namespace bluetooth
194