Skip to content

Commit

Permalink
Adde convenience method
Browse files Browse the repository at this point in the history
  • Loading branch information
uweseimet committed Aug 27, 2023
1 parent 818b77c commit 21addfd
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 4 deletions.
4 changes: 4 additions & 0 deletions cpp/controllers/controller_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ shared_ptr<AbstractController> ControllerManager::FindController(int target_id)
return it == controllers.end() ? nullptr : it->second;
}

bool ControllerManager::HasController(int target_id) const {
return FindController(target_id) != nullptr;
}

unordered_set<shared_ptr<PrimaryDevice>> ControllerManager::GetAllDevices() const
{
unordered_set<shared_ptr<PrimaryDevice>> devices;
Expand Down
1 change: 1 addition & 0 deletions cpp/controllers/controller_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class ControllerManager : public enable_shared_from_this<ControllerManager>
bool DeleteController(shared_ptr<AbstractController>);
shared_ptr<AbstractController> IdentifyController(int) const;
shared_ptr<AbstractController> FindController(int) const;
bool HasController(int) const;
unordered_set<shared_ptr<PrimaryDevice>> GetAllDevices() const;
void DeleteAllControllers();
shared_ptr<PrimaryDevice> GetDeviceByIdAndLun(int, int) const;
Expand Down
4 changes: 2 additions & 2 deletions cpp/piscsi/piscsi_executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ string PiscsiExecutor::SetReservedIds(string_view ids)
return "Invalid ID " + id_to_reserve;
}

if (controller_manager.FindController(res_id) != nullptr) {
if (controller_manager.HasController(res_id)) {
return "ID " + id_to_reserve + " is currently in use";
}

Expand Down Expand Up @@ -680,7 +680,7 @@ string PiscsiExecutor::ValidateLunSetup(const PbCommand& command) const

bool PiscsiExecutor::VerifyExistingIdAndLun(const CommandContext& context, int id, int lun) const
{
if (controller_manager.FindController(id) == nullptr) {
if (!controller_manager.HasController(id)) {
return context.ReturnLocalizedError(LocalizationKey::ERROR_NON_EXISTING_DEVICE, to_string(id));
}

Expand Down
7 changes: 6 additions & 1 deletion cpp/test/controller_manager_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// SCSI Target Emulator PiSCSI
// for Raspberry Pi
//
// Copyright (C) 2022 Uwe Seimet
// Copyright (C) 2022-2023 Uwe Seimet
//
//---------------------------------------------------------------------------

Expand All @@ -26,22 +26,27 @@ TEST(ControllerManagerTest, LifeCycle)

device = device_factory.CreateDevice(SCHS, LUN1, "");
EXPECT_TRUE(controller_manager->AttachToScsiController(ID, device));
EXPECT_TRUE(controller_manager->HasController(ID));
auto controller = controller_manager->FindController(ID);
EXPECT_NE(nullptr, controller);
EXPECT_EQ(1, controller->GetLunCount());
EXPECT_NE(nullptr, controller_manager->IdentifyController(1 << ID));
EXPECT_EQ(nullptr, controller_manager->IdentifyController(0));
EXPECT_FALSE(controller_manager->HasController(0));
EXPECT_EQ(nullptr, controller_manager->FindController(0));
EXPECT_NE(nullptr, controller_manager->GetDeviceByIdAndLun(ID, LUN1));
EXPECT_EQ(nullptr, controller_manager->GetDeviceByIdAndLun(0, 0));

device = device_factory.CreateDevice(SCHS, LUN2, "");
EXPECT_TRUE(controller_manager->AttachToScsiController(ID, device));
EXPECT_TRUE(controller_manager->HasController(ID));
controller = controller_manager->FindController(ID);
EXPECT_NE(nullptr, controller_manager->FindController(ID));
EXPECT_TRUE(controller_manager->DeleteController(controller));
EXPECT_EQ(nullptr, controller_manager->FindController(ID));

controller_manager->DeleteAllControllers();
EXPECT_FALSE(controller_manager->HasController(ID));
EXPECT_EQ(nullptr, controller_manager->FindController(ID));
EXPECT_EQ(nullptr, controller_manager->GetDeviceByIdAndLun(ID, LUN1));
}
Expand Down
2 changes: 1 addition & 1 deletion cpp/test/piscsi_executor_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ TEST_F(PiscsiExecutorTest, DetachAll)

auto device = device_factory.CreateDevice(SCHS, 0, "");
EXPECT_TRUE(controller_manager->AttachToScsiController(ID, device));
EXPECT_NE(nullptr, controller_manager->FindController(ID));
EXPECT_TRUE(controller_manager->HasController(ID));
EXPECT_FALSE(controller_manager->GetAllDevices().empty());

executor.DetachAll();
Expand Down

0 comments on commit 21addfd

Please sign in to comment.