Skip to content

Commit

Permalink
Refactor for support of multiple ABI revisions
Browse files Browse the repository at this point in the history
  • Loading branch information
v1993 committed Nov 11, 2023
1 parent 88e0d52 commit d3c7d6b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
31 changes: 25 additions & 6 deletions src/UsbDeviceClient.vala
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,13 @@ namespace NXDumpClient {
GenericArray<string>? nca_checksums; // Prefixes for NCA files
}

[Flags]
private enum ClientFeatures {
NONE = 0,
}

private const string COMMAND_MAGIC = "NXDT";
private const uint32 HEADER_SIZE = 0x10;
private const uint8 ABI_MAJOR = 1;
private const uint8 ABI_MINOR = 1;
private const uint8 ABI_FULL = (ABI_MAJOR << 4) | ABI_MINOR;
private const uint32 RESPONSE_SIZE = 0x10;
private const uint DEFAULT_TIMEOUT = 5000;
private const uint32 STATUS_SUCCESS = 0x0;
Expand Down Expand Up @@ -205,10 +207,22 @@ namespace NXDumpClient {
status = CONNECTED;
}

private static GenericSet<uint8> supported_abis = new GenericSet<uint8>(null, null);

private static uint8 make_abi_version(uint8 major, uint8 minor)
requires (major < 16 && minor < 16) {
return major << 4 | minor;
}

static construct {
supported_abis.add(make_abi_version(1, 1));
}

private GUsb.Interface iface = null;
private GUsb.Endpoint endpoint_input = null;
private GUsb.Endpoint endpoint_output = null;
private NspDumpStatus? nsp_dump_status = null;
private ClientFeatures features = NONE;

public UsbDeviceClient(owned UsbDeviceOpener devopener_, Cancellable? cancellable_ = null) throws Error {
Object(devopener: (owned)devopener_, cancellable: cancellable_);
Expand Down Expand Up @@ -333,8 +347,14 @@ namespace NXDumpClient {
var ver_micro = istream.read_byte(cancellable);

var abi_ver = istream.read_byte(cancellable);
if (abi_ver != ABI_FULL) {
throw new UsbDeviceProtocolError.UNSUPPORTED_ABI_VERSION("Unsupported USB ABI version x0%X", abi_ver);
if (!(abi_ver in supported_abis)) {
throw new UsbDeviceProtocolError.UNSUPPORTED_ABI_VERSION("Unsupported USB ABI version %s", format_usb_abi(abi_ver));
}

{
features = NONE;

// Put feature selection code here
}

var git_hash = (string)istream.read_bytes(8, cancellable).get_data();
Expand Down Expand Up @@ -543,7 +563,6 @@ namespace NXDumpClient {
debug("File transfer finished");
} catch(Error e) {
transfer_failed.emit(file, false);
// Use error_to_recoverable once error handling during transfer is used
throw e;
}

Expand Down
4 changes: 4 additions & 0 deletions src/Utils.vala
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ namespace NXDumpClient {
}
}

string format_usb_abi(uint8 abi) {
return @"%hhu.%hhu".printf(abi >> 4, abi & 0xF);
}

namespace FileSettingUtils {
[CCode (has_target = false)]
delegate string DefaultPathFunc();
Expand Down
4 changes: 2 additions & 2 deletions src/widgets/DeviceStatusRow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,11 @@ namespace NXDumpClient {

if (device.transfer_total_bytes >= MIN_TRANSFER_SPEED_SIZE) {
var time_passed = get_monotonic_time() - device.transfer_started_time;
// Time is measured after the first transfer, so skip it
var current_bytes_adjusted = device.transfer_current_bytes - BLOCK_SIZE;
builder.append_c(' ');
if (current_bytes_adjusted > 0 && time_passed > 0) {
// Time is measured after the first transfer, so skip it
var bytes_remaining = device.transfer_total_bytes - current_bytes_adjusted;
var bytes_remaining = device.transfer_total_bytes - device.transfer_current_bytes;
var time_remaining = time_passed * bytes_remaining / current_bytes_adjusted / 1000000;
var bytes_per_second = current_bytes_adjusted * 1000000 / time_passed;
builder.append_printf(C_("file transfer speed and min:sec remaining", "(%s/s, %02lld:%02lld remaining)"),
Expand Down

0 comments on commit d3c7d6b

Please sign in to comment.