/// <summary> /// Open the IR device and begin reading. /// </summary> protected void CaptureFromDevice() { if (!System.Threading.Monitor.TryEnter(_capturingLocker)) { throw new InvalidOperationException("Capture already in progress."); } try { OnBeforeRawCapture(); if (string.IsNullOrWhiteSpace(CaptureDevice)) { throw new ArgumentNullException(nameof(CaptureDevice), $"The capture device must be set."); } using (var irDevice = _fileSystem.OpenRead(CaptureDevice)) { DeviceFeatures deviceFeatures = _utility.GetFeatures(irDevice); CheckDevice(irDevice, deviceFeatures); if (TimeoutMicrosecs >= 0) { _utility.SetRxTimeout(irDevice, TimeoutMicrosecs); } byte[] buffer = new byte[4]; while (true) { int readCount = irDevice.Stream.Read(buffer, 0, buffer.Length); // Note if "modprobe -r gpio_ir_recv" is used to unload the driver while this is reading then we get an IOException. if (readCount != 4) { throw new System.IO.IOException($"Read {readCount} bytes instead of the expected 4."); } Mode2PacketType packetType = (Mode2PacketType)buffer[3]; buffer[3] = 0; Lazy <int> number = new Lazy <int>(() => BitConverter.ToInt32(buffer)); if (!OnReceivePacket(packetType, number)) { return; } } } } finally { System.Threading.Monitor.Exit(_capturingLocker); } }
/// <summary> /// Assess a single IR device. /// </summary> /// <param name="path">Example: /dev/lirc0</param> /// <exception cref="System.IO.FileNotFoundException"></exception> /// <exception cref="System.IO.DirectoryNotFoundException"></exception> /// <exception cref="NotAnIRDeviceException"></exception> /// <exception cref="UnauthorizedAccessException"></exception> public AssessmentResult AssessDevice(string path) { string fullPath = _fileSystem.GetFullPath(path); using (var irDevice = _fileSystem.OpenRead(fullPath)) { DeviceFeatures features = _utility.GetFeatures(irDevice); /// If this is not an IR device then expect a <see cref="NotAnIRDeviceException"/> exception from this. string realPath = _fileSystem.GetRealPath(fullPath); uint?minTimeOut = null; uint?maxTimeOut = null; uint?currentTimeOut = null; if (features.CanReceive()) // Only try this query for receive devices since it is not applicable to transmit devices so is very unlikely to work. { minTimeOut = GetIoCtlValueIfImplemented(irDevice, LircConstants.LIRC_GET_MIN_TIMEOUT); maxTimeOut = GetIoCtlValueIfImplemented(irDevice, LircConstants.LIRC_GET_MAX_TIMEOUT); currentTimeOut = GetIoCtlValueIfImplemented(irDevice, LircConstants.LIRC_GET_REC_TIMEOUT); } return(new AssessmentResult(fullPath, realPath, features, minTimeOut, maxTimeOut, currentTimeOut)); } }