mirror of
https://github.com/akuker/RASCSI.git
synced 2026-04-21 18:17:07 +00:00
Issues 1179 and 1182 (#1232)
* Update logging * Remove duplicate code * Update unit tests * Clean up includes * Merge ProtobufSerializer into protobuf_util namespace * Precompile regex * Add const * Add Split() convenience method, update log level/ID parsing * Move log.h to legacy folder * Elimininate gotos * Fixes for gcc 13 * Update compiler flags * Update default folder handling * Use references instead of pointers * Move code for better encapsulation * Move code * Remove unused method argument * Move device logger * Remove redundant to_string * Rename for consistency * Update handling of protobuf pointers * Simplify protobuf usage * Memory handling update * Add hasher
This commit is contained in:
@@ -1,29 +0,0 @@
|
||||
//---------------------------------------------------------------------------
|
||||
//
|
||||
// SCSI Target Emulator PiSCSI
|
||||
// for Raspberry Pi
|
||||
//
|
||||
// Powered by XM6 TypeG Technology.
|
||||
// Copyright (C) 2016-2020 GIMONS
|
||||
// Copyright (C) 2020 akuker
|
||||
//
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "spdlog/spdlog.h"
|
||||
|
||||
static const int LOGBUF_SIZE = 512;
|
||||
|
||||
#define SPDLOGWRAPPER(loglevel, ...) \
|
||||
{ \
|
||||
char logbuf[LOGBUF_SIZE]; \
|
||||
snprintf(logbuf, sizeof(logbuf), __VA_ARGS__); \
|
||||
spdlog::log(loglevel, logbuf); \
|
||||
};
|
||||
|
||||
#define LOGTRACE(...) SPDLOGWRAPPER(spdlog::level::trace, __VA_ARGS__)
|
||||
#define LOGDEBUG(...) SPDLOGWRAPPER(spdlog::level::debug, __VA_ARGS__)
|
||||
#define LOGINFO(...) SPDLOGWRAPPER(spdlog::level::info, __VA_ARGS__)
|
||||
#define LOGWARN(...) SPDLOGWRAPPER(spdlog::level::warn, __VA_ARGS__)
|
||||
#define LOGERROR(...) SPDLOGWRAPPER(spdlog::level::err, __VA_ARGS__)
|
||||
@@ -0,0 +1,75 @@
|
||||
//---------------------------------------------------------------------------
|
||||
//
|
||||
// SCSI Target Emulator PiSCSI
|
||||
// for Raspberry Pi
|
||||
//
|
||||
// Copyright (C) 2023 Uwe Seimet
|
||||
//
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
#include "network_util.h"
|
||||
#include <cstring>
|
||||
#include <ifaddrs.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <netinet/in.h>
|
||||
#include <net/if.h>
|
||||
#include <unistd.h>
|
||||
#include <netdb.h>
|
||||
#include <unistd.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
bool network_util::IsInterfaceUp(const string& interface)
|
||||
{
|
||||
ifreq ifr = {};
|
||||
strncpy(ifr.ifr_name, interface.c_str(), IFNAMSIZ - 1); //NOSONAR Using strncpy is safe
|
||||
const int fd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
|
||||
|
||||
if (!ioctl(fd, SIOCGIFFLAGS, &ifr) && (ifr.ifr_flags & IFF_UP)) {
|
||||
close(fd);
|
||||
return true;
|
||||
}
|
||||
|
||||
close(fd);
|
||||
return false;
|
||||
}
|
||||
|
||||
set<string, less<>> network_util::GetNetworkInterfaces()
|
||||
{
|
||||
set<string, less<>> network_interfaces;
|
||||
|
||||
#ifdef __linux__
|
||||
ifaddrs *addrs;
|
||||
getifaddrs(&addrs);
|
||||
ifaddrs *tmp = addrs;
|
||||
|
||||
while (tmp) {
|
||||
if (const string name = tmp->ifa_name; tmp->ifa_addr && tmp->ifa_addr->sa_family == AF_PACKET &&
|
||||
name != "lo" && name != "piscsi_bridge" && !name.starts_with("dummy") && IsInterfaceUp(name)) {
|
||||
// Only list interfaces that are up
|
||||
network_interfaces.insert(name);
|
||||
}
|
||||
|
||||
tmp = tmp->ifa_next;
|
||||
}
|
||||
|
||||
freeifaddrs(addrs);
|
||||
#endif
|
||||
|
||||
return network_interfaces;
|
||||
}
|
||||
|
||||
bool network_util::ResolveHostName(const string& host, sockaddr_in *addr)
|
||||
{
|
||||
addrinfo hints = {};
|
||||
hints.ai_family = AF_INET;
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
|
||||
if (addrinfo *result; !getaddrinfo(host.c_str(), nullptr, &hints, &result)) {
|
||||
*addr = *reinterpret_cast<sockaddr_in *>(result->ai_addr); //NOSONAR bit_cast is not supported by the bullseye compiler
|
||||
freeaddrinfo(result);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
//---------------------------------------------------------------------------
|
||||
//
|
||||
// SCSI Target Emulator PiSCSI
|
||||
// for Raspberry Pi
|
||||
//
|
||||
// Copyright (C) 2023 Uwe Seimet
|
||||
//
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <set>
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct sockaddr_in;
|
||||
|
||||
namespace network_util
|
||||
{
|
||||
bool IsInterfaceUp(const string&);
|
||||
set<string, less<>> GetNetworkInterfaces();
|
||||
bool ResolveHostName(const string&, sockaddr_in *);
|
||||
}
|
||||
@@ -36,7 +36,7 @@ class scsi_exception : public exception
|
||||
|
||||
public:
|
||||
|
||||
scsi_exception(scsi_defs::sense_key sense_key, scsi_defs::asc asc = scsi_defs::asc::NO_ADDITIONAL_SENSE_INFORMATION)
|
||||
scsi_exception(scsi_defs::sense_key sense_key, scsi_defs::asc asc = scsi_defs::asc::no_additional_sense_information)
|
||||
: sense_key(sense_key), asc(asc) {}
|
||||
~scsi_exception() override = default;
|
||||
|
||||
|
||||
+62
-21
@@ -7,13 +7,48 @@
|
||||
//
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
#include "controllers/controller_manager.h"
|
||||
#include "piscsi_version.h"
|
||||
#include "piscsi_util.h"
|
||||
#include <spdlog/spdlog.h>
|
||||
#include <cassert>
|
||||
#include <cstring>
|
||||
#include <sstream>
|
||||
#include <filesystem>
|
||||
#include <algorithm>
|
||||
|
||||
using namespace std;
|
||||
using namespace filesystem;
|
||||
|
||||
vector<string> piscsi_util::Split(const string& s, char separator, int limit)
|
||||
{
|
||||
assert(limit >= 0);
|
||||
|
||||
string component;
|
||||
vector<string> result;
|
||||
stringstream str(s);
|
||||
|
||||
while (--limit > 0 && getline(str, component, separator)) {
|
||||
result.push_back(component);
|
||||
}
|
||||
|
||||
if (!str.eof()) {
|
||||
getline(str, component);
|
||||
result.push_back(component);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
string piscsi_util::GetLocale()
|
||||
{
|
||||
const char *locale = setlocale(LC_MESSAGES, "");
|
||||
if (locale == nullptr || !strcmp(locale, "C")) {
|
||||
locale = "en";
|
||||
}
|
||||
|
||||
return locale;
|
||||
}
|
||||
|
||||
bool piscsi_util::GetAsUnsignedInt(const string& value, int& result)
|
||||
{
|
||||
@@ -35,10 +70,8 @@ bool piscsi_util::GetAsUnsignedInt(const string& value, int& result)
|
||||
return true;
|
||||
}
|
||||
|
||||
string piscsi_util::ProcessId(const string& id_spec, int max_luns, int& id, int& lun)
|
||||
string piscsi_util::ProcessId(const string& id_spec, int& id, int& lun)
|
||||
{
|
||||
assert(max_luns > 0);
|
||||
|
||||
id = -1;
|
||||
lun = -1;
|
||||
|
||||
@@ -46,27 +79,32 @@ string piscsi_util::ProcessId(const string& id_spec, int max_luns, int& id, int&
|
||||
return "Missing device ID";
|
||||
}
|
||||
|
||||
if (const size_t separator_pos = id_spec.find(COMPONENT_SEPARATOR); separator_pos == string::npos) {
|
||||
if (!GetAsUnsignedInt(id_spec, id) || id >= 8) {
|
||||
id = -1;
|
||||
const int id_max = ControllerManager::GetScsiIdMax();
|
||||
const int lun_max = ControllerManager::GetScsiLunMax();
|
||||
|
||||
return "Invalid device ID (0-7)";
|
||||
if (const auto& components = Split(id_spec, COMPONENT_SEPARATOR, 2); !components.empty()) {
|
||||
if (components.size() == 1) {
|
||||
if (!GetAsUnsignedInt(components[0], id) || id >= id_max) {
|
||||
id = -1;
|
||||
|
||||
return "Invalid device ID (0-" + to_string(ControllerManager::GetScsiIdMax() - 1) + ")";
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
lun = 0;
|
||||
}
|
||||
else if (!GetAsUnsignedInt(id_spec.substr(0, separator_pos), id) || id > 7 ||
|
||||
!GetAsUnsignedInt(id_spec.substr(separator_pos + 1), lun) || lun >= max_luns) {
|
||||
id = -1;
|
||||
lun = -1;
|
||||
if (!GetAsUnsignedInt(components[0], id) || id >= id_max || !GetAsUnsignedInt(components[1], lun) || lun >= lun_max) {
|
||||
id = -1;
|
||||
lun = -1;
|
||||
|
||||
return "Invalid LUN (0-" + to_string(max_luns - 1) + ")";
|
||||
return "Invalid LUN (0-" + to_string(lun_max - 1) + ")";
|
||||
}
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
string piscsi_util::Banner(const string& app)
|
||||
string piscsi_util::Banner(string_view app)
|
||||
{
|
||||
ostringstream s;
|
||||
|
||||
@@ -79,15 +117,18 @@ string piscsi_util::Banner(const string& app)
|
||||
return s.str();
|
||||
}
|
||||
|
||||
string piscsi_util::GetExtensionLowerCase(const string& filename)
|
||||
string piscsi_util::GetExtensionLowerCase(string_view filename)
|
||||
{
|
||||
string ext;
|
||||
if (const size_t separator = filename.rfind('.'); separator != string::npos) {
|
||||
ext = filename.substr(separator + 1);
|
||||
}
|
||||
transform(ext.begin(), ext.end(), ext.begin(), [](unsigned char c){ return std::tolower(c); });
|
||||
ranges::transform(path(filename).extension().string(), back_inserter(ext), ::tolower);
|
||||
|
||||
return ext;
|
||||
// Remove the leading dot
|
||||
return ext.empty() ? "" : ext.substr(1);
|
||||
}
|
||||
|
||||
void piscsi_util::LogErrno(const string& msg)
|
||||
{
|
||||
spdlog::error(errno ? msg + ": " + string(strerror(errno)) : msg);
|
||||
}
|
||||
|
||||
// Pin the thread to a specific CPU
|
||||
|
||||
@@ -3,13 +3,16 @@
|
||||
// SCSI Target Emulator PiSCSI
|
||||
// for Raspberry Pi
|
||||
//
|
||||
// Copyright (C) 2021-2022 Uwe Seimet
|
||||
// Copyright (C) 2021-2023 Uwe Seimet
|
||||
//
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <climits>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
@@ -18,11 +21,38 @@ namespace piscsi_util
|
||||
// Separator for compound options like ID:LUN
|
||||
static const char COMPONENT_SEPARATOR = ':';
|
||||
|
||||
bool GetAsUnsignedInt(const string&, int&);
|
||||
string ProcessId(const string&, int, int&, int&);
|
||||
string Banner(const string&);
|
||||
struct StringHash {
|
||||
using is_transparent = void;
|
||||
|
||||
string GetExtensionLowerCase(const string&);
|
||||
size_t operator()(string_view sv) const {
|
||||
hash<string_view> hasher;
|
||||
return hasher(sv);
|
||||
}
|
||||
};
|
||||
|
||||
string Join(const auto& collection, const string_view separator = ", ") {
|
||||
ostringstream s;
|
||||
|
||||
for (const auto& element : collection) {
|
||||
if (s.tellp()) {
|
||||
s << separator;
|
||||
}
|
||||
|
||||
s << element;
|
||||
}
|
||||
|
||||
return s.str();
|
||||
}
|
||||
|
||||
vector<string> Split(const string&, char, int = INT_MAX);
|
||||
string GetLocale();
|
||||
bool GetAsUnsignedInt(const string&, int&);
|
||||
string ProcessId(const string&, int&, int&);
|
||||
string Banner(string_view);
|
||||
|
||||
string GetExtensionLowerCase(string_view);
|
||||
|
||||
void LogErrno(const string&);
|
||||
|
||||
void FixCpu(int);
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
// for Raspberry Pi
|
||||
//
|
||||
// Copyright (C) 2020 akuker
|
||||
// [ Define the version string ]
|
||||
//
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
@@ -14,8 +13,8 @@
|
||||
|
||||
// The following should be updated for each release
|
||||
const int piscsi_major_version = 23; // Last two digits of year
|
||||
const int piscsi_minor_version = 5; // Month
|
||||
const int piscsi_patch_version = -1; // Patch number - increment for each update
|
||||
const int piscsi_minor_version = 10; // Month
|
||||
const int piscsi_patch_version = -1; // Patch number - increment for each update
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
@@ -1,80 +0,0 @@
|
||||
//---------------------------------------------------------------------------
|
||||
//
|
||||
// SCSI Target Emulator PiSCSI
|
||||
// for Raspberry Pi
|
||||
//
|
||||
// Copyright (C) 2022 Uwe Seimet
|
||||
//
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
#include "shared/protobuf_serializer.h"
|
||||
#include "shared/piscsi_exceptions.h"
|
||||
#include "generated/piscsi_interface.pb.h"
|
||||
#include <unistd.h>
|
||||
|
||||
using namespace std;
|
||||
using namespace piscsi_interface;
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
//
|
||||
// Serialize/Deserialize protobuf message: Length followed by the actual data.
|
||||
// A little endian platform is assumed.
|
||||
//
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
void ProtobufSerializer::SerializeMessage(int fd, const google::protobuf::Message& message) const
|
||||
{
|
||||
string data;
|
||||
message.SerializeToString(&data);
|
||||
|
||||
// Write the size of the protobuf data as a header
|
||||
auto size = static_cast<int32_t>(data.length());
|
||||
if (write(fd, &size, sizeof(size)) != sizeof(size)) {
|
||||
throw io_exception("Can't write protobuf message header");
|
||||
}
|
||||
|
||||
// Write the actual protobuf data
|
||||
if (write(fd, data.data(), size) != size) {
|
||||
throw io_exception("Can't write protobuf message data");
|
||||
}
|
||||
}
|
||||
|
||||
void ProtobufSerializer::DeserializeMessage(int fd, google::protobuf::Message& message) const
|
||||
{
|
||||
// Read the header with the size of the protobuf data
|
||||
vector<byte> header_buf(4);
|
||||
if (ReadBytes(fd, header_buf) < header_buf.size()) {
|
||||
throw io_exception("Invalid protobuf message header");
|
||||
}
|
||||
|
||||
const int size = (static_cast<int>(header_buf[3]) << 24) + (static_cast<int>(header_buf[2]) << 16)
|
||||
+ (static_cast<int>(header_buf[1]) << 8) + static_cast<int>(header_buf[0]);
|
||||
if (size < 0) {
|
||||
throw io_exception("Invalid protobuf message header");
|
||||
}
|
||||
|
||||
// Read the binary protobuf data
|
||||
vector<byte> data_buf(size);
|
||||
if (ReadBytes(fd, data_buf) < data_buf.size()) {
|
||||
throw io_exception("Missing protobuf message data");
|
||||
}
|
||||
|
||||
// Create protobuf message
|
||||
string data((const char *)data_buf.data(), size);
|
||||
message.ParseFromString(data);
|
||||
}
|
||||
|
||||
size_t ProtobufSerializer::ReadBytes(int fd, vector<byte>& buf) const
|
||||
{
|
||||
size_t offset = 0;
|
||||
while (offset < buf.size()) {
|
||||
const ssize_t len = read(fd, &buf.data()[offset], buf.size() - offset);
|
||||
if (len <= 0) {
|
||||
return len;
|
||||
}
|
||||
|
||||
offset += len;
|
||||
}
|
||||
|
||||
return offset;
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
//---------------------------------------------------------------------------
|
||||
//
|
||||
// SCSI Target Emulator PiSCSI
|
||||
// for Raspberry Pi
|
||||
//
|
||||
// Copyright (C) 2022 Uwe Seimet
|
||||
//
|
||||
// Helper for serializing/deserializing protobuf messages
|
||||
//
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "google/protobuf/message.h"
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class ProtobufSerializer
|
||||
{
|
||||
public:
|
||||
|
||||
ProtobufSerializer() = default;
|
||||
~ProtobufSerializer() = default;
|
||||
|
||||
void SerializeMessage(int, const google::protobuf::Message&) const;
|
||||
void DeserializeMessage(int, google::protobuf::Message&) const;
|
||||
size_t ReadBytes(int, vector<byte>&) const;
|
||||
};
|
||||
+100
-72
@@ -3,15 +3,18 @@
|
||||
// SCSI Target Emulator PiSCSI
|
||||
// for Raspberry Pi
|
||||
//
|
||||
// Copyright (C) 2021-2022 Uwe Seimet
|
||||
// Copyright (C) 2021-2023 Uwe Seimet
|
||||
//
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
#include "log.h"
|
||||
#include "shared/piscsi_exceptions.h"
|
||||
#include "piscsi_util.h"
|
||||
#include "protobuf_serializer.h"
|
||||
#include "protobuf_util.h"
|
||||
#include <unistd.h>
|
||||
#include <sstream>
|
||||
#include <array>
|
||||
#include <vector>
|
||||
|
||||
|
||||
using namespace std;
|
||||
using namespace piscsi_util;
|
||||
@@ -32,61 +35,21 @@ void protobuf_util::ParseParameters(PbDeviceDefinition& device, const string& pa
|
||||
return;
|
||||
}
|
||||
|
||||
stringstream ss(params);
|
||||
string p;
|
||||
while (getline(ss, p, COMPONENT_SEPARATOR)) {
|
||||
if (!p.empty()) {
|
||||
const size_t separator_pos = p.find(KEY_VALUE_SEPARATOR);
|
||||
if (separator_pos != string::npos) {
|
||||
SetParam(device, p.substr(0, separator_pos), string_view(p).substr(separator_pos + 1));
|
||||
}
|
||||
for (const auto& p : Split(params, COMPONENT_SEPARATOR)) {
|
||||
if (const auto& param = Split(p, KEY_VALUE_SEPARATOR, 2); param.size() == 2) {
|
||||
SetParam(device, param[0], param[1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
string protobuf_util::GetParam(const PbCommand& command, const string& key)
|
||||
{
|
||||
const auto& it = command.params().find(key);
|
||||
return it != command.params().end() ? it->second : "";
|
||||
}
|
||||
|
||||
string protobuf_util::GetParam(const PbDeviceDefinition& device, const string& key)
|
||||
{
|
||||
const auto& it = device.params().find(key);
|
||||
return it != device.params().end() ? it->second : "";
|
||||
}
|
||||
|
||||
void protobuf_util::SetParam(PbCommand& command, const string& key, string_view value)
|
||||
{
|
||||
if (!key.empty() && !value.empty()) {
|
||||
auto& map = *command.mutable_params();
|
||||
map[key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
void protobuf_util::SetParam(PbDevice& device, const string& key, string_view value)
|
||||
{
|
||||
if (!key.empty() && !value.empty()) {
|
||||
auto& map = *device.mutable_params();
|
||||
map[key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
void protobuf_util::SetParam(PbDeviceDefinition& device, const string& key, string_view value)
|
||||
{
|
||||
if (!key.empty() && !value.empty()) {
|
||||
auto& map = *device.mutable_params();
|
||||
map[key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
void protobuf_util::SetPatternParams(PbCommand& command, string_view patterns)
|
||||
void protobuf_util::SetPatternParams(PbCommand& command, const string& patterns)
|
||||
{
|
||||
string folder_pattern;
|
||||
string file_pattern;
|
||||
if (const size_t separator_pos = patterns.find(COMPONENT_SEPARATOR); separator_pos != string::npos) {
|
||||
folder_pattern = patterns.substr(0, separator_pos);
|
||||
file_pattern = patterns.substr(separator_pos + 1);
|
||||
|
||||
if (const auto& components = Split(patterns, ':', 2); components.size() == 2) {
|
||||
folder_pattern = components[0];
|
||||
file_pattern = components[1];
|
||||
}
|
||||
else {
|
||||
file_pattern = patterns;
|
||||
@@ -98,40 +61,40 @@ void protobuf_util::SetPatternParams(PbCommand& command, string_view patterns)
|
||||
|
||||
void protobuf_util::SetProductData(PbDeviceDefinition& device, const string& data)
|
||||
{
|
||||
string name = data;
|
||||
const auto& components = Split(data, COMPONENT_SEPARATOR, 3);
|
||||
switch (components.size()) {
|
||||
case 3:
|
||||
device.set_revision(components[2]);
|
||||
[[fallthrough]];
|
||||
|
||||
if (size_t separator_pos = name.find(COMPONENT_SEPARATOR); separator_pos != string::npos) {
|
||||
device.set_vendor(name.substr(0, separator_pos));
|
||||
name = name.substr(separator_pos + 1);
|
||||
separator_pos = name.find(COMPONENT_SEPARATOR);
|
||||
if (separator_pos != string::npos) {
|
||||
device.set_product(name.substr(0, separator_pos));
|
||||
device.set_revision(name.substr(separator_pos + 1));
|
||||
}
|
||||
else {
|
||||
device.set_product(name);
|
||||
}
|
||||
}
|
||||
else {
|
||||
device.set_vendor(name);
|
||||
case 2:
|
||||
device.set_product(components[1]);
|
||||
[[fallthrough]];
|
||||
|
||||
case 1:
|
||||
device.set_vendor(components[0]);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
string protobuf_util::SetIdAndLun(PbDeviceDefinition& device, const string& value, int max_luns)
|
||||
string protobuf_util::SetIdAndLun(PbDeviceDefinition& device, const string& value)
|
||||
{
|
||||
int id;
|
||||
int lun;
|
||||
if (const string error = ProcessId(value, max_luns, id, lun); !error.empty()) {
|
||||
if (const string error = ProcessId(value, id, lun); !error.empty()) {
|
||||
return error;
|
||||
}
|
||||
|
||||
device.set_id(id);
|
||||
device.set_unit(lun);
|
||||
device.set_unit(lun != -1 ? lun : 0);
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
string protobuf_util::ListDevices(const list<PbDevice>& pb_devices)
|
||||
string protobuf_util::ListDevices(const vector<PbDevice>& pb_devices)
|
||||
{
|
||||
if (pb_devices.empty()) {
|
||||
return "No devices currently attached.\n";
|
||||
@@ -142,8 +105,8 @@ string protobuf_util::ListDevices(const list<PbDevice>& pb_devices)
|
||||
<< "| ID | LUN | TYPE | IMAGE FILE\n"
|
||||
<< "+----+-----+------+-------------------------------------\n";
|
||||
|
||||
list<PbDevice> devices = pb_devices;
|
||||
devices.sort([](const auto& a, const auto& b) { return a.id() < b.id() || a.unit() < b.unit(); });
|
||||
vector<PbDevice> devices = pb_devices;
|
||||
ranges::sort(devices, [](const auto& a, const auto& b) { return a.id() < b.id() || a.unit() < b.unit(); });
|
||||
|
||||
for (const auto& device : devices) {
|
||||
string filename;
|
||||
@@ -179,3 +142,68 @@ string protobuf_util::ListDevices(const list<PbDevice>& pb_devices)
|
||||
|
||||
return s.str();
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
//
|
||||
// Serialize/Deserialize protobuf message: Length followed by the actual data.
|
||||
// A little endian platform is assumed.
|
||||
//
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
void protobuf_util::SerializeMessage(int fd, const google::protobuf::Message& message)
|
||||
{
|
||||
const string data = message.SerializeAsString();
|
||||
|
||||
// Write the size of the protobuf data as a header
|
||||
const auto size = static_cast<int32_t>(data.length());
|
||||
if (write(fd, &size, sizeof(size)) != sizeof(size)) {
|
||||
throw io_exception("Can't write protobuf message size");
|
||||
}
|
||||
|
||||
// Write the actual protobuf data
|
||||
if (write(fd, data.data(), size) != size) {
|
||||
throw io_exception("Can't write protobuf message data");
|
||||
}
|
||||
}
|
||||
|
||||
void protobuf_util::DeserializeMessage(int fd, google::protobuf::Message& message)
|
||||
{
|
||||
// Read the header with the size of the protobuf data
|
||||
array<byte, sizeof(int32_t)> header_buf;
|
||||
if (ReadBytes(fd, header_buf) < header_buf.size()) {
|
||||
throw io_exception("Can't read protobuf message size");
|
||||
}
|
||||
|
||||
const int size = (static_cast<int>(header_buf[3]) << 24) + (static_cast<int>(header_buf[2]) << 16)
|
||||
+ (static_cast<int>(header_buf[1]) << 8) + static_cast<int>(header_buf[0]);
|
||||
if (size < 0) {
|
||||
throw io_exception("Invalid protobuf message size");
|
||||
}
|
||||
|
||||
// Read the binary protobuf data
|
||||
vector<byte> data_buf(size);
|
||||
if (ReadBytes(fd, data_buf) != data_buf.size()) {
|
||||
throw io_exception("Invalid protobuf message data");
|
||||
}
|
||||
|
||||
message.ParseFromArray(data_buf.data(), size);
|
||||
}
|
||||
|
||||
size_t protobuf_util::ReadBytes(int fd, span<byte> buf)
|
||||
{
|
||||
size_t offset = 0;
|
||||
while (offset < buf.size()) {
|
||||
const auto len = read(fd, &buf.data()[offset], buf.size() - offset);
|
||||
if (len == -1) {
|
||||
throw io_exception("Read error: " + string(strerror(errno)));
|
||||
}
|
||||
|
||||
if (!len) {
|
||||
break;
|
||||
}
|
||||
|
||||
offset += len;
|
||||
}
|
||||
|
||||
return offset;
|
||||
}
|
||||
|
||||
+25
-12
@@ -3,7 +3,7 @@
|
||||
// SCSI Target Emulator PiSCSI
|
||||
// for Raspberry Pi
|
||||
//
|
||||
// Copyright (C) 2021-2022 Uwe Seimet
|
||||
// Copyright (C) 2021-2023 Uwe Seimet
|
||||
//
|
||||
// Helper methods for setting up/evaluating protobuf messages
|
||||
//
|
||||
@@ -11,10 +11,10 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "google/protobuf/message.h"
|
||||
#include "generated/piscsi_interface.pb.h"
|
||||
#include <string>
|
||||
#include <list>
|
||||
#include <span>
|
||||
#include <vector>
|
||||
#include "generated/piscsi_interface.pb.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace piscsi_interface;
|
||||
@@ -23,14 +23,27 @@ namespace protobuf_util
|
||||
{
|
||||
static const char KEY_VALUE_SEPARATOR = '=';
|
||||
|
||||
string GetParam(const auto& item, const string& key)
|
||||
{
|
||||
const auto& it = item.params().find(key);
|
||||
return it != item.params().end() ? it->second : "";
|
||||
}
|
||||
|
||||
void SetParam(auto& item, const string& key, string_view value)
|
||||
{
|
||||
if (!key.empty() && !value.empty()) {
|
||||
auto& map = *item.mutable_params();
|
||||
map[key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
void ParseParameters(PbDeviceDefinition&, const string&);
|
||||
string GetParam(const PbCommand&, const string&);
|
||||
string GetParam(const PbDeviceDefinition&, const string&);
|
||||
void SetParam(PbCommand&, const string&, string_view);
|
||||
void SetParam(PbDevice&, const string&, string_view);
|
||||
void SetParam(PbDeviceDefinition&, const string&, string_view);
|
||||
void SetPatternParams(PbCommand&, string_view);
|
||||
void SetPatternParams(PbCommand&, const string&);
|
||||
void SetProductData(PbDeviceDefinition&, const string&);
|
||||
string SetIdAndLun(PbDeviceDefinition&, const string&, int);
|
||||
string ListDevices(const list<PbDevice>&);
|
||||
string SetIdAndLun(PbDeviceDefinition&, const string&);
|
||||
string ListDevices(const vector<PbDevice>&);
|
||||
|
||||
void SerializeMessage(int, const google::protobuf::Message&);
|
||||
void DeserializeMessage(int, google::protobuf::Message&);
|
||||
size_t ReadBytes(int, span<byte>);
|
||||
}
|
||||
|
||||
+57
-47
@@ -1,33 +1,39 @@
|
||||
//---------------------------------------------------------------------------
|
||||
//
|
||||
// X68000 EMULATOR "XM6"
|
||||
// X68000 EMULATOR "XM6"
|
||||
//
|
||||
// Copyright (C) 2001-2006 PI.(ytanaka@ipc-tokai.or.jp)
|
||||
// Copyright (C) 2014-2020 GIMONS
|
||||
// Copyright (C) 2001-2006 PI.(ytanaka@ipc-tokai.or.jp)
|
||||
// Copyright (C) 2014-2020 GIMONS
|
||||
// Copyright (C) 2021-2023 Uwe Seimet
|
||||
//
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <span>
|
||||
#include <unordered_map>
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
// Command Descriptor Block
|
||||
using cdb_t = span<const int>;
|
||||
|
||||
namespace scsi_defs
|
||||
{
|
||||
enum class scsi_level : int {
|
||||
SCSI_1_CCS = 1,
|
||||
SCSI_2 = 2,
|
||||
SPC = 3,
|
||||
SPC_2 = 4,
|
||||
SPC_3 = 5,
|
||||
SPC_4 = 6,
|
||||
SPC_5 = 7,
|
||||
SPC_6 = 8
|
||||
enum class scsi_level {
|
||||
scsi_1_ccs = 1,
|
||||
scsi_2 = 2,
|
||||
spc = 3,
|
||||
spc_2 = 4,
|
||||
spc_3 = 5,
|
||||
spc_4 = 6,
|
||||
spc_5 = 7,
|
||||
spc_6 = 8
|
||||
};
|
||||
|
||||
// Phase definitions
|
||||
enum class phase_t : int {
|
||||
// Phase definitions
|
||||
enum class phase_t {
|
||||
busfree,
|
||||
arbitration,
|
||||
selection,
|
||||
@@ -41,16 +47,16 @@ enum class phase_t : int {
|
||||
reserved
|
||||
};
|
||||
|
||||
enum class device_type : int {
|
||||
DIRECT_ACCESS = 0,
|
||||
PRINTER = 2,
|
||||
PROCESSOR = 3,
|
||||
CD_ROM = 5,
|
||||
OPTICAL_MEMORY = 7,
|
||||
COMMUNICATIONS = 9
|
||||
enum class device_type {
|
||||
direct_access = 0,
|
||||
printer = 2,
|
||||
processor = 3,
|
||||
cd_rom = 5,
|
||||
optical_memory = 7,
|
||||
communications = 9
|
||||
};
|
||||
|
||||
enum class scsi_command : int {
|
||||
enum class scsi_command {
|
||||
eCmdTestUnitReady = 0x00,
|
||||
eCmdRezero = 0x01,
|
||||
eCmdRequestSense = 0x03,
|
||||
@@ -104,35 +110,39 @@ enum class scsi_command : int {
|
||||
eCmdReportLuns = 0xA0
|
||||
};
|
||||
|
||||
enum class status : int { GOOD = 0x00, CHECK_CONDITION = 0x02, RESERVATION_CONFLICT = 0x18 };
|
||||
|
||||
enum class sense_key : int {
|
||||
NO_SENSE = 0x00,
|
||||
NOT_READY = 0x02,
|
||||
MEDIUM_ERROR = 0x03,
|
||||
ILLEGAL_REQUEST = 0x05,
|
||||
UNIT_ATTENTION = 0x06,
|
||||
DATA_PROTECT = 0x07,
|
||||
ABORTED_COMMAND = 0x0b
|
||||
enum class status {
|
||||
good = 0x00,
|
||||
check_condition = 0x02,
|
||||
reservation_conflict = 0x18
|
||||
};
|
||||
|
||||
enum class asc : int {
|
||||
NO_ADDITIONAL_SENSE_INFORMATION = 0x00,
|
||||
WRITE_FAULT = 0x03,
|
||||
READ_FAULT = 0x11,
|
||||
INVALID_COMMAND_OPERATION_CODE = 0x20,
|
||||
LBA_OUT_OF_RANGE = 0x21,
|
||||
INVALID_FIELD_IN_CDB = 0x24,
|
||||
INVALID_LUN = 0x25,
|
||||
INVALID_FIELD_IN_PARAMETER_LIST = 0x26,
|
||||
WRITE_PROTECTED = 0x27,
|
||||
NOT_READY_TO_READY_CHANGE = 0x28,
|
||||
POWER_ON_OR_RESET = 0x29,
|
||||
MEDIUM_NOT_PRESENT = 0x3a,
|
||||
LOAD_OR_EJECT_FAILED = 0x53
|
||||
enum class sense_key {
|
||||
no_sense = 0x00,
|
||||
not_ready = 0x02,
|
||||
medium_error = 0x03,
|
||||
illegal_request = 0x05,
|
||||
unit_attention = 0x06,
|
||||
data_protect = 0x07,
|
||||
aborted_command = 0x0b
|
||||
};
|
||||
|
||||
static const unordered_map<scsi_command, pair<int, const char *>> command_mapping = {
|
||||
enum class asc {
|
||||
no_additional_sense_information = 0x00,
|
||||
write_fault = 0x03,
|
||||
read_fault = 0x11,
|
||||
invalid_command_operation_code = 0x20,
|
||||
lba_out_of_range = 0x21,
|
||||
invalid_field_in_cdb = 0x24,
|
||||
invalid_lun = 0x25,
|
||||
invalid_field_in_parameter_list = 0x26,
|
||||
write_protected = 0x27,
|
||||
not_ready_to_ready_change = 0x28,
|
||||
power_on_or_reset = 0x29,
|
||||
medium_not_present = 0x3a,
|
||||
load_or_eject_failed = 0x53
|
||||
};
|
||||
|
||||
static const unordered_map<scsi_command, pair<int, string>> command_mapping = {
|
||||
{scsi_command::eCmdTestUnitReady, make_pair(6, "TestUnitReady")},
|
||||
{scsi_command::eCmdRezero, make_pair(6, "Rezero")},
|
||||
{scsi_command::eCmdRequestSense, make_pair(6, "RequestSense")},
|
||||
|
||||
Reference in New Issue
Block a user