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

Commit db247a7c authored by Martin Brabham's avatar Martin Brabham Committed by Myles Watson
Browse files

NameDB: Actually remove the address from requested addresses

When a ReadRemoteName is called, there is a single PendingRemoteNameRead
struct that is set.  This contains the callback and handler.  Once this
is set, you cannot set another.  The initial problem is that in the
OnRemoteReadResponse we never remove it from an internal tracking map.
This map prevents us from making duplicate requests which is good, but
since it's never removed, we can only request one time.

This CL fixes that by removing it in the OnRemoteReadResponse.

Additionally, from an API standpoint we want to provide a functioning
API.  If a caller requests a name read and provides a callback, then why
not just call their callback when we get the data instead of telling
them we can't meet their needs (which is untrue).

Bug: 162984360
Tag: #gd-refactor
Test: cert/run --host SecurityTest
Change-Id: I55e41905cde5146bcb06b39078a02aa9ed059ab3
parent ad3087af
Loading
Loading
Loading
Loading
+11 −7
Original line number Diff line number Diff line
@@ -49,7 +49,7 @@ struct NameDbModule::impl {
  void Stop();

 private:
  std::unordered_map<hci::Address, PendingRemoteNameRead> address_to_pending_read_map_;
  std::unordered_map<hci::Address, std::list<PendingRemoteNameRead>> address_to_pending_read_map_;
  std::unordered_map<hci::Address, RemoteName> address_to_name_map_;

  void OnRemoteNameResponse(hci::ErrorCode status, hci::Address address, RemoteName name);
@@ -67,11 +67,14 @@ neighbor::NameDbModule::impl::impl(const neighbor::NameDbModule& module) : modul
void neighbor::NameDbModule::impl::ReadRemoteNameRequest(
    hci::Address address, ReadRemoteNameDbCallback callback, os::Handler* handler) {
  if (address_to_pending_read_map_.find(address) != address_to_pending_read_map_.end()) {
    LOG_WARN("Already have remote read db in progress and currently can only have one outstanding");
    LOG_WARN("Already have remote read db in progress; adding callback to callback list");
    address_to_pending_read_map_[address].push_back({std::move(callback), handler});
    return;
  }

  address_to_pending_read_map_[address] = {std::move(callback), std::move(handler)};
  std::list<PendingRemoteNameRead> tmp;
  address_to_pending_read_map_[address] = std::move(tmp);
  address_to_pending_read_map_[address].push_back({std::move(callback), handler});

  // TODO(cmanton) Use remote name request defaults for now
  hci::PageScanRepetitionMode page_scan_repetition_mode = hci::PageScanRepetitionMode::R1;
@@ -88,13 +91,14 @@ void neighbor::NameDbModule::impl::ReadRemoteNameRequest(

void neighbor::NameDbModule::impl::OnRemoteNameResponse(hci::ErrorCode status, hci::Address address, RemoteName name) {
  ASSERT(address_to_pending_read_map_.find(address) != address_to_pending_read_map_.end());
  PendingRemoteNameRead callback_handler = std::move(address_to_pending_read_map_.at(address));

  if (status == hci::ErrorCode::SUCCESS) {
    address_to_name_map_[address] = name;
  }
  callback_handler.handler_->Post(
      common::BindOnce(std::move(callback_handler.callback_), address, status == hci::ErrorCode::SUCCESS));
  auto& callback_list = address_to_pending_read_map_.at(address);
  for (auto& it : callback_list) {
    it.handler_->Call(std::move(it.callback_), address, status == hci::ErrorCode::SUCCESS);
  }
  address_to_pending_read_map_.erase(address);
}

bool neighbor::NameDbModule::impl::IsNameCached(hci::Address address) const {
+1 −1
Original line number Diff line number Diff line
@@ -32,7 +32,7 @@ using ReadRemoteNameDbCallback = common::OnceCallback<void(hci::Address address,

class NameDbModule : public bluetooth::Module {
 public:
  void ReadRemoteNameRequest(hci::Address address, ReadRemoteNameDbCallback callback, os::Handler* handler);
  virtual void ReadRemoteNameRequest(hci::Address address, ReadRemoteNameDbCallback callback, os::Handler* handler);

  bool IsNameCached(hci::Address address) const;
  RemoteName ReadCachedRemoteName(hci::Address address) const;
+14 −0
Original line number Diff line number Diff line
@@ -28,6 +28,20 @@ class FakeNameDbModule : public neighbor::NameDbModule {
  void ListDependencies(ModuleList* list) override {}
  void Start() override {}
  void Stop() override {}

  void ReadRemoteNameRequest(
      hci::Address address, neighbor::ReadRemoteNameDbCallback callback, os::Handler* handler) override {
    handler->Call(std::move(callback), address, true);
  }

  bool IsNameCached(hci::Address address) const {
    return true;
  }

  neighbor::RemoteName ReadCachedRemoteName(hci::Address address) const {
    neighbor::RemoteName name = {'t', 'e', 's', 't'};
    return name;
  }
};

}  // namespace security