//--------------------------------------------------------------------------- // // 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 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(shared_from_this(), id); if (controller->AddDevice(device)) { controllers[id] = controller; return true; } } return false; } bool ControllerManager::DeleteController(shared_ptr controller) { return controllers.erase(controller->GetTargetId()) == 1; } shared_ptr ControllerManager::IdentifyController(int data) const { for (const auto& [id, controller] : controllers) { if (data & (1 << controller->GetTargetId())) { return controller; } } return nullptr; } shared_ptr ControllerManager::FindController(int target_id) const { const auto& it = controllers.find(target_id); return it == controllers.end() ? nullptr : it->second; } unordered_set> ControllerManager::GetAllDevices() const { unordered_set> 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 ControllerManager::GetDeviceByIdAndLun(int id, int lun) const { if (const auto& controller = FindController(id); controller != nullptr) { return controller->GetDeviceForLun(lun); } return nullptr; }