Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;

namespace CoreBluetooth.Foundation
{
internal static class AnyObject
{
public static void Release(IntPtr handle)
{
if (handle != IntPtr.Zero)
{
NativeMethods.any_object_release(handle);
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ internal static class NativeMethods
#endif

[DllImport(DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
internal static extern void ns_object_release(IntPtr handle);
internal static extern void any_object_release(IntPtr handle);

[DllImport(DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
internal static extern SafeNSNumberHandle ns_number_new_bool([MarshalAs(UnmanagedType.I1)] bool value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ protected SafeNSObjectHandle(IntPtr handle) : base(true)

protected override bool ReleaseHandle()
{
NativeMethods.ns_object_release(handle);
AnyObject.Release(handle);
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ internal static class NativeMethods
#else
const string DLL_NAME = "libCoreBluetoothForUnity";
#endif
[DllImport(DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
internal static extern void cb4u_central_release(IntPtr handle);

[DllImport(DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
internal static extern IntPtr cb4u_central_identifier(
SafeNativeCentralHandle handle,
Expand Down Expand Up @@ -74,9 +71,6 @@ int serviceUUIDsCount
[return: MarshalAs(UnmanagedType.I1)]
internal static extern bool cb4u_central_manager_is_scanning(SafeNativeCentralManagerHandle handle);

[DllImport(DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
internal static extern void cb4u_peripheral_release(IntPtr handle);

// NOTE: using comma-separated service UUIDs instead of an array of service UUIDs to avoid marshalling issues
internal delegate void CB4UPeripheralDidDiscoverServicesHandler(IntPtr peripheralPtr, IntPtr commaSeparatedServiceUUIDsPtr, int errorCode);
internal delegate void CB4UPeripheralDidDiscoverCharacteristicsHandler(IntPtr peripheralPtr, IntPtr serviceUUIDPtr, IntPtr commaSeparatedCharacteristicUUIDsPtr, int errorCode);
Expand Down Expand Up @@ -179,9 +173,6 @@ internal static extern int cb4u_peripheral_characteristic_properties(
[DllImport(DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
internal static extern SafeNativePeripheralManagerHandle cb4u_peripheral_manager_new(IntPtr options);

[DllImport(DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
internal static extern void cb4u_peripheral_manager_release(IntPtr handle);

internal delegate void CB4UPeripheralManagerDidUpdateStateHandler(IntPtr peripheralManagerPtr, CBManagerState state);
internal delegate void CB4UPeripheralManagerDidAddServiceHandler(IntPtr peripheralManagerPtr, IntPtr serviceUUIDPtr, int errorCode);
internal delegate void CB4UPeripheralManagerDidStartAdvertisingHandler(IntPtr peripheralManagerPtr, int errorCode);
Expand Down Expand Up @@ -251,9 +242,6 @@ int subscribedCentralsCount
[DllImport(DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
internal static extern SafeNativeMutableServiceHandle cb4u_mutable_service_new([MarshalAs(UnmanagedType.LPStr), In] string uuid, [MarshalAs(UnmanagedType.I1)] bool primary);

[DllImport(DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
internal static extern void cb4u_mutable_service_release(IntPtr handle);

[DllImport(DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
internal static extern int cb4u_mutable_service_set_characteristics(SafeNativeMutableServiceHandle handle, IntPtr[] characteristics, int characteristicsCount);

Expand All @@ -266,9 +254,6 @@ internal static extern SafeNativeMutableCharacteristicHandle cb4u_mutable_charac
int permissions
);

[DllImport(DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
internal static extern void cb4u_mutable_characteristic_release(IntPtr handle);

[DllImport(DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
internal static extern int cb4u_mutable_characteristic_value_length(SafeNativeMutableCharacteristicHandle handle);

Expand All @@ -290,9 +275,6 @@ int permissions
[DllImport(DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
internal static extern void cb4u_mutable_characteristic_set_permissions(SafeNativeMutableCharacteristicHandle handle, int permissions);

[DllImport(DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
internal static extern void cb4u_att_request_release(IntPtr handle);

[DllImport(DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
internal static extern IntPtr cb4u_att_request_central(SafeNativeATTRequestHandle handle);

Expand All @@ -317,9 +299,6 @@ int characteristicUUIDSize
[DllImport(DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
internal static extern int cb4u_att_request_offset(SafeNativeATTRequestHandle handle);

[DllImport(DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
internal static extern IntPtr cb4u_att_requests_release(IntPtr handle);

[DllImport(DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
internal static extern int cb4u_att_requests_count(SafeNativeATTRequestsHandle handle);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Runtime.InteropServices;
using CoreBluetooth.Foundation;

namespace CoreBluetooth
{
Expand All @@ -11,7 +12,7 @@ internal SafeNativeATTRequestHandle(IntPtr handle) : base(handle, true) { }

protected override bool ReleaseHandle()
{
NativeMethods.cb4u_att_request_release(handle);
AnyObject.Release(handle);
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Runtime.InteropServices;
using CoreBluetooth.Foundation;

namespace CoreBluetooth
{
Expand All @@ -11,7 +12,7 @@ internal SafeNativeATTRequestsHandle(IntPtr handle) : base(handle, true) { }

protected override bool ReleaseHandle()
{
NativeMethods.cb4u_att_requests_release(handle);
AnyObject.Release(handle);
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Runtime.InteropServices;
using CoreBluetooth.Foundation;

namespace CoreBluetooth
{
Expand All @@ -11,7 +12,7 @@ internal SafeNativeCentralHandle(IntPtr handle) : base(handle, true) { }

protected override bool ReleaseHandle()
{
NativeMethods.cb4u_central_release(handle);
AnyObject.Release(handle);
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using CoreBluetooth.Foundation;
using Microsoft.Win32.SafeHandles;

namespace CoreBluetooth
Expand All @@ -24,7 +25,7 @@ public static SafeNativeCentralManagerHandle Create()

protected override bool ReleaseHandle()
{
NativeMethods.cb4u_central_manager_release(handle);
AnyObject.Release(handle);
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using CoreBluetooth.Foundation;
using Microsoft.Win32.SafeHandles;

namespace CoreBluetooth
Expand All @@ -23,7 +24,7 @@ CBAttributePermissions permissions

protected override bool ReleaseHandle()
{
NativeMethods.cb4u_mutable_characteristic_release(handle);
AnyObject.Release(handle);
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using CoreBluetooth.Foundation;
using Microsoft.Win32.SafeHandles;

namespace CoreBluetooth
Expand All @@ -13,7 +14,7 @@ internal static SafeNativeMutableServiceHandle Create(string uuid, bool isPrimar

protected override bool ReleaseHandle()
{
NativeMethods.cb4u_mutable_service_release(handle);
AnyObject.Release(handle);
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Runtime.InteropServices;
using CoreBluetooth.Foundation;

namespace CoreBluetooth
{
Expand All @@ -13,7 +14,7 @@ internal SafeNativePeripheralHandle(IntPtr handle) : base(handle, true)

protected override bool ReleaseHandle()
{
NativeMethods.cb4u_peripheral_release(handle);
AnyObject.Release(handle);
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using CoreBluetooth.Foundation;
using Microsoft.Win32.SafeHandles;

namespace CoreBluetooth
Expand All @@ -24,7 +25,7 @@ internal static SafeNativePeripheralManagerHandle Create()

protected override bool ReleaseHandle()
{
NativeMethods.cb4u_peripheral_manager_release(handle);
AnyObject.Release(handle);
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import CoreBluetooth

@_cdecl("cb4u_central_release")
public func cb4u_central_release(_ centralPtr: UnsafeRawPointer) {
Unmanaged<CB4UCentral>.fromOpaque(centralPtr).release()
}

@_cdecl("cb4u_central_identifier")
public func cb4u_central_identifier(_ centralPtr: UnsafeRawPointer, _ identifier: UnsafeMutablePointer<CChar>, _ identifierSize: Int32) {
let instance = Unmanaged<CB4UCentral>.fromOpaque(centralPtr).takeUnretainedValue()
Expand Down Expand Up @@ -38,11 +33,6 @@ public func cb4u_central_manager_new(_ optionsPtr: UnsafeRawPointer?) -> UnsafeM
return Unmanaged.passRetained(CB4UCentralManager(options)).toOpaque()
}

@_cdecl("cb4u_central_manager_release")
public func cb4u_central_manager_release(_ centralManagerPtr: UnsafeRawPointer) {
Unmanaged<CB4UCentralManager>.fromOpaque(centralManagerPtr).release()
}

public typealias CB4UCentralManagerDidConnectHandler = @convention(c) (UnsafeRawPointer, UnsafePointer<CChar>) -> Void
public typealias CB4UCentralManagerDidDisconnectPeripheralHandler = @convention(c) (UnsafeRawPointer, UnsafePointer<CChar>, Int32) -> Void
public typealias CB4UCentralManagerDidFailToConnectHandler = @convention(c) (UnsafeRawPointer, UnsafePointer<CChar>, Int32) -> Void
Expand Down Expand Up @@ -133,11 +123,6 @@ public func cb4u_central_manager_is_scanning(_ centralPtr: UnsafeRawPointer) ->
return instance.isScanning
}

@_cdecl("cb4u_peripheral_release")
public func cb4u_peripheral_release(_ peripheralPtr: UnsafeRawPointer) {
Unmanaged<CB4UPeripheral>.fromOpaque(peripheralPtr).release()
}

public typealias CB4UPeripheralDidDiscoverServicesHandler = @convention(c) (UnsafeRawPointer, UnsafePointer<CChar>, Int32) -> Void
public typealias CB4UPeripheralDidDiscoverCharacteristicsHandler = @convention(c) (UnsafeRawPointer, UnsafePointer<CChar>, UnsafePointer<CChar>, Int32) -> Void
public typealias CB4UPeripheralDidUpdateValueForCharacteristicHandler = @convention(c) (UnsafeRawPointer, UnsafePointer<CChar>, UnsafePointer<CChar>, UnsafePointer<UInt8>, Int32, Int32) -> Void
Expand Down Expand Up @@ -328,15 +313,10 @@ public func cb4u_peripheral_manager_new(_ optionsPtr: UnsafeRawPointer?) -> Unsa
let nsMutableDictionary = Unmanaged<NSMutableDictionary>.fromOpaque(optionsPtr).takeUnretainedValue()
options = nsMutableDictionary as? [String: Any]
}

return Unmanaged.passRetained(CB4UPeripheralManager(options)).toOpaque()
}

@_cdecl("cb4u_peripheral_manager_release")
public func cb4u_peripheral_manager_release(_ peripheralManagerPtr: UnsafeRawPointer) {
Unmanaged<CB4UPeripheralManager>.fromOpaque(peripheralManagerPtr).release()
}

public typealias CB4UPeripheralManagerDidUpdateStateHandler = @convention(c) (UnsafeRawPointer, Int32) -> Void
public typealias CB4UPeripheralManagerDidAddServiceHandler = @convention(c) (UnsafeRawPointer, UnsafePointer<CChar>, Int32) -> Void
public typealias CB4UPeripheralManagerDidStartAdvertisingHandler = @convention(c) (UnsafeRawPointer, Int32) -> Void
Expand Down Expand Up @@ -457,11 +437,6 @@ public func cb4u_mutable_service_new(_ serviceUUID: UnsafePointer<CChar>, _ prim
return Unmanaged.passRetained(CB4UMutableService(service: service)).toOpaque()
}

@_cdecl("cb4u_mutable_service_release")
public func cb4u_mutable_service_release(_ servicePtr: UnsafeRawPointer) {
Unmanaged<CB4UMutableService>.fromOpaque(servicePtr).release()
}

@_cdecl("cb4u_mutable_service_set_characteristics")
public func cb4u_mutable_service_set_characteristics(_ servicePtr: UnsafeRawPointer, _ characteristicsPtr: UnsafePointer<UnsafeRawPointer>?, _ characteristicsCount: Int32) {
let service = Unmanaged<CB4UMutableService>.fromOpaque(servicePtr).takeUnretainedValue()
Expand Down Expand Up @@ -495,11 +470,6 @@ public func cb4u_mutable_characteristic_new(
return Unmanaged.passRetained(CB4UMutableCharacteristic(characteristic: characteristic)).toOpaque()
}

@_cdecl("cb4u_mutable_characteristic_release")
public func cb4u_mutable_characteristic_release(_ characteristicPtr: UnsafeRawPointer) {
Unmanaged<CB4UMutableCharacteristic>.fromOpaque(characteristicPtr).release()
}

@_cdecl("cb4u_mutable_characteristic_value_length")
public func cb4u_mutable_characteristic_value_length(_ characteristicPtr: UnsafeRawPointer) -> Int32 {
let instance = Unmanaged<CB4UMutableCharacteristic>.fromOpaque(characteristicPtr).takeUnretainedValue()
Expand Down Expand Up @@ -561,11 +531,6 @@ public func cb4u_mutable_characteristic_set_permissions(_ characteristicPtr: Uns
instance.permissions = CBAttributePermissions(rawValue: UInt(permissions))
}

@_cdecl("cb4u_att_request_release")
public func cb4u_att_request_release(_ requestPtr: UnsafeRawPointer) {
Unmanaged<CB4UATTRequest>.fromOpaque(requestPtr).release()
}

@_cdecl("cb4u_att_request_central")
public func cb4u_att_request_central(_ requestPtr: UnsafeRawPointer) -> UnsafeMutableRawPointer {
let instance = Unmanaged<CB4UATTRequest>.fromOpaque(requestPtr).takeUnretainedValue()
Expand Down Expand Up @@ -646,11 +611,6 @@ public func cb4u_att_request_offset(_ requestPtr: UnsafeRawPointer) -> Int32 {
return Int32(instance.offset)
}

@_cdecl("cb4u_att_requests_release")
public func cb4u_att_requests_release(_ requestsPtr: UnsafeRawPointer) {
Unmanaged<CB4UATTRequests>.fromOpaque(requestsPtr).release()
}

@_cdecl("cb4u_att_requests_count")
public func cb4u_att_requests_count(_ requestsPtr: UnsafeRawPointer) -> Int32 {
let instance = Unmanaged<CB4UATTRequests>.fromOpaque(requestsPtr).takeUnretainedValue()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import Foundation

@_cdecl("ns_object_release")
public func ns_object_release(_ handle: UnsafeRawPointer) {
Unmanaged<NSObject>.fromOpaque(handle).release()
@_cdecl("any_object_release")
public func any_object_release(_ handle: UnsafeRawPointer) {
Unmanaged<AnyObject>.fromOpaque(handle).release()
}

@_cdecl("ns_number_new_bool")
Expand Down Expand Up @@ -85,19 +85,19 @@ public func ns_mutable_dictionary_new() -> UnsafeMutableRawPointer {
@_cdecl("ns_mutable_dictionary_get_value")
public func ns_mutable_dictionary_get_value(_ handle: UnsafeRawPointer, _ keyPtr: UnsafeRawPointer) -> UnsafeMutableRawPointer? {
let instance = Unmanaged<NSMutableDictionary>.fromOpaque(handle).takeUnretainedValue()

let key = Unmanaged<NSObject>.fromOpaque(keyPtr).takeUnretainedValue()
if let value = instance[key] as? NSObject {
return Unmanaged.passRetained(value).toOpaque()
}

return nil
}

@_cdecl("ns_mutable_dictionary_set_value")
public func ns_mutable_dictionary_set_value(_ handle: UnsafeRawPointer, _ keyPtr: UnsafeRawPointer, _ valuePtr: UnsafeRawPointer?) {
let instance = Unmanaged<NSMutableDictionary>.fromOpaque(handle).takeUnretainedValue()

let key = Unmanaged<NSObject>.fromOpaque(keyPtr).takeUnretainedValue()
if let valuePtr = valuePtr {
let value = Unmanaged<NSObject>.fromOpaque(valuePtr).takeUnretainedValue()
Expand Down