From 8cb41054099061182602325750ac64b204cada93 Mon Sep 17 00:00:00 2001 From: Uwe Seimet <48174652+uweseimet@users.noreply.github.com> Date: Wed, 1 Nov 2023 12:53:05 +0100 Subject: [PATCH] Fix SonarQube issues (#1276) * Fix SonarQube issues * Fix error handling when target ID for INQUIRY is missing --- cpp/controllers/controller_manager.cpp | 5 ++++- cpp/controllers/phase_handler.cpp | 22 ++++++++++++++++++++++ cpp/controllers/phase_handler.h | 13 +++---------- cpp/scsidump/scsidump_core.cpp | 4 ++-- cpp/test/phase_handler_test.cpp | 1 + cpp/test/scsi_controller_test.cpp | 1 + 6 files changed, 33 insertions(+), 13 deletions(-) create mode 100644 cpp/controllers/phase_handler.cpp diff --git a/cpp/controllers/controller_manager.cpp b/cpp/controllers/controller_manager.cpp index d7d2a540..3b55833d 100644 --- a/cpp/controllers/controller_manager.cpp +++ b/cpp/controllers/controller_manager.cpp @@ -15,7 +15,10 @@ using namespace std; shared_ptr ControllerManager::CreateScsiController(BUS& bus, int id) const { - return make_shared(bus, id); + auto controller = make_shared(bus, id); + controller->Init(); + + return controller; } bool ControllerManager::AttachToController(BUS& bus, int id, shared_ptr device) diff --git a/cpp/controllers/phase_handler.cpp b/cpp/controllers/phase_handler.cpp new file mode 100644 index 00000000..664bd095 --- /dev/null +++ b/cpp/controllers/phase_handler.cpp @@ -0,0 +1,22 @@ +//--------------------------------------------------------------------------- +// +// SCSI Target Emulator PiSCSI +// for Raspberry Pi +// +// Copyright (C) 2023 Uwe Seimet +// +//--------------------------------------------------------------------------- + +#include "phase_handler.h" + +void PhaseHandler::Init() +{ + phase_executors[phase_t::busfree] = [this] () { BusFree(); }; + phase_executors[phase_t::selection] = [this] () { Selection(); }; + phase_executors[phase_t::dataout] = [this] () { DataOut(); }; + phase_executors[phase_t::datain] = [this] () { DataIn(); }; + phase_executors[phase_t::command] = [this] () { Command(); }; + phase_executors[phase_t::status] = [this] () { Status(); }; + phase_executors[phase_t::msgout] = [this] () { MsgOut(); }; + phase_executors[phase_t::msgin] = [this] () { MsgIn(); }; +} diff --git a/cpp/controllers/phase_handler.h b/cpp/controllers/phase_handler.h index a7c52035..949c122d 100644 --- a/cpp/controllers/phase_handler.h +++ b/cpp/controllers/phase_handler.h @@ -25,6 +25,8 @@ public: PhaseHandler() = default; virtual ~PhaseHandler() = default; + void Init(); + virtual void BusFree() = 0; virtual void Selection() = 0; virtual void Command() = 0; @@ -61,14 +63,5 @@ protected: private: - const unordered_map> phase_executors = { - { phase_t::busfree, [this] () { BusFree(); } }, - { phase_t::selection, [this] () { Selection(); } }, - { phase_t::dataout, [this] () { DataOut(); } }, - { phase_t::datain, [this] () { DataIn(); } }, - { phase_t::command, [this] () { Command(); } }, - { phase_t::status, [this] () { Status(); } }, - { phase_t::msgout, [this] () { MsgOut(); } }, - { phase_t::msgin, [this] () { MsgIn(); } }, - }; + unordered_map> phase_executors; }; diff --git a/cpp/scsidump/scsidump_core.cpp b/cpp/scsidump/scsidump_core.cpp index 05737842..41c1dabd 100644 --- a/cpp/scsidump/scsidump_core.cpp +++ b/cpp/scsidump/scsidump_core.cpp @@ -153,7 +153,7 @@ void ScsiDump::ParseArguments(span args) throw parser_exception("Missing filename"); } - if (!scan_bus && !inquiry && target_id == -1) { + if (!scan_bus && target_id == -1) { throw parser_exception("Missing target ID"); } @@ -638,7 +638,7 @@ int ScsiDump::DumpRestore() cout << "Transfered : " << inq_info.capacity * inq_info.sector_size << " bytes [" << inq_info.capacity * inq_info.sector_size / 1024 / 1024 << "MiB]\n"; cout << "Total time: " << duration << " seconds (" << duration / 60 << " minutes\n"; - cout << "Averate transfer rate: " << (inq_info.capacity * inq_info.sector_size / 8) / duration + cout << "Average transfer rate: " << (inq_info.capacity * inq_info.sector_size / 8) / duration << " bytes per second (" << (inq_info.capacity * inq_info.sector_size / 8) / duration / 1024 << " KiB per second)\n"; cout << DIVIDER << "\n"; diff --git a/cpp/test/phase_handler_test.cpp b/cpp/test/phase_handler_test.cpp index 70532b54..fc063d99 100644 --- a/cpp/test/phase_handler_test.cpp +++ b/cpp/test/phase_handler_test.cpp @@ -98,6 +98,7 @@ TEST(PhaseHandlerTest, Phases) TEST(PhaseHandlerTest, ProcessPhase) { MockPhaseHandler handler; + handler.Init(); handler.SetPhase(phase_t::selection); EXPECT_CALL(handler, Selection); diff --git a/cpp/test/scsi_controller_test.cpp b/cpp/test/scsi_controller_test.cpp index 4060bc3a..ff726479 100644 --- a/cpp/test/scsi_controller_test.cpp +++ b/cpp/test/scsi_controller_test.cpp @@ -31,6 +31,7 @@ TEST(ScsiControllerTest, Process) { auto bus = make_shared>(); MockScsiController controller(bus, 0); + controller.Init(); controller.SetPhase(phase_t::reserved); ON_CALL(*bus, GetRST).WillByDefault(Return(true));