Hardware/HDD/AbstractHarddrive.cs
author moel.mich
Wed, 18 Jul 2012 22:47:30 +0000
changeset 368 1036b453f1f6
parent 344 3145aadca3d2
child 369 5077ed7ddca8
permissions -rw-r--r--
Added a missing default case in the mainboard specific configuration selection. The missing default case prevented sensor data from getting displayed on unknown Asus mainboards with NCT6779D super I/O.
     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 	Copyright (C) 2010 Paul Werelds
     9   Copyright (C) 2011 Roland Reinl <roland-reinl@gmx.de>
    10 	
    11 */
    12 
    13 using System;
    14 using System.Collections.Generic;
    15 using System.Globalization;
    16 using System.Text;
    17 using OpenHardwareMonitor.Collections;
    18 
    19 namespace OpenHardwareMonitor.Hardware.HDD {
    20   internal abstract class AbstractHarddrive : Hardware {
    21 
    22     private const int UPDATE_DIVIDER = 30; // update only every 30s
    23 
    24     // array of all harddrive types, matching type is searched in this order
    25     private static Type[] hddTypes = {       
    26       typeof(SSDPlextor),
    27       typeof(SSDIntel),
    28       typeof(SSDSandforce),
    29       typeof(SSDIndilinx),
    30       typeof(SSDSamsung),
    31       typeof(SSDMicron),
    32       typeof(GenericHarddisk)
    33     };
    34 
    35     private string firmwareRevision;
    36     private readonly ISmart smart;
    37 
    38     private readonly IntPtr handle;
    39     private readonly int index;
    40     private int count;
    41 
    42     private IList<SmartAttribute> smartAttributes;
    43     private IDictionary<SmartAttribute, Sensor> sensors;
    44 
    45     protected AbstractHarddrive(ISmart smart, string name, 
    46       string firmwareRevision, int index, 
    47       IEnumerable<SmartAttribute> smartAttributes, ISettings settings) 
    48       : base(name, new Identifier("hdd",
    49         index.ToString(CultureInfo.InvariantCulture)), settings)
    50     {
    51       this.firmwareRevision = firmwareRevision;
    52       this.smart = smart;
    53       handle = smart.OpenDrive(index);
    54 
    55       smart.EnableSmart(handle, index);
    56 
    57       this.index = index;
    58       this.count = 0;
    59 
    60       this.smartAttributes = new List<SmartAttribute>(smartAttributes);
    61 
    62       CreateSensors();
    63     }
    64 
    65     public static AbstractHarddrive CreateInstance(ISmart smart, 
    66       int driveIndex, ISettings settings) 
    67     {
    68       IntPtr deviceHandle = smart.OpenDrive(driveIndex);
    69 
    70       if (deviceHandle == smart.InvalidHandle) 
    71         return null;
    72 
    73       string name;
    74       string firmwareRevision;
    75       bool nameValid = smart.ReadNameAndFirmwareRevision(deviceHandle, 
    76         driveIndex, out name, out firmwareRevision);
    77       bool smartEnabled = smart.EnableSmart(deviceHandle, driveIndex);
    78 
    79       DriveAttributeValue[] values = {};
    80       if (smartEnabled)
    81         values = smart.ReadSmartData(deviceHandle, driveIndex);
    82 
    83       smart.CloseHandle(deviceHandle);
    84 
    85       if (!nameValid || string.IsNullOrEmpty(name)) 
    86         return null;
    87 
    88       foreach (Type type in hddTypes) {
    89         // get the array of name prefixes for the current type
    90         NamePrefixAttribute[] namePrefixes = type.GetCustomAttributes(
    91           typeof(NamePrefixAttribute), true) as NamePrefixAttribute[];
    92 
    93         // get the array of the required SMART attributes for the current type
    94         RequireSmartAttribute[] requiredAttributes = type.GetCustomAttributes(
    95           typeof(RequireSmartAttribute), true) as RequireSmartAttribute[];
    96 
    97         // check if all required attributes are present
    98         bool allRequiredAttributesFound = true;
    99         foreach (var requireAttribute in requiredAttributes) {
   100           bool adttributeFound = false;
   101           foreach (DriveAttributeValue value in values) {
   102             if (value.Identifier == requireAttribute.AttributeId) {
   103               adttributeFound = true;
   104               break;
   105             }
   106           }
   107           if (!adttributeFound) {
   108             allRequiredAttributesFound = false;
   109             break;
   110           }
   111         }
   112 
   113         // if an attribute is missing, then try the next type
   114         if (!allRequiredAttributesFound)
   115           continue;        
   116 
   117         // check if there is a matching name prefix for this type
   118         foreach (NamePrefixAttribute prefix in namePrefixes) {
   119           if (name.StartsWith(prefix.Prefix, StringComparison.InvariantCulture)) 
   120             return Activator.CreateInstance(type, smart, name, firmwareRevision,
   121               driveIndex, settings) as AbstractHarddrive;
   122         }
   123       }
   124 
   125       // no matching type has been found
   126       return null;
   127     }
   128 
   129     private void CreateSensors() {
   130       sensors = new Dictionary<SmartAttribute, Sensor>();
   131 
   132       IList<Pair<SensorType, int>> sensorTypeAndChannels = 
   133         new List<Pair<SensorType, int>>();
   134 
   135       DriveAttributeValue[] values = smart.ReadSmartData(handle, index);
   136 
   137       foreach (SmartAttribute attribute in smartAttributes) {
   138         if (!attribute.SensorType.HasValue) 
   139           continue;
   140 
   141         bool found = false;
   142         foreach (DriveAttributeValue value in values) {
   143           if (value.Identifier == attribute.Identifier) {
   144             found = true;
   145             break;
   146           }
   147         }
   148         if (!found)
   149           continue;
   150 
   151         Pair<SensorType, int> pair = new Pair<SensorType, int>(
   152           attribute.SensorType.Value, attribute.SensorChannel);
   153 
   154         if (!sensorTypeAndChannels.Contains(pair)) {
   155           Sensor sensor = new Sensor(attribute.Name, 
   156             attribute.SensorChannel, attribute.DefaultHiddenSensor, 
   157             attribute.SensorType.Value, this, null, settings);
   158 
   159           sensors.Add(attribute, sensor);
   160           ActivateSensor(sensor);
   161           sensorTypeAndChannels.Add(pair);
   162         }     
   163       }
   164     }
   165 
   166     public override HardwareType HardwareType {
   167       get { return HardwareType.HDD; }
   168     }
   169 
   170     public virtual void UpdateAdditionalSensors(DriveAttributeValue[] values) {}
   171 
   172     public override void Update() {
   173       if (count == 0) {
   174         DriveAttributeValue[] values = smart.ReadSmartData(handle, index);
   175 
   176         foreach (KeyValuePair<SmartAttribute, Sensor> keyValuePair in sensors) {
   177           SmartAttribute attribute = keyValuePair.Key;          
   178           foreach (DriveAttributeValue value in values) {
   179             if (value.Identifier == attribute.Identifier) {
   180               Sensor sensor = keyValuePair.Value;
   181               sensor.Value = attribute.ConvertValue(value);
   182             }
   183           }
   184         }
   185 
   186         UpdateAdditionalSensors(values);
   187       }
   188 
   189       count++; 
   190       count %= UPDATE_DIVIDER; 
   191     }
   192 
   193     public override string GetReport() {
   194       StringBuilder r = new StringBuilder();
   195       DriveAttributeValue[] values = smart.ReadSmartData(handle, index);
   196       DriveThresholdValue[] thresholds = 
   197         smart.ReadSmartThresholds(handle, index);
   198 
   199       if (values.Length > 0) {
   200         r.AppendLine(this.GetType().Name);
   201         r.AppendLine();
   202         r.AppendLine("Drive name: " + name);
   203         r.AppendLine("Firmware version: " + firmwareRevision);
   204         r.AppendLine();
   205         r.AppendFormat(CultureInfo.InvariantCulture, 
   206           " {0}{1}{2}{3}{4}{5}{6}{7}",
   207           ("ID").PadRight(3),
   208           ("Description").PadRight(35),
   209           ("Raw Value").PadRight(13),
   210           ("Worst").PadRight(6),
   211           ("Value").PadRight(6),
   212           ("Thres").PadRight(6),
   213           ("Physical").PadRight(8),
   214           Environment.NewLine);
   215 
   216         foreach (DriveAttributeValue value in values) {
   217           if (value.Identifier == 0x00) 
   218             break;
   219 
   220           byte? threshold = null;
   221           foreach (DriveThresholdValue t in thresholds) {
   222             if (t.Identifier == value.Identifier) {
   223               threshold = t.Threshold;
   224             }
   225           }
   226 
   227           string description = "Unknown";
   228           float? physical = null;
   229           foreach (SmartAttribute a in smartAttributes) {
   230             if (a.Identifier == value.Identifier) {
   231               description = a.Name;
   232               if (a.HasRawValueConversion | a.SensorType.HasValue)
   233                 physical = a.ConvertValue(value);
   234               else
   235                 physical = null;
   236             }
   237           }
   238 
   239           string raw = BitConverter.ToString(value.RawValue);
   240           r.AppendFormat(CultureInfo.InvariantCulture, 
   241             " {0}{1}{2}{3}{4}{5}{6}{7}",
   242             value.Identifier.ToString("X2").PadRight(3),
   243             description.PadRight(35),
   244             raw.Replace("-", "").PadRight(13),
   245             value.WorstValue.ToString(CultureInfo.InvariantCulture).PadRight(6),
   246             value.AttrValue.ToString(CultureInfo.InvariantCulture).PadRight(6),
   247             (threshold.HasValue ? threshold.Value.ToString(
   248               CultureInfo.InvariantCulture) : "-").PadRight(6),
   249             (physical.HasValue ? physical.Value.ToString(
   250               CultureInfo.InvariantCulture) : "-").PadRight(8),
   251             Environment.NewLine);
   252         }
   253         r.AppendLine();
   254       }
   255 
   256       return r.ToString();
   257     }
   258 
   259     protected static float RawToInt(byte[] raw, byte value) {
   260       return (raw[3] << 24) | (raw[2] << 16) | (raw[1] << 8) | raw[0];
   261     }
   262 
   263     public override void Close() {
   264       smart.CloseHandle(handle);
   265       base.Close();
   266     }
   267 
   268     public override void Traverse(IVisitor visitor) {
   269       foreach (ISensor sensor in Sensors)
   270         sensor.Accept(visitor);
   271     }
   272   }
   273 }