From eedadeacb31cde39719de9bc293ac563ccd06030 Mon Sep 17 00:00:00 2001 From: Christian Date: Sun, 23 Jun 2019 12:29:46 +1000 Subject: [PATCH 01/18] Break the code --- src/Device.Net/DeviceBase.cs | 14 ++++++++++++-- src/Usb.Net.Android/AndroidUsbDevice.cs | 2 +- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/Device.Net/DeviceBase.cs b/src/Device.Net/DeviceBase.cs index 7876bf6d..28c89012 100644 --- a/src/Device.Net/DeviceBase.cs +++ b/src/Device.Net/DeviceBase.cs @@ -21,10 +21,18 @@ public abstract class DeviceBase : IDisposable #endregion #region Public Properties - public ITracer Tracer { get; set; } public ConnectedDeviceDefinitionBase ConnectedDeviceDefinition { get; set; } public string DeviceId { get; set; } - public ILogger Logger { get; set; } + public ILogger Logger { get; } + public ITracer Tracer { get; } + #endregion + + #region Constructor + public DeviceBase(ILogger logger, ITracer tracer) + { + Tracer = tracer; + Logger = logger; + } #endregion #region Private Methods @@ -74,7 +82,9 @@ public async Task WriteAndReadAsync(byte[] writeBuffer) try { await WriteAsync(writeBuffer); + Tracer?.Trace(true, writeBuffer); var retVal = await ReadAsync(); + Tracer?.Trace(false, retVal); Log($"Successfully called {nameof(WriteAndReadAsync)}"); return retVal; } diff --git a/src/Usb.Net.Android/AndroidUsbDevice.cs b/src/Usb.Net.Android/AndroidUsbDevice.cs index 30838c89..b23a0385 100644 --- a/src/Usb.Net.Android/AndroidUsbDevice.cs +++ b/src/Usb.Net.Android/AndroidUsbDevice.cs @@ -43,7 +43,7 @@ public int DeviceNumberId #endregion #region Constructor - public AndroidUsbDevice(UsbManager usbManager, Context androidContext, int deviceNumberId) + public AndroidUsbDevice(UsbManager usbManager, Context androidContext, int deviceNumberId, ILogger logger, ITracer tracer) : base(logger, tracer) { UsbManager = usbManager; AndroidContext = androidContext; From e9da4424bc37d2626d15cf32baece09ef3f40636 Mon Sep 17 00:00:00 2001 From: Christian Date: Sun, 23 Jun 2019 12:42:06 +1000 Subject: [PATCH 02/18] More compile --- src/Device.Net.UWP/UWPDeviceFactoryBase.cs | 3 ++- src/Device.Net/DeviceBase.cs | 2 +- src/Device.Net/Windows/WindowsDeviceBase.cs | 2 +- .../Windows/WindowsDeviceFactoryBase.cs | 11 ++++++++++- src/Hid.Net/Windows/WindowsHIDDevice.cs | 4 ++-- src/Hid.Net/Windows/WindowsHidDeviceFactory.cs | 9 ++++++++- src/Usb.Net.Android/AndroidUsbDeviceFactory.cs | 15 +++++++++------ src/Usb.Net.UWP/UWPUsbDevice.cs | 4 ++-- src/Usb.Net.UWP/UWPUsbDeviceFactory.cs | 3 +-- src/Usb.Net/Windows/WindowsUsbDevice.cs | 2 +- 10 files changed, 37 insertions(+), 18 deletions(-) diff --git a/src/Device.Net.UWP/UWPDeviceFactoryBase.cs b/src/Device.Net.UWP/UWPDeviceFactoryBase.cs index 46ce9f4e..ff5644ff 100644 --- a/src/Device.Net.UWP/UWPDeviceFactoryBase.cs +++ b/src/Device.Net.UWP/UWPDeviceFactoryBase.cs @@ -23,7 +23,8 @@ public abstract class UWPDeviceFactoryBase #endregion #region Public Properties - public ILogger Logger { get; set; } + public ILogger Logger { get; } + public ITracer Tracer { get; } #endregion #region Public Abstract Properties diff --git a/src/Device.Net/DeviceBase.cs b/src/Device.Net/DeviceBase.cs index 28c89012..edfcde05 100644 --- a/src/Device.Net/DeviceBase.cs +++ b/src/Device.Net/DeviceBase.cs @@ -28,7 +28,7 @@ public abstract class DeviceBase : IDisposable #endregion #region Constructor - public DeviceBase(ILogger logger, ITracer tracer) + protected DeviceBase(ILogger logger, ITracer tracer) { Tracer = tracer; Logger = logger; diff --git a/src/Device.Net/Windows/WindowsDeviceBase.cs b/src/Device.Net/Windows/WindowsDeviceBase.cs index 3947b7f5..ffd83c9c 100644 --- a/src/Device.Net/Windows/WindowsDeviceBase.cs +++ b/src/Device.Net/Windows/WindowsDeviceBase.cs @@ -14,7 +14,7 @@ public abstract class WindowsDeviceBase : DeviceBase #endregion #region Constructor - protected WindowsDeviceBase(string deviceId) + protected WindowsDeviceBase(string deviceId, ILogger logger, ITracer tracer) : base(logger, tracer) { DeviceId = deviceId; } diff --git a/src/Device.Net/Windows/WindowsDeviceFactoryBase.cs b/src/Device.Net/Windows/WindowsDeviceFactoryBase.cs index 19a4bef5..2fa31942 100644 --- a/src/Device.Net/Windows/WindowsDeviceFactoryBase.cs +++ b/src/Device.Net/Windows/WindowsDeviceFactoryBase.cs @@ -14,7 +14,8 @@ namespace Device.Net.Windows public abstract class WindowsDeviceFactoryBase { #region Public Properties - public ILogger Logger { get; set; } + public ILogger Logger { get; } + public ITracer Tracer { get; } #endregion #region Public Abstract Properties @@ -26,6 +27,14 @@ public abstract class WindowsDeviceFactoryBase protected abstract Guid GetClassGuid(); #endregion + #region Constructor + protected WindowsDeviceFactoryBase(ILogger logger, ITracer tracer) + { + Logger = logger; + Tracer = tracer; + } + #endregion + #region Public Methods public async Task> GetConnectedDeviceDefinitionsAsync(FilterDeviceDefinition filterDeviceDefinition) { diff --git a/src/Hid.Net/Windows/WindowsHIDDevice.cs b/src/Hid.Net/Windows/WindowsHIDDevice.cs index 4c4c57ca..0e8cd139 100644 --- a/src/Hid.Net/Windows/WindowsHIDDevice.cs +++ b/src/Hid.Net/Windows/WindowsHIDDevice.cs @@ -41,11 +41,11 @@ public sealed class WindowsHidDevice : WindowsDeviceBase, IHidDevice #endregion #region Constructor - public WindowsHidDevice(string deviceId) : this(deviceId, null, null) + public WindowsHidDevice(string deviceId, ILogger logger, ITracer tracer) : this(deviceId, null, null, logger, tracer) { } - public WindowsHidDevice(string deviceId, ushort? writeBufferSize, ushort? readBufferSize) : base(deviceId) + public WindowsHidDevice(string deviceId, ushort? writeBufferSize, ushort? readBufferSize, ILogger logger, ITracer tracer) : base(deviceId, logger, tracer) { _WriteBufferSize = writeBufferSize; _ReadBufferSize = readBufferSize; diff --git a/src/Hid.Net/Windows/WindowsHidDeviceFactory.cs b/src/Hid.Net/Windows/WindowsHidDeviceFactory.cs index b856148c..811afff9 100644 --- a/src/Hid.Net/Windows/WindowsHidDeviceFactory.cs +++ b/src/Hid.Net/Windows/WindowsHidDeviceFactory.cs @@ -36,10 +36,17 @@ protected override Guid GetClassGuid() #endregion + #region Constructor + public WindowsHidDeviceFactory(ILogger logger, ITracer tracer) : base(logger, tracer) + { + + } + #endregion + #region Public Methods public IDevice GetDevice(ConnectedDeviceDefinition deviceDefinition) { - return deviceDefinition.DeviceType != DeviceType ? null : new WindowsHidDevice(deviceDefinition.DeviceId) { Logger = Logger }; + return deviceDefinition.DeviceType != DeviceType ? null : new WindowsHidDevice(deviceDefinition.DeviceId, Logger, Tracer); } #endregion diff --git a/src/Usb.Net.Android/AndroidUsbDeviceFactory.cs b/src/Usb.Net.Android/AndroidUsbDeviceFactory.cs index 4d1a8cfa..7449e785 100644 --- a/src/Usb.Net.Android/AndroidUsbDeviceFactory.cs +++ b/src/Usb.Net.Android/AndroidUsbDeviceFactory.cs @@ -16,7 +16,8 @@ public class AndroidUsbDeviceFactory : IDeviceFactory #region Public Properties public UsbManager UsbManager { get; } public Context Context { get; } - public ILogger Logger { get; set; } + public ILogger Logger { get; } + public ITracer Tracer { get; } #endregion #region Public Static Properties @@ -24,10 +25,12 @@ public class AndroidUsbDeviceFactory : IDeviceFactory #endregion #region Constructor - public AndroidUsbDeviceFactory(UsbManager usbManager, Context context) + public AndroidUsbDeviceFactory(UsbManager usbManager, Context context, ILogger logger, ITracer tracer) { UsbManager = usbManager; Context = context; + Logger = logger; + Tracer = tracer; } #endregion @@ -54,7 +57,7 @@ public IDevice GetDevice(ConnectedDeviceDefinition deviceDefinition) throw new Exception($"The device Id '{deviceDefinition.DeviceId}' is not a valid integer"); } - return new AndroidUsbDevice(UsbManager, Context, deviceId) { Logger = Logger }; + return new AndroidUsbDevice(UsbManager, Context, deviceId, Logger, Tra) { Logger = Logger }; } #endregion @@ -76,12 +79,12 @@ public static ConnectedDeviceDefinition GetAndroidDeviceDefinition(UsbDevice usb public static void Register(UsbManager usbManager, Context context) { - Register(usbManager, context, null); + Register(usbManager, context, null, null); } - public static void Register(UsbManager usbManager, Context context, ILogger logger) + public static void Register(UsbManager usbManager, Context context, ILogger logger, ITracer tracer) { - DeviceManager.Current.DeviceFactories.Add(new AndroidUsbDeviceFactory(usbManager, context) { Logger = logger }); + DeviceManager.Current.DeviceFactories.Add(new AndroidUsbDeviceFactory(usbManager, context, logger, tracer)); } #endregion } diff --git a/src/Usb.Net.UWP/UWPUsbDevice.cs b/src/Usb.Net.UWP/UWPUsbDevice.cs index d30766db..0a89602e 100644 --- a/src/Usb.Net.UWP/UWPUsbDevice.cs +++ b/src/Usb.Net.UWP/UWPUsbDevice.cs @@ -26,11 +26,11 @@ public class UWPUsbDevice : UWPDeviceBase #endregion #region Constructors - public UWPUsbDevice() : base() + public UWPUsbDevice(ILogger logger, ITracer tracer) : this(null, logger, tracer) { } - public UWPUsbDevice(ConnectedDeviceDefinition deviceDefinition) : base(deviceDefinition.DeviceId) + public UWPUsbDevice(ConnectedDeviceDefinition deviceDefinition, ILogger logger, ITracer tracer) : base(deviceDefinition.DeviceId) { ConnectedDeviceDefinition = deviceDefinition; } diff --git a/src/Usb.Net.UWP/UWPUsbDeviceFactory.cs b/src/Usb.Net.UWP/UWPUsbDeviceFactory.cs index 1e3fcd17..f4773a6f 100644 --- a/src/Usb.Net.UWP/UWPUsbDeviceFactory.cs +++ b/src/Usb.Net.UWP/UWPUsbDeviceFactory.cs @@ -25,8 +25,7 @@ protected override string GetAqsFilter(uint? vendorId, uint? productId) #region Public Methods public IDevice GetDevice(ConnectedDeviceDefinition deviceDefinition) { - if (deviceDefinition.DeviceType == DeviceType.Hid) return null; - return new UWPUsbDevice(deviceDefinition) { Logger = Logger }; + return deviceDefinition.DeviceType == DeviceType.Hid ? null : new UWPUsbDevice(deviceDefinition, Logger, Tracer); } #endregion diff --git a/src/Usb.Net/Windows/WindowsUsbDevice.cs b/src/Usb.Net/Windows/WindowsUsbDevice.cs index 4ed9c747..863774bd 100644 --- a/src/Usb.Net/Windows/WindowsUsbDevice.cs +++ b/src/Usb.Net/Windows/WindowsUsbDevice.cs @@ -26,7 +26,7 @@ public sealed class WindowsUsbDevice : WindowsDeviceBase, IDevice #endregion #region Constructor - public WindowsUsbDevice(string deviceId) : base(deviceId) + public WindowsUsbDevice(string deviceId, ILogger logger, ITracer tracer) : base(deviceId, logger, tracer) { } #endregion From eb25b5f7433e6c69b4c207d7065221787639eb97 Mon Sep 17 00:00:00 2001 From: Christian Date: Sun, 23 Jun 2019 13:03:50 +1000 Subject: [PATCH 03/18] Almost compiling --- src/Device.Net.UWP/UWPDeviceBase - Generic.cs | 7 +------ src/Device.Net.UWP/UWPDeviceBase.cs | 7 +++++++ src/Device.Net.UWP/UWPDeviceFactoryBase.cs | 8 ++++++++ src/Device.Net.UnitTests/MockDeviceBase.cs | 5 +++++ src/Device.Net.UnitTests/MockFactoryBase.cs | 9 ++++++++- src/Device.Net.UnitTests/MockHidDevice.cs | 3 +-- src/Device.Net.UnitTests/MockHidFactory.cs | 9 ++++----- src/Device.Net.UnitTests/MockUsbDevice.cs | 3 +-- src/Device.Net.UnitTests/MockUsbFactory.cs | 9 ++++----- src/Device.Net.UnitTests/UnitTests.cs | 4 +--- src/Hid.Net.UWP/UWPHidDevice.cs | 7 ++++--- src/Hid.Net.UWP/UWPHidDeviceFactory.cs | 15 ++++++++++---- .../Windows/WindowsHidDeviceFactory.cs | 12 ++++++++--- .../AndroidUsbDeviceFactory.cs | 4 ++-- src/Usb.Net.UWP/UWPUsbDevice.cs | 2 +- src/Usb.Net.UWP/UWPUsbDeviceFactory.cs | 14 +++++++++---- .../Windows/WindowsUsbDeviceFactory.cs | 20 +++++++++++++++---- 17 files changed, 93 insertions(+), 45 deletions(-) diff --git a/src/Device.Net.UWP/UWPDeviceBase - Generic.cs b/src/Device.Net.UWP/UWPDeviceBase - Generic.cs index f111e36d..3c741e30 100644 --- a/src/Device.Net.UWP/UWPDeviceBase - Generic.cs +++ b/src/Device.Net.UWP/UWPDeviceBase - Generic.cs @@ -16,12 +16,7 @@ public abstract class UWPDeviceBase : UWPDeviceBase, IDevice #endregion #region Constructor - protected UWPDeviceBase() - { - - } - - protected UWPDeviceBase(string deviceId) + protected UWPDeviceBase(string deviceId, ILogger logger, ITracer tracer) : base(logger, tracer) { DeviceId = deviceId; } diff --git a/src/Device.Net.UWP/UWPDeviceBase.cs b/src/Device.Net.UWP/UWPDeviceBase.cs index 03f66396..42ae4eff 100644 --- a/src/Device.Net.UWP/UWPDeviceBase.cs +++ b/src/Device.Net.UWP/UWPDeviceBase.cs @@ -35,5 +35,12 @@ protected void HandleDataReceived(byte[] bytes) #region Public Abstract Methods public abstract Task InitializeAsync(); #endregion + + #region Constructor + protected UWPDeviceBase(ILogger logger, ITracer tracer) : base(logger, tracer) + { + + } + #endregion } } diff --git a/src/Device.Net.UWP/UWPDeviceFactoryBase.cs b/src/Device.Net.UWP/UWPDeviceFactoryBase.cs index ff5644ff..458671ad 100644 --- a/src/Device.Net.UWP/UWPDeviceFactoryBase.cs +++ b/src/Device.Net.UWP/UWPDeviceFactoryBase.cs @@ -35,6 +35,14 @@ public abstract class UWPDeviceFactoryBase protected abstract string GetAqsFilter(uint? vendorId, uint? productId); #endregion + #region Constructor + protected UWPDeviceFactoryBase(ILogger logger, ITracer tracer) + { + Logger = logger; + Tracer = tracer; + } + #endregion + #region Abstraction Methods protected string GetVendorPart(uint? vendorId) { diff --git a/src/Device.Net.UnitTests/MockDeviceBase.cs b/src/Device.Net.UnitTests/MockDeviceBase.cs index e41b4680..14e42243 100644 --- a/src/Device.Net.UnitTests/MockDeviceBase.cs +++ b/src/Device.Net.UnitTests/MockDeviceBase.cs @@ -11,6 +11,11 @@ public abstract class MockDeviceBase : DeviceBase, IDevice public override bool IsInitialized => _IsInitialized; + protected MockDeviceBase(ILogger logger, ITracer tracer) : base(logger, tracer) + { + + } + public void Close() { } diff --git a/src/Device.Net.UnitTests/MockFactoryBase.cs b/src/Device.Net.UnitTests/MockFactoryBase.cs index 47b79f5b..596f22fb 100644 --- a/src/Device.Net.UnitTests/MockFactoryBase.cs +++ b/src/Device.Net.UnitTests/MockFactoryBase.cs @@ -12,7 +12,14 @@ public abstract class MockFactoryBase : IDeviceFactory public abstract DeviceType DeviceType { get; } - public ILogger Logger { get; protected set; } + public ILogger Logger { get; } + public ITracer Tracer { get; } + + protected MockFactoryBase(ILogger logger, ITracer tracer) + { + Logger = logger; + Tracer = tracer; + } public abstract uint ProductId { get; } public abstract uint VendorId { get; } diff --git a/src/Device.Net.UnitTests/MockHidDevice.cs b/src/Device.Net.UnitTests/MockHidDevice.cs index 0331988f..0e9887bb 100644 --- a/src/Device.Net.UnitTests/MockHidDevice.cs +++ b/src/Device.Net.UnitTests/MockHidDevice.cs @@ -6,11 +6,10 @@ public class MockHidDevice : MockDeviceBase, IDevice public const uint VendorId = 1; public const string MockedDeviceId = "123"; - public MockHidDevice() + public MockHidDevice(ILogger logger, ITracer tracer) : base(logger, tracer) { DeviceId = MockedDeviceId; ConnectedDeviceDefinition = new ConnectedDeviceDefinition(DeviceId) { ProductId = ProductId, VendorId = VendorId }; - Logger = new DebugLogger { LogToConsole = true }; } } } diff --git a/src/Device.Net.UnitTests/MockHidFactory.cs b/src/Device.Net.UnitTests/MockHidFactory.cs index 02438ada..f19ee0bd 100644 --- a/src/Device.Net.UnitTests/MockHidFactory.cs +++ b/src/Device.Net.UnitTests/MockHidFactory.cs @@ -4,9 +4,8 @@ namespace Device.Net.UnitTests { public class MockHidFactory : MockFactoryBase, IDeviceFactory { - public MockHidFactory() + public MockHidFactory(ILogger logger, ITracer tracer) : base(logger, tracer) { - Logger = new DebugLogger { LogToConsole = true }; } public override string DeviceId => MockHidDevice.MockedDeviceId; @@ -21,9 +20,9 @@ public MockHidFactory() public override uint VendorId => MockHidDevice.VendorId; - public static void Register(ILogger logger) + public static void Register(ILogger logger, ITracer trace) { - DeviceManager.Current.DeviceFactories.Add(new MockHidFactory() { Logger = logger }); + DeviceManager.Current.DeviceFactories.Add(new MockHidFactory(logger, trace)); } public override IDevice GetDevice(ConnectedDeviceDefinition deviceDefinition) @@ -32,7 +31,7 @@ public override IDevice GetDevice(ConnectedDeviceDefinition deviceDefinition) { if (deviceDefinition.DeviceId == DeviceId) { - if (!deviceDefinition.DeviceType.HasValue || deviceDefinition.DeviceType == DeviceType.Hid) return new MockHidDevice(); + if (!deviceDefinition.DeviceType.HasValue || deviceDefinition.DeviceType == DeviceType.Hid) return new MockHidDevice(Logger, Tracer); } } diff --git a/src/Device.Net.UnitTests/MockUsbDevice.cs b/src/Device.Net.UnitTests/MockUsbDevice.cs index 0623e60c..98d93bbc 100644 --- a/src/Device.Net.UnitTests/MockUsbDevice.cs +++ b/src/Device.Net.UnitTests/MockUsbDevice.cs @@ -6,11 +6,10 @@ public class MockUsbDevice : MockDeviceBase, IDevice public const uint VendorId = 2; public const string MockedDeviceId = "321"; - public MockUsbDevice() + public MockUsbDevice(ILogger logger, ITracer tracer) : base(logger, tracer) { DeviceId = MockedDeviceId; ConnectedDeviceDefinition = new ConnectedDeviceDefinition(DeviceId) { ProductId = ProductId, VendorId = VendorId }; - Logger = new DebugLogger { LogToConsole = true }; } } } diff --git a/src/Device.Net.UnitTests/MockUsbFactory.cs b/src/Device.Net.UnitTests/MockUsbFactory.cs index ffec9a99..bcd3f641 100644 --- a/src/Device.Net.UnitTests/MockUsbFactory.cs +++ b/src/Device.Net.UnitTests/MockUsbFactory.cs @@ -4,9 +4,8 @@ namespace Device.Net.UnitTests { public class MockUsbFactory : MockFactoryBase, IDeviceFactory { - public MockUsbFactory() + public MockUsbFactory(ILogger logger, ITracer tracer) : base(logger, tracer) { - Logger = new DebugLogger { LogToConsole = true }; } public override string DeviceId => MockUsbDevice.MockedDeviceId; @@ -21,9 +20,9 @@ public MockUsbFactory() public override uint VendorId => MockUsbDevice.VendorId; - public static void Register(ILogger logger) + public static void Register(ILogger logger, ITracer tracer) { - DeviceManager.Current.DeviceFactories.Add(new MockUsbFactory() { Logger = logger }); + DeviceManager.Current.DeviceFactories.Add(new MockUsbFactory(logger, tracer)); } public override IDevice GetDevice(ConnectedDeviceDefinition deviceDefinition) @@ -32,7 +31,7 @@ public override IDevice GetDevice(ConnectedDeviceDefinition deviceDefinition) { if (deviceDefinition.DeviceId == DeviceId) { - if (!deviceDefinition.DeviceType.HasValue || deviceDefinition.DeviceType == DeviceType.Usb) return new MockUsbDevice() ; + if (!deviceDefinition.DeviceType.HasValue || deviceDefinition.DeviceType == DeviceType.Usb) return new MockUsbDevice(Logger, Tracer); } } diff --git a/src/Device.Net.UnitTests/UnitTests.cs b/src/Device.Net.UnitTests/UnitTests.cs index bf29560a..02f9f05e 100644 --- a/src/Device.Net.UnitTests/UnitTests.cs +++ b/src/Device.Net.UnitTests/UnitTests.cs @@ -40,14 +40,12 @@ public async Task TestWithMatchedFilterAsync(bool isHidConnected, bool isUsbConn [TestMethod] [DataRow(true, true, MockHidDevice.VendorId, MockHidDevice.ProductId)] [DataRow(true, false, MockHidDevice.VendorId, MockHidDevice.ProductId)] - //[DataRow(true, true, MockUsbDevice.VendorId, MockUsbDevice.ProductId)] - //[DataRow(false, true, MockUsbDevice.VendorId, MockUsbDevice.ProductId)] public async Task TestWriteAndReadThreadSafety(bool isHidConnected, bool isUsbConnected, uint vid, uint pid) { MockHidFactory.IsConnectedStatic = isHidConnected; MockUsbFactory.IsConnectedStatic = isUsbConnected; var connectedDeviceDefinition = (await DeviceManager.Current.GetConnectedDeviceDefinitionsAsync(new FilterDeviceDefinition { ProductId = pid, VendorId = vid })).ToList().First(); - var mockHidDevice = new MockHidDevice() { DeviceId = connectedDeviceDefinition.DeviceId }; + var mockHidDevice = new MockHidDevice(new DebugLogger(), new DebugTracer()) { DeviceId = connectedDeviceDefinition.DeviceId }; var writeAndReadTasks = new List>(); diff --git a/src/Hid.Net.UWP/UWPHidDevice.cs b/src/Hid.Net.UWP/UWPHidDevice.cs index 21d67df1..9931d300 100644 --- a/src/Hid.Net.UWP/UWPHidDevice.cs +++ b/src/Hid.Net.UWP/UWPHidDevice.cs @@ -1,4 +1,5 @@ -using Device.Net.UWP; +using Device.Net; +using Device.Net.UWP; using System; using System.Runtime.InteropServices.WindowsRuntime; using System.Threading.Tasks; @@ -34,11 +35,11 @@ private void _HidDevice_InputReportReceived(HidDevice sender, HidInputReportRece #endregion #region Constructors - public UWPHidDevice() + public UWPHidDevice(ILogger logger, ITracer tracer) : this(null, logger, tracer) { } - public UWPHidDevice(string deviceId) : base(deviceId) + public UWPHidDevice(string deviceId, ILogger logger, ITracer tracer) : base(deviceId, logger, tracer) { } #endregion diff --git a/src/Hid.Net.UWP/UWPHidDeviceFactory.cs b/src/Hid.Net.UWP/UWPHidDeviceFactory.cs index c4ac8c29..30ba4e57 100644 --- a/src/Hid.Net.UWP/UWPHidDeviceFactory.cs +++ b/src/Hid.Net.UWP/UWPHidDeviceFactory.cs @@ -64,10 +64,17 @@ public override async Task TestConnection(string deviceId) } #endregion + #region Constructor + public UWPHidDeviceFactory(ILogger logger, ITracer tracer) : base(logger, tracer) + { + + } + #endregion + #region Public Methods public IDevice GetDevice(ConnectedDeviceDefinition deviceDefinition) { - return deviceDefinition.DeviceType == DeviceType.Usb ? null : new UWPHidDevice(deviceDefinition.DeviceId) { Logger = Logger }; + return deviceDefinition.DeviceType == DeviceType.Usb ? null : new UWPHidDevice(deviceDefinition.DeviceId, Logger, Tracer); } public void Dispose() @@ -84,17 +91,17 @@ public void Dispose() #region Public Static Methods public static void Register() { - Register(null); + Register(null, null); } - public static void Register(ILogger logger) + public static void Register(ILogger logger, ITracer tracer) { foreach (var deviceFactory in DeviceManager.Current.DeviceFactories) { if (deviceFactory is UWPHidDeviceFactory) return; } - DeviceManager.Current.DeviceFactories.Add(new UWPHidDeviceFactory() { Logger = logger }); + DeviceManager.Current.DeviceFactories.Add(new UWPHidDeviceFactory(logger, tracer)); } #endregion diff --git a/src/Hid.Net/Windows/WindowsHidDeviceFactory.cs b/src/Hid.Net/Windows/WindowsHidDeviceFactory.cs index 811afff9..8fbb6dbf 100644 --- a/src/Hid.Net/Windows/WindowsHidDeviceFactory.cs +++ b/src/Hid.Net/Windows/WindowsHidDeviceFactory.cs @@ -77,14 +77,20 @@ public static ConnectedDeviceDefinition GetDeviceDefinition(string deviceId, Saf #endregion #region Public Static Methods + /// + /// Register the factory for enumerating Hid devices on UWP. Warning: no tracing or logging will be used. Please user the other constructor overload for logging and tracing + /// public static void Register() { - Register(null); + Register(null, null); } - public static void Register(ILogger logger) + /// + /// Register the factory for enumerating Hid devices on UWP. + /// + public static void Register(ILogger logger, ITracer tracer) { - DeviceManager.Current.DeviceFactories.Add(new WindowsHidDeviceFactory() { Logger = logger }); + DeviceManager.Current.DeviceFactories.Add(new WindowsHidDeviceFactory(logger, tracer)); } #endregion } diff --git a/src/Usb.Net.Android/AndroidUsbDeviceFactory.cs b/src/Usb.Net.Android/AndroidUsbDeviceFactory.cs index 7449e785..b83f4e01 100644 --- a/src/Usb.Net.Android/AndroidUsbDeviceFactory.cs +++ b/src/Usb.Net.Android/AndroidUsbDeviceFactory.cs @@ -16,7 +16,7 @@ public class AndroidUsbDeviceFactory : IDeviceFactory #region Public Properties public UsbManager UsbManager { get; } public Context Context { get; } - public ILogger Logger { get; } + public ILogger Logger { get; } public ITracer Tracer { get; } #endregion @@ -57,7 +57,7 @@ public IDevice GetDevice(ConnectedDeviceDefinition deviceDefinition) throw new Exception($"The device Id '{deviceDefinition.DeviceId}' is not a valid integer"); } - return new AndroidUsbDevice(UsbManager, Context, deviceId, Logger, Tra) { Logger = Logger }; + return new AndroidUsbDevice(UsbManager, Context, deviceId, Logger, Tracer); } #endregion diff --git a/src/Usb.Net.UWP/UWPUsbDevice.cs b/src/Usb.Net.UWP/UWPUsbDevice.cs index 0a89602e..b2ce621c 100644 --- a/src/Usb.Net.UWP/UWPUsbDevice.cs +++ b/src/Usb.Net.UWP/UWPUsbDevice.cs @@ -30,7 +30,7 @@ public UWPUsbDevice(ILogger logger, ITracer tracer) : this(null, logger, tracer) { } - public UWPUsbDevice(ConnectedDeviceDefinition deviceDefinition, ILogger logger, ITracer tracer) : base(deviceDefinition.DeviceId) + public UWPUsbDevice(ConnectedDeviceDefinition deviceDefinition, ILogger logger, ITracer tracer) : base(deviceDefinition.DeviceId, logger, tracer) { ConnectedDeviceDefinition = deviceDefinition; } diff --git a/src/Usb.Net.UWP/UWPUsbDeviceFactory.cs b/src/Usb.Net.UWP/UWPUsbDeviceFactory.cs index f4773a6f..8e7b2a09 100644 --- a/src/Usb.Net.UWP/UWPUsbDeviceFactory.cs +++ b/src/Usb.Net.UWP/UWPUsbDeviceFactory.cs @@ -22,6 +22,12 @@ protected override string GetAqsFilter(uint? vendorId, uint? productId) } #endregion + #region Constructur + public UWPUsbDeviceFactory(ILogger logger, ITracer tracer) : base(logger, tracer) + { + } + #endregion + #region Public Methods public IDevice GetDevice(ConnectedDeviceDefinition deviceDefinition) { @@ -32,22 +38,22 @@ public IDevice GetDevice(ConnectedDeviceDefinition deviceDefinition) #region Public Static Methods public static void Register() { - Register(null); + Register(null, null); } - public static void Register(ILogger logger) + public static void Register(ILogger logger, ITracer tracer) { foreach (var deviceFactory in DeviceManager.Current.DeviceFactories) { if (deviceFactory is UWPUsbDeviceFactory) return; } - DeviceManager.Current.DeviceFactories.Add(new UWPUsbDeviceFactory() { Logger = logger }); + DeviceManager.Current.DeviceFactories.Add(new UWPUsbDeviceFactory(logger, tracer)); } #endregion #region Public Overrides - public override Task TestConnection(string Id) => Task.FromResult(new ConnectionInfo {CanConnect=true }); + public override Task TestConnection(string Id) => Task.FromResult(new ConnectionInfo { CanConnect = true }); #endregion } } diff --git a/src/Usb.Net/Windows/WindowsUsbDeviceFactory.cs b/src/Usb.Net/Windows/WindowsUsbDeviceFactory.cs index 130573e7..a1101757 100644 --- a/src/Usb.Net/Windows/WindowsUsbDeviceFactory.cs +++ b/src/Usb.Net/Windows/WindowsUsbDeviceFactory.cs @@ -18,10 +18,16 @@ public class WindowsUsbDeviceFactory : WindowsDeviceFactoryBase, IDeviceFactory protected override Guid GetClassGuid() => WindowsDeviceConstants.WinUSBGuid; #endregion + #region Constructor + public WindowsUsbDeviceFactory(ILogger logger, ITracer tracer) : base(logger, tracer) + { + } + #endregion + #region Public Methods public IDevice GetDevice(ConnectedDeviceDefinition deviceDefinition) { - return deviceDefinition.DeviceType != DeviceType ? null : new WindowsUsbDevice(deviceDefinition.DeviceId) { Logger = Logger }; + return deviceDefinition.DeviceType != DeviceType ? null : new WindowsUsbDevice(deviceDefinition.DeviceId, Logger, Tracer); } #endregion @@ -33,14 +39,20 @@ protected override ConnectedDeviceDefinition GetDeviceDefinition(string deviceId #endregion #region Public Static Methods + /// + /// Register the factory for enumerating USB devices on UWP. Warning: no tracing or logging will be used. Please user the other constructor overload for logging and tracing + /// public static void Register() { - Register(null); + Register(null, null); } - public static void Register(ILogger logger) + /// + /// Register the factory for enumerating USB devices on UWP. + /// + public static void Register(ILogger logger, ITracer tracer) { - DeviceManager.Current.DeviceFactories.Add(new WindowsUsbDeviceFactory() { Logger = logger }); + DeviceManager.Current.DeviceFactories.Add(new WindowsUsbDeviceFactory(logger, tracer)); } #endregion } From c7d05101aafdc16b8dfa9a4f594fc858e03db9b3 Mon Sep 17 00:00:00 2001 From: Christian Date: Sun, 23 Jun 2019 13:13:09 +1000 Subject: [PATCH 04/18] All compiling --- src/Device.Net.LibUsb/LibUsbDeviceFactoryBase.cs | 11 ++++++++++- src/Device.Net.LibUsb/LibUsbUsbDeviceFactory.cs | 14 ++++++++------ src/Device.Net.UnitTests/UnitTests.cs | 6 ++++-- src/Device.Net/DebugTracer.cs | 2 +- src/Hid.Net.UWP/UWPHidDeviceFactory.cs | 8 +++----- src/Hid.Net/Windows/WindowsHidDeviceFactory.cs | 8 -------- src/Usb.Net.Android/AndroidUsbDeviceFactory.cs | 8 +++----- src/Usb.Net.AndroidSample/MainActivity.cs | 6 ++---- src/Usb.Net.UWP/UWPUsbDeviceFactory.cs | 8 +++----- src/Usb.Net.WindowsSample/Program.cs | 10 +++++++--- src/Usb.Net/Windows/WindowsUsbDeviceFactory.cs | 10 +--------- 11 files changed, 42 insertions(+), 49 deletions(-) diff --git a/src/Device.Net.LibUsb/LibUsbDeviceFactoryBase.cs b/src/Device.Net.LibUsb/LibUsbDeviceFactoryBase.cs index cce9b530..d531e8ae 100644 --- a/src/Device.Net.LibUsb/LibUsbDeviceFactoryBase.cs +++ b/src/Device.Net.LibUsb/LibUsbDeviceFactoryBase.cs @@ -9,7 +9,8 @@ namespace Device.Net.LibUsb public abstract class LibUsbDeviceFactoryBase : IDeviceFactory { #region Public Properties - public ILogger Logger { get; set; } + public ILogger Logger { get; } + public ITracer Tracer { get; } #endregion #region Public Abstraction Properties @@ -59,5 +60,13 @@ public IDevice GetDevice(ConnectedDeviceDefinition deviceDefinition) return usbDevice != null ? new LibUsbDevice(usbDevice, 3000) { Logger = Logger } : null; } #endregion + + #region Constructor + protected LibUsbDeviceFactoryBase(ILogger logger, ITracer tracer) + { + Logger = logger; + Tracer = tracer; + } + #endregion } } diff --git a/src/Device.Net.LibUsb/LibUsbUsbDeviceFactory.cs b/src/Device.Net.LibUsb/LibUsbUsbDeviceFactory.cs index 933e5686..2296806d 100644 --- a/src/Device.Net.LibUsb/LibUsbUsbDeviceFactory.cs +++ b/src/Device.Net.LibUsb/LibUsbUsbDeviceFactory.cs @@ -2,16 +2,18 @@ { public class LibUsbUsbDeviceFactory : LibUsbDeviceFactoryBase { - public override DeviceType DeviceType => DeviceType.Usb; - - public static void Register() + public LibUsbUsbDeviceFactory(ILogger logger, ITracer tracer) : base(logger, tracer) { - Register(null); } - public static void Register(ILogger logger) + public override DeviceType DeviceType => DeviceType.Usb; + + /// + /// Register the factory for enumerating USB devices. + /// + public static void Register(ILogger logger, ITracer tracer) { - DeviceManager.Current.DeviceFactories.Add(new LibUsbUsbDeviceFactory() { Logger = logger }); + DeviceManager.Current.DeviceFactories.Add(new LibUsbUsbDeviceFactory(logger, tracer)); } } } diff --git a/src/Device.Net.UnitTests/UnitTests.cs b/src/Device.Net.UnitTests/UnitTests.cs index 02f9f05e..5e4cb966 100644 --- a/src/Device.Net.UnitTests/UnitTests.cs +++ b/src/Device.Net.UnitTests/UnitTests.cs @@ -14,8 +14,9 @@ public class UnitTests [TestInitialize] public void Startup() { - MockHidFactory.Register(null); - MockUsbFactory.Register(null); + //TODO: Test logging and tracing + MockHidFactory.Register(null, null); + MockUsbFactory.Register(null, null); } [TestMethod] @@ -45,6 +46,7 @@ public async Task TestWriteAndReadThreadSafety(bool isHidConnected, bool isUsbCo MockHidFactory.IsConnectedStatic = isHidConnected; MockUsbFactory.IsConnectedStatic = isUsbConnected; var connectedDeviceDefinition = (await DeviceManager.Current.GetConnectedDeviceDefinitionsAsync(new FilterDeviceDefinition { ProductId = pid, VendorId = vid })).ToList().First(); + //TODO: Test logging and tracing var mockHidDevice = new MockHidDevice(new DebugLogger(), new DebugTracer()) { DeviceId = connectedDeviceDefinition.DeviceId }; var writeAndReadTasks = new List>(); diff --git a/src/Device.Net/DebugTracer.cs b/src/Device.Net/DebugTracer.cs index 0f8f242d..b5f4b261 100644 --- a/src/Device.Net/DebugTracer.cs +++ b/src/Device.Net/DebugTracer.cs @@ -6,7 +6,7 @@ public class DebugTracer : ITracer { public void Trace(bool isWrite, byte[] data) { - Debug.WriteLine($"({string.Join(",", data)}) - {(isWrite ? "Write" : "Read")} ({data.Length})"); + Debug.WriteLine($"{(isWrite ? "Write" : "Read")}: ({string.Join(",", data)}) ({data.Length})"); } } } diff --git a/src/Hid.Net.UWP/UWPHidDeviceFactory.cs b/src/Hid.Net.UWP/UWPHidDeviceFactory.cs index 30ba4e57..37a159da 100644 --- a/src/Hid.Net.UWP/UWPHidDeviceFactory.cs +++ b/src/Hid.Net.UWP/UWPHidDeviceFactory.cs @@ -89,11 +89,9 @@ public void Dispose() #endregion #region Public Static Methods - public static void Register() - { - Register(null, null); - } - + /// + /// Register the factory for enumerating Hid devices on UWP. + /// public static void Register(ILogger logger, ITracer tracer) { foreach (var deviceFactory in DeviceManager.Current.DeviceFactories) diff --git a/src/Hid.Net/Windows/WindowsHidDeviceFactory.cs b/src/Hid.Net/Windows/WindowsHidDeviceFactory.cs index 8fbb6dbf..ec30a681 100644 --- a/src/Hid.Net/Windows/WindowsHidDeviceFactory.cs +++ b/src/Hid.Net/Windows/WindowsHidDeviceFactory.cs @@ -77,14 +77,6 @@ public static ConnectedDeviceDefinition GetDeviceDefinition(string deviceId, Saf #endregion #region Public Static Methods - /// - /// Register the factory for enumerating Hid devices on UWP. Warning: no tracing or logging will be used. Please user the other constructor overload for logging and tracing - /// - public static void Register() - { - Register(null, null); - } - /// /// Register the factory for enumerating Hid devices on UWP. /// diff --git a/src/Usb.Net.Android/AndroidUsbDeviceFactory.cs b/src/Usb.Net.Android/AndroidUsbDeviceFactory.cs index b83f4e01..eadafab4 100644 --- a/src/Usb.Net.Android/AndroidUsbDeviceFactory.cs +++ b/src/Usb.Net.Android/AndroidUsbDeviceFactory.cs @@ -77,11 +77,9 @@ public static ConnectedDeviceDefinition GetAndroidDeviceDefinition(UsbDevice usb }; } - public static void Register(UsbManager usbManager, Context context) - { - Register(usbManager, context, null, null); - } - + /// + /// Register the factory for enumerating USB devices on Android. + /// public static void Register(UsbManager usbManager, Context context, ILogger logger, ITracer tracer) { DeviceManager.Current.DeviceFactories.Add(new AndroidUsbDeviceFactory(usbManager, context, logger, tracer)); diff --git a/src/Usb.Net.AndroidSample/MainActivity.cs b/src/Usb.Net.AndroidSample/MainActivity.cs index 73a4379c..50b52cb2 100644 --- a/src/Usb.Net.AndroidSample/MainActivity.cs +++ b/src/Usb.Net.AndroidSample/MainActivity.cs @@ -1,5 +1,4 @@ using Android.App; -using Android.Content; using Android.Hardware.Usb; using Android.OS; using Android.Support.Design.Widget; @@ -8,7 +7,6 @@ using Android.Views; using Device.Net; using System; -using Usb.Net.Android; using Usb.Net.Sample; namespace Usb.Net.AndroidSample @@ -69,7 +67,7 @@ private void FabOnClick(object sender, EventArgs eventArgs) if (usbManager == null) throw new Exception("UsbManager is null"); //Register the factory for creating Usb devices. This only needs to be done once. - AndroidUsbDeviceFactory.Register(usbManager, base.ApplicationContext); + AndroidUsbDeviceFactory.Register(usbManager, base.ApplicationContext, new DebugLogger(), new DebugTracer()); _TrezorExample.TrezorDisconnected += _TrezorExample_TrezorDisconnected; _TrezorExample.TrezorInitialized += _TrezorExample_TrezorInitialized; @@ -82,7 +80,7 @@ private void FabOnClick(object sender, EventArgs eventArgs) DisplayMessage("Waiting for device..."); } - catch(Exception ex) + catch (Exception ex) { DisplayMessage("Failed to start listener..." + ex.Message); } diff --git a/src/Usb.Net.UWP/UWPUsbDeviceFactory.cs b/src/Usb.Net.UWP/UWPUsbDeviceFactory.cs index 8e7b2a09..a0bfdad3 100644 --- a/src/Usb.Net.UWP/UWPUsbDeviceFactory.cs +++ b/src/Usb.Net.UWP/UWPUsbDeviceFactory.cs @@ -36,11 +36,9 @@ public IDevice GetDevice(ConnectedDeviceDefinition deviceDefinition) #endregion #region Public Static Methods - public static void Register() - { - Register(null, null); - } - + /// + /// Register the factory for enumerating USB devices on UWP. + /// public static void Register(ILogger logger, ITracer tracer) { foreach (var deviceFactory in DeviceManager.Current.DeviceFactories) diff --git a/src/Usb.Net.WindowsSample/Program.cs b/src/Usb.Net.WindowsSample/Program.cs index 64b1ed3d..ed45c4e9 100644 --- a/src/Usb.Net.WindowsSample/Program.cs +++ b/src/Usb.Net.WindowsSample/Program.cs @@ -17,7 +17,11 @@ internal class Program { #region Fields private static TrezorExample _DeviceConnectionExample = new TrezorExample(); + /// + /// TODO: Test these! + /// private static DebugLogger Logger = new DebugLogger(); + private static DebugTracer Tracer = new DebugTracer(); #endregion #region Main @@ -25,10 +29,10 @@ private static void Main(string[] args) { //Register the factory for creating Usb devices. This only needs to be done once. #if (LIBUSB) - LibUsbUsbDeviceFactory.Register(Logger); + LibUsbUsbDeviceFactory.Register(Logger, Tracer); #else - WindowsUsbDeviceFactory.Register(Logger); - WindowsHidDeviceFactory.Register(Logger); + WindowsUsbDeviceFactory.Register(Logger, Tracer); + WindowsHidDeviceFactory.Register(Logger, Tracer); #endif _DeviceConnectionExample.TrezorInitialized += _DeviceConnectionExample_TrezorInitialized; diff --git a/src/Usb.Net/Windows/WindowsUsbDeviceFactory.cs b/src/Usb.Net/Windows/WindowsUsbDeviceFactory.cs index a1101757..b0aece87 100644 --- a/src/Usb.Net/Windows/WindowsUsbDeviceFactory.cs +++ b/src/Usb.Net/Windows/WindowsUsbDeviceFactory.cs @@ -40,15 +40,7 @@ protected override ConnectedDeviceDefinition GetDeviceDefinition(string deviceId #region Public Static Methods /// - /// Register the factory for enumerating USB devices on UWP. Warning: no tracing or logging will be used. Please user the other constructor overload for logging and tracing - /// - public static void Register() - { - Register(null, null); - } - - /// - /// Register the factory for enumerating USB devices on UWP. + /// Register the factory for enumerating USB devices in Windows. /// public static void Register(ILogger logger, ITracer tracer) { From 0a92b209fe2f3013b6294d8c191c04d1b76483ea Mon Sep 17 00:00:00 2001 From: Christian Date: Sun, 23 Jun 2019 13:18:23 +1000 Subject: [PATCH 05/18] Remove all duplicate traces --- src/Device.Net.UWP/UWPDeviceBase - Generic.cs | 2 +- src/Hid.Net.UWP/UWPHidDevice.cs | 1 - src/Hid.Net/Windows/WindowsHIDDevice.cs | 4 ---- src/Usb.Net.Android/AndroidUsbDevice.cs | 4 ---- src/Usb.Net.UWP/UWPUsbDevice.cs | 2 -- src/Usb.Net/Windows/WindowsUsbDevice.cs | 2 -- 6 files changed, 1 insertion(+), 14 deletions(-) diff --git a/src/Device.Net.UWP/UWPDeviceBase - Generic.cs b/src/Device.Net.UWP/UWPDeviceBase - Generic.cs index 3c741e30..08ecc9a7 100644 --- a/src/Device.Net.UWP/UWPDeviceBase - Generic.cs +++ b/src/Device.Net.UWP/UWPDeviceBase - Generic.cs @@ -49,7 +49,7 @@ public override async Task ReadAsync() if (Chunks.Count > 0) { var retVal = Chunks[0]; - Tracer?.Trace(false, retVal); + Logger?.Log("Received data from device", GetType().Name, null, LogLevel.Information); Chunks.RemoveAt(0); return retVal; } diff --git a/src/Hid.Net.UWP/UWPHidDevice.cs b/src/Hid.Net.UWP/UWPHidDevice.cs index 9931d300..ecb269f7 100644 --- a/src/Hid.Net.UWP/UWPHidDevice.cs +++ b/src/Hid.Net.UWP/UWPHidDevice.cs @@ -113,7 +113,6 @@ public async Task WriteReportAsync(byte[] data, byte? reportId) { var operation = ConnectedDevice.SendOutputReportAsync(outReport); await operation.AsTask(); - Tracer?.Trace(false, bytes); } catch (ArgumentException ex) { diff --git a/src/Hid.Net/Windows/WindowsHIDDevice.cs b/src/Hid.Net/Windows/WindowsHIDDevice.cs index 0e8cd139..2e2c2360 100644 --- a/src/Hid.Net/Windows/WindowsHIDDevice.cs +++ b/src/Hid.Net/Windows/WindowsHIDDevice.cs @@ -190,8 +190,6 @@ public async Task ReadReportAsync() var retVal = ReadBufferHasReportId ? RemoveFirstByte(bytes) : bytes; - Tracer?.Trace(false, retVal); - return new ReadReport(reportId, retVal); } @@ -240,8 +238,6 @@ public async Task WriteReportAsync(byte[] data, byte? reportId) Log(Helpers.WriteErrorMessage, ex); throw new IOException(Helpers.WriteErrorMessage, ex); } - - Tracer?.Trace(true, bytes); } else { diff --git a/src/Usb.Net.Android/AndroidUsbDevice.cs b/src/Usb.Net.Android/AndroidUsbDevice.cs index b23a0385..febbcaa6 100644 --- a/src/Usb.Net.Android/AndroidUsbDevice.cs +++ b/src/Usb.Net.Android/AndroidUsbDevice.cs @@ -114,8 +114,6 @@ public override async Task ReadAsync() //Marshal.Copy(byteBuffer.GetDirectBufferAddress(), buffers, 0, ReadBufferLength); - Tracer?.Trace(false, buffers); - return buffers; } catch (Exception ex) @@ -134,8 +132,6 @@ public override async Task WriteAsync(byte[] data) request.Initialize(_UsbDeviceConnection, _WriteEndpoint); var byteBuffer = ByteBuffer.Wrap(data); - Tracer?.Trace(true, data); - #pragma warning disable CS0618 // Type or member is obsolete request.Queue(byteBuffer, data.Length); #pragma warning restore CS0618 // Type or member is obsolete diff --git a/src/Usb.Net.UWP/UWPUsbDevice.cs b/src/Usb.Net.UWP/UWPUsbDevice.cs index b2ce621c..a7089377 100644 --- a/src/Usb.Net.UWP/UWPUsbDevice.cs +++ b/src/Usb.Net.UWP/UWPUsbDevice.cs @@ -106,8 +106,6 @@ public override async Task WriteAsync(byte[] data) if (data.Length > WriteBufferSize) throw new Exception("The buffer size is too large"); await _DefaultOutPipe.OutputStream.WriteAsync(data.AsBuffer()); - - Tracer?.Trace(false, data); } #endregion } diff --git a/src/Usb.Net/Windows/WindowsUsbDevice.cs b/src/Usb.Net/Windows/WindowsUsbDevice.cs index 863774bd..e552066a 100644 --- a/src/Usb.Net/Windows/WindowsUsbDevice.cs +++ b/src/Usb.Net/Windows/WindowsUsbDevice.cs @@ -108,7 +108,6 @@ public override async Task ReadAsync() //TODO: Allow for different interfaces and pipes... var isSuccess = WinUsbApiCalls.WinUsb_ReadPipe(_DefaultUsbInterface.Handle, _DefaultUsbInterface.ReadPipe.WINUSB_PIPE_INFORMATION.PipeId, bytes, ReadBufferSize, out var bytesRead, IntPtr.Zero); HandleError(isSuccess, "Couldn't read data"); - Tracer?.Trace(false, bytes); return bytes; }); } @@ -125,7 +124,6 @@ await Task.Run(() => //TODO: Allow for different interfaces and pipes... var isSuccess = WinUsbApiCalls.WinUsb_WritePipe(_DefaultUsbInterface.Handle, _DefaultUsbInterface.WritePipe.WINUSB_PIPE_INFORMATION.PipeId, data, (uint)data.Length, out var bytesWritten, IntPtr.Zero); HandleError(isSuccess, "Couldn't write data"); - Tracer?.Trace(true, data); }); } From 602451d28f9d2b41fbc05eda6bc2d6d57a3f81c3 Mon Sep 17 00:00:00 2001 From: Christian Date: Sun, 23 Jun 2019 13:21:48 +1000 Subject: [PATCH 06/18] Fix the UWP sample --- src/Device.Net - All.sln | 2 ++ src/Usb.Net.UWP.Sample/MainPage.xaml.cs | 5 +++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Device.Net - All.sln b/src/Device.Net - All.sln index 314122a3..a1aeedb4 100644 --- a/src/Device.Net - All.sln +++ b/src/Device.Net - All.sln @@ -72,6 +72,8 @@ Global {8E29E247-78B7-4FC8-AF1C-DA273050978C}.Release|Any CPU.ActiveCfg = Release|Any CPU {8E29E247-78B7-4FC8-AF1C-DA273050978C}.Release|Any CPU.Build.0 = Release|Any CPU {63695F84-3EC6-4F6A-8C8C-5219D75EDB68}.Debug|Any CPU.ActiveCfg = Debug|x86 + {63695F84-3EC6-4F6A-8C8C-5219D75EDB68}.Debug|Any CPU.Build.0 = Debug|x86 + {63695F84-3EC6-4F6A-8C8C-5219D75EDB68}.Debug|Any CPU.Deploy.0 = Debug|x86 {63695F84-3EC6-4F6A-8C8C-5219D75EDB68}.Release|Any CPU.ActiveCfg = Release|x86 {63695F84-3EC6-4F6A-8C8C-5219D75EDB68}.Release|Any CPU.Build.0 = Release|x86 {63695F84-3EC6-4F6A-8C8C-5219D75EDB68}.Release|Any CPU.Deploy.0 = Release|x86 diff --git a/src/Usb.Net.UWP.Sample/MainPage.xaml.cs b/src/Usb.Net.UWP.Sample/MainPage.xaml.cs index d9d84b57..cc90b862 100644 --- a/src/Usb.Net.UWP.Sample/MainPage.xaml.cs +++ b/src/Usb.Net.UWP.Sample/MainPage.xaml.cs @@ -29,12 +29,13 @@ public MainPage() _DeviceConnectionExample.TrezorDisconnected += _DeviceConnectionExample_TrezorDisconnected; var logger = new DebugLogger(); + var tracer = new DebugTracer(); //Register the factory for creating Usb devices. This only needs to be done once. - UWPUsbDeviceFactory.Register(logger); + UWPUsbDeviceFactory.Register(logger, tracer); //Register the factory for creating Usb devices. This only needs to be done once. - UWPHidDeviceFactory.Register(logger); + UWPHidDeviceFactory.Register(logger, tracer); } #endregion From f2598153a730160a1f651a769b4bba94c2c74c77 Mon Sep 17 00:00:00 2001 From: Christian Date: Sun, 23 Jun 2019 13:36:24 +1000 Subject: [PATCH 07/18] Add tracing to unit tests --- src/Device.Net.UnitTests/MockTracer.cs | 17 +++++++++++++++++ src/Device.Net.UnitTests/UnitTests.cs | 14 +++++++++++--- 2 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 src/Device.Net.UnitTests/MockTracer.cs diff --git a/src/Device.Net.UnitTests/MockTracer.cs b/src/Device.Net.UnitTests/MockTracer.cs new file mode 100644 index 00000000..ff4e471d --- /dev/null +++ b/src/Device.Net.UnitTests/MockTracer.cs @@ -0,0 +1,17 @@ +using System; + +namespace Device.Net.UnitTests +{ + public class MockTracer : ITracer + { + public int WriteCount { get; private set; } + public int ReadCount { get; private set; } + + public void Trace(bool isWrite, byte[] data) + { + if (data == null) throw new Exception("data must not be null"); + if (isWrite) WriteCount++; + if (!isWrite) ReadCount++; + } + } +} diff --git a/src/Device.Net.UnitTests/UnitTests.cs b/src/Device.Net.UnitTests/UnitTests.cs index 5e4cb966..44915be4 100644 --- a/src/Device.Net.UnitTests/UnitTests.cs +++ b/src/Device.Net.UnitTests/UnitTests.cs @@ -46,14 +46,19 @@ public async Task TestWriteAndReadThreadSafety(bool isHidConnected, bool isUsbCo MockHidFactory.IsConnectedStatic = isHidConnected; MockUsbFactory.IsConnectedStatic = isUsbConnected; var connectedDeviceDefinition = (await DeviceManager.Current.GetConnectedDeviceDefinitionsAsync(new FilterDeviceDefinition { ProductId = pid, VendorId = vid })).ToList().First(); - //TODO: Test logging and tracing - var mockHidDevice = new MockHidDevice(new DebugLogger(), new DebugTracer()) { DeviceId = connectedDeviceDefinition.DeviceId }; + + var logger = new DebugLogger(); + var tracer = new MockTracer(); + + var mockHidDevice = new MockHidDevice(logger, tracer) { DeviceId = connectedDeviceDefinition.DeviceId }; var writeAndReadTasks = new List>(); //TODO: Does this properly test thread safety? - for (byte i = 0; i < 10; i++) + const int count = 10; + + for (byte i = 0; i < count; i++) { writeAndReadTasks.Add(mockHidDevice.WriteAndReadAsync(new byte[64] { i, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 })); } @@ -65,6 +70,9 @@ public async Task TestWriteAndReadThreadSafety(bool isHidConnected, bool isUsbCo var result = results[i]; Assert.IsTrue(result[0] == i); } + + Assert.AreEqual(count, tracer.ReadCount); + Assert.AreEqual(count, tracer.WriteCount); } [TestMethod] From e057ddbb3e87e18fb6c2521fa03c537f152360c4 Mon Sep 17 00:00:00 2001 From: Christian Date: Sun, 23 Jun 2019 13:54:34 +1000 Subject: [PATCH 08/18] Failing unit test --- src/Device.Net.UnitTests/MockFactoryBase.cs | 9 +++--- src/Device.Net.UnitTests/MockHidFactory.cs | 9 +++++- src/Device.Net.UnitTests/MockLogger.cs | 17 +++++++++++ src/Device.Net.UnitTests/UnitTests.cs | 33 ++++++++++++++++----- 4 files changed, 55 insertions(+), 13 deletions(-) create mode 100644 src/Device.Net.UnitTests/MockLogger.cs diff --git a/src/Device.Net.UnitTests/MockFactoryBase.cs b/src/Device.Net.UnitTests/MockFactoryBase.cs index 596f22fb..4448b24f 100644 --- a/src/Device.Net.UnitTests/MockFactoryBase.cs +++ b/src/Device.Net.UnitTests/MockFactoryBase.cs @@ -1,5 +1,4 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Threading.Tasks; namespace Device.Net.UnitTests @@ -12,8 +11,8 @@ public abstract class MockFactoryBase : IDeviceFactory public abstract DeviceType DeviceType { get; } - public ILogger Logger { get; } - public ITracer Tracer { get; } + public ILogger Logger { get; } + public ITracer Tracer { get; } protected MockFactoryBase(ILogger logger, ITracer tracer) { @@ -28,7 +27,7 @@ public Task> GetConnectedDeviceDefinition { var result = new List(); - var mockConnectedDeviceDefinition = new ConnectedDeviceDefinition(DeviceId) { ProductId = ProductId, VendorId = VendorId }; + var mockConnectedDeviceDefinition = new ConnectedDeviceDefinition(DeviceId) { ProductId = ProductId, VendorId = VendorId, DeviceType = deviceDefinition.DeviceType }; if (IsConnected) { diff --git a/src/Device.Net.UnitTests/MockHidFactory.cs b/src/Device.Net.UnitTests/MockHidFactory.cs index f19ee0bd..628b6d08 100644 --- a/src/Device.Net.UnitTests/MockHidFactory.cs +++ b/src/Device.Net.UnitTests/MockHidFactory.cs @@ -4,6 +4,8 @@ namespace Device.Net.UnitTests { public class MockHidFactory : MockFactoryBase, IDeviceFactory { + public const string FoundMessage = "Found device {0}"; + public MockHidFactory(ILogger logger, ITracer tracer) : base(logger, tracer) { } @@ -31,7 +33,12 @@ public override IDevice GetDevice(ConnectedDeviceDefinition deviceDefinition) { if (deviceDefinition.DeviceId == DeviceId) { - if (!deviceDefinition.DeviceType.HasValue || deviceDefinition.DeviceType == DeviceType.Hid) return new MockHidDevice(Logger, Tracer); + if (!deviceDefinition.DeviceType.HasValue || deviceDefinition.DeviceType == DeviceType.Hid) + { + Logger?.Log(string.Format(FoundMessage, DeviceId), nameof(MockHidFactory), null, LogLevel.Information); + + return new MockHidDevice(Logger, Tracer); + } } } diff --git a/src/Device.Net.UnitTests/MockLogger.cs b/src/Device.Net.UnitTests/MockLogger.cs new file mode 100644 index 00000000..1cf5f8ff --- /dev/null +++ b/src/Device.Net.UnitTests/MockLogger.cs @@ -0,0 +1,17 @@ +using System; +using System.Text; + +namespace Device.Net.UnitTests +{ + internal class MockLogger : ILogger + { + StringBuilder _stringBuilder = new StringBuilder(); + + public string LogText => _stringBuilder.ToString(); + + public void Log(string message, string region, Exception ex, LogLevel logLevel) + { + _stringBuilder.Append(message); + } + } +} \ No newline at end of file diff --git a/src/Device.Net.UnitTests/UnitTests.cs b/src/Device.Net.UnitTests/UnitTests.cs index 44915be4..8363a0d9 100644 --- a/src/Device.Net.UnitTests/UnitTests.cs +++ b/src/Device.Net.UnitTests/UnitTests.cs @@ -10,13 +10,15 @@ namespace Device.Net.UnitTests [TestClass] public class UnitTests { + private static MockLogger logger = new MockLogger(); + private static MockTracer tracer = new MockTracer(); + #region Tests [TestInitialize] public void Startup() { - //TODO: Test logging and tracing - MockHidFactory.Register(null, null); - MockUsbFactory.Register(null, null); + MockHidFactory.Register(logger, tracer); + MockUsbFactory.Register(logger, tracer); } [TestMethod] @@ -34,6 +36,20 @@ public async Task TestWithMatchedFilterAsync(bool isHidConnected, bool isUsbConn MockHidFactory.IsConnectedStatic = isHidConnected; MockUsbFactory.IsConnectedStatic = isUsbConnected; var connectedDeviceDefinitions = (await DeviceManager.Current.GetConnectedDeviceDefinitionsAsync(new FilterDeviceDefinition { ProductId = pid, VendorId = vid })).ToList(); + + if (connectedDeviceDefinitions.Count > 0) + { + foreach (var connectedDeviceDefinition in connectedDeviceDefinitions) + { + DeviceManager.Current.GetDevice(connectedDeviceDefinition); + + if (connectedDeviceDefinition.DeviceType == DeviceType.Hid) + { + Assert.IsTrue(logger.LogText.Contains(string.Format(MockHidFactory.FoundMessage, connectedDeviceDefinition.DeviceId))); + } + } + } + Assert.IsNotNull(connectedDeviceDefinitions); Assert.AreEqual(expectedCount, connectedDeviceDefinitions.Count); } @@ -43,12 +59,13 @@ public async Task TestWithMatchedFilterAsync(bool isHidConnected, bool isUsbConn [DataRow(true, false, MockHidDevice.VendorId, MockHidDevice.ProductId)] public async Task TestWriteAndReadThreadSafety(bool isHidConnected, bool isUsbConnected, uint vid, uint pid) { + var readtraceCount = tracer.ReadCount; + var writetraceCount = tracer.WriteCount; + MockHidFactory.IsConnectedStatic = isHidConnected; MockUsbFactory.IsConnectedStatic = isUsbConnected; var connectedDeviceDefinition = (await DeviceManager.Current.GetConnectedDeviceDefinitionsAsync(new FilterDeviceDefinition { ProductId = pid, VendorId = vid })).ToList().First(); - var logger = new DebugLogger(); - var tracer = new MockTracer(); var mockHidDevice = new MockHidDevice(logger, tracer) { DeviceId = connectedDeviceDefinition.DeviceId }; @@ -71,8 +88,10 @@ public async Task TestWriteAndReadThreadSafety(bool isHidConnected, bool isUsbCo Assert.IsTrue(result[0] == i); } - Assert.AreEqual(count, tracer.ReadCount); - Assert.AreEqual(count, tracer.WriteCount); + Assert.AreEqual(readtraceCount + count, tracer.ReadCount); + Assert.AreEqual(writetraceCount + count, tracer.WriteCount); + + Assert.IsTrue(logger.LogText.Contains("Successfully called ")); } [TestMethod] From 4ee9640d0dab110d114a77cb8a98fc7868bceced Mon Sep 17 00:00:00 2001 From: Christian Date: Sun, 23 Jun 2019 16:47:10 +1000 Subject: [PATCH 09/18] Test pass more good --- src/Device.Net.UnitTests/MockFactoryBase.cs | 2 +- src/Device.Net.UnitTests/MockHidDevice.cs | 2 +- src/Device.Net.UnitTests/MockUsbDevice.cs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Device.Net.UnitTests/MockFactoryBase.cs b/src/Device.Net.UnitTests/MockFactoryBase.cs index 4448b24f..ab6c61f2 100644 --- a/src/Device.Net.UnitTests/MockFactoryBase.cs +++ b/src/Device.Net.UnitTests/MockFactoryBase.cs @@ -27,7 +27,7 @@ public Task> GetConnectedDeviceDefinition { var result = new List(); - var mockConnectedDeviceDefinition = new ConnectedDeviceDefinition(DeviceId) { ProductId = ProductId, VendorId = VendorId, DeviceType = deviceDefinition.DeviceType }; + var mockConnectedDeviceDefinition = new ConnectedDeviceDefinition(DeviceId) { ProductId = ProductId, VendorId = VendorId, DeviceType = DeviceType.Hid }; if (IsConnected) { diff --git a/src/Device.Net.UnitTests/MockHidDevice.cs b/src/Device.Net.UnitTests/MockHidDevice.cs index 0e9887bb..f988a32d 100644 --- a/src/Device.Net.UnitTests/MockHidDevice.cs +++ b/src/Device.Net.UnitTests/MockHidDevice.cs @@ -9,7 +9,7 @@ public class MockHidDevice : MockDeviceBase, IDevice public MockHidDevice(ILogger logger, ITracer tracer) : base(logger, tracer) { DeviceId = MockedDeviceId; - ConnectedDeviceDefinition = new ConnectedDeviceDefinition(DeviceId) { ProductId = ProductId, VendorId = VendorId }; + ConnectedDeviceDefinition = new ConnectedDeviceDefinition(DeviceId) { ProductId = ProductId, VendorId = VendorId, DeviceType = DeviceType.Hid }; } } } diff --git a/src/Device.Net.UnitTests/MockUsbDevice.cs b/src/Device.Net.UnitTests/MockUsbDevice.cs index 98d93bbc..4d71ef98 100644 --- a/src/Device.Net.UnitTests/MockUsbDevice.cs +++ b/src/Device.Net.UnitTests/MockUsbDevice.cs @@ -9,7 +9,7 @@ public class MockUsbDevice : MockDeviceBase, IDevice public MockUsbDevice(ILogger logger, ITracer tracer) : base(logger, tracer) { DeviceId = MockedDeviceId; - ConnectedDeviceDefinition = new ConnectedDeviceDefinition(DeviceId) { ProductId = ProductId, VendorId = VendorId }; + ConnectedDeviceDefinition = new ConnectedDeviceDefinition(DeviceId) { ProductId = ProductId, VendorId = VendorId, DeviceType = DeviceType.Usb }; } } } From 04bfbf277afc6dd705fdbfda62009b225bc4327d Mon Sep 17 00:00:00 2001 From: Christian Date: Sun, 23 Jun 2019 16:58:02 +1000 Subject: [PATCH 10/18] All tests pass --- src/Device.Net.UnitTests/MockHidFactory.cs | 2 +- src/Device.Net.UnitTests/UnitTests.cs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Device.Net.UnitTests/MockHidFactory.cs b/src/Device.Net.UnitTests/MockHidFactory.cs index 628b6d08..64768209 100644 --- a/src/Device.Net.UnitTests/MockHidFactory.cs +++ b/src/Device.Net.UnitTests/MockHidFactory.cs @@ -42,7 +42,7 @@ public override IDevice GetDevice(ConnectedDeviceDefinition deviceDefinition) } } - throw new Exception("Couldn't get a device"); + return null; } } } diff --git a/src/Device.Net.UnitTests/UnitTests.cs b/src/Device.Net.UnitTests/UnitTests.cs index 8363a0d9..d128e842 100644 --- a/src/Device.Net.UnitTests/UnitTests.cs +++ b/src/Device.Net.UnitTests/UnitTests.cs @@ -41,9 +41,9 @@ public async Task TestWithMatchedFilterAsync(bool isHidConnected, bool isUsbConn { foreach (var connectedDeviceDefinition in connectedDeviceDefinitions) { - DeviceManager.Current.GetDevice(connectedDeviceDefinition); + var device = DeviceManager.Current.GetDevice(connectedDeviceDefinition); - if (connectedDeviceDefinition.DeviceType == DeviceType.Hid) + if (device != null && connectedDeviceDefinition.DeviceType == DeviceType.Hid) { Assert.IsTrue(logger.LogText.Contains(string.Format(MockHidFactory.FoundMessage, connectedDeviceDefinition.DeviceId))); } From cc540aed8b611ae1aadc6d7184e7bc1bcc4e4901 Mon Sep 17 00:00:00 2001 From: Christian Date: Sun, 23 Jun 2019 17:07:12 +1000 Subject: [PATCH 11/18] Make test more good --- src/Device.Net.UnitTests/MockFactoryBase.cs | 11 ++++++++++- src/Device.Net.UnitTests/MockUsbFactory.cs | 9 ++++++++- src/Device.Net.UnitTests/UnitTests.cs | 5 +++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/Device.Net.UnitTests/MockFactoryBase.cs b/src/Device.Net.UnitTests/MockFactoryBase.cs index ab6c61f2..dba649c2 100644 --- a/src/Device.Net.UnitTests/MockFactoryBase.cs +++ b/src/Device.Net.UnitTests/MockFactoryBase.cs @@ -27,7 +27,16 @@ public Task> GetConnectedDeviceDefinition { var result = new List(); - var mockConnectedDeviceDefinition = new ConnectedDeviceDefinition(DeviceId) { ProductId = ProductId, VendorId = VendorId, DeviceType = DeviceType.Hid }; + var mockConnectedDeviceDefinition = new ConnectedDeviceDefinition(DeviceId) { ProductId = ProductId, VendorId = VendorId }; + + if (this is MockHidFactory mockHidFactory) + { + mockConnectedDeviceDefinition.DeviceType = DeviceType.Hid; + } + else + { + mockConnectedDeviceDefinition.DeviceType = DeviceType.Usb; + } if (IsConnected) { diff --git a/src/Device.Net.UnitTests/MockUsbFactory.cs b/src/Device.Net.UnitTests/MockUsbFactory.cs index bcd3f641..68242af4 100644 --- a/src/Device.Net.UnitTests/MockUsbFactory.cs +++ b/src/Device.Net.UnitTests/MockUsbFactory.cs @@ -20,6 +20,8 @@ public MockUsbFactory(ILogger logger, ITracer tracer) : base(logger, tracer) public override uint VendorId => MockUsbDevice.VendorId; + public const string FoundMessage = "Found device {0}"; + public static void Register(ILogger logger, ITracer tracer) { DeviceManager.Current.DeviceFactories.Add(new MockUsbFactory(logger, tracer)); @@ -31,7 +33,12 @@ public override IDevice GetDevice(ConnectedDeviceDefinition deviceDefinition) { if (deviceDefinition.DeviceId == DeviceId) { - if (!deviceDefinition.DeviceType.HasValue || deviceDefinition.DeviceType == DeviceType.Usb) return new MockUsbDevice(Logger, Tracer); + if (!deviceDefinition.DeviceType.HasValue || deviceDefinition.DeviceType == DeviceType.Usb) + { + Logger?.Log(string.Format(FoundMessage, DeviceId), nameof(MockUsbFactory), null, LogLevel.Information); + + return new MockUsbDevice(Logger, Tracer); + } } } diff --git a/src/Device.Net.UnitTests/UnitTests.cs b/src/Device.Net.UnitTests/UnitTests.cs index d128e842..38af136f 100644 --- a/src/Device.Net.UnitTests/UnitTests.cs +++ b/src/Device.Net.UnitTests/UnitTests.cs @@ -47,6 +47,11 @@ public async Task TestWithMatchedFilterAsync(bool isHidConnected, bool isUsbConn { Assert.IsTrue(logger.LogText.Contains(string.Format(MockHidFactory.FoundMessage, connectedDeviceDefinition.DeviceId))); } + + if (device != null && connectedDeviceDefinition.DeviceType == DeviceType.Usb) + { + Assert.IsTrue(logger.LogText.Contains(string.Format(MockUsbFactory.FoundMessage, connectedDeviceDefinition.DeviceId))); + } } } From 9ae76c440861720961382c8e2453a92a6a31d150 Mon Sep 17 00:00:00 2001 From: Christian Date: Sun, 23 Jun 2019 17:15:02 +1000 Subject: [PATCH 12/18] Move a constant in to messages --- src/Device.Net.UnitTests/UnitTests.cs | 2 +- src/Device.Net/DeviceBase.cs | 2 +- src/Device.Net/Messages.cs | 7 +++++++ 3 files changed, 9 insertions(+), 2 deletions(-) create mode 100644 src/Device.Net/Messages.cs diff --git a/src/Device.Net.UnitTests/UnitTests.cs b/src/Device.Net.UnitTests/UnitTests.cs index 38af136f..2f2f2563 100644 --- a/src/Device.Net.UnitTests/UnitTests.cs +++ b/src/Device.Net.UnitTests/UnitTests.cs @@ -96,7 +96,7 @@ public async Task TestWriteAndReadThreadSafety(bool isHidConnected, bool isUsbCo Assert.AreEqual(readtraceCount + count, tracer.ReadCount); Assert.AreEqual(writetraceCount + count, tracer.WriteCount); - Assert.IsTrue(logger.LogText.Contains("Successfully called ")); + Assert.IsTrue(logger.LogText.Contains(Messages.SuccessMessageWriteAndReadCalled)); } [TestMethod] diff --git a/src/Device.Net/DeviceBase.cs b/src/Device.Net/DeviceBase.cs index edfcde05..3c57c9ee 100644 --- a/src/Device.Net/DeviceBase.cs +++ b/src/Device.Net/DeviceBase.cs @@ -85,7 +85,7 @@ public async Task WriteAndReadAsync(byte[] writeBuffer) Tracer?.Trace(true, writeBuffer); var retVal = await ReadAsync(); Tracer?.Trace(false, retVal); - Log($"Successfully called {nameof(WriteAndReadAsync)}"); + Log(Messages.SuccessMessageWriteAndReadCalled); return retVal; } catch (Exception ex) diff --git a/src/Device.Net/Messages.cs b/src/Device.Net/Messages.cs new file mode 100644 index 00000000..112cf53e --- /dev/null +++ b/src/Device.Net/Messages.cs @@ -0,0 +1,7 @@ +namespace Device.Net +{ + public static class Messages + { + public static string SuccessMessageWriteAndReadCalled => $"Successfully called {nameof(DeviceBase.WriteAndReadAsync)}"; + } +} From 39bf4acf6418947401e15062bb7db0a63ffa5e7d Mon Sep 17 00:00:00 2001 From: Christian Date: Sun, 23 Jun 2019 17:57:41 +1000 Subject: [PATCH 13/18] Refactor the libusb stuff --- src/Device.Net.LibUsb/LibUsbDevice.cs | 34 ++++++++++++------- .../LibUsbDeviceFactoryBase.cs | 2 +- src/Device.Net/DeviceBase.cs | 1 - src/Device.Net/Messages.cs | 7 ++++ src/Hid.Net.UWP/UWPHidDevice.cs | 15 +++++++- src/Hid.Net/Windows/WindowsHIDDevice.cs | 1 + src/Usb.Net.Android/AndroidUsbDevice.cs | 1 + src/Usb.Net.UWP/UWPUsbDevice.cs | 18 ++++++++-- src/Usb.Net/Windows/WindowsUsbDevice.cs | 1 + 9 files changed, 62 insertions(+), 18 deletions(-) diff --git a/src/Device.Net.LibUsb/LibUsbDevice.cs b/src/Device.Net.LibUsb/LibUsbDevice.cs index ad0c9712..2243f00d 100644 --- a/src/Device.Net.LibUsb/LibUsbDevice.cs +++ b/src/Device.Net.LibUsb/LibUsbDevice.cs @@ -3,12 +3,13 @@ using LibUsbDotNet.Main; using LibUsbDotNet.WinUsb; using System; +using System.IO; using System.Threading; using System.Threading.Tasks; namespace Device.Net.LibUsb { - public class LibUsbDevice : IDevice + public class LibUsbDevice : DeviceBase, IDevice { #region Fields private UsbEndpointReader _UsbEndpointReader; @@ -16,6 +17,7 @@ public class LibUsbDevice : IDevice private int ReadPacketSize; private SemaphoreSlim _WriteAndReadLock = new SemaphoreSlim(1, 1); private bool disposed; + private bool _IsInitialized; #endregion #region Public Properties @@ -23,10 +25,11 @@ public class LibUsbDevice : IDevice public int VendorId => GetVendorId(UsbDevice); public int ProductId => GetProductId(UsbDevice); public int Timeout { get; } - public bool IsInitialized { get; private set; } - public ConnectedDeviceDefinitionBase ConnectedDeviceDefinition => throw new NotImplementedException(); + public override bool IsInitialized => _IsInitialized; public string DeviceId => UsbDevice.DevicePath; public ILogger Logger { get; set; } + public override ushort WriteBufferSize => throw new NotImplementedException(); + public override ushort ReadBufferSize => throw new NotImplementedException(); #endregion #region Events @@ -35,7 +38,7 @@ public class LibUsbDevice : IDevice #endregion #region Constructor - public LibUsbDevice(UsbDevice usbDevice, int timeout) + public LibUsbDevice(UsbDevice usbDevice, int timeout, ILogger logger, ITracer tracer) : base(logger, tracer) { UsbDevice = usbDevice; Timeout = timeout; @@ -86,11 +89,11 @@ await Task.Run(() => _UsbEndpointReader = UsbDevice.OpenEndpointReader(ReadEndpointID.Ep01); ReadPacketSize = _UsbEndpointReader.EndpointInfo.Descriptor.MaxPacketSize; - IsInitialized = true; + _IsInitialized = true; }); } - public async Task ReadAsync() + public override async Task ReadAsync() { await _WriteAndReadLock.WaitAsync(); @@ -111,7 +114,7 @@ public async Task ReadAsync() } } - public async Task WriteAsync(byte[] data) + public override async Task WriteAsync(byte[] data) { await _WriteAndReadLock.WaitAsync(); @@ -119,7 +122,17 @@ public async Task WriteAsync(byte[] data) { await Task.Run(() => { - _UsbEndpointWriter.Write(data, Timeout, out var bytesWritten); + var errorCode = _UsbEndpointWriter.Write(data, Timeout, out var bytesWritten); + if (errorCode == ErrorCode.Ok || errorCode == ErrorCode.Success) + { + Tracer?.Trace(true, data); + } + else + { + var message = $"Error. Write error code: {errorCode}"; + Logger?.Log(message, GetType().Name, null, LogLevel.Error); + throw new IOException(message); + } }); } finally @@ -128,11 +141,6 @@ await Task.Run(() => } } - public async Task WriteAndReadAsync(byte[] writeBuffer) - { - await WriteAsync(writeBuffer); - return await ReadAsync(); - } #endregion #region Public Static Methods diff --git a/src/Device.Net.LibUsb/LibUsbDeviceFactoryBase.cs b/src/Device.Net.LibUsb/LibUsbDeviceFactoryBase.cs index d531e8ae..b584da80 100644 --- a/src/Device.Net.LibUsb/LibUsbDeviceFactoryBase.cs +++ b/src/Device.Net.LibUsb/LibUsbDeviceFactoryBase.cs @@ -57,7 +57,7 @@ public IDevice GetDevice(ConnectedDeviceDefinition deviceDefinition) { var usbDeviceFinder = new UsbDeviceFinder((int)deviceDefinition.VendorId.Value, (int)deviceDefinition.ProductId.Value); var usbDevice = UsbDevice.OpenUsbDevice(usbDeviceFinder); - return usbDevice != null ? new LibUsbDevice(usbDevice, 3000) { Logger = Logger } : null; + return usbDevice != null ? new LibUsbDevice(usbDevice, 3000, Logger, Tracer) : null; } #endregion diff --git a/src/Device.Net/DeviceBase.cs b/src/Device.Net/DeviceBase.cs index 3c57c9ee..342786cf 100644 --- a/src/Device.Net/DeviceBase.cs +++ b/src/Device.Net/DeviceBase.cs @@ -82,7 +82,6 @@ public async Task WriteAndReadAsync(byte[] writeBuffer) try { await WriteAsync(writeBuffer); - Tracer?.Trace(true, writeBuffer); var retVal = await ReadAsync(); Tracer?.Trace(false, retVal); Log(Messages.SuccessMessageWriteAndReadCalled); diff --git a/src/Device.Net/Messages.cs b/src/Device.Net/Messages.cs index 112cf53e..7cd35bc2 100644 --- a/src/Device.Net/Messages.cs +++ b/src/Device.Net/Messages.cs @@ -2,6 +2,13 @@ { public static class Messages { + public const string ErrorMessageNotInitialized = "The device has not been initialized."; + public static string SuccessMessageWriteAndReadCalled => $"Successfully called {nameof(DeviceBase.WriteAndReadAsync)}"; + + public static string GetErrorMessageInvalidWriteLength(int length, uint count) + { + return $"Write failure. {length} bytes were sent to the device but it claims that {count} were sent."; + } } } diff --git a/src/Hid.Net.UWP/UWPHidDevice.cs b/src/Hid.Net.UWP/UWPHidDevice.cs index ecb269f7..c8658543 100644 --- a/src/Hid.Net.UWP/UWPHidDevice.cs +++ b/src/Hid.Net.UWP/UWPHidDevice.cs @@ -1,6 +1,7 @@ using Device.Net; using Device.Net.UWP; using System; +using System.IO; using System.Runtime.InteropServices.WindowsRuntime; using System.Threading.Tasks; using Windows.Devices.HumanInterfaceDevice; @@ -112,7 +113,17 @@ public async Task WriteReportAsync(byte[] data, byte? reportId) try { var operation = ConnectedDevice.SendOutputReportAsync(outReport); - await operation.AsTask(); + var count = await operation.AsTask(); + if (count == bytes.Length) + { + Tracer?.Trace(true, bytes); + } + else + { + var message = Messages.GetErrorMessageInvalidWriteLength(bytes.Length, count); + Logger?.Log(message, GetType().Name, null, LogLevel.Error); + throw new IOException(message); + } } catch (ArgumentException ex) { @@ -124,6 +135,8 @@ public async Task WriteReportAsync(byte[] data, byte? reportId) throw; } } + + #endregion #region Public Overrides diff --git a/src/Hid.Net/Windows/WindowsHIDDevice.cs b/src/Hid.Net/Windows/WindowsHIDDevice.cs index 2e2c2360..8bdf943f 100644 --- a/src/Hid.Net/Windows/WindowsHIDDevice.cs +++ b/src/Hid.Net/Windows/WindowsHIDDevice.cs @@ -232,6 +232,7 @@ public async Task WriteReportAsync(byte[] data, byte? reportId) try { await _WriteFileStream.WriteAsync(bytes, 0, bytes.Length); + Tracer?.Trace(true, bytes); } catch (Exception ex) { diff --git a/src/Usb.Net.Android/AndroidUsbDevice.cs b/src/Usb.Net.Android/AndroidUsbDevice.cs index febbcaa6..9f8774ee 100644 --- a/src/Usb.Net.Android/AndroidUsbDevice.cs +++ b/src/Usb.Net.Android/AndroidUsbDevice.cs @@ -136,6 +136,7 @@ public override async Task WriteAsync(byte[] data) request.Queue(byteBuffer, data.Length); #pragma warning restore CS0618 // Type or member is obsolete await _UsbDeviceConnection.RequestWaitAsync(); + Tracer?.Trace(true, data); } catch (Exception ex) { diff --git a/src/Usb.Net.UWP/UWPUsbDevice.cs b/src/Usb.Net.UWP/UWPUsbDevice.cs index a7089377..379837d6 100644 --- a/src/Usb.Net.UWP/UWPUsbDevice.cs +++ b/src/Usb.Net.UWP/UWPUsbDevice.cs @@ -1,6 +1,7 @@ using Device.Net; using Device.Net.UWP; using System; +using System.IO; using System.Linq; using System.Runtime.InteropServices.WindowsRuntime; using System.Threading.Tasks; @@ -102,10 +103,23 @@ private void InterruptPipe_DataReceived(UsbInterruptInPipe sender, UsbInterruptI #region Public Methods public override async Task WriteAsync(byte[] data) { - if (_DefaultOutPipe == null) throw new Exception("The device has not been initialized."); + if (_DefaultOutPipe == null) throw new Exception(Messages.ErrorMessageNotInitialized); if (data.Length > WriteBufferSize) throw new Exception("The buffer size is too large"); - await _DefaultOutPipe.OutputStream.WriteAsync(data.AsBuffer()); + var count = await _DefaultOutPipe.OutputStream.WriteAsync(data.AsBuffer()); + + if (count == data.Length) + { + Tracer?.Trace(true, data); + } + else + { + var message = Messages.GetErrorMessageInvalidWriteLength(data.Length, count); + Logger?.Log(message, GetType().Name, null, LogLevel.Error); + throw new IOException(message); + } + + Tracer?.Trace(true, data); } #endregion } diff --git a/src/Usb.Net/Windows/WindowsUsbDevice.cs b/src/Usb.Net/Windows/WindowsUsbDevice.cs index e552066a..5b381ba1 100644 --- a/src/Usb.Net/Windows/WindowsUsbDevice.cs +++ b/src/Usb.Net/Windows/WindowsUsbDevice.cs @@ -124,6 +124,7 @@ await Task.Run(() => //TODO: Allow for different interfaces and pipes... var isSuccess = WinUsbApiCalls.WinUsb_WritePipe(_DefaultUsbInterface.Handle, _DefaultUsbInterface.WritePipe.WINUSB_PIPE_INFORMATION.PipeId, data, (uint)data.Length, out var bytesWritten, IntPtr.Zero); HandleError(isSuccess, "Couldn't write data"); + Tracer?.Trace(true, data); }); } From 4439c092bc95bdaef466d9fee8a01403bf6d1f71 Mon Sep 17 00:00:00 2001 From: Christian Date: Sun, 23 Jun 2019 18:00:38 +1000 Subject: [PATCH 14/18] A fix --- src/Device.Net.UnitTests/MockDeviceBase.cs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/Device.Net.UnitTests/MockDeviceBase.cs b/src/Device.Net.UnitTests/MockDeviceBase.cs index 14e42243..4a9a448e 100644 --- a/src/Device.Net.UnitTests/MockDeviceBase.cs +++ b/src/Device.Net.UnitTests/MockDeviceBase.cs @@ -28,16 +28,22 @@ public Task InitializeAsync() private byte[] LastWrittenBuffer; - public async override Task ReadAsync() + public override async Task ReadAsync() { - if (LastWrittenBuffer != null) return LastWrittenBuffer; - - return await Task.FromResult(new byte[64] { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }); + if (LastWrittenBuffer != null) + { + Tracer.Trace(false, LastWrittenBuffer); + return LastWrittenBuffer; + } + var data = new byte[64] { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; + Tracer.Trace(false, data); + return await Task.FromResult(data); } public override Task WriteAsync(byte[] data) { LastWrittenBuffer = data; + Tracer.Trace(true, data); return Task.FromResult(true); } } From 76922e273bf6cffbb986f74bc40735ae4747b657 Mon Sep 17 00:00:00 2001 From: Christian Date: Sun, 23 Jun 2019 18:09:06 +1000 Subject: [PATCH 15/18] Add traces on all reads --- src/Device.Net.LibUsb/LibUsbDevice.cs | 8 +++++--- src/Device.Net.UWP/UWPDeviceBase - Generic.cs | 9 ++++++--- src/Device.Net/DeviceBase.cs | 1 - src/Hid.Net.UWP/UWPHidDevice.cs | 4 +++- src/Hid.Net/Windows/WindowsHIDDevice.cs | 4 +++- src/Usb.Net.Android/AndroidUsbDevice.cs | 7 ++++--- src/Usb.Net/Windows/WindowsUsbDevice.cs | 7 ++++--- 7 files changed, 25 insertions(+), 15 deletions(-) diff --git a/src/Device.Net.LibUsb/LibUsbDevice.cs b/src/Device.Net.LibUsb/LibUsbDevice.cs index 2243f00d..dda0182d 100644 --- a/src/Device.Net.LibUsb/LibUsbDevice.cs +++ b/src/Device.Net.LibUsb/LibUsbDevice.cs @@ -101,11 +101,13 @@ public override async Task ReadAsync() { return await Task.Run(() => { - var buffer = new byte[ReadPacketSize]; + var data = new byte[ReadPacketSize]; - _UsbEndpointReader.Read(buffer, Timeout, out var bytesRead); + _UsbEndpointReader.Read(data, Timeout, out var bytesRead); - return buffer; + Tracer?.Trace(false, data); + + return data; }); } finally diff --git a/src/Device.Net.UWP/UWPDeviceBase - Generic.cs b/src/Device.Net.UWP/UWPDeviceBase - Generic.cs index 08ecc9a7..b697416b 100644 --- a/src/Device.Net.UWP/UWPDeviceBase - Generic.cs +++ b/src/Device.Net.UWP/UWPDeviceBase - Generic.cs @@ -48,16 +48,19 @@ public override async Task ReadAsync() { if (Chunks.Count > 0) { - var retVal = Chunks[0]; + var data2 = Chunks[0]; Logger?.Log("Received data from device", GetType().Name, null, LogLevel.Information); Chunks.RemoveAt(0); - return retVal; + Tracer?.Trace(false, data2); + return data2; } } IsReading = true; ReadChunkTaskCompletionSource = new TaskCompletionSource(); - return await ReadChunkTaskCompletionSource.Task; + var data = await ReadChunkTaskCompletionSource.Task; + Tracer?.Trace(false, data); + return data; } #endregion diff --git a/src/Device.Net/DeviceBase.cs b/src/Device.Net/DeviceBase.cs index 342786cf..58f9c8ed 100644 --- a/src/Device.Net/DeviceBase.cs +++ b/src/Device.Net/DeviceBase.cs @@ -83,7 +83,6 @@ public async Task WriteAndReadAsync(byte[] writeBuffer) { await WriteAsync(writeBuffer); var retVal = await ReadAsync(); - Tracer?.Trace(false, retVal); Log(Messages.SuccessMessageWriteAndReadCalled); return retVal; } diff --git a/src/Hid.Net.UWP/UWPHidDevice.cs b/src/Hid.Net.UWP/UWPHidDevice.cs index c8658543..61868ef6 100644 --- a/src/Hid.Net.UWP/UWPHidDevice.cs +++ b/src/Hid.Net.UWP/UWPHidDevice.cs @@ -156,7 +156,9 @@ public async Task ReadReportAsync() public override async Task ReadAsync() { - return (await ReadReportAsync()).Data; + var data = (await ReadReportAsync()).Data; + Tracer?.Trace(false, data); + return data; } #endregion diff --git a/src/Hid.Net/Windows/WindowsHIDDevice.cs b/src/Hid.Net/Windows/WindowsHIDDevice.cs index 8bdf943f..33f98ea8 100644 --- a/src/Hid.Net/Windows/WindowsHIDDevice.cs +++ b/src/Hid.Net/Windows/WindowsHIDDevice.cs @@ -162,7 +162,9 @@ public override async Task InitializeAsync() public override async Task ReadAsync() { - return (await ReadReportAsync()).Data; + var data = (await ReadReportAsync()).Data; + Tracer?.Trace(false, data); + return data; } public async Task ReadReportAsync() diff --git a/src/Usb.Net.Android/AndroidUsbDevice.cs b/src/Usb.Net.Android/AndroidUsbDevice.cs index 9f8774ee..39babc08 100644 --- a/src/Usb.Net.Android/AndroidUsbDevice.cs +++ b/src/Usb.Net.Android/AndroidUsbDevice.cs @@ -104,17 +104,18 @@ public override async Task ReadAsync() request.Queue(byteBuffer, ReadBufferSize); #pragma warning restore CS0618 // Type or member is obsolete await _UsbDeviceConnection.RequestWaitAsync(); - var buffers = new byte[ReadBufferSize]; + var data = new byte[ReadBufferSize]; byteBuffer.Rewind(); for (var i = 0; i < ReadBufferSize; i++) { - buffers[i] = (byte)byteBuffer.Get(); + data[i] = (byte)byteBuffer.Get(); } //Marshal.Copy(byteBuffer.GetDirectBufferAddress(), buffers, 0, ReadBufferLength); - return buffers; + Tracer?.Trace(false, data); + return data; } catch (Exception ex) { diff --git a/src/Usb.Net/Windows/WindowsUsbDevice.cs b/src/Usb.Net/Windows/WindowsUsbDevice.cs index 5b381ba1..53806694 100644 --- a/src/Usb.Net/Windows/WindowsUsbDevice.cs +++ b/src/Usb.Net/Windows/WindowsUsbDevice.cs @@ -104,11 +104,12 @@ public override async Task ReadAsync() { return await Task.Run(() => { - var bytes = new byte[ReadBufferSize]; + var data = new byte[ReadBufferSize]; //TODO: Allow for different interfaces and pipes... - var isSuccess = WinUsbApiCalls.WinUsb_ReadPipe(_DefaultUsbInterface.Handle, _DefaultUsbInterface.ReadPipe.WINUSB_PIPE_INFORMATION.PipeId, bytes, ReadBufferSize, out var bytesRead, IntPtr.Zero); + var isSuccess = WinUsbApiCalls.WinUsb_ReadPipe(_DefaultUsbInterface.Handle, _DefaultUsbInterface.ReadPipe.WINUSB_PIPE_INFORMATION.PipeId, data, ReadBufferSize, out var bytesRead, IntPtr.Zero); HandleError(isSuccess, "Couldn't read data"); - return bytes; + Tracer?.Trace(false, data); + return data; }); } From 8b09ff70efa70c9eaf8e54ce71181fa3215210fe Mon Sep 17 00:00:00 2001 From: Christian Date: Sun, 23 Jun 2019 18:14:16 +1000 Subject: [PATCH 16/18] Remove a duplicate trace --- src/Usb.Net.UWP/UWPUsbDevice.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Usb.Net.UWP/UWPUsbDevice.cs b/src/Usb.Net.UWP/UWPUsbDevice.cs index 379837d6..ee394146 100644 --- a/src/Usb.Net.UWP/UWPUsbDevice.cs +++ b/src/Usb.Net.UWP/UWPUsbDevice.cs @@ -118,8 +118,6 @@ public override async Task WriteAsync(byte[] data) Logger?.Log(message, GetType().Name, null, LogLevel.Error); throw new IOException(message); } - - Tracer?.Trace(true, data); } #endregion } From f74cf39da758595e099ca77babcfa700fe1d791e Mon Sep 17 00:00:00 2001 From: Christian Date: Sun, 23 Jun 2019 18:24:43 +1000 Subject: [PATCH 17/18] Add the old constructor overloads back --- src/Device.Net.LibUsb/LibUsbDevice.cs | 4 ++++ src/Hid.Net.UWP/UWPHidDevice.cs | 4 ++++ src/Hid.Net/Windows/WindowsHIDDevice.cs | 4 ++++ src/Usb.Net.Android/AndroidUsbDevice.cs | 4 ++++ src/Usb.Net.UWP/UWPUsbDevice.cs | 4 ++++ src/Usb.Net/Windows/WindowsUsbDevice.cs | 4 ++++ 6 files changed, 24 insertions(+) diff --git a/src/Device.Net.LibUsb/LibUsbDevice.cs b/src/Device.Net.LibUsb/LibUsbDevice.cs index dda0182d..03c779b8 100644 --- a/src/Device.Net.LibUsb/LibUsbDevice.cs +++ b/src/Device.Net.LibUsb/LibUsbDevice.cs @@ -38,6 +38,10 @@ public class LibUsbDevice : DeviceBase, IDevice #endregion #region Constructor + public LibUsbDevice(UsbDevice usbDevice, int timeout) : this(usbDevice, timeout, null, null) + { + } + public LibUsbDevice(UsbDevice usbDevice, int timeout, ILogger logger, ITracer tracer) : base(logger, tracer) { UsbDevice = usbDevice; diff --git a/src/Hid.Net.UWP/UWPHidDevice.cs b/src/Hid.Net.UWP/UWPHidDevice.cs index 61868ef6..465f4b1c 100644 --- a/src/Hid.Net.UWP/UWPHidDevice.cs +++ b/src/Hid.Net.UWP/UWPHidDevice.cs @@ -40,6 +40,10 @@ public UWPHidDevice(ILogger logger, ITracer tracer) : this(null, logger, tracer) { } + public UWPHidDevice(string deviceId) : this(deviceId, null, null) + { + } + public UWPHidDevice(string deviceId, ILogger logger, ITracer tracer) : base(deviceId, logger, tracer) { } diff --git a/src/Hid.Net/Windows/WindowsHIDDevice.cs b/src/Hid.Net/Windows/WindowsHIDDevice.cs index 33f98ea8..039f13a9 100644 --- a/src/Hid.Net/Windows/WindowsHIDDevice.cs +++ b/src/Hid.Net/Windows/WindowsHIDDevice.cs @@ -41,6 +41,10 @@ public sealed class WindowsHidDevice : WindowsDeviceBase, IHidDevice #endregion #region Constructor + public WindowsHidDevice(string deviceId) : this(deviceId, null, null, null, null) + { + } + public WindowsHidDevice(string deviceId, ILogger logger, ITracer tracer) : this(deviceId, null, null, logger, tracer) { } diff --git a/src/Usb.Net.Android/AndroidUsbDevice.cs b/src/Usb.Net.Android/AndroidUsbDevice.cs index 39babc08..85bc795b 100644 --- a/src/Usb.Net.Android/AndroidUsbDevice.cs +++ b/src/Usb.Net.Android/AndroidUsbDevice.cs @@ -43,6 +43,10 @@ public int DeviceNumberId #endregion #region Constructor + public AndroidUsbDevice(UsbManager usbManager, Context androidContext, int deviceNumberId) : this(usbManager, androidContext, deviceNumberId, null, null) + { + } + public AndroidUsbDevice(UsbManager usbManager, Context androidContext, int deviceNumberId, ILogger logger, ITracer tracer) : base(logger, tracer) { UsbManager = usbManager; diff --git a/src/Usb.Net.UWP/UWPUsbDevice.cs b/src/Usb.Net.UWP/UWPUsbDevice.cs index ee394146..0e2d1517 100644 --- a/src/Usb.Net.UWP/UWPUsbDevice.cs +++ b/src/Usb.Net.UWP/UWPUsbDevice.cs @@ -31,6 +31,10 @@ public UWPUsbDevice(ILogger logger, ITracer tracer) : this(null, logger, tracer) { } + public UWPUsbDevice(ConnectedDeviceDefinition deviceDefinition) : this(deviceDefinition, null, null) + { + } + public UWPUsbDevice(ConnectedDeviceDefinition deviceDefinition, ILogger logger, ITracer tracer) : base(deviceDefinition.DeviceId, logger, tracer) { ConnectedDeviceDefinition = deviceDefinition; diff --git a/src/Usb.Net/Windows/WindowsUsbDevice.cs b/src/Usb.Net/Windows/WindowsUsbDevice.cs index 53806694..870ee4a5 100644 --- a/src/Usb.Net/Windows/WindowsUsbDevice.cs +++ b/src/Usb.Net/Windows/WindowsUsbDevice.cs @@ -26,6 +26,10 @@ public sealed class WindowsUsbDevice : WindowsDeviceBase, IDevice #endregion #region Constructor + public WindowsUsbDevice(string deviceId) : base(deviceId, null, null) + { + } + public WindowsUsbDevice(string deviceId, ILogger logger, ITracer tracer) : base(deviceId, logger, tracer) { } From 75ddf460238cf5b8d79868ef9e73b162bfdbbed7 Mon Sep 17 00:00:00 2001 From: Christian Date: Sun, 23 Jun 2019 18:31:10 +1000 Subject: [PATCH 18/18] Cleanup --- src/Device.Net.UWP/UWPDeviceBase.cs | 1 - src/Device.Net.UnitTests/MockHidFactory.cs | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Device.Net.UWP/UWPDeviceBase.cs b/src/Device.Net.UWP/UWPDeviceBase.cs index 42ae4eff..5e3863bb 100644 --- a/src/Device.Net.UWP/UWPDeviceBase.cs +++ b/src/Device.Net.UWP/UWPDeviceBase.cs @@ -39,7 +39,6 @@ protected void HandleDataReceived(byte[] bytes) #region Constructor protected UWPDeviceBase(ILogger logger, ITracer tracer) : base(logger, tracer) { - } #endregion } diff --git a/src/Device.Net.UnitTests/MockHidFactory.cs b/src/Device.Net.UnitTests/MockHidFactory.cs index 64768209..8b1f1892 100644 --- a/src/Device.Net.UnitTests/MockHidFactory.cs +++ b/src/Device.Net.UnitTests/MockHidFactory.cs @@ -22,9 +22,9 @@ public MockHidFactory(ILogger logger, ITracer tracer) : base(logger, tracer) public override uint VendorId => MockHidDevice.VendorId; - public static void Register(ILogger logger, ITracer trace) + public static void Register(ILogger logger, ITracer tracer) { - DeviceManager.Current.DeviceFactories.Add(new MockHidFactory(logger, trace)); + DeviceManager.Current.DeviceFactories.Add(new MockHidFactory(logger, tracer)); } public override IDevice GetDevice(ConnectedDeviceDefinition deviceDefinition)