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

Commit f688134e authored by Chris Manton's avatar Chris Manton
Browse files

Add gd shim support for name module

Bug: 142570089
Test: Start pairing in Gd mode

Change-Id: Ib568f7cd0f511c5f3ef30bd00f237f510135c6b1
parent 22f278b4
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@ filegroup {
            "hci_layer.cc",
            "inquiry.cc",
            "l2cap.cc",
            "name.cc",
            "page.cc",
            "scanning.cc",
            "stack.cc",

system/gd/shim/iname.h

0 → 100644
+40 −0
Original line number Diff line number Diff line
/*
 * Copyright 2019 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 <cstdint>
#include <functional>
#include <string>

/**
 * The gd API exported to the legacy api
 */
using ReadRemoteNameCallback =
    std::function<void(std::string address_string, uint8_t hci_status, std::array<uint8_t, 248> remote_name)>;
using CancelRemoteNameCallback = std::function<void(std::string address_string, uint8_t hci_status)>;

namespace bluetooth {
namespace shim {

struct IName {
  virtual void ReadRemoteNameRequest(std::string remote_address, ReadRemoteNameCallback callback) = 0;
  virtual void CancelRemoteNameRequest(std::string remote_address, CancelRemoteNameCallback callback) = 0;

  virtual ~IName() {}
};

}  // namespace shim
}  // namespace bluetooth
+2 −0
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ struct IConnectability;
struct IDiscoverability;
struct IHciLayer;
struct IInquiry;
struct IName;
struct IL2cap;
struct IPage;
struct IScanning;
@@ -43,6 +44,7 @@ struct IStack {
  virtual IDiscoverability* GetDiscoverability() = 0;
  virtual IHciLayer* GetHciLayer() = 0;
  virtual IInquiry* GetInquiry() = 0;
  virtual IName* GetName() = 0;
  virtual IL2cap* GetL2cap() = 0;
  virtual IPage* GetPage() = 0;
  virtual IScanning* GetScanning() = 0;

system/gd/shim/name.cc

0 → 100644
+125 −0
Original line number Diff line number Diff line
/*
 * Copyright 2019 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 "hci/hci_packets.h"
#include "module.h"
#include "neighbor/name.h"
#include "os/handler.h"
#include "os/log.h"
#include "shim/name.h"

namespace bluetooth {
namespace shim {

struct Name::impl {
  void ReadRemoteNameRequest(const hci::Address address, hci::PageScanRepetitionMode page_scan_repetition_mode,
                             uint16_t clock_offset, hci::ClockOffsetValid clock_offset_valid,
                             ReadRemoteNameCallback callback);
  void CancelRemoteNameRequest(const hci::Address address, CancelRemoteNameCallback callback);

  void OnReadRemoteName(hci::ErrorCode status, hci::Address address, std::array<uint8_t, 248> name);
  void OnCancelRemoteName(hci::ErrorCode status, hci::Address address);

  impl(neighbor::NameModule* module, os::Handler* handler);
  ~impl();

 private:
  std::unordered_map<hci::Address, ReadRemoteNameCallback> address_to_read_remote_callback_map_;
  std::unordered_map<hci::Address, CancelRemoteNameCallback> address_to_cancel_remote_callback_map_;

  neighbor::NameModule* module_{nullptr};
  os::Handler* handler_;
};

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

void Name::impl::OnReadRemoteName(hci::ErrorCode status, hci::Address address, std::array<uint8_t, 248> name) {
  LOG_DEBUG("%s from %s", __func__, address.ToString().c_str());
  ASSERT(address_to_read_remote_callback_map_.find(address) != address_to_read_remote_callback_map_.end());
  ReadRemoteNameCallback callback = address_to_read_remote_callback_map_[address];
  address_to_read_remote_callback_map_.erase(address);
  callback(address.ToString(), static_cast<uint8_t>(status), name);
}

void Name::impl::OnCancelRemoteName(hci::ErrorCode status, hci::Address address) {
  LOG_DEBUG("%s from %s", __func__, address.ToString().c_str());
  ASSERT(address_to_cancel_remote_callback_map_.find(address) != address_to_cancel_remote_callback_map_.end());
  CancelRemoteNameCallback callback = address_to_cancel_remote_callback_map_[address];
  address_to_cancel_remote_callback_map_.erase(address);
  callback(address.ToString(), static_cast<uint8_t>(status));
}

void Name::impl::ReadRemoteNameRequest(const hci::Address address,
                                       hci::PageScanRepetitionMode page_scan_repetition_mode, uint16_t clock_offset,
                                       hci::ClockOffsetValid clock_offset_valid, ReadRemoteNameCallback callback) {
  ASSERT(address_to_read_remote_callback_map_.find(address) == address_to_read_remote_callback_map_.end());
  address_to_read_remote_callback_map_[address] = callback;
  module_->ReadRemoteNameRequest(address, page_scan_repetition_mode, clock_offset, clock_offset_valid,
                                 common::BindOnce(&Name::impl::OnReadRemoteName, common::Unretained(this)), handler_);
}

void Name::impl::CancelRemoteNameRequest(const hci::Address address, CancelRemoteNameCallback callback) {
  ASSERT(address_to_cancel_remote_callback_map_.find(address) == address_to_cancel_remote_callback_map_.end());
  address_to_cancel_remote_callback_map_[address] = callback;
  module_->CancelRemoteNameRequest(address, common::BindOnce(&Name::impl::OnCancelRemoteName, common::Unretained(this)),
                                   handler_);
}

Name::impl::impl(neighbor::NameModule* module, os::Handler* handler) : module_(module), handler_(handler) {}

Name::impl::~impl() {}

void Name::ReadRemoteNameRequest(std::string remote_address, ReadRemoteNameCallback callback) {
  hci::Address address;
  hci::Address::FromString(remote_address, address);

  // TODO(cmanton) Use remote name request defaults for now
  hci::PageScanRepetitionMode page_scan_repetition_mode = hci::PageScanRepetitionMode::R1;
  uint16_t clock_offset = 0;
  hci::ClockOffsetValid clock_offset_valid = hci::ClockOffsetValid::INVALID;
  pimpl_->ReadRemoteNameRequest(address, page_scan_repetition_mode, clock_offset, clock_offset_valid, callback);
}

void Name::CancelRemoteNameRequest(std::string remote_address, CancelRemoteNameCallback callback) {
  hci::Address address;
  hci::Address::FromString(remote_address, address);
  pimpl_->CancelRemoteNameRequest(address, callback);
}

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

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

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

}  // namespace shim
}  // namespace bluetooth

system/gd/shim/name.h

0 → 100644
+48 −0
Original line number Diff line number Diff line
/*
 * Copyright 2019 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 <memory>

#include "module.h"
#include "shim/iname.h"

namespace bluetooth {
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;

  Name() = default;
  ~Name() = default;

  static const ModuleFactory Factory;

 protected:
  void ListDependencies(ModuleList* list) override;  // Module
  void Start() override;                             // Module
  void Stop() override;                              // Module

 private:
  struct impl;
  std::unique_ptr<impl> pimpl_;
  DISALLOW_COPY_AND_ASSIGN(Name);
};

}  // namespace shim
}  // namespace bluetooth
Loading