This commit is contained in:
Uwe Seimet 2023-11-16 10:10:39 +01:00
parent 6a3fa14d68
commit c98cc6af84
4 changed files with 20 additions and 28 deletions

View File

@ -237,7 +237,7 @@ void PhaseExecutor::MsgOut() const
bool PhaseExecutor::WaitForFree() const
{
// Wait for up to 2 s
int count = 10000;
int count = 10'000;
do {
// Wait 20 ms
Sleep( { .tv_sec = 0, .tv_nsec = 20'000 });
@ -253,7 +253,7 @@ bool PhaseExecutor::WaitForFree() const
bool PhaseExecutor::WaitForBusy() const
{
// Wait for up to 2 s
int count = 10000;
int count = 10'000;
do {
// Wait 20 ms
Sleep( { .tv_sec = 0, .tv_nsec = 20'000 });

View File

@ -10,7 +10,6 @@
#include "shared/scsi.h"
#include "scsiexec/scsi_executor.h"
#include <google/protobuf/util/json_util.h>
#include <spdlog/spdlog.h>
#include <array>
#include <fstream>
#include <filesystem>
@ -18,19 +17,17 @@
using namespace std;
using namespace filesystem;
using namespace google::protobuf::util;
using namespace spdlog;
using namespace scsi_defs;
using namespace piscsi_interface;
bool ScsiExecutor::Execute(const string& filename, bool binary, PbResult& result, string& error)
string ScsiExecutor::Execute(const string& filename, bool binary, PbResult& result)
{
int length = 0;
if (binary) {
ifstream in(filename, ios::binary);
if (in.fail()) {
error = "Can't open binary input file '" + filename + "': " + strerror(errno);
return false;
return "Can't open binary input file '" + filename + "': " + strerror(errno);
}
length = file_size(filename);
@ -41,8 +38,7 @@ bool ScsiExecutor::Execute(const string& filename, bool binary, PbResult& result
else {
ifstream in(filename);
if (in.fail()) {
error = "Can't open JSON input file '" + filename + "': " + strerror(errno);
return false;
return "Can't open JSON input file '" + filename + "': " + strerror(errno);
}
stringstream buf;
@ -58,32 +54,29 @@ bool ScsiExecutor::Execute(const string& filename, bool binary, PbResult& result
cdb[8] = static_cast<uint8_t>(length);
if (!phase_executor->Execute(scsi_command::eCmdExecuteOperation, cdb, buffer, length)) {
error = "Can't execute operation";
return false;
return "Can't execute operation";
}
cdb[7] = static_cast<uint8_t>(buffer.size() >> 8);
cdb[8] = static_cast<uint8_t>(buffer.size());
if (!phase_executor->Execute(scsi_command::eCmdReadOperationResult, cdb, buffer, buffer.size())) {
error = "Can't read operation result";
return false;
return "Can't read operation result";
}
length = phase_executor->GetByteCount();
if (binary) {
if (!result.ParseFromArray(buffer.data(), length)) {
error = "Can't parse received binary protobuf data";
return false;
if (!result.ParseFromArray(buffer.data(), phase_executor->GetByteCount())) {
return "Can't parse binary protobuf data";
}
}
else {
const string json((const char*) buffer.data(), length);
const string json((const char*) buffer.data(), phase_executor->GetByteCount());
if (!JsonStringToMessage(json, &result).ok()) {
error = "Can't parse received JSON protobuf data";
return false;
return "Can't parse JSON protobuf data";
}
}
return true;
return "";
}
bool ScsiExecutor::ShutDown()

View File

@ -30,7 +30,7 @@ public:
}
~ScsiExecutor() = default;
bool Execute(const string&, bool, PbResult&, string&);
string Execute(const string&, bool, PbResult&);
bool ShutDown();
void SetTarget(int id, int lun)

View File

@ -29,7 +29,7 @@ using namespace piscsi_util;
void ScsiExec::CleanUp() const
{
if (bus != nullptr) {
if (bus) {
bus->Cleanup();
}
}
@ -54,7 +54,7 @@ bool ScsiExec::Banner(span<char*> args) const
<< " LUN is the optional target device LUN (0-" << (ControllerManager::GetScsiLunMax() - 1) << ")."
<< " Default is 0.\n"
<< " BID is the PiSCSI board ID (0-7). Default is 7.\n"
<< " INPUT_FILE is the protobuf data input file.\n"
<< " INPUT_FILE is the protobuf data input file, either in binary or in JSON protobuf format.\n"
<< " OUTPUT_FILE is the protobuf data output file. If not specified the output is always JSON"
<< " and goes to stdout.\n"
<< " LOG_LEVEL is the log level {trace|debug|info|warn|err|off}, default is 'info'.\n"
@ -82,7 +82,7 @@ bool ScsiExec::Init(bool)
bus = GPIOBUS_Factory::Create(BUS::mode_e::INITIATOR);
if (bus != nullptr) {
if (bus) {
scsi_executor = make_unique<ScsiExecutor>(*bus, initiator_id);
}
@ -198,8 +198,7 @@ int ScsiExec::run(span<char*> args, bool in_process)
}
PbResult result;
string error;
if (scsi_executor->Execute(input_filename, binary, result, error)) {
if (string error = scsi_executor->Execute(input_filename, binary, result); !error.empty()) {
cerr << "Error: " << error << endl;
CleanUp();