• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "hci/le_report.h"
30 #include "os/log.h"
31 #include "packet/raw_builder.h"
32 
33 namespace bluetooth {
34 namespace hci {
35 namespace facade {
36 
37 using ::grpc::ServerAsyncResponseWriter;
38 using ::grpc::ServerAsyncWriter;
39 using ::grpc::ServerContext;
40 using ::grpc::ServerWriter;
41 using ::grpc::Status;
42 
43 class LeScanningManagerFacadeService : public LeScanningManagerFacade::Service, public LeScanningManagerCallbacks {
44  public:
LeScanningManagerFacadeService(LeScanningManager * le_scanning_manager,os::Handler * facade_handler)45   LeScanningManagerFacadeService(LeScanningManager* le_scanning_manager, os::Handler* facade_handler)
46       : le_scanning_manager_(le_scanning_manager), facade_handler_(facade_handler) {
47     ASSERT(le_scanning_manager_ != nullptr);
48     ASSERT(facade_handler_ != nullptr);
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_->StartScan(this);
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     std::shared_ptr<std::promise<void>> on_stopped = std::make_shared<std::promise<void>>();
60     auto future = on_stopped->get_future();
61     le_scanning_manager_->StopScan(
62         common::Bind([](std::shared_ptr<std::promise<void>> p) { p->set_value(); }, on_stopped));
63     auto result = future.wait_for(std::chrono::milliseconds(1000));
64     ASSERT(result == std::future_status::ready);
65     return ::grpc::Status::OK;
66   }
67 
on_advertisements(std::vector<std::shared_ptr<LeReport>> reports)68   void on_advertisements(std::vector<std::shared_ptr<LeReport>> reports) override {
69     for (const auto report : reports) {
70       switch (report->report_type_) {
71         case hci::LeReport::ReportType::ADVERTISING_EVENT: {
72           LeReportMsg le_report_msg;
73           std::vector<LeAdvertisingReport> advertisements;
74           LeAdvertisingReport le_advertising_report;
75           le_advertising_report.address_type_ = report->address_type_;
76           le_advertising_report.address_ = report->address_;
77           le_advertising_report.advertising_data_ = report->gap_data_;
78           le_advertising_report.event_type_ = report->advertising_event_type_;
79           le_advertising_report.rssi_ = report->rssi_;
80           advertisements.push_back(le_advertising_report);
81 
82           auto builder = LeAdvertisingReportBuilder::Create(advertisements);
83           std::vector<uint8_t> bytes;
84           BitInserter bit_inserter(bytes);
85           builder->Serialize(bit_inserter);
86           le_report_msg.set_event(std::string(bytes.begin(), bytes.end()));
87           pending_events_.OnIncomingEvent(std::move(le_report_msg));
88         } break;
89         case hci::LeReport::ReportType::EXTENDED_ADVERTISING_EVENT: {
90           LeReportMsg le_report_msg;
91           std::vector<LeExtendedAdvertisingReport> advertisements;
92           LeExtendedAdvertisingReport le_extended_advertising_report;
93           le_extended_advertising_report.address_ = report->address_;
94           le_extended_advertising_report.advertising_data_ = report->gap_data_;
95           le_extended_advertising_report.rssi_ = report->rssi_;
96           advertisements.push_back(le_extended_advertising_report);
97 
98           auto builder = LeExtendedAdvertisingReportBuilder::Create(advertisements);
99           std::vector<uint8_t> bytes;
100           BitInserter bit_inserter(bytes);
101           builder->Serialize(bit_inserter);
102           le_report_msg.set_event(std::string(bytes.begin(), bytes.end()));
103           pending_events_.OnIncomingEvent(std::move(le_report_msg));
104         } break;
105         case hci::LeReport::ReportType::DIRECTED_ADVERTISING_EVENT: {
106           LeReportMsg le_report_msg;
107           std::vector<LeDirectedAdvertisingReport> advertisements;
108           LeDirectedAdvertisingReport le_directed_advertising_report;
109           le_directed_advertising_report.address_ = report->address_;
110           le_directed_advertising_report.direct_address_ = ((DirectedLeReport*)report.get())->direct_address_;
111           le_directed_advertising_report.direct_address_type_ = DirectAddressType::RANDOM_DEVICE_ADDRESS;
112           le_directed_advertising_report.event_type_ = DirectAdvertisingEventType::ADV_DIRECT_IND;
113           le_directed_advertising_report.rssi_ = report->rssi_;
114           advertisements.push_back(le_directed_advertising_report);
115 
116           auto builder = LeDirectedAdvertisingReportBuilder::Create(advertisements);
117           std::vector<uint8_t> bytes;
118           BitInserter bit_inserter(bytes);
119           builder->Serialize(bit_inserter);
120           le_report_msg.set_event(std::string(bytes.begin(), bytes.end()));
121           pending_events_.OnIncomingEvent(std::move(le_report_msg));
122         } break;
123         default:
124           LOG_INFO("Skipping unknown report type %d", static_cast<int>(report->report_type_));
125       }
126     }
127   }
128 
on_timeout()129   void on_timeout() override {
130     LeReportMsg le_report_msg;
131     auto builder = LeScanTimeoutBuilder::Create();
132     std::vector<uint8_t> bytes;
133     BitInserter bit_inserter(bytes);
134     builder->Serialize(bit_inserter);
135     le_report_msg.set_event(std::string(bytes.begin(), bytes.end()));
136     pending_events_.OnIncomingEvent(std::move(le_report_msg));
137   }
138 
Handler()139   os::Handler* Handler() override {
140     return facade_handler_;
141   }
142 
143   LeScanningManager* le_scanning_manager_;
144   os::Handler* facade_handler_;
145   ::bluetooth::grpc::GrpcEventQueue<LeReportMsg> pending_events_{"LeReports"};
146 };
147 
ListDependencies(ModuleList * list)148 void LeScanningManagerFacadeModule::ListDependencies(ModuleList* list) {
149   ::bluetooth::grpc::GrpcFacadeModule::ListDependencies(list);
150   list->add<hci::LeScanningManager>();
151 }
152 
Start()153 void LeScanningManagerFacadeModule::Start() {
154   ::bluetooth::grpc::GrpcFacadeModule::Start();
155   service_ = new LeScanningManagerFacadeService(GetDependency<hci::LeScanningManager>(), GetHandler());
156 }
157 
Stop()158 void LeScanningManagerFacadeModule::Stop() {
159   delete service_;
160   ::bluetooth::grpc::GrpcFacadeModule::Stop();
161 }
162 
GetService() const163 ::grpc::Service* LeScanningManagerFacadeModule::GetService() const {
164   return service_;
165 }
166 
167 const ModuleFactory LeScanningManagerFacadeModule::Factory =
__anon2e20bfa50202() 168     ::bluetooth::ModuleFactory([]() { return new LeScanningManagerFacadeModule(); });
169 
170 }  // namespace facade
171 }  // namespace hci
172 }  // namespace bluetooth
173