Skip to content

Commit

Permalink
Support property file list and BlueSCSI compatibility mode (#23, #24)
Browse files Browse the repository at this point in the history
  • Loading branch information
uweseimet committed Jan 17, 2024
1 parent ca9a84a commit 827976d
Show file tree
Hide file tree
Showing 29 changed files with 411 additions and 144 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Build and test on code change
name: Build and test after code change

on:
workflow_call:
Expand Down
32 changes: 21 additions & 11 deletions cpp/base/property_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
using namespace std;
using namespace s2p_util;

void PropertyHandler::Init(const string &f, const property_map &cmd_properties)
void PropertyHandler::Init(const string &filenames, const property_map &cmd_properties)
{
// Clearing the property cache helps with testing because Init() can be called for different files
property_cache.clear();
Expand All @@ -26,18 +26,33 @@ void PropertyHandler::Init(const string &f, const property_map &cmd_properties)
property_cache[PropertyHandler::SCAN_DEPTH] = "1";
property_cache[PropertyHandler::TOKEN_FILE] = "";

const string filename = f.empty() ? GetHomeDir() + "/" + DEFAULT_PROPERTIES_FILE : f;
if (filenames.empty()) {
ParsePropertyFile(GetHomeDir() + "/" + DEFAULT_PROPERTY_FILE, true);
}
else {
for (const auto &filename : Split(filenames, ',')) {
ParsePropertyFile(filename, false);
}
}

// Merge properties from property files and from the command line
for (const auto& [k, v] : cmd_properties) {
property_cache[k] = v;
}
}

void PropertyHandler::ParsePropertyFile(const string &filename, bool default_file)
{
ifstream property_file(filename);
// Only report an error if an explicitly specified file is missing
if (property_file.fail() && !f.empty()) {
throw parser_exception(fmt::format("No properties file '{}'", filename));
if (property_file.fail() && !default_file) {
// Only report an error if an explicitly specified file is missing
throw parser_exception(fmt::format("No property file '{}'", filename));
}

string property;
while (getline(property_file, property)) {
if (property_file.fail()) {
throw parser_exception(fmt::format("Error reading from properties file '{}'", filename));
throw parser_exception(fmt::format("Error reading from property file '{}'", filename));
}

if (!property.empty() && !property.starts_with("#")) {
Expand All @@ -49,11 +64,6 @@ void PropertyHandler::Init(const string &f, const property_map &cmd_properties)
property_cache[kv[0]] = kv[1];
}
}

// Merge properties from property file and from command line
for (const auto& [k, v] : cmd_properties) {
property_cache[k] = v;
}
}

string PropertyHandler::GetProperty(const string &key) const
Expand Down
5 changes: 3 additions & 2 deletions cpp/base/property_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class PropertyHandler
inline static const string LOG_LEVEL = "log_level";
inline static const string MODE_PAGE = "mode_page";
inline static const string PORT = "port";
inline static const string PROPERTY_FILE = "property_file";
inline static const string PROPERTY_FILES = "property_files";
inline static const string RESERVED_IDS = "reserved_ids";
inline static const string SASI = "sasi";
inline static const string SCAN_DEPTH = "scan_depth";
Expand All @@ -44,6 +44,7 @@ class PropertyHandler
{
return property_cache;
}
void ParsePropertyFile(const string&, bool);
string GetProperty(const string&) const;
map<int, vector<byte>> GetCustomModePages(const string&, const string&) const;

Expand All @@ -60,5 +61,5 @@ class PropertyHandler
{ '9', 9 }, { 'a', 10 }, { 'b', 11 }, { 'c', 12 }, { 'd', 13 }, { 'e', 14 }, { 'f', 15 }
};

inline static const string DEFAULT_PROPERTIES_FILE = ".config/s2p.properties";
inline static const string DEFAULT_PROPERTY_FILE = ".config/s2p.properties";
};
7 changes: 6 additions & 1 deletion cpp/controllers/controller_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,23 @@ bool ControllerFactory::DeleteController(const AbstractController &controller)
return controllers.erase(controller.GetTargetId()) == 1;
}

void ControllerFactory::DeleteAllControllers()
bool ControllerFactory::DeleteAllControllers()
{
bool has_controller = false;

unordered_set<shared_ptr<AbstractController>> values;
ranges::transform(controllers, inserter(values, values.begin()), [](const auto &controller) {
return controller.second;
});

for (const auto &controller : values) {
DeleteController(*controller);
has_controller = true;
}

assert(controllers.empty());

return has_controller;
}

AbstractController::shutdown_mode ControllerFactory::ProcessOnController(int id_data) const
Expand Down
2 changes: 1 addition & 1 deletion cpp/controllers/controller_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class ControllerFactory

bool AttachToController(Bus&, int, shared_ptr<PrimaryDevice>);
bool DeleteController(const AbstractController&);
void DeleteAllControllers();
bool DeleteAllControllers();
AbstractController::shutdown_mode ProcessOnController(int) const;
shared_ptr<AbstractController> FindController(int) const;
bool HasController(int) const;
Expand Down
8 changes: 3 additions & 5 deletions cpp/devices/mode_page_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,12 @@ void mode_page_util::EnrichFormatPage(map<int, vector<byte>> &pages, bool change

void mode_page_util::AddAppleVendorModePage(map<int, vector<byte>> &pages, bool changeable)
{
// Page code 48 (30h) - Apple Vendor Mode Page
// Needed for SCCD for stock Apple driver support
// Needed for SCHD for stock Apple HD SC Setup
pages[48] = vector<byte>(30);
// Needed for SCCD for stock Apple driver support and stock Apple HD SC Setup
pages[48] = vector<byte>(24);

// No changeable area
if (!changeable) {
constexpr const char APPLE_DATA[] = "APPLE COMPUTER, INC ";
memcpy(&pages[48].data()[2], APPLE_DATA, sizeof(APPLE_DATA));
memcpy(&pages[48][2], APPLE_DATA, sizeof(APPLE_DATA) - 1);
}
}
4 changes: 2 additions & 2 deletions cpp/s2p/s2p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// SCSI target emulator and SCSI tools for the Raspberry Pi
//
// Copyright (C) 2022-2023 Uwe Seimet
// Copyright (C) 2022-2024 Uwe Seimet
//
//---------------------------------------------------------------------------

Expand All @@ -14,5 +14,5 @@ int main(int argc, char *argv[])
{
vector<char*> args(argv, argv + argc);

return S2p().run(args);
return S2p().Run(args);
}
6 changes: 3 additions & 3 deletions cpp/s2p/s2p_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ bool S2p::HandleDeviceListChange(const CommandContext &context, PbOperation oper
return true;
}

int S2p::run(span<char*> args, bool in_process)
int S2p::Run(span<char*> args, bool in_process)
{
GOOGLE_PROTOBUF_VERIFY_VERSION;

Expand All @@ -145,7 +145,7 @@ int S2p::run(span<char*> args, bool in_process)
int port;
try {
const auto &properties = s2p_parser.ParseArguments(args, is_sasi);
property_handler.Init(properties.at(PropertyHandler::PROPERTY_FILE), properties);
property_handler.Init(properties.at(PropertyHandler::PROPERTY_FILES), properties);

if (const string &log_level = property_handler.GetProperty(PropertyHandler::LOG_LEVEL);
!CommandDispatcher::SetLogLevel(log_level)) {
Expand Down Expand Up @@ -324,7 +324,7 @@ void S2p::CreateDevices()
#ifdef BUILD_SCHS
// Ensure that all host services have a dispatcher
for (auto d : controller_factory->GetAllDevices()) {
if (auto host_services = dynamic_pointer_cast<HostServices>(d); host_services != nullptr) {
if (auto host_services = dynamic_pointer_cast<HostServices>(d); host_services) {
host_services->SetDispatcher(dispatcher);
}
}
Expand Down
2 changes: 1 addition & 1 deletion cpp/s2p/s2p_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class S2p

public:

int run(span<char*>, bool = false);
int Run(span<char*>, bool = false);

private:

Expand Down
Loading

0 comments on commit 827976d

Please sign in to comment.