示例#1
0
            /// <summary>
            /// reads an Input report from the device using a control transfer.
            /// </summary>
            /// <param name="readHandle">the handle for reading from the device.</param>
            /// <param name="hidHandle">the handle for other device communications.</param>
            /// <param name="myDeviceDetected">tells whether the device is currently attached.</param>
            /// <param name="inputReportBuffer">contains the requested report.</param>
            /// <param name="success">read success</param>
            protected override void ProtectedRead(int readHandle, int hidHandle, ref bool myDeviceDetected,
                                                  ref byte[] inputReportBuffer, ref bool success)
            {
                try
                {
                    // ***
                    // API function: HidD_GetInputReport
                    // Purpose: Attempts to read an Input report from the device using a control transfer.
                    // Supported under Windows XP and later only.
                    // Requires:
                    // A handle to a HID
                    // A pointer to a buffer containing the report ID and report
                    // The size of the buffer.
                    // Returns: true on success, false on failure.
                    // ***

                    success =
                        HidApiDeclarations.HidD_GetInputReport(hidHandle, ref inputReportBuffer[0], inputReportBuffer.Length);

                    if (Settings.Instance.ExtensiveLogging)
                    {
                        Log.Debug(Debugging.ResultOfAPICall("ReadFile"));
                    }
                }
                catch (Exception ex)
                {
                    HandleException(ModuleName + ":" + MethodBase.GetCurrentMethod(), ex);
                }
            }
示例#2
0
        /// <summary>
        /// Remove any Input reports waiting in the buffer.
        /// </summary>
        /// <param name="hidHandle">a handle to a device.</param>
        /// <returns>True on success, False on failure.</returns>
        internal static bool FlushQueue(int hidHandle)
        {
            bool Result = false;

            try
            {
                // ***
                // API function: HidD_FlushQueue
                // Purpose: Removes any Input reports waiting in the buffer.
                // Accepts: a handle to the device.
                // Returns: True on success, False on failure.
                // ***
                Result = HidApiDeclarations.HidD_FlushQueue(hidHandle);

                if (Settings.Instance.ExtensiveLogging)
                {
                    Log.Debug(Debugging.ResultOfAPICall("HidD_FlushQueue, ReadHandle"));
                    Log.Debug("Result = " + Result);
                }
            }
            catch (Exception ex)
            {
                HandleException(ModuleName + ":" + MethodBase.GetCurrentMethod(), ex);
            }

            return(Result);
        }
示例#3
0
            /// <summary>
            /// writes an Output report to the device using a control transfer.
            /// </summary>
            /// <param name="hidHandle">a handle to the device.</param>
            /// <param name="outputReportBuffer">contains the report ID and report to send.</param>
            /// <returns>True on success. False on failure.</returns>
            protected override bool ProtectedWrite(int hidHandle, byte[] outputReportBuffer)
            {
                bool Success = false;

                try
                {
                    // ***
                    // API function: HidD_SetOutputReport
                    // Purpose:
                    // Attempts to send an Output report to the device using a control transfer.
                    // Requires Windows XP or later.
                    // Accepts:
                    // A handle to a HID
                    // A pointer to a buffer containing the report ID and report
                    // The size of the buffer.
                    // Returns: true on success, false on failure.
                    // ***
                    Success =
                        HidApiDeclarations.HidD_SetOutputReport(hidHandle, ref outputReportBuffer[0], outputReportBuffer.Length);
                    if (Settings.Instance.ExtensiveLogging)
                    {
                        Log.Debug(Debugging.ResultOfAPICall("Hidd_SetFeature"));
                        Log.Debug("");
                        Log.Debug(" OutputReportByteLength = " + outputReportBuffer.Length);
                        Log.Debug(" Report ID: " + outputReportBuffer[0]);
                        Log.Debug(" Report Data:");

                        for (int i = 0; i < outputReportBuffer.Length; i++)
                        {
                            Log.Debug(" " + outputReportBuffer[i].ToString("x"));
                        }
                    }
                }
                catch (Exception ex)
                {
                    HandleException(ModuleName + ":" + MethodBase.GetCurrentMethod(), ex);
                }

                return(Success);
            }
示例#4
0
        /// <summary>
        /// Uses a series of API calls to locate a HID-class device
        /// by its Vendor ID and Product ID.
        /// </summary>
        /// <returns>True if the device is detected, False if not detected.</returns>
        private bool FindTheHid()
        {
            string[] DevicePathName = new string[128];
            FileIOApiDeclarations.SECURITY_ATTRIBUTES Security = new FileIOApiDeclarations.SECURITY_ATTRIBUTES();

            try
            {
                if (Settings.Instance.ExtensiveLogging)
                {
                    Log.Debug("MiniDisplay.VFD_Control: Searching for display with VendorID:{0:X} & ProductID:{1:X}", _VendorID,
                              _ProductID);
                }

                Guid HidGuid = Guid.Empty;
                _MyDeviceDetected = false;

                // Values for the SECURITY_ATTRIBUTES structure:
                Security.lpSecurityDescriptor = 0;
                Security.bInheritHandle       = Convert.ToInt32(true);
                Security.nLength = Marshal.SizeOf(Security);


                /*
                 *        API function: 'HidD_GetHidGuid
                 *        Purpose: Retrieves the interface class GUID for the HID class.
                 *        Accepts: 'A System.Guid object for storing the GUID.
                 */

                HidApiDeclarations.HidD_GetHidGuid(ref HidGuid);
                if (Settings.Instance.ExtensiveLogging)
                {
                    Log.Debug("MiniDisplay.VFD_Control: " + Debugging.ResultOfAPICall("GetHidGuid"));
                }

                // Display the GUID.
                string GUIDString = HidGuid.ToString();
                if (Settings.Instance.ExtensiveLogging)
                {
                    Log.Debug("MiniDisplay.VFD_Control: GUID for system HIDs: " + GUIDString);
                }

                // Fill an array with the device path names of all attached HIDs.
                bool DeviceFound = DeviceManagement.FindDeviceFromGuid(HidGuid, ref DevicePathName);

                // If there is at least one HID, attempt to read the Vendor ID and Product ID
                // of each device until there is a match or all devices have been examined.

                if (DeviceFound)
                {
                    int MemberIndex = 0;
                    do
                    {
                        // ***
                        // API function:
                        // CreateFile
                        // Purpose:
                        // Retrieves a handle to a device.
                        // Accepts:
                        // A device path name returned by SetupDiGetDeviceInterfaceDetail
                        // The type of access requested (read/write).
                        // FILE_SHARE attributes to allow other processes to access the device while this handle is open.
                        // A Security structure. Using Null for this may cause problems under Windows XP.
                        // A creation disposition value. Use OPEN_EXISTING for devices.
                        // Flags and attributes for files. Not used for devices.
                        // Handle to a template file. Not used.
                        // Returns: a handle that enables reading and writing to the device.
                        // ***

                        _HIDHandle = FileIOApiDeclarations.CreateFile
                                         (DevicePathName[MemberIndex],
                                         FileIOApiDeclarations.GENERIC_READ | FileIOApiDeclarations.GENERIC_WRITE,
                                         FileIOApiDeclarations.FILE_SHARE_READ | FileIOApiDeclarations.FILE_SHARE_WRITE,
                                         ref Security,
                                         FileIOApiDeclarations.OPEN_EXISTING, 0, 0);

                        if (Settings.Instance.ExtensiveLogging)
                        {
                            Log.Debug("MiniDisplay.VFD_Control: " + Debugging.ResultOfAPICall("CreateFile"));
                        }
                        if (Settings.Instance.ExtensiveLogging)
                        {
                            Log.Debug("MiniDisplay.VFD_Control: Returned handle: " + _HIDHandle.ToString("x") + "h");
                        }

                        if (_HIDHandle != FileIOApiDeclarations.INVALID_HANDLE_VALUE)
                        {
                            // The returned handle is valid,
                            // so find out if this is the device we're looking for.

                            // Set the Size property of DeviceAttributes to the number of bytes in the structure.
                            _MyHID.DeviceAttributes.Size = Marshal.SizeOf(_MyHID.DeviceAttributes);

                            // ***
                            // API function:
                            // HidD_GetAttributes
                            // Purpose:
                            // Retrieves a HIDD_ATTRIBUTES structure containing the Vendor ID,
                            // Product ID, and Product Version Number for a device.
                            // Accepts:
                            // A handle returned by CreateFile.
                            // A pointer to receive a HIDD_ATTRIBUTES structure.
                            // Returns:
                            // True on success, False on failure.
                            // ***

                            int Result = HidApiDeclarations.HidD_GetAttributes(_HIDHandle, ref _MyHID.DeviceAttributes);


                            if (Settings.Instance.ExtensiveLogging)
                            {
                                Log.Debug("MiniDisplay.VFD_Control: " + Debugging.ResultOfAPICall("HidD_GetAttributes"));
                            }

                            if (Result != 0)
                            {
                                if (Settings.Instance.ExtensiveLogging)
                                {
                                    Log.Debug("MiniDisplay.VFD_Control: HIDD_ATTRIBUTES structure filled without error.");
                                }

                                if (Settings.Instance.ExtensiveLogging)
                                {
                                    Log.Debug(
                                        "MiniDisplay.VFD_Control: Vendor ID: {0:X}, Product ID: {1:X}, Version {2:X}" +
                                        _MyHID.DeviceAttributes.VendorID, _MyHID.DeviceAttributes.ProductID,
                                        _MyHID.DeviceAttributes.VersionNumber);
                                }

                                // Find out if the device matches the one we're looking for.
                                if ((_MyHID.DeviceAttributes.VendorID == _VendorID) &
                                    (_MyHID.DeviceAttributes.ProductID == _ProductID))
                                {
                                    // It's the desired device.
                                    if (Settings.Instance.ExtensiveLogging)
                                    {
                                        Log.Debug("MiniDisplay.VFD_Control: My device detected");
                                    }

                                    _MyDeviceDetected = true;

                                    // Save the DevicePathName so OnDeviceChange() knows which name is my device.
                                }
                                else
                                {
                                    // It's not a match, so close the handle.
                                    _MyDeviceDetected = false;

                                    FileIOApiDeclarations.CloseHandle(_HIDHandle);

                                    if (Settings.Instance.ExtensiveLogging)
                                    {
                                        Log.Debug("MiniDisplay.VFD_Control: " + Debugging.ResultOfAPICall("CloseHandle"));
                                    }
                                }
                            }
                            else
                            {
                                // There was a problem in retrieving the information.
                                if (Settings.Instance.ExtensiveLogging)
                                {
                                    Log.Debug("MiniDisplay.VFD_Control: Error in filling HIDD_ATTRIBUTES structure.");
                                }
                                _MyDeviceDetected = false;
                                FileIOApiDeclarations.CloseHandle(_HIDHandle);
                            }
                        }

                        // Keep looking until we find the device or there are no more left to examine.
                        MemberIndex = MemberIndex + 1;
                    } while (!(_MyDeviceDetected || (MemberIndex == DevicePathName.Length) || DevicePathName[MemberIndex] == null));
                }

                if (_MyDeviceDetected)
                {
                    // The device was detected.
                    // Learn the capabilities of the device.
                    _MyHID.Capabilities = _MyHID.GetDeviceCapabilities(_HIDHandle);
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                //HandleException(this.Name + ":" + System.Reflection.MethodBase.GetCurrentMethod(), ex);
            }
            return(_MyDeviceDetected);
        }
示例#5
0
        /// <summary>
        /// Retrieves a structure with information about a device's capabilities.
        /// </summary>
        /// <param name="hidHandle">a handle to a device.</param>
        /// <returns>An HIDP_CAPS structure.</returns>
        internal HidApiDeclarations.HIDP_CAPS GetDeviceCapabilities(int hidHandle)
        {
            byte[] PreparsedDataBytes   = new byte[30];
            IntPtr PreparsedDataPointer = new IntPtr();

            byte[] ValueCaps = new byte[1024]; // (the array size is a guess)

            try
            {
                // ***
                // API function: HidD_GetPreparsedData
                // Purpose: retrieves a pointer to a buffer containing information about the device's capabilities.
                // HidP_GetCaps and other API functions require a pointer to the buffer.
                // Requires:
                // A handle returned by CreateFile.
                // A pointer to a buffer.
                // Returns:
                // True on success, False on failure.
                // ***
                HidApiDeclarations.HidD_GetPreparsedData(hidHandle, ref PreparsedDataPointer);

                if (Settings.Instance.ExtensiveLogging)
                {
                    Log.Debug(Debugging.ResultOfAPICall("HidD_GetPreparsedData"));
                    Log.Debug("");
                }

                // Copy the data at PreparsedDataPointer into a byte array.
                string PreparsedDataString = Convert.ToBase64String(PreparsedDataBytes);


                // ***
                // API function: HidP_GetCaps
                // Purpose: find out a device's capabilities.
                // For standard devices such as joysticks, you can find out the specific
                // capabilities of the device.
                // For a custom device where the software knows what the device is capable of,
                // this call may be unneeded.
                // Accepts:
                // A pointer returned by HidD_GetPreparsedData
                // A pointer to a HIDP_CAPS structure.
                // Returns: True on success, False on failure.
                // ***
                int Result = HidApiDeclarations.HidP_GetCaps(PreparsedDataPointer, ref Capabilities);
                if (Result != 0)
                {
                    //  Debug data:
                    if (Settings.Instance.ExtensiveLogging)
                    {
                        Log.Debug(Debugging.ResultOfAPICall("HidP_GetCaps"));
                        Log.Debug("Data String: " + PreparsedDataString);
                        Log.Debug("  Usage: " + Capabilities.Usage.ToString("x"));
                        Log.Debug("  Usage Page: " + Capabilities.UsagePage.ToString("x"));
                        Log.Debug("  Input Report Byte Length: " + Capabilities.InputReportByteLength);
                        Log.Debug("  Output Report Byte Length: " + Capabilities.OutputReportByteLength);
                        Log.Debug("  Feature Report Byte Length: " + Capabilities.FeatureReportByteLength);
                        Log.Debug("  Number of Link Collection Nodes: " + Capabilities.NumberLinkCollectionNodes);
                        Log.Debug("  Number of Input Button Caps: " + Capabilities.NumberInputButtonCaps);
                        Log.Debug("  Number of Input Value Caps: " + Capabilities.NumberInputValueCaps);
                        Log.Debug("  Number of Input Data Indices: " + Capabilities.NumberInputDataIndices);
                        Log.Debug("  Number of Output Button Caps: " + Capabilities.NumberOutputButtonCaps);
                        Log.Debug("  Number of Output Value Caps: " + Capabilities.NumberOutputValueCaps);
                        Log.Debug("  Number of Output Data Indices: " + Capabilities.NumberOutputDataIndices);
                        Log.Debug("  Number of Feature Button Caps: " + Capabilities.NumberFeatureButtonCaps);
                        Log.Debug("  Number of Feature Value Caps: " + Capabilities.NumberFeatureValueCaps);
                        Log.Debug("  Number of Feature Data Indices: " + Capabilities.NumberFeatureDataIndices);
                    }
                    // ***
                    // API function: HidP_GetValueCaps
                    // Purpose: retrieves a buffer containing an array of HidP_ValueCaps structures.
                    // Each structure defines the capabilities of one value.
                    // This application doesn't use this data.
                    // Accepts:
                    // A report type enumerator from hidpi.h,
                    // A pointer to a buffer for the returned array,
                    // The NumberInputValueCaps member of the device's HidP_Caps structure,
                    // A pointer to the PreparsedData structure returned by HidD_GetPreparsedData.
                    // Returns: True on success, False on failure.
                    // ***
                    HidApiDeclarations.HidP_GetValueCaps(HidApiDeclarations.HidP_Input, ref ValueCaps[0],
                                                         ref Capabilities.NumberInputValueCaps, PreparsedDataPointer);

                    if (Settings.Instance.ExtensiveLogging)
                    {
                        Log.Debug(Debugging.ResultOfAPICall("HidP_GetValueCaps"));
                    }

                    // (To use this data, copy the ValueCaps byte array into an array of structures.)
                    // ***
                    // API function: HidD_FreePreparsedData
                    // Purpose: frees the buffer reserved by HidD_GetPreparsedData.
                    // Accepts: A pointer to the PreparsedData structure returned by HidD_GetPreparsedData.
                    // Returns: True on success, False on failure.
                    // ***
                    HidApiDeclarations.HidD_FreePreparsedData(ref PreparsedDataPointer);

                    if (Settings.Instance.ExtensiveLogging)
                    {
                        Log.Debug(Debugging.ResultOfAPICall("HidD_FreePreparsedData"));
                    }
                }
            }
            catch (Exception ex)
            {
                HandleException(ModuleName + ":" + MethodBase.GetCurrentMethod(), ex);
            }

            return(Capabilities);
        }