Bug 60303 - Homekit enum crashes
Summary: Homekit enum crashes
Status: RESOLVED FIXED
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: XI 11.4 (d15-5)
Hardware: PC Mac OS
: --- normal
Target Milestone: xcode9.2
Assignee: Sebastien Pouliot
URL:
Depends on:
Blocks:
 
Reported: 2017-10-20 21:11 UTC by James Clancey
Modified: 2017-11-22 02:00 UTC (History)
4 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description James Clancey 2017-10-20 21:11:29 UTC
For homekit we took a lot of the strings and bound them to enums.

This doesn't work in the real world.  Devices can pass whatever string they want...

https://developer.apple.com/documentation/homekit/hmservice/1615885-servicetype
https://developer.apple.com/documentation/homekit/hmcharacteristic/1624197-characteristictype

When I try and run samples against my real devices everything blows up.


These are the two I have had issues with so far.


Errors:

{HMService 80C51926-F8B4-5FD8-AA1A-17A83E24628A: Custom(49FB9D4D-0FEA-4BF1-8FA6-E7B18AB86DCE)}

System.NotSupportedException: 49FB9D4D-0FEA-4BF1-8FA6-E7B18AB86DCE has no associated enum value in HMServiceType on this platform.
  at HomeKit.HMServiceTypeExtensions.GetValue (Foundation.NSString constant) [0x002f4] in /Users/builder/data/lanes/5481/2f8bbec0/source/xamarin-macios/src/build/ios/native/HomeKit/HMServiceType.g.cs:715 
  at HomeKit.HMService.get_ServiceType () [0x00000] in /Users/builder/data/lanes/5481/2f8bbec0/source/xamarin-macios/src/build/ios/native/HomeKit/HMService.g.cs:278 


and

System.NotSupportedException: E863F10C-079E-48FF-8F27-9C2605A29F52 has no associated enum value in HMCharacteristicType on this platform.
  at HomeKit.HMCharacteristicTypeExtensions.GetValue (Foundation.NSString constant) [0x00885] in /Users/builder/data/lanes/5481/2f8bbec0/source/xamarin-macios/src/build/ios/native/HomeKit/HMCharacteristicType.g.cs:2002 
  at HomeKit.HMCharacteristic.get_CharacteristicType () [0x00000] in /Users/builder/data/lanes/5481/2f8bbec0/source/xamarin-macios/src/build/ios/native/HomeKit/HMCharacteristic.g.cs:227
Comment 1 James Clancey 2017-10-20 21:21:35 UTC
One thing we can do to fix it.

Create a new enum type "Custom"
We return that if we don't recognize the string value.

We also would need to expose the original string so the developer can handle that edge case.

This should be done to most enums in HomeKit since apple doesn't enforce those string values.
Comment 2 Manuel de la Peña [MSFT] 2017-10-23 11:42:40 UTC
Cab you provide the sample you are using to get this crash? That way we can as QA to take a look and ensure we do have a workaround.
Comment 3 James Clancey 2017-10-23 17:28:48 UTC
I used https://developer.xamarin.com/samples/monotouch/HomeKit/HomeKitIntro/

However, you will need some smart equipment to connect it too.  The Homekit simulator may work. If you add custom elements to the hardware you emulate.

I can also test fix. Since a few things in my house cause it to blow up.
Comment 4 Vincent Dondain [MSFT] 2017-11-15 23:09:11 UTC
So one *simple* but not optimal workaround for this issue is to add:

```
[DefaultEnumValue]
[Field (null)]
Custom = -1,
```

...to the smart enum and people have to use:

```
[Export ("serviceType", ArgumentSemantic.Copy)]
NSString WeakServiceType { get; }
```

...to access the original constant.

Here's the equivalent of that change where the generated code is moved to a dedicated binding file and some tests that show the *expected* behavior https://gist.github.com/VincentDondain/aafc3708d08dad6e581a909757c89655

In the test we're using `GetConstant ()` which doesn't work as we'd need to fix `public static NSString GetConstant (this HMServiceType self)` for that by ideally returning `WeakServiceType` which sounds impossible to me as this is an enum extension.

We're currently discussing a general solution for that internally.
Comment 5 Sebastien Pouliot 2017-11-21 20:46:22 UTC
https://github.com/xamarin/xamarin-macios/pull/3025 expose weakly typed (NSString) API for HomeKit

Next step is being tracked with https://bugzilla.xamarin.com/show_bug.cgi?id=60790
Comment 6 Sebastien Pouliot 2017-11-22 02:00:28 UTC
Weakly (NSString) based alternative API are now exposed and can be used, instead of enums, when dealing with extended (non defined) constants.

Merged in xcode9.2 with https://github.com/xamarin/xamarin-macios/commit/a2d9c80cb5606ab7669c299c557c69fbb4eb786e