Exemplo n.º 1
0
    /**
     * Get a handle for USB device file
     * @param filename the name of the file OR vendor and device ids formatted as "vid&pid"
     * @param report_size [optional] report size in bytes
     * @return open read/write Stream
     */
    public override Stream GetUSBHandle(string filename, int report_size)
    { 
        HidDeviceLoader loader = new HidDeviceLoader();
        int vid = 0;
        int pid = 0;
        if (filename.IndexOf("&") > 0) {
            String[] parts = filename.Split(new Char[]{'&'});
            vid = Convert.ToInt32(parts[0]);
            pid = Convert.ToInt32(parts[1]);
        } else {
            System.Console.WriteLine("Invalid device specification: " + filename);
            return null;
        }

        HidDevice dev = loader.GetDeviceOrDefault(vid, pid, null, null);
        if (dev == null) {
            System.Console.WriteLine("Could not find requested device: " + filename);
            var devices = loader.GetDevices().ToArray();
            foreach (HidDevice d in devices) {
                System.Console.WriteLine(d);
            }
            return null;
        }

        HidStream stream;
        if (!dev.TryOpen(out stream)) {
            System.Console.WriteLine("Found requested device but cannot connect: " + filename);
            return null;
        }

        return stream;
    }
Exemplo n.º 2
0
        /* write config to connected devices */
        public bool Program(eeprom.eep_config config)
        {
            var loader = new HidDeviceLoader();
            var device = loader.GetDevices(0x1234, 0x9876).First();

            HidStream stream;
            if (!device.TryOpen(out stream))
                return false;
            if (!CheckBootloader(stream))
                return false;

            // convert config to a byte array buffer
            int size = Marshal.SizeOf(config);
            byte[] buffer = new byte[size];
            IntPtr ptr = Marshal.AllocHGlobal(size);
            Marshal.StructureToPtr(config, ptr, true);
            Marshal.Copy(ptr, buffer, 0, size);
            Marshal.FreeHGlobal(ptr);

            // write EEPROM
            //if (ExecuteHIDCommand(stream, (int)BootloaderCommands.RESET_POINTER) == null)
            //	return false;
            if (ExecuteHIDCommand(stream, (int)BootloaderCommands.WRITE_EEPROM, 0, buffer) == null)
                return false;

            return true;
        }
Exemplo n.º 3
0
        public HidManager()
        {
            HidDeviceLoader = new HidDeviceLoader();
            FoundDevices = new ObservableCollection<HidDeviceRepresentation>();

            _hidScanThread = new Thread(() =>
            {
                while (true)
                {
                    var currentDevices = HidDeviceLoader.GetDevices().Select(hidDevice => new HidDeviceRepresentation(hidDevice)).ToList();

                    if (!Thread.CurrentThread.IsAlive) { break; }

                    Application.Current.Dispatcher.Invoke(() =>
                    {
                        foreach (var newDevice in currentDevices.Where(device => !FoundDevices.Contains(device)))
                        {
                            FoundDevices.Add(newDevice);
                        }
                    });

                    Thread.Sleep(TimeSpan.FromSeconds(10));
                }
                // ReSharper disable once FunctionNeverReturns
            });
            _hidScanThread.Start();
        }
Exemplo n.º 4
0
        public Gamepad(GamepadConfiguration config, VGenWrapper vGenWrapper, HidDeviceLoader hidDeviceLoader)
        {
            _config = config;
            _vGenWrapper = vGenWrapper;
            _hidDeviceLoader = hidDeviceLoader;
            _virtualMappings = new Dictionary<XInputGamepadButtons, XInputGamepadButtons>();

            foreach (var virtualMapping in _config.Mapping.VirtualKeysItems.Where(item => item.DestinationItem != null))
            {
                var virtualPattern = virtualMapping.SourceKeys
                    .Where(sourceKey => sourceKey != null)
                    .Aggregate((XInputGamepadButtons)0, (current, sourceKey) => current | sourceKey.Value);

                // ReSharper disable once PossibleInvalidOperationException
                _virtualMappings[virtualPattern] = (XInputGamepadButtons) virtualMapping.DestinationItem;
            }
        }
Exemplo n.º 5
0
        /* read config from first connected device */
        /* returns null on failure */
        public config Read()
        {
            var loader = new HidDeviceLoader();
            var device = loader.GetDevices(0x1234, 0x9876).First();

            HidStream stream;
            if (!device.TryOpen(out stream))
                return null;
            if (!CheckBootloader(stream))
                return null;

            // read EEPROM
            var result = ExecuteHIDCommand(stream, (int)BootloaderCommands.READ_EEPROM, 0);
            if (result == null)
                return null;

            if (result.Length != (4 + 32))	// header + one EEPROM page
            {
                Console.WriteLine("Incorrect size response from bootloader.");
                return null;
            }

            // marshall the bytes from result into a new eep_config object
            var eep = new eeprom.eep_config();
            int size = Marshal.SizeOf(eep);
            IntPtr ptr = Marshal.AllocHGlobal(size);
            Marshal.Copy(result, 4, ptr, size);
            eep = (eeprom.eep_config)Marshal.PtrToStructure(ptr, typeof(eeprom.eep_config));
            Marshal.FreeHGlobal(ptr);

            // test EEPROM config and convert to config
            string error_message;
            var cfg = eeprom.DecodeEEPROMConfig(eep, out error_message);
            if (cfg == null)
            {
                Console.WriteLine(error_message);
                return null;
            }

            return cfg;
        }
        protected override List<IDevice> GetSubDevicesInternal()
        {
            List<IDevice> ret = base.GetSubDevicesInternal();

            IEnumerable<HidDevice> hidDevices = new HidDeviceLoader().GetDevices(VID_CORSAIR_LINK);

            foreach (HidDevice hidDevice in hidDevices)
            {
                USB.BaseUSBDevice device;
                switch (hidDevice.ProductID)
                {
                    case PID_CORSAIR_COMMANDER_LINK_A:
                        device = new DeviceCommanderA(this, hidDevice);
                        break;
                    case PID_CORSAIR_COMMANDER_LINK_B:
                        device = new DeviceCommanderB(this, hidDevice);
                        break;
                    case PID_CORSAIR_BOOTLOADER:
                        device = new DeviceBootloader(this, hidDevice);
                        break;
                    case PID_CORSAIR_MODERN:
                        device = new DeviceModern(this, hidDevice);
                        break;
                    default:
                        device = null;
                        break;
                }
                if (device != null)
                    ret.Add(device);
            }

            return ret;
        }
Exemplo n.º 7
0
 //internal static bool TestKeyboard()
 //{
 //    //FIND WHETHER A USB KEYBOARD IS PLUGGED IN
 //    var hd = new HidDeviceLoader();
 //    if (hd.GetDevices().Any(item => item.ProductName.Contains("Keyboard")))
 //        return true;
 //    //FIND WHETHER A PS/2 KEYBOARD IS PLUGGED IN.
 //    const string query = "select * from Win32_Keyboard";
 //    var oQuery = new ObjectQuery(query);
 //    var searcher = new ManagementObjectSearcher(oQuery);
 //    var recordSet = searcher.Get();
 //    return
 //        recordSet.Cast<ManagementObject>()
 //            .Any(record => record.Properties["Description"].Value.ToString().Contains("Keyboard"));
 //}
 internal static bool TestCherryKeyboard()
 {
     var hd = new HidDeviceLoader();
     return hd.GetDevices().Any(item => item.VendorID == 1130);
 }
Exemplo n.º 8
0
        public override bool Open(string devicePath)
        {
            var loader = new HidDeviceLoader();

            // search for HID
            _currentHidDevice = loader.GetDevices(VendorId, ProductId).FirstOrDefault();

            if (_currentHidDevice == null)
            {
                Log.ErrorFormat("Couldn't find device with VID: {0}, PID: {1}",
                    VendorId, ProductId);
                return false;
            }

            // open HID
            if (!_currentHidDevice.TryOpen(out _currentHidStream))
            {
                Log.ErrorFormat("Couldn't open device {0}", _currentHidDevice);
                return false;
            }

            // since these devices have no MAC address, generate one
            DeviceAddress = PhysicalAddress.Parse(MacAddressGenerator.NewMacAddress);

            IsActive = true;
            Path = devicePath;

            return IsActive;
        }
Exemplo n.º 9
0
        /// <summary>
        /// Initializes communication with the DTS Card
        /// </summary>
        /// <returns>True if update is sucessful, false if there was a problem.</returns>
        public bool initialize()
        {
            if (_device == null) 
            {
                dispose();
            }

            HidDeviceLoader deviceLoader = new HidDeviceLoader();
            foreach (HidDevice device in deviceLoader.GetDevices(0x04d8, 0xf64e))
            {
                if (device.SerialNumber.Equals(SerialNumber))
                {
                    _device = device;
                    break;
                }
            }

            if (_device != null)
            {
                _device.TryOpen(out _stream);
            }

            return _stream != null;
        }
Exemplo n.º 10
0
        /// <summary>
        /// Attempts to connect to a BlinkStick device.
        /// 
        /// After a successful connection, a DeviceAttached event will normally be sent.
        /// </summary>
        /// <returns>True if a Blinkstick device is connected, False otherwise.</returns>
        public bool OpenDevice()
        {
            bool result;

            this._VersionMajor = -1;
            this._VersionMinor = -1;

            if (this.device == null) {
                HidDeviceLoader loader = new HidDeviceLoader();
                HidDevice adevice = loader.GetDevices(VendorId, ProductId).FirstOrDefault();
                result = OpenDevice (adevice);
            } else {
                result = OpenCurrentDevice();
            }

            CheckRequiresSoftwareColorPatch();

            return result;
        }
Exemplo n.º 11
0
        /// <summary>
        /// Find all BlinkStick devices.
        /// </summary>
        /// <returns>An array of BlinkStick devices</returns>
        public static BlinkStick[] FindAll()
        {
            List<BlinkStick> result = new List<BlinkStick>();

            HidDeviceLoader loader = new HidDeviceLoader();
            foreach (HidDevice adevice in loader.GetDevices(VendorId, ProductId).ToArray())
            {
                BlinkStick hid = new BlinkStick();
                hid.device = adevice;
                result.Add(hid);
            }

            return result.ToArray();
        }
Exemplo n.º 12
0
        /// <summary>
        /// Initializes the dongle to receive buzzer inputs. Uses HidSharp Library.
        /// </summary>
        public void InitWBuzzReceiver()
        {
            // Looks for the Device according to its VID and PID, then tries to open a stream to write to it
            var loader = new HidDeviceLoader();
            var device = loader.GetDevices(0x054C, 0x1000).First();
            HidStream stream;
            device.TryOpen(out stream);

            // Sends a 7-byte message to make it ready to communicate with the buzzers
            var message = new byte[] { 0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
            stream.Write(message);
            stream.Close();
        }