Example #1
0
 protected static extern bool SetupDiEnumDeviceInterfaces
     (SafeDevInfoTableHandle lpDeviceInfoSet,
     IntPtr DeviceInfoData,
     ref Guid gClass,
     uint nIndex,
     ref SP_DEVICE_INTERFACE_DATA oInterfaceData
     );
Example #2
0
 protected static extern bool SetupDiGetDeviceInterfaceDetail
     (SafeDevInfoTableHandle lpDeviceInfoSet,
     ref SP_DEVICE_INTERFACE_DATA oInterfaceData,
     IntPtr oDetailData,
     uint nDeviceInterfaceDetailDataSize,
     ref uint nRequiredSize,
     IntPtr lpDeviceInfoData
     );
Example #3
0
 protected static extern bool SetupDiGetDeviceRegistryProperty
 (
     SafeDevInfoTableHandle DeviceInfoSet,
     ref SP_DEVINFO_DATA DeviceInfoData,
     uint Property,
     IntPtr PropertyRegDataType,
     [MarshalAs(UnmanagedType.LPArray)] char[] PropertyBuffer,
     uint PropertyBufferSize,
     IntPtr RequiredSize
 );
Example #4
0
 protected static extern bool SetupDiGetDeviceRegistryProperty
 (
     SafeDevInfoTableHandle DeviceInfoSet,
     ref SP_DEVINFO_DATA DeviceInfoData,
     uint Property,
     IntPtr PropertyRegDataType,
     IntPtr PropertyBuffer,
     uint PropertyBufferSize,
     ref uint RequiredSize
 );
        public void UpdateListOfAttachedDevices()
        {
            AsyncOperation asyncOp = AsyncOperationManager.CreateOperation(new Guid());

            ThreadStart start =
                delegate()
            {
                Monitor.Enter(_programmers);

                try
                {
                    #region Search of devices

                    SafeDevInfoTableHandle DeviceInfoTable = SetupDiGetClassDevs(ref guid, null, IntPtr.Zero, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
                    if (DeviceInfoTable.IsInvalid)
                    {
                        Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
                    }

                    SpDevinfoData.cbSize             = (UInt32)Marshal.SizeOf(SpDevinfoData);
                    SpDeviceInterfaceData.Size       = (UInt32)Marshal.SizeOf(SpDeviceInterfaceData);
                    SpDeviceInterfaceDetailData.Size = 6;

                    IDictionary <string, string> actualProgs = new Dictionary <string, string>();

                    for (UInt32 interface_index = 0;
                         SetupDiEnumDeviceInterfaces(DeviceInfoTable, new IntPtr(0),
                                                     ref guid, interface_index, ref SpDeviceInterfaceData); interface_index++)
                    {
                        if (!SetupDiEnumDeviceInfo(DeviceInfoTable, interface_index, ref SpDevinfoData))
                        {
                            continue;
                        }

                        uint recSize = 0;
                        bool res     = SetupDiGetDeviceRegistryProperty(DeviceInfoTable, ref SpDevinfoData,
                                                                        SPDRP_HARDWAREID, new IntPtr(0), new IntPtr(0), 0, ref recSize);
                        if (!res)
                        {
                            if ((uint)Marshal.GetLastWin32Error() != ERROR_INSUFFICIENT_BUFFER)
                            {
                                Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
                            }
                        }

                        char[] PropertyBuffer = new char[recSize / 2];

                        res = SetupDiGetDeviceRegistryProperty(DeviceInfoTable, ref SpDevinfoData,
                                                               SPDRP_HARDWAREID, new IntPtr(0), PropertyBuffer, recSize, new IntPtr(0));
                        if (!res)
                        {
                            Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
                        }

                        string deviceName = (new string(PropertyBuffer)).ToUpperInvariant();


                        foreach (string str in _programmerFabrics.Keys)
                        {
                            if (deviceName.Contains(str))
                            {
                                deviceName = string.Copy(str);
                                break;
                            }
                        }

                        //deviceName = deviceName.Substring(0, deviceName.IndexOf("\0"));

                        if (_programmerFabrics.ContainsKey(deviceName))
                        {
                            uint structSize = 0;

                            res = SetupDiGetDeviceInterfaceDetail(DeviceInfoTable, ref SpDeviceInterfaceData,
                                                                  new IntPtr(0), 0, ref structSize, new IntPtr(0));
                            if (!res)
                            {
                                if ((uint)Marshal.GetLastWin32Error() != ERROR_INSUFFICIENT_BUFFER)
                                {
                                    Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
                                }
                            }


                            res = SetupDiGetDeviceInterfaceDetail(DeviceInfoTable, ref SpDeviceInterfaceData,
                                                                  ref SpDeviceInterfaceDetailData, structSize,
                                                                  new IntPtr(0), new IntPtr(0));
                            if (!res)
                            {
                                Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
                            }


                            actualProgs.Add(SpDeviceInterfaceDetailData.DevicePath, deviceName);
                        }
                    }

                    #endregion


                    List <string> remProgs = new List <string>();
                    //int lastNumberOfProgs = _programmers.Count;
                    //List<IChipProgrammer> progsInSMode = new List<IChipProgrammer>();
                    //List<IChipProgrammer> removedProgs = new List<IChipProgrammer>();


                    //List<IChipProgrammer> _progsInResetterMode = new List<IChipProgrammer>();
                    //List<IChipProgrammer> _progsInServiceMode = new List<IChipProgrammer>();
                    List <IChipProgrammer> _removedProgs = new List <IChipProgrammer>();
                    int progsInProgrammerModeCount       = _progsInProgrammerMode.Count;
                    int progsInServiceModeCount          = _progsInServiceMode.Count;

                    foreach (KeyValuePair <string, IChipProgrammer> kvp in _programmers)
                    {
                        if (actualProgs.ContainsKey(kvp.Key))
                        {
                            actualProgs.Remove(kvp.Key);
                        }
                        else
                        {
                            remProgs.Add(kvp.Key);
                        }
                    }


                    foreach (string remProg in remProgs)
                    {
                        _progsInProgrammerMode.Remove(_programmers[remProg]);
                        _progsInServiceMode.Remove(_programmers[remProg]);
                        _removedProgs.Add(_programmers[remProg]);
                        _programmers.Remove(remProg);
                    }
                    foreach (KeyValuePair <string, string> kvp in actualProgs)
                    {
                        #region Inquiry again attached devices

                        IChipProgrammer prog  = _programmerFabrics[kvp.Value](kvp.Key);
                        HardwareMode    hMode = HardwareMode.UnknownMode;
                        Thread          th    = new Thread(
                            delegate()
                        {
                            hMode = prog.GetMode();
                        }
                            );
                        th.Start();
                        //th.Join();
                        if (th.Join(2000))
                        {
                            switch (hMode)
                            {
                            case HardwareMode.ProgrammerMode:
                                _programmers.Add(kvp.Key, prog);
                                _progsInProgrammerMode.Add(prog);
                                break;

                            case HardwareMode.ServiceMode:
                                _programmers.Add(kvp.Key, prog);
                                _progsInServiceMode.Add(prog);
                                break;

                            case HardwareMode.UnknownMode:
                                asyncOp.Post(
                                    delegate(object args)
                                {
                                    HardwareControllerEventArgs hce = new HardwareControllerEventArgs();
                                    hce.Error = new Exception("Attached hardware in unknown mode");

                                    OnHardwareDetectionError(hce);
                                },
                                    null
                                    );
                                break;
                            }
                        }
                        else
                        {
                            th.Abort();
                            asyncOp.Post(
                                delegate(object args)
                            {
                                HardwareControllerEventArgs hce = new HardwareControllerEventArgs();
                                hce.Error = new Exception("Attached hardware not answer");

                                OnHardwareDetectionError(hce);
                            },
                                null
                                );
                        }

                        #endregion
                    }

                    if (_removedProgs.Count != 0)
                    {
                        asyncOp.Post(
                            delegate(object args)
                        {
                            HardwareControllerEventArgs hce = new HardwareControllerEventArgs();

                            hce.ListOfDevices = new List <IChipProgrammer>(_removedProgs.ToArray());
                            OnDeviceRemoved(hce);
                        },
                            null
                            );
                    }
                    if (_progsInServiceMode.Count > progsInServiceModeCount)
                    {
                        asyncOp.Post(
                            delegate(object args)
                        {
                            HardwareControllerEventArgs hce = new HardwareControllerEventArgs();
                            hce.ListOfDevices = new List <IChipProgrammer>(_progsInServiceMode.ToArray());
                            OnDeviceInServiceModeDetected(hce);
                        },
                            null
                            );
                    }
                    if (_progsInProgrammerMode.Count > progsInProgrammerModeCount)
                    {
                        asyncOp.Post(
                            delegate(object args)
                        {
                            HardwareControllerEventArgs hce = new HardwareControllerEventArgs();
                            hce.ListOfDevices = new List <IChipProgrammer>(_progsInProgrammerMode.ToArray());
                            OnDeviceInProgrammerModeDetected(hce);
                        },
                            null
                            );
                    }
                }
                catch (Exception e)
                {
                    asyncOp.PostOperationCompleted(
                        delegate(object args)
                    {
                        HardwareControllerEventArgs hce = new HardwareControllerEventArgs();
                        hce.Error = e;
                        OnHardwareDetectionError(hce);
                    },
                        null
                        );
                }

                Monitor.PulseAll(_programmers);
                Monitor.Exit(_programmers);
            };

            (new Thread(start)).Start();
        }
Example #6
0
 protected static extern bool SetupDiEnumDeviceInfo
 (
     SafeDevInfoTableHandle DeviceInfoSet,
     uint MemberIndex,
     ref SP_DEVINFO_DATA DeviceInfoData
 );