RASCSI/cpp/controllers/controller_manager.cpp
Uwe Seimet 41bdcd4aed
Issues 1179 and 1182 (#1232)
* Update logging

* Remove duplicate code

* Update unit tests

* Clean up includes

* Merge ProtobufSerializer into protobuf_util namespace

* Precompile regex

* Add const

* Add Split() convenience method, update log level/ID parsing

* Move log.h to legacy folder

* Elimininate gotos

* Fixes for gcc 13

* Update compiler flags

* Update default folder handling

* Use references instead of pointers

* Move code for better encapsulation

* Move code

* Remove unused method argument

* Move device logger

* Remove redundant to_string

* Rename for consistency

* Update handling of protobuf pointers

* Simplify protobuf usage

* Memory handling update

* Add hasher
2023-10-15 08:38:15 +02:00

111 lines
2.9 KiB
C++

//---------------------------------------------------------------------------
//
// SCSI Target Emulator PiSCSI
// for Raspberry Pi
//
// Copyright (C) 2022-2023 Uwe Seimet
//
//---------------------------------------------------------------------------
#include "devices/primary_device.h"
#include "scsi_controller.h"
#include "controller_manager.h"
using namespace std;
shared_ptr<ScsiController> ControllerManager::CreateScsiController(BUS& bus, int id) const
{
return make_shared<ScsiController>(bus, id);
}
bool ControllerManager::AttachToController(BUS& bus, int id, shared_ptr<PrimaryDevice> device)
{
if (auto controller = FindController(id); controller != nullptr) {
if (controller->HasDeviceForLun(device->GetLun())) {
return false;
}
return controller->AddDevice(device);
}
// If this is LUN 0 create a new controller
if (!device->GetLun()) {
if (auto controller = CreateScsiController(bus, id); controller->AddDevice(device)) {
controllers[id] = controller;
return true;
}
}
return false;
}
bool ControllerManager::DeleteController(const AbstractController& controller)
{
for (const auto& device : controller.GetDevices()) {
device->CleanUp();
}
return controllers.erase(controller.GetTargetId()) == 1;
}
void ControllerManager::DeleteAllControllers()
{
unordered_set<shared_ptr<AbstractController>> values;
ranges::transform(controllers, inserter(values, values.begin()), [] (const auto& controller) { return controller.second; } );
for (const auto& controller : values) {
DeleteController(*controller);
}
assert(controllers.empty());
}
AbstractController::piscsi_shutdown_mode ControllerManager::ProcessOnController(int id_data) const
{
if (const auto& it = ranges::find_if(controllers, [&] (const auto& c) { return (id_data & (1 << c.first)); } );
it != controllers.end()) {
(*it).second->ProcessOnController(id_data);
return (*it).second->GetShutdownMode();
}
return AbstractController::piscsi_shutdown_mode::NONE;
}
shared_ptr<AbstractController> ControllerManager::FindController(int target_id) const
{
const auto& it = controllers.find(target_id);
return it == controllers.end() ? nullptr : it->second;
}
bool ControllerManager::HasController(int target_id) const {
return controllers.contains(target_id);
}
unordered_set<shared_ptr<PrimaryDevice>> ControllerManager::GetAllDevices() const
{
unordered_set<shared_ptr<PrimaryDevice>> devices;
for (const auto& [_, controller] : controllers) {
const auto& d = controller->GetDevices();
devices.insert(d.begin(), d.end());
}
return devices;
}
bool ControllerManager::HasDeviceForIdAndLun(int id, int lun) const
{
return GetDeviceForIdAndLun(id, lun) != nullptr;
}
shared_ptr<PrimaryDevice> ControllerManager::GetDeviceForIdAndLun(int id, int lun) const
{
if (const auto& controller = FindController(id); controller != nullptr) {
return controller->GetDeviceForLun(lun);
}
return nullptr;
}