From 2071610bbe2f1f9243e3920add5a78d8cdfa973a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20M=C3=B6ller?= Date: Sun, 15 Aug 2010 14:46:58 +0000 Subject: [PATCH] Fixed some Code Analysis warnings. --- Collections/ListSet.cs | 1 + Collections/ReadOnlyArray.cs | 4 + GUI/Node.cs | 4 +- Hardware/ATI/ADL.cs | 2 +- Hardware/ATI/ATIGroup.cs | 2 +- Hardware/CPU/CPUGroup.cs | 10 +- Hardware/CPU/CPUID.cs | 10 +- Hardware/CPU/CPULoad.cs | 42 ++- Hardware/CPU/IntelCPU.cs | 14 +- Hardware/Computer.cs | 23 +- Hardware/HDD/HDD.cs | 5 +- Hardware/HDD/SMART.cs | 89 +++-- Hardware/Hardware.cs | 5 +- Hardware/IComputer.cs | 2 +- Hardware/Identifier.cs | 2 +- Hardware/LPC/Chip.cs | 34 +- Hardware/LPC/LMSensors.cs | 5 +- Hardware/LPC/LPCIO.cs | 525 ++++++++++++++------------ Hardware/LPC/W836XX.cs | 2 +- Hardware/Mainboard/Mainboard.cs | 8 +- Hardware/Mainboard/SMBIOS.cs | 35 +- Hardware/Mainboard/SuperIOHardware.cs | 37 +- Hardware/Nvidia/NVAPI.cs | 4 +- Hardware/Nvidia/NvidiaGPU.cs | 3 +- Hardware/PInvokeDelegateFactory.cs | 20 +- Hardware/Parameter.cs | 9 +- Hardware/Sensor.cs | 33 +- Hardware/SensorVisitor.cs | 6 + Hardware/TBalancer/FTD2XX.cs | 87 +++-- Hardware/TBalancer/TBalancer.cs | 7 +- Hardware/WinRing0.cs | 110 +++--- OpenHardwareMonitorLib.csproj | 1 + Properties/AssemblyLibInfo.cs | 3 +- 33 files changed, 609 insertions(+), 535 deletions(-) diff --git a/Collections/ListSet.cs b/Collections/ListSet.cs index af3fb37..1fc61f3 100644 --- a/Collections/ListSet.cs +++ b/Collections/ListSet.cs @@ -41,6 +41,7 @@ using System.Collections.Generic; using System.Text; namespace OpenHardwareMonitor.Collections { + public class ListSet : IEnumerable { private List list = new List(); diff --git a/Collections/ReadOnlyArray.cs b/Collections/ReadOnlyArray.cs index df937c8..70621a0 100644 --- a/Collections/ReadOnlyArray.cs +++ b/Collections/ReadOnlyArray.cs @@ -66,5 +66,9 @@ namespace OpenHardwareMonitor.Collections { public static implicit operator ReadOnlyArray(T[] array) { return new ReadOnlyArray(array); } + + public T[] ToArray() { + return (T[])array.Clone(); + } } } diff --git a/GUI/Node.cs b/GUI/Node.cs index 5666b2c..adcdf12 100644 --- a/GUI/Node.cs +++ b/GUI/Node.cs @@ -159,7 +159,7 @@ namespace OpenHardwareMonitor.GUI { protected override void InsertItem(int index, Node item) { if (item == null) - throw new ArgumentNullException(); + throw new ArgumentNullException("item"); if (item.parent != owner) { if (item.parent != null) @@ -189,7 +189,7 @@ namespace OpenHardwareMonitor.GUI { protected override void SetItem(int index, Node item) { if (item == null) - throw new ArgumentNullException(); + throw new ArgumentNullException("item"); RemoveAt(index); InsertItem(index, item); diff --git a/Hardware/ATI/ADL.cs b/Hardware/ATI/ADL.cs index 878f780..0264d52 100644 --- a/Hardware/ATI/ADL.cs +++ b/Hardware/ATI/ADL.cs @@ -156,7 +156,7 @@ namespace OpenHardwareMonitor.Hardware.ATI { private static ADL_Adapter_AdapterInfo_GetDelegate _ADL_Adapter_AdapterInfo_Get; - public static ADL_Main_Control_DestroyDelegate + public static ADL_Main_Control_DestroyDelegate ADL_Main_Control_Destroy; public static ADL_Adapter_NumberOfAdapters_GetDelegate ADL_Adapter_NumberOfAdapters_Get; diff --git a/Hardware/ATI/ATIGroup.cs b/Hardware/ATI/ATIGroup.cs index 50db112..819f7f6 100644 --- a/Hardware/ATI/ATIGroup.cs +++ b/Hardware/ATI/ATIGroup.cs @@ -104,7 +104,7 @@ namespace OpenHardwareMonitor.Hardware.ATI { report.Append("AdapterID: 0x"); report.AppendLine(adapterID.ToString("X", CultureInfo.InvariantCulture)); - if (adapterID != 0 && adapterInfo[i].UDID != "" && + if (adapterID != 0 && !string.IsNullOrEmpty(adapterInfo[i].UDID) && (adapterInfo[i].VendorID == ADL.ATI_VENDOR_ID1 || adapterInfo[i].VendorID == ADL.ATI_VENDOR_ID2)) { bool found = false; diff --git a/Hardware/CPU/CPUGroup.cs b/Hardware/CPU/CPUGroup.cs index c7f67c0..1d99f92 100644 --- a/Hardware/CPU/CPUGroup.cs +++ b/Hardware/CPU/CPUGroup.cs @@ -48,13 +48,13 @@ namespace OpenHardwareMonitor.Hardware.CPU { private CPUID[][][] threads; - private CPUID[][] GetProcessorThreads() { + private static CPUID[][] GetProcessorThreads() { List threads = new List(); for (int i = 0; i < 32; i++) { try { threads.Add(new CPUID(i)); - } catch (ArgumentException) { } + } catch (ArgumentOutOfRangeException) { } } SortedDictionary> processors = @@ -78,7 +78,7 @@ namespace OpenHardwareMonitor.Hardware.CPU { return processorThreads; } - private CPUID[][] GroupThreadsByCore(CPUID[] threads) { + private static CPUID[][] GroupThreadsByCore(CPUID[] threads) { SortedDictionary> cores = new SortedDictionary>(); @@ -151,7 +151,9 @@ namespace OpenHardwareMonitor.Hardware.CPU { } } - private void AppendCpuidData(StringBuilder r, uint[,] data, uint offset) { + private static void AppendCpuidData(StringBuilder r, uint[,] data, + uint offset) + { for (int i = 0; i < data.GetLength(0); i++) { r.Append(" "); r.Append((i + offset).ToString("X8", CultureInfo.InvariantCulture)); diff --git a/Hardware/CPU/CPUID.cs b/Hardware/CPU/CPUID.cs index 942bea7..9544dd0 100644 --- a/Hardware/CPU/CPUID.cs +++ b/Hardware/CPU/CPUID.cs @@ -75,14 +75,14 @@ namespace OpenHardwareMonitor.Hardware.CPU { public static uint CPUID_0 = 0; public static uint CPUID_EXT = 0x80000000; - private void AppendRegister(StringBuilder b, uint value) { + private static void AppendRegister(StringBuilder b, uint value) { b.Append((char)((value) & 0xff)); b.Append((char)((value >> 8) & 0xff)); b.Append((char)((value >> 16) & 0xff)); b.Append((char)((value >> 24) & 0xff)); } - private uint NextLog2(long x) { + private static uint NextLog2(long x) { if (x <= 0) return 0; @@ -105,7 +105,7 @@ namespace OpenHardwareMonitor.Hardware.CPU { uint eax, ebx, ecx, edx; if (thread >= 32) - throw new ArgumentException(); + throw new ArgumentOutOfRangeException("thread"); UIntPtr mask = (UIntPtr)(1L << thread); if (WinRing0.CpuidTx(CPUID_0, 0, @@ -139,10 +139,10 @@ namespace OpenHardwareMonitor.Hardware.CPU { else return; } else { - throw new ArgumentException(); + throw new ArgumentOutOfRangeException("thread"); } } else { - throw new ArgumentException(); + throw new ArgumentOutOfRangeException("thread"); } maxCpuid = Math.Min(maxCpuid, 1024); diff --git a/Hardware/CPU/CPULoad.cs b/Hardware/CPU/CPULoad.cs index dffafc6..df876c3 100644 --- a/Hardware/CPU/CPULoad.cs +++ b/Hardware/CPU/CPULoad.cs @@ -62,12 +62,6 @@ namespace OpenHardwareMonitor.Hardware.CPU { SystemProcessorPerformanceInformation = 8 } - [DllImport("ntdll.dll")] - private static extern int NtQuerySystemInformation( - SystemInformationClass informationClass, - [Out] SystemProcessorPerformanceInformation[] informations, - int structSize, out IntPtr returnLength); - private CPUID[][] cpuid; private long systemTime; @@ -78,16 +72,17 @@ namespace OpenHardwareMonitor.Hardware.CPU { private bool available = false; - private long[] GetIdleTimes() { + private static long[] GetIdleTimes() { SystemProcessorPerformanceInformation[] informations = new SystemProcessorPerformanceInformation[64]; int size = Marshal.SizeOf(typeof(SystemProcessorPerformanceInformation)); IntPtr returnLength; - NtQuerySystemInformation( + if (NativeMethods.NtQuerySystemInformation( SystemInformationClass.SystemProcessorPerformanceInformation, - informations, informations.Length * size, out returnLength); + informations, informations.Length * size, out returnLength) != 0) + return null; long[] result = new long[(int)returnLength / size]; @@ -127,10 +122,13 @@ namespace OpenHardwareMonitor.Hardware.CPU { if (this.idleTimes == null) return; - long localSystemTime = DateTime.Now.Ticks; - long[] localIdleTimes = GetIdleTimes(); + long newSystemTime = DateTime.Now.Ticks; + long[] newIdleTimes = GetIdleTimes(); - if (localSystemTime - this.systemTime < 10000) + if (newSystemTime - this.systemTime < 10000) + return; + + if (newIdleTimes == null) return; float total = 0; @@ -139,29 +137,37 @@ namespace OpenHardwareMonitor.Hardware.CPU { float value = 0; for (int j = 0; j < cpuid[i].Length; j++) { long index = cpuid[i][j].Thread; - if (index < localIdleTimes.Length) { - long delta = localIdleTimes[index] - this.idleTimes[index]; + if (index < newIdleTimes.Length) { + long delta = newIdleTimes[index] - this.idleTimes[index]; value += delta; total += delta; count++; } } value = 1.0f - value / (cpuid[i].Length * - (localSystemTime - this.systemTime)); + (newSystemTime - this.systemTime)); value = value < 0 ? 0 : value; coreLoads[i] = value * 100; } if (count > 0) { - total = 1.0f - total / (count * (localSystemTime - this.systemTime)); + total = 1.0f - total / (count * (newSystemTime - this.systemTime)); total = total < 0 ? 0 : total; } else { total = 0; } this.totalLoad = total * 100; - this.systemTime = localSystemTime; - this.idleTimes = localIdleTimes; + this.systemTime = newSystemTime; + this.idleTimes = newIdleTimes; } + private static class NativeMethods { + + [DllImport("ntdll.dll")] + public static extern int NtQuerySystemInformation( + SystemInformationClass informationClass, + [Out] SystemProcessorPerformanceInformation[] informations, + int structSize, out IntPtr returnLength); + } } } diff --git a/Hardware/CPU/IntelCPU.cs b/Hardware/CPU/IntelCPU.cs index 7f59c4f..cd76828 100644 --- a/Hardware/CPU/IntelCPU.cs +++ b/Hardware/CPU/IntelCPU.cs @@ -253,7 +253,7 @@ namespace OpenHardwareMonitor.Hardware.CPU { get { return HardwareType.CPU; } } - private void AppendMSRData(StringBuilder r, uint msr, int thread) { + private static void AppendMSRData(StringBuilder r, uint msr, int thread) { uint eax, edx; if (WinRing0.RdmsrTx(msr, out eax, out edx, (UIntPtr)(1L << thread))) { r.Append(" "); @@ -298,7 +298,7 @@ namespace OpenHardwareMonitor.Hardware.CPU { return r.ToString(); } - private double EstimateMaxClock(double timeWindow) { + private static double EstimateMaxClock(double timeWindow) { long ticks = (long)(timeWindow * Stopwatch.Frequency); uint lsbBegin, msbBegin, lsbEnd, msbEnd; @@ -359,7 +359,7 @@ namespace OpenHardwareMonitor.Hardware.CPU { else maxClock = estimatedMaxClock; - double busClock = 0; + double newBusClock = 0; uint eax, edx; for (int i = 0; i < coreClocks.Length; i++) { System.Threading.Thread.Sleep(1); @@ -369,7 +369,7 @@ namespace OpenHardwareMonitor.Hardware.CPU { uint nehalemMultiplier = eax & 0xff; coreClocks[i].Value = (float)(nehalemMultiplier * maxClock / maxNehalemMultiplier); - busClock = (float)(maxClock / maxNehalemMultiplier); + newBusClock = (float)(maxClock / maxNehalemMultiplier); } else { // Core 2 uint multiplier = (eax >> 8) & 0x1f; uint maxMultiplier = (edx >> 8) & 0x1f; @@ -378,7 +378,7 @@ namespace OpenHardwareMonitor.Hardware.CPU { uint maxFactor = (maxMultiplier << 1) | ((edx >> 14) & 1); if (maxFactor > 0) { coreClocks[i].Value = (float)(factor * maxClock / maxFactor); - busClock = (float)(2 * maxClock / maxFactor); + newBusClock = (float)(2 * maxClock / maxFactor); } } } else { // Intel Pentium 4 @@ -386,8 +386,8 @@ namespace OpenHardwareMonitor.Hardware.CPU { coreClocks[i].Value = (float)maxClock; } } - if (busClock > 0) { - this.busClock.Value = (float)busClock; + if (newBusClock > 0) { + this.busClock.Value = (float)newBusClock; ActivateSensor(this.busClock); } } diff --git a/Hardware/Computer.cs b/Hardware/Computer.cs index d282636..62d0600 100644 --- a/Hardware/Computer.cs +++ b/Hardware/Computer.cs @@ -41,6 +41,9 @@ using System.IO; using System.Globalization; using System.Text; using System.Threading; +using System.Security; +using System.Security.Permissions; + namespace OpenHardwareMonitor.Hardware { @@ -86,10 +89,13 @@ namespace OpenHardwareMonitor.Hardware { HardwareRemoved(hardware); } + [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)] public void Open() { if (open) return; + WinRing0.Open(); + Add(new Mainboard.MainboardGroup(settings)); Add(new CPU.CPUGroup(settings)); Add(new ATI.ATIGroup(settings)); @@ -101,9 +107,11 @@ namespace OpenHardwareMonitor.Hardware { open = true; } - + public bool HDDEnabled { get { return hddEnabled; } + + [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)] set { if (open && value && !hddEnabled) { Add(new HDD.HDDGroup(settings)); @@ -129,7 +137,7 @@ namespace OpenHardwareMonitor.Hardware { } } - private void NewSection(TextWriter writer) { + private static void NewSection(TextWriter writer) { for (int i = 0; i < 8; i++) writer.Write("----------"); writer.WriteLine(); @@ -186,7 +194,7 @@ namespace OpenHardwareMonitor.Hardware { private void ReportHardware(IHardware hardware, TextWriter w) { string hardwareReport = hardware.GetReport(); - if (hardwareReport != null && hardwareReport != "") { + if (!string.IsNullOrEmpty(hardwareReport)) { NewSection(w); w.Write(hardwareReport); } @@ -237,7 +245,7 @@ namespace OpenHardwareMonitor.Hardware { foreach (IGroup group in groups) { string report = group.GetReport(); - if (report != null && report != "") { + if (!string.IsNullOrEmpty(report)) { NewSection(w); w.Write(report); } @@ -259,6 +267,8 @@ namespace OpenHardwareMonitor.Hardware { group.Close(); groups.Clear(); + WinRing0.Close(); + open = false; } @@ -266,8 +276,9 @@ namespace OpenHardwareMonitor.Hardware { public event HardwareEventHandler HardwareRemoved; public void Accept(IVisitor visitor) { - if (visitor != null) - visitor.VisitComputer(this); + if (visitor == null) + throw new ArgumentNullException("visitor"); + visitor.VisitComputer(this); } public void Traverse(IVisitor visitor) { diff --git a/Hardware/HDD/HDD.cs b/Hardware/HDD/HDD.cs index 70761f5..5384223 100644 --- a/Hardware/HDD/HDD.cs +++ b/Hardware/HDD/HDD.cs @@ -117,8 +117,9 @@ namespace OpenHardwareMonitor.Hardware.HDD { #pragma warning restore 67 public void Accept(IVisitor visitor) { - if (visitor != null) - visitor.VisitHardware(this); + if (visitor == null) + throw new ArgumentNullException("visitor"); + visitor.VisitHardware(this); } public void Traverse(IVisitor visitor) { } diff --git a/Hardware/HDD/SMART.cs b/Hardware/HDD/SMART.cs index 3c58a64..d6029f1 100644 --- a/Hardware/HDD/SMART.cs +++ b/Hardware/HDD/SMART.cs @@ -232,37 +232,10 @@ namespace OpenHardwareMonitor.Hardware.HDD { private const int MAX_DRIVE_ATTRIBUTES = 512; - private const string KERNEL = "kernel32.dll"; - - [DllImport(KERNEL, CallingConvention = CallingConvention.Winapi)] - private static extern IntPtr CreateFile(string fileName, - AccessMode desiredAccess, ShareMode shareMode, IntPtr securityAttributes, - CreationMode creationDisposition, FileAttribute flagsAndAttributes, - IntPtr templateFilehandle); - - [DllImport(KERNEL, CallingConvention = CallingConvention.Winapi)] - public static extern int CloseHandle(IntPtr handle); - - [DllImport(KERNEL, CallingConvention = CallingConvention.Winapi)] - private static extern bool DeviceIoControl(IntPtr handle, - DriveCommand command, ref DriveCommandParameter parameter, - int parameterSize, out DriveSmartReadResult result, int resultSize, - out uint bytesReturned, IntPtr overlapped); - - [DllImport(KERNEL, CallingConvention = CallingConvention.Winapi)] - private static extern bool DeviceIoControl(IntPtr handle, - DriveCommand command, ref DriveCommandParameter parameter, - int parameterSize, out DriveCommandResult result, int resultSize, - out uint bytesReturned, IntPtr overlapped); - - [DllImport(KERNEL, CallingConvention = CallingConvention.Winapi)] - private static extern bool DeviceIoControl(IntPtr handle, - DriveCommand command, ref DriveCommandParameter parameter, - int parameterSize, out DriveIdentifyResult result, int resultSize, - out uint bytesReturned, IntPtr overlapped); + private SMART() { } public static IntPtr OpenPhysicalDrive(int driveNumber) { - return CreateFile(@"\\.\PhysicalDrive" + driveNumber, + return NativeMethods.CreateFile(@"\\.\PhysicalDrive" + driveNumber, AccessMode.Read | AccessMode.Write, ShareMode.Read | ShareMode.Write, IntPtr.Zero, CreationMode.OpenExisting, FileAttribute.Device, IntPtr.Zero); @@ -279,8 +252,8 @@ namespace OpenHardwareMonitor.Hardware.HDD { parameter.Registers.LBAHigh = SMART_LBA_HI; parameter.Registers.Command = SMART_CMD; - return DeviceIoControl(handle, DriveCommand.SendDriveCommand, - ref parameter, Marshal.SizeOf(parameter), out result, + return NativeMethods.DeviceIoControl(handle, DriveCommand.SendDriveCommand, + ref parameter, Marshal.SizeOf(typeof(DriveCommandParameter)), out result, Marshal.SizeOf(typeof(DriveCommandResult)), out bytesReturned, IntPtr.Zero); } @@ -296,10 +269,10 @@ namespace OpenHardwareMonitor.Hardware.HDD { parameter.Registers.LBAHigh = SMART_LBA_HI; parameter.Registers.Command = SMART_CMD; - bool valid = DeviceIoControl(handle, DriveCommand.ReceiveDriveData, - ref parameter, Marshal.SizeOf(parameter), out result, - Marshal.SizeOf(typeof(DriveSmartReadResult)), out bytesReturned, - IntPtr.Zero); + bool valid = NativeMethods.DeviceIoControl(handle, + DriveCommand.ReceiveDriveData, ref parameter, Marshal.SizeOf(parameter), + out result, Marshal.SizeOf(typeof(DriveSmartReadResult)), + out bytesReturned, IntPtr.Zero); if (!valid) return null; @@ -315,10 +288,10 @@ namespace OpenHardwareMonitor.Hardware.HDD { parameter.DriveNumber = (byte)driveNumber; parameter.Registers.Command = ID_CMD; - bool valid = DeviceIoControl(handle, DriveCommand.ReceiveDriveData, - ref parameter, Marshal.SizeOf(parameter), out result, - Marshal.SizeOf(typeof(DriveIdentifyResult)), out bytesReturned, - IntPtr.Zero); + bool valid = NativeMethods.DeviceIoControl(handle, + DriveCommand.ReceiveDriveData, ref parameter, Marshal.SizeOf(parameter), + out result, Marshal.SizeOf(typeof(DriveIdentifyResult)), + out bytesReturned, IntPtr.Zero); if (!valid) return null; @@ -335,5 +308,43 @@ namespace OpenHardwareMonitor.Hardware.HDD { } } + public static int CloseHandle(IntPtr handle) { + return NativeMethods.CloseHandle(handle); + } + + private static class NativeMethods { + private const string KERNEL = "kernel32.dll"; + + [DllImport(KERNEL, CallingConvention = CallingConvention.Winapi, + CharSet = CharSet.Unicode)] + public static extern IntPtr CreateFile(string fileName, + AccessMode desiredAccess, ShareMode shareMode, IntPtr securityAttributes, + CreationMode creationDisposition, FileAttribute flagsAndAttributes, + IntPtr templateFilehandle); + + [DllImport(KERNEL, CallingConvention = CallingConvention.Winapi)] + public static extern int CloseHandle(IntPtr handle); + + [DllImport(KERNEL, CallingConvention = CallingConvention.Winapi)] + [return: MarshalAsAttribute(UnmanagedType.Bool)] + public static extern bool DeviceIoControl(IntPtr handle, + DriveCommand command, ref DriveCommandParameter parameter, + int parameterSize, out DriveSmartReadResult result, int resultSize, + out uint bytesReturned, IntPtr overlapped); + + [DllImport(KERNEL, CallingConvention = CallingConvention.Winapi)] + [return: MarshalAsAttribute(UnmanagedType.Bool)] + public static extern bool DeviceIoControl(IntPtr handle, + DriveCommand command, ref DriveCommandParameter parameter, + int parameterSize, out DriveCommandResult result, int resultSize, + out uint bytesReturned, IntPtr overlapped); + + [DllImport(KERNEL, CallingConvention = CallingConvention.Winapi)] + [return: MarshalAsAttribute(UnmanagedType.Bool)] + public static extern bool DeviceIoControl(IntPtr handle, + DriveCommand command, ref DriveCommandParameter parameter, + int parameterSize, out DriveIdentifyResult result, int resultSize, + out uint bytesReturned, IntPtr overlapped); + } } } diff --git a/Hardware/Hardware.cs b/Hardware/Hardware.cs index 8a6fe19..b987663 100644 --- a/Hardware/Hardware.cs +++ b/Hardware/Hardware.cs @@ -80,8 +80,9 @@ namespace OpenHardwareMonitor.Hardware { public abstract void Update(); public void Accept(IVisitor visitor) { - if (visitor != null) - visitor.VisitHardware(this); + if (visitor == null) + throw new ArgumentNullException("visitor"); + visitor.VisitHardware(this); } public void Traverse(IVisitor visitor) { diff --git a/Hardware/IComputer.cs b/Hardware/IComputer.cs index 39ce0d1..24f7f0a 100644 --- a/Hardware/IComputer.cs +++ b/Hardware/IComputer.cs @@ -46,7 +46,7 @@ namespace OpenHardwareMonitor.Hardware { IHardware[] Hardware { get; } - bool HDDEnabled { get; set; } + bool HDDEnabled { get; } string GetReport(); diff --git a/Hardware/Identifier.cs b/Hardware/Identifier.cs index e6b7513..bccfa00 100644 --- a/Hardware/Identifier.cs +++ b/Hardware/Identifier.cs @@ -45,7 +45,7 @@ namespace OpenHardwareMonitor.Hardware { private static char SEPARATOR = '/'; - private void CheckIdentifiers(string[] identifiers) { + private static void CheckIdentifiers(string[] identifiers) { foreach (string s in identifiers) if (s.Contains(" ") || s.Contains(SEPARATOR.ToString())) throw new ArgumentException("Invalid identifier"); diff --git a/Hardware/LPC/Chip.cs b/Hardware/LPC/Chip.cs index 83f3a99..b05f592 100644 --- a/Hardware/LPC/Chip.cs +++ b/Hardware/LPC/Chip.cs @@ -25,7 +25,39 @@ namespace OpenHardwareMonitor.Hardware.LPC { F71869 = 0x0814, F71882 = 0x0541, F71889ED = 0x0909, - F71889F = 0x0723 + F71889F = 0x0723 + } + + internal class ChipName { + + private ChipName() { } + + public static string GetName(Chip chip) { + switch (chip) { + case Chip.F71858: return "Fintek F71858"; + case Chip.F71862: return "Fintek F71862"; + case Chip.F71869: return "Fintek F71869"; + case Chip.F71882: return "Fintek F71882"; + case Chip.F71889ED: return "Fintek F71889ED"; + case Chip.F71889F: return "Fintek F71889F"; + case Chip.IT8712F: return "ITE IT8712F"; + case Chip.IT8716F: return "ITE IT8716F"; + case Chip.IT8718F: return "ITE IT8718F"; + case Chip.IT8720F: return "ITE IT8720F"; + case Chip.IT8726F: return "ITE IT8726F"; + case Chip.W83627DHG: return "Winbond W83627DHG"; + case Chip.W83627DHGP: return "Winbond W83627DHG-P"; + case Chip.W83627EHF: return "Winbond W83627EHF"; + case Chip.W83627HF: return "Winbond W83627HF"; + case Chip.W83627THF: return "Winbond W83627THF"; + case Chip.W83667HG: return "Winbond W83667HG"; + case Chip.W83667HGB: return "Winbond W83667HG-B"; + case Chip.W83687THF: return "Winbond W83687THF"; + case Chip.Unknown: return "Unkown"; + default: return "Unknown"; + } + } + } } diff --git a/Hardware/LPC/LMSensors.cs b/Hardware/LPC/LMSensors.cs index e28a576..c6a3add 100755 --- a/Hardware/LPC/LMSensors.cs +++ b/Hardware/LPC/LMSensors.cs @@ -51,9 +51,8 @@ namespace OpenHardwareMonitor.Hardware.LPC { foreach (string path in devicePaths) { string name = null; try { - StreamReader reader = new StreamReader(path + "/device/name"); - name = reader.ReadLine(); - reader.Close(); + using (StreamReader reader = new StreamReader(path + "/device/name")) + name = reader.ReadLine(); } catch (IOException) { } switch (name) { case "f71858fg": diff --git a/Hardware/LPC/LPCIO.cs b/Hardware/LPC/LPCIO.cs index b994b91..46652f9 100644 --- a/Hardware/LPC/LPCIO.cs +++ b/Hardware/LPC/LPCIO.cs @@ -47,8 +47,6 @@ namespace OpenHardwareMonitor.Hardware.LPC { private List superIOs = new List(); private StringBuilder report = new StringBuilder(); - private Chip chip = Chip.Unknown; - // I/O Ports private ushort[] REGISTER_PORTS = new ushort[] { 0x2e, 0x4e }; private ushort[] VALUE_PORTS = new ushort[] { 0x2f, 0x4f }; @@ -121,261 +119,290 @@ namespace OpenHardwareMonitor.Hardware.LPC { WinRing0.WriteIoPortByte(registerPort, 0xAA); } + private void ReportUnknownChip(string type, int chip) { + report.Append("Chip ID: Unknown "); + report.Append(type); + report.Append(" with ID 0x"); + report.Append(chip.ToString("X", CultureInfo.InvariantCulture)); + report.Append(" at 0x"); + report.Append(registerPort.ToString("X", CultureInfo.InvariantCulture)); + report.Append("/0x"); + report.AppendLine(valuePort.ToString("X", CultureInfo.InvariantCulture)); + report.AppendLine(); + } + + private bool DetectWinbondFintek() { + WinbondFintekEnter(); + + byte logicalDeviceNumber; + byte id = ReadByte(CHIP_ID_REGISTER); + byte revision = ReadByte(CHIP_REVISION_REGISTER); + Chip chip = Chip.Unknown; + logicalDeviceNumber = 0; + switch (id) { + case 0x05: + switch (revision) { + case 0x07: + chip = Chip.F71858; + logicalDeviceNumber = F71858_HARDWARE_MONITOR_LDN; + break; + case 0x41: + chip = Chip.F71882; + logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN; + break; + } break; + case 0x06: + switch (revision) { + case 0x01: + chip = Chip.F71862; + logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN; + break; + } break; + case 0x07: + switch (revision) { + case 0x23: + chip = Chip.F71889F; + logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN; + break; + } break; + case 0x08: + switch (revision) { + case 0x14: + chip = Chip.F71869; + logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN; + break; + } break; + case 0x09: + switch (revision) { + case 0x09: + chip = Chip.F71889ED; + logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN; + break; + } break; + case 0x52: + switch (revision) { + case 0x17: + case 0x3A: + case 0x41: + chip = Chip.W83627HF; + logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN; + break; + } break; + case 0x82: + switch (revision & 0xF0) { + case 0x80: + chip = Chip.W83627THF; + logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN; + break; + } break; + case 0x85: + switch (revision) { + case 0x41: + chip = Chip.W83687THF; + logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN; + break; + } break; + case 0x88: + switch (revision & 0xF0) { + case 0x50: + case 0x60: + chip = Chip.W83627EHF; + logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN; + break; + } break; + case 0xA0: + switch (revision & 0xF0) { + case 0x20: + chip = Chip.W83627DHG; + logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN; + break; + } break; + case 0xA5: + switch (revision & 0xF0) { + case 0x10: + chip = Chip.W83667HG; + logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN; + break; + } break; + case 0xB0: + switch (revision & 0xF0) { + case 0x70: + chip = Chip.W83627DHGP; + logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN; + break; + } break; + case 0xB3: + switch (revision & 0xF0) { + case 0x50: + chip = Chip.W83667HGB; + logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN; + break; + } break; + } + if (chip == Chip.Unknown) { + if (id != 0 && id != 0xff) { + WinbondFintekExit(); + + ReportUnknownChip("Winbond / Fintek", ((id << 8) | revision)); + } + } else { + + Select(logicalDeviceNumber); + ushort address = ReadWord(BASE_ADDRESS_REGISTER); + Thread.Sleep(1); + ushort verify = ReadWord(BASE_ADDRESS_REGISTER); + + ushort vendorID = ReadWord(FINTEK_VENDOR_ID_REGISTER); + + WinbondFintekExit(); + + if (address != verify) { + report.Append("Chip ID: 0x"); + report.AppendLine(chip.ToString("X")); + report.Append("Chip revision: 0x"); + report.AppendLine(revision.ToString("X", + CultureInfo.InvariantCulture)); + report.AppendLine("Error: Address verification failed"); + report.AppendLine(); + return false; + } + + // some Fintek chips have address register offset 0x05 added already + if ((address & 0x07) == 0x05) + address &= 0xFFF8; + + if (address < 0x100 || (address & 0xF007) != 0) { + report.Append("Chip ID: 0x"); + report.AppendLine(chip.ToString("X")); + report.Append("Chip revision: 0x"); + report.AppendLine(revision.ToString("X", + CultureInfo.InvariantCulture)); + report.Append("Error: Invalid address 0x"); + report.AppendLine(address.ToString("X", + CultureInfo.InvariantCulture)); + report.AppendLine(); + return false; + } + + switch (chip) { + case Chip.W83627DHG: + case Chip.W83627DHGP: + case Chip.W83627EHF: + case Chip.W83627HF: + case Chip.W83627THF: + case Chip.W83667HG: + case Chip.W83667HGB: + case Chip.W83687THF: + superIOs.Add(new W836XX(chip, revision, address)); + break; + case Chip.F71858: + case Chip.F71862: + case Chip.F71869: + case Chip.F71882: + case Chip.F71889ED: + case Chip.F71889F: + if (vendorID != FINTEK_VENDOR_ID) { + report.Append("Chip ID: 0x"); + report.AppendLine(chip.ToString("X")); + report.Append("Chip revision: 0x"); + report.AppendLine(revision.ToString("X", + CultureInfo.InvariantCulture)); + report.Append("Error: Invalid vendor ID 0x"); + report.AppendLine(vendorID.ToString("X", + CultureInfo.InvariantCulture)); + report.AppendLine(); + return false; + } + superIOs.Add(new F718XX(chip, address)); + break; + default: break; + } + + return true; + } + + return false; + } + + private bool DetectIT87() { + IT87Enter(); + + ushort chipID = ReadWord(CHIP_ID_REGISTER); + Chip chip; + switch (chipID) { + case 0x8712: chip = Chip.IT8712F; break; + case 0x8716: chip = Chip.IT8716F; break; + case 0x8718: chip = Chip.IT8718F; break; + case 0x8720: chip = Chip.IT8720F; break; + case 0x8726: chip = Chip.IT8726F; break; + default: chip = Chip.Unknown; break; + } + if (chip == Chip.Unknown) { + if (chipID != 0 && chipID != 0xffff) { + IT87Exit(); + + ReportUnknownChip("ITE", chipID); + } + } else { + Select(IT87_ENVIRONMENT_CONTROLLER_LDN); + ushort address = ReadWord(BASE_ADDRESS_REGISTER); + Thread.Sleep(1); + ushort verify = ReadWord(BASE_ADDRESS_REGISTER); + + byte version = (byte)(ReadByte(IT87_CHIP_VERSION_REGISTER) & 0x0F); + + IT87Exit(); + + if (address != verify || address < 0x100 || (address & 0xF007) != 0) { + report.Append("Chip ID: 0x"); + report.AppendLine(chip.ToString("X")); + report.Append("Error: Invalid address 0x"); + report.AppendLine(address.ToString("X", + CultureInfo.InvariantCulture)); + report.AppendLine(); + return false; + } + + superIOs.Add(new IT87XX(chip, address, version)); + return true; + } + + return false; + } + + private bool DetectSMSC() { + SMSCEnter(); + + ushort chipID = ReadWord(CHIP_ID_REGISTER); + Chip chip; + switch (chipID) { + default: chip = Chip.Unknown; break; + } + if (chip == Chip.Unknown) { + if (chipID != 0 && chipID != 0xffff) { + SMSCExit(); + + ReportUnknownChip("SMSC", chipID); + } + } else { + SMSCExit(); + return true; + } + + return false; + } + private void Detect() { for (int i = 0; i < REGISTER_PORTS.Length; i++) { registerPort = REGISTER_PORTS[i]; valuePort = VALUE_PORTS[i]; - WinbondFintekEnter(); + if (DetectWinbondFintek()) continue; - byte logicalDeviceNumber; - byte id = ReadByte(CHIP_ID_REGISTER); - byte revision = ReadByte(CHIP_REVISION_REGISTER); - chip = Chip.Unknown; - logicalDeviceNumber = 0; - switch (id) { - case 0x05: - switch (revision) { - case 0x07: - chip = Chip.F71858; - logicalDeviceNumber = F71858_HARDWARE_MONITOR_LDN; - break; - case 0x41: - chip = Chip.F71882; - logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN; - break; - } break; - case 0x06: - switch (revision) { - case 0x01: - chip = Chip.F71862; - logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN; - break; - } break; - case 0x07: - switch (revision) { - case 0x23: - chip = Chip.F71889F; - logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN; - break; - } break; - case 0x08: - switch (revision) { - case 0x14: - chip = Chip.F71869; - logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN; - break; - } break; - case 0x09: - switch (revision) { - case 0x09: - chip = Chip.F71889ED; - logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN; - break; - } break; - case 0x52: - switch (revision) { - case 0x17: - case 0x3A: - case 0x41: - chip = Chip.W83627HF; - logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN; - break; - } break; - case 0x82: - switch (revision & 0xF0) { - case 0x80: - chip = Chip.W83627THF; - logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN; - break; - } break; - case 0x85: - switch (revision) { - case 0x41: - chip = Chip.W83687THF; - logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN; - break; - } break; - case 0x88: - switch (revision & 0xF0) { - case 0x50: - case 0x60: - chip = Chip.W83627EHF; - logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN; - break; - } break; - case 0xA0: - switch (revision & 0xF0) { - case 0x20: - chip = Chip.W83627DHG; - logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN; - break; - } break; - case 0xA5: - switch (revision & 0xF0) { - case 0x10: - chip = Chip.W83667HG; - logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN; - break; - } break; - case 0xB0: - switch (revision & 0xF0) { - case 0x70: - chip = Chip.W83627DHGP; - logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN; - break; - } break; - case 0xB3: - switch (revision & 0xF0) { - case 0x50: - chip = Chip.W83667HGB; - logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN; - break; - } break; - } - if (chip == Chip.Unknown) { - if (id != 0 && id != 0xff) { - WinbondFintekExit(); + if (DetectIT87()) continue; - report.Append("Chip ID: Unknown Winbond / Fintek with ID 0x"); - report.AppendLine(((id << 8) | revision).ToString("X", - CultureInfo.InvariantCulture)); - report.AppendLine(); - } - } else { - - Select(logicalDeviceNumber); - ushort address = ReadWord(BASE_ADDRESS_REGISTER); - Thread.Sleep(1); - ushort verify = ReadWord(BASE_ADDRESS_REGISTER); - - ushort vendorID = ReadWord(FINTEK_VENDOR_ID_REGISTER); - - WinbondFintekExit(); - - if (address != verify) { - report.Append("Chip ID: 0x"); - report.AppendLine(chip.ToString("X")); - report.Append("Chip revision: 0x"); - report.AppendLine(revision.ToString("X", CultureInfo.InvariantCulture)); - report.AppendLine("Error: Address verification failed"); - report.AppendLine(); - return; - } - - // some Fintek chips have address register offset 0x05 added already - if ((address & 0x07) == 0x05) - address &= 0xFFF8; - - if (address < 0x100 || (address & 0xF007) != 0) { - report.Append("Chip ID: 0x"); - report.AppendLine(chip.ToString("X")); - report.Append("Chip revision: 0x"); - report.AppendLine(revision.ToString("X", CultureInfo.InvariantCulture)); - report.Append("Error: Invalid address 0x"); - report.AppendLine(address.ToString("X", CultureInfo.InvariantCulture)); - report.AppendLine(); - return; - } - - switch (chip) { - case Chip.W83627DHG: - case Chip.W83627DHGP: - case Chip.W83627EHF: - case Chip.W83627HF: - case Chip.W83627THF: - case Chip.W83667HG: - case Chip.W83667HGB: - case Chip.W83687THF: - superIOs.Add(new W836XX(chip, revision, address)); - break; - case Chip.F71858: - case Chip.F71862: - case Chip.F71869: - case Chip.F71882: - case Chip.F71889ED: - case Chip.F71889F: - if (vendorID != FINTEK_VENDOR_ID) { - report.Append("Chip ID: 0x"); - report.AppendLine(chip.ToString("X")); - report.Append("Chip revision: 0x"); - report.AppendLine(revision.ToString("X", CultureInfo.InvariantCulture)); - report.Append("Error: Invalid vendor ID 0x"); - report.AppendLine(vendorID.ToString("X", CultureInfo.InvariantCulture)); - report.AppendLine(); - return; - } - superIOs.Add(new F718XX(chip, address)); - break; - default: break; - } - - return; - } - - IT87Enter(); - - ushort chipID = ReadWord(CHIP_ID_REGISTER); - switch (chipID) { - case 0x8712: chip = Chip.IT8712F; break; - case 0x8716: chip = Chip.IT8716F; break; - case 0x8718: chip = Chip.IT8718F; break; - case 0x8720: chip = Chip.IT8720F; break; - case 0x8726: chip = Chip.IT8726F; break; - default: chip = Chip.Unknown; break; - } - if (chip == Chip.Unknown) { - if (chipID != 0 && chipID != 0xffff) { - IT87Exit(); - - report.Append("Chip ID: Unknown ITE with ID 0x"); - report.AppendLine(chipID.ToString("X", CultureInfo.InvariantCulture)); - report.AppendLine(); - } - } else { - Select(IT87_ENVIRONMENT_CONTROLLER_LDN); - ushort address = ReadWord(BASE_ADDRESS_REGISTER); - Thread.Sleep(1); - ushort verify = ReadWord(BASE_ADDRESS_REGISTER); - - byte version = (byte)(ReadByte(IT87_CHIP_VERSION_REGISTER) & 0x0F); - - IT87Exit(); - - if (address != verify || address < 0x100 || (address & 0xF007) != 0) { - report.Append("Chip ID: 0x"); - report.AppendLine(chip.ToString("X")); - report.Append("Error: Invalid address 0x"); - report.AppendLine(address.ToString("X", CultureInfo.InvariantCulture)); - report.AppendLine(); - return; - } - - superIOs.Add(new IT87XX(chip, address, version)); - - return; - } - - SMSCEnter(); - - chipID = ReadWord(CHIP_ID_REGISTER); - switch (chipID) { - default: chip = Chip.Unknown; break; - } - if (chip == Chip.Unknown) { - if (chipID != 0 && chipID != 0xffff) { - SMSCExit(); - - report.Append("Chip ID: Unknown SMSC with ID 0x"); - report.AppendLine(chipID.ToString("X", CultureInfo.InvariantCulture)); - report.AppendLine(); - } - } else { - SMSCExit(); - - return; - } + if (DetectSMSC()) continue; } } diff --git a/Hardware/LPC/W836XX.cs b/Hardware/LPC/W836XX.cs index 6f25a53..8d918b2 100644 --- a/Hardware/LPC/W836XX.cs +++ b/Hardware/LPC/W836XX.cs @@ -178,7 +178,7 @@ namespace OpenHardwareMonitor.Hardware.LPC { return vendorId == WINBOND_VENDOR_ID; } - private ulong SetBit(ulong target, int bit, int value) { + private static ulong SetBit(ulong target, int bit, int value) { if ((value & 1) != value) throw new ArgumentException("Value must be one bit only."); diff --git a/Hardware/Mainboard/Mainboard.cs b/Hardware/Mainboard/Mainboard.cs index d256b17..e31fddf 100644 --- a/Hardware/Mainboard/Mainboard.cs +++ b/Hardware/Mainboard/Mainboard.cs @@ -53,8 +53,7 @@ namespace OpenHardwareMonitor.Hardware.Mainboard { this.smbios = new SMBIOS(); if (smbios.Board != null) { - if (smbios.Board.ProductName != null - && smbios.Board.ProductName != "") { + if (!string.IsNullOrEmpty(smbios.Board.ProductName)) { if (smbios.Board.Manufacturer == Manufacturer.Unknown) this.name = smbios.Board.ProductName; else @@ -131,8 +130,9 @@ namespace OpenHardwareMonitor.Hardware.Mainboard { #pragma warning restore 67 public void Accept(IVisitor visitor) { - if (visitor != null) - visitor.VisitHardware(this); + if (visitor == null) + throw new ArgumentNullException("visitor"); + visitor.VisitHardware(this); } public void Traverse(IVisitor visitor) { diff --git a/Hardware/Mainboard/SMBIOS.cs b/Hardware/Mainboard/SMBIOS.cs index d914a57..16fb2dc 100644 --- a/Hardware/Mainboard/SMBIOS.cs +++ b/Hardware/Mainboard/SMBIOS.cs @@ -51,7 +51,7 @@ namespace OpenHardwareMonitor.Hardware.Mainboard { private BIOSInformation biosInformation = null; private BaseBoardInformation baseBoardInformation = null; - private string ReadSysFS(string path) { + private static string ReadSysFS(string path) { try { if (File.Exists(path)) { using (StreamReader reader = new StreamReader(path)) @@ -85,9 +85,12 @@ namespace OpenHardwareMonitor.Hardware.Mainboard { raw = null; try { - ManagementObjectCollection collection = + ManagementObjectCollection collection; + using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\WMI", - "SELECT SMBiosData FROM MSSMBios_RawSMBiosTables").Get(); + "SELECT SMBiosData FROM MSSMBios_RawSMBiosTables")) { + collection = searcher.Get(); + } foreach (ManagementObject mo in collection) { raw = (byte[])mo["SMBiosData"]; @@ -144,17 +147,19 @@ namespace OpenHardwareMonitor.Hardware.Mainboard { public string GetReport() { StringBuilder r = new StringBuilder(); - if (biosInformation != null) { - r.Append("BIOS Vendor: "); r.AppendLine(biosInformation.Vendor); - r.Append("BIOS Version: "); r.AppendLine(biosInformation.Version); + if (BIOS != null) { + r.Append("BIOS Vendor: "); r.AppendLine(BIOS.Vendor); + r.Append("BIOS Version: "); r.AppendLine(BIOS.Version); r.AppendLine(); } - if (baseBoardInformation != null) { + if (Board != null) { r.Append("Mainboard Manufacturer: "); - r.AppendLine(baseBoardInformation.ManufacturerName); + r.AppendLine(Board.ManufacturerName); r.Append("Mainboard Name: "); - r.AppendLine(baseBoardInformation.ProductName); + r.AppendLine(Board.ProductName); + r.Append("Mainboard Version: "); + r.AppendLine(Board.Version); r.AppendLine(); } @@ -249,10 +254,10 @@ namespace OpenHardwareMonitor.Hardware.Mainboard { private Manufacturer manufacturer; private Model model; - private void SetManufacturerName(string manufacturerName) { - this.manufacturerName = manufacturerName; + private void SetManufacturerName(string name) { + this.manufacturerName = name; - switch (manufacturerName) { + switch (name) { case "ASRock": manufacturer = Manufacturer.ASRock; break; case "ASUSTeK Computer INC.": @@ -286,10 +291,10 @@ namespace OpenHardwareMonitor.Hardware.Mainboard { } } - private void SetProductName(string productName) { - this.productName = productName; + private void SetProductName(string name) { + this.productName = name; - switch (productName) { + switch (name) { case "880GMH/USB3": model = Model._880GMH_USB3; break; case "Crosshair III Formula": diff --git a/Hardware/Mainboard/SuperIOHardware.cs b/Hardware/Mainboard/SuperIOHardware.cs index 2c5c57f..32e613f 100644 --- a/Hardware/Mainboard/SuperIOHardware.cs +++ b/Hardware/Mainboard/SuperIOHardware.cs @@ -44,39 +44,18 @@ namespace OpenHardwareMonitor.Hardware.Mainboard { internal class SuperIOHardware : Hardware { private ISuperIO superIO; - protected readonly string name; + private string name; private List voltages = new List(); private List temperatures = new List(); - private List fans = new List(); + private List fans = new List(); + public SuperIOHardware(ISuperIO superIO, Manufacturer manufacturer, Model model, ISettings settings) { this.superIO = superIO; - - switch (superIO.Chip) { - case Chip.F71858: name = "Fintek F71858"; break; - case Chip.F71862: name = "Fintek F71862"; break; - case Chip.F71869: name = "Fintek F71869"; break; - case Chip.F71882: name = "Fintek F71882"; break; - case Chip.F71889ED: name = "Fintek F71889ED"; break; - case Chip.F71889F: name = "Fintek F71889F"; break; - case Chip.IT8712F: this.name = "ITE IT8712F"; break; - case Chip.IT8716F: this.name = "ITE IT8716F"; break; - case Chip.IT8718F: this.name = "ITE IT8718F"; break; - case Chip.IT8720F: this.name = "ITE IT8720F"; break; - case Chip.IT8726F: this.name = "ITE IT8726F"; break; - case Chip.W83627DHG: this.name = "Winbond W83627DHG"; break; - case Chip.W83627DHGP: this.name = "Winbond W83627DHG-P"; break; - case Chip.W83627EHF: this.name = "Winbond W83627EHF"; break; - case Chip.W83627HF: this.name = "Winbond W83627HF"; break; - case Chip.W83627THF: this.name = "Winbond W83627THF"; break; - case Chip.W83667HG: this.name = "Winbond W83667HG"; break; - case Chip.W83667HGB: this.name = "Winbond W83667HG-B"; break; - case Chip.W83687THF: this.name = "Winbond W83687THF"; break; - case Chip.Unknown: this.name = "Unkown"; break; - } + this.name = ChipName.GetName(superIO.Chip); List v = new List(); List t = new List(); @@ -483,7 +462,6 @@ namespace OpenHardwareMonitor.Hardware.Mainboard { v.Add(new Voltage("Standby +3.3V", 7, 34, 34, 0)); v.Add(new Voltage("VBAT", 8, 34, 34, 0)); t.Add(new Temperature("CPU", 0)); - t.Add(new Temperature("Auxiliary", 1, true)); t.Add(new Temperature("Motherboard", 2)); f.Add(new Fan("Chassis Fan #1", 0)); f.Add(new Fan("CPU Fan", 1)); @@ -673,15 +651,10 @@ namespace OpenHardwareMonitor.Hardware.Mainboard { private class Temperature { public readonly string Name; public readonly int Index; - public readonly bool Hidden; - public Temperature(string name, int index) : - this(name, index, false) { } - - public Temperature(string name, int index, bool hidden) { + public Temperature(string name, int index) { this.Name = name; this.Index = index; - this.Hidden = hidden; } } diff --git a/Hardware/Nvidia/NVAPI.cs b/Hardware/Nvidia/NVAPI.cs index 41277a6..bed595c 100644 --- a/Hardware/Nvidia/NVAPI.cs +++ b/Hardware/Nvidia/NVAPI.cs @@ -310,7 +310,9 @@ namespace OpenHardwareMonitor.Hardware.Nvidia { public static readonly NvAPI_GPU_GetMemoryInfoDelegate NvAPI_GPU_GetMemoryInfo; public static readonly NvAPI_GetDisplayDriverVersionDelegate - NvAPI_GetDisplayDriverVersion; + NvAPI_GetDisplayDriverVersion; + + private NVAPI() { } public static NvStatus NvAPI_GPU_GetFullName(NvPhysicalGpuHandle gpuHandle, out string name) { diff --git a/Hardware/Nvidia/NvidiaGPU.cs b/Hardware/Nvidia/NvidiaGPU.cs index e70278c..10e5a3c 100644 --- a/Hardware/Nvidia/NvidiaGPU.cs +++ b/Hardware/Nvidia/NvidiaGPU.cs @@ -239,7 +239,8 @@ namespace OpenHardwareMonitor.Hardware.Nvidia { r.Append("Driver Version: "); r.Append(driverVersion.DriverVersion / 100); r.Append("."); - r.Append((driverVersion.DriverVersion % 100).ToString("00", CultureInfo.InvariantCulture)); + r.Append((driverVersion.DriverVersion % 100).ToString("00", + CultureInfo.InvariantCulture)); r.AppendLine(); r.Append("Driver Branch: "); r.AppendLine(driverVersion.BuildBranch); diff --git a/Hardware/PInvokeDelegateFactory.cs b/Hardware/PInvokeDelegateFactory.cs index 92aae5c..7ec192e 100644 --- a/Hardware/PInvokeDelegateFactory.cs +++ b/Hardware/PInvokeDelegateFactory.cs @@ -45,25 +45,15 @@ namespace OpenHardwareMonitor.Hardware { internal sealed class PInvokeDelegateFactory { - private static AssemblyBuilder assemblyBuilder; - private static ModuleBuilder moduleBuilder; + private static ModuleBuilder moduleBuilder = + AppDomain.CurrentDomain.DefineDynamicAssembly( + new AssemblyName("PInvokeDelegateFactoryInternalAssembly"), + AssemblyBuilderAccess.Run).DefineDynamicModule( + "PInvokeDelegateFactoryInternalModule"); private static IDictionary wrapperTypes = new Dictionary(); - static PInvokeDelegateFactory() { - - AssemblyName assemblyName = new AssemblyName(); - assemblyName.Name = "PInvokeDelegateFactoryInternalAssembly"; - - assemblyBuilder = - AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, - AssemblyBuilderAccess.Run); - - moduleBuilder = assemblyBuilder.DefineDynamicModule( - "PInvokeDelegateFactoryInternalModule"); - } - private PInvokeDelegateFactory() { } public static void CreateDelegate(DllImportAttribute dllImportAttribute, diff --git a/Hardware/Parameter.cs b/Hardware/Parameter.cs index 9a96747..aaafe02 100644 --- a/Hardware/Parameter.cs +++ b/Hardware/Parameter.cs @@ -41,7 +41,7 @@ using System.Collections.Generic; namespace OpenHardwareMonitor.Hardware { - public struct ParameterDescription { + internal struct ParameterDescription { private string name; private string description; private float defaultValue; @@ -60,7 +60,7 @@ namespace OpenHardwareMonitor.Hardware { public float DefaultValue { get { return defaultValue; } } } - public class Parameter : IParameter { + internal class Parameter : IParameter { private ISensor sensor; private ParameterDescription description; private float value; @@ -129,8 +129,9 @@ namespace OpenHardwareMonitor.Hardware { } public void Accept(IVisitor visitor) { - if (visitor != null) - visitor.VisitParameter(this); + if (visitor == null) + throw new ArgumentNullException("visitor"); + visitor.VisitParameter(this); } public void Traverse(IVisitor visitor) { } diff --git a/Hardware/Sensor.cs b/Hardware/Sensor.cs index bb2723b..5f2e7d2 100644 --- a/Hardware/Sensor.cs +++ b/Hardware/Sensor.cs @@ -51,9 +51,9 @@ namespace OpenHardwareMonitor.Hardware { private SensorType sensorType; private IHardware hardware; private ReadOnlyArray parameters; - private float? value; - private float? min; - private float? max; + private float? currentValue; + private float? minValue; + private float? maxValue; private Queue values = new Queue(MAX_MINUTES * 15); private ISettings settings; @@ -114,7 +114,7 @@ namespace OpenHardwareMonitor.Hardware { return name; } set { - if (value != "") + if (!string.IsNullOrEmpty(value)) name = value; else name = defaultName; @@ -136,7 +136,7 @@ namespace OpenHardwareMonitor.Hardware { public float? Value { get { - return value; + return currentValue; } set { while (values.Count > 0 && @@ -153,23 +153,23 @@ namespace OpenHardwareMonitor.Hardware { } } - this.value = value; - if (min > value || !min.HasValue) - min = value; - if (max < value || !max.HasValue) - max = value; + this.currentValue = value; + if (minValue > value || !minValue.HasValue) + minValue = value; + if (maxValue < value || !maxValue.HasValue) + maxValue = value; } } - public float? Min { get { return min; } } - public float? Max { get { return max; } } + public float? Min { get { return minValue; } } + public float? Max { get { return maxValue; } } public void ResetMin() { - min = null; + minValue = null; } public void ResetMax() { - max = null; + maxValue = null; } public IEnumerable Values { @@ -177,8 +177,9 @@ namespace OpenHardwareMonitor.Hardware { } public void Accept(IVisitor visitor) { - if (visitor != null) - visitor.VisitSensor(this); + if (visitor == null) + throw new ArgumentNullException("visitor"); + visitor.VisitSensor(this); } public void Traverse(IVisitor visitor) { diff --git a/Hardware/SensorVisitor.cs b/Hardware/SensorVisitor.cs index b467937..530d6f0 100644 --- a/Hardware/SensorVisitor.cs +++ b/Hardware/SensorVisitor.cs @@ -45,14 +45,20 @@ namespace OpenHardwareMonitor.Hardware { private SensorEventHandler handler; public SensorVisitor(SensorEventHandler handler) { + if (handler == null) + throw new ArgumentNullException("handler"); this.handler = handler; } public void VisitComputer(IComputer computer) { + if (computer == null) + throw new ArgumentNullException("computer"); computer.Traverse(this); } public void VisitHardware(IHardware hardware) { + if (hardware == null) + throw new ArgumentNullException("hardware"); hardware.Traverse(this); } diff --git a/Hardware/TBalancer/FTD2XX.cs b/Hardware/TBalancer/FTD2XX.cs index de0ef3f..37fba9d 100644 --- a/Hardware/TBalancer/FTD2XX.cs +++ b/Hardware/TBalancer/FTD2XX.cs @@ -128,18 +128,44 @@ namespace OpenHardwareMonitor.Hardware.TBalancer { public delegate FT_STATUS FT_ReadDelegate(FT_HANDLE handle, [Out] byte[] buffer, uint bytesToRead, out uint bytesReturned); - public static FT_CreateDeviceInfoListDelegate FT_CreateDeviceInfoList; - public static FT_GetDeviceInfoListDelegate FT_GetDeviceInfoList; - public static FT_OpenDelegate FT_Open; - public static FT_CloseDelegate FT_Close; - public static FT_SetBaudRateDelegate FT_SetBaudRate; - public static FT_SetDataCharacteristicsDelegate FT_SetDataCharacteristics; - public static FT_SetFlowControlDelegate FT_SetFlowControl; - public static FT_SetTimeoutsDelegate FT_SetTimeouts; - public static FT_WriteDelegate FT_Write; - public static FT_PurgeDelegate FT_Purge; - public static FT_GetStatusDelegate FT_GetStatus; - public static FT_ReadDelegate FT_Read; + public static readonly FT_CreateDeviceInfoListDelegate + FT_CreateDeviceInfoList = CreateDelegate< + FT_CreateDeviceInfoListDelegate>("FT_CreateDeviceInfoList"); + public static readonly FT_GetDeviceInfoListDelegate + FT_GetDeviceInfoList = CreateDelegate< + FT_GetDeviceInfoListDelegate>("FT_GetDeviceInfoList"); + public static readonly FT_OpenDelegate + FT_Open = CreateDelegate< + FT_OpenDelegate>("FT_Open"); + public static readonly FT_CloseDelegate + FT_Close = CreateDelegate< + FT_CloseDelegate>("FT_Close"); + public static readonly FT_SetBaudRateDelegate + FT_SetBaudRate = CreateDelegate< + FT_SetBaudRateDelegate>("FT_SetBaudRate"); + public static readonly FT_SetDataCharacteristicsDelegate + FT_SetDataCharacteristics = CreateDelegate< + FT_SetDataCharacteristicsDelegate>("FT_SetDataCharacteristics"); + public static readonly FT_SetFlowControlDelegate + FT_SetFlowControl = CreateDelegate< + FT_SetFlowControlDelegate>("FT_SetFlowControl"); + public static readonly FT_SetTimeoutsDelegate + FT_SetTimeouts = CreateDelegate< + FT_SetTimeoutsDelegate>("FT_SetTimeouts"); + public static readonly FT_WriteDelegate + FT_Write = CreateDelegate< + FT_WriteDelegate>("FT_Write"); + public static readonly FT_PurgeDelegate + FT_Purge = CreateDelegate< + FT_PurgeDelegate>("FT_Purge"); + public static readonly FT_GetStatusDelegate + FT_GetStatus = CreateDelegate< + FT_GetStatusDelegate>("FT_GetStatus"); + public static readonly FT_ReadDelegate + FT_Read = CreateDelegate< + FT_ReadDelegate>("FT_Read"); + + private FTD2XX() { } public static FT_STATUS Write(FT_HANDLE handle, byte[] buffer) { uint bytesWritten; @@ -168,40 +194,27 @@ namespace OpenHardwareMonitor.Hardware.TBalancer { uint bytesReturned; FT_STATUS status = FT_Read(handle, buffer, 1, out bytesReturned); if (status != FT_STATUS.FT_OK || bytesReturned != 1) - throw new Exception(); + throw new InvalidOperationException(); return buffer[0]; } - private static string dllName; + private static string GetDllName() { + int p = (int)System.Environment.OSVersion.Platform; + if ((p == 4) || (p == 128)) + return "libftd2xx.so"; + else + return "ftd2xx.dll"; + } - private static void GetDelegate(string entryPoint, out T newDelegate) + private static T CreateDelegate(string entryPoint) where T : class { - DllImportAttribute attribute = new DllImportAttribute(dllName); + DllImportAttribute attribute = new DllImportAttribute(GetDllName()); attribute.CallingConvention = CallingConvention.StdCall; attribute.PreserveSig = true; attribute.EntryPoint = entryPoint; + T newDelegate; PInvokeDelegateFactory.CreateDelegate(attribute, out newDelegate); - } - - static FTD2XX() { - int p = (int)System.Environment.OSVersion.Platform; - if ((p == 4) || (p == 128)) - dllName = "libftd2xx.so"; - else - dllName = "ftd2xx.dll"; - - GetDelegate("FT_CreateDeviceInfoList", out FT_CreateDeviceInfoList); - GetDelegate("FT_GetDeviceInfoList", out FT_GetDeviceInfoList); - GetDelegate("FT_Open", out FT_Open); - GetDelegate("FT_Close", out FT_Close); - GetDelegate("FT_SetBaudRate", out FT_SetBaudRate); - GetDelegate("FT_SetDataCharacteristics", out FT_SetDataCharacteristics); - GetDelegate("FT_SetFlowControl", out FT_SetFlowControl); - GetDelegate("FT_SetTimeouts", out FT_SetTimeouts); - GetDelegate("FT_Write", out FT_Write); - GetDelegate("FT_Purge", out FT_Purge); - GetDelegate("FT_GetStatus", out FT_GetStatus); - GetDelegate("FT_Read", out FT_Read); + return newDelegate; } } } diff --git a/Hardware/TBalancer/TBalancer.cs b/Hardware/TBalancer/TBalancer.cs index b0fc7c7..9cd0ccb 100644 --- a/Hardware/TBalancer/TBalancer.cs +++ b/Hardware/TBalancer/TBalancer.cs @@ -161,8 +161,6 @@ namespace OpenHardwareMonitor.Hardware.TBalancer { } for (int i = 0; i < 2; i++) { - float maxRPM = 20.0f * data[offset + 44 + 2 * i]; - if (miniNGFans[number * 2 + i] == null) miniNGFans[number * 2 + i] = new Sensor("miniNG #" + (number + 1) + " Fan Channel " + (i + 1), @@ -376,8 +374,9 @@ namespace OpenHardwareMonitor.Hardware.TBalancer { public event SensorEventHandler SensorRemoved; public void Accept(IVisitor visitor) { - if (visitor != null) - visitor.VisitHardware(this); + if (visitor == null) + throw new ArgumentNullException("visitor"); + visitor.VisitHardware(this); } public void Traverse(IVisitor visitor) { } diff --git a/Hardware/WinRing0.cs b/Hardware/WinRing0.cs index f577863..ef22563 100644 --- a/Hardware/WinRing0.cs +++ b/Hardware/WinRing0.cs @@ -44,6 +44,8 @@ using System.Threading; namespace OpenHardwareMonitor.Hardware { internal class WinRing0 { + + private WinRing0() { } public enum OlsDllStatus{ OLS_DLL_NO_ERROR = 0, @@ -56,7 +58,7 @@ namespace OpenHardwareMonitor.Hardware { } private static bool available = false; - public static Mutex isaBusMutex; + private static Mutex isaBusMutex; private static string GetDllName() { int p = (int)System.Environment.OSVersion.Platform; @@ -78,10 +80,7 @@ namespace OpenHardwareMonitor.Hardware { private delegate bool InitializeOlsDelegate(); private delegate void DeinitializeOlsDelegate(); - public delegate uint GetDllStatusDelegate(); public delegate bool IsCpuidDelegate(); - public delegate bool CpuidDelegate(uint index, uint ecxValue, - out uint eax, out uint ebx, out uint ecx, out uint edx); public delegate bool CpuidTxDelegate(uint index, uint ecxValue, out uint eax, out uint ebx, out uint ecx, out uint edx, UIntPtr threadAffinityMask); @@ -90,7 +89,6 @@ namespace OpenHardwareMonitor.Hardware { UIntPtr threadAffinityMask); public delegate byte ReadIoPortByteDelegate(ushort port); public delegate void WriteIoPortByteDelegate(ushort port, byte value); - public delegate void SetPciMaxBusIndexDelegate(byte max); public delegate uint FindPciDeviceByIdDelegate(ushort vendorId, ushort deviceId, byte index); public delegate bool ReadPciConfigDwordExDelegate(uint pciAddress, @@ -101,85 +99,73 @@ namespace OpenHardwareMonitor.Hardware { UIntPtr threadAffinityMask); public delegate bool RdtscDelegate(out uint eax, out uint edx); - private static InitializeOlsDelegate InitializeOls; - private static DeinitializeOlsDelegate DeinitializeOls; + private static InitializeOlsDelegate InitializeOls = + CreateDelegate("InitializeOls"); + private static DeinitializeOlsDelegate DeinitializeOls = + CreateDelegate("DeinitializeOls"); - public static readonly GetDllStatusDelegate GetDllStatus; - public static readonly IsCpuidDelegate IsCpuid; - public static readonly CpuidDelegate Cpuid; - public static readonly CpuidTxDelegate CpuidTx; - public static readonly RdmsrDelegate Rdmsr; - public static readonly RdmsrTxDelegate RdmsrTx; - public static readonly ReadIoPortByteDelegate ReadIoPortByte; - public static readonly WriteIoPortByteDelegate WriteIoPortByte; - public static readonly SetPciMaxBusIndexDelegate SetPciMaxBusIndex; - public static readonly FindPciDeviceByIdDelegate FindPciDeviceById; - public static readonly ReadPciConfigDwordExDelegate ReadPciConfigDwordEx; - public static readonly WritePciConfigDwordExDelegate WritePciConfigDwordEx; - public static readonly RdtscTxDelegate RdtscTx; - public static readonly RdtscDelegate Rdtsc; - - - - private static void GetDelegate(string entryPoint, out T newDelegate) - where T : class - { + public static readonly IsCpuidDelegate IsCpuid = + CreateDelegate("IsCpuid"); + public static readonly CpuidTxDelegate CpuidTx = + CreateDelegate("CpuidTx"); + public static readonly RdmsrDelegate Rdmsr = + CreateDelegate("Rdmsr"); + public static readonly RdmsrTxDelegate RdmsrTx = + CreateDelegate("RdmsrTx"); + public static readonly ReadIoPortByteDelegate ReadIoPortByte = + CreateDelegate("ReadIoPortByte"); + public static readonly WriteIoPortByteDelegate WriteIoPortByte = + CreateDelegate("WriteIoPortByte"); + public static readonly FindPciDeviceByIdDelegate FindPciDeviceById = + CreateDelegate("FindPciDeviceById"); + public static readonly ReadPciConfigDwordExDelegate ReadPciConfigDwordEx = + CreateDelegate("ReadPciConfigDwordEx"); + public static readonly WritePciConfigDwordExDelegate WritePciConfigDwordEx = + CreateDelegate("WritePciConfigDwordEx"); + public static readonly RdtscTxDelegate RdtscTx = + CreateDelegate("RdtscTx"); + public static readonly RdtscDelegate Rdtsc = + CreateDelegate("Rdtsc"); + + private static T CreateDelegate(string entryPoint) where T : class { DllImportAttribute attribute = new DllImportAttribute(GetDllName()); attribute.CallingConvention = CallingConvention.Winapi; attribute.PreserveSig = true; attribute.EntryPoint = entryPoint; attribute.CharSet = CharSet.Auto; - PInvokeDelegateFactory.CreateDelegate(attribute, out newDelegate); + T result; + PInvokeDelegateFactory.CreateDelegate(attribute, out result); + return result; } - static WinRing0() { - GetDelegate("InitializeOls", out InitializeOls); - GetDelegate("DeinitializeOls", out DeinitializeOls); - GetDelegate("GetDllStatus", out GetDllStatus); - GetDelegate("IsCpuid", out IsCpuid); - GetDelegate("Cpuid", out Cpuid); - GetDelegate("CpuidTx", out CpuidTx); - GetDelegate("Rdmsr", out Rdmsr); - GetDelegate("RdmsrTx", out RdmsrTx); - GetDelegate("ReadIoPortByte", out ReadIoPortByte); - GetDelegate("WriteIoPortByte", out WriteIoPortByte); - GetDelegate("SetPciMaxBusIndex", out SetPciMaxBusIndex); - GetDelegate("FindPciDeviceById", out FindPciDeviceById); - GetDelegate("ReadPciConfigDwordEx", out ReadPciConfigDwordEx); - GetDelegate("WritePciConfigDwordEx", out WritePciConfigDwordEx); - GetDelegate("RdtscTx", out RdtscTx); - GetDelegate("Rdtsc", out Rdtsc); - + public static void Open() { try { if (InitializeOls != null && InitializeOls()) available = true; - } catch (DllNotFoundException) { } - + } catch (DllNotFoundException) { } + isaBusMutex = new Mutex(false, "Access_ISABUS.HTP.Method"); } - + public static bool IsAvailable { get { return available; } } + public static void Close() { + if (available) + DeinitializeOls(); + isaBusMutex.Close(); + } + public static bool WaitIsaBusMutex(int millisecondsTimeout) { try { - return isaBusMutex.WaitOne(millisecondsTimeout); - } catch { return false; } + return isaBusMutex.WaitOne(millisecondsTimeout, false); + } catch (AbandonedMutexException) { return false; } + catch (InvalidOperationException) { return false; } } public static void ReleaseIsaBusMutex() { isaBusMutex.ReleaseMutex(); - } - - private static Deinitializer deinitializer = new Deinitializer(); - private class Deinitializer { - ~Deinitializer() { - if (available) - DeinitializeOls(); - - isaBusMutex.Close(); - } - } + } } } diff --git a/OpenHardwareMonitorLib.csproj b/OpenHardwareMonitorLib.csproj index cb846d2..acd6f1a 100644 --- a/OpenHardwareMonitorLib.csproj +++ b/OpenHardwareMonitorLib.csproj @@ -41,6 +41,7 @@ prompt 4 AllRules.ruleset + false none diff --git a/Properties/AssemblyLibInfo.cs b/Properties/AssemblyLibInfo.cs index 2d40f7e..a4a1eab 100644 --- a/Properties/AssemblyLibInfo.cs +++ b/Properties/AssemblyLibInfo.cs @@ -50,4 +50,5 @@ using System.Runtime.InteropServices; [assembly: AssemblyCulture("")] [assembly: ComVisible(false)] -[assembly: CLSCompliant(true)] \ No newline at end of file +[assembly: CLSCompliant(true)] +