Exemplo n.º 1
0
        protected override void Open()
        {
            string devicePath = FindDevice(_deviceClass);

            if (devicePath == null)
            {
                return;
            }
            if (LogVerbose)
            {
                MceRemoteReceiver.LogInfo("Using {0}", devicePath);
            }

            SafeFileHandle deviceHandle = CreateFile(devicePath, FileAccess.Read, FileShare.ReadWrite, 0, FileMode.Open,
                                                     FileFlag.Overlapped, 0);

            if (deviceHandle.IsInvalid)
            {
                throw new Exception(string.Format("Failed to open remote ({0})", GetLastError()));
            }

            _deviceWatcher.RegisterDeviceRemoval(deviceHandle);

            // open a stream from the device and begin an asynchronous read
            _deviceStream = new FileStream(deviceHandle, FileAccess.Read, 128, true);
            _deviceStream.BeginRead(_deviceBuffer, 0, _deviceBuffer.Length, new AsyncCallback(OnReadComplete), null);
        }
Exemplo n.º 2
0
        internal void RegisterDeviceArrival()
        {
            DeviceBroadcastInterface dbi = new DeviceBroadcastInterface();

            dbi.Size       = Marshal.SizeOf(dbi);
            dbi.DeviceType = 0x5;
            dbi.ClassGuid  = _deviceClass;

            try
            {
                _handleDeviceArrival = new SafeFileHandle(RegisterDeviceNotification(Handle, ref dbi, 0), true);
            }
            catch
            {
                MceRemoteReceiver.LogInfo("DeviceWatcher.RegisterDeviceArrival: Error={0}.", Marshal.GetLastWin32Error());
            }

            if (_handleDeviceArrival.IsInvalid)
            {
                throw new Exception(string.Format("Failed in call to RegisterDeviceNotification ({0})", GetLastError()));
            }
        }
Exemplo n.º 3
0
        internal void RegisterDeviceRemoval(SafeHandle deviceHandle)
        {
            DeviceBroadcastHandle dbh = new DeviceBroadcastHandle();

            dbh.Size       = Marshal.SizeOf(dbh);
            dbh.DeviceType = 0x6;
            dbh.Handle     = deviceHandle.DangerousGetHandle();

            _deviceHandle = deviceHandle;
            try
            {
                _handleDeviceRemoval = new SafeFileHandle(RegisterDeviceNotification(Handle, ref dbh, 0), true);
            }
            catch
            {
                MceRemoteReceiver.LogInfo("DeviceWatcher.RegisterDeviceRemoval: Error={0}.", Marshal.GetLastWin32Error());
            }

            if (_handleDeviceRemoval.IsInvalid)
            {
                throw new Exception(string.Format("Failed in call to RegisterDeviceNotification ({0})", GetLastError()));
            }
        }
Exemplo n.º 4
0
        private void Init()
        {
            try
            {
                _deviceClass     = HidGuid;
                _doubleClickTime = GetDoubleClickTime();

                _deviceBuffer = new byte[256];

                _deviceWatcher = new DeviceWatcher();
                _deviceWatcher.Create();
                _deviceWatcher.Class            = _deviceClass;
                _deviceWatcher.DeviceArrival   += new DeviceEventHandler(OnDeviceArrival);
                _deviceWatcher.DeviceRemoval   += new DeviceEventHandler(OnDeviceRemoval);
                _deviceWatcher.SettingsChanged += new SettingsChanged(OnSettingsChanged);
                _deviceWatcher.RegisterDeviceArrival();

                Open();
            }
            catch (Exception e)
            {
                MceRemoteReceiver.LogInfo("Init: {0}", e.Message);
            }
        }
Exemplo n.º 5
0
        protected string FindDevice(Guid classGuid)
        {
            if (Transceivers != null)
            {
                _eHomeTransceivers = Transceivers.Select(t => t.DeviceID);
            }

            IntPtr handle = SetupDiGetClassDevs(ref classGuid, 0, 0, 0x12);

            string devicePath = null;

            if (handle.ToInt32() == -1)
            {
                throw new Exception(string.Format("Failed in call to SetupDiGetClassDevs ({0})", GetLastError()));
            }

            for (int deviceIndex = 0;; deviceIndex++)
            {
                DeviceInfoData deviceInfoData = new DeviceInfoData();
                deviceInfoData.Size = Marshal.SizeOf(deviceInfoData);

                if (SetupDiEnumDeviceInfo(handle, deviceIndex, ref deviceInfoData) == false)
                {
                    // out of devices or do we have an error?
                    if (GetLastError() != 0x103 && GetLastError() != 0x7E)
                    {
                        SetupDiDestroyDeviceInfoList(handle);
                        throw new Exception(string.Format("Failed in call to SetupDiEnumDeviceInfo ({0})", GetLastError()));
                    }

                    SetupDiDestroyDeviceInfoList(handle);
                    break;
                }

                DeviceInterfaceData deviceInterfaceData = new DeviceInterfaceData();
                deviceInterfaceData.Size = Marshal.SizeOf(deviceInterfaceData);

                if (SetupDiEnumDeviceInterfaces(handle, ref deviceInfoData, ref classGuid, 0, ref deviceInterfaceData) == false)
                {
                    SetupDiDestroyDeviceInfoList(handle);
                    throw new Exception(string.Format("Failed in call to SetupDiEnumDeviceInterfaces ({0})", GetLastError()));
                }

                uint cbData = 0;

                if (SetupDiGetDeviceInterfaceDetail(handle, ref deviceInterfaceData, 0, 0, ref cbData, 0) == false &&
                    cbData == 0)
                {
                    SetupDiDestroyDeviceInfoList(handle);
                    throw new Exception(string.Format("Failed in call to SetupDiGetDeviceInterfaceDetail ({0})", GetLastError()));
                }

                DeviceInterfaceDetailData deviceInterfaceDetailData = new DeviceInterfaceDetailData();
                deviceInterfaceDetailData.Size = 5;

                if (
                    SetupDiGetDeviceInterfaceDetail(handle, ref deviceInterfaceData, ref deviceInterfaceDetailData, cbData, 0, 0) ==
                    false)
                {
                    SetupDiDestroyDeviceInfoList(handle);
                    throw new Exception(string.Format("Failed in call to SetupDiGetDeviceInterfaceDetail ({0})", GetLastError()));
                }

                if (LogVerbose)
                {
                    MceRemoteReceiver.LogInfo("Found: {0}", deviceInterfaceDetailData.DevicePath);
                }

                foreach (string deviceId in _eHomeTransceivers)
                {
                    if ((deviceInterfaceDetailData.DevicePath.IndexOf(deviceId) != -1) ||
                        (deviceInterfaceDetailData.DevicePath.StartsWith(@"\\?\hid#irdevice&col01#2")) ||
                        // eHome Infrared Transceiver List XP
                        (deviceInterfaceDetailData.DevicePath.StartsWith(@"\\?\hid#irdevicev2&col01#2")))
                    // Microsoft/Philips 2005 (Vista)
                    {
                        SetupDiDestroyDeviceInfoList(handle);
                        devicePath = deviceInterfaceDetailData.DevicePath;
                    }
                }
                if (devicePath != null)
                {
                    break;
                }
            }
            return(devicePath);
        }