Skip to content

Commit

Permalink
Move code between controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
uweseimet committed Jan 24, 2025
1 parent b2a2a10 commit b57cf30
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 50 deletions.
24 changes: 0 additions & 24 deletions cpp/controllers/abstract_controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include <spdlog/sinks/stdout_color_sinks.h>
#include "base/primary_device.h"
#include "buses/bus.h"
#include "script_generator.h"

using namespace s2p_util;

Expand Down Expand Up @@ -44,25 +43,6 @@ void AbstractController::Reset()
}
}

void AbstractController::SetScriptGenerator(shared_ptr<ScriptGenerator> s)
{
script_generator = s;
}

void AbstractController::AddCdbToScript()
{
if (script_generator) {
script_generator->AddCdb(target_id, GetEffectiveLun(), cdb);
}
}

void AbstractController::AddDataToScript(span<const uint8_t> data) const
{
if (script_generator) {
script_generator->AddData(data);
}
}

void AbstractController::SetCurrentLength(int length)
{
if (length > static_cast<int>(buffer.size())) {
Expand Down Expand Up @@ -142,10 +122,6 @@ ShutdownMode AbstractController::ProcessOnController(int ids)
// Handle bus phases until the bus is free for the next command
}

if (script_generator) {
script_generator->WriteEol();
}

return shutdown_mode;
}

Expand Down
8 changes: 0 additions & 8 deletions cpp/controllers/abstract_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#include "shared/s2p_formatter.h"

class PrimaryDevice;
class ScriptGenerator;

using namespace spdlog;

Expand All @@ -37,8 +36,6 @@ class AbstractController : public PhaseHandler

void CleanUp() const;

void SetScriptGenerator(shared_ptr<ScriptGenerator>);

int GetInitiatorId() const
{
return initiator_id;
Expand Down Expand Up @@ -109,9 +106,6 @@ class AbstractController : public PhaseHandler

protected:

void AddCdbToScript();
void AddDataToScript(span<const uint8_t>) const;

virtual bool Process() = 0;

void SetCdbByte(int index, int value)
Expand Down Expand Up @@ -154,8 +148,6 @@ class AbstractController : public PhaseHandler

shared_ptr<logger> controller_logger;

shared_ptr<ScriptGenerator> script_generator;

// Logical units of this controller mapped to their LUN numbers
unordered_map<int, shared_ptr<PrimaryDevice>> luns;

Expand Down
30 changes: 29 additions & 1 deletion cpp/controllers/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "buses/bus.h"
#include "shared/command_meta_data.h"
#include "shared/s2p_exceptions.h"
#include "script_generator.h"

using namespace spdlog;
using namespace s2p_util;
Expand Down Expand Up @@ -51,7 +52,15 @@ bool Controller::Process()
return false;
}

return !IsBusFree();
if (IsBusFree()) {
if (script_generator) {
script_generator->WriteEol();
}

return false;
}

return true;
}

void Controller::BusFree()
Expand Down Expand Up @@ -676,3 +685,22 @@ int Controller::GetEffectiveLun() const
// Return LUN from IDENTIFY message, or return the LUN from the CDB as fallback
return identified_lun != -1 ? identified_lun : GetCdb()[1] >> 5;
}

void Controller::SetScriptGenerator(shared_ptr<ScriptGenerator> s)
{
script_generator = s;
}

void Controller::AddCdbToScript()
{
if (script_generator) {
script_generator->AddCdb(GetTargetId(), GetEffectiveLun(), GetCdb());
}
}

void Controller::AddDataToScript(span<const uint8_t> data) const
{
if (script_generator) {
script_generator->AddData(data);
}
}
8 changes: 8 additions & 0 deletions cpp/controllers/controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "abstract_controller.h"

class Bus;
class ScriptGenerator;

class Controller : public AbstractController
{
Expand Down Expand Up @@ -38,6 +39,8 @@ class Controller : public AbstractController

int GetEffectiveLun() const override;

void SetScriptGenerator(shared_ptr<ScriptGenerator>);

private:

void ResetFlags();
Expand All @@ -56,6 +59,11 @@ class Controller : public AbstractController
void RaiseDeferredError(SenseKey, Asc);
void ProvideSenseData();

void AddCdbToScript();
void AddDataToScript(span<const uint8_t>) const;

shared_ptr<ScriptGenerator> script_generator;

Bus &bus;

// The LUN from the IDENTIFY message
Expand Down
17 changes: 0 additions & 17 deletions cpp/test/abstract_controller_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,20 +141,3 @@ TEST(AbstractControllerTest, ProcessOnController)
EXPECT_CALL(controller, Process);
controller.ProcessOnController(0x06);
}

TEST(AbstractControllerTest, ScriptGenerator)
{
NiceMock<MockAbstractController> controller;
auto generator = make_shared<ScriptGenerator>();
controller.SetScriptGenerator(generator);
const string &filename = CreateTempFile();
generator->CreateFile(filename);

controller.AddCdbToScript();
array<uint8_t, 1> data = { };
controller.AddDataToScript(data);
ifstream file(filename);
string s;
getline(file, s);
EXPECT_EQ(s, "-i 0:0 -c 00:00:00:00:00:00 -d 00");
}

0 comments on commit b57cf30

Please sign in to comment.