Skip to content

Commit

Permalink
Merge pull request #426 from WildernessLabs/fixes_cleanup
Browse files Browse the repository at this point in the history
Fixes cleanup
  • Loading branch information
ctacke authored Jan 16, 2024
2 parents b3204d3 + 989b1c2 commit f5f8d47
Show file tree
Hide file tree
Showing 14 changed files with 195 additions and 285 deletions.
47 changes: 27 additions & 20 deletions Source/v2/Meadow.CLI.Core/DFU/DfuSharp.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
using System;
using System.IO;
using System.Threading;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace DfuSharp
{
Expand All @@ -23,7 +19,6 @@ public enum LogLevel

public delegate void HotplugCallback(IntPtr ctx, IntPtr device, HotplugEventType eventType, IntPtr userData);


class NativeMethods
{

Expand Down Expand Up @@ -266,7 +261,7 @@ public class UploadingEventArgs : EventArgs

public UploadingEventArgs(int bytesUploaded)
{
this.BytesUploaded = bytesUploaded;
BytesUploaded = bytesUploaded;
}
}

Expand All @@ -277,16 +272,19 @@ public class DfuDevice : IDisposable
const int transfer_size = 0x800;
const int address = 0x08000000;

IntPtr handle;
readonly IntPtr handle;
InterfaceDescriptor interface_descriptor;
DfuFunctionDescriptor dfu_descriptor;

public DfuDevice(IntPtr device, InterfaceDescriptor interface_descriptor, DfuFunctionDescriptor dfu_descriptor)
{
this.interface_descriptor = interface_descriptor;
this.dfu_descriptor = dfu_descriptor;
interface_descriptor = interface_descriptor;
dfu_descriptor = dfu_descriptor;

if (NativeMethods.libusb_open(device, ref handle) < 0)
{
throw new Exception("Error opening device");
}
}

public event UploadingEventHandler Uploading;
Expand Down Expand Up @@ -411,6 +409,7 @@ public void Download(FileStream file)
{
int count = 0;
ushort transaction = 2;

using (var writer = new BinaryWriter(file))
{
while (count < flash_size)
Expand Down Expand Up @@ -659,7 +658,7 @@ public class Context : IDisposable
IntPtr _callbackHandle = IntPtr.Zero;


IntPtr handle;
readonly IntPtr handle;
public Context(LogLevel debug_level = LogLevel.None)
{
var ret = NativeMethods.libusb_init(ref handle);
Expand All @@ -669,12 +668,12 @@ public Context(LogLevel debug_level = LogLevel.None)
throw new Exception(string.Format("Error: {0} while trying to initialize libusb", ret));

// instantiate our callback handler
this._hotplugCallbackHandler = new HotplugCallback(HandleHotplugCallback);
_hotplugCallbackHandler = new HotplugCallback(HandleHotplugCallback);
}

public void Dispose()
{
//this.StopListeningForHotplugEvents(); // not needed, they're automatically deregistered in libusb_exit.
//StopListeningForHotplugEvents(); // not needed, they're automatically deregistered in libusb_exit.
NativeMethods.libusb_exit(handle);
}

Expand Down Expand Up @@ -707,8 +706,10 @@ public List<DfuDevice> GetDfuDevices(List<ushort> idVendors)
var ret2 = NativeMethods.libusb_get_config_descriptor(devices[i], (ushort)j, out ptr);

if (ret2 < 0)
{
continue;
//throw new Exception(string.Format("Error: {0} while trying to get the config descriptor", ret2));
}
//throw new Exception(string.Format("Error: {0} while trying to get the config descriptor", ret2));

var config_descriptor = Marshal.PtrToStructure<ConfigDescriptor>(ptr);

Expand All @@ -717,7 +718,9 @@ public List<DfuDevice> GetDfuDevices(List<ushort> idVendors)
var p = config_descriptor.interfaces + j * Marshal.SizeOf<@Interface>();

if (p == IntPtr.Zero)
{
continue;
}

var @interface = Marshal.PtrToStructure<@Interface>(p);
for (int l = 0; l < @interface.num_altsetting; l++)
Expand All @@ -726,11 +729,16 @@ public List<DfuDevice> GetDfuDevices(List<ushort> idVendors)

// Ensure this is a DFU descriptor
if (interface_descriptor.bInterfaceClass != 0xfe || interface_descriptor.bInterfaceSubClass != 0x1)
{
continue;
}

var dfu_descriptor = FindDescriptor(interface_descriptor.extra, interface_descriptor.extra_length, (byte)Consts.USB_DT_DFU);

if (dfu_descriptor != null)
{
dfu_devices.Add(new DfuDevice(devices[i], interface_descriptor, dfu_descriptor.Value));
}
}
}
}
Expand Down Expand Up @@ -763,7 +771,7 @@ public List<DfuDevice> GetDfuDevices(List<ushort> idVendors)

public bool HasCapability(Capabilities caps)
{
return NativeMethods.libusb_has_capability(caps) == 0 ? false : true;
return NativeMethods.libusb_has_capability(caps) != 0;
}

public void BeginListeningForHotplugEvents()
Expand Down Expand Up @@ -791,8 +799,8 @@ public void BeginListeningForHotplugEvents()
int deviceClass = -1;
IntPtr userData = IntPtr.Zero;

ErrorCodes success = NativeMethods.libusb_hotplug_register_callback(this.handle, HotplugEventType.DeviceArrived | HotplugEventType.DeviceLeft, HotplugFlags.DefaultNoFlags,
vendorID, productID, deviceClass, this._hotplugCallbackHandler, userData, out _callbackHandle);
ErrorCodes success = NativeMethods.libusb_hotplug_register_callback(handle, HotplugEventType.DeviceArrived | HotplugEventType.DeviceLeft, HotplugFlags.DefaultNoFlags,
vendorID, productID, deviceClass, _hotplugCallbackHandler, userData, out _callbackHandle);

if (success == ErrorCodes.Success)
{
Expand All @@ -813,15 +821,14 @@ public void StopListeningForHotplugEvents()
return;
}

NativeMethods.libusb_hotplug_deregister_callback(this.handle, this._callbackHandle);

NativeMethods.libusb_hotplug_deregister_callback(handle, _callbackHandle);
}

public void HandleHotplugCallback(IntPtr ctx, IntPtr device, HotplugEventType eventType, IntPtr userData)
{
Debug.WriteLine("Hotplug Callback called, event type: " + eventType.ToString());
// raise the event
this.DeviceConnected(this, new EventArgs());
DeviceConnected(this, new EventArgs());
}
}
}
29 changes: 6 additions & 23 deletions Source/v2/Meadow.CLI.Core/DFU/DfuUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ namespace Meadow.CLI.Core.Internals.Dfu;

public static class DfuUtils
{
private static int _osAddress = 0x08000000;

// public static string LastSerialNumber { get; private set; } = "";
private static readonly int _osAddress = 0x08000000;

public enum DfuFlashFormat
{
Expand Down Expand Up @@ -98,15 +96,15 @@ public static async Task<bool> FlashFile(string fileName, string dfuSerialNumber
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
logger.LogError("dfu-util update required. To update, run in administrator mode: `meadow install dfu-util`");
logger.LogError("dfu-util update required - to update, run in administrator mode: `meadow install dfu-util`");
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
logger.LogError("dfu-util update required. To update, run: `brew upgrade dfu-util`");
logger.LogError("dfu-util update required - to update, run: `brew upgrade dfu-util`");
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
logger.LogError("dfu-util update required. To update , run: `apt upgrade dfu-util` or the equivalent for your Linux distribution");
logger.LogError("dfu-util update required - to update , run: `apt upgrade dfu-util` or the equivalent for your Linux distribution");
}
else
{
Expand Down Expand Up @@ -275,24 +273,10 @@ public static async Task InstallDfuUtil(

var targetDir = is64Bit
? Environment.GetFolderPath(Environment.SpecialFolder.System)
: Environment.GetFolderPath(
Environment.SpecialFolder.SystemX86);
: Environment.GetFolderPath(Environment.SpecialFolder.SystemX86);

File.Copy(dfuUtilExe.FullName, Path.Combine(targetDir, dfuUtilExe.Name), true);
File.Copy(libUsbDll.FullName, Path.Combine(targetDir, libUsbDll.Name), true);

// clean up from previous version
var dfuPath = Path.Combine(@"C:\Windows\System", dfuUtilExe.Name);
var libUsbPath = Path.Combine(@"C:\Windows\System", libUsbDll.Name);
if (File.Exists(dfuPath))
{
File.Delete(dfuPath);
}

if (File.Exists(libUsbPath))
{
File.Delete(libUsbPath);
}
}
finally
{
Expand All @@ -302,5 +286,4 @@ public static async Task InstallDfuUtil(
}
}
}

}
}
2 changes: 1 addition & 1 deletion Source/v2/Meadow.CLI/Meadow.CLI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<Authors>Wilderness Labs, Inc</Authors>
<Company>Wilderness Labs, Inc</Company>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageVersion>2.0.0.0</PackageVersion>
<PackageVersion>2.0.0.5</PackageVersion>
<Platforms>AnyCPU</Platforms>
<PackageProjectUrl>http://developer.wildernesslabs.co/Meadow/Meadow.Foundation/</PackageProjectUrl>
<PackageIcon>icon.png</PackageIcon>
Expand Down
2 changes: 1 addition & 1 deletion Source/v2/Meadow.Cli/AppManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,4 @@ public static async Task DeployApplication(
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ public class FileDeleteCommand : BaseDeviceCommand<FileDeleteCommand>

public FileDeleteCommand(MeadowConnectionManager connectionManager, ILoggerFactory loggerFactory)
: base(connectionManager, loggerFactory)
{
}
{ }

protected override async ValueTask ExecuteCommand()
{
Expand Down
Loading

0 comments on commit f5f8d47

Please sign in to comment.