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

[NearbyInteraction] Updates for Xcode14 Beta 1 #15840

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/NearbyInteraction/Enums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public enum NIErrorCode : long
ResourceUsageTimeout = -5886,
ActiveSessionsLimitExceeded = -5885,
UserDidNotAllow = -5884,
AccessoryPeerDeviceUnavailable = -5882,
InvalidARConfiguration = -5883,
}

[Watch (8,0), NoTV, NoMac, iOS (14,0)]
Expand All @@ -33,4 +35,24 @@ public enum NINearbyObjectRemovalReason : long
Timeout,
PeerEnded,
}

[iOS (16,0), NoMac, Watch (9,0), NoTV, MacCatalyst (16,0)]
[Native]
public enum NIAlgorithmConvergenceStatus : long
{
Unknown,
NotConverged,
Converged,
}

[iOS (16,0), NoMac, Watch (9,0), NoTV, MacCatalyst (16,0)]
[Native]
public enum NINearbyObjectVerticalDirectionEstimate : long
{
Unknown = 0,
Same = 1,
Above = 2,
Below = 3,
AboveOrBelow = 4,
}
}
52 changes: 52 additions & 0 deletions src/NearbyInteraction/NIAlgorithmConvergenceStatusReasonValues.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//
// NIAlgorithmConvergenceStatusReasonDescription manual binding
//
// Authors:
// TJ Lambert <TJ.Lambert@microsoft.com>
//
// Copyright 2022 Microsoft Corp.
//

#if IOS || WATCH || __MACCATALYST__

#nullable enable

using System;
using System.Runtime.InteropServices;
using CoreFoundation;
using Foundation;
using ObjCRuntime;

#if !NET
using NativeHandle = System.IntPtr;
#endif

namespace NearbyInteraction {
public partial class NIAlgorithmConvergenceStatusReasonValues
{
#if NET
[SupportedOSPlatform ("ios16.0")]
[SupportedOSPlatform ("maccatalyst16.0")]
[UnsupportedOSPlatform ("tvos")]
[UnsupportedOSPlatform ("macos")]
#else
[iOS (16,0), NoMac, Watch (9,0), NoTV, MacCatalyst (16,0)]
#endif // NET
[DllImport (Constants.NearbyInteractionLibrary)]
static extern NativeHandle /* NSString */ NIAlgorithmConvergenceStatusReasonDescription (NativeHandle /* NIAlgorithmConvergenceStatusReason */ reason);

#if NET
[SupportedOSPlatform ("ios16.0")]
[SupportedOSPlatform ("maccatalyst16.0")]
[UnsupportedOSPlatform ("tvos")]
[UnsupportedOSPlatform ("macos")]
#else
[iOS (16,0), NoMac, Watch (9,0), NoTV, MacCatalyst (16,0)]
#endif // NET
public static NSString GetConvergenceStatusReason (NIAlgorithmConvergenceStatusReason reason)
{
return Runtime.GetNSObject<NSString> (NIAlgorithmConvergenceStatusReasonDescription (reason.GetConstant ().GetHandle ()))!;
}
}
}
#endif // IOS || WATCH || __MACCATALYST__
19 changes: 18 additions & 1 deletion src/NearbyInteraction/NINearbyObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
using ObjCRuntime;
#if NET
using Vector3 = global::System.Numerics.Vector3;
using MatrixFloat4x4 = global::CoreGraphics.NMatrix4;
#else
using Vector3 = global::OpenTK.Vector3;
using MatrixFloat4x4 = global::OpenTK.NMatrix4;
#endif

#if __IOS__ || WATCH
Expand All @@ -41,7 +43,22 @@ public static Vector3 DirectionNotAvailable {
return (Vector3)_DirectionNotAvailable;
}
}
}

static MatrixFloat4x4? _WorldTransformNotAvailable;

// Following similar strategy found here: https://github.com/xamarin/maccore/issues/2274
[Field ("NINearbyObjectWorldTransformNotAvailable", "NearbyInteraction")]
public static MatrixFloat4x4 WorldTransformNotAvailable {
get {
if (_WorldTransformNotAvailable is null) {
unsafe {
MatrixFloat4x4 *pointer = (MatrixFloat4x4 *) Dlfcn.GetIndirect (Libraries.NearbyInteraction.Handle, "NINearbyObjectWorldTransformNotAvailable");
_WorldTransformNotAvailable = *pointer;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will crash if executed on an earlier OS version (where the native field doesn't exist).

Something like this should work instead:

Suggested change
_WorldTransformNotAvailable = *pointer;
if (pointer == null)
throw new PlatformNotSupportedException ("This property is not supported on this version of the OS");
_WorldTransformNotAvailable = *pointer;

The property also needs availability attributes.

}
}
return (MatrixFloat4x4)_WorldTransformNotAvailable;
}
}
}
}
#endif
1 change: 1 addition & 0 deletions src/frameworks.sources
Original file line number Diff line number Diff line change
Expand Up @@ -1321,6 +1321,7 @@ NEARBYINTERACTION_API_SOURCES = \

NEARBYINTERACTION_SOURCES = \
NearbyInteraction/NINearbyObject.cs \
NearbyInteraction/NIAlgorithmConvergenceStatusReasonValues.cs \

# NetworkExtension

Expand Down
102 changes: 102 additions & 0 deletions src/nearbyinteraction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,20 @@
using Foundation;
using CoreFoundation;
using System;
#if __MACCATALYST__
using ARSession = Foundation.NSObject;
#elif IOS
using ARKit;
#else
using ARSession = Foundation.NSObject;
#endif

#if NET
using Vector3 = global::System.Numerics.Vector3;
using MatrixFloat4x4 = global::CoreGraphics.NMatrix4;
#else
using Vector3 = global::OpenTK.Vector3;
using MatrixFloat4x4 = global::OpenTK.NMatrix4;
#endif

#if !NET
Expand Down Expand Up @@ -46,6 +56,10 @@ interface NINearbyPeerConfiguration

[Export ("initWithPeerToken:")]
NativeHandle Constructor (NIDiscoveryToken peerToken);

[NoWatch, iOS (16,0), MacCatalyst (16,0), NoTV, NoMac]
[Export ("cameraAssistanceEnabled")]
bool CameraAssistanceEnabled { [Bind ("isCameraAssistanceEnabled")] get; set; }
}

[Watch (8,0), NoTV, NoMac, iOS (14,0)]
Expand All @@ -68,6 +82,18 @@ Vector3 Direction {

[Field ("NINearbyObjectDistanceNotAvailable")]
float DistanceNotAvailable { get; }

[Watch (9,0), NoTV, NoMac, iOS (16,0), MacCatalyst (16,0)]
[Field ("NINearbyObjectAngleNotAvailable")]
float AngleNotAvailable { get; }

[Watch (9,0), NoTV, NoMac, iOS (16,0), MacCatalyst (16,0)]
[Export ("verticalDirectionEstimate")]
NINearbyObjectVerticalDirectionEstimate VerticalDirectionEstimate { get; }

[Watch (9,0), NoTV, NoMac, iOS (16,0), MacCatalyst (16,0)]
[Export ("horizontalAngle")]
float HorizontalAngle { get; }
}

[Watch (8,0), NoTV, NoMac, iOS (14,0)]
Expand Down Expand Up @@ -103,6 +129,20 @@ interface NISession

[Export ("invalidate")]
void Invalidate ();

[Watch (9,0), NoTV, NoMac, iOS (16,0), MacCatalyst (16,0)]
[Export ("setARSession:")]
void SetARSession (ARSession session);

[Watch (9,0), NoTV, NoMac, iOS (16,0), MacCatalyst (16,0)]
[Export ("worldTransformForObject:")]
[MarshalDirective (NativePrefix = "xamarin_simd__", Library = "__Internal")]
MatrixFloat4x4 GetWorldTransform (NINearbyObject @object);

[Watch (9,0), NoTV, NoMac, iOS (16,0), MacCatalyst (16,0)]
[Static]
[Export ("deviceCapabilities")]
INIDeviceCapability DeviceCapabilities { get; }
}

interface INISessionDelegate {}
Expand Down Expand Up @@ -136,6 +176,14 @@ interface NISessionDelegate
[Watch (8,0), NoTV, NoMac, iOS (15,0), MacCatalyst (15,0)]
[Export ("session:didGenerateShareableConfigurationData:forObject:")]
void DidGenerateShareableConfigurationData (NISession session, NSData shareableConfigurationData, NINearbyObject @object);

[Watch (9,0), NoTV, NoMac, iOS (16,0), MacCatalyst (16,0)]
[Export ("session:didUpdateAlgorithmConvergence:forObject:")]
void DidUpdateAlgorithmConvergence (NISession session, NIAlgorithmConvergence convergence, [NullAllowed] NINearbyObject @object);

[Watch (9,0), NoTV, NoMac, iOS (16,0), MacCatalyst (16,0)]
[Export ("sessionDidStartRunning:")]
void SessionDidStartRunning (NISession session);
}

[Watch (8,0), NoTV, NoMac, iOS (15,0), MacCatalyst (15,0)]
Expand All @@ -148,6 +196,60 @@ interface NINearbyAccessoryConfiguration

[Export ("initWithData:error:")]
NativeHandle Constructor (NSData data, [NullAllowed] out NSError error);

[iOS (16,0), NoMac, NoWatch, NoTV, MacCatalyst (16,0)]
[Export ("initWithAccessoryData:bluetoothPeerIdentifier:error:")]
NativeHandle Constructor (NSData accessoryData, NSUuid identifier, [NullAllowed] out NSError error);

[iOS (16,0), NoMac, NoWatch, NoTV, MacCatalyst (16,0)]
[Export ("cameraAssistanceEnabled")]
bool CameraAssistanceEnabled { [Bind ("isCameraAssistanceEnabled")] get; set; }
}

[iOS (16,0), NoMac, Watch (9,0), NoTV, MacCatalyst (16,0)]
public enum NIAlgorithmConvergenceStatusReason
{
[Field ("NIAlgorithmConvergenceStatusReasonInsufficientHorizontalSweep")]
InsufficientHorizontalSweep,

[Field ("NIAlgorithmConvergenceStatusReasonInsufficientVerticalSweep")]
InsufficientVerticalSweep,

[Field ("NIAlgorithmConvergenceStatusReasonInsufficientMovement")]
InsufficientMovement,

[Field ("NIAlgorithmConvergenceStatusReasonInsufficientLighting")]
InsufficientLighting,
}

interface INIDeviceCapability {}

[Watch (9,0), NoTV, NoMac, iOS (16,0), MacCatalyst (16,0)]
[Protocol]
interface NIDeviceCapability
{
[Abstract]
[Export ("supportsPreciseDistanceMeasurement")]
bool SupportsPreciseDistanceMeasurement { get; }

[Abstract]
[Export ("supportsDirectionMeasurement")]
bool SupportsDirectionMeasurement { get; }

[Abstract]
[Export ("supportsCameraAssistance")]
bool SupportsCameraAssistance { get; }
}

[Watch (9,0), NoTV, NoMac, iOS (16,0), MacCatalyst (16,0)]
[BaseType (typeof (NSObject))]
[DisableDefaultCtor]
interface NIAlgorithmConvergence : NSCopying, NSSecureCoding
{
[Export ("status")]
NIAlgorithmConvergenceStatus Status { get; }

[Export ("reasons")]
string[] Reasons { get; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2022 Microsoft Corp.

#if IOS || WATCH || __MACCATALYST__

using System;
using Foundation;
using ObjCRuntime;
using NearbyInteraction;
using NUnit.Framework;
using Xamarin.Utils;

namespace MonoTouchFixtures.NearbyInteraction {

[TestFixture]
[Preserve (AllMembers = true)]
public class NIAlgorithmConvergenceStatusReasonValuesTest {

[SetUp]
public void Setup ()
{
TestRuntime.AssertXcodeVersion (14, 0);
}

[Test]
public void GetConvergenceStatusReasonTest ()
{
var reason = NIAlgorithmConvergenceStatusReason.InsufficientHorizontalSweep;
Assert.IsNotNull (NIAlgorithmConvergenceStatusReasonValues.GetConvergenceStatusReason (reason), "NIAlgorithmConvergenceStatusReason should not be null.");
}
}
}

#endif // IOS || WATCH || __MACCATALYST__
18 changes: 18 additions & 0 deletions tests/monotouch-test/NearbyInteraction/NINearbyObjectTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@

#if NET
using System.Numerics;
using MatrixFloat4x4 = global::CoreGraphics.NMatrix4;
#else
using OpenTK;
using MatrixFloat4x4 = global::OpenTK.NMatrix4;
#endif

namespace MonoTouchFixtures.NearbyInteraction {
Expand Down Expand Up @@ -42,6 +44,22 @@ public void DirectionNotAvailable ()
Assert.That (ptr [i], Is.EqualTo (zero), $"Position {i}");
}
}

[Test]
public void WorldTransformNotAvailable ()
{
TestRuntime.AssertXcodeVersion (14, 0);

MatrixFloat4x4 matrix = NINearbyObject.WorldTransformNotAvailable;

unsafe {
MatrixFloat4x4* m = &matrix;
byte* ptr = (byte*) m;
byte zero = 0;
for (var i = 0; i < sizeof (MatrixFloat4x4); i++)
Assert.That (ptr [i], Is.EqualTo (zero), $"Position {i}");
}
}
}
}

Expand Down

This file was deleted.

Loading