1 /*
2  * Copyright (C) 2022 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 "rust/topshim/gatt/gatt_ble_advertiser_shim.h"
18 
19 #include <base/functional/bind.h>
20 #include <base/functional/callback.h>
21 
22 #include <algorithm>
23 #include <iterator>
24 #include <memory>
25 #include <vector>
26 
27 #include "bind_helpers.h"
28 #include "include/hardware/bt_common_types.h"
29 #include "rust/cxx.h"
30 #include "src/profiles/gatt.rs.h"
31 #include "types/bluetooth/uuid.h"
32 #include "types/raw_address.h"
33 
34 namespace bluetooth {
35 namespace topshim {
36 namespace rust {
37 
38 namespace rusty = ::bluetooth::topshim::rust;
39 
40 // AdvertisingCallbacks implementations
41 
OnAdvertisingSetStarted(int reg_id,uint8_t advertiser_id,int8_t tx_power,uint8_t status)42 void BleAdvertiserIntf::OnAdvertisingSetStarted(int reg_id, uint8_t advertiser_id, int8_t tx_power, uint8_t status) {
43   rusty::gdadv_on_advertising_set_started(reg_id, advertiser_id, tx_power, status);
44 }
OnAdvertisingEnabled(uint8_t advertiser_id,bool enable,uint8_t status)45 void BleAdvertiserIntf::OnAdvertisingEnabled(uint8_t advertiser_id, bool enable, uint8_t status) {
46   rusty::gdadv_on_advertising_enabled(advertiser_id, enable, status);
47 }
OnAdvertisingDataSet(uint8_t advertiser_id,uint8_t status)48 void BleAdvertiserIntf::OnAdvertisingDataSet(uint8_t advertiser_id, uint8_t status) {
49   rusty::gdadv_on_advertising_data_set(advertiser_id, status);
50 }
OnScanResponseDataSet(uint8_t advertiser_id,uint8_t status)51 void BleAdvertiserIntf::OnScanResponseDataSet(uint8_t advertiser_id, uint8_t status) {
52   rusty::gdadv_on_scan_response_data_set(advertiser_id, status);
53 }
OnAdvertisingParametersUpdated(uint8_t advertiser_id,int8_t tx_power,uint8_t status)54 void BleAdvertiserIntf::OnAdvertisingParametersUpdated(uint8_t advertiser_id, int8_t tx_power, uint8_t status) {
55   rusty::gdadv_on_advertising_parameters_updated(advertiser_id, tx_power, status);
56 }
OnPeriodicAdvertisingParametersUpdated(uint8_t advertiser_id,uint8_t status)57 void BleAdvertiserIntf::OnPeriodicAdvertisingParametersUpdated(uint8_t advertiser_id, uint8_t status) {
58   rusty::gdadv_on_periodic_advertising_parameters_updated(advertiser_id, status);
59 }
OnPeriodicAdvertisingDataSet(uint8_t advertiser_id,uint8_t status)60 void BleAdvertiserIntf::OnPeriodicAdvertisingDataSet(uint8_t advertiser_id, uint8_t status) {
61   rusty::gdadv_on_periodic_advertising_data_set(advertiser_id, status);
62 }
OnPeriodicAdvertisingEnabled(uint8_t advertiser_id,bool enable,uint8_t status)63 void BleAdvertiserIntf::OnPeriodicAdvertisingEnabled(uint8_t advertiser_id, bool enable, uint8_t status) {
64   rusty::gdadv_on_periodic_advertising_enabled(advertiser_id, enable, status);
65 }
OnOwnAddressRead(uint8_t advertiser_id,uint8_t address_type,RawAddress addr)66 void BleAdvertiserIntf::OnOwnAddressRead(uint8_t advertiser_id, uint8_t address_type, RawAddress addr) {
67   rusty::gdadv_on_own_address_read(advertiser_id, address_type, &addr);
68 }
69 
70 // BleAdvertiserInterface implementations
71 
RegisterAdvertiser()72 void BleAdvertiserIntf::RegisterAdvertiser() {
73   adv_intf_->RegisterAdvertiser(base::Bind(&BleAdvertiserIntf::OnIdStatusCallback, base::Unretained(this)));
74 }
75 
Unregister(uint8_t adv_id)76 void BleAdvertiserIntf::Unregister(uint8_t adv_id) {
77   adv_intf_->Unregister(adv_id);
78 }
79 
GetOwnAddress(uint8_t adv_id)80 void BleAdvertiserIntf::GetOwnAddress(uint8_t adv_id) {
81   adv_intf_->GetOwnAddress(
82       adv_id, base::Bind(&BleAdvertiserIntf::OnGetAddressCallback, base::Unretained(this), adv_id));
83 }
84 
SetParameters(uint8_t adv_id,AdvertiseParameters params)85 void BleAdvertiserIntf::SetParameters(uint8_t adv_id, AdvertiseParameters params) {
86   adv_intf_->SetParameters(
87       adv_id,
88       params,
89       base::Bind(&BleAdvertiserIntf::OnParametersCallback, base::Unretained(this), adv_id));
90 }
91 
SetData(uint8_t adv_id,bool set_scan_rsp,::rust::Vec<uint8_t> data)92 void BleAdvertiserIntf::SetData(uint8_t adv_id, bool set_scan_rsp, ::rust::Vec<uint8_t> data) {
93   std::vector<uint8_t> converted;
94   std::copy(data.begin(), data.end(), std::back_inserter(converted));
95 
96   adv_intf_->SetData(
97       adv_id,
98       set_scan_rsp,
99       converted,
100       base::Bind(&BleAdvertiserIntf::OnIdStatusCallback, base::Unretained(this), adv_id));
101 }
102 
Enable(uint8_t adv_id,bool enable,uint16_t duration,uint8_t max_ext_adv_events)103 void BleAdvertiserIntf::Enable(uint8_t adv_id, bool enable, uint16_t duration, uint8_t max_ext_adv_events) {
104   adv_intf_->Enable(
105       adv_id,
106       enable,
107       base::Bind(&BleAdvertiserIntf::OnIdStatusCallback, base::Unretained(this), adv_id),
108       duration,
109       max_ext_adv_events,
110       base::Bind(&BleAdvertiserIntf::OnIdStatusCallback, base::Unretained(this), adv_id));
111 }
112 
StartAdvertising(uint8_t adv_id,AdvertiseParameters params,::rust::Vec<uint8_t> advertise_data,::rust::Vec<uint8_t> scan_response_data,int32_t timeout_in_sec)113 void BleAdvertiserIntf::StartAdvertising(
114     uint8_t adv_id,
115     AdvertiseParameters params,
116     ::rust::Vec<uint8_t> advertise_data,
117     ::rust::Vec<uint8_t> scan_response_data,
118     int32_t timeout_in_sec) {
119   std::vector<uint8_t> converted_adv_data, converted_scan_rsp_data;
120   std::copy(advertise_data.begin(), advertise_data.end(), std::back_inserter(converted_adv_data));
121   std::copy(scan_response_data.begin(), scan_response_data.end(), std::back_inserter(converted_scan_rsp_data));
122 
123   adv_intf_->StartAdvertising(
124       adv_id,
125       base::Bind(&BleAdvertiserIntf::OnIdStatusCallback, base::Unretained(this), adv_id),
126       params,
127       converted_adv_data,
128       converted_scan_rsp_data,
129       timeout_in_sec,
130       base::Bind(&BleAdvertiserIntf::OnIdStatusCallback, base::Unretained(this), adv_id));
131 }
132 
StartAdvertisingSet(int32_t reg_id,AdvertiseParameters params,::rust::Vec<uint8_t> advertise_data,::rust::Vec<uint8_t> scan_response_data,PeriodicAdvertisingParameters periodic_params,::rust::Vec<uint8_t> periodic_data,uint16_t duration,uint8_t max_ext_adv_events)133 void BleAdvertiserIntf::StartAdvertisingSet(
134     int32_t reg_id,
135     AdvertiseParameters params,
136     ::rust::Vec<uint8_t> advertise_data,
137     ::rust::Vec<uint8_t> scan_response_data,
138     PeriodicAdvertisingParameters periodic_params,
139     ::rust::Vec<uint8_t> periodic_data,
140     uint16_t duration,
141     uint8_t max_ext_adv_events) {
142   std::vector<uint8_t> converted_adv_data, converted_scan_rsp_data, converted_periodic_data;
143   std::copy(advertise_data.begin(), advertise_data.end(), std::back_inserter(converted_adv_data));
144   std::copy(scan_response_data.begin(), scan_response_data.end(), std::back_inserter(converted_scan_rsp_data));
145   std::copy(periodic_data.begin(), periodic_data.end(), std::back_inserter(converted_periodic_data));
146 
147   adv_intf_->StartAdvertisingSet(
148       kAdvertiserClientIdJni,
149       reg_id,
150       base::Bind(&BleAdvertiserIntf::OnIdTxPowerStatusCallback, base::Unretained(this)),
151       params,
152       converted_adv_data,
153       converted_scan_rsp_data,
154       periodic_params,
155       converted_periodic_data,
156       duration,
157       max_ext_adv_events,
158       base::Bind(&BleAdvertiserIntf::OnIdStatusCallback, base::Unretained(this)));
159 }
160 
SetPeriodicAdvertisingParameters(uint8_t adv_id,PeriodicAdvertisingParameters params)161 void BleAdvertiserIntf::SetPeriodicAdvertisingParameters(
162     uint8_t adv_id, PeriodicAdvertisingParameters params) {
163   adv_intf_->SetPeriodicAdvertisingParameters(
164       adv_id,
165       params,
166       base::Bind(&BleAdvertiserIntf::OnIdStatusCallback, base::Unretained(this), adv_id));
167 }
168 
SetPeriodicAdvertisingData(uint8_t adv_id,::rust::Vec<uint8_t> data)169 void BleAdvertiserIntf::SetPeriodicAdvertisingData(uint8_t adv_id, ::rust::Vec<uint8_t> data) {
170   std::vector<uint8_t> converted;
171   std::copy(data.begin(), data.end(), std::back_inserter(converted));
172 
173   adv_intf_->SetPeriodicAdvertisingData(
174       adv_id, converted, base::Bind(&BleAdvertiserIntf::OnIdStatusCallback, base::Unretained(this), adv_id));
175 }
176 
SetPeriodicAdvertisingEnable(uint8_t adv_id,bool enable,bool include_adi)177 void BleAdvertiserIntf::SetPeriodicAdvertisingEnable(uint8_t adv_id, bool enable, bool include_adi) {
178   adv_intf_->SetPeriodicAdvertisingEnable(
179       adv_id, enable, include_adi, base::Bind(&BleAdvertiserIntf::OnIdStatusCallback, base::Unretained(this), adv_id));
180 }
181 
RegisterCallbacks()182 void BleAdvertiserIntf::RegisterCallbacks() {
183   adv_intf_->RegisterCallbacks(this);
184 }
185 
186 // Inband callbacks
187 
OnIdStatusCallback(uint8_t adv_id,uint8_t status)188 void BleAdvertiserIntf::OnIdStatusCallback(uint8_t adv_id, uint8_t status) {
189   gdadv_idstatus_callback(adv_id, status);
190 }
OnIdTxPowerStatusCallback(uint8_t adv_id,int8_t tx_power,uint8_t status)191 void BleAdvertiserIntf::OnIdTxPowerStatusCallback(uint8_t adv_id, int8_t tx_power, uint8_t status) {
192   gdadv_idtxpowerstatus_callback(adv_id, tx_power, status);
193 }
OnParametersCallback(uint8_t adv_id,uint8_t status,int8_t tx_power)194 void BleAdvertiserIntf::OnParametersCallback(uint8_t adv_id, uint8_t status, int8_t tx_power) {
195   gdadv_parameters_callback(adv_id, status, tx_power);
196 }
OnGetAddressCallback(uint8_t adv_id,uint8_t addr_type,RawAddress addr)197 void BleAdvertiserIntf::OnGetAddressCallback(uint8_t adv_id, uint8_t addr_type, RawAddress addr) {
198   gdadv_getaddress_callback(adv_id, addr_type, &addr);
199 }
200 
GetBleAdvertiserIntf(const unsigned char * gatt_intf)201 std::unique_ptr<BleAdvertiserIntf> GetBleAdvertiserIntf(const unsigned char* gatt_intf) {
202   return std::make_unique<BleAdvertiserIntf>(reinterpret_cast<const btgatt_interface_t*>(gatt_intf)->advertiser);
203 }
204 
205 }  // namespace rust
206 }  // namespace topshim
207 }  // namespace bluetooth
208