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

Commit 621577fe authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "gd: Add shim access to name db"

parents ab7ff119 66f43e91
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -10,6 +10,7 @@ filegroup {
            "inquiry.cc",
            "l2cap.cc",
            "name.cc",
            "name_db.cc",
            "page.cc",
            "scanning.cc",
            "security.cc",
+41 −0
Original line number Diff line number Diff line
/*
 * Copyright 2020 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.
 */
#pragma once

#include <array>
#include <cstdint>
#include <functional>
#include <string>

/**
 * The gd API exported to the legacy api
 */
using ReadRemoteNameDbCallback = std::function<void(std::string string_address, bool success)>;

namespace bluetooth {
namespace shim {

struct INameDb {
  virtual void ReadRemoteNameDbRequest(std::string string_address, ReadRemoteNameDbCallback callback) = 0;

  virtual bool IsNameCached(std::string string_address) const = 0;
  virtual std::array<uint8_t, 248> ReadCachedRemoteName(std::string string_address) const = 0;

  virtual ~INameDb() {}
};

}  // namespace shim
}  // namespace bluetooth
+2 −0
Original line number Diff line number Diff line
@@ -31,6 +31,7 @@ struct IDumpsys;
struct IHciLayer;
struct IInquiry;
struct IName;
struct INameDb;
struct IL2cap;
struct IPage;
struct IScanning;
@@ -49,6 +50,7 @@ struct IStack {
  virtual IHciLayer* GetHciLayer() = 0;
  virtual IInquiry* GetInquiry() = 0;
  virtual IName* GetName() = 0;
  virtual INameDb* GetNameDb() = 0;
  virtual IL2cap* GetL2cap() = 0;
  virtual IPage* GetPage() = 0;
  virtual IScanning* GetScanning() = 0;
+2 −2
Original line number Diff line number Diff line
@@ -26,8 +26,8 @@ namespace shim {

class Name : public bluetooth::Module, public bluetooth::shim::IName {
 public:
  void ReadRemoteNameRequest(std::string remote_address, ReadRemoteNameCallback callback) override;
  void CancelRemoteNameRequest(std::string remote_address, CancelRemoteNameCallback callback) override;
  void ReadRemoteNameRequest(std::string string_address, ReadRemoteNameCallback callback) override;
  void CancelRemoteNameRequest(std::string string_address, CancelRemoteNameCallback callback) override;

  Name() = default;
  ~Name() = default;
+106 −0
Original line number Diff line number Diff line
/*
 * Copyright 2020 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.
 */
#define LOG_TAG "bt_gd_shim"

#include <functional>
#include <memory>
#include <string>
#include <unordered_map>

#include "hci/address.h"
#include "module.h"
#include "neighbor/name_db.h"
#include "os/handler.h"
#include "shim/name_db.h"

namespace bluetooth {
namespace shim {

namespace {
constexpr char kModuleName[] = "shim::NameDb";
}  // namespace

struct NameDb::impl {
  impl(neighbor::NameDbModule* module, os::Handler* handler);
  ~impl() = default;

  void OnReadRemoteName(hci::Address address, bool success);
  void ReadRemoteNameDbRequest(hci::Address address, ReadRemoteNameDbCallback callback);

  std::unordered_map<hci::Address, ReadRemoteNameDbCallback> address_to_read_remote_callback_map_;
  neighbor::NameDbModule* module_{nullptr};
  os::Handler* handler_;
};

const ModuleFactory NameDb::Factory = ModuleFactory([]() { return new NameDb(); });

NameDb::impl::impl(neighbor::NameDbModule* module, os::Handler* handler) : module_(module), handler_(handler) {}

void NameDb::impl::OnReadRemoteName(hci::Address address, bool success) {
  LOG_DEBUG("%s from %s status:%s", __func__, address.ToString().c_str(), success ? "true" : "false");
  ASSERT(address_to_read_remote_callback_map_.find(address) != address_to_read_remote_callback_map_.end());
  ReadRemoteNameDbCallback callback = std::move(address_to_read_remote_callback_map_.at(address));
  address_to_read_remote_callback_map_.erase(address);
  callback(address.ToString(), success);
}

void NameDb::impl::ReadRemoteNameDbRequest(hci::Address address, ReadRemoteNameDbCallback callback) {
  ASSERT(address_to_read_remote_callback_map_.find(address) == address_to_read_remote_callback_map_.end());
  address_to_read_remote_callback_map_[address] = std::move(callback);

  module_->ReadRemoteNameRequest(address, common::BindOnce(&NameDb::impl::OnReadRemoteName, common::Unretained(this)),
                                 handler_);
}

void NameDb::ReadRemoteNameDbRequest(std::string string_address, ReadRemoteNameDbCallback callback) {
  hci::Address address;
  ASSERT(hci::Address::FromString(string_address, address));
  pimpl_->ReadRemoteNameDbRequest(address, std::move(callback));
}

bool NameDb::IsNameCached(std::string string_address) const {
  hci::Address address;
  ASSERT(hci::Address::FromString(string_address, address));
  return pimpl_->module_->IsNameCached(address);
}

std::array<uint8_t, 248> NameDb::ReadCachedRemoteName(std::string string_address) const {
  hci::Address address;
  ASSERT(hci::Address::FromString(string_address, address));
  return pimpl_->module_->ReadCachedRemoteName(address);
}

/**
 * Module methods
 */
void NameDb::ListDependencies(ModuleList* list) {
  list->add<neighbor::NameDbModule>();
}

void NameDb::Start() {
  pimpl_ = std::make_unique<impl>(GetDependency<neighbor::NameDbModule>(), GetHandler());
}

void NameDb::Stop() {
  pimpl_.reset();
}

std::string NameDb::ToString() const {
  return kModuleName;
}

}  // namespace shim
}  // namespace bluetooth
Loading