Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ea183db4 authored by Abhishek Pandit-Subedi's avatar Abhishek Pandit-Subedi
Browse files

floss: Remove duplicate topshim address conversions

Get rid of duplicated to_rust_address and from_rust_address conversions
in various topshim files. All the implementations now share a single
conversion from common/utils.h.

Bug: 204488136
Tag: #floss
Test: Build on ChromeOS + Android presubmit build
Change-Id: I37774e37ea3370b76f813af059d6e93cc7aa9dc6
parent a327d905
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -52,6 +52,7 @@ cc_library_static {
        "gatt/gatt_shim.cc",
        "hfp/hfp_shim.cc",
        "controller/controller_shim.cc",
        "common/utils.cc",
    ],
    generated_headers: [
        "libbt_init_flags_bridge_header",
+1 −0
Original line number Diff line number Diff line
@@ -56,6 +56,7 @@ source_set("btif_cxx_bridge_code") {
    "hfp/hfp_shim.cc",
    "gatt/gatt_shim.cc",
    "controller/controller_shim.cc",
    "common/utils.cc",
  ]

  deps = [":btif_bridge_header", "//bt/system/gd:BluetoothGeneratedPackets_h"]
+12 −23
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@
#include <memory>

#include "base/callback.h"
#include "gd/rust/topshim/common/utils.h"
#include "include/hardware/avrcp/avrcp.h"
#include "include/hardware/bluetooth.h"
#include "rust/cxx.h"
@@ -103,18 +104,6 @@ namespace internal {
static A2dpIntf* g_a2dpif;
static AvrcpIntf* g_avrcpif;

static RustRawAddress to_rust_address(const RawAddress& address) {
  RustRawAddress raddr;
  std::copy(std::begin(address.address), std::end(address.address), std::begin(raddr.address));
  return raddr;
}

static RawAddress from_rust_address(const RustRawAddress& raddr) {
  RawAddress addr;
  addr.FromOctets(raddr.address.data());
  return addr;
}

static A2dpCodecConfig to_rust_codec_config(const btav_a2dp_codec_config_t& config) {
  A2dpCodecConfig rconfig = {.codec_type = static_cast<uint8_t>(config.codec_type),
                             .codec_priority = config.codec_priority,
@@ -153,11 +142,11 @@ static ::rust::Vec<A2dpCodecConfig> to_rust_codec_config_vec(const std::vector<b
}

static void connection_state_cb(const RawAddress& bd_addr, btav_connection_state_t state) {
  RustRawAddress addr = to_rust_address(bd_addr);
  RustRawAddress addr = rusty::CopyToRustAddress(bd_addr);
  rusty::connection_state_callback(addr, state);
}
static void audio_state_cb(const RawAddress& bd_addr, btav_audio_state_t state) {
  RustRawAddress addr = to_rust_address(bd_addr);
  RustRawAddress addr = rusty::CopyToRustAddress(bd_addr);
  rusty::audio_state_callback(addr, state);
}
static void audio_config_cb(
@@ -165,14 +154,14 @@ static void audio_config_cb(
    btav_a2dp_codec_config_t codec_config,
    std::vector<btav_a2dp_codec_config_t> codecs_local_capabilities,
    std::vector<btav_a2dp_codec_config_t> codecs_selectable_capabilities) {
  RustRawAddress addr = to_rust_address(bd_addr);
  RustRawAddress addr = rusty::CopyToRustAddress(bd_addr);
  A2dpCodecConfig cfg = to_rust_codec_config(codec_config);
  ::rust::Vec<A2dpCodecConfig> lcaps = to_rust_codec_config_vec(codecs_local_capabilities);
  ::rust::Vec<A2dpCodecConfig> scaps = to_rust_codec_config_vec(codecs_selectable_capabilities);
  rusty::audio_config_callback(addr, cfg, lcaps, scaps);
}
static bool mandatory_codec_preferred_cb(const RawAddress& bd_addr) {
  RustRawAddress addr = to_rust_address(bd_addr);
  RustRawAddress addr = rusty::CopyToRustAddress(bd_addr);
  rusty::mandatory_codec_preferred_callback(addr);
  return true;
}
@@ -208,23 +197,23 @@ int A2dpIntf::init() const {
}

int A2dpIntf::connect(RustRawAddress bt_addr) const {
  RawAddress addr = internal::from_rust_address(bt_addr);
  RawAddress addr = rusty::CopyFromRustAddress(bt_addr);
  return intf_->connect(addr);
}
int A2dpIntf::disconnect(RustRawAddress bt_addr) const {
  RawAddress addr = internal::from_rust_address(bt_addr);
  RawAddress addr = rusty::CopyFromRustAddress(bt_addr);
  return intf_->disconnect(addr);
}
int A2dpIntf::set_silence_device(RustRawAddress bt_addr, bool silent) const {
  RawAddress addr = internal::from_rust_address(bt_addr);
  RawAddress addr = rusty::CopyFromRustAddress(bt_addr);
  return intf_->set_silence_device(addr, silent);
}
int A2dpIntf::set_active_device(RustRawAddress bt_addr) const {
  RawAddress addr = internal::from_rust_address(bt_addr);
  RawAddress addr = rusty::CopyFromRustAddress(bt_addr);
  return intf_->set_active_device(addr);
}
int A2dpIntf::config_codec(RustRawAddress bt_addr, ::rust::Vec<A2dpCodecConfig> codec_preferences) const {
  RawAddress addr = internal::from_rust_address(bt_addr);
  RawAddress addr = rusty::CopyFromRustAddress(bt_addr);
  std::vector<btav_a2dp_codec_config_t> prefs;
  for (int i = 0; i < codec_preferences.size(); ++i) {
    prefs.push_back(internal::from_rust_codec_config(codec_preferences[i]));
@@ -286,11 +275,11 @@ void AvrcpIntf::cleanup() {
}

int AvrcpIntf::connect(RustRawAddress bt_addr) {
  RawAddress addr = internal::from_rust_address(bt_addr);
  RawAddress addr = rusty::CopyFromRustAddress(bt_addr);
  return intf_->ConnectDevice(addr);
}
int AvrcpIntf::disconnect(RustRawAddress bt_addr) {
  RawAddress addr = internal::from_rust_address(bt_addr);
  RawAddress addr = rusty::CopyFromRustAddress(bt_addr);
  return intf_->DisconnectDevice(addr);
}

+6 −9
Original line number Diff line number Diff line
@@ -18,11 +18,14 @@

#include <memory>

#include "gd/rust/topshim/common/utils.h"
#include "include/hardware/bluetooth.h"
#include "rust/cxx.h"
#include "src/profiles/a2dp.rs.h"
#include "types/raw_address.h"

namespace rusty = ::bluetooth::topshim::rust;

namespace bluetooth {
namespace topshim {
namespace rust {
@@ -39,12 +42,6 @@ btav_sink_callbacks_t g_a2dp_sink_callbacks = {
    audio_state_cb,
    audio_config_cb,
};

static RawAddress from_rust_address(const RustRawAddress& raddr) {
  RawAddress addr;
  addr.FromOctets(raddr.address.data());
  return addr;
}
}  // namespace internal

A2dpSinkIntf::~A2dpSinkIntf() {
@@ -67,15 +64,15 @@ int A2dpSinkIntf::init() const {
}

int A2dpSinkIntf::connect(RustRawAddress bt_addr) const {
  return intf_->connect(internal::from_rust_address(bt_addr));
  return intf_->connect(rusty::CopyFromRustAddress(bt_addr));
}

int A2dpSinkIntf::disconnect(RustRawAddress bt_addr) const {
  return intf_->disconnect(internal::from_rust_address(bt_addr));
  return intf_->disconnect(rusty::CopyFromRustAddress(bt_addr));
}

int A2dpSinkIntf::set_active_device(RustRawAddress bt_addr) const {
  return intf_->set_active_device(internal::from_rust_address(bt_addr));
  return intf_->set_active_device(rusty::CopyFromRustAddress(bt_addr));
}

void A2dpSinkIntf::cleanup() const {
+41 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "gd/rust/topshim/common/utils.h"

#include "src/btif.rs.h"

using bluetooth::topshim::rust::RustRawAddress;

namespace bluetooth {
namespace topshim {
namespace rust {

RustRawAddress CopyToRustAddress(const RawAddress& address) {
  RustRawAddress raddr;
  std::copy(std::begin(address.address), std::end(address.address), std::begin(raddr.address));
  return raddr;
}

RawAddress CopyFromRustAddress(const RustRawAddress& rust_address) {
  RawAddress addr;
  addr.FromOctets(rust_address.address.data());
  return addr;
}

}  // namespace rust
}  // namespace topshim
}  // namespace bluetooth
Loading