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, uint8_t status) override;
39   void OnAdvertisingEnabled(uint8_t advertiser_id, bool enable, uint8_t status) override;
40   void OnAdvertisingDataSet(uint8_t advertiser_id, uint8_t status) override;
41   void OnScanResponseDataSet(uint8_t advertiser_id, uint8_t status) override;
42   void OnAdvertisingParametersUpdated(uint8_t advertiser_id, int8_t tx_power, uint8_t status) override;
43   void OnPeriodicAdvertisingParametersUpdated(uint8_t advertiser_id, uint8_t status) override;
44   void OnPeriodicAdvertisingDataSet(uint8_t advertiser_id, uint8_t status) override;
45   void OnPeriodicAdvertisingEnabled(uint8_t advertiser_id, bool enable, uint8_t status) override;
46   void OnOwnAddressRead(uint8_t advertiser_id, uint8_t address_type, RawAddress address) override;
47 
48   // BleAdvertiserInterface implementations
49 
50   void RegisterAdvertiser();
51   void Unregister(uint8_t adv_id);
52 
53   void GetOwnAddress(uint8_t adv_id);
54   void SetParameters(uint8_t adv_id, AdvertiseParameters params);
55   void SetData(uint8_t adv_id, bool set_scan_rsp, ::rust::Vec<uint8_t> data);
56   void Enable(uint8_t adv_id, bool enable, uint16_t duration, uint8_t max_ext_adv_events);
57   void StartAdvertising(
58       uint8_t adv_id,
59       AdvertiseParameters params,
60       ::rust::Vec<uint8_t> advertise_data,
61       ::rust::Vec<uint8_t> scan_response_data,
62       int32_t timeout_in_sec);
63   void StartAdvertisingSet(
64       int32_t reg_id,
65       AdvertiseParameters params,
66       ::rust::Vec<uint8_t> advertise_data,
67       ::rust::Vec<uint8_t> scan_response_data,
68       PeriodicAdvertisingParameters periodic_params,
69       ::rust::Vec<uint8_t> periodic_data,
70       uint16_t duration,
71       uint8_t max_ext_adv_events);
72   void SetPeriodicAdvertisingParameters(uint8_t adv_id, PeriodicAdvertisingParameters params);
73   void SetPeriodicAdvertisingData(uint8_t adv_id, ::rust::Vec<uint8_t> data);
74   void SetPeriodicAdvertisingEnable(uint8_t adv_id, bool enable, bool include_adi);
75 
76   void RegisterCallbacks();
77 
78  private:
79   // In-band callbacks will get binded to these and sent to Rust via static
80   // callbacks.
81   void OnIdStatusCallback(uint8_t adv_id, uint8_t status);
82   void OnIdTxPowerStatusCallback(uint8_t adv_id, int8_t tx_power, uint8_t status);
83   void OnParametersCallback(uint8_t adv_id, uint8_t status, int8_t tx_power);
84   void OnGetAddressCallback(uint8_t adv_id, uint8_t addr_type, RawAddress address);
85 
86   BleAdvertiserInterface* adv_intf_;
87 };
88 
89 std::unique_ptr<BleAdvertiserIntf> GetBleAdvertiserIntf(const unsigned char* gatt_intf);
90 
91 }  // namespace rust
92 }  // namespace topshim
93 }  // namespace bluetooth
94 
95 #endif  // GD_RUST_TOPSHIM_GATT_GATT_BLE_ADVERTISER_SHIM_H
96