From 0db591a34e02bddca70dbe8c84849268987431fd Mon Sep 17 00:00:00 2001 From: Uwe Seimet Date: Mon, 6 Jan 2025 18:30:31 +0100 Subject: [PATCH] Update status code handling in initiator tools --- cpp/initiator/initiator_executor.cpp | 17 +---------------- cpp/initiator/initiator_executor.h | 2 -- cpp/s2pexec/s2pexec_core.cpp | 8 +++----- cpp/shared/s2p_util.cpp | 13 +++++++++++++ cpp/shared/s2p_util.h | 2 ++ 5 files changed, 19 insertions(+), 23 deletions(-) diff --git a/cpp/initiator/initiator_executor.cpp b/cpp/initiator/initiator_executor.cpp index 6aafd970..0427af8b 100644 --- a/cpp/initiator/initiator_executor.cpp +++ b/cpp/initiator/initiator_executor.cpp @@ -84,7 +84,7 @@ int InitiatorExecutor::Execute(span cdb, span buffer, int leng } if (enable_log) { - LogStatus(); + initiator_logger.warn(GetStatusString(status_code)); } return status_code; @@ -337,18 +337,3 @@ void InitiatorExecutor::SetTarget(int id, int lun, bool s) sasi = s; } -void InitiatorExecutor::LogStatus() const -{ - if (status_code) { - if (const auto &it = STATUS_MAPPING.find(static_cast(status_code)); it != STATUS_MAPPING.end()) { - initiator_logger.warn("Device reported {0} (status code ${1:02x})", it->second, status_code); - } - else if (status_code != 0xff) { - initiator_logger.warn("Device reported an unknown status (status code ${:02x})", status_code); - } - else { - initiator_logger.warn("Device did not respond"); - } - } -} - diff --git a/cpp/initiator/initiator_executor.h b/cpp/initiator/initiator_executor.h index d560c356..6778fa70 100644 --- a/cpp/initiator/initiator_executor.h +++ b/cpp/initiator/initiator_executor.h @@ -72,8 +72,6 @@ class InitiatorExecutor bool WaitForFree() const; bool WaitForBusy() const; - void LogStatus() const; - void Sleep(const timespec &ns) const { nanosleep(&ns, nullptr); diff --git a/cpp/s2pexec/s2pexec_core.cpp b/cpp/s2pexec/s2pexec_core.cpp index 97cd5037..60ace34b 100644 --- a/cpp/s2pexec/s2pexec_core.cpp +++ b/cpp/s2pexec/s2pexec_core.cpp @@ -492,13 +492,11 @@ tuple S2pExec::ExecuteCommand() return executor->GetSenseData(); } - if (status == 0x02) { - throw execution_exception("Device reported CHECK CONDITION"); - } + throw execution_exception(GetStatusString(status)); } else { - throw execution_exception(fmt::format("Can't execute command {}", - CommandMetaData::Instance().GetCommandName(static_cast(cdb[0])))); + throw execution_exception(fmt::format("Can't execute command {0} (${1:2x})", + CommandMetaData::Instance().GetCommandName(static_cast(cdb[0])), cdb[0])); } } diff --git a/cpp/shared/s2p_util.cpp b/cpp/shared/s2p_util.cpp index 9b8f868e..b370073e 100644 --- a/cpp/shared/s2p_util.cpp +++ b/cpp/shared/s2p_util.cpp @@ -245,6 +245,19 @@ string s2p_util::GetScsiLevel(int scsi_level) } } +string s2p_util::GetStatusString(int status_code) +{ + if (const auto &it = STATUS_MAPPING.find(static_cast(status_code)); it != STATUS_MAPPING.end()) { + return fmt::format("Device reported {0} (status code ${1:02x})", it->second, status_code); + } + else if (status_code != 0xff) { + return fmt::format("Device reported an unknown status (status code ${:02x})", status_code); + } + else { + return "Device did not respond"; + } +} + string s2p_util::FormatSenseData(span sense_data) { const auto flags = static_cast(sense_data[2]); diff --git a/cpp/shared/s2p_util.h b/cpp/shared/s2p_util.h index 8802e628..c8319f5d 100644 --- a/cpp/shared/s2p_util.h +++ b/cpp/shared/s2p_util.h @@ -70,6 +70,8 @@ tuple GetInquiryProductData(span); string GetScsiLevel(int); +string GetStatusString(int); + string FormatSenseData(span); string FormatSenseData(SenseKey, Asc, int = 0);