2022-10-04 15:23:42 +00:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// SCSI Target Emulator RaSCSI Reloaded
|
|
|
|
// for Raspberry Pi
|
|
|
|
//
|
|
|
|
// Copyright (C) 2021-2022 Uwe Seimet
|
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2022-11-11 20:08:48 +00:00
|
|
|
#include "spdlog/spdlog.h"
|
2022-11-10 06:44:06 +00:00
|
|
|
#include "shared/protobuf_serializer.h"
|
2022-11-04 07:22:32 +00:00
|
|
|
#include "rascsi/rascsi_response.h"
|
2022-11-11 20:08:48 +00:00
|
|
|
#include <unordered_set>
|
|
|
|
#include <unordered_map>
|
2022-10-04 15:23:42 +00:00
|
|
|
|
|
|
|
class RascsiImage;
|
|
|
|
class DeviceFactory;
|
|
|
|
class ControllerManager;
|
|
|
|
class PrimaryDevice;
|
|
|
|
class CommandContext;
|
|
|
|
|
2022-11-11 20:08:48 +00:00
|
|
|
using namespace spdlog;
|
|
|
|
|
2022-10-04 15:23:42 +00:00
|
|
|
class RascsiExecutor
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2022-11-04 07:22:32 +00:00
|
|
|
RascsiExecutor(RascsiImage& rascsi_image, ControllerManager& controller_manager)
|
|
|
|
: rascsi_image(rascsi_image), controller_manager(controller_manager) {}
|
2022-10-04 15:23:42 +00:00
|
|
|
~RascsiExecutor() = default;
|
|
|
|
|
|
|
|
unordered_set<int> GetReservedIds() const { return reserved_ids; }
|
|
|
|
|
2022-10-23 19:51:39 +00:00
|
|
|
bool ProcessDeviceCmd(const CommandContext&, const PbDeviceDefinition&, const PbCommand&, bool);
|
2022-10-04 15:23:42 +00:00
|
|
|
bool ProcessCmd(const CommandContext&, const PbCommand&);
|
|
|
|
bool SetLogLevel(const string&) const;
|
2022-11-02 06:36:25 +00:00
|
|
|
bool Start(PrimaryDevice&, bool) const;
|
|
|
|
bool Stop(PrimaryDevice&, bool) const;
|
|
|
|
bool Eject(PrimaryDevice&, bool) const;
|
|
|
|
bool Protect(PrimaryDevice&, bool) const;
|
|
|
|
bool Unprotect(PrimaryDevice&, bool) const;
|
2022-10-04 15:23:42 +00:00
|
|
|
bool Attach(const CommandContext&, const PbDeviceDefinition&, bool);
|
2022-11-02 06:36:25 +00:00
|
|
|
bool Insert(const CommandContext&, const PbDeviceDefinition&, const shared_ptr<PrimaryDevice>&, bool) const;
|
|
|
|
bool Detach(const CommandContext&, const shared_ptr<PrimaryDevice>&, bool) const;
|
2022-10-04 15:23:42 +00:00
|
|
|
void DetachAll();
|
|
|
|
bool ShutDown(const CommandContext&, const string&);
|
|
|
|
string SetReservedIds(string_view);
|
2022-11-02 06:36:25 +00:00
|
|
|
bool ValidateImageFile(const CommandContext&, StorageDevice&, const string&, string&) const;
|
2022-10-04 15:23:42 +00:00
|
|
|
void PrintCommand(const PbCommand&, const PbDeviceDefinition&, bool) const;
|
|
|
|
string ValidateLunSetup(const PbCommand&) const;
|
|
|
|
bool VerifyExistingIdAndLun(const CommandContext&, int, int) const;
|
|
|
|
shared_ptr<PrimaryDevice> CreateDevice(const CommandContext&, const PbDeviceType, int, const string&) const;
|
2022-10-23 19:51:39 +00:00
|
|
|
bool SetSectorSize(const CommandContext&, shared_ptr<PrimaryDevice>, int) const;
|
2022-10-04 15:23:42 +00:00
|
|
|
|
2022-11-02 06:36:25 +00:00
|
|
|
static bool ValidateOperationAgainstDevice(const CommandContext&, const PrimaryDevice&, const PbOperation&);
|
2022-10-04 15:23:42 +00:00
|
|
|
static bool ValidateIdAndLun(const CommandContext&, int, int);
|
2022-11-02 06:36:25 +00:00
|
|
|
static bool SetProductData(const CommandContext&, const PbDeviceDefinition&, PrimaryDevice&);
|
2022-10-04 15:23:42 +00:00
|
|
|
|
2022-11-04 07:22:32 +00:00
|
|
|
private:
|
|
|
|
|
|
|
|
const RascsiResponse rascsi_response;
|
|
|
|
|
|
|
|
RascsiImage& rascsi_image;
|
|
|
|
|
|
|
|
ControllerManager& controller_manager;
|
|
|
|
|
|
|
|
const DeviceFactory device_factory;
|
|
|
|
|
|
|
|
const ProtobufSerializer serializer;
|
|
|
|
|
|
|
|
unordered_set<int> reserved_ids;
|
2022-11-11 20:08:48 +00:00
|
|
|
|
|
|
|
static inline const unordered_map<string, level::level_enum> log_level_mapping = {
|
|
|
|
{ "trace", level::trace },
|
|
|
|
{ "debug", level::debug },
|
|
|
|
{ "info", level::info },
|
|
|
|
{ "warn", level::warn },
|
|
|
|
{ "err", level::err },
|
|
|
|
{ "off", level::off }
|
|
|
|
};
|
2022-10-04 15:23:42 +00:00
|
|
|
};
|