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

Commit 7c550125 authored by Jack He's avatar Jack He
Browse files

GD-Shim: Refactor shim layer

* Fix a memmory leak in GetGabeldorscheStack() by allocating singleton
  as static variables instead of heap allocated variables
* Move stack.h/cc from GD to MAIN as such code is not required for GD's
  operation
* Create handler for Stack so that legacy stack callback can run on such
  handler
* Use GD alarm to do scan and observe timers in Btm
* Make Btm park of stack so that we don't have to keep a static copy of
  Btm in btm_api.cc

Bug: 160101414
Tag: #gd-refactor
Test: enable and disable GD via shim
Change-Id: I5521773ac70070d0402afde14707d0e4665a0b10
parent ad463825
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -3,7 +3,6 @@ filegroup {
    srcs: [
            "dumpsys.cc",
            "l2cap.cc",
            "stack.cc",
    ]
}

system/gd/shim/stack.cc

deleted100644 → 0
+0 −126
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 "shim/stack.h"

#include "att/att_module.h"
#include "hal/hci_hal.h"
#include "hci/acl_manager.h"
#include "hci/hci_layer.h"
#include "hci/le_advertising_manager.h"
#include "hci/le_scanning_manager.h"
#include "l2cap/classic/l2cap_classic_module.h"
#include "l2cap/le/l2cap_le_module.h"
#include "neighbor/connectability.h"
#include "neighbor/discoverability.h"
#include "neighbor/inquiry.h"
#include "neighbor/name.h"
#include "neighbor/name_db.h"
#include "neighbor/page.h"
#include "neighbor/scan.h"
#include "os/log.h"
#include "os/thread.h"
#include "security/security_module.h"
#include "shim/dumpsys.h"
#include "shim/l2cap.h"
#include "stack_manager.h"
#include "storage/storage_module.h"

using ::bluetooth::os::Thread;

struct bluetooth::shim::Stack::impl {
  void Start() {
    if (is_running_) {
      LOG_ERROR("%s Gd stack already running", __func__);
      return;
    }

    LOG_INFO("%s Starting Gd stack", __func__);
    ModuleList modules;
    modules.add<::bluetooth::att::AttModule>();
    modules.add<::bluetooth::hal::HciHal>();
    modules.add<::bluetooth::hci::AclManager>();
    modules.add<::bluetooth::hci::HciLayer>();
    modules.add<::bluetooth::hci::LeAdvertisingManager>();
    modules.add<::bluetooth::hci::LeScanningManager>();
    modules.add<::bluetooth::l2cap::classic::L2capClassicModule>();
    modules.add<::bluetooth::l2cap::le::L2capLeModule>();
    modules.add<::bluetooth::neighbor::ConnectabilityModule>();
    modules.add<::bluetooth::neighbor::DiscoverabilityModule>();
    modules.add<::bluetooth::neighbor::InquiryModule>();
    modules.add<::bluetooth::neighbor::NameModule>();
    modules.add<::bluetooth::neighbor::NameDbModule>();
    modules.add<::bluetooth::neighbor::PageModule>();
    modules.add<::bluetooth::neighbor::ScanModule>();
    modules.add<::bluetooth::security::SecurityModule>();
    modules.add<::bluetooth::storage::StorageModule>();
    modules.add<::bluetooth::shim::Dumpsys>();
    modules.add<::bluetooth::shim::L2cap>();

    stack_thread_ = new Thread("gd_stack_thread", Thread::Priority::NORMAL);
    stack_manager_.StartUp(&modules, stack_thread_);
    // TODO(cmanton) Gd stack has spun up another thread with no
    // ability to ascertain the completion
    is_running_ = true;
    LOG_INFO("%s Successfully toggled Gd stack", __func__);
  }

  void Stop() {
    if (!is_running_) {
      LOG_ERROR("%s Gd stack not running", __func__);
      return;
    }

    stack_manager_.ShutDown();
    delete stack_thread_;
    is_running_ = false;
    LOG_INFO("%s Successfully shut down Gd stack", __func__);
  }

  StackManager* GetStackManager() {
    ASSERT(is_running_);
    return &stack_manager_;
  }

 private:
  os::Thread* stack_thread_ = nullptr;
  bool is_running_ = false;
  StackManager stack_manager_;
};

bluetooth::shim::Stack::Stack() {
  pimpl_ = std::make_unique<impl>();
  LOG_INFO("%s Created gd stack", __func__);
}

void bluetooth::shim::Stack::Start() {
  pimpl_->Start();
}

void bluetooth::shim::Stack::Stop() {
  pimpl_->Stop();
}

bluetooth::StackManager* bluetooth::shim::Stack::GetStackManager() {
  return pimpl_->GetStackManager();
}

bluetooth::shim::Stack* bluetooth::shim::GetGabeldorscheStack() {
  static Stack* instance = new Stack();
  return instance;
}
+1 −1
Original line number Diff line number Diff line
@@ -12,6 +12,6 @@ filegroup {
        "l2c_api.cc",
        "l2cap.cc",
        "shim.cc",
        "timer.cc",
        "stack.cc",
    ]
}
+320 −356

File changed.

Preview size limit exceeded, changes collapsed.

+39 −49
Original line number Diff line number Diff line
@@ -23,12 +23,16 @@
#include <unordered_map>
#include <vector>

#include "main/shim/timer.h"
#include "stack/include/btm_api_types.h"

#include "hci/hci_packets.h"

#include "stack/include/btm_api_types.h"
#include "types/raw_address.h"

#include "gd/common/callback.h"
#include "gd/hci/le_advertising_manager.h"
#include "gd/hci/le_scanning_manager.h"
#include "gd/neighbor/inquiry.h"
#include "gd/os/alarm.h"

//
// NOTE: limited and general constants for inquiry and discoverable are swapped
@@ -106,39 +110,10 @@ using BtmStatus = enum : uint16_t {
  BTM_DEV_BLACKLISTED = 21,            /* The device is Blacklisted */
};

class ReadRemoteName {
 public:
  bool Start(RawAddress raw_address) {
    std::unique_lock<std::mutex> lock(mutex_);
    if (in_progress_) {
      return false;
    }
    raw_address_ = raw_address;
    in_progress_ = true;
    return true;
  }

  void Stop() {
    std::unique_lock<std::mutex> lock(mutex_);
    raw_address_ = RawAddress::kEmpty;
    in_progress_ = false;
  }

  bool IsInProgress() const { return in_progress_; }

  std::string AddressString() const { return raw_address_.ToString(); }

  ReadRemoteName() : in_progress_{false}, raw_address_(RawAddress::kEmpty) {}

 private:
  bool in_progress_;
  RawAddress raw_address_;
  std::mutex mutex_;
};

class Btm {
 public:
  Btm() = default;
  // |handler| is used to run timer tasks and scan callbacks
  Btm(os::Handler* handler, neighbor::InquiryModule* inquiry);
  ~Btm() = default;

  // Inquiry result callbacks
@@ -181,7 +156,6 @@ class Btm {
  bool limited_inquiry_active_{false};
  bool general_periodic_inquiry_active_{false};
  bool limited_periodic_inquiry_active_{false};
  void RegisterInquiryCallbacks();
  void SetClassicGeneralDiscoverability(uint16_t window, uint16_t interval);
  void SetClassicLimitedDiscoverability(uint16_t window, uint16_t interval);
  void SetClassicDiscoverabilityOff();
@@ -227,15 +201,13 @@ class Btm {

  size_t GetNumberOfAdvertisingInstances() const;

  void SetObservingTimer(uint64_t duration_ms, std::function<void()>);
  void SetObservingTimer(uint64_t duration_ms,
                         common::OnceCallback<void()> callback);
  void CancelObservingTimer();
  void SetScanningTimer(uint64_t duration_ms, std::function<void()>);
  void SetScanningTimer(uint64_t duration_ms,
                        common::OnceCallback<void()> callback);
  void CancelScanningTimer();

  // Lifecycle
  static void StartUp(Btm* btm);
  static void ShutDown(Btm* btm);

  tBTM_STATUS CreateBond(const RawAddress& bd_addr, tBLE_ADDR_TYPE addr_type,
                         tBT_TRANSPORT transport, int device_type);
  bool CancelBond(const RawAddress& bd_addr);
@@ -244,17 +216,35 @@ class Btm {
  uint16_t GetAclHandle(const RawAddress& remote_bda, tBT_TRANSPORT transport);

 private:
  ReadRemoteName le_read_remote_name_;
  ReadRemoteName classic_read_remote_name_;
  os::Alarm scanning_timer_;
  os::Alarm observing_timer_;

  Timer* observing_timer_{nullptr};
  Timer* scanning_timer_{nullptr};
  LegacyInquiryCompleteCallback legacy_inquiry_complete_callback_{};
  uint8_t active_inquiry_mode_ = 0;

  std::mutex sync_mutex_;
  class ReadRemoteName {
   public:
    ReadRemoteName() = default;
    bool Start(RawAddress raw_address);
    void Stop();
    bool IsInProgress() const;
    std::string AddressString() const;

  LegacyInquiryCompleteCallback legacy_inquiry_complete_callback_{};
   private:
    std::mutex mutex_;
    bool in_progress_ = false;
    RawAddress raw_address_ = RawAddress::kEmpty;
  };
  ReadRemoteName le_read_remote_name_;
  ReadRemoteName classic_read_remote_name_;

  uint8_t active_inquiry_mode_ = 0;
  class ScanningCallbacks : public hci::LeScanningManagerCallbacks {
    void on_advertisements(
        std::vector<std::shared_ptr<hci::LeReport>> reports) override;
    void on_timeout() override;
    os::Handler* Handler() override;
  };
  ScanningCallbacks scanning_callbacks_;

  // TODO(cmanton) abort if there is no classic acl link up
  bool CheckClassicAclLink(const RawAddress& raw_address) { return true; }
Loading