Add gd shim support for name module

Bug: 142570089
Test: Start pairing in Gd mode

Change-Id: Ib568f7cd0f511c5f3ef30bd00f237f510135c6b1
diff --git a/gd/shim/Android.bp b/gd/shim/Android.bp
index cd006bd..33c9b65 100644
--- a/gd/shim/Android.bp
+++ b/gd/shim/Android.bp
@@ -8,6 +8,7 @@
             "hci_layer.cc",
             "inquiry.cc",
             "l2cap.cc",
+            "name.cc",
             "page.cc",
             "scanning.cc",
             "stack.cc",
diff --git a/gd/shim/iname.h b/gd/shim/iname.h
new file mode 100644
index 0000000..c99ba37
--- /dev/null
+++ b/gd/shim/iname.h
@@ -0,0 +1,40 @@
+/*
+ * 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
diff --git a/gd/shim/istack.h b/gd/shim/istack.h
index 899e316..f432dac 100644
--- a/gd/shim/istack.h
+++ b/gd/shim/istack.h
@@ -29,6 +29,7 @@
 struct IDiscoverability;
 struct IHciLayer;
 struct IInquiry;
+struct IName;
 struct IL2cap;
 struct IPage;
 struct IScanning;
@@ -43,6 +44,7 @@
   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;
diff --git a/gd/shim/name.cc b/gd/shim/name.cc
new file mode 100644
index 0000000..582d1ed
--- /dev/null
+++ b/gd/shim/name.cc
@@ -0,0 +1,125 @@
+/*
+ * 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
diff --git a/gd/shim/name.h b/gd/shim/name.h
new file mode 100644
index 0000000..7d8233f
--- /dev/null
+++ b/gd/shim/name.h
@@ -0,0 +1,48 @@
+/*
+ * 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
diff --git a/gd/shim/only_include_this_file_into_legacy_stack___ever.h b/gd/shim/only_include_this_file_into_legacy_stack___ever.h
index 6bbbc00..335778a 100644
--- a/gd/shim/only_include_this_file_into_legacy_stack___ever.h
+++ b/gd/shim/only_include_this_file_into_legacy_stack___ever.h
@@ -31,6 +31,7 @@
 #include "gd/shim/ihci_layer.h"
 #include "gd/shim/iinquiry.h"
 #include "gd/shim/il2cap.h"
+#include "gd/shim/iname.h"
 #include "gd/shim/ipage.h"
 #include "gd/shim/iscanning.h"
 #include "gd/shim/istack.h"
diff --git a/gd/shim/stack.cc b/gd/shim/stack.cc
index c3043d5..7822a35 100644
--- a/gd/shim/stack.cc
+++ b/gd/shim/stack.cc
@@ -27,6 +27,7 @@
 #include "neighbor/connectability.h"
 #include "neighbor/discoverability.h"
 #include "neighbor/inquiry.h"
+#include "neighbor/name.h"
 #include "neighbor/page.h"
 #include "neighbor/scan.h"
 #include "os/log.h"
@@ -39,6 +40,7 @@
 #include "shim/hci_layer.h"
 #include "shim/inquiry.h"
 #include "shim/l2cap.h"
+#include "shim/name.h"
 #include "shim/page.h"
 #include "shim/scanning.h"
 #include "stack_manager.h"
@@ -63,6 +65,7 @@
     modules.add<::bluetooth::neighbor::ConnectabilityModule>();
     modules.add<::bluetooth::neighbor::DiscoverabilityModule>();
     modules.add<::bluetooth::neighbor::InquiryModule>();
+    modules.add<::bluetooth::neighbor::NameModule>();
     modules.add<::bluetooth::neighbor::PageModule>();
     modules.add<::bluetooth::neighbor::ScanModule>();
     modules.add<::bluetooth::shim::Controller>();
@@ -72,6 +75,7 @@
     modules.add<::bluetooth::shim::Connectability>();
     modules.add<::bluetooth::shim::Discoverability>();
     modules.add<::bluetooth::shim::Inquiry>();
+    modules.add<::bluetooth::shim::Name>();
     modules.add<::bluetooth::shim::L2cap>();
     modules.add<::bluetooth::shim::Page>();
     modules.add<::bluetooth::shim::Scanning>();
@@ -124,6 +128,10 @@
     return stack_manager_.GetInstance<bluetooth::shim::L2cap>();
   }
 
+  IName* GetName() {
+    return stack_manager_.GetInstance<bluetooth::shim::Name>();
+  }
+
   IPage* GetPage() {
     return stack_manager_.GetInstance<bluetooth::shim::Page>();
   }
@@ -179,6 +187,10 @@
   return pimpl_->GetL2cap();
 }
 
+bluetooth::shim::IName* bluetooth::shim::Stack::GetName() {
+  return pimpl_->GetName();
+}
+
 bluetooth::shim::IPage* bluetooth::shim::Stack::GetPage() {
   return pimpl_->GetPage();
 }
diff --git a/gd/shim/stack.h b/gd/shim/stack.h
index 4c23b96..f4a51fc 100644
--- a/gd/shim/stack.h
+++ b/gd/shim/stack.h
@@ -25,6 +25,7 @@
 #include "shim/ihci_layer.h"
 #include "shim/iinquiry.h"
 #include "shim/il2cap.h"
+#include "shim/iname.h"
 #include "shim/ipage.h"
 #include "shim/iscanning.h"
 #include "shim/istack.h"
@@ -49,6 +50,7 @@
   IHciLayer* GetHciLayer() override;      // IStack
   IDiscoverability* GetDiscoverability() override;  // IStack
   IInquiry* GetInquiry() override;                  // IStack
+  IName* GetName() override;                        // IStack
   IL2cap* GetL2cap() override;                      // IStack
   IPage* GetPage() override;                        // IStack
   IScanning* GetScanning() override;                // IStack
diff --git a/main/shim/test_stack.cc b/main/shim/test_stack.cc
index 32ada99..9d38541 100644
--- a/main/shim/test_stack.cc
+++ b/main/shim/test_stack.cc
@@ -92,6 +92,8 @@
 
 bluetooth::shim::IL2cap* TestStack::GetL2cap() { return &test_l2cap_; }
 
+bluetooth::shim::IName* TestStack::GetName() { return nullptr; }
+
 bluetooth::shim::IPage* TestStack::GetPage() { return nullptr; }
 
 bluetooth::shim::IScanning* TestStack::GetScanning() { return nullptr; }
diff --git a/main/shim/test_stack.h b/main/shim/test_stack.h
index 5867cc7..92027b1 100644
--- a/main/shim/test_stack.h
+++ b/main/shim/test_stack.h
@@ -61,6 +61,7 @@
   bluetooth::shim::IHciLayer* GetHciLayer();
   bluetooth::shim::IInquiry* GetInquiry();
   bluetooth::shim::IL2cap* GetL2cap();
+  bluetooth::shim::IName* GetName();
   bluetooth::shim::IPage* GetPage();
   bluetooth::shim::IScanning* GetScanning();