Example #1
0
        ///  <summary>
        ///  Creates a 32-bit Usage from the Usage Page and Usage ID.
        ///  Determines whether the Usage is a system mouse or keyboard.
        ///  Can be modified to detect other Usages.
        ///  </summary>
        ///
        ///  <param name="myCapabilities"> a HIDP_CAPS structure retrieved with HidP_GetCaps. </param>
        ///
        ///  <returns>
        ///  A String describing the Usage.
        ///  </returns>

        internal String GetHidUsage(NativeMethods.HIDP_CAPS myCapabilities)
        {
            String usageDescription = "";

            try
            {
                //  Create32-bit Usage from Usage Page and Usage ID.

                Int32 usage = myCapabilities.UsagePage * 256 + myCapabilities.Usage;

                if (usage == Convert.ToInt32(0X102))
                {
                    usageDescription = "mouse";
                }
                if (usage == Convert.ToInt32(0X106))
                {
                    usageDescription = "keyboard";
                }
            }

            catch (Exception ex)
            {
                DisplayException(ModuleName, ex);
                throw;
            }

            return(usageDescription);
        }
Example #2
0
        ///  <summary>
        ///  Creates a 32-bit Usage from the Usage Page and Usage ID.
        ///  Determines whether the Usage is a system mouse or keyboard.
        ///  Can be modified to detect other Usages.
        ///  </summary>
        ///
        ///  <param name="myCapabilities">A HIDP_CAPS structure retrieved with HidP_GetCaps.</param>
        ///
        ///  <returns>A String describing the Usage.</returns>

        internal String GetHidUsage(NativeMethods.HIDP_CAPS myCapabilities)
        {
            String usageDescription = "";

            traceSource.TraceEvent(TraceEventType.Verbose, 1, "GetHidUsage");

            try
            {
                //  Create32-bit Usage from Usage Page and Usage ID.

                Int32 usage = myCapabilities.UsagePage * 256 + myCapabilities.Usage;

                if (usage == Convert.ToInt32(0X102))
                {
                    usageDescription = "mouse";
                }
                if (usage == Convert.ToInt32(0X106))
                {
                    usageDescription = "keyboard";
                }
            }
            catch (Exception ex)
            {
                DisplayException(MethodBase.GetCurrentMethod().Name, ex, ShowMsgBoxOnException);
            }

            return(usageDescription);
        }
            public byte[] Run(IntPtr preparsed, NativeMethods.HIDP_CAPS caps)
            {
                _builder   = new ReportDescriptorBuilder();
                _preparsed = preparsed;

                _nodes = new NativeMethods.HIDP_LINK_COLLECTION_NODE[caps.NumberLinkCollectionNodes]; int nodeCount = _nodes.Length;
                if (NativeMethods.HidP_GetLinkCollectionNodes(_nodes, ref nodeCount, preparsed) != NativeMethods.HIDP_STATUS_SUCCESS || nodeCount != _nodes.Length)
                {
                    throw new NotImplementedException();
                }

                _types = new ReportCaps[(int)NativeMethods.HIDP_REPORT_TYPE.Count];
                GetReportCaps(NativeMethods.HIDP_REPORT_TYPE.Input, caps.NumberInputButtonCaps, caps.NumberInputValueCaps, caps.InputReportByteLength);
                GetReportCaps(NativeMethods.HIDP_REPORT_TYPE.Output, caps.NumberOutputButtonCaps, caps.NumberOutputValueCaps, caps.OutputReportByteLength);
                GetReportCaps(NativeMethods.HIDP_REPORT_TYPE.Feature, caps.NumberFeatureButtonCaps, caps.NumberFeatureValueCaps, caps.FeatureReportByteLength);

                _currentNodes = new List <ushort>();
                SetCollection(new List <ushort>()
                {
                    0
                });
                EncodeReports(NativeMethods.HIDP_REPORT_TYPE.Input, MainItemTag.Input);
                EncodeReports(NativeMethods.HIDP_REPORT_TYPE.Output, MainItemTag.Output);
                EncodeReports(NativeMethods.HIDP_REPORT_TYPE.Feature, MainItemTag.Feature);
                SetCollection(new List <ushort>());

                return(_builder.GetReportDescriptor());
            }
Example #4
0
 internal HidDeviceCapabilities(NativeMethods.HIDP_CAPS capabilities)
 {
     Usage                     = capabilities.Usage;
     UsagePage                 = capabilities.UsagePage;
     InputReportByteLength     = capabilities.InputReportByteLength;
     OutputReportByteLength    = capabilities.OutputReportByteLength;
     FeatureReportByteLength   = capabilities.FeatureReportByteLength;
     Reserved                  = capabilities.Reserved;
     NumberLinkCollectionNodes = capabilities.NumberLinkCollectionNodes;
     NumberInputButtonCaps     = capabilities.NumberInputButtonCaps;
     NumberInputValueCaps      = capabilities.NumberInputValueCaps;
     NumberInputDataIndices    = capabilities.NumberInputDataIndices;
     NumberOutputButtonCaps    = capabilities.NumberOutputButtonCaps;
     NumberOutputValueCaps     = capabilities.NumberOutputValueCaps;
     NumberOutputDataIndices   = capabilities.NumberOutputDataIndices;
     NumberFeatureButtonCaps   = capabilities.NumberFeatureButtonCaps;
     NumberFeatureValueCaps    = capabilities.NumberFeatureValueCaps;
     NumberFeatureDataIndices  = capabilities.NumberFeatureDataIndices;
 }
Example #5
0
        public void OpenMsr(string DevicePath)
        {
            try
            {
                if (disposed)
                {
                    throw new ObjectDisposedException("USBReadThread");
                }

                // close the device (OK to call if it's already closed)
                CloseMsr();

                // (Re)Open the device
                HidHandle = NativeMethods.CreateFile(
                    DevicePath,
                    NativeMethods.GENERIC_READ | NativeMethods.GENERIC_WRITE,
                    0,                      // No sharing
                    IntPtr.Zero,
                    NativeMethods.OPEN_EXISTING,
                    NativeMethods.FILE_FLAG_OVERLAPPED,
                    IntPtr.Zero);
                if ((int)HidHandle == -1)
                {
                    HidHandle = IntPtr.Zero;
                    Logger.Error(SOName, "Error opening file: " + DevicePath);
                    throw new Win32Exception(Marshal.GetLastWin32Error());
                }


                IntPtr PtrToPreparsedData = IntPtr.Zero;
                if (NativeMethods.HidD_GetPreparsedData(HidHandle, ref PtrToPreparsedData) == (byte)0)
                {
                    Logger.Error(SOName, "Error reading HID preparsed data.");
                    throw new PosControlException(rm.GetString("IDS_DEVICE_COMMUNICATION_FAILURE"), ErrorCode.Failure);
                }

                NativeMethods.HIDP_CAPS DeviceCaps = new NativeMethods.HIDP_CAPS();
                int retVal = NativeMethods.HidP_GetCaps(PtrToPreparsedData, ref DeviceCaps);
                if (retVal < 0)
                {
                    Logger.Error(SOName, "Error reading HID capabilities: " + retVal.ToString(CultureInfo.InvariantCulture));
                    throw new PosControlException(rm.GetString("IDS_DEVICE_COMMUNICATION_FAILURE"), ErrorCode.Failure);
                }

                InputReportByteLength   = DeviceCaps.InputReportByteLength;
                FeatureReportByteLength = DeviceCaps.FeatureReportByteLength;

                Logger.Info(SOName, "InputReportByteLength = " + InputReportByteLength.ToString(CultureInfo.InvariantCulture), PosSettings.Default.Debug);
                Logger.Info(SOName, "FeatureReportByteLength = " + FeatureReportByteLength.ToString(CultureInfo.InvariantCulture), PosSettings.Default.Debug);

                // Reset events
                ThreadTerminating.Reset();
                ThreadStarted.Reset();

                // start thread for getting input
                ReadThread      = new Thread(new ThreadStart(ThreadMethod));
                ReadThread.Name = "Example Msr USB thread";
                ReadThread.Start();

                // If the thread doesn't start in 30 seconds throw an exception
                if (!ThreadStarted.WaitOne(30000, false))
                {
                    Logger.Error(SOName, "USB read thread failed to start in 30 seconds.");
                    throw new PosControlException(rm.GetString("IDS_THREAD_FAILED_TO_START"), ErrorCode.Failure);
                }

                Logger.Info(SOName, "USB read thread started successfully.", PosSettings.Default.Debug);
            }
            catch (Exception e)
            {
                Logger.Error(SOName, "An exception occurred while attempting to open the device.", e);
                CloseMsr();
                throw;
            }
        }
Example #6
0
            public void StartReading()
            {
                IntPtr PtrToPreparsedData = IntPtr.Zero;

                try
                {
                    // close the device (OK to call if it's already closed)
                    StopReading();


                    lock (syncRoot)
                    {
                        // (Re)Open the device
                        HidHandle = NativeMethods.CreateFile(
                            devicepath,
                            NativeMethods.GENERIC_READ | NativeMethods.GENERIC_WRITE,
                            0,  // No sharing
                            IntPtr.Zero,
                            NativeMethods.OPEN_EXISTING,
                            NativeMethods.FILE_FLAG_OVERLAPPED,
                            IntPtr.Zero);
                        int LastErr = Marshal.GetLastWin32Error();
                        if (HidHandle.IsInvalid)
                        {
                            Logger.Error(devicename, "Error opening path: " + devicepath);
                            throw new Win32Exception(LastErr);
                        }

                        if (NativeMethods.HidD_GetPreparsedData(HidHandle, ref PtrToPreparsedData) == (byte)0)
                        {
                            Logger.Error(devicename, "Error reading HID preparsed data.");
                            throw new PosControlException(rm.GetString("IDS_DEVICE_COMMUNICATION_FAILURE"), ErrorCode.Failure);
                        }
                    }

                    NativeMethods.HIDP_CAPS DeviceCaps = new NativeMethods.HIDP_CAPS();
                    int retVal = NativeMethods.HidP_GetCaps(PtrToPreparsedData, ref DeviceCaps);
                    if (retVal < 0)
                    {
                        Logger.Error(devicename, "Error reading HID capabilities: " + retVal.ToString(CultureInfo.InvariantCulture));
                        throw new PosControlException(rm.GetString("IDS_DEVICE_COMMUNICATION_FAILURE"), ErrorCode.Failure);
                    }

                    InputReportByteLength   = DeviceCaps.InputReportByteLength;
                    FeatureReportByteLength = DeviceCaps.FeatureReportByteLength;

                    Logger.Info(devicename, "InputReportByteLength = " + InputReportByteLength.ToString(CultureInfo.InvariantCulture));
                    Logger.Info(devicename, "FeatureReportByteLength = " + FeatureReportByteLength.ToString(CultureInfo.InvariantCulture));

                    // Reset events
                    ThreadTerminating.Reset();
                    ThreadStarted.Reset();

                    // start thread for getting input
                    ReadThread              = new Thread(new ThreadStart(ThreadMethod));
                    ReadThread.Name         = "HidThread: " + devicename;
                    ReadThread.IsBackground = true;
                    ReadThread.Start();

                    // If the thread doesn't start in 30 seconds throw an exception
                    if (!ThreadStarted.WaitOne(30000, false))
                    {
                        Logger.Error(devicename, "HidThread - thread failed to start in 30 seconds.");
                        throw new PosControlException(rm.GetString("IDS_THREAD_FAILED_TO_START"), ErrorCode.Failure);
                    }

                    Logger.Info(devicename, "HidThread - thread started successfully.");
                }
                catch (Exception e)
                {
                    Logger.Error(devicename, "An exception occurred while attempting to open the device.", e);
                    StopReading();
                    throw;
                }
                finally
                {
                    // Free Preparsed Data
                    if (PtrToPreparsedData != IntPtr.Zero)
                    {
                        NativeMethods.HidD_FreePreparsedData(PtrToPreparsedData);
                    }
                }
            }
Example #7
0
            public void StartReading()
            {
                IntPtr PtrToPreparsedData = IntPtr.Zero;

                try
                {
                    // close the device (OK to call if it's already closed)
                    StopReading();


                    lock (syncRoot)
                    {
                        // (Re)Open the device
                        HidHandle = NativeMethods.CreateFile(
                            devicepath,
                            NativeMethods.GENERIC_READ | NativeMethods.GENERIC_WRITE,
                            0,  // No sharing
                            IntPtr.Zero,
                            NativeMethods.OPEN_EXISTING,
                            NativeMethods.FILE_FLAG_OVERLAPPED,
                            IntPtr.Zero);
                        int LastErr = Marshal.GetLastWin32Error();
                        if (HidHandle.IsInvalid)
                        {
                            Logger.Error(devicename, "Error opening path: " + devicepath);
                            throw new Win32Exception(LastErr);
                        }

                        if (NativeMethods.HidD_GetPreparsedData(HidHandle, ref PtrToPreparsedData) == (byte)0)
                        {
                            Logger.Error(devicename, "Error reading HID preparsed data.");
                            throw new PosControlException(rm.GetString("IDS_DEVICE_COMMUNICATION_FAILURE"), ErrorCode.Failure);
                        }
                    }

                    NativeMethods.HIDP_CAPS DeviceCaps = new NativeMethods.HIDP_CAPS();
                    int retVal = NativeMethods.HidP_GetCaps(PtrToPreparsedData, ref DeviceCaps);
                    if (retVal < 0)
                    {
                        Logger.Error(devicename, "Error reading HID capabilities: " + retVal.ToString(CultureInfo.InvariantCulture));
                        throw new PosControlException(rm.GetString("IDS_DEVICE_COMMUNICATION_FAILURE"), ErrorCode.Failure);
                    }

                    InputReportByteLength = DeviceCaps.InputReportByteLength;
                    FeatureReportByteLength = DeviceCaps.FeatureReportByteLength;

                    Logger.Info(devicename, "InputReportByteLength = " + InputReportByteLength.ToString(CultureInfo.InvariantCulture));
                    Logger.Info(devicename, "FeatureReportByteLength = " + FeatureReportByteLength.ToString(CultureInfo.InvariantCulture));

                    // Reset events
                    ThreadTerminating.Reset();
                    ThreadStarted.Reset();

                    // start thread for getting input
                    ReadThread = new Thread(new ThreadStart(ThreadMethod));
                    ReadThread.Name = "HidThread: " + devicename;
                    ReadThread.IsBackground = true;
                    ReadThread.Start();

                    // If the thread doesn't start in 30 seconds throw an exception
                    if (!ThreadStarted.WaitOne(30000, false))
                    {
                        Logger.Error(devicename, "HidThread - thread failed to start in 30 seconds.");
                        throw new PosControlException(rm.GetString("IDS_THREAD_FAILED_TO_START"), ErrorCode.Failure);
                    }

                    Logger.Info(devicename, "HidThread - thread started successfully.");
                }
                catch (Exception e)
                {
                    Logger.Error(devicename, "An exception occurred while attempting to open the device.", e);
                    StopReading();
                    throw;
                }
                finally
                {
                    // Free Preparsed Data
                    if (PtrToPreparsedData != IntPtr.Zero)
                        NativeMethods.HidD_FreePreparsedData(PtrToPreparsedData);
                }
            }