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

[Bug] iOS - "Exception of type 'Foundation.You_Should_Not_Call_base_In_This_Method' was thrown." #29

Open
WanftMoon opened this issue Jan 12, 2024 · 23 comments

Comments

@WanftMoon
Copy link

WanftMoon commented Jan 12, 2024

Describe the bug
Project in xamarin forms with ble.net, when calling start in iOS, the following exception is thrown:

"Exception of type 'Foundation.You_Should_Not_Call_base_In_This_Method' was thrown."

obs: Need guidance in case i'm missing something to setup the dfu correctly in iOS, because the same exception is not happening in Android. The process starts ok

at Laerdal.Dfu.Bindings.iOS.DFUFirmware..ctor (Foundation.NSUrl urlToZipFile, Foundation.NSError& error) [0x0000d] in /Users/runner/work/1/s/Laerdal.Dfu.Bindings.iOS/obj/Release/xamarin.ios10/iOS/Laerdal.Dfu.Bindings.iOS/DFUFirmware.g.cs:119 
  at Laerdal.Dfu.DfuServiceInitiatorDelegate..ctor (Laerdal.Dfu.DfuInstallation dfuInstallation) [0x00010] in /_/Laerdal.Dfu/Platforms/iOS/DfuServiceInitiatorDelegate.cs:29 
  at Laerdal.Dfu.DfuInstallation.Start (Laerdal.Dfu.DfuConfiguration configuration) [0x00034] in /_/Laerdal.Dfu/Platforms/iOS/DfuInstallation.cs:33 
  at Viewmodel.BleConnectedDeviceViewModel.FlashDevice (System.Object arg) [0x0010c] 

To Reproduce
Steps to reproduce the behavior:

  1. Create Xamarin.Forms project
  2. Include ble library and configure it to scan a device
  3. Add Laerdal.Dfu
  4. Configure install
dfuInstall = new DfuInstallation(deviceAddress /*59:A1:A6:62:41:7A*/, fi.FullName);

dfuInstall.DfuErrorReceived += OnDfuErrorOccured;
dfuInstall.DfuStateChanged += OnDfuStateChanged;
dfuInstall.DfuProgressChanged += OnDfuProgressChanged;

dfuInstall.Start(new DfuConfiguration() { DisableNotification = true, Foreground = false });

Expected behavior
The install to start like it does in Android.

Screenshots
N/A

Desktop (please complete the following information):

  • OS: MacOS Sonoma
  • Versions
    -- ble.net 1.2.1
    -- Laerdal.Dfu 1.27.2-xamarin-10.45630
    -- Xamarin.Essentials - 1.8.1
    -- Xamarin.Forms - 5.0.0.2622

Smartphone (please complete the following information):

  • Device: iPhone 8+
  • OS: 16
  • Browser N/A
  • Version N/A

Additional context
N/A

@schneifa
Copy link

schneifa commented Jan 13, 2024

We are currently discussing this issue in #27.

@WanftMoon
Copy link
Author

WanftMoon commented Jan 22, 2024

Oki, for those interested. The issue is in: https://github.com/Laerdal/Laerdal.Dfu.Bindings.iOS/tree/master

@schneifa @framinosona @ksidirop-laerdal #27

I re-did the bindings locally, so if anyone that maintain the project and would like to check the changes below and also update all the native binding and references, that would make people happy.

  1. Project linking against a xcframework - set to no binding Embedding (right now is embedding)
    <NoBindingEmbedding>true</NoBindingEmbedding>

  2. The api definition needs tweaking:
    a. Interfaces just need the BaseType (the exception reported was because there were [Model] everywhere which generates a model with no code besides the exception being thrown

// @interface DFUFirmware : NSObject
[BaseType(typeof(NSObject), Name = "_TtC9NordicDFU11DFUFirmware")]
[DisableDefaultCtor]
interface DFUFirmware

b. Protocols, there are only 4 of them. I've left the [BaseType][Protocol][Model]

// @protocol DFUPeripheralSelectorDelegate
[BaseType(typeof(NSObject), Name = "_TtP9NordicDFU29DFUPeripheralSelectorDelegate_")]
[Protocol(Name = "_TtP9NordicDFU29DFUPeripheralSelectorDelegate_")]
[Model(Name = "_TtP9NordicDFU29DFUPeripheralSelectorDelegate_")]
interface DFUPeripheralSelectorDelegate

att.

@ISSPRO-Eng
Copy link

Thank you for the work @WanftMoon !!!! Hopefully the local project is updated soon :)

@algrm
Copy link

algrm commented Feb 7, 2024

@WanftMoon thanks for your insights. I don't think it fully applies here, as AppDefinition.cs here is different from the one in Laerdal.Dfu.Bindings.iOS. And thanks @framinosona for sharing this work, extremely useful to me.
After several days on this topic, here is what I did:

  • Rebuild separate nuget packages for both bindings, from Laerdal's sources. I removed the net6 support and applied @WanftMoon advices on the iOS package. The iOS library must be build on a Mac.
  • Create a new MAUI/.NET 8 library and copy Laerdal.Dfu source in it. This is mostly a matter or putting the files in different folders. I use the binding packages as dependencies.

I stumbled today on another problem (undefined symbol for the ios Nordic functions). I solved it by changing the path of the framework dependencies and make them point to the arm64 framework in the Carthage folder.

I hope this might help. Too many changes to make a PR but I'll be happy to share my work if asked.

@neicureuil
Copy link

Hi,
I'm facing the same issue, I tried to compile the bindings my self and following the instructions of @WanftMoon but I don't really understand what need to be change in the iOS binding.

I have tried to remove the "[Model]" attribute but same error : "Foundation.You_Should_Not_Call_base_In_This_Method".

Can you provide a patch or help please ?

@WanftMoon
Copy link
Author

WanftMoon commented Mar 8, 2024

Hi, I'm facing the same issue, I tried to compile the bindings my self and following the instructions of @WanftMoon but I don't really understand what need to be change in the iOS binding.

I have tried to remove the "[Model]" attribute but same error : "Foundation.You_Should_Not_Call_base_In_This_Method".

Can you provide a patch or help please ?

if you are re-doing the native binding: just make sure you edit your api definition file as follows:

a. Interfaces just need the BaseType (the exception reported was because there were [Model] everywhere which generates a model with no code besides the exception being thrown

// @interface DFUFirmware : NSObject
[BaseType(typeof(NSObject), Name = "_TtC9NordicDFU11DFUFirmware")]
[DisableDefaultCtor]
interface DFUFirmware

b. Protocols, there are only 4 of them. I've left the [BaseType][Protocol][Model]

// @protocol DFUPeripheralSelectorDelegate
[BaseType(typeof(NSObject), Name = "_TtP9NordicDFU29DFUPeripheralSelectorDelegate_")]
[Protocol(Name = "_TtP9NordicDFU29DFUPeripheralSelectorDelegate_")]
[Model(Name = "_TtP9NordicDFU29DFUPeripheralSelectorDelegate_")]
interface DFUPeripheralSelectorDelegate

@neicureuil
Copy link

That's what I did but I got the same error :

[0:] Exception choosing file: Foundation.You_Should_Not_Call_base_In_This_Method: Exception of type 'Foundation.You_Should_Not_Call_base_In_This_Method' was thrown.
  at Laerdal.Dfu.Bindings.iOS.DFUFirmware..ctor (Foundation.NSUrl urlToZipFile, Foundation.NSError& error) [0x0000d] in /Users/runner/work/1/s/Laerdal.Dfu.Bindings.iOS/obj/Release/xamarin.ios10/iOS/Laerdal.Dfu.Bindings.iOS/DFUFirmware.g.cs:119 
  at Laerdal.Dfu.DfuServiceInitiatorDelegate..ctor (Laerdal.Dfu.DfuInstallation dfuInstallation) [0x00010] in /Users/neicureuil/Documents/Laerdal.Dfu/Laerdal.Dfu/Platforms/iOS/DfuServiceInitiatorDelegate.cs:29 
  at Laerdal.Dfu.DfuInstallation.Start (Laerdal.Dfu.DfuConfiguration configuration) [0x00034] in /Users/neicureuil/Documents/Laerdal.Dfu/Laerdal.Dfu/Platforms/iOS/DfuInstallation.cs:33 
  at Nordic.Sdk.Impl.Dfu.DfuManager.Start (System.String deviceAddress, System.String firmwareZipFile) [0x00062] in C:\Users

Here is my modified ApiDefinition.cs, do you see anything that I forgot ? https://gist.github.com/neicureuil/d2893c384437422c5a4f1c6bbb20f677

@WanftMoon
Copy link
Author

That's what I did but I got the same error :

[0:] Exception choosing file: Foundation.You_Should_Not_Call_base_In_This_Method: Exception of type 'Foundation.You_Should_Not_Call_base_In_This_Method' was thrown.
  at Laerdal.Dfu.Bindings.iOS.DFUFirmware..ctor (Foundation.NSUrl urlToZipFile, Foundation.NSError& error) [0x0000d] in /Users/runner/work/1/s/Laerdal.Dfu.Bindings.iOS/obj/Release/xamarin.ios10/iOS/Laerdal.Dfu.Bindings.iOS/DFUFirmware.g.cs:119 
  at Laerdal.Dfu.DfuServiceInitiatorDelegate..ctor (Laerdal.Dfu.DfuInstallation dfuInstallation) [0x00010] in /Users/neicureuil/Documents/Laerdal.Dfu/Laerdal.Dfu/Platforms/iOS/DfuServiceInitiatorDelegate.cs:29 
  at Laerdal.Dfu.DfuInstallation.Start (Laerdal.Dfu.DfuConfiguration configuration) [0x00034] in /Users/neicureuil/Documents/Laerdal.Dfu/Laerdal.Dfu/Platforms/iOS/DfuInstallation.cs:33 
  at Nordic.Sdk.Impl.Dfu.DfuManager.Start (System.String deviceAddress, System.String firmwareZipFile) [0x00062] in C:\Users

Here is my modified ApiDefinition.cs, do you see anything that I forgot ? https://gist.github.com/neicureuil/d2893c384437422c5a4f1c6bbb20f677

Looking at your file, you didn't. You have several classes that maps the sdk interfaces marked as protocol.

// @interface DFUFirmware : NSObject
        [BaseType (typeof(NSObject), Name = "_TtC13iOSDFULibrary11DFUFirmware")]
        [Protocol (Name = "_TtC13iOSDFULibrary11DFUFirmware")]
        [DisableDefaultCtor]
        interface DFUFirmware

@neicureuil
Copy link

Thanks for your help, it was a nuget cache issue.
With your modification, I got an other error :

[0:] Exception choosing file: System.Exception: Could not create an native instance of the type 'Laerdal.Dfu.Bindings.iOS.DFUFirmware': the native class hasn't been loaded.
It is possible to ignore this condition by setting ObjCRuntime.Class.ThrowOnInitFailure to false.
  at Foundation.NSObject.InitializeObject (System.Boolean alloced) [0x000b2] in /Library/Frameworks/Xamarin.iOS.framework/Versions/16.4.0.23/src/Xamarin.iOS/Foundation/NSObject2.cs:360 
  at Foundation.NSObject..ctor (Foundation.NSObjectFlag x) [0x0000d] in /Library/Frameworks/Xamarin.iOS.framework/Versions/16.4.0.23/src/Xamarin.iOS/Foundation/NSObject2.cs:201 

It seems that the native lib isn't loaded. Do you have any idea why I get this error ?

@WanftMoon
Copy link
Author

Thanks for your help, it was a nuget cache issue. With your modification, I got an other error :

[0:] Exception choosing file: System.Exception: Could not create an native instance of the type 'Laerdal.Dfu.Bindings.iOS.DFUFirmware': the native class hasn't been loaded.
It is possible to ignore this condition by setting ObjCRuntime.Class.ThrowOnInitFailure to false.
  at Foundation.NSObject.InitializeObject (System.Boolean alloced) [0x000b2] in /Library/Frameworks/Xamarin.iOS.framework/Versions/16.4.0.23/src/Xamarin.iOS/Foundation/NSObject2.cs:360 
  at Foundation.NSObject..ctor (Foundation.NSObjectFlag x) [0x0000d] in /Library/Frameworks/Xamarin.iOS.framework/Versions/16.4.0.23/src/Xamarin.iOS/Foundation/NSObject2.cs:201 

It seems that the native lib isn't loaded. Do you have any idea why I get this error ?

You might want to check that the xcframework is being copied to the correct location (referencing the ios native binding in your ios project might help).
Also, check that native binding is not embedding.
<NoBindingEmbedding>true</NoBindingEmbedding>

@neicureuil
Copy link

It's work now !
I have to directly reference the xcframework in my iOS app project. Is there a way not to have to do this ?

Thank you for your help, you save me !

@ISSPRO-Eng
Copy link

@algrm or @neicureuil

Any chance you could share the updated Nuget Package with me? I do not have any experience on building or editing NuGet Packages, so I am still waiting on an update to the master Nuget.

@ISSPRO-Eng
Copy link

Detected package downgrade: Laerdal.Dfu.Bindings.iOS from 4.13.0.43857 to 4.13.0.88. Reference the package directly from the project to select a different version.

@algrm @WanftMoon @neicureuil

Getting this message when trying to add a newly built iOS Bindings Library to my app to replace the 4.13.0.43857 version that Laerdal.Dfu 1.27.8 relies on

@WanftMoon
Copy link
Author

Detected package downgrade: Laerdal.Dfu.Bindings.iOS from 4.13.0.43857 to 4.13.0.88. Reference the package directly from the project to select a different version.

@algrm @WanftMoon @neicureuil

Getting this message when trying to add a newly built iOS Bindings Library to my app to replace the 4.13.0.43857 version that Laerdal.Dfu 1.27.8 relies on

Don't use both, since you build a native binding, you will need to do the same with the wrapper (laerdal.dfu). Just get Laerdal.Dfu code, add it to the solution, then update the references.

@ISSPRO-Eng
Copy link

Detected package downgrade: Laerdal.Dfu.Bindings.iOS from 4.13.0.43857 to 4.13.0.88. Reference the package directly from the project to select a different version.
@algrm @WanftMoon @neicureuil
Getting this message when trying to add a newly built iOS Bindings Library to my app to replace the 4.13.0.43857 version that Laerdal.Dfu 1.27.8 relies on

Don't use both, since you build a native binding, you will need to do the same with the wrapper (laerdal.dfu). Just get Laerdal.Dfu code, add it to the solution, then update the references.

Guess I am a bit confused. I cloned both the Laerdal.Dfu Library and the iOS Binding Library. I updated the Bindings Library to use your changes. How would I then rebuild the Laerdal.Dfu Nuget to use the updated bindings library I made? Sorry a bit new to this and normally just use third party sources since I am super fresh to C#

@WanftMoon
Copy link
Author

WanftMoon commented Mar 19, 2024

Detected package downgrade: Laerdal.Dfu.Bindings.iOS from 4.13.0.43857 to 4.13.0.88. Reference the package directly from the project to select a different version.
@algrm @WanftMoon @neicureuil
Getting this message when trying to add a newly built iOS Bindings Library to my app to replace the 4.13.0.43857 version that Laerdal.Dfu 1.27.8 relies on

Don't use both, since you build a native binding, you will need to do the same with the wrapper (laerdal.dfu). Just get Laerdal.Dfu code, add it to the solution, then update the references.

Guess I am a bit confused. I cloned both the Laerdal.Dfu Library and the iOS Binding Library. I updated the Bindings Library to use your changes. How would I then rebuild the Laerdal.Dfu Nuget to use the updated bindings library I made? Sorry a bit new to this and normally just use third party sources since I am super fresh to C#

Just edit the csproj of the Laerdal.Dfu to reference your bindings.

<ItemGroup Condition="$(TargetFramework.StartsWith('xamarin.ios'))">
        <Compile Include="Platforms\iOS\*.cs" />
	<!-- this is the old reference <PackageReference Include="Laerdal.Dfu.Bindings.iOS" Version="4.13.0-xamarin.43596" />-->
	<ProjectReference Include="..\MY_PATH_TO_MY_NATIVE_BINDINGS\Dfu.Bindings.iOS.csproj" />
</ItemGroup>

And don't use the published nuget, just reference your local projects

@ISSPRO-Eng
Copy link

ISSPRO-Eng commented Mar 19, 2024

Detected package downgrade: Laerdal.Dfu.Bindings.iOS from 4.13.0.43857 to 4.13.0.88. Reference the package directly from the project to select a different version.
@algrm @WanftMoon @neicureuil
Getting this message when trying to add a newly built iOS Bindings Library to my app to replace the 4.13.0.43857 version that Laerdal.Dfu 1.27.8 relies on

Don't use both, since you build a native binding, you will need to do the same with the wrapper (laerdal.dfu). Just get Laerdal.Dfu code, add it to the solution, then update the references.

Guess I am a bit confused. I cloned both the Laerdal.Dfu Library and the iOS Binding Library. I updated the Bindings Library to use your changes. How would I then rebuild the Laerdal.Dfu Nuget to use the updated bindings library I made? Sorry a bit new to this and normally just use third party sources since I am super fresh to C#

Just edit the csproj of the Laerdal.Dfu to reference your bindings.

<ItemGroup Condition="$(TargetFramework.StartsWith('xamarin.ios'))">
        <Compile Include="Platforms\iOS\*.cs" />
	<!-- this is the old reference <PackageReference Include="Laerdal.Dfu.Bindings.iOS" Version="4.13.0-xamarin.43596" />-->
	<ProjectReference Include="..\MY_PATH_TO_MY_NATIVE_BINDINGS\Dfu.Bindings.iOS.csproj" />
</ItemGroup>

Okay thanks. I see the current package version of the cloned Laerdal Dfu project is 0.0.2. Did this need any change since the latest Nuget is 1.27.8 or can that just stay the same

Also, got this when referencing the csproj library/Frameworks/Mono.framework/Versions/6.12.0/lib/mono/msbuild/Current/bin/Microsoft.Common.CurrentVersion.targets(1982,5): warning : The referenced project 'Users/isspro/Documents/GitHub/nuget_repo/Laerdal.Dfu.Bindings.iOS/Laerdal.Dfu.Bindings.iOS/Dfu.Bindings.iOS.csproj' does not exist. [/Users/isspro/Documents/GitHub/nuget_repo/Laerdal.Dfu/Laerdal.Dfu/Laerdal.Dfu.csproj]

@WanftMoon
Copy link
Author

Detected package downgrade: Laerdal.Dfu.Bindings.iOS from 4.13.0.43857 to 4.13.0.88. Reference the package directly from the project to select a different version.
@algrm @WanftMoon @neicureuil
Getting this message when trying to add a newly built iOS Bindings Library to my app to replace the 4.13.0.43857 version that Laerdal.Dfu 1.27.8 relies on

Don't use both, since you build a native binding, you will need to do the same with the wrapper (laerdal.dfu). Just get Laerdal.Dfu code, add it to the solution, then update the references.

Guess I am a bit confused. I cloned both the Laerdal.Dfu Library and the iOS Binding Library. I updated the Bindings Library to use your changes. How would I then rebuild the Laerdal.Dfu Nuget to use the updated bindings library I made? Sorry a bit new to this and normally just use third party sources since I am super fresh to C#

Just edit the csproj of the Laerdal.Dfu to reference your bindings.

<ItemGroup Condition="$(TargetFramework.StartsWith('xamarin.ios'))">
        <Compile Include="Platforms\iOS\*.cs" />
	<!-- this is the old reference <PackageReference Include="Laerdal.Dfu.Bindings.iOS" Version="4.13.0-xamarin.43596" />-->
	<ProjectReference Include="..\MY_PATH_TO_MY_NATIVE_BINDINGS\Dfu.Bindings.iOS.csproj" />
</ItemGroup>

Okay thanks. I see the current package version of the cloned Laerdal Dfu project is 0.0.2. Did this need any change since the latest Nuget is 1.27.8 or can that just stay the same

Also, got this when referencing the csproj library/Frameworks/Mono.framework/Versions/6.12.0/lib/mono/msbuild/Current/bin/Microsoft.Common.CurrentVersion.targets(1982,5): warning : The referenced project 'Users/isspro/Documents/GitHub/nuget_repo/Laerdal.Dfu.Bindings.iOS/Laerdal.Dfu.Bindings.iOS/Dfu.Bindings.iOS.csproj' does not exist. [/Users/isspro/Documents/GitHub/nuget_repo/Laerdal.Dfu/Laerdal.Dfu/Laerdal.Dfu.csproj]

The referenced project ... does not exist looks like your path is wrong.

@ISSPRO-Eng
Copy link

@WanftMoon What branch did you use? I had been on the main branch and am now seeing it is not even updated for .NET 7 which is what I am using. Is the develop branch the one that you used?

@WanftMoon
Copy link
Author

@WanftMoon What branch did you use? I had been on the main branch and am now seeing it is not even updated for .NET 7 which is what I am using. Is the develop branch the one that you used?

My project is xamarin forms.

@ISSPRO-Eng
Copy link

Has anyone had any luck with the newer release of the nuget in fixing this issue or still on going?

@framinosona
Copy link
Member

Hi, we've had a bit of time this morning to work on this. Can you guys test https://www.nuget.org/packages/Laerdal.Dfu/1.27.62 ?

@ISSPRO-Eng
Copy link

ISSPRO-Eng commented May 7, 2024

@framinosona Still getting the error: Foundation.You_Should_Not_Call_base_In_This_Method: Exception of type 'Foundation.You_Should_Not_Call_base_In_This_Method' was thrown.
at Laerdal.Dfu.Bindings.iOS.DFUFirmware..ctor(NSUrl urlToZipFile, NSError& error)

It looks like you need to still remove the Model form the Interfaces in the Laerdal.Dfu.Bindings.iOS. It looked like you removed them, but then you re added them in

ksidirop-laerdal added a commit that referenced this issue May 15, 2024
…ddress the native errors that users report under iOS

   #29 (comment)
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