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 #ifndef GD_RUST_TOPSHIM_GATT_GATT_BLE_ADVERTISER_SHIM_H
17 #define GD_RUST_TOPSHIM_GATT_GATT_BLE_ADVERTISER_SHIM_H
18 
19 #include <memory>
20 
21 #include "include/hardware/ble_advertiser.h"
22 #include "rust/cxx.h"
23 
24 namespace bluetooth {
25 namespace topshim {
26 namespace rust {
27 
28 // See include/hardware/ble_advertiser.h for more documentation.
29 //
30 // This shim implementation just calls the underlying interface and binds the
31 // local callbacks in order to dispatch the Rust callbacks.
32 class BleAdvertiserIntf : public ::AdvertisingCallbacks {
33 public:
BleAdvertiserIntf(::BleAdvertiserInterface * adv_intf)34   BleAdvertiserIntf(::BleAdvertiserInterface* adv_intf) : adv_intf_(adv_intf) {}
35   ~BleAdvertiserIntf() = default;
36 
37   // AdvertisingCallbacks overrides
38   void OnAdvertisingSetStarted(int reg_id, uint8_t advertiser_id, int8_t tx_power,
39                                uint8_t status) override;
40   void OnAdvertisingEnabled(uint8_t advertiser_id, bool enable, uint8_t status) override;
41   void OnAdvertisingDataSet(uint8_t advertiser_id, uint8_t status) override;
42   void OnScanResponseDataSet(uint8_t advertiser_id, uint8_t status) override;
43   void OnAdvertisingParametersUpdated(uint8_t advertiser_id, int8_t tx_power,
44                                       uint8_t status) override;
45   void OnPeriodicAdvertisingParametersUpdated(uint8_t advertiser_id, uint8_t status) override;
46   void OnPeriodicAdvertisingDataSet(uint8_t advertiser_id, uint8_t status) override;
47   void OnPeriodicAdvertisingEnabled(uint8_t advertiser_id, bool enable, uint8_t status) override;
48   void OnOwnAddressRead(uint8_t advertiser_id, uint8_t address_type, RawAddress address) override;
49 
50   // BleAdvertiserInterface implementations
51 
52   void RegisterAdvertiser();
53   void Unregister(uint8_t adv_id);
54 
55   void GetOwnAddress(uint8_t adv_id);
56   void SetParameters(uint8_t adv_id, ::AdvertiseParameters params);
57   void SetData(uint8_t adv_id, bool set_scan_rsp, ::rust::Vec<uint8_t> data);
58   void Enable(uint8_t adv_id, bool enable, uint16_t duration, uint8_t max_ext_adv_events);
59   void StartAdvertising(uint8_t adv_id, ::AdvertiseParameters params,
60                         ::rust::Vec<uint8_t> advertise_data,
61                         ::rust::Vec<uint8_t> scan_response_data, int32_t timeout_in_sec);
62   void StartAdvertisingSet(int32_t reg_id, ::AdvertiseParameters params,
63                            ::rust::Vec<uint8_t> advertise_data,
64                            ::rust::Vec<uint8_t> scan_response_data,
65                            ::PeriodicAdvertisingParameters periodic_params,
66                            ::rust::Vec<uint8_t> periodic_data, uint16_t duration,
67                            uint8_t max_ext_adv_events);
68   void SetPeriodicAdvertisingParameters(uint8_t adv_id, ::PeriodicAdvertisingParameters params);
69   void SetPeriodicAdvertisingData(uint8_t adv_id, ::rust::Vec<uint8_t> data);
70   void SetPeriodicAdvertisingEnable(uint8_t adv_id, bool enable, bool include_adi);
71 
72   void RegisterCallbacks();
73 
74 private:
75   // In-band callbacks will get binded to these and sent to Rust via static
76   // callbacks.
77   void OnIdStatusCallback(uint8_t adv_id, uint8_t status);
78   void OnIdTxPowerStatusCallback(uint8_t adv_id, int8_t tx_power, uint8_t status);
79   void OnParametersCallback(uint8_t adv_id, uint8_t status, int8_t tx_power);
80   void OnGetAddressCallback(uint8_t adv_id, uint8_t addr_type, RawAddress address);
81 
82   ::BleAdvertiserInterface* adv_intf_;
83 };
84 
85 std::unique_ptr<BleAdvertiserIntf> GetBleAdvertiserIntf(const unsigned char* gatt_intf);
86 
87 }  // namespace rust
88 }  // namespace topshim
89 }  // namespace bluetooth
90 
91 #endif  // GD_RUST_TOPSHIM_GATT_GATT_BLE_ADVERTISER_SHIM_H
92