static bool TryParseReportDescriptor(IntPtr device, out ReportDescriptors.Parser.ReportDescriptorParser parser, out byte[] reportDescriptor)
        {
            parser           = null;
            reportDescriptor = null;
            string devnode = NativeMethods.udev_device_get_devnode(device);

            if (null == devnode)
            {
                return(false);
            }

            int handle = NativeMethods.retry(() => NativeMethods.open
                                                 (devnode, NativeMethods.oflag.NONBLOCK));

            if (handle < 0)
            {
                return(false);
            }

            try {
                uint descsize;
                if (NativeMethods.ioctl(handle, NativeMethods.HIDIOCGRDESCSIZE, out descsize) < 0)
                {
                    return(false);
                }
                if (descsize > NativeMethods.HID_MAX_DESCRIPTOR_SIZE)
                {
                    return(false);
                }

                var desc = new NativeMethods.hidraw_report_descriptor()
                {
                    size = descsize
                };
                if (NativeMethods.ioctl(handle, NativeMethods.HIDIOCGRDESC, ref desc) < 0)
                {
                    return(false);
                }

                Array.Resize(ref desc.value, (int)descsize);
                parser = new ReportDescriptors.Parser.ReportDescriptorParser();
                parser.Parse(desc.value);
                reportDescriptor = desc.value;
                return(true);
            } catch (Exception e) {
                Debug.Print("{0}", e);
                throw;
            } finally {
                NativeMethods.retry(() => NativeMethods.close(handle));
            }
        }
Esempio n. 2
0
        bool TryParseReportDescriptor(out Reports.ReportDescriptor parser, out byte[] reportDescriptor)
        {
            parser = null; reportDescriptor = null;

            int handle;

            try { handle = LinuxHidStream.DeviceHandleFromPath(_path, this, NativeMethods.oflag.NONBLOCK); }
            catch (FileNotFoundException) { throw DeviceException.CreateIOException(this, "Failed to read report descriptor."); }

            try
            {
                uint descsize;
                if (NativeMethods.ioctl(handle, NativeMethods.HIDIOCGRDESCSIZE, out descsize) < 0)
                {
                    return(false);
                }
                if (descsize > NativeMethods.HID_MAX_DESCRIPTOR_SIZE)
                {
                    return(false);
                }

                var desc = new NativeMethods.hidraw_report_descriptor()
                {
                    size = descsize
                };
                if (NativeMethods.ioctl(handle, NativeMethods.HIDIOCGRDESC, ref desc) < 0)
                {
                    return(false);
                }

                Array.Resize(ref desc.value, (int)descsize);
                parser           = new Reports.ReportDescriptor(desc.value);
                reportDescriptor = desc.value; return(true);
            }
            finally
            {
                NativeMethods.retry(() => NativeMethods.close(handle));
            }
        }