Esempio n. 1
0
        public HidDevice(string path)
        {
            //device mutex
            lockObject = new object();

            //static buffers
            strBuffer = new byte[256];

            //open device
            reference = Kernel32.File.Create(path, Kernel32.FileAccess.GenericWrite, Kernel32.FileShare.Read | Kernel32.FileShare.Write, IntPtr.Zero, CreationDisposition.OpenExisting, Kernel32.FileAttributes.Overlapped, IntPtr.Zero);

            if (reference.IsInvalid)
            {
                return;
            }

            //overlapped
            fileEvent      = new ManualResetEvent(false);
            fileOverlapped = new SafeOverlapped(fileEvent);

            //get preparsed data
            IntPtr preparsed;

            if (!NativeMethods.HidD_GetPreparsedData(reference, out preparsed))
            {
                return;
            }

            preparsedData = new PreparsedData(preparsed);

            //transfer buffers
            Caps capabilities = preparsedData.Capabilities;

            inBuffer      = new byte[capabilities.InputReportByteLength];
            outBuffer     = new byte[capabilities.OutputReportByteLength];
            featureBuffer = new byte[capabilities.FeatureReportByteLength];

            //dynamic caps
            inputValueCaps   = new List <ValueCaps>();
            outputValueCaps  = new List <ValueCaps>();
            featureValueCaps = new List <ValueCaps>();

            ushort numInputCaps   = capabilities.NumberInputValueCaps;
            ushort numOutputCaps  = capabilities.NumberOutputValueCaps;
            ushort numFeatureCaps = capabilities.NumberFeatureValueCaps;

            ushort maxValueCaps = Math.Max(numInputCaps, numOutputCaps);

            maxValueCaps = Math.Max(maxValueCaps, numFeatureCaps);

            int    valueCapSize = Marshal.SizeOf <ValueCaps>();
            IntPtr valueCapPtr  = Marshal.AllocHGlobal(maxValueCaps * valueCapSize);

            HidPStatus stat = NativeMethods.HidP_GetValueCaps(ReportType.Input, valueCapPtr, ref numInputCaps, preparsed);

            for (int i = 0; i < numInputCaps; i++)
            {
                ValueCaps val = Marshal.PtrToStructure <ValueCaps>(new IntPtr(valueCapPtr.ToInt64() + (i * valueCapSize)));
                inputValueCaps.Add(val);
            }

            stat = NativeMethods.HidP_GetValueCaps(ReportType.Output, valueCapPtr, ref numOutputCaps, preparsed);

            for (int i = 0; i < numOutputCaps; i++)
            {
                ValueCaps val = Marshal.PtrToStructure <ValueCaps>(new IntPtr(valueCapPtr.ToInt64() + i * valueCapSize));
                outputValueCaps.Add(val);
            }

            stat = NativeMethods.HidP_GetValueCaps(ReportType.Feature, valueCapPtr, ref numFeatureCaps, preparsed);

            for (int i = 0; i < numFeatureCaps; i++)
            {
                ValueCaps val = Marshal.PtrToStructure <ValueCaps>(new IntPtr(valueCapPtr.ToInt64() + i * valueCapSize));
                featureValueCaps.Add(val);
            }

            Marshal.FreeHGlobal(valueCapPtr);
        }
Esempio n. 2
0
        public HidDevice(string path)
        {
            //device mutex
            lockObject = new object();

            //static buffers
            strBuffer = new StringBuilder(256);

            //open device
            //TODO why is 0x40000000 missing from FileAttribute in PInvoke?
            reference = Kernel32.CreateFile(path, Kernel32.ACCESS_MASK.GenericRight.GENERIC_WRITE, Kernel32.FileShare.FILE_SHARE_READ | Kernel32.FileShare.FILE_SHARE_WRITE, IntPtr.Zero, Kernel32.CreationDisposition.OPEN_EXISTING, Kernel32.CreateFileFlags.FILE_FLAG_OVERLAPPED, Kernel32.SafeObjectHandle.Null);

            if (reference.IsInvalid)
            {
                return;
            }

            //overlapped
            fileEvent      = new ManualResetEvent(false);
            fileOverlapped = new SafeOverlapped(fileEvent);

            //get preparsed data
            if (!Hid.HidD_GetPreparsedData(reference, out preparsedData))
            {
                return;
            }

            //transfer buffers
            capabilities  = Hid.HidP_GetCaps(preparsedData);
            inBuffer      = new byte[capabilities.InputReportByteLength];
            outBuffer     = new byte[capabilities.OutputReportByteLength];
            featureBuffer = new byte[capabilities.FeatureReportByteLength];

            //dynamic caps
            inputValueCaps   = new List <ValueCaps>();
            outputValueCaps  = new List <ValueCaps>();
            featureValueCaps = new List <ValueCaps>();

            ushort numInputCaps   = capabilities.NumberInputValueCaps;
            ushort numOutputCaps  = capabilities.NumberOutputValueCaps;
            ushort numFeatureCaps = capabilities.NumberFeatureValueCaps;

            ushort maxValueCaps = Math.Max(numInputCaps, numOutputCaps);

            maxValueCaps = Math.Max(maxValueCaps, numFeatureCaps);

            int    valueCapSize = Marshal.SizeOf <ValueCaps>();
            IntPtr valueCapPtr  = Marshal.AllocHGlobal(maxValueCaps * valueCapSize);

            HidPStatus stat = Hid.HidP_GetValueCaps(ReportType.Input, valueCapPtr, ref numInputCaps, preparsedData);

            for (int i = 0; i < numInputCaps; i++)
            {
                ValueCaps val = Marshal.PtrToStructure <ValueCaps>(new IntPtr(valueCapPtr.ToInt64() + (i * valueCapSize)));
                inputValueCaps.Add(val);
            }

            stat = Hid.HidP_GetValueCaps(ReportType.Output, valueCapPtr, ref numOutputCaps, preparsedData);

            for (int i = 0; i < numOutputCaps; i++)
            {
                ValueCaps val = Marshal.PtrToStructure <ValueCaps>(new IntPtr(valueCapPtr.ToInt64() + i * valueCapSize));
                outputValueCaps.Add(val);
            }

            stat = Hid.HidP_GetValueCaps(ReportType.Feature, valueCapPtr, ref numFeatureCaps, preparsedData);

            for (int i = 0; i < numFeatureCaps; i++)
            {
                ValueCaps val = Marshal.PtrToStructure <ValueCaps>(new IntPtr(valueCapPtr.ToInt64() + i * valueCapSize));
                featureValueCaps.Add(val);
            }

            Marshal.FreeHGlobal(valueCapPtr);
        }