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