Example #1
0
        /// <summary>
        /// Enumerates the serial ports.
        /// </summary>
        /// <param name="iterator">An IOKit enumerator object.</param>
        /// <param name="exclusionFilter">A function that will filter the entries in the returned enumerable.</param>
        /// <returns>An enumerable of the serial ports in the system that are not excluded by <paramref name="exclusionFilter"/>.</returns>
        public static IEnumerable <string> EnumerateSerialPorts(this IOIterator iterator, System.Predicate <string> exclusionFilter)
        {
            var ports = new List <string>();

            if ((iterator != null) && iterator.IsValid)
            {
                var validEntry = true;
                do
                {
                    using (var serialPortRegistryEntry = iterator.Next <IORegistryEntry>())
                    {
                        validEntry = serialPortRegistryEntry != null;
                        if (validEntry)
                        {
                            var portName = CreateSerialPortName(serialPortRegistryEntry.GetProperty <NSString>(NativeMethods.kIOTTYDeviceKey));
                            DebugOutput("Discovered serial port: " + portName);
                            if ((exclusionFilter == null) || !exclusionFilter(portName))
                            {
                                ports.Add(portName);
                            }
                        }
                    }
                }while (validEntry);
            }

            return(ports);
        }
Example #2
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);
                }
            }
Example #3
0
        /// <summary>
        /// Gets an iterator to enumerate RS-232 serial services.
        /// </summary>
        /// <returns>The RS-232 serial services iterator.</returns>
        public IOIterator GetRS232SerialServicesIterator()
        {
            IOIterator iterator           = null;
            var        servicesDictionary = GetRS232SerialMatchDictionary();

            if (servicesDictionary != null)
            {
                iterator = new IOIterator(this, servicesDictionary);
            }
            return(iterator);
        }
Example #4
0
        /// <summary>
        /// Gets the USB device services iterator.
        /// </summary>
        /// <param name="vendorId">The numeric value of the vendor ID to match. If zero, this is ignored.</param>
        /// <param name="productId">The numeric value of the product ID to match. If zero, this is ignored.</param>
        /// <returns>The USB device services iterator.</returns>
        public IOIterator GetUSBServicesIterator(int vendorId, int productId)
        {
            IOIterator iterator           = null;
            var        servicesDictionary = GetUSBMatchDictionary(vendorId, productId);

            if (servicesDictionary != null)
            {
                iterator = new IOIterator(this, servicesDictionary);
            }
            return(iterator);
        }
Example #5
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);
                }
            }
 /// <summary>
 /// Initialize a new instance of IONotificationEventArgs.
 /// </summary>
 /// <param name="refcon">Custom data provided when the notification was registered.</param>
 /// <param name="iterator">The data from the IOKit notification system.</param>
 public IONotificationEventArgs(NSObject refcon, IOIterator iterator)
 {
     Refcon   = refcon;
     Iterator = iterator;
 }