2022-10-04 15:23:42 +00:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
2022-12-05 17:58:23 +00:00
|
|
|
// SCSI Target Emulator PiSCSI
|
2022-10-04 15:23:42 +00:00
|
|
|
// for Raspberry Pi
|
|
|
|
//
|
2023-10-15 06:38:15 +00:00
|
|
|
// Copyright (C) 2021-2023 Uwe Seimet
|
2022-10-04 15:23:42 +00:00
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
#include "hal/bus.h"
|
|
|
|
#include "controllers/controller_manager.h"
|
2022-11-11 20:08:48 +00:00
|
|
|
#include <unordered_set>
|
2022-10-04 15:23:42 +00:00
|
|
|
|
|
|
|
class DeviceFactory;
|
|
|
|
class PrimaryDevice;
|
2023-10-15 06:38:15 +00:00
|
|
|
class StorageDevice;
|
2022-10-04 15:23:42 +00:00
|
|
|
class CommandContext;
|
|
|
|
|
2022-12-05 17:58:23 +00:00
|
|
|
class PiscsiExecutor
|
2022-10-04 15:23:42 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2023-10-30 12:57:46 +00:00
|
|
|
PiscsiExecutor(BUS& bus, ControllerManager& controller_manager) : bus(bus), controller_manager(controller_manager) {}
|
2022-12-05 17:58:23 +00:00
|
|
|
~PiscsiExecutor() = default;
|
2022-10-04 15:23:42 +00:00
|
|
|
|
2023-10-30 12:57:46 +00:00
|
|
|
// TODO At least some of these methods should be private, currently they are directly called by the unit tests
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
auto GetReservedIds() const { return reserved_ids; }
|
2022-10-04 15:23:42 +00:00
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
bool ProcessDeviceCmd(const CommandContext&, const PbDeviceDefinition&, bool);
|
|
|
|
bool ProcessCmd(const CommandContext&);
|
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;
|
2023-10-15 06:38:15 +00:00
|
|
|
bool Detach(const CommandContext&, PrimaryDevice&, bool);
|
2022-10-04 15:23:42 +00:00
|
|
|
void DetachAll();
|
|
|
|
string SetReservedIds(string_view);
|
2023-10-15 06:38:15 +00:00
|
|
|
bool ValidateImageFile(const CommandContext&, StorageDevice&, const string&) const;
|
|
|
|
string PrintCommand(const PbCommand&, const PbDeviceDefinition&) const;
|
|
|
|
string EnsureLun0(const PbCommand&) const;
|
2022-10-04 15:23:42 +00:00
|
|
|
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
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
static bool ValidateOperationAgainstDevice(const CommandContext&, const PrimaryDevice&, 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:
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
static bool CheckForReservedFile(const CommandContext&, const string&);
|
|
|
|
|
|
|
|
BUS& bus;
|
|
|
|
|
2022-11-04 07:22:32 +00:00
|
|
|
ControllerManager& controller_manager;
|
|
|
|
|
|
|
|
const DeviceFactory device_factory;
|
|
|
|
|
|
|
|
unordered_set<int> reserved_ids;
|
2022-10-04 15:23:42 +00:00
|
|
|
};
|