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

DfuInstallation.Start() Exception thrown on .Net MAUI #27

Open
MouratidisA opened this issue Sep 26, 2023 · 14 comments
Open

DfuInstallation.Start() Exception thrown on .Net MAUI #27

MouratidisA opened this issue Sep 26, 2023 · 14 comments

Comments

@MouratidisA
Copy link

Describe the bug

I tried to implement the DFU on a .NET MAUI application, but it seems that when I create the DfuInstallation object and call the Start() function the process throws an Exception with a message:
Exception of type 'Foundation.You_Should_Not_Call_base_In_This_Method' was thrown.

To Reproduce

Steps to reproduce the behavior:

  1. Create a MAUI project
  2. Install the Laerdal.Dfu 1.27.8 NuGet package
  3. Create a method to download or retrieve your firmware update file
  4. Scan for the desired device to DFU
  5. Create the DfuInstallation object and call Start()

To download the firmware File:


public async Task<bool> DownloadFirmwareFile()
{ 
    //Retrieve firmware file in bytes 
    byte[] fileBytes = await HttpClientService.Instance.GetByteArrayAsync(Constants.GetFirmwareUpdate);

    //Create the file inside LocalApplicationData
    string outputFilePath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "app_dfu_package.zip");

    File.WriteAllBytes(outputFilePath, fileBytes);

    //Check if the file exists and update the SelectedFirmwareFile
    var exists = File.Exists(outputFilePath);
    if (!exists)
    {
        Instance.SelectedFirmwareFile = null;                            
        return false;
    }
    else
    {
        Instance.SelectedFirmwareFile = new FileInfo(outputFilePath);
        return true;
    }
}

To launch the update process:

var deviceId = $"{Instance.SelectedDevice?.Id.ToString()}";
var filePath = $"{Instance.SelectedFirmwareFile?.FullName.ToString()}";

var fileExists = File.Exists(filePath);
Debug.WriteLine($"File path exists: {fileExists}");

var output = new DfuInstallation(deviceId, filePath);

output.DfuErrorReceived += DfuInstallationOnErrorOccured;
output.DfuStateChanged += DfuInstallationOnStateChanged;
output.DfuProgressChanged += DfuInstallationOnProgressChanged;

output.Start();

FileUrl value: /var/mobile/Containers/Data/Application/537D36A3-7D6D-4A0F-B78A-8A03F85CEFEF/Documents/app_dfu_package.zip

Expected behavior

Start and complete the DFU process with the BLE device.

Exception information
Source: Laerdal.Dfu.Bindings.iOS
Message: Exception of type 'Foundation.You_Should_Not_Call_base_In_This_Method' was thrown.
StackTrace:
at Laerdal.Dfu.Bindings.iOS.DFUFirmware..ctor(NSUrl urlToZipFile, NSError& error)
at Laerdal.Dfu.DfuServiceInitiatorDelegate..ctor(DfuInstallation dfuInstallation)
at Laerdal.Dfu.DfuInstallation.Start(DfuConfiguration configuration)
at ProjectName.ViewModels.FirmwareUpdatePopUpViewModel.GetDfuInstallation() in /Users/username/ProjectName/ProjectName/ViewModels/FirmwareUpdatePopUpViewModel.cs: line 264

line 264 has the command Start command

Environment:

  • OS: macOS 13.5.2 (22G91)
  • IDE: Visual Studio for Mac 17.6.4 (build 472)
  • NuGet package Laerdal.Dfu version 1.27.8
  • Tested target device: iPhone 11 with iOS 17.1

Additional context

I've tried to recreate the exception on Android 12, but the debugger gets disconnected when reaches the start command.
(I am using wireless debug, due to maui issues #16274.

Is there an active example on MAUI I can follow?

@ISSPRO-Eng
Copy link

What BLE Nuget are you using? @MouratidisA

@MouratidisA
Copy link
Author

@ISSPRO-Eng I am using this NuGet package Plugin.BLE version 3.0.0.

@ISSPRO-Eng
Copy link

Okay. I am currently porting over a DFU application from Xamarin. Forms to MAUI, so I can help out once I get there. Do you have a public repo of the DFU app you have currently? No worries if you don't @MouratidisA

@framinosona
Copy link
Member

Hi guys, I'm sorry I have very little time to contribute here. I have heard that one of our team has experienced that too. Not sure what the fix for it should be though.

If one of you finds a fix, and has the time, I'd love to approve a PR on this 👍👍

@MouratidisA
Copy link
Author

Hey @framinosona , I'm not very experienced with NuGet package development, but I'm willing to give it a shot and assist with this issue. I've noticed there might be problems related to the new NSUrl(FileUrl, false) line in the Laerdal.Dfu/Laerdal.Dfu/iOS/DfuInstallation.cs file, specifically on line 26. Could you please look into this?

@ISSPRO-Eng
Copy link

ISSPRO-Eng commented Oct 16, 2023

@MouratidisA Did you have a working Xamarin.Forms version of the DFU Process? I could share that with you if that would help as well. Also, I am unable to set the File URL as it is protected in the updated nuget. Any idea how to get around this?

public FileInfo SelectedFirmwareFile
{
get => GetValue();
set
{
if (SetValue(value))
{
DfuInstallationConfigurationPageViewModel.Instance.DfuInstallation.FileUrl = value?.FullName;
}
}
}

@MouratidisA
Copy link
Author

@ISSPRO-Eng You can use my DownloadFirmwareFile() from the description of the issue. I was using a property on my view model class and then when I had to execute the DFU I had to create a new DfuInstallation object (using the ctor you can pass the filePath and the device Id values).

@ISSPRO-Eng
Copy link

at Laerdal.Dfu.Bindings.iOS.DFUFirmware..ctor(NSUrl urlToZipFile, NSError& error)
at Laerdal.Dfu.DfuServiceInitiatorDelegate..ctor(DfuInstallation dfuInstallation)
at Laerdal.Dfu.DfuInstallation.Start(DfuConfiguration configuration)
at MauiAttributeProgrammer.ViewModels.DFUPageViewModel.Start() in /Users/isspro/Documents/GitHub/MauiAttributeProgrammer/MauiAttributeProgrammer/ViewModels/DFUPageViewModel.cs:line 31

@MouratidisA Same error here now... guess it is time to start trying to modify the nuget.

@ISSPRO-Eng
Copy link

@MouratidisA Have you tried on an Android device or only iOS?

@juniorsaraviao
Copy link

I'm getting the same error using Net MAUI v8 on iOS.
Also, when I try to upgrade a firmware on Android I got the following error (issue):

PostForegroundServiceNotificationException: Bad notification for startForeground

@ISSPRO-Eng
Copy link

@juniorsaraviao Jumping back onto this project. Any luck with either iOS or Android? I of not need notifications or even to configure anything. Just need to select device and firmware file and send over. Everything else can be default for the time being.

@juniorsaraviao
Copy link

@ISSPRO-Eng I'm still getting issues on iOS, but there's a workaround to avoid Bad notification for startForeground on Android using (full implementation here):

DfuConfiguration() { DisableNotification = true };

@schneifa
Copy link

schneifa commented Jan 9, 2024

Going back to the original topic of this issue (the iOS error), the exception

at Laerdal.Dfu.Bindings.iOS.DFUFirmware..ctor(NSUrl urlToZipFile, NSError& error)
at Laerdal.Dfu.DfuServiceInitiatorDelegate..ctor(DfuInstallation dfuInstallation)
at Laerdal.Dfu.DfuInstallation.Start(DfuConfiguration configuration)
at MauiAttributeProgrammer.ViewModels.DFUPageViewModel.Start() in /Users/isspro/Documents/GitHub/MauiAttributeProgrammer/MauiAttributeProgrammer/ViewModels/DFUPageViewModel.cs:line 31

seems to occur in the SetInitiator()-Method of the iOS specific DfuInstallation.cs in line 26:

Firmware = new Laerdal.Dfu.iOS.DFUFrmware(new NSUrl(FileUrl, false), out NSError error);

As someone mentioned in another issue, nordic seems to have switched from NSUrl to Url type in their library implementation.
Unfortunately, I know very little about library binding development or iOS specific development, but could changing this line to

Firmware = new Laerdal.Dfu.iOS.DFUFrmware(new Url(FileUrl, false), out NSError error);

and accordingly changing the DFUFirmware interface in ApiDefinition.cs of ObjC and Sharpie from NSUrl to Url fix this issue?
Can someone with a little more knowledge of the package look into this and maybe try?

@WanftMoon
Copy link

For those interested, the issue is in the native bindings: dotnet/maui#29

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants