Hardware/Computer.cs
author moel.mich
Sun, 23 Sep 2012 18:37:43 +0000
changeset 380 573f1fff48b2
parent 360 c1a4377c11d1
permissions -rw-r--r--
Fixed Issue 387. The new implementation does not try to start a ring 0 driver that already exists, but could not be opened. It tries to delete the driver and install it new. The driver is now stored temporarily in the application folder. The driver is not correctly removed on system shutdown.
     1 /*
     2  
     3   This Source Code Form is subject to the terms of the Mozilla Public
     4   License, v. 2.0. If a copy of the MPL was not distributed with this
     5   file, You can obtain one at http://mozilla.org/MPL/2.0/.
     6  
     7   Copyright (C) 2009-2012 Michael Möller <mmoeller@openhardwaremonitor.org>
     8 	
     9 */
    10 
    11 using System;
    12 using System.Collections.Generic;
    13 using System.Globalization;
    14 using System.IO;
    15 using System.Security.Permissions;
    16 using System.Reflection;
    17 
    18 namespace OpenHardwareMonitor.Hardware {
    19 
    20   public class Computer : IComputer {
    21 
    22     private readonly List<IGroup> groups = new List<IGroup>();
    23     private readonly ISettings settings;
    24 
    25     private SMBIOS smbios;
    26 
    27     private bool open;
    28 
    29     private bool mainboardEnabled;
    30     private bool cpuEnabled;
    31     private bool ramEnabled;
    32     private bool gpuEnabled;
    33     private bool fanControllerEnabled;
    34     private bool hddEnabled;    
    35 
    36     public Computer() {
    37       this.settings = new Settings();
    38     }
    39 
    40     public Computer(ISettings settings) {
    41       this.settings = settings ?? new Settings();
    42     }
    43 
    44     private void Add(IGroup group) {
    45       if (groups.Contains(group))
    46         return;
    47 
    48       groups.Add(group);
    49 
    50       if (HardwareAdded != null)
    51         foreach (IHardware hardware in group.Hardware)
    52           HardwareAdded(hardware);
    53     }
    54 
    55     private void Remove(IGroup group) {
    56       if (!groups.Contains(group))
    57         return;
    58 
    59       groups.Remove(group);
    60 
    61       if (HardwareRemoved != null)
    62         foreach (IHardware hardware in group.Hardware)
    63           HardwareRemoved(hardware);
    64 
    65       group.Close();
    66     }
    67 
    68     private void RemoveType<T>() where T : IGroup {
    69       List<IGroup> list = new List<IGroup>();
    70       foreach (IGroup group in groups)
    71         if (group is T)
    72           list.Add(group);
    73       foreach (IGroup group in list)
    74         Remove(group);
    75     }
    76 
    77     [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
    78     public void Open() {
    79       if (open)
    80         return;
    81 
    82       this.smbios = new SMBIOS();
    83 
    84       Ring0.Open();
    85       Opcode.Open();
    86 
    87       if (mainboardEnabled)
    88         Add(new Mainboard.MainboardGroup(smbios, settings));
    89       
    90       if (cpuEnabled)
    91         Add(new CPU.CPUGroup(settings));
    92 
    93       if (ramEnabled)
    94         Add(new RAM.RAMGroup(smbios, settings));
    95 
    96       if (gpuEnabled) {
    97         Add(new ATI.ATIGroup(settings));
    98         Add(new Nvidia.NvidiaGroup(settings));
    99       }
   100 
   101       if (fanControllerEnabled) {
   102         Add(new TBalancer.TBalancerGroup(settings));
   103         Add(new Heatmaster.HeatmasterGroup(settings));
   104       }
   105 
   106       if (hddEnabled)
   107         Add(new HDD.HarddriveGroup(settings));
   108 
   109       open = true;
   110     }
   111 
   112     public bool MainboardEnabled {
   113       get { return mainboardEnabled; }
   114 
   115       [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
   116       set {
   117         if (open && value != mainboardEnabled) {
   118           if (value)
   119             Add(new Mainboard.MainboardGroup(smbios, settings));
   120           else
   121             RemoveType<Mainboard.MainboardGroup>();
   122         }
   123         mainboardEnabled = value;
   124       }
   125     }
   126 
   127     public bool CPUEnabled {
   128       get { return cpuEnabled; }
   129 
   130       [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
   131       set {
   132         if (open && value != cpuEnabled) {
   133           if (value)
   134             Add(new CPU.CPUGroup(settings));
   135           else
   136             RemoveType<CPU.CPUGroup>();
   137         }
   138         cpuEnabled = value;
   139       }
   140     }
   141 
   142     public bool RAMEnabled {
   143       get { return ramEnabled; }
   144 
   145       [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
   146       set {
   147         if (open && value != ramEnabled) {
   148           if (value)
   149             Add(new RAM.RAMGroup(smbios, settings));
   150           else
   151             RemoveType<RAM.RAMGroup>();
   152         }
   153         ramEnabled = value;
   154       }
   155     }    
   156 
   157     public bool GPUEnabled {
   158       get { return gpuEnabled; }
   159 
   160       [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
   161       set {
   162         if (open && value != gpuEnabled) {
   163           if (value) {
   164             Add(new ATI.ATIGroup(settings));
   165             Add(new Nvidia.NvidiaGroup(settings));
   166           } else {
   167             RemoveType<ATI.ATIGroup>();
   168             RemoveType<Nvidia.NvidiaGroup>();
   169           }
   170         }
   171         gpuEnabled = value;
   172       }
   173     }
   174 
   175     public bool FanControllerEnabled {
   176       get { return fanControllerEnabled; }
   177 
   178       [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
   179       set {
   180         if (open && value != fanControllerEnabled) {
   181           if (value) {
   182             Add(new TBalancer.TBalancerGroup(settings));
   183             Add(new Heatmaster.HeatmasterGroup(settings));
   184           } else {
   185             RemoveType<TBalancer.TBalancerGroup>();
   186             RemoveType<Heatmaster.HeatmasterGroup>();
   187           }
   188         }
   189         fanControllerEnabled = value;
   190       }
   191     }
   192 
   193     public bool HDDEnabled {
   194       get { return hddEnabled; }
   195 
   196       [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
   197       set {
   198         if (open && value != hddEnabled) {
   199           if (value)
   200             Add(new HDD.HarddriveGroup(settings));
   201           else
   202             RemoveType<HDD.HarddriveGroup>();
   203         }
   204         hddEnabled = value;
   205       }
   206     }
   207 
   208     public IHardware[] Hardware {
   209       get {
   210         List<IHardware> list = new List<IHardware>();
   211         foreach (IGroup group in groups)
   212           foreach (IHardware hardware in group.Hardware)
   213             list.Add(hardware);
   214         return list.ToArray();
   215       }
   216     }
   217 
   218     private static void NewSection(TextWriter writer) {
   219       for (int i = 0; i < 8; i++)
   220         writer.Write("----------");
   221       writer.WriteLine();
   222       writer.WriteLine();
   223     }
   224 
   225     private static int CompareSensor(ISensor a, ISensor b) {
   226       int c = a.SensorType.CompareTo(b.SensorType);
   227       if (c == 0)
   228         return a.Index.CompareTo(b.Index);
   229       else
   230         return c;
   231     }
   232 
   233     private static void ReportHardwareSensorTree(
   234       IHardware hardware, TextWriter w, string space) 
   235     {
   236       w.WriteLine("{0}|", space);
   237       w.WriteLine("{0}+- {1} ({2})",
   238         space, hardware.Name, hardware.Identifier);
   239       ISensor[] sensors = hardware.Sensors;
   240       Array.Sort(sensors, CompareSensor);
   241       foreach (ISensor sensor in sensors) {
   242         w.WriteLine("{0}|  +- {1,-14} : {2,8:G6} {3,8:G6} {4,8:G6} ({5})", 
   243           space, sensor.Name, sensor.Value, sensor.Min, sensor.Max, 
   244           sensor.Identifier);
   245       }
   246       foreach (IHardware subHardware in hardware.SubHardware)
   247         ReportHardwareSensorTree(subHardware, w, "|  ");
   248     }
   249 
   250     private static void ReportHardwareParameterTree(
   251       IHardware hardware, TextWriter w, string space) {
   252       w.WriteLine("{0}|", space);
   253       w.WriteLine("{0}+- {1} ({2})",
   254         space, hardware.Name, hardware.Identifier);
   255       ISensor[] sensors = hardware.Sensors;
   256       Array.Sort(sensors, CompareSensor);
   257       foreach (ISensor sensor in sensors) {
   258         string innerSpace = space + "|  ";
   259         if (sensor.Parameters.Length > 0) {
   260           w.WriteLine("{0}|", innerSpace);
   261           w.WriteLine("{0}+- {1} ({2})",
   262             innerSpace, sensor.Name, sensor.Identifier);
   263           foreach (IParameter parameter in sensor.Parameters) {
   264             string innerInnerSpace = innerSpace + "|  ";
   265             w.WriteLine("{0}+- {1} : {2}",
   266               innerInnerSpace, parameter.Name,
   267               string.Format(CultureInfo.InvariantCulture, "{0} : {1}",
   268                 parameter.DefaultValue, parameter.Value));
   269           }
   270         }
   271       }
   272       foreach (IHardware subHardware in hardware.SubHardware)
   273         ReportHardwareParameterTree(subHardware, w, "|  ");
   274     }
   275 
   276     private static void ReportHardware(IHardware hardware, TextWriter w) {
   277       string hardwareReport = hardware.GetReport();
   278       if (!string.IsNullOrEmpty(hardwareReport)) {
   279         NewSection(w);
   280         w.Write(hardwareReport);
   281       }
   282       foreach (IHardware subHardware in hardware.SubHardware)
   283         ReportHardware(subHardware, w);
   284     }
   285 
   286     public string GetReport() {
   287 
   288       using (StringWriter w = new StringWriter(CultureInfo.InvariantCulture)) {
   289 
   290         w.WriteLine();
   291         w.WriteLine("Open Hardware Monitor Report");
   292         w.WriteLine();
   293 
   294         Version version = typeof(Computer).Assembly.GetName().Version;
   295 
   296         NewSection(w);
   297         w.Write("Version: "); w.WriteLine(version.ToString());
   298         w.WriteLine();
   299 
   300         NewSection(w);
   301         w.Write("Common Language Runtime: ");
   302         w.WriteLine(Environment.Version.ToString());
   303         w.Write("Operating System: ");
   304         w.WriteLine(Environment.OSVersion.ToString());
   305         w.Write("Process Type: ");
   306         w.WriteLine(IntPtr.Size == 4 ? "32-Bit" : "64-Bit");
   307         w.WriteLine();
   308 
   309         string r = Ring0.GetReport();
   310         if (r != null) {
   311           NewSection(w);
   312           w.Write(r);
   313           w.WriteLine();
   314         }
   315 
   316         NewSection(w);
   317         w.WriteLine("Sensors");
   318         w.WriteLine();
   319         foreach (IGroup group in groups) {
   320           foreach (IHardware hardware in group.Hardware)
   321             ReportHardwareSensorTree(hardware, w, "");
   322         }
   323         w.WriteLine();
   324 
   325         NewSection(w);
   326         w.WriteLine("Parameters");
   327         w.WriteLine();
   328         foreach (IGroup group in groups) {
   329           foreach (IHardware hardware in group.Hardware)
   330             ReportHardwareParameterTree(hardware, w, "");
   331         }
   332         w.WriteLine();
   333 
   334         foreach (IGroup group in groups) {
   335           string report = group.GetReport();
   336           if (!string.IsNullOrEmpty(report)) {
   337             NewSection(w);
   338             w.Write(report);
   339           }
   340 
   341           IHardware[] hardwareArray = group.Hardware;
   342           foreach (IHardware hardware in hardwareArray)
   343             ReportHardware(hardware, w);
   344 
   345         }
   346         return w.ToString();
   347       }
   348     }
   349 
   350     public void Close() {      
   351       if (!open)
   352         return;
   353 
   354       while (groups.Count > 0) {
   355         IGroup group = groups[groups.Count - 1];
   356         Remove(group);         
   357       } 
   358 
   359       Opcode.Close();
   360       Ring0.Close();
   361 
   362       this.smbios = null;
   363 
   364       open = false;
   365     }
   366 
   367     public event HardwareEventHandler HardwareAdded;
   368     public event HardwareEventHandler HardwareRemoved;
   369 
   370     public void Accept(IVisitor visitor) {
   371       if (visitor == null)
   372         throw new ArgumentNullException("visitor");
   373       visitor.VisitComputer(this);
   374     }
   375 
   376     public void Traverse(IVisitor visitor) {
   377       foreach (IGroup group in groups)
   378         foreach (IHardware hardware in group.Hardware) 
   379           hardware.Accept(visitor);
   380     }
   381 
   382     private class Settings : ISettings {
   383 
   384       public bool Contains(string name) {
   385         return false;
   386       }
   387 
   388       public void SetValue(string name, string value) { }
   389 
   390       public string GetValue(string name, string value) {
   391         return value;
   392       }
   393 
   394       public void Remove(string name) { }
   395     }
   396   }
   397 }