Allow listing devices from remote.

This commit is contained in:
2019-10-12 23:12:39 +01:00
parent 58b2b84f0a
commit 580b8e0981
3 changed files with 60 additions and 27 deletions

View File

@@ -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)
{
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.");
throw new InvalidOperationException(
$"Platform {DetectOS.GetRealPlatformID()} not yet supported.");
}
try
{
using (var remote = new Remote.Remote(dicRemote))
{
return remote.ListDevices();
}
}
catch (Exception)
{
DicConsole.ErrorWriteLine("Error connecting to host.");
return new DeviceInfo[0];
}
}
}

View File

@@ -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];
}
}
}

View File

@@ -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,31 +60,38 @@ namespace DiscImageChef.Commands
public override int Invoke(IEnumerable<string> arguments)
{
List<string> 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;
}
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();
@@ -94,12 +101,12 @@ namespace DiscImageChef.Commands
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);
}
return (int)ErrorNumber.NoError;
return (int) ErrorNumber.NoError;
}
}
}