1 /*
2 * Copyright 2021 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 #include "hci/vendor_specific_event_manager.h"
17
18 #include "hci/controller.h"
19 #include "hci/hci_layer.h"
20 #include "hci/hci_packets.h"
21
22 namespace bluetooth {
23 namespace hci {
24
25 const ModuleFactory VendorSpecificEventManager::Factory =
__anon6a8a0f030102() 26 ModuleFactory([]() { return new VendorSpecificEventManager(); });
27
28 struct VendorSpecificEventManager::impl {
implbluetooth::hci::VendorSpecificEventManager::impl29 impl(Module* module) : module_(module){};
30
~implbluetooth::hci::VendorSpecificEventManager::impl31 ~impl() {}
32
startbluetooth::hci::VendorSpecificEventManager::impl33 void start(os::Handler* handler, hci::HciLayer* hci_layer, hci::Controller* controller) {
34 module_handler_ = handler;
35 hci_layer_ = hci_layer;
36 controller_ = controller;
37 hci_layer_->RegisterEventHandler(
38 EventCode::VENDOR_SPECIFIC, handler->BindOn(this, &VendorSpecificEventManager::impl::on_vendor_specific_event));
39 vendor_capabilities_ = controller->GetVendorCapabilities();
40 }
41
stopbluetooth::hci::VendorSpecificEventManager::impl42 void stop() {}
43
register_eventbluetooth::hci::VendorSpecificEventManager::impl44 void register_event(VseSubeventCode event, common::ContextualCallback<void(VendorSpecificEventView)> handler) {
45 ASSERT_LOG(
46 subevent_handlers_.count(event) == 0,
47 "Can not register a second handler for %02hhx (%s)",
48 event,
49 VseSubeventCodeText(event).c_str());
50 subevent_handlers_[event] = handler;
51 }
52
unregister_eventbluetooth::hci::VendorSpecificEventManager::impl53 void unregister_event(VseSubeventCode event) {
54 subevent_handlers_.erase(subevent_handlers_.find(event));
55 }
56
check_event_supportedbluetooth::hci::VendorSpecificEventManager::impl57 bool check_event_supported(VseSubeventCode event) {
58 switch (event) {
59 case (VseSubeventCode::BLE_THRESHOLD): {
60 if (vendor_capabilities_.total_scan_results_storage_ > 0) {
61 return true;
62 }
63 } break;
64 case (VseSubeventCode::BLE_TRACKING): {
65 if (vendor_capabilities_.total_num_of_advt_tracked_ > 0) {
66 return true;
67 }
68 } break;
69 case (VseSubeventCode::DEBUG_INFO): {
70 return vendor_capabilities_.debug_logging_supported_;
71 } break;
72 case (VseSubeventCode::BQR_EVENT): {
73 return vendor_capabilities_.bluetooth_quality_report_support_;
74 } break;
75 default:
76 LOG_WARN("Unhandled event %s", VseSubeventCodeText(event).c_str());
77 }
78 return false;
79 }
80
on_vendor_specific_eventbluetooth::hci::VendorSpecificEventManager::impl81 void on_vendor_specific_event(EventView event_view) {
82 auto vendor_specific_event_view = VendorSpecificEventView::Create(event_view);
83 ASSERT(vendor_specific_event_view.IsValid());
84 VseSubeventCode vse_subevent_code = vendor_specific_event_view.GetSubeventCode();
85 if (subevent_handlers_.find(vse_subevent_code) == subevent_handlers_.end()) {
86 LOG_WARN("Unhandled vendor specific event of type 0x%02hhx", vse_subevent_code);
87 return;
88 }
89 subevent_handlers_[vse_subevent_code].Invoke(vendor_specific_event_view);
90 }
91
92 Module* module_;
93 os::Handler* module_handler_;
94 hci::HciLayer* hci_layer_;
95 hci::Controller* controller_;
96 VendorCapabilities vendor_capabilities_;
97 std::map<VseSubeventCode, common::ContextualCallback<void(VendorSpecificEventView)>> subevent_handlers_;
98 };
99
VendorSpecificEventManager()100 VendorSpecificEventManager::VendorSpecificEventManager() {
101 pimpl_ = std::make_unique<impl>(this);
102 }
103
ListDependencies(ModuleList * list)104 void VendorSpecificEventManager::ListDependencies(ModuleList* list) {
105 list->add<hci::HciLayer>();
106 list->add<hci::Controller>();
107 }
108
Start()109 void VendorSpecificEventManager::Start() {
110 pimpl_->start(GetHandler(), GetDependency<hci::HciLayer>(), GetDependency<hci::Controller>());
111 }
112
Stop()113 void VendorSpecificEventManager::Stop() {
114 pimpl_->stop();
115 pimpl_.reset();
116 }
117
ToString() const118 std::string VendorSpecificEventManager::ToString() const {
119 return "Vendor Specific Event Manager";
120 }
121
RegisterEventHandler(VseSubeventCode event,common::ContextualCallback<void (VendorSpecificEventView)> handler)122 void VendorSpecificEventManager::RegisterEventHandler(
123 VseSubeventCode event, common::ContextualCallback<void(VendorSpecificEventView)> handler) {
124 CallOn(pimpl_.get(), &impl::register_event, event, handler);
125 }
126
UnregisterEventHandler(VseSubeventCode event)127 void VendorSpecificEventManager::UnregisterEventHandler(VseSubeventCode event) {
128 CallOn(pimpl_.get(), &impl::unregister_event, event);
129 }
130
131 } // namespace hci
132 } // namespace bluetooth