RASCSI/cpp/controllers/controller_manager.cpp
Daniel Markstedt 52c2aa474f
Rebrand project to PiSCSI (#1016)
* Rebrand project to PiSCSI
- rascsi ->piscsi
- rasctl -> scsictl
- rasdump -> scsidump
- ras* -> piscsi* (rasutil -> piscsi_util, etc.)

* Refined the formatting and wording of the app startup banner
* Kept some references to rascsi and rasctl where backwards compatibility is concerned
* Point to the new github repo URL

Co-authored-by: nucleogenic <nr@nucleogenic.com>
Co-authored-by: Uwe Seimet <Uwe.Seimet@seimet.de>
2022-12-05 09:58:23 -08:00

85 lines
2.0 KiB
C++

//---------------------------------------------------------------------------
//
// SCSI Target Emulator PiSCSI
// 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;
}