1 // Copyright 2023 The Android Open Source Project 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 #pragma once 15 16 #include <cstdint> 17 #include <memory> 18 #include <string> 19 20 #include "model/devices/device.h" 21 #include "packets/link_layer_packets.h" 22 #include "phy.h" 23 #include "rust/cxx.h" 24 25 namespace netsim::hci::facade { 26 // Use forward declaration instead of including "netsim-cxx/src/lib.rs.h". 27 struct DynRustBluetoothChipCallbacks; 28 class RustBluetoothChip; 29 30 class RustDevice : public rootcanal::Device { 31 public: RustDevice(rust::Box<DynRustBluetoothChipCallbacks> callbacks,const std::string & type,const std::string & address)32 RustDevice(rust::Box<DynRustBluetoothChipCallbacks> callbacks, 33 const std::string &type, const std::string &address) 34 : callbacks_(std::move(callbacks)), TYPE(type) { 35 rootcanal::Address::FromString(address, address_); 36 this->SetAddress(address_); 37 } 38 39 void Tick() override; GetTypeString()40 std::string GetTypeString() const override { return TYPE; } ToString()41 std::string ToString() const override { return TYPE; } Close()42 void Close() override {} 43 void ReceiveLinkLayerPacket(::model::packets::LinkLayerPacketView packet, 44 rootcanal::Phy::Type type, int8_t rssi) override; 45 void SetRustBluetoothChip(std::unique_ptr<RustBluetoothChip>); 46 47 private: 48 rust::Box<DynRustBluetoothChipCallbacks> callbacks_; 49 const std::string TYPE; 50 }; 51 52 /// Delegation class for RustDevice to be used in Rust. 53 class RustBluetoothChip { 54 public: RustBluetoothChip(std::shared_ptr<RustDevice> rust_device)55 RustBluetoothChip(std::shared_ptr<RustDevice> rust_device) 56 : rust_device(std::move(rust_device)) {} 57 58 void SendLinkLayerLePacket(const rust::Slice<const uint8_t> packet, 59 int8_t tx_power) const; 60 61 private: 62 std::shared_ptr<RustDevice> rust_device; 63 }; 64 } // namespace netsim::hci::facade