예제 #1
0
            private static void TerminateNotification(IntPtr refcon, IntPtr iteratorPtr)
            {
                DebugOutput("IOService.IOKitNotificationPort: TerminateNotification");
                var iterator = new IOIterator(iteratorPtr);
                var newPorts = iterator.EnumerateSerialPorts(IOKitHelpers.BluetoothPortsExclusionFilter);

                foreach (var port in newPorts)
                {
                    ReportPortDeparture(port);
                }
            }
예제 #2
0
            /// <inheritdoc/>
            internal override void StartInThread()
            {
                NotificationPort = new IONotificationPort();
                Interop.NativeMethods.CFRunLoopAddSource(CFRunLoop.Current, NotificationPort.RunLoopSource, (NSString)CFRunLoop.ModeDefault);

                var systemPowerDelegate = new IOServiceInterestCallback(SystemPowerInterestCallback);

                IOConnectionPort = IOConnect.CreateSystemPowerMonitorConnection(systemPowerDelegate, this);
                Interop.NativeMethods.CFRunLoopAddSource(CFRunLoop.Current, IOConnectionPort.NotificationPort.RunLoopSource, (NSString)CFRunLoop.ModeCommon);

                var servicesDictionary = IOMachPort.GetRS232SerialMatchDictionary();

#if __UNIFIED__
                servicesDictionary.DangerousRetain(); // retain an extra time because we're using it twice
#else
                servicesDictionary.Retain();          // retain an extra time because we're using it twice
#endif // __UNIFIED__
                var publishDelegate = new IONotificationPortCallback(FirstMatchNotification);
                var callback        = System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegate(publishDelegate);

                IntPtr iterator;
                var    result = NativeMethods.IOServiceAddMatchingNotification(NotificationPort.Handle, KIOFirstMatchNotification, servicesDictionary.Handle, callback, IntPtr.Zero, out iterator);
                System.Diagnostics.Debug.Assert(result == NativeMethods.Success, "IOService.IOKitNotificationPort: Failed to add notification.");

                if (result == NativeMethods.Success)
                {
                    PublishNotificationIterator = new IOIterator(iterator);

                    var terminateDelegate = new IONotificationPortCallback(TerminateNotification);
                    callback = System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegate(terminateDelegate);
                    result   = NativeMethods.IOServiceAddMatchingNotification(NotificationPort.Handle, KIOTerminatedNotification, servicesDictionary.Handle, callback, IntPtr.Zero, out iterator);
                    TerminateNotificationIterator = new IOIterator(iterator);

                    // The iterators returned when adding the matching notifications must be iterated to
                    // completion in order to arm the notifications. Otherwise, they will never fire.
                    PublishNotificationIterator.EnumerateSerialPorts(null);
                    TerminateNotificationIterator.EnumerateSerialPorts(null);
                }
            }