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

Commit 9c9f0666 authored by Myles Watson's avatar Myles Watson Committed by Automerger Merge Worker
Browse files

Merge "Revert "RootCanal: Use weak_ptr for Phy, Device"" am: 3e0b7ad0

parents 9c437345 3e0b7ad0
Loading
Loading
Loading
Loading
+4 −8
Original line number Diff line number Diff line
@@ -31,8 +31,7 @@ void Device::RegisterPhyLayer(std::shared_ptr<PhyLayer> phy) {
}

void Device::UnregisterPhyLayers() {
  for (auto weak_phy : phy_layers_) {
    auto phy = weak_phy.lock();
  for (auto phy : phy_layers_) {
    if (phy != nullptr) {
      phy->Unregister();
    }
@@ -41,8 +40,7 @@ void Device::UnregisterPhyLayers() {
}

void Device::UnregisterPhyLayer(Phy::Type phy_type, uint32_t factory_id) {
  for (auto& weak_phy : phy_layers_) {
    auto phy = weak_phy.lock();
  for (auto& phy : phy_layers_) {
    if (phy != nullptr && phy->IsFactoryId(factory_id) &&
        phy->GetType() == phy_type) {
      phy->Unregister();
@@ -61,8 +59,7 @@ bool Device::IsAdvertisementAvailable() const {
void Device::SendLinkLayerPacket(
    std::shared_ptr<model::packets::LinkLayerPacketBuilder> to_send,
    Phy::Type phy_type) {
  for (auto weak_phy : phy_layers_) {
    auto phy = weak_phy.lock();
  for (auto phy : phy_layers_) {
    if (phy != nullptr && phy->GetType() == phy_type) {
      phy->Send(to_send);
    }
@@ -71,8 +68,7 @@ void Device::SendLinkLayerPacket(

void Device::SendLinkLayerPacket(model::packets::LinkLayerPacketView to_send,
                                 Phy::Type phy_type) {
  for (auto weak_phy : phy_layers_) {
    auto phy = weak_phy.lock();
  for (auto phy : phy_layers_) {
    if (phy != nullptr && phy->GetType() == phy_type) {
      phy->Send(to_send);
    }
+1 −1
Original line number Diff line number Diff line
@@ -85,7 +85,7 @@ class Device {
  void RegisterCloseCallback(std::function<void()>);

 protected:
  std::vector<std::weak_ptr<PhyLayer>> phy_layers_;
  std::vector<std::shared_ptr<PhyLayer>> phy_layers_;

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

+14 −23
Original line number Diff line number Diff line
@@ -88,13 +88,13 @@ void TestModel::Del(size_t dev_index) {
  schedule_task_(model_user_id_, std::chrono::milliseconds(0),
                 [this, dev_index]() {
                   devices_[dev_index]->UnregisterPhyLayers();
                   devices_[dev_index].reset();
                   devices_[dev_index] = nullptr;
                 });
}

size_t TestModel::AddPhy(Phy::Type phy_type) {
  size_t factory_id = phys_.size();
  phys_.push_back(std::make_shared<PhyLayerFactory>(phy_type, factory_id));
  phys_.emplace_back(phy_type, factory_id);
  return factory_id;
}

@@ -103,11 +103,9 @@ void TestModel::DelPhy(size_t phy_index) {
    LOG_WARN("Unknown phy at index %zu", phy_index);
    return;
  }
  schedule_task_(model_user_id_, std::chrono::milliseconds(0),
                 [this, phy_index]() {
                   phys_[phy_index]->UnregisterAllPhyLayers();
                   phys_[phy_index].reset();
                 });
  schedule_task_(
      model_user_id_, std::chrono::milliseconds(0),
      [this, phy_index]() { phys_[phy_index].UnregisterAllPhyLayers(); });
}

void TestModel::AddDeviceToPhy(size_t dev_index, size_t phy_index) {
@@ -120,13 +118,9 @@ void TestModel::AddDeviceToPhy(size_t dev_index, size_t phy_index) {
    return;
  }
  auto dev = devices_[dev_index];
  std::weak_ptr<Device> weak_dev = dev;
  dev->RegisterPhyLayer(phys_[phy_index]->GetPhyLayer(
      [weak_dev](model::packets::LinkLayerPacketView packet) {
        auto device = weak_dev.lock();
        if (device != nullptr) {
          device->IncomingPacket(std::move(packet));
        }
  dev->RegisterPhyLayer(phys_[phy_index].GetPhyLayer(
      [dev](model::packets::LinkLayerPacketView packet) {
        dev->IncomingPacket(std::move(packet));
      },
      dev_index));
}
@@ -143,8 +137,8 @@ void TestModel::DelDeviceFromPhy(size_t dev_index, size_t phy_index) {
  schedule_task_(model_user_id_, std::chrono::milliseconds(0),
                 [this, dev_index, phy_index]() {
                   devices_[dev_index]->UnregisterPhyLayer(
                       phys_[phy_index]->GetType(),
                       phys_[phy_index]->GetFactoryId());
                       phys_[phy_index].GetType(),
                       phys_[phy_index].GetFactoryId());
                 });
}

@@ -156,7 +150,7 @@ void TestModel::AddLinkLayerConnection(std::shared_ptr<Device> dev,
  AsyncUserId user_id = get_user_id_();

  for (size_t i = 0; i < phys_.size(); i++) {
    if (phy_type == phys_[i]->GetType()) {
    if (phy_type == phys_[i].GetType()) {
      AddDeviceToPhy(index, i);
    }
  }
@@ -208,7 +202,8 @@ void TestModel::OnConnectionClosed(size_t index, AsyncUserId user_id) {
  }

  cancel_tasks_from_user_(user_id);
  Del(index);
  devices_[index]->UnregisterPhyLayers();
  devices_[index] = nullptr;
}

void TestModel::SetDeviceAddress(size_t index, Address address) {
@@ -233,11 +228,7 @@ const std::string& TestModel::List() {
  list_string_ += " Phys: \r\n";
  for (size_t i = 0; i < phys_.size(); i++) {
    list_string_ += "  " + std::to_string(i) + ":";
    if (phys_[i] == nullptr) {
      list_string_ += " deleted \r\n";
    } else {
      list_string_ += phys_[i]->ToString() + " \r\n";
    }
    list_string_ += phys_[i].ToString() + " \r\n";
  }
  return list_string_;
}
+1 −1
Original line number Diff line number Diff line
@@ -100,7 +100,7 @@ class TestModel {
  void Reset();

 private:
  std::vector<std::shared_ptr<PhyLayerFactory>> phys_;
  std::vector<PhyLayerFactory> phys_;
  std::vector<std::shared_ptr<Device>> devices_;
  std::string list_string_;