diff --git a/DiscImageChef.Devices/Device/List.cs b/DiscImageChef.Devices/Device/List.cs index cc3bfaad4..429130446 100644 --- a/DiscImageChef.Devices/Device/List.cs +++ b/DiscImageChef.Devices/Device/List.cs @@ -33,6 +33,7 @@ using System; using System.Runtime.InteropServices; using DiscImageChef.CommonTypes.Interop; +using DiscImageChef.Console; using PlatformID = DiscImageChef.CommonTypes.Interop.PlatformID; namespace DiscImageChef.Devices @@ -60,15 +61,30 @@ namespace DiscImageChef.Devices public partial class Device { - public static DeviceInfo[] ListDevices() + public static DeviceInfo[] ListDevices(string dicRemote = null) { - switch (DetectOS.GetRealPlatformID()) + if (dicRemote is null) + switch (DetectOS.GetRealPlatformID()) + { + case PlatformID.Win32NT: return Windows.ListDevices.GetList(); + case PlatformID.Linux: return Linux.ListDevices.GetList(); + case PlatformID.FreeBSD: return FreeBSD.ListDevices.GetList(); + default: + throw new InvalidOperationException( + $"Platform {DetectOS.GetRealPlatformID()} not yet supported."); + } + + try { - case PlatformID.Win32NT: return Windows.ListDevices.GetList(); - case PlatformID.Linux: return Linux.ListDevices.GetList(); - case PlatformID.FreeBSD: return FreeBSD.ListDevices.GetList(); - default: - throw new InvalidOperationException($"Platform {DetectOS.GetRealPlatformID()} not yet supported."); + using (var remote = new Remote.Remote(dicRemote)) + { + return remote.ListDevices(); + } + } + catch (Exception) + { + DicConsole.ErrorWriteLine("Error connecting to host."); + return new DeviceInfo[0]; } } } diff --git a/DiscImageChef.Devices/Remote/Remote.cs b/DiscImageChef.Devices/Remote/Remote.cs index 26446c974..657355c45 100644 --- a/DiscImageChef.Devices/Remote/Remote.cs +++ b/DiscImageChef.Devices/Remote/Remote.cs @@ -10,7 +10,7 @@ using Version = DiscImageChef.CommonTypes.Interop.Version; namespace DiscImageChef.Devices.Remote { - public class Remote + public class Remote : IDisposable { private readonly Socket _socket; @@ -115,10 +115,20 @@ namespace DiscImageChef.Devices.Remote public string ServerArchitecture { get; } public int ServerProtocolVersion { get; } + public void Dispose() + { + Disconnect(); + } + public void Disconnect() { _socket.Shutdown(SocketShutdown.Both); _socket.Close(); } + + public DeviceInfo[] ListDevices() + { + return new DeviceInfo[0]; + } } } \ No newline at end of file diff --git a/DiscImageChef/Commands/ListDevices.cs b/DiscImageChef/Commands/ListDevices.cs index 2050e96e3..501a40398 100644 --- a/DiscImageChef/Commands/ListDevices.cs +++ b/DiscImageChef/Commands/ListDevices.cs @@ -40,9 +40,9 @@ using Mono.Options; namespace DiscImageChef.Commands { - class ListDevicesCommand : Command + internal class ListDevicesCommand : Command { - bool showHelp; + private bool showHelp; public ListDevicesCommand() : base("list-devices", "Lists all connected devices.") { @@ -51,7 +51,7 @@ namespace DiscImageChef.Commands $"{MainClass.AssemblyTitle} {MainClass.AssemblyVersion?.InformationalVersion}", $"{MainClass.AssemblyCopyright}", "", - $"usage: DiscImageChef {Name}", + $"usage: DiscImageChef {Name} [dic-remote-host]", "", Help, {"help|h|?", "Show this message and exit.", v => showHelp = v != null} @@ -60,46 +60,53 @@ namespace DiscImageChef.Commands public override int Invoke(IEnumerable arguments) { - List extra = Options.Parse(arguments); + var extra = Options.Parse(arguments); - if(showHelp) + if (showHelp) { Options.WriteOptionDescriptions(CommandSet.Out); - return (int)ErrorNumber.HelpRequested; + return (int) ErrorNumber.HelpRequested; } MainClass.PrintCopyright(); - if(MainClass.Debug) DicConsole.DebugWriteLineEvent += System.Console.Error.WriteLine; - if(MainClass.Verbose) DicConsole.VerboseWriteLineEvent += System.Console.WriteLine; + if (MainClass.Debug) DicConsole.DebugWriteLineEvent += System.Console.Error.WriteLine; + if (MainClass.Verbose) DicConsole.VerboseWriteLineEvent += System.Console.WriteLine; Statistics.AddCommand("list-devices"); - if(extra.Count > 0) + string dicRemote = null; + + if (extra.Count > 1) { DicConsole.ErrorWriteLine("Too many arguments."); - return (int)ErrorNumber.UnexpectedArgumentCount; + return (int) ErrorNumber.UnexpectedArgumentCount; } - DicConsole.DebugWriteLine("List-Devices command", "--debug={0}", MainClass.Debug); + if (extra.Count == 1) dicRemote = extra[0]; + + DicConsole.DebugWriteLine("List-Devices command", "--debug={0}", MainClass.Debug); DicConsole.DebugWriteLine("List-Devices command", "--verbose={0}", MainClass.Verbose); - DeviceInfo[] devices = Device.ListDevices(); + var devices = Device.ListDevices(dicRemote); - if(devices == null || devices.Length == 0) DicConsole.WriteLine("No known devices attached."); + if (devices == null || devices.Length == 0) + { + DicConsole.WriteLine("No known devices attached."); + } else { devices = devices.OrderBy(d => d.Path).ToArray(); DicConsole.WriteLine("{0,-22}|{1,-16}|{2,-24}|{3,-24}|{4,-10}|{5,-10}", "Path", "Vendor", "Model", - "Serial", "Bus", "Supported?"); + "Serial", "Bus", "Supported?"); DicConsole.WriteLine("{0,-22}+{1,-16}+{2,-24}+{3,-24}+{4,-10}+{5,-10}", "----------------------", - "----------------", "------------------------", "------------------------", - "----------", "----------"); - foreach(DeviceInfo dev in devices) + "----------------", "------------------------", "------------------------", + "----------", "----------"); + foreach (var dev in devices) DicConsole.WriteLine("{0,-22}|{1,-16}|{2,-24}|{3,-24}|{4,-10}|{5,-10}", dev.Path, dev.Vendor, - dev.Model, dev.Serial, dev.Bus, dev.Supported); + dev.Model, dev.Serial, dev.Bus, dev.Supported); } - return (int)ErrorNumber.NoError; + return (int) ErrorNumber.NoError; } } } \ No newline at end of file