Hardware/HDD/HDDGroup.cs
author moel.mich
Thu, 14 Oct 2010 16:52:23 +0000
changeset 221 a950ba30d4dd
parent 205 a38b51ef489c
child 231 30f5a06f5d8a
permissions -rw-r--r--
Added a mainboard specific configuration for the ASRock P55 Deluxe.
     1 /*
     2   
     3   Version: MPL 1.1/GPL 2.0/LGPL 2.1
     4 
     5   The contents of this file are subject to the Mozilla Public License Version
     6   1.1 (the "License"); you may not use this file except in compliance with
     7   the License. You may obtain a copy of the License at
     8  
     9   http://www.mozilla.org/MPL/
    10 
    11   Software distributed under the License is distributed on an "AS IS" basis,
    12   WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
    13   for the specific language governing rights and limitations under the License.
    14 
    15   The Original Code is the Open Hardware Monitor code.
    16 
    17   The Initial Developer of the Original Code is 
    18   Michael Möller <m.moeller@gmx.ch>.
    19   Portions created by the Initial Developer are Copyright (C) 2009-2010
    20   the Initial Developer. All Rights Reserved.
    21 
    22   Contributor(s): Paul Werelds
    23 
    24   Alternatively, the contents of this file may be used under the terms of
    25   either the GNU General Public License Version 2 or later (the "GPL"), or
    26   the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
    27   in which case the provisions of the GPL or the LGPL are applicable instead
    28   of those above. If you wish to allow use of your version of this file only
    29   under the terms of either the GPL or the LGPL, and not to allow others to
    30   use your version of this file under the terms of the MPL, indicate your
    31   decision by deleting the provisions above and replace them with the notice
    32   and other provisions required by the GPL or the LGPL. If you do not delete
    33   the provisions above, a recipient may use your version of this file under
    34   the terms of any one of the MPL, the GPL or the LGPL.
    35  
    36 */
    37 
    38 using System;
    39 using System.Collections.Generic;
    40 using System.Globalization;
    41 using System.Text;
    42 
    43 namespace OpenHardwareMonitor.Hardware.HDD {
    44   internal class HDDGroup : IGroup {
    45 
    46     private const int MAX_DRIVES = 32;
    47 
    48     private readonly List<HDD> hardware = new List<HDD>();
    49 
    50     public HDDGroup(ISettings settings) {
    51       int p = (int)Environment.OSVersion.Platform;
    52       if (p == 4 || p == 128) return;
    53 
    54       for (int drive = 0; drive < MAX_DRIVES; drive++) {
    55         IntPtr handle = SMART.OpenPhysicalDrive(drive);
    56 
    57         if (handle == SMART.INVALID_HANDLE_VALUE)
    58           continue;
    59 
    60         if (!SMART.EnableSmart(handle, drive)) {
    61           SMART.CloseHandle(handle);
    62           continue;
    63         }
    64 
    65         string name = SMART.ReadName(handle, drive);
    66         if (name == null) {
    67           SMART.CloseHandle(handle);
    68           continue;
    69         }
    70 
    71         List<SMART.DriveAttribute> attributes =
    72           new List<SMART.DriveAttribute>(SMART.ReadSmart(handle, drive));
    73         
    74         if (!(attributes.Count > 0)) {
    75           SMART.CloseHandle(handle);
    76           continue;
    77         }
    78 
    79         SMART.SSDLifeID ssdLifeID = GetSSDLifeID(attributes);
    80         if (ssdLifeID == SMART.SSDLifeID.None) {
    81           SMART.AttributeID temperatureID = GetTemperatureIndex(attributes);
    82 
    83           if (temperatureID != 0x00) {
    84             hardware.Add(new HDD(name, handle, drive, temperatureID, settings));
    85             continue;
    86           }
    87         } else {
    88           hardware.Add(new HDD(name, handle, drive, ssdLifeID, settings));
    89           continue;
    90         }
    91         
    92         SMART.CloseHandle(handle);
    93       }
    94     }
    95 
    96     private SMART.SSDLifeID GetSSDLifeID(List<SMART.DriveAttribute> attributes) {
    97       // ID E9 is present on Intel, JM, SF and Samsung
    98       // ID D2 is present on Indilinx
    99       // Neither ID has been found on a mechanical hard drive (yet),
   100       // So this seems like a good way to check if it's an SSD.
   101       bool isKnownSSD = (attributes.Exists(attr => (int)attr.ID == 0xE9) ||
   102               attributes.Exists(attr => (int)attr.ID == 0xD2)
   103       );
   104 
   105       if (!isKnownSSD) return SMART.SSDLifeID.None;
   106 
   107       // We start with a traditional loop, because there are 4 unique ID's
   108       // that potentially identify one of the vendors
   109       for (int i = 0; i < attributes.Count; i++) {
   110 
   111         switch ((int)attributes[i].ID) {
   112           case 0xB4:
   113             return SMART.SSDLifeID.Samsung;
   114           case 0xAB:
   115             return SMART.SSDLifeID.SandForce;
   116           case 0xD2:
   117             return SMART.SSDLifeID.Indilinx;
   118         }
   119       }
   120 
   121       // TODO: Find out JMicron's Life attribute ID; their unique ID = 0xE4
   122 
   123       // For Intel, we make sure we have their 3 most important ID's
   124       // We do a traditional loop again, because we all we need to know
   125       // is whether we can find all 3; pointless to use Exists()
   126       int intelRegisterCount = 0;
   127       foreach (SMART.DriveAttribute attribute in attributes) {
   128         if ((int)attribute.ID == 0xE1 ||
   129           (int)attribute.ID == 0xE8 ||
   130           (int)attribute.ID == 0xE9
   131         )
   132           intelRegisterCount++;
   133       }
   134 
   135       return (intelRegisterCount == 3)
   136         ? SMART.SSDLifeID.Intel
   137         : SMART.SSDLifeID.None;
   138     }
   139 
   140     private SMART.AttributeID GetTemperatureIndex(
   141       List<SMART.DriveAttribute> attributes)
   142     {
   143       SMART.AttributeID[] validIds = new[] {
   144         SMART.AttributeID.Temperature,
   145         SMART.AttributeID.DriveTemperature,
   146         SMART.AttributeID.AirflowTemperature
   147       };
   148 
   149       foreach (SMART.AttributeID validId in validIds) {
   150         SMART.AttributeID id = validId;
   151         if (attributes.Exists(attr => attr.ID == id))
   152           return validId;
   153       }
   154 
   155       return 0x00;
   156     }
   157 
   158     public IHardware[] Hardware {
   159       get {
   160         return hardware.ToArray();
   161       }
   162     }
   163 
   164     public string GetReport() {
   165       int p = (int)Environment.OSVersion.Platform;
   166       if (p == 4 || p == 128) return null;
   167 
   168       StringBuilder r = new StringBuilder();
   169 
   170       r.AppendLine("S.M.A.R.T Data");
   171       r.AppendLine();
   172 
   173       for (int drive = 0; drive < MAX_DRIVES; drive++) {
   174         IntPtr handle = SMART.OpenPhysicalDrive(drive);
   175 
   176         if (handle == SMART.INVALID_HANDLE_VALUE)
   177           continue;
   178 
   179         if (!SMART.EnableSmart(handle, drive)) {
   180           SMART.CloseHandle(handle);
   181           continue;
   182         }
   183 
   184         string name = SMART.ReadName(handle, drive);
   185         if (name == null) {
   186           SMART.CloseHandle(handle);
   187           continue;
   188         }
   189 
   190         List<SMART.DriveAttribute> attributes = SMART.ReadSmart(handle, drive);
   191 
   192         if (attributes != null) {
   193           r.AppendLine("Drive name: " + name);
   194           r.AppendLine();
   195           r.AppendFormat(CultureInfo.InvariantCulture, " {0}{1}{2}{3}{4}{5}",
   196             ("ID").PadRight(6),
   197             ("RawValue").PadRight(20),
   198             ("WorstValue").PadRight(12),
   199             ("AttrValue").PadRight(12),
   200             ("Name"),
   201             Environment.NewLine);
   202 
   203           foreach (SMART.DriveAttribute a in attributes) {
   204             if (a.ID == 0) continue;
   205             string raw = BitConverter.ToString(a.RawValue);
   206             r.AppendFormat(CultureInfo.InvariantCulture, " {0}{1}{2}{3}{4}{5}",
   207               a.ID.ToString("d").PadRight(6), 
   208               raw.Replace("-", " ").PadRight(20),
   209               a.WorstValue.ToString(CultureInfo.InvariantCulture).PadRight(12),
   210               a.AttrValue.ToString(CultureInfo.InvariantCulture).PadRight(12),
   211               a.ID,
   212               Environment.NewLine);
   213           }
   214           r.AppendLine();
   215         }
   216 
   217         SMART.CloseHandle(handle);
   218       }
   219 
   220       return r.ToString();
   221     }
   222 
   223     public void Close() {
   224       foreach (HDD hdd in hardware) 
   225         hdd.Close();
   226     }
   227   }
   228 }