1 /*
2  * Copyright 2020 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 "hci/facade/controller_facade.h"
18 
19 #include <condition_variable>
20 #include <memory>
21 #include <mutex>
22 
23 #include "blueberry/facade/hci/controller_facade.grpc.pb.h"
24 #include "blueberry/facade/hci/controller_facade.pb.h"
25 #include "common/bind.h"
26 #include "common/blocking_queue.h"
27 #include "grpc/grpc_event_queue.h"
28 #include "hci/address.h"
29 #include "hci/controller.h"
30 
31 using ::grpc::ServerAsyncResponseWriter;
32 using ::grpc::ServerAsyncWriter;
33 using ::grpc::ServerContext;
34 
35 namespace bluetooth {
36 namespace hci {
37 namespace facade {
38 
39 using namespace blueberry::facade::hci;
40 using blueberry::facade::BluetoothAddress;
41 
42 class ControllerFacadeService : public ControllerFacade::Service {
43  public:
ControllerFacadeService(Controller * controller,::bluetooth::os::Handler *)44   ControllerFacadeService(Controller* controller, ::bluetooth::os::Handler*) : controller_(controller) {}
45 
GetMacAddress(::grpc::ServerContext *,const::google::protobuf::Empty *,BluetoothAddress * response)46   ::grpc::Status GetMacAddress(
47       ::grpc::ServerContext* /* context */,
48       const ::google::protobuf::Empty* /* request */,
49       BluetoothAddress* response) override {
50     Address local_address = controller_->GetMacAddress();
51     response->set_address(local_address.ToString());
52     return ::grpc::Status::OK;
53   }
54 
GetLocalName(::grpc::ServerContext *,const::google::protobuf::Empty *,NameMsg * response)55   ::grpc::Status GetLocalName(
56       ::grpc::ServerContext* /* context */,
57       const ::google::protobuf::Empty* /* request */,
58       NameMsg* response) override {
59     std::string local_name = controller_->GetLocalName();
60     response->set_name(local_name);
61     return ::grpc::Status::OK;
62   }
63 
WriteLocalName(::grpc::ServerContext *,const NameMsg * request,::google::protobuf::Empty *)64   ::grpc::Status WriteLocalName(
65       ::grpc::ServerContext* /* context */,
66       const NameMsg* request,
67       ::google::protobuf::Empty* /* response */) override {
68     controller_->WriteLocalName(request->name());
69     return ::grpc::Status::OK;
70   }
71 
IsSupportedCommand(::grpc::ServerContext *,const OpCodeMsg * request,SupportedMsg * response)72   ::grpc::Status IsSupportedCommand(
73       ::grpc::ServerContext* /* context */,
74       const OpCodeMsg* request,
75       SupportedMsg* response) override {
76     bool ret = controller_->IsSupported(static_cast<OpCode>(request->op_code()));
77     response->set_supported(ret);
78     return ::grpc::Status::OK;
79   }
80 
81 #define SUPPORTED_API(name)                           \
82   ::grpc::Status name(                                \
83       ::grpc::ServerContext* /* context */,           \
84       const ::google::protobuf::Empty* /* request */, \
85       SupportedMsg* response) override {              \
86     response->set_supported(controller_->name());     \
87     return ::grpc::Status::OK;                        \
88   }
89 
90   SUPPORTED_API(SupportsSimplePairing)
SUPPORTED_API(SupportsSecureConnections)91   SUPPORTED_API(SupportsSecureConnections)
92   SUPPORTED_API(SupportsSimultaneousLeBrEdr)
93   SUPPORTED_API(SupportsInterlacedInquiryScan)
94   SUPPORTED_API(SupportsRssiWithInquiryResults)
95   SUPPORTED_API(SupportsExtendedInquiryResponse)
96   SUPPORTED_API(SupportsRoleSwitch)
97   SUPPORTED_API(Supports3SlotPackets)
98   SUPPORTED_API(Supports5SlotPackets)
99   SUPPORTED_API(SupportsClassic2mPhy)
100   SUPPORTED_API(SupportsClassic3mPhy)
101   SUPPORTED_API(Supports3SlotEdrPackets)
102   SUPPORTED_API(Supports5SlotEdrPackets)
103   SUPPORTED_API(SupportsSco)
104   SUPPORTED_API(SupportsHv2Packets)
105   SUPPORTED_API(SupportsHv3Packets)
106   SUPPORTED_API(SupportsEv3Packets)
107   SUPPORTED_API(SupportsEv4Packets)
108   SUPPORTED_API(SupportsEv5Packets)
109   SUPPORTED_API(SupportsEsco2mPhy)
110   SUPPORTED_API(SupportsEsco3mPhy)
111   SUPPORTED_API(Supports3SlotEscoEdrPackets)
112   SUPPORTED_API(SupportsHoldMode)
113   SUPPORTED_API(SupportsSniffMode)
114   SUPPORTED_API(SupportsParkMode)
115   SUPPORTED_API(SupportsNonFlushablePb)
116   SUPPORTED_API(SupportsSniffSubrating)
117   SUPPORTED_API(SupportsEncryptionPause)
118   SUPPORTED_API(SupportsBle)
119   SUPPORTED_API(SupportsBleEncryption)
120   SUPPORTED_API(SupportsBleConnectionParametersRequest)
121   SUPPORTED_API(SupportsBleExtendedReject)
122   SUPPORTED_API(SupportsBlePeripheralInitiatedFeaturesExchange)
123   SUPPORTED_API(SupportsBlePing)
124   SUPPORTED_API(SupportsBleDataPacketLengthExtension)
125   SUPPORTED_API(SupportsBlePrivacy)
126   SUPPORTED_API(SupportsBleExtendedScannerFilterPolicies)
127   SUPPORTED_API(SupportsBle2mPhy)
128   SUPPORTED_API(SupportsBleStableModulationIndexTx)
129   SUPPORTED_API(SupportsBleStableModulationIndexRx)
130   SUPPORTED_API(SupportsBleCodedPhy)
131   SUPPORTED_API(SupportsBleExtendedAdvertising)
132   SUPPORTED_API(SupportsBlePeriodicAdvertising)
133   SUPPORTED_API(SupportsBleChannelSelectionAlgorithm2)
134   SUPPORTED_API(SupportsBlePowerClass1)
135   SUPPORTED_API(SupportsBleMinimumUsedChannels)
136   SUPPORTED_API(SupportsBleConnectionCteRequest)
137   SUPPORTED_API(SupportsBleConnectionCteResponse)
138   SUPPORTED_API(SupportsBleConnectionlessCteTransmitter)
139   SUPPORTED_API(SupportsBleConnectionlessCteReceiver)
140   SUPPORTED_API(SupportsBleAntennaSwitchingDuringCteTx)
141   SUPPORTED_API(SupportsBleAntennaSwitchingDuringCteRx)
142   SUPPORTED_API(SupportsBleReceivingConstantToneExtensions)
143   SUPPORTED_API(SupportsBlePeriodicAdvertisingSyncTransferSender)
144   SUPPORTED_API(SupportsBlePeriodicAdvertisingSyncTransferRecipient)
145   SUPPORTED_API(SupportsBleSleepClockAccuracyUpdates)
146   SUPPORTED_API(SupportsBleRemotePublicKeyValidation)
147   SUPPORTED_API(SupportsBleConnectedIsochronousStreamCentral)
148   SUPPORTED_API(SupportsBleConnectedIsochronousStreamPeripheral)
149   SUPPORTED_API(SupportsBleIsochronousBroadcaster)
150   SUPPORTED_API(SupportsBleSynchronizedReceiver)
151   SUPPORTED_API(SupportsBleIsochronousChannelsHostSupport)
152   SUPPORTED_API(SupportsBlePowerControlRequest)
153   SUPPORTED_API(SupportsBlePowerChangeIndication)
154   SUPPORTED_API(SupportsBlePathLossMonitoring)
155   SUPPORTED_API(SupportsBlePeriodicAdvertisingAdi)
156 
157   ::grpc::Status GetLeNumberOfSupportedAdvertisingSets(
158       ::grpc::ServerContext* /* context */,
159       const ::google::protobuf::Empty* /* request */,
160       SingleValueMsg* response) override {
161     uint8_t ret = controller_->GetLeNumberOfSupportedAdverisingSets();
162     response->set_value(ret);
163     return ::grpc::Status::OK;
164   }
165 
166  private:
167   Controller* controller_;
168 };
169 
ListDependencies(ModuleList * list) const170 void ControllerFacadeModule::ListDependencies(ModuleList* list) const {
171   ::bluetooth::grpc::GrpcFacadeModule::ListDependencies(list);
172   list->add<Controller>();
173 }
174 
Start()175 void ControllerFacadeModule::Start() {
176   ::bluetooth::grpc::GrpcFacadeModule::Start();
177   service_ = new ControllerFacadeService(GetDependency<Controller>(), GetHandler());
178 }
179 
Stop()180 void ControllerFacadeModule::Stop() {
181   delete service_;
182   ::bluetooth::grpc::GrpcFacadeModule::Stop();
183 }
184 
GetService() const185 ::grpc::Service* ControllerFacadeModule::GetService() const {
186   return service_;
187 }
188 
189 const ModuleFactory ControllerFacadeModule::Factory =
__anon2e5c8ef90102() 190     ::bluetooth::ModuleFactory([]() { return new ControllerFacadeModule(); });
191 
192 }  // namespace facade
193 }  // namespace hci
194 }  // namespace bluetooth
195