Skip to content

Commit

Permalink
Clean up templates
Browse files Browse the repository at this point in the history
  • Loading branch information
uweseimet committed Oct 19, 2024
1 parent 7b4fe50 commit 21e9d7f
Show file tree
Hide file tree
Showing 33 changed files with 517 additions and 534 deletions.
14 changes: 1 addition & 13 deletions cpp/base/memory_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,6 @@ void memory_util::SetInt16(vector<T> &buf, int offset, int value)
template void memory_util::SetInt16(vector<byte>&, int, int);
template void memory_util::SetInt16(vector<uint8_t>&, int, int);

template<typename T>
void memory_util::SetInt24(vector<T> &buf, int offset, int value)
{
assert(buf.size() > static_cast<size_t>(offset) + 2);

buf[offset] = static_cast<T>(value >> 16);
buf[offset + 1] = static_cast<T>(value >> 8);
buf[offset + 2] = static_cast<T>(value);
}
template void memory_util::SetInt24(vector<byte>&, int, int);
template void memory_util::SetInt24(vector<uint8_t>&, int, int);

template<typename T>
void memory_util::SetInt32(vector<T> &buf, int offset, uint32_t value)
{
Expand Down Expand Up @@ -76,7 +64,7 @@ uint64_t memory_util::GetInt64(span<const int> buf, int offset)
(static_cast<uint64_t>(buf[offset + 6]) << 8) | static_cast<uint64_t>(buf[offset + 7]);
}

void memory_util::SetInt64(vector<uint8_t> &buf, int offset, uint64_t value)
void memory_util::SetInt64(span<uint8_t> buf, int offset, uint64_t value)
{
assert(buf.size() > static_cast<size_t>(offset) + 7);

Expand Down
3 changes: 1 addition & 2 deletions cpp/base/memory_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,11 @@ int GetInt16(const auto &buf, int offset)
}

template<typename T> void SetInt16(vector<T>&, int, int);
template<typename T> void SetInt24(vector<T>&, int, int);
template<typename T> void SetInt32(vector<T>&, int, uint32_t);

int GetInt24(span<const int>, int);
int32_t GetSignedInt24(span<const int>, int);
uint32_t GetInt32(span<const int>, int);
uint64_t GetInt64(span<const int>, int);
void SetInt64(vector<uint8_t>&, int, uint64_t);
void SetInt64(span<uint8_t>, int, uint64_t);
}
21 changes: 10 additions & 11 deletions cpp/base/primary_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

#include "primary_device.h"
#include "buses/bus_factory.h"
#include "shared/s2p_exceptions.h"

using namespace memory_util;
using namespace s2p_util;
Expand All @@ -18,33 +17,33 @@ bool PrimaryDevice::Init(const param_map &params)
SetParams(params);

// Mandatory SCSI primary commands
AddCommand(scsi_command::cmd_test_unit_ready, [this]
AddCommand(scsi_command::test_unit_ready, [this]
{
TestUnitReady();
});
AddCommand(scsi_command::cmd_inquiry, [this]
AddCommand(scsi_command::inquiry, [this]
{
Inquiry();
});
AddCommand(scsi_command::cmd_report_luns, [this]
AddCommand(scsi_command::report_luns, [this]
{
ReportLuns();
});

// Optional commands supported by all device types
AddCommand(scsi_command::cmd_request_sense, [this]
AddCommand(scsi_command::request_sense, [this]
{
RequestSense();
});
AddCommand(scsi_command::cmd_reserve6, [this]
AddCommand(scsi_command::reserve6, [this]
{
ReserveUnit();
});
AddCommand(scsi_command::cmd_release6, [this]
AddCommand(scsi_command::release6, [this]
{
ReleaseUnit();
});
AddCommand(scsi_command::cmd_send_diagnostic, [this]
AddCommand(scsi_command::send_diagnostic, [this]
{
SendDiagnostic();
});
Expand Down Expand Up @@ -303,13 +302,13 @@ bool PrimaryDevice::CheckReservation(int initiator_id) const

// A reservation is valid for all commands except those excluded below
const auto cmd = static_cast<scsi_command>(GetCdbByte(0));
if (cmd == scsi_command::cmd_inquiry || cmd == scsi_command::cmd_request_sense
|| cmd == scsi_command::cmd_release6) {
if (cmd == scsi_command::inquiry || cmd == scsi_command::request_sense
|| cmd == scsi_command::release6) {
return true;
}

// PREVENT ALLOW MEDIUM REMOVAL is permitted if the prevent bit is 0
if (cmd == scsi_command::cmd_prevent_allow_medium_removal && !(GetCdbByte(4) & 0x01)) {
if (cmd == scsi_command::prevent_allow_medium_removal && !(GetCdbByte(4) & 0x01)) {
return true;
}

Expand Down
94 changes: 47 additions & 47 deletions cpp/buses/bus_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,53 +17,53 @@ using namespace spdlog;
BusFactory::BusFactory()
{
// This mapping only contains the commands supported by s2p
AddCommand(scsi_command::cmd_test_unit_ready, 6, "TEST UNIT READY");
AddCommand(scsi_command::cmd_rezero, 6, "REZERO/REWIND");
AddCommand(scsi_command::cmd_read_block_limits, 6, "READ BLOCK LIMITS");
AddCommand(scsi_command::cmd_request_sense, 6, "REQUEST SENSE");
AddCommand(scsi_command::cmd_format_unit, 6, "FORMAT UNIT/FORMAT MEDIUM");
AddCommand(scsi_command::cmd_reassign_blocks, 6, "REASSIGN BLOCKS");
AddCommand(scsi_command::cmd_read6, 6, "READ(6)/GET MESSAGE(6)");
AddCommand(scsi_command::cmd_retrieve_stats, 6, "RETRIEVE STATS");
AddCommand(scsi_command::cmd_write6, 6, "WRITE(6)/SEND MESSAGE(6)/PRINT");
AddCommand(scsi_command::cmd_seek6, 6, "SEEK(6)");
AddCommand(scsi_command::cmd_set_iface_mode, 6, "SET INTERFACE MODE");
AddCommand(scsi_command::cmd_set_mcast_addr, 6, "SET MULTICAST ADDRESS");
AddCommand(scsi_command::cmd_enable_interface, 6, "ENABLE INTERFACE");
AddCommand(scsi_command::cmd_synchronize_buffer, 6, "SYNCHRONIZE BUFFER/WRITE_FILEMARKS(6)");
AddCommand(scsi_command::cmd_space6, 6, "SPACE(6)");
AddCommand(scsi_command::cmd_inquiry, 6, "INQUIRY");
AddCommand(scsi_command::cmd_mode_select6, 6, "MODE SELECT(6)");
AddCommand(scsi_command::cmd_reserve6, 6, "RESERVE(6)");
AddCommand(scsi_command::cmd_release6, 6, "RELEASE(6)");
AddCommand(scsi_command::cmd_erase6, 6, "ERASE(6)");
AddCommand(scsi_command::cmd_mode_sense6, 6, "MODE SENSE(6)");
AddCommand(scsi_command::cmd_start_stop, 6, "START STOP UNIT/STOP PRINT");
AddCommand(scsi_command::cmd_send_diagnostic, 6, "SEND DIAGNOSTIC");
AddCommand(scsi_command::cmd_prevent_allow_medium_removal, 6, "PREVENT ALLOW MEDIUM REMOVAL");
AddCommand(scsi_command::cmd_read_capacity10, 10, "READ CAPACITY(10)");
AddCommand(scsi_command::cmd_read10, 10, "READ(10)");
AddCommand(scsi_command::cmd_write10, 10, "WRITE(10)");
AddCommand(scsi_command::cmd_seek10, 10, "SEEK(10)/LOCATE(10)");
AddCommand(scsi_command::cmd_verify10, 10, "VERIFY(10)");
AddCommand(scsi_command::cmd_synchronize_cache10, 10, "SYNCHRONIZE CACHE(10)");
AddCommand(scsi_command::cmd_read_defect_data10, 10, "READ DEFECT DATA(10)");
AddCommand(scsi_command::cmd_read_long10, 10, "READ LONG(10)");
AddCommand(scsi_command::cmd_write_long10, 10, "WRITE LONG(10)");
AddCommand(scsi_command::cmd_read_toc, 10, "READ TOC");
AddCommand(scsi_command::cmd_mode_select10, 10, "MODE SELECT(10)");
AddCommand(scsi_command::cmd_mode_sense10, 10, "MODE SENSE(10)");
AddCommand(scsi_command::cmd_read16, 16, "READ(16)");
AddCommand(scsi_command::cmd_write16, 16, "WRITE(16)");
AddCommand(scsi_command::cmd_verify16, 16, "VERIFY(16)");
AddCommand(scsi_command::cmd_read_position, 10, "READ POSITION");
AddCommand(scsi_command::cmd_synchronize_cache16, 16, "SYNCHRONIZE CACHE(16)");
AddCommand(scsi_command::cmd_locate16, 16, "LOCATE(16)");
AddCommand(scsi_command::cmd_read_capacity16_read_long16, 16, "READ CAPACITY(16)/READ LONG(16)");
AddCommand(scsi_command::cmd_write_long16, 16, "WRITE LONG(16)");
AddCommand(scsi_command::cmd_report_luns, 12, "REPORT LUNS");
AddCommand(scsi_command::cmd_execute_operation, 10, "EXECUTE OPERATION (SCSI2Pi-specific)");
AddCommand(scsi_command::cmd_receive_operation_results, 10, "RECEIVE OPERATION RESULTS (SCSI2Pi-specific)");
AddCommand(scsi_command::test_unit_ready, 6, "TEST UNIT READY");
AddCommand(scsi_command::rezero, 6, "REZERO/REWIND");
AddCommand(scsi_command::read_block_limits, 6, "READ BLOCK LIMITS");
AddCommand(scsi_command::request_sense, 6, "REQUEST SENSE");
AddCommand(scsi_command::format_unit, 6, "FORMAT UNIT/FORMAT MEDIUM");
AddCommand(scsi_command::reassign_blocks, 6, "REASSIGN BLOCKS");
AddCommand(scsi_command::read6, 6, "READ(6)/GET MESSAGE(6)");
AddCommand(scsi_command::retrieve_stats, 6, "RETRIEVE STATS");
AddCommand(scsi_command::write6, 6, "WRITE(6)/SEND MESSAGE(6)/PRINT");
AddCommand(scsi_command::seek6, 6, "SEEK(6)");
AddCommand(scsi_command::set_iface_mode, 6, "SET INTERFACE MODE");
AddCommand(scsi_command::set_mcast_addr, 6, "SET MULTICAST ADDRESS");
AddCommand(scsi_command::enable_interface, 6, "ENABLE INTERFACE");
AddCommand(scsi_command::synchronize_buffer, 6, "SYNCHRONIZE BUFFER/WRITE_FILEMARKS(6)");
AddCommand(scsi_command::space6, 6, "SPACE(6)");
AddCommand(scsi_command::inquiry, 6, "INQUIRY");
AddCommand(scsi_command::mode_select6, 6, "MODE SELECT(6)");
AddCommand(scsi_command::reserve6, 6, "RESERVE(6)");
AddCommand(scsi_command::release6, 6, "RELEASE(6)");
AddCommand(scsi_command::erase6, 6, "ERASE(6)");
AddCommand(scsi_command::mode_sense6, 6, "MODE SENSE(6)");
AddCommand(scsi_command::start_stop, 6, "START STOP UNIT/STOP PRINT");
AddCommand(scsi_command::send_diagnostic, 6, "SEND DIAGNOSTIC");
AddCommand(scsi_command::prevent_allow_medium_removal, 6, "PREVENT ALLOW MEDIUM REMOVAL");
AddCommand(scsi_command::read_capacity10, 10, "READ CAPACITY(10)");
AddCommand(scsi_command::read10, 10, "READ(10)");
AddCommand(scsi_command::write10, 10, "WRITE(10)");
AddCommand(scsi_command::seek10, 10, "SEEK(10)/LOCATE(10)");
AddCommand(scsi_command::verify10, 10, "VERIFY(10)");
AddCommand(scsi_command::synchronize_cache10, 10, "SYNCHRONIZE CACHE(10)");
AddCommand(scsi_command::read_defect_data10, 10, "READ DEFECT DATA(10)");
AddCommand(scsi_command::read_long10, 10, "READ LONG(10)");
AddCommand(scsi_command::write_long10, 10, "WRITE LONG(10)");
AddCommand(scsi_command::read_toc, 10, "READ TOC");
AddCommand(scsi_command::mode_select10, 10, "MODE SELECT(10)");
AddCommand(scsi_command::mode_sense10, 10, "MODE SENSE(10)");
AddCommand(scsi_command::read16, 16, "READ(16)");
AddCommand(scsi_command::write16, 16, "WRITE(16)");
AddCommand(scsi_command::verify16, 16, "VERIFY(16)");
AddCommand(scsi_command::read_position, 10, "READ POSITION");
AddCommand(scsi_command::synchronize_cache16, 16, "SYNCHRONIZE CACHE(16)");
AddCommand(scsi_command::locate16, 16, "LOCATE(16)");
AddCommand(scsi_command::read_capacity16_read_long16, 16, "READ CAPACITY(16)/READ LONG(16)");
AddCommand(scsi_command::write_long16, 16, "WRITE LONG(16)");
AddCommand(scsi_command::report_luns, 12, "REPORT LUNS");
AddCommand(scsi_command::execute_operation, 10, "EXECUTE OPERATION (SCSI2Pi-specific)");
AddCommand(scsi_command::receive_operation_results, 10, "RECEIVE OPERATION RESULTS (SCSI2Pi-specific)");
}

void BusFactory::AddCommand(scsi_command opcode, int byte_count, const char *name)
Expand Down
34 changes: 17 additions & 17 deletions cpp/controllers/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ void Controller::Execute()

auto device = GetDeviceForLun(GetEffectiveLun());
if (!device) {
if (opcode != scsi_command::cmd_inquiry && opcode != scsi_command::cmd_request_sense) {
if (opcode != scsi_command::inquiry && opcode != scsi_command::request_sense) {
Error(sense_key::illegal_request, asc::invalid_lun);
return;
}
Expand All @@ -165,7 +165,7 @@ void Controller::Execute()
}

// Discard pending sense data from the previous command if the current command is not REQUEST SENSE
if (opcode != scsi_command::cmd_request_sense) {
if (opcode != scsi_command::request_sense) {
SetStatus(status_code::good);
device->SetStatus(sense_key::no_sense, asc::no_additional_sense_information);
}
Expand Down Expand Up @@ -459,11 +459,11 @@ void Controller::Receive()
bool Controller::XferIn()
{
// Limited to read commands with DATA IN phase
assert(static_cast<scsi_command>(GetCdb()[0]) == scsi_command::cmd_read6 ||
static_cast<scsi_command>(GetCdb()[0]) == scsi_command::cmd_read10 ||
static_cast<scsi_command>(GetCdb()[0]) == scsi_command::cmd_read16 ||
static_cast<scsi_command>(GetCdb()[0]) == scsi_command::cmd_get_message6 ||
static_cast<scsi_command>(GetCdb()[0]) == scsi_command::cmd_read_capacity16_read_long16);
assert(static_cast<scsi_command>(GetCdb()[0]) == scsi_command::read6 ||
static_cast<scsi_command>(GetCdb()[0]) == scsi_command::read10 ||
static_cast<scsi_command>(GetCdb()[0]) == scsi_command::read16 ||
static_cast<scsi_command>(GetCdb()[0]) == scsi_command::get_message6 ||
static_cast<scsi_command>(GetCdb()[0]) == scsi_command::read_capacity16_read_long16);

try {
SetCurrentLength(GetDeviceForLun(GetEffectiveLun())->ReadData(GetBuffer()));
Expand All @@ -482,19 +482,19 @@ bool Controller::XferOut(bool pending_data)
const auto device = GetDeviceForLun(GetEffectiveLun());
try {
switch (const auto opcode = static_cast<scsi_command>(GetCdb()[0]); opcode) {
case scsi_command::cmd_mode_select6:
case scsi_command::cmd_mode_select10:
case scsi_command::mode_select6:
case scsi_command::mode_select10:
device->ModeSelect(GetCdb(), GetBuffer(), GetOffset());
break;

case scsi_command::cmd_write6:
case scsi_command::cmd_write10:
case scsi_command::cmd_write16:
case scsi_command::cmd_verify10:
case scsi_command::cmd_verify16:
case scsi_command::cmd_write_long10:
case scsi_command::cmd_write_long16:
case scsi_command::cmd_execute_operation: {
case scsi_command::write6:
case scsi_command::write10:
case scsi_command::write16:
case scsi_command::verify10:
case scsi_command::verify16:
case scsi_command::write_long10:
case scsi_command::write_long16:
case scsi_command::execute_operation: {
const auto length = device->WriteData(GetBuffer(), opcode);
if (pending_data) {
SetCurrentLength(length);
Expand Down
16 changes: 8 additions & 8 deletions cpp/devices/daynaport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,27 @@ DaynaPort::DaynaPort(int lun) : PrimaryDevice(SCDP, scsi_level::scsi_2, lun, DAY

bool DaynaPort::SetUp()
{
AddCommand(scsi_command::cmd_get_message6, [this]
AddCommand(scsi_command::get_message6, [this]
{
GetMessage6();
});
AddCommand(scsi_command::cmd_send_message6, [this]
AddCommand(scsi_command::send_message6, [this]
{
SendMessage6();
});
AddCommand(scsi_command::cmd_retrieve_stats, [this]
AddCommand(scsi_command::retrieve_stats, [this]
{
RetrieveStats();
});
AddCommand(scsi_command::cmd_set_iface_mode, [this]
AddCommand(scsi_command::set_iface_mode, [this]
{
SetInterfaceMode();
});
AddCommand(scsi_command::cmd_set_mcast_addr, [this]
AddCommand(scsi_command::set_mcast_addr, [this]
{
SetMcastAddr();
});
AddCommand(scsi_command::cmd_enable_interface, [this]
AddCommand(scsi_command::enable_interface, [this]
{
EnableInterface();
});
Expand Down Expand Up @@ -190,8 +190,8 @@ int DaynaPort::GetMessage(vector<uint8_t> &buf)
//---------------------------------------------------------------------------
int DaynaPort::WriteData(span<const uint8_t> buf, scsi_command command)
{
assert(command == scsi_command::cmd_send_message6);
if (command != scsi_command::cmd_send_message6) {
assert(command == scsi_command::send_message6);
if (command != scsi_command::send_message6) {
throw scsi_exception(sense_key::aborted_command);
}

Expand Down
Loading

0 comments on commit 21e9d7f

Please sign in to comment.