RASCSI/cpp/controllers/controller_manager.cpp
Uwe Seimet c98c52ffb8
Cleaned up dependencies on controller manager (#964)
* Cleaned up dependencies on controller manager

* Removed global fields

* Simplified setting up RascsiResponse and RascsiExecutor

* Got rid of remaining raw pointers

* Use references instead of pointers

* Improved encapsulation
2022-11-04 08:22:32 +01:00

85 lines
2.0 KiB
C++

//---------------------------------------------------------------------------
//
// SCSI Target Emulator RaSCSI Reloaded
// for Raspberry Pi
//
// Copyright (C) 2022 Uwe Seimet
//
//---------------------------------------------------------------------------
#include "devices/device_factory.h"
#include "devices/primary_device.h"
#include "scsi_controller.h"
#include "controller_manager.h"
using namespace std;
bool ControllerManager::AttachToScsiController(int id, shared_ptr<PrimaryDevice> device)
{
auto controller = FindController(id);
if (controller != nullptr) {
return controller->AddDevice(device);
}
// If there is no LUN yet the first LUN must be LUN 0
if (device->GetLun() == 0) {
controller = make_shared<ScsiController>(shared_from_this(), id);
if (controller->AddDevice(device)) {
controllers[id] = controller;
return true;
}
}
return false;
}
bool ControllerManager::DeleteController(shared_ptr<AbstractController> controller)
{
return controllers.erase(controller->GetTargetId()) == 1;
}
shared_ptr<AbstractController> ControllerManager::IdentifyController(int data) const
{
for (const auto& [id, controller] : controllers) {
if (data & (1 << controller->GetTargetId())) {
return controller;
}
}
return nullptr;
}
shared_ptr<AbstractController> ControllerManager::FindController(int target_id) const
{
const auto& it = controllers.find(target_id);
return it == controllers.end() ? nullptr : it->second;
}
unordered_set<shared_ptr<PrimaryDevice>> ControllerManager::GetAllDevices() const
{
unordered_set<shared_ptr<PrimaryDevice>> devices;
for (const auto& [id, controller] : controllers) {
const auto& d = controller->GetDevices();
devices.insert(d.begin(), d.end());
}
return devices;
}
void ControllerManager::DeleteAllControllers()
{
controllers.clear();
}
shared_ptr<PrimaryDevice> ControllerManager::GetDeviceByIdAndLun(int id, int lun) const
{
if (const auto& controller = FindController(id); controller != nullptr) {
return controller->GetDeviceForLun(lun);
}
return nullptr;
}