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

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

Merge changes Id1de1279,I4fb13f4b,I1f27fffa,I6f650623

* changes:
  Get rid of HciLayer shim
  Prepare to remove HCI layer shim
  Get rid of Advertising shim
  Get rid of Inquiry shim
parents 3e868c34 f104b960
Loading
Loading
Loading
Loading
+6 −6
Original line number Original line Diff line number Diff line
@@ -28,21 +28,21 @@
#include "hci/facade/le_acl_manager_facade.h"
#include "hci/facade/le_acl_manager_facade.h"
#include "hci/facade/le_advertising_manager_facade.h"
#include "hci/facade/le_advertising_manager_facade.h"
#include "hci/facade/le_scanning_manager_facade.h"
#include "hci/facade/le_scanning_manager_facade.h"
#include "hci/hci_layer.h"
#include "hci/le_advertising_manager.h"
#include "hci/le_scanning_manager.h"
#include "hci/le_scanning_manager.h"
#include "l2cap/classic/facade.h"
#include "l2cap/classic/facade.h"
#include "neighbor/connectability.h"
#include "neighbor/connectability.h"
#include "neighbor/discoverability.h"
#include "neighbor/discoverability.h"
#include "neighbor/facade/facade.h"
#include "neighbor/facade/facade.h"
#include "neighbor/inquiry.h"
#include "neighbor/name.h"
#include "neighbor/name.h"
#include "neighbor/page.h"
#include "neighbor/page.h"
#include "os/log.h"
#include "os/log.h"
#include "os/thread.h"
#include "os/thread.h"
#include "security/facade.h"
#include "security/facade.h"
#include "security/security_module.h"
#include "security/security_module.h"
#include "shim/advertising.h"
#include "shim/dumpsys.h"
#include "shim/dumpsys.h"
#include "shim/hci_layer.h"
#include "shim/inquiry.h"
#include "shim/l2cap.h"
#include "shim/l2cap.h"
#include "stack_manager.h"
#include "stack_manager.h"
#include "storage/legacy.h"
#include "storage/legacy.h"
@@ -105,15 +105,15 @@ class RootFacadeService : public ::bluetooth::facade::RootFacade::Service {
        modules.add<::bluetooth::hci::facade::LeScanningManagerFacadeModule>();
        modules.add<::bluetooth::hci::facade::LeScanningManagerFacadeModule>();
        break;
        break;
      case BluetoothModule::SHIM:
      case BluetoothModule::SHIM:
        modules.add<::bluetooth::shim::Advertising>();
        modules.add<::bluetooth::neighbor::ConnectabilityModule>();
        modules.add<::bluetooth::neighbor::ConnectabilityModule>();
        modules.add<::bluetooth::neighbor::DiscoverabilityModule>();
        modules.add<::bluetooth::neighbor::DiscoverabilityModule>();
        modules.add<::bluetooth::neighbor::InquiryModule>();
        modules.add<::bluetooth::neighbor::NameModule>();
        modules.add<::bluetooth::neighbor::NameModule>();
        modules.add<::bluetooth::shim::Dumpsys>();
        modules.add<::bluetooth::shim::Dumpsys>();
        modules.add<::bluetooth::shim::HciLayer>();
        modules.add<::bluetooth::shim::Inquiry>();
        modules.add<::bluetooth::shim::L2cap>();
        modules.add<::bluetooth::shim::L2cap>();
        modules.add<::bluetooth::neighbor::PageModule>();
        modules.add<::bluetooth::neighbor::PageModule>();
        modules.add<::bluetooth::hci::HciLayer>();
        modules.add<::bluetooth::hci::LeAdvertisingManager>();
        modules.add<::bluetooth::hci::LeScanningManager>();
        modules.add<::bluetooth::hci::LeScanningManager>();
        modules.add<::bluetooth::security::SecurityModule>();
        modules.add<::bluetooth::security::SecurityModule>();
        modules.add<::bluetooth::storage::LegacyModule>();
        modules.add<::bluetooth::storage::LegacyModule>();
+0 −3
Original line number Original line Diff line number Diff line
filegroup {
filegroup {
    name: "BluetoothShimSources",
    name: "BluetoothShimSources",
    srcs: [
    srcs: [
            "advertising.cc",
            "dumpsys.cc",
            "dumpsys.cc",
            "hci_layer.cc",
            "inquiry.cc",
            "l2cap.cc",
            "l2cap.cc",
            "stack.cc",
            "stack.cc",
    ]
    ]

system/gd/shim/advertising.cc

deleted100644 → 0
+0 −134
Original line number Original line 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 "hci/address.h"
#include "hci/hci_packets.h"
#include "hci/le_advertising_manager.h"
#include "module.h"
#include "os/handler.h"
#include "os/log.h"
#include "shim/advertising.h"

namespace bluetooth {
namespace shim {

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

struct Advertising::impl {
  impl(hci::LeAdvertisingManager* module, os::Handler* handler);
  ~impl();

  void StartAdvertising();
  void StopAdvertising();

  size_t GetNumberOfAdvertisingInstances() const;

 private:
  void OnScan(hci::Address address, hci::AddressType address_type);
  void OnTerminated(hci::ErrorCode code, uint8_t handle, uint8_t num_events);

  hci::AdvertiserId advertiser_id_{hci::LeAdvertisingManager::kInvalidId};

  hci::LeAdvertisingManager* advertising_manager_{nullptr};
  os::Handler* handler_;
};

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

Advertising::impl::impl(hci::LeAdvertisingManager* advertising_manager, os::Handler* handler)
    : advertising_manager_(advertising_manager), handler_(handler) {}

Advertising::impl::~impl() {}

void Advertising::impl::StartAdvertising() {
  if (advertiser_id_ == hci::LeAdvertisingManager::kInvalidId) {
    LOG_WARN("%s Already advertising; please stop prior to starting again", __func__);
    return;
  }

  hci::AdvertisingConfig config;
  advertiser_id_ =
      advertising_manager_->CreateAdvertiser(config, common::Bind(&impl::OnScan, common::Unretained(this)),
                                             common::Bind(&impl::OnTerminated, common::Unretained(this)), handler_);
  if (advertiser_id_ == hci::LeAdvertisingManager::kInvalidId) {
    LOG_WARN("%s Unable to start advertising", __func__);
    return;
  }
  LOG_DEBUG("%s Started advertising", __func__);
}

void Advertising::impl::StopAdvertising() {
  if (advertiser_id_ == hci::LeAdvertisingManager::kInvalidId) {
    LOG_WARN("%s No active advertising", __func__);
    return;
  }
  advertising_manager_->RemoveAdvertiser(advertiser_id_);
  advertiser_id_ = hci::LeAdvertisingManager::kInvalidId;
  LOG_DEBUG("%s Stopped advertising", __func__);
}

void Advertising::impl::OnScan(hci::Address address, hci::AddressType address_type) {
  LOG_INFO("%s UNIMPLEMENTED Received le advert from:%s", __func__, address.ToString().c_str());
}

void Advertising::impl::OnTerminated(hci::ErrorCode code, uint8_t handle, uint8_t num_events) {
  LOG_INFO("%s UNIMPLEMENTED", __func__);
}

size_t Advertising::impl::GetNumberOfAdvertisingInstances() const {
  return advertising_manager_->GetNumberOfAdvertisingInstances();
}

size_t Advertising::GetNumberOfAdvertisingInstances() const {
  return pimpl_->GetNumberOfAdvertisingInstances();
}

void Advertising::StartAdvertising() {
  pimpl_->StartAdvertising();
}

void Advertising::StopAdvertising() {
  pimpl_->StopAdvertising();
}

/**
 * Module methods
 */
void Advertising::ListDependencies(ModuleList* list) {
  list->add<hci::LeAdvertisingManager>();
}

void Advertising::Start() {
  pimpl_ = std::make_unique<impl>(GetDependency<hci::LeAdvertisingManager>(), GetHandler());
}

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

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

}  // namespace shim
}  // namespace bluetooth

system/gd/shim/advertising.h

deleted100644 → 0
+0 −51
Original line number Original line 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 <string>

#include "module.h"

namespace bluetooth {
namespace shim {

class Advertising : public bluetooth::Module {
 public:
  Advertising() = default;
  ~Advertising() = default;

  void StartAdvertising();
  void StopAdvertising();

  size_t GetNumberOfAdvertisingInstances() const;

  static const ModuleFactory Factory;

 protected:
  void ListDependencies(ModuleList* list) override;  // Module
  void Start() override;                             // Module
  void Stop() override;                              // Module
  std::string ToString() const override;             // Module

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

}  // namespace shim
}  // namespace bluetooth

system/gd/shim/hci_layer.cc

deleted100644 → 0
+0 −242
Original line number Original line 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 <cstdint>
#include <memory>
#include <queue>
#include <string>
#include <unordered_map>
#include <vector>

#include "hci/hci_layer.h"
#include "hci/hci_packets.h"
#include "module.h"
#include "os/handler.h"
#include "os/log.h"
#include "packet/raw_builder.h"
#include "shim/hci_layer.h"

namespace bluetooth {
namespace shim {

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

using TokenQueue = std::queue<const void*>;
using OpCodeTokenQueueMap = std::unordered_map<hci::OpCode, TokenQueue>;

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

struct HciLayer::impl {
  impl(os::Handler* handler, hci::HciLayer* hci_layer) : handler_(handler), hci_layer_(hci_layer) {}

  void OnTransmitPacketCommandComplete(hci::CommandCompleteView view) {
    if (command_complete_callback_ == nullptr) {
      LOG_WARN("%s Received packet complete with no complete callback registered", __func__);
      return;
    }

    uint16_t command_op_code = static_cast<uint16_t>(view.GetCommandOpCode());
    std::vector<const uint8_t> data(view.begin(), view.end());

    if (op_code_token_queue_map_.count(view.GetCommandOpCode()) == 0) {
      LOG_WARN("%s Received unexpected command complete for opcode:0x%04x", __func__, command_op_code);
      return;
    }
    const void* token = op_code_token_queue_map_[view.GetCommandOpCode()].front();
    if (token == nullptr) {
      LOG_WARN("%s Received expected command status but no token for opcode:0x%04x", __func__, command_op_code);
      return;
    }

    op_code_token_queue_map_[view.GetCommandOpCode()].pop();
    command_complete_callback_(command_op_code, data, token);
  }

  void OnTransmitPacketStatus(hci::CommandStatusView view) {
    if (command_status_callback_ == nullptr) {
      LOG_WARN("%s Received packet complete with no status callback registered", __func__);
      return;
    }

    uint16_t command_op_code = static_cast<uint16_t>(view.GetCommandOpCode());
    std::vector<const uint8_t> data(view.begin(), view.end());

    if (op_code_token_queue_map_.count(view.GetCommandOpCode()) == 0) {
      LOG_WARN("%s Received unexpected command status for opcode:0x%04x", __func__, command_op_code);
      return;
    }
    const void* token = op_code_token_queue_map_[view.GetCommandOpCode()].front();
    if (token == nullptr) {
      LOG_WARN("%s Received expected command status but no token for opcode:0x%04x", __func__, command_op_code);
      return;
    }

    op_code_token_queue_map_[view.GetCommandOpCode()].pop();
    uint8_t status = static_cast<uint8_t>(view.GetStatus());
    command_status_callback_(command_op_code, data, token, status);
  }

  void TransmitCommand(uint16_t command, const uint8_t* data, size_t len, const void* token) {
    ASSERT(data != nullptr);
    ASSERT(token != nullptr);

    const hci::OpCode op_code = static_cast<const hci::OpCode>(command);

    auto payload = MakeUniquePacket(data, len);
    auto packet = hci::CommandPacketBuilder::Create(op_code, std::move(payload));

    op_code_token_queue_map_[op_code].push(token);
    if (IsCommandStatusOpcode(op_code)) {
      hci_layer_->EnqueueCommand(std::move(packet),
                                 common::BindOnce(&impl::OnTransmitPacketStatus, common::Unretained(this)), handler_);
    } else {
      hci_layer_->EnqueueCommand(std::move(packet),
                                 common::BindOnce(&impl::OnTransmitPacketCommandComplete, common::Unretained(this)),
                                 handler_);
    }
  }

  void RegisterCommandComplete(CommandCompleteCallback callback) {
    ASSERT(command_complete_callback_ == nullptr);
    command_complete_callback_ = callback;
  }

  void UnregisterCommandComplete() {
    ASSERT(command_complete_callback_ != nullptr);
    command_complete_callback_ = nullptr;
  }

  void RegisterCommandStatus(CommandStatusCallback callback) {
    ASSERT(command_status_callback_ == nullptr);
    command_status_callback_ = callback;
  }

  void UnregisterCommandStatus() {
    ASSERT(command_status_callback_ != nullptr);
    command_status_callback_ = nullptr;
  }

 private:
  os::Handler* handler_{nullptr};
  hci::HciLayer* hci_layer_{nullptr};

  CommandCompleteCallback command_complete_callback_;
  CommandStatusCallback command_status_callback_;

  OpCodeTokenQueueMap op_code_token_queue_map_;

  /**
   * Returns true if expecting command complete, false otherwise
   */
  bool IsCommandStatusOpcode(hci::OpCode op_code) {
    switch (op_code) {
      case hci::OpCode::INQUIRY:
      case hci::OpCode::CREATE_CONNECTION:
      case hci::OpCode::DISCONNECT:
      case hci::OpCode::ACCEPT_CONNECTION_REQUEST:
      case hci::OpCode::REJECT_CONNECTION_REQUEST:
      case hci::OpCode::CHANGE_CONNECTION_PACKET_TYPE:
      case hci::OpCode::AUTHENTICATION_REQUESTED:
      case hci::OpCode::SET_CONNECTION_ENCRYPTION:
      case hci::OpCode::CHANGE_CONNECTION_LINK_KEY:
      case hci::OpCode::MASTER_LINK_KEY:
      case hci::OpCode::REMOTE_NAME_REQUEST:
      case hci::OpCode::READ_REMOTE_SUPPORTED_FEATURES:
      case hci::OpCode::READ_REMOTE_EXTENDED_FEATURES:
      case hci::OpCode::READ_REMOTE_VERSION_INFORMATION:
      case hci::OpCode::READ_CLOCK_OFFSET:
      case hci::OpCode::SETUP_SYNCHRONOUS_CONNECTION:
      case hci::OpCode::ACCEPT_SYNCHRONOUS_CONNECTION:
      case hci::OpCode::REJECT_SYNCHRONOUS_CONNECTION:
      case hci::OpCode::ENHANCED_SETUP_SYNCHRONOUS_CONNECTION:
      case hci::OpCode::ENHANCED_ACCEPT_SYNCHRONOUS_CONNECTION:
      case hci::OpCode::HOLD_MODE:
      case hci::OpCode::SNIFF_MODE:
      case hci::OpCode::EXIT_SNIFF_MODE:
      case hci::OpCode::QOS_SETUP:
      case hci::OpCode::SWITCH_ROLE:
      case hci::OpCode::FLOW_SPECIFICATION:
      case hci::OpCode::REFRESH_ENCRYPTION_KEY:
      case hci::OpCode::LE_CREATE_CONNECTION:
      case hci::OpCode::LE_CONNECTION_UPDATE:
      case hci::OpCode::LE_READ_REMOTE_FEATURES:
      case hci::OpCode::LE_READ_LOCAL_P_256_PUBLIC_KEY_COMMAND:
      case hci::OpCode::LE_GENERATE_DHKEY_COMMAND:
      case hci::OpCode::LE_SET_PHY:
      case hci::OpCode::LE_EXTENDED_CREATE_CONNECTION:
      case hci::OpCode::LE_PERIODIC_ADVERTISING_CREATE_SYNC:
        return true;
      default:
        return false;
    }
  }

  std::unique_ptr<packet::RawBuilder> MakeUniquePacket(const uint8_t* data, size_t len) {
    packet::RawBuilder builder;
    std::vector<uint8_t> bytes(data, data + len);

    auto payload = std::make_unique<packet::RawBuilder>();
    payload->AddOctets(bytes);

    return payload;
  }
};

void HciLayer::TransmitCommand(uint16_t op_code, const uint8_t* data, size_t len, const void* token) {
  pimpl_->TransmitCommand(op_code, data, len, std::move(token));
}

void HciLayer::RegisterCommandComplete(CommandCompleteCallback callback) {
  pimpl_->RegisterCommandComplete(callback);
}

void HciLayer::UnregisterCommandComplete() {
  pimpl_->UnregisterCommandComplete();
}

void HciLayer::RegisterCommandStatus(CommandStatusCallback callback) {
  pimpl_->RegisterCommandStatus(callback);
}

void HciLayer::UnregisterCommandStatus() {
  pimpl_->UnregisterCommandStatus();
}

/**
 * Module methods
 */
void HciLayer::ListDependencies(ModuleList* list) {
  list->add<hci::HciLayer>();
}

void HciLayer::Start() {
  LOG_INFO("%s Starting controller shim layer", __func__);
  pimpl_ = std::make_unique<impl>(GetHandler(), GetDependency<hci::HciLayer>());
}

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

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

}  // namespace shim
}  // namespace bluetooth
Loading