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 "hci/facade/le_scanning_manager_facade.h"
18
19 #include <bluetooth/log.h>
20
21 #include <cstdint>
22 #include <unordered_map>
23 #include <utility>
24
25 #include "blueberry/facade/hci/le_scanning_manager_facade.grpc.pb.h"
26 #include "blueberry/facade/hci/le_scanning_manager_facade.pb.h"
27 #include "common/bidi_queue.h"
28 #include "common/bind.h"
29 #include "grpc/grpc_event_queue.h"
30 #include "hci/le_scanning_manager.h"
31 #include "os/log.h"
32 #include "packet/raw_builder.h"
33
34 namespace bluetooth {
35 namespace hci {
36 namespace facade {
37
38 using ::grpc::ServerAsyncResponseWriter;
39 using ::grpc::ServerAsyncWriter;
40 using ::grpc::ServerContext;
41 using ::grpc::ServerWriter;
42 using ::grpc::Status;
43
44 using namespace blueberry::facade::hci;
45
46 class LeScanningManagerFacadeService : public LeScanningManagerFacade::Service, ScanningCallback {
47 public:
LeScanningManagerFacadeService(LeScanningManager * le_scanning_manager,os::Handler * facade_handler)48 LeScanningManagerFacadeService(LeScanningManager* le_scanning_manager, os::Handler* facade_handler)
49 : le_scanning_manager_(le_scanning_manager), facade_handler_(facade_handler) {
50 log::assert_that(
51 le_scanning_manager_ != nullptr, "assert failed: le_scanning_manager_ != nullptr");
52 log::assert_that(facade_handler_ != nullptr, "assert failed: facade_handler_ != nullptr");
53 le_scanning_manager_->RegisterScanningCallback(this);
54 }
55
RegisterScanner(::grpc::ServerContext *,const RegisterScannerRequest * request,::google::protobuf::Empty *)56 ::grpc::Status RegisterScanner(
57 ::grpc::ServerContext* /* context */,
58 const RegisterScannerRequest* request,
59 ::google::protobuf::Empty* /* response */) override {
60 uint32_t uuid_raw = request->uuid();
61 bluetooth::hci::Uuid uuid = bluetooth::hci::Uuid::From32Bit(uuid_raw);
62 le_scanning_manager_->RegisterScanner(uuid);
63 return ::grpc::Status::OK;
64 }
65
Unregister(::grpc::ServerContext *,const UnregisterRequest * request,::google::protobuf::Empty *)66 ::grpc::Status Unregister(
67 ::grpc::ServerContext* /* context */,
68 const UnregisterRequest* request,
69 ::google::protobuf::Empty* /* response */) override {
70 le_scanning_manager_->Unregister(request->scanner_id());
71 return ::grpc::Status::OK;
72 }
73
Scan(::grpc::ServerContext *,const ScanRequest * request,::google::protobuf::Empty *)74 ::grpc::Status Scan(
75 ::grpc::ServerContext* /* context */,
76 const ScanRequest* request,
77 ::google::protobuf::Empty* /* response */) override {
78 le_scanning_manager_->Scan(request->start());
79 return ::grpc::Status::OK;
80 }
81
SetScanParameters(::grpc::ServerContext *,const SetScanParametersRequest * request,::google::protobuf::Empty *)82 ::grpc::Status SetScanParameters(
83 ::grpc::ServerContext* /* context */,
84 const SetScanParametersRequest* request,
85 ::google::protobuf::Empty* /* response */) override {
86 auto scan_type = static_cast<hci::LeScanType>(request->scan_type());
87 le_scanning_manager_->SetScanParameters(
88 request->scanner_id(),
89 scan_type,
90 request->scan_interval(),
91 request->scan_window(),
92 request->scan_phy());
93 return ::grpc::Status::OK;
94 }
95
FetchCallbackEvents(::grpc::ServerContext * context,const::google::protobuf::Empty *,::grpc::ServerWriter<ScanningCallbackMsg> * writer)96 ::grpc::Status FetchCallbackEvents(
97 ::grpc::ServerContext* context,
98 const ::google::protobuf::Empty* /* request */,
99 ::grpc::ServerWriter<ScanningCallbackMsg>* writer) override {
100 return callback_events_.RunLoop(context, writer);
101 }
102
FetchAdvertisingReports(::grpc::ServerContext * context,const::google::protobuf::Empty *,::grpc::ServerWriter<AdvertisingReportMsg> * writer)103 ::grpc::Status FetchAdvertisingReports(
104 ::grpc::ServerContext* context,
105 const ::google::protobuf::Empty* /* request */,
106 ::grpc::ServerWriter<AdvertisingReportMsg>* writer) override {
107 return advertising_reports_.RunLoop(context, writer);
108 }
109
OnScannerRegistered(const bluetooth::hci::Uuid app_uuid,ScannerId,ScanningStatus status)110 void OnScannerRegistered(
111 const bluetooth::hci::Uuid app_uuid, ScannerId /* scanner_id */, ScanningStatus status) {
112 ScanningCallbackMsg msg;
113 msg.set_message_type(ScanningCallbackMsgType::SCANNER_REGISTERED);
114 msg.set_status(static_cast<facade::ScanningStatus>(status));
115 msg.set_data(app_uuid.As32Bit());
116 callback_events_.OnIncomingEvent(msg);
117 };
118
OnSetScannerParameterComplete(ScannerId scanner_id,ScanningStatus status)119 void OnSetScannerParameterComplete(ScannerId scanner_id, ScanningStatus status) {
120 ScanningCallbackMsg msg;
121 msg.set_message_type(ScanningCallbackMsgType::SET_SCANNER_PARAMETER_COMPLETE);
122 msg.set_status(static_cast<facade::ScanningStatus>(status));
123 msg.set_data(static_cast<uint32_t>(scanner_id));
124 callback_events_.OnIncomingEvent(msg);
125 };
126
OnScanResult(uint16_t,uint8_t address_type,Address address,uint8_t,uint8_t,uint8_t,int8_t,int8_t rssi,uint16_t,std::vector<uint8_t> advertising_data)127 void OnScanResult(
128 uint16_t /* event_type */,
129 uint8_t address_type,
130 Address address,
131 uint8_t /* primary_phy */,
132 uint8_t /* secondary_phy */,
133 uint8_t /* advertising_sid */,
134 int8_t /* tx_power */,
135 int8_t rssi,
136 uint16_t /* periodic_advertising_interval */,
137 std::vector<uint8_t> advertising_data) {
138 AdvertisingReportMsg advertising_report_msg;
139 std::vector<LeExtendedAdvertisingResponseRaw> advertisements;
140 LeExtendedAdvertisingResponseRaw le_extended_advertising_report;
141 le_extended_advertising_report.address_type_ = (DirectAdvertisingAddressType)address_type;
142 le_extended_advertising_report.address_ = address;
143 le_extended_advertising_report.advertising_data_ = advertising_data;
144 le_extended_advertising_report.rssi_ = rssi;
145 advertisements.push_back(le_extended_advertising_report);
146
147 auto builder = LeExtendedAdvertisingReportRawBuilder::Create(advertisements);
148 std::vector<uint8_t> bytes;
149 BitInserter bit_inserter(bytes);
150 builder->Serialize(bit_inserter);
151 advertising_report_msg.set_event(std::string(bytes.begin(), bytes.end()));
152 advertising_reports_.OnIncomingEvent(std::move(advertising_report_msg));
153 };
OnTrackAdvFoundLost(AdvertisingFilterOnFoundOnLostInfo)154 void OnTrackAdvFoundLost(AdvertisingFilterOnFoundOnLostInfo /* on_found_on_lost_info */){};
OnBatchScanReports(int,int,int,int,std::vector<uint8_t>)155 void OnBatchScanReports(
156 int /* client_if */,
157 int /* status */,
158 int /* report_format */,
159 int /* num_records */,
160 std::vector<uint8_t> /* data */){};
OnBatchScanThresholdCrossed(int)161 void OnBatchScanThresholdCrossed(int /* client_if */){};
OnTimeout()162 void OnTimeout(){};
OnFilterEnable(Enable,uint8_t)163 void OnFilterEnable(Enable /* enable */, uint8_t /* status */){};
OnFilterParamSetup(uint8_t,ApcfAction,uint8_t)164 void OnFilterParamSetup(
165 uint8_t /* available_spaces */, ApcfAction /* action */, uint8_t /* status */){};
OnFilterConfigCallback(ApcfFilterType,uint8_t,ApcfAction,uint8_t)166 void OnFilterConfigCallback(
167 ApcfFilterType /* filter_type */,
168 uint8_t /* available_spaces */,
169 ApcfAction /* action */,
170 uint8_t /* status */){};
171
OnPeriodicSyncStarted(int,uint8_t,uint16_t,uint8_t,AddressWithType,uint8_t,uint16_t)172 void OnPeriodicSyncStarted(
173 int /* reg_id */,
174 uint8_t /* status */,
175 uint16_t /* sync_handle */,
176 uint8_t /* advertising_sid */,
177 AddressWithType /* address_with_type */,
178 uint8_t /* phy */,
179 uint16_t /* interval */) override {
180 log::info("OnPeriodicSyncStarted in LeScanningManagerFacadeService");
181 };
182
OnPeriodicSyncReport(uint16_t,int8_t,int8_t,uint8_t,std::vector<uint8_t>)183 void OnPeriodicSyncReport(
184 uint16_t /* sync_handle */,
185 int8_t /* tx_power */,
186 int8_t /* rssi */,
187 uint8_t /* status */,
188 std::vector<uint8_t> /* data */) override {
189 log::info("OnPeriodicSyncReport in LeScanningManagerFacadeService");
190 };
191
OnPeriodicSyncLost(uint16_t)192 void OnPeriodicSyncLost(uint16_t /* sync_handle */) override {
193 log::info("OnPeriodicSyncLost in LeScanningManagerFacadeService");
194 };
195
OnPeriodicSyncTransferred(int,uint8_t,Address)196 void OnPeriodicSyncTransferred(
197 int /* pa_source */, uint8_t /* status */, Address /* address */) override {
198 log::info("OnPeriodicSyncTransferred in LeScanningManagerFacadeService");
199 };
200
OnBigInfoReport(uint16_t,bool)201 void OnBigInfoReport(uint16_t /* sync_handle */, bool /* encrypted */) override {
202 log::info("OnBigInfoReport in LeScanningManagerFacadeService");
203 };
204
205 LeScanningManager* le_scanning_manager_;
206 os::Handler* facade_handler_;
207 ::bluetooth::grpc::GrpcEventQueue<AdvertisingReportMsg> advertising_reports_{"advertising reports"};
208 ::bluetooth::grpc::GrpcEventQueue<ScanningCallbackMsg> callback_events_{"callback events"};
209 };
210
ListDependencies(ModuleList * list) const211 void LeScanningManagerFacadeModule::ListDependencies(ModuleList* list) const {
212 ::bluetooth::grpc::GrpcFacadeModule::ListDependencies(list);
213 list->add<hci::LeScanningManager>();
214 }
215
Start()216 void LeScanningManagerFacadeModule::Start() {
217 ::bluetooth::grpc::GrpcFacadeModule::Start();
218 service_ = new LeScanningManagerFacadeService(GetDependency<hci::LeScanningManager>(), GetHandler());
219 }
220
Stop()221 void LeScanningManagerFacadeModule::Stop() {
222 delete service_;
223 ::bluetooth::grpc::GrpcFacadeModule::Stop();
224 }
225
GetService() const226 ::grpc::Service* LeScanningManagerFacadeModule::GetService() const {
227 return service_;
228 }
229
230 const ModuleFactory LeScanningManagerFacadeModule::Factory =
__anon047fa6100102() 231 ::bluetooth::ModuleFactory([]() { return new LeScanningManagerFacadeModule(); });
232
233 } // namespace facade
234 } // namespace hci
235 } // namespace bluetooth
236