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

Commit 299f41d6 authored by Myles Watson's avatar Myles Watson
Browse files

Track phys in Device in a list

Bug: 154020729
Test: cert/run --host
Tag: #stability
Change-Id: Ia789132ece845051b29b29fc52d4085350738157
parent 00c3520d
Loading
Loading
Loading
Loading
+18 −12
Original line number Diff line number Diff line
@@ -29,23 +29,25 @@ std::string Device::ToString() const {
}

void Device::RegisterPhyLayer(std::shared_ptr<PhyLayer> phy) {
  phy_layers_[phy->GetType()].push_back(phy);
  phy_layers_.push_back(phy);
}

void Device::UnregisterPhyLayers() {
  for (auto phy_pair : phy_layers_) {
    auto phy_list = std::get<1>(phy_pair);
    for (auto phy : phy_list) {
  for (auto phy : phy_layers_) {
    if (phy != nullptr) {
      phy->Unregister();
    }
  }
  phy_layers_.clear();
}

void Device::UnregisterPhyLayer(Phy::Type phy_type, uint32_t factory_id) {
  for (const auto phy_layer : phy_layers_[phy_type]) {
    if (phy_layer->IsFactoryId(factory_id)) {
      phy_layer->Unregister();
      phy_layers_[phy_type].remove(phy_layer);
  for (auto& phy : phy_layers_) {
    if (phy != nullptr && phy->IsFactoryId(factory_id) &&
        phy->GetType() == phy_type) {
      phy->Unregister();
      phy.reset();
      return;
    }
  }
}
@@ -58,17 +60,21 @@ bool Device::IsAdvertisementAvailable() const {
void Device::SendLinkLayerPacket(
    std::shared_ptr<model::packets::LinkLayerPacketBuilder> to_send,
    Phy::Type phy_type) {
  for (const auto& phy : phy_layers_[phy_type]) {
  for (auto phy : phy_layers_) {
    if (phy != nullptr && phy->GetType() == phy_type) {
      phy->Send(to_send);
    }
  }
}

void Device::SendLinkLayerPacket(model::packets::LinkLayerPacketView to_send,
                                 Phy::Type phy_type) {
  for (const auto& phy : phy_layers_[phy_type]) {
  for (auto phy : phy_layers_) {
    if (phy != nullptr && phy->GetType() == phy_type) {
      phy->Send(to_send);
    }
  }
}

void Device::SetAddress(Address) {
  LOG_INFO("%s does not implement %s", GetTypeString().c_str(), __func__);
+1 −2
Original line number Diff line number Diff line
@@ -18,7 +18,6 @@

#include <chrono>
#include <cstdint>
#include <list>
#include <map>
#include <string>
#include <vector>
@@ -87,7 +86,7 @@ class Device {
                                   Phy::Type phy_type);

 protected:
  std::map<Phy::Type, std::list<std::shared_ptr<PhyLayer>>> phy_layers_;
  std::vector<std::shared_ptr<PhyLayer>> phy_layers_;

  std::chrono::steady_clock::time_point last_advertisement_;

+9 −8
Original line number Diff line number Diff line
@@ -41,18 +41,20 @@ std::shared_ptr<PhyLayer> PhyLayerFactory::GetPhyLayer(
}

void PhyLayerFactory::UnregisterPhyLayer(uint32_t id) {
  for (auto it = phy_layers_.begin(); it != phy_layers_.end();) {
    if ((*it)->GetId() == id) {
      it = phy_layers_.erase(it);
    } else {
      it++;
  for (auto phy : phy_layers_) {
    if (phy->GetId() == id) {
      phy_layers_.remove(phy);
      return;
    }
  }
}

void PhyLayerFactory::UnregisterAllPhyLayers() {
  for (const auto& phy : phy_layers_) {
    UnregisterPhyLayer(phy->GetId());
  while (!phy_layers_.empty()) {
    if (phy_layers_.begin() != phy_layers_.end()) {
      auto id = (*phy_layers_.begin())->GetId();
      UnregisterPhyLayer(id);
    }
  }
}

@@ -116,7 +118,6 @@ PhyLayerImpl::PhyLayerImpl(
    : PhyLayer(phy_type, id, device_receive, device_id), factory_(factory) {}

PhyLayerImpl::~PhyLayerImpl() {
  Unregister();
}

void PhyLayerImpl::Send(
+2 −1
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

#pragma once

#include <list>
#include <memory>
#include <vector>

@@ -58,7 +59,7 @@ class PhyLayerFactory {

 private:
  Phy::Type phy_type_;
  std::vector<std::shared_ptr<PhyLayer>> phy_layers_;
  std::list<std::shared_ptr<PhyLayer>> phy_layers_;
  uint32_t next_id_{1};
  const uint32_t factory_id_;
};