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/le_scanning_manager.h"
18
19 #include <cstdint>
20 #include <unordered_map>
21 #include <utility>
22
23 #include "common/bidi_queue.h"
24 #include "common/bind.h"
25 #include "grpc/grpc_event_queue.h"
26 #include "hci/facade/le_scanning_manager_facade.grpc.pb.h"
27 #include "hci/facade/le_scanning_manager_facade.h"
28 #include "hci/facade/le_scanning_manager_facade.pb.h"
29 #include "os/log.h"
30 #include "packet/raw_builder.h"
31
32 namespace bluetooth {
33 namespace hci {
34 namespace facade {
35
36 using ::grpc::ServerAsyncResponseWriter;
37 using ::grpc::ServerAsyncWriter;
38 using ::grpc::ServerContext;
39 using ::grpc::ServerWriter;
40 using ::grpc::Status;
41
42 class LeScanningManagerFacadeService : public LeScanningManagerFacade::Service, ScanningCallback {
43 public:
LeScanningManagerFacadeService(LeScanningManager * le_scanning_manager,os::Handler * facade_handler)44 LeScanningManagerFacadeService(LeScanningManager* le_scanning_manager, os::Handler* facade_handler)
45 : le_scanning_manager_(le_scanning_manager), facade_handler_(facade_handler) {
46 ASSERT(le_scanning_manager_ != nullptr);
47 ASSERT(facade_handler_ != nullptr);
48 le_scanning_manager_->RegisterScanningCallback(this);
49 }
50
StartScan(::grpc::ServerContext * context,const::google::protobuf::Empty *,::grpc::ServerWriter<LeReportMsg> * writer)51 ::grpc::Status StartScan(::grpc::ServerContext* context, const ::google::protobuf::Empty*,
52 ::grpc::ServerWriter<LeReportMsg>* writer) override {
53 le_scanning_manager_->Scan(true);
54 return pending_events_.RunLoop(context, writer);
55 }
56
StopScan(::grpc::ServerContext * context,const::google::protobuf::Empty *,ScanStoppedMsg * response)57 ::grpc::Status StopScan(::grpc::ServerContext* context, const ::google::protobuf::Empty*,
58 ScanStoppedMsg* response) override {
59 le_scanning_manager_->Scan(false);
60 return ::grpc::Status::OK;
61 }
62
OnScannerRegistered(const bluetooth::hci::Uuid app_uuid,ScannerId scanner_id,ScanningStatus status)63 void OnScannerRegistered(const bluetooth::hci::Uuid app_uuid, ScannerId scanner_id, ScanningStatus status){};
OnScanResult(uint16_t event_type,uint8_t address_type,Address address,uint8_t primary_phy,uint8_t secondary_phy,uint8_t advertising_sid,int8_t tx_power,int8_t rssi,uint16_t periodic_advertising_interval,std::vector<uint8_t> advertising_data)64 void OnScanResult(
65 uint16_t event_type,
66 uint8_t address_type,
67 Address address,
68 uint8_t primary_phy,
69 uint8_t secondary_phy,
70 uint8_t advertising_sid,
71 int8_t tx_power,
72 int8_t rssi,
73 uint16_t periodic_advertising_interval,
74 std::vector<uint8_t> advertising_data) {
75 LeReportMsg le_report_msg;
76 std::vector<LeExtendedAdvertisingReport> advertisements;
77 LeExtendedAdvertisingReport le_extended_advertising_report;
78 le_extended_advertising_report.address_type_ = (DirectAdvertisingAddressType)address_type;
79 le_extended_advertising_report.address_ = address;
80 le_extended_advertising_report.advertising_data_ = advertising_data;
81 le_extended_advertising_report.rssi_ = rssi;
82 advertisements.push_back(le_extended_advertising_report);
83
84 auto builder = LeExtendedAdvertisingReportBuilder::Create(advertisements);
85 std::vector<uint8_t> bytes;
86 BitInserter bit_inserter(bytes);
87 builder->Serialize(bit_inserter);
88 le_report_msg.set_event(std::string(bytes.begin(), bytes.end()));
89 pending_events_.OnIncomingEvent(std::move(le_report_msg));
90 };
OnTrackAdvFoundLost(AdvertisingFilterOnFoundOnLostInfo on_found_on_lost_info)91 void OnTrackAdvFoundLost(AdvertisingFilterOnFoundOnLostInfo on_found_on_lost_info){};
OnBatchScanReports(int client_if,int status,int report_format,int num_records,std::vector<uint8_t> data)92 void OnBatchScanReports(int client_if, int status, int report_format, int num_records, std::vector<uint8_t> data){};
OnBatchScanThresholdCrossed(int client_if)93 void OnBatchScanThresholdCrossed(int client_if){};
OnTimeout()94 void OnTimeout(){};
OnFilterEnable(Enable enable,uint8_t status)95 void OnFilterEnable(Enable enable, uint8_t status){};
OnFilterParamSetup(uint8_t available_spaces,ApcfAction action,uint8_t status)96 void OnFilterParamSetup(uint8_t available_spaces, ApcfAction action, uint8_t status){};
OnFilterConfigCallback(ApcfFilterType filter_type,uint8_t available_spaces,ApcfAction action,uint8_t status)97 void OnFilterConfigCallback(
98 ApcfFilterType filter_type, uint8_t available_spaces, ApcfAction action, uint8_t status){};
99
100 LeScanningManager* le_scanning_manager_;
101 os::Handler* facade_handler_;
102 ::bluetooth::grpc::GrpcEventQueue<LeReportMsg> pending_events_{"LeReports"};
103 };
104
ListDependencies(ModuleList * list)105 void LeScanningManagerFacadeModule::ListDependencies(ModuleList* list) {
106 ::bluetooth::grpc::GrpcFacadeModule::ListDependencies(list);
107 list->add<hci::LeScanningManager>();
108 }
109
Start()110 void LeScanningManagerFacadeModule::Start() {
111 ::bluetooth::grpc::GrpcFacadeModule::Start();
112 service_ = new LeScanningManagerFacadeService(GetDependency<hci::LeScanningManager>(), GetHandler());
113 }
114
Stop()115 void LeScanningManagerFacadeModule::Stop() {
116 delete service_;
117 ::bluetooth::grpc::GrpcFacadeModule::Stop();
118 }
119
GetService() const120 ::grpc::Service* LeScanningManagerFacadeModule::GetService() const {
121 return service_;
122 }
123
124 const ModuleFactory LeScanningManagerFacadeModule::Factory =
__anon42d063a60102() 125 ::bluetooth::ModuleFactory([]() { return new LeScanningManagerFacadeModule(); });
126
127 } // namespace facade
128 } // namespace hci
129 } // namespace bluetooth
130