Skip to content

Commit

Permalink
Fix SonarQube issues (PiSCSI#1276)
Browse files Browse the repository at this point in the history
* Fix SonarQube issues

* Fix error handling when target ID for INQUIRY is missing
  • Loading branch information
uweseimet authored Nov 1, 2023
1 parent 029cf06 commit 8cb4105
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 13 deletions.
5 changes: 4 additions & 1 deletion cpp/controllers/controller_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ using namespace std;

shared_ptr<ScsiController> ControllerManager::CreateScsiController(BUS& bus, int id) const
{
return make_shared<ScsiController>(bus, id);
auto controller = make_shared<ScsiController>(bus, id);
controller->Init();

return controller;
}

bool ControllerManager::AttachToController(BUS& bus, int id, shared_ptr<PrimaryDevice> device)
Expand Down
22 changes: 22 additions & 0 deletions cpp/controllers/phase_handler.cpp
Original file line number Diff line number Diff line change
@@ -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(); };
}
13 changes: 3 additions & 10 deletions cpp/controllers/phase_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class PhaseHandler
PhaseHandler() = default;
virtual ~PhaseHandler() = default;

void Init();

virtual void BusFree() = 0;
virtual void Selection() = 0;
virtual void Command() = 0;
Expand Down Expand Up @@ -61,14 +63,5 @@ class PhaseHandler

private:

const unordered_map<phase_t, function<void()>> 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_t, function<void()>> phase_executors;
};
4 changes: 2 additions & 2 deletions cpp/scsidump/scsidump_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ void ScsiDump::ParseArguments(span<char *> 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");
}

Expand Down Expand Up @@ -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";
Expand Down
1 change: 1 addition & 0 deletions cpp/test/phase_handler_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ TEST(PhaseHandlerTest, Phases)
TEST(PhaseHandlerTest, ProcessPhase)
{
MockPhaseHandler handler;
handler.Init();

handler.SetPhase(phase_t::selection);
EXPECT_CALL(handler, Selection);
Expand Down
1 change: 1 addition & 0 deletions cpp/test/scsi_controller_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ TEST(ScsiControllerTest, Process)
{
auto bus = make_shared<NiceMock<MockBus>>();
MockScsiController controller(bus, 0);
controller.Init();

controller.SetPhase(phase_t::reserved);
ON_CALL(*bus, GetRST).WillByDefault(Return(true));
Expand Down

0 comments on commit 8cb4105

Please sign in to comment.