Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Old and new LibUsbDotNet nugets. #338

Merged
merged 11 commits into from
Aug 24, 2023
22 changes: 17 additions & 5 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
name: Meadow.CLI
env:
CLI_RELEASE_VERSION: 1.2.0.0
IDE_TOOLS_RELEASE_VERSION: 1.2.0
MEADOW_OS_VERSION: 1.2.0.0
CLI_RELEASE_VERSION: 1.3.0.0
IDE_TOOLS_RELEASE_VERSION: 1.3.0
MEADOW_OS_VERSION: 1.3.0.0
VS_MAC_2019_VERSION: 8.10
VS_MAC_2022_VERSION: 17.5

Expand Down Expand Up @@ -62,10 +62,22 @@ jobs:
- name: Add MSBuild to Path
uses: microsoft/[email protected]

- name: Restore dependencies
- name: Restore Classic dependencies
run: dotnet restore main/MeadowCLI.Classic.sln /p:Configuration=Release

- name: Build Classic CLI
run: dotnet build main/MeadowCLI.Classic.sln /p:Configuration=Release

- name: Upload nuget Artifacts for internal testing
uses: actions/upload-artifact@v2
with:
name: Meadow.CLI.Classic.nuget.${{ ENV.CLI_RELEASE_VERSION }}
path: 'main\Meadow.CLI.Classic\bin\Release\*.nupkg'

- name: Restore normal dependencies
run: dotnet restore main/MeadowCLI.sln /p:Configuration=Release

- name: Build
- name: Build normal CLI
run: dotnet build main/MeadowCLI.sln /p:Configuration=Release

- name: Upload nuget Artifacts for internal testing
Expand Down
2 changes: 1 addition & 1 deletion Meadow.CLI.Core/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Meadow.CLI.Core
{
public static class Constants
{
public const string CLI_VERSION = "1.2.0.0";
public const string CLI_VERSION = "1.2.2.0";
public const ushort HCOM_PROTOCOL_PREVIOUS_VERSION_NUMBER = 0x0006;
public const ushort HCOM_PROTOCOL_CURRENT_VERSION_NUMBER = 0x0007; // Used for transmission
public const string WILDERNESS_LABS_USB_VID = "2E6A";
Expand Down
60 changes: 57 additions & 3 deletions Meadow.CLI.Core/Internals/Dfu/DfuUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@ public static bool CheckForValidDevice()
return false;
}
}

#if WIN_10
public static UsbRegistry GetDeviceInBootloaderMode()
#else
public static IUsbDevice GetDeviceInBootloaderMode()
#endif
{
var allDevices = GetDevicesInBootloaderMode();
if (allDevices.Count() > 1)
Expand All @@ -50,9 +53,33 @@ public static IUsbDevice GetDeviceInBootloaderMode()

return device;
}

#if WIN_10
public static IEnumerable<UsbRegistry> GetDevicesInBootloaderMode()
#else
public static IEnumerable<IUsbDevice> GetDevicesInBootloaderMode()
#endif
{
#if WIN_10

var allDevices = UsbDevice.AllDevices;
IEnumerable<UsbRegistry> ourDevices;

switch (Environment.OSVersion.Platform)
{
case PlatformID.Win32NT:
ourDevices = allDevices.Where(d => d.DeviceProperties["FriendlyName"].ToString() == _usbStmName);
break;
default:
ourDevices = allDevices.Where(d => d.DeviceProperties["DeviceDesc"].ToString() == _usbStmName);
break;
}

if (ourDevices.Count() < 1)
{
throw new DeviceNotFoundException("No Devices found. Connect a device in bootloader mode. If the device is in bootloader mode, please update the device driver. See instructions at https://wldrn.es/usbdriver");
}
return ourDevices;
#else
using (UsbContext context = new UsbContext())
{
var allDevices = context.List();
Expand All @@ -63,9 +90,32 @@ public static IEnumerable<IUsbDevice> GetDevicesInBootloaderMode()
}
return ourDevices;
}
#endif
}

#if WIN_10
public static string GetDeviceSerial(UsbRegistry device)
{
if (device != null && device.DeviceProperties != null)
{
switch (Environment.OSVersion.Platform)
{
case PlatformID.Win32NT:
var deviceID = device.DeviceProperties["DeviceID"].ToString();
if (!string.IsNullOrWhiteSpace(deviceID))
return deviceID.Substring(deviceID.LastIndexOf("\\") + 1);
else
return string.Empty;
default:
return device.DeviceProperties["SerialNumber"].ToString();
}
}
else
return string.Empty;
}
#else
public static string GetDeviceSerial(IUsbDevice device)

{
var serialNumber = string.Empty;

Expand All @@ -81,6 +131,7 @@ public static string GetDeviceSerial(IUsbDevice device)

return serialNumber;
}
#endif

public enum DfuFlashFormat
{
Expand Down Expand Up @@ -143,8 +194,11 @@ public static Task<bool> FlashLatest(ILogger? logger = null, DfuFlashFormat form

return FlashFile(fileName: fileName, logger: logger, format: DfuUtils.DfuFlashFormat.ConsoleOut);
}

#if WIN_10
public static async Task<bool> FlashFile(string fileName, UsbRegistry? device = null, ILogger? logger = null, DfuFlashFormat format = DfuFlashFormat.Percent)
#else
public static async Task<bool> FlashFile(string fileName, IUsbDevice? device = null, ILogger? logger = null, DfuFlashFormat format = DfuFlashFormat.Percent)
#endif
{
logger ??= NullLogger.Instance;
device ??= GetDeviceInBootloaderMode();
Expand Down
2 changes: 1 addition & 1 deletion Meadow.CLI.Core/Meadow.CLI.Core.6.0.0.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<LangVersion>preview</LangVersion>
<Nullable>enable</Nullable>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Version>1.2.0.0</Version>
<Version>1.3.0.0</Version>
</PropertyGroup>

<PropertyGroup>
Expand Down
84 changes: 84 additions & 0 deletions Meadow.CLI.Core/Meadow.CLI.Core.Classic.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Platforms>AnyCPU</Platforms>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>MeadowCLIKey.snk</AssemblyOriginatorKeyFile>
<GenerateAssemblyVersionAttribute>false</GenerateAssemblyVersionAttribute>
<GenerateAssemblyInformationalVersionAttribute>false</GenerateAssemblyInformationalVersionAttribute>
<GenerateAssemblyFileVersionAttribute>false</GenerateAssemblyFileVersionAttribute>
<LangVersion>preview</LangVersion>
<Nullable>enable</Nullable>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Version>1.3.0.0</Version>
</PropertyGroup>

<PropertyGroup>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<OutputPath>..\Meadow.CLI.Core.Classic\bin\Debug</OutputPath>
<DefineConstants>TRACE;WIN_10;DEBUG;NET;NET6_0;NETCOREAPP</DefineConstants>
<WarningLevel>4</WarningLevel>
<ExternalConsole>false</ExternalConsole>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<OutputPath>..\Meadow.CLI.Core.Classic\bin\Release</OutputPath>
<DefineConstants>TRACE;RELEASE;NET;NET6_0;NETCOREAPP;WIN_10</DefineConstants>
<WarningLevel>4</WarningLevel>
</PropertyGroup>

<ItemGroup>
<Compile Remove="Internals\Dfu\DfuContext.cs" />
<Compile Remove="Internals\Dfu\DfuSharp.cs" />
<Compile Remove="Internals\Dfu\WIP.DfuContext.cs" />
<Compile Remove="Internals\Dfu\WIP.DfuSharp.cs" />
<Compile Remove="Internals\Dfu\WIP.DfuUpload.cs" />
<Compile Remove="Internals\Dfu\WIP.NativeMethods.cs" />
</ItemGroup>

<ItemGroup>
<None Update="lib\illink.dll" CopyToOutputDirectory="Always" />
<None Update="lib\meadow_link.xml" CopyToOutputDirectory="Always" />
<None Update="lib\Mono.Cecil.dll" CopyToOutputDirectory="Always" />
<None Update="lib\illink.runtimeconfig.json" CopyToOutputDirectory="Always" />
<None Update="libusb-1.0.dll" CopyToOutputDirectory="Always" />
<None Update="libusb-1.0.dylib" CopyToOutputDirectory="Always" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="CredentialManagement.Standard" Version="1.0.4" />
<PackageReference Include="IdentityModel.OidcClient" Version="3.1.2" />
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="Mono.Cecil" Version="0.11.5" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="7.0.0" />
<PackageReference Include="System.IO" Version="4.3.0" />
<PackageReference Include="System.IO.Hashing" Version="7.0.0" />
<PackageReference Include="System.IO.Ports" Version="7.0.0" />
<PackageReference Include="System.Management" Version="7.0.2" />
<PackageReference Include="System.Memory" Version="4.5.5" />
<PackageReference Include="System.Net.Sockets" Version="4.3.0" />
<PackageReference Include="System.Text.Json" Version="7.0.3" />
<PackageReference Include="Microsoft.Net.Compilers.Toolset" Version="4.6.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="YamlDotNet" Version="13.1.1" />
<PackageReference Include="LibUsbDotNet" Version="2.2.29" />
</ItemGroup>

<ItemGroup>
<None Include="images\icon.png" PackagePath="icon.png" Pack="true" />
</ItemGroup>

<ItemGroup>
<Reference Include="System.Management" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Meadow.Hcom\Meadow.Hcom.6.0.0.csproj" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion Meadow.CLI.Core/Meadow.CLI.Core.VS2019.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<LangVersion>preview</LangVersion>
<Nullable>enable</Nullable>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Version>1.2.0.0</Version>
<Version>1.3.0.0</Version>
</PropertyGroup>

<PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion Meadow.CLI.Core/Meadow.CLI.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<LangVersion>preview</LangVersion>
<Nullable>enable</Nullable>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Version>1.2.0.0</Version>
<Version>1.3.0.0</Version>
</PropertyGroup>

<PropertyGroup>
Expand Down
1 change: 0 additions & 1 deletion Meadow.CLI/Commands/Cloud/Package/CreatePackageCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public CreateCommand(ILoggerFactory loggerFactory, PackageManager packageManager
[CommandOption("osVersion", 'v', Description = "Version of Meadow OS to include in package", IsRequired = false)]
public string OsVersion { get; init; }


public async ValueTask ExecuteAsync(IConsole console)
{
var cancellationToken = console.RegisterCancellationHandler();
Expand Down
3 changes: 3 additions & 0 deletions Meadow.CLI/Commands/Cloud/Package/PublishPackageCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,13 @@ public PublishCommand(ILoggerFactory loggerFactory, PackageService packageServic

[CommandOption("packageId", 'p', Description = "ID of the package to publish", IsRequired = true)]
public string PackageId { get; init; }

[CommandOption("collectionId", 'c', Description = "The target collection for publishing", IsRequired = true)]
public string CollectionId { get; set; }

[CommandOption("metadata", 'm', Description = "Pass through metadata", IsRequired = false)]
public string Metadata { get; set; }

[CommandOption("host", Description = "Optionally set a host (default is https://www.meadowcloud.co)", IsRequired = false)]
public string Host { get; set; }

Expand Down
17 changes: 12 additions & 5 deletions Meadow.CLI/Commands/DeviceManagement/FlashOsCommand.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
using CliFx.Attributes;
using CliFx.Infrastructure;
#if WIN_10
using LibUsbDotNet.Main;
#else
using LibUsbDotNet.LibUsb;
#endif
using Meadow.CLI.Commands.Utility;
using Meadow.CLI.Core;
using Meadow.CLI.Core.DeviceManagement;
Expand Down Expand Up @@ -112,7 +116,7 @@ async Task<bool> ValidateVersionAndPromptUserToEraseFlash(IConsole console, Canc
{
// We just flashed the OS so it will show the current version
// But the runtime hasn't been updated yet so should match the previous OS version
Version previousOsVersion;
System.Version previousOsVersion;
string checkName;

try
Expand All @@ -125,18 +129,18 @@ async Task<bool> ValidateVersionAndPromptUserToEraseFlash(IConsole console, Canc
}
else
{
previousOsVersion = new Version(runtimeVersion.Split(' ')[0]);
previousOsVersion = new System.Version(runtimeVersion.Split(' ')[0]);
}
checkName = "runtime";
}
catch
{
previousOsVersion = new Version(MINIMUM_OS_VERSION);
previousOsVersion = new System.Version(MINIMUM_OS_VERSION);
checkName = "OS";
}

// If less that B6.1 flash
if (previousOsVersion.CompareTo(new Version(MINIMUM_OS_VERSION)) <= 0)
if (previousOsVersion.CompareTo(new System.Version(MINIMUM_OS_VERSION)) <= 0)
{
// Ask User 1st before wiping
Logger.LogInformation($"Your {checkName} version is older than {MINIMUM_OS_VERSION} (or unreadable). A flash erase is highly recommended.");
Expand Down Expand Up @@ -182,8 +186,11 @@ async Task<MeadowDeviceHelper> FindCurrentMeadowDevice(string serialPortName, st
async Task SetMeadowToDfuMode(string serialPortName, CancellationToken cancellationToken)
{
var dfuAttempts = 0;

#if WIN_10
UsbRegistry dfuDevice;
#else
IUsbDevice dfuDevice;
#endif
while (true)
{
try
Expand Down
3 changes: 2 additions & 1 deletion Meadow.CLI/Commands/MeadowSerialCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.Extensions.Logging;
using System;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;

namespace Meadow.CLI.Commands
Expand Down Expand Up @@ -56,7 +57,7 @@ private string GetSerialPort()

private bool PortExists(string name)
{
if (OperatingSystem.IsWindows())
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
// windows is case-insensitive
return System.IO.Ports.SerialPort.GetPortNames().Contains(name, StringComparer.InvariantCultureIgnoreCase);
Expand Down
Loading
Loading