Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor to allow the USB system to be destructed after use. #772

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 35 additions & 21 deletions lib/usb/usb.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@
#include "lib/logger.h"
#include "greaseweazle.h"

static USB* usb = NULL;
static USB* usb = nullptr;

USB::~USB() {}
USB::~USB()
{
usb = nullptr;
}

static std::shared_ptr<CandidateDevice> selectDevice()
{
Expand Down Expand Up @@ -59,42 +62,53 @@ static std::shared_ptr<CandidateDevice> selectDevice()
exit(1);
}

USB* get_usb_impl()
std::unique_ptr<USB> USB::create()
{
std::unique_ptr<USB> r;
if (usb)
error("more than one USB object created");

/* Special case for certain configurations. */

if (globalConfig()->usb().has_greaseweazle() &&
globalConfig()->usb().greaseweazle().has_port())
{
const auto& conf = globalConfig()->usb().greaseweazle();
log("Using Greaseweazle on serial port {}", conf.port());
return createGreaseweazleUsb(conf.port(), conf);
r.reset(createGreaseweazleUsb(conf.port(), conf));
}

/* Otherwise, select a device by USB ID. */

auto candidate = selectDevice();
switch (candidate->id)
else
{
case FLUXENGINE_ID:
log("Using FluxEngine {}", candidate->serial);
return createFluxengineUsb(candidate->device);
/* Otherwise, select a device by USB ID. */

case GREASEWEAZLE_ID:
log("Using Greaseweazle {} on {}",
candidate->serial,
candidate->serialPort);
return createGreaseweazleUsb(
candidate->serialPort, globalConfig()->usb().greaseweazle());
auto candidate = selectDevice();
switch (candidate->id)
{
case FLUXENGINE_ID:
log("Using FluxEngine {}", candidate->serial);
r.reset(createFluxengineUsb(candidate->device));
break;

case GREASEWEAZLE_ID:
log("Using Greaseweazle {} on {}",
candidate->serial,
candidate->serialPort);
r.reset(createGreaseweazleUsb(candidate->serialPort,
globalConfig()->usb().greaseweazle()));
break;

default:
error("internal");
default:
error("internal");
}
}

usb = r.get();
return r;
}

USB& getUsb()
{
if (!usb)
usb = get_usb_impl();
error("USB instance not created");
return *usb;
}
3 changes: 3 additions & 0 deletions lib/usb/usb.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ namespace libusbp

class USB
{
public:
static std::unique_ptr<USB> create();

public:
virtual ~USB();

Expand Down
1 change: 1 addition & 0 deletions src/fe-analysedriveresponse.cc
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ int mainAnalyseDriveResponse(int argc, const char* argv[])
if (globalConfig()->flux_sink().type() != FLUXTYPE_DRIVE)
error("this only makes sense with a real disk drive");

auto usb = USB::create();
usbSetDrive(globalConfig()->drive().drive(),
globalConfig()->drive().high_density(),
globalConfig()->drive().index_mode());
Expand Down
2 changes: 2 additions & 0 deletions src/fe-analyselayout.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "lib/csvreader.h"
#include "lib/image.h"
#include "lib/decoders/fluxmapreader.h"
#include "lib/usb/usb.h"
#include "agg2d.h"
#include "stb_image_write.h"
#include <math.h>
Expand Down Expand Up @@ -228,6 +229,7 @@ static Image readCsv(const std::string& filename)
int mainAnalyseLayout(int argc, const char* argv[])
{
flags.parseFlags(argc, argv);
auto usb = USB::create();

Image image = readCsv(source.get());
visualiseSectorsToFile(image, "out.svg");
Expand Down
15 changes: 5 additions & 10 deletions src/fe-format.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "fluxengine.h"
#include "lib/vfs/sectorinterface.h"
#include "lib/vfs/vfs.h"
#include "lib/usb/usb.h"
#include "src/fileutils.h"
#include <google/protobuf/text_format.h>
#include <fstream>
Expand All @@ -27,16 +28,10 @@ int mainFormat(int argc, const char* argv[])
showProfiles("format", formats);
flags.parseFlagsWithConfigFiles(argc, argv, formats);

try
{
auto filesystem = Filesystem::createFilesystemFromConfig();
filesystem->create(quick, volumeName);
filesystem->flushChanges();
}
catch (const FilesystemException& e)
{
error("{}", e.message);
}
auto usb = USB::create();
auto filesystem = Filesystem::createFilesystemFromConfig();
filesystem->create(quick, volumeName);
filesystem->flushChanges();

return 0;
}
17 changes: 6 additions & 11 deletions src/fe-getdiskinfo.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "lib/vfs/sectorinterface.h"
#include "lib/vfs/vfs.h"
#include "lib/utils.h"
#include "lib/usb/usb.h"
#include "src/fileutils.h"
#include <google/protobuf/text_format.h>
#include <fstream>
Expand All @@ -24,18 +25,12 @@ int mainGetDiskInfo(int argc, const char* argv[])
showProfiles("getdiskinfo", formats);
flags.parseFlagsWithConfigFiles(argc, argv, formats);

try
{
auto filesystem = Filesystem::createFilesystemFromConfig();
auto attributes = filesystem->getMetadata();
auto usb = USB::create();
auto filesystem = Filesystem::createFilesystemFromConfig();
auto attributes = filesystem->getMetadata();

for (const auto& e : attributes)
fmt::print("{}={}\n", e.first, quote(e.second));
}
catch (const FilesystemException& e)
{
error("{}", e.message);
}
for (const auto& e : attributes)
fmt::print("{}={}\n", e.first, quote(e.second));

return 0;
}
32 changes: 14 additions & 18 deletions src/fe-getfile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "fluxengine.h"
#include "lib/vfs/sectorinterface.h"
#include "lib/vfs/vfs.h"
#include "lib/usb/usb.h"
#include "src/fileutils.h"
#include <google/protobuf/text_format.h>
#include <fstream>
Expand All @@ -26,24 +27,19 @@ int mainGetFile(int argc, const char* argv[])
showProfiles("getfile", formats);
flags.parseFlagsWithConfigFiles(argc, argv, formats);

try
{
Path inputFilename(directory);
if (inputFilename.size() == 0)
error("you must supply a filename to read");

std::string outputFilename = output;
if (outputFilename.empty())
outputFilename = inputFilename.back();

auto filesystem = Filesystem::createFilesystemFromConfig();
auto data = filesystem->getFile(inputFilename);
data.writeToFile(outputFilename);
}
catch (const FilesystemException& e)
{
error("{}", e.message);
}
auto usb = USB::create();

Path inputFilename(directory);
if (inputFilename.size() == 0)
error("you must supply a filename to read");

std::string outputFilename = output;
if (outputFilename.empty())
outputFilename = inputFilename.back();

auto filesystem = Filesystem::createFilesystemFromConfig();
auto data = filesystem->getFile(inputFilename);
data.writeToFile(outputFilename);

return 0;
}
18 changes: 7 additions & 11 deletions src/fe-getfileinfo.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "lib/vfs/sectorinterface.h"
#include "lib/vfs/vfs.h"
#include "lib/utils.h"
#include "lib/usb/usb.h"
#include "src/fileutils.h"
#include <google/protobuf/text_format.h>
#include <fstream>
Expand All @@ -26,18 +27,13 @@ int mainGetFileInfo(int argc, const char* argv[])
showProfiles("getfileinfo", formats);
flags.parseFlagsWithConfigFiles(argc, argv, formats);

try
{
auto filesystem = Filesystem::createFilesystemFromConfig();
auto dirent = filesystem->getDirent(Path(directory));
auto usb = USB::create();

for (const auto& e : dirent->attributes)
fmt::print("{}={}\n", e.first, quote(e.second));
}
catch (const FilesystemException& e)
{
error("{}", e.message);
}
auto filesystem = Filesystem::createFilesystemFromConfig();
auto dirent = filesystem->getDirent(Path(directory));

for (const auto& e : dirent->attributes)
fmt::print("{}={}\n", e.first, quote(e.second));

return 0;
}
2 changes: 2 additions & 0 deletions src/fe-inspect.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "lib/decoders/rawbits.h"
#include "lib/sector.h"
#include "lib/proto.h"
#include "lib/usb/usb.h"

static FlagGroup flags;

Expand Down Expand Up @@ -133,6 +134,7 @@ int mainInspect(int argc, const char* argv[])
{
globalConfig().overrides()->mutable_flux_source()->set_type(FLUXTYPE_DRIVE);
flags.parseFlagsWithConfigFiles(argc, argv, {});
auto usb = USB::create();

auto& fluxSource = globalConfig().getFluxSource();
const auto fluxmap = fluxSource->readFlux(trackFlag, headFlag)->next();
Expand Down
42 changes: 19 additions & 23 deletions src/fe-ls.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "lib/vfs/sectorinterface.h"
#include "lib/vfs/vfs.h"
#include "lib/utils.h"
#include "lib/usb/usb.h"
#include "src/fileutils.h"
#include <google/protobuf/text_format.h>
#include <fstream>
Expand Down Expand Up @@ -40,33 +41,28 @@ int mainLs(int argc, const char* argv[])
showProfiles("ls", formats);
flags.parseFlagsWithConfigFiles(argc, argv, formats);

try
{
auto filesystem = Filesystem::createFilesystemFromConfig();
auto files = filesystem->list(Path(directory));
auto usb = USB::create();

int maxlen = 0;
for (const auto& dirent : files)
maxlen = std::max(maxlen, (int)quote(dirent->filename).size());
auto filesystem = Filesystem::createFilesystemFromConfig();
auto files = filesystem->list(Path(directory));

uint32_t total = 0;
for (const auto& dirent : files)
{
fmt::print("{} {:{}} {:6} {:4} {}\n",
fileTypeChar(dirent->file_type),
quote(dirent->filename),
maxlen + 2,
dirent->length,
dirent->mode,
dirent->attributes[Filesystem::CTIME]);
total += dirent->length;
}
fmt::print("({} files, {} bytes)\n", files.size(), total);
}
catch (const FilesystemException& e)
int maxlen = 0;
for (const auto& dirent : files)
maxlen = std::max(maxlen, (int)quote(dirent->filename).size());

uint32_t total = 0;
for (const auto& dirent : files)
{
error("{}", e.message);
fmt::print("{} {:{}} {:6} {:4} {}\n",
fileTypeChar(dirent->file_type),
quote(dirent->filename),
maxlen + 2,
dirent->length,
dirent->mode,
dirent->attributes[Filesystem::CTIME]);
total += dirent->length;
}
fmt::print("({} files, {} bytes)\n", files.size(), total);

return 0;
}
21 changes: 8 additions & 13 deletions src/fe-mkdir.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "fluxengine.h"
#include "lib/vfs/vfs.h"
#include "lib/utils.h"
#include "lib/usb/usb.h"
#include "src/fileutils.h"
#include <google/protobuf/text_format.h>
#include <fstream>
Expand All @@ -18,21 +19,15 @@ int mainMkDir(int argc, const char* argv[])
showProfiles("mkdir", formats);
flags.parseFlagsWithConfigFiles(argc, argv, formats);

try
{
auto filesystem = Filesystem::createFilesystemFromConfig();
auto usb = USB::create();
auto filesystem = Filesystem::createFilesystemFromConfig();

Path path(filename);
if (path.size() == 0)
error("filename missing");
Path path(filename);
if (path.size() == 0)
error("filename missing");

filesystem->createDirectory(path);
filesystem->flushChanges();
}
catch (const FilesystemException& e)
{
error("{}", e.message);
}
filesystem->createDirectory(path);
filesystem->flushChanges();

return 0;
}
Loading
Loading