Ejemplo n.º 1
0
        public HidDeviceWrap Read(Stream input, int reportId = -1)
        {
            if (!_ready || !Opened || input == null)
            {
                return(this);
            }
            while (Receiver.TryRead(_rawInputBytes, 0, out var report))
            {
                int offset = InputReportOffsets[report.ReportID];
                Buffer.BlockCopy(_rawInputBytes, 0, _inputBytes, offset, report.Length);
            }

            if (reportId < 0)
            {
                input.SetLength(InputLength);
                input.Position = 0;
                input.Write(_inputBytes, 0, InputLength);
            }
            else if (InputReportOffsets.ContainsKey(reportId))
            {
                input.SetLength(MaxInputReportLength);
                input.Position = 0;
                input.Write(_inputBytes, InputReportOffsets[reportId], MaxInputReportLength);
            }
            else
            {
                input.SetLength(0);
            }
            return(this);
        }
Ejemplo n.º 2
0
        public HidDeviceWrap Initialize()
        {
            Error  = null;
            Opened = false;
            _ready = false;

            DevicePath = Device.DevicePath;
            Change();

            Task.Run(() =>
            {
                Opening = true;
                try
                {
                    //Stream = Device.Open();

                    Descriptor = Device.GetReportDescriptor();

                    MaxInputReportLength = Device.GetMaxInputReportLength();
                    InputReportCount     = 0;
                    InputReportOffsets.Clear();
                    foreach (var report in Descriptor.InputReports)
                    {
                        InputReportOffsets.Add(report.ReportID, InputReportCount * MaxInputReportLength);
                        InputReportCount++;
                    }
                    InputLength  = MaxInputReportLength * InputReportCount;
                    OutputLength = Device.GetMaxOutputReportLength();

                    _inputBytes    = new byte[InputLength];
                    _rawInputBytes = new byte[MaxInputReportLength];
                    _outputBytes   = new byte[OutputLength];

                    Receiver = Descriptor.CreateHidDeviceInputReceiver();

                    var options = new OpenConfiguration();
                    options.SetOption(OpenOption.Exclusive, false);
                    options.SetOption(OpenOption.Transient, false);
                    options.SetOption(OpenOption.Interruptible, false);
                    options.SetOption(OpenOption.Priority, OpenPriority.High);

                    Stream = Device.Open(options);

                    Receiver.Start(Stream);
                    Receiver.Received += (sender, args) =>
                    {
                        Opened = true;
                        _ready = true;
                        Change();
                    };

                    Opened = true;
                }
                catch (Exception e)
                {
                    Opened = false;
                    Error  = e;
                }
                Opening = false;
                Change();
                _ready = Opened;
            });
            return(this);
        }