Hardware/HDD/HDDGroup.cs
author moel.mich
Sun, 17 Oct 2010 17:12:38 +0000
changeset 231 30f5a06f5d8a
parent 218 194186efdde9
child 233 c5139c236200
permissions -rw-r--r--
Changed the SMART AttributeID type from an enum to a struct.
     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.AttributeID ssdLifeID = GetSSDLifeID(attributes);
    80         if (ssdLifeID == SMART.AttributeID.None) {
    81           SMART.AttributeID temperatureID = GetTemperatureIndex(attributes);
    82 
    83           if (temperatureID != SMART.AttributeID.None) {
    84             hardware.Add(new HDD(name, handle, drive, temperatureID, 
    85               SMART.AttributeID.None, settings));
    86             continue;
    87           }
    88         } else {
    89           hardware.Add(new HDD(name, handle, drive, SMART.AttributeID.None, 
    90             ssdLifeID, settings));
    91           continue;
    92         }
    93         
    94         SMART.CloseHandle(handle);
    95       }
    96     }
    97 
    98     private SMART.AttributeID GetSSDLifeID(List<SMART.DriveAttribute> attributes) {
    99       // ID E9 is present on Intel, JM, SF and Samsung
   100       // ID D2 is present on Indilinx
   101       // Neither ID has been found on a mechanical hard drive (yet),
   102       // So this seems like a good way to check if it's an SSD.
   103       bool isKnownSSD = (
   104         attributes.Exists(attr => attr.ID == new SMART.AttributeID(0xE9)) ||
   105         attributes.Exists(attr => attr.ID == new SMART.AttributeID(0xD2))
   106       );
   107 
   108       if (!isKnownSSD) return SMART.AttributeID.None;
   109 
   110       // We start with a traditional loop, because there are 4 unique ID's
   111       // that potentially identify one of the vendors
   112       for (int i = 0; i < attributes.Count; i++) {
   113 
   114         if (attributes[i].ID == SMART.SamsungAttributes.RemainingLife)
   115           return SMART.SamsungAttributes.RemainingLife;
   116         else if (attributes[i].ID == new SMART.AttributeID(0xAB))          
   117           return  SMART.SandForceAttributes.RemainingLife;
   118         else if (attributes[i].ID == new SMART.AttributeID(0xD2))   
   119           return SMART.IndilinxAttributes.RemainingLife;        
   120       }
   121 
   122       // TODO: Find out JMicron's Life attribute ID; their unique ID = 0xE4
   123 
   124       // For Intel, we make sure we have their 3 most important ID's
   125       // We do a traditional loop again, because we all we need to know
   126       // is whether we can find all 3; pointless to use Exists()
   127       int intelRegisterCount = 0;
   128       foreach (SMART.DriveAttribute attribute in attributes) {
   129         if (attribute.ID == new SMART.AttributeID(0xE1) ||
   130           attribute.ID == new SMART.AttributeID(0xE8) ||
   131           attribute.ID == new SMART.AttributeID(0xE9)
   132         )
   133           intelRegisterCount++;
   134       }
   135 
   136       return (intelRegisterCount == 3)
   137         ? SMART.IntelAttributes.RemainingLife
   138         : SMART.AttributeID.None;
   139     }
   140 
   141     private SMART.AttributeID GetTemperatureIndex(
   142       List<SMART.DriveAttribute> attributes)
   143     {
   144       SMART.AttributeID[] validIds = new[] {
   145         SMART.CommonAttributes.Temperature,
   146         SMART.CommonAttributes.DriveTemperature,
   147         SMART.CommonAttributes.AirflowTemperature
   148       };
   149 
   150       foreach (SMART.AttributeID validId in validIds) {
   151         SMART.AttributeID id = validId;
   152         if (attributes.Exists(attr => attr.ID == id))
   153           return validId;
   154       }
   155 
   156       return SMART.AttributeID.None;
   157     }
   158 
   159     public IHardware[] Hardware {
   160       get {
   161         return hardware.ToArray();
   162       }
   163     }
   164 
   165     public string GetReport() {
   166       int p = (int)Environment.OSVersion.Platform;
   167       if (p == 4 || p == 128) return null;
   168 
   169       StringBuilder r = new StringBuilder();
   170 
   171       r.AppendLine("S.M.A.R.T Data");
   172       r.AppendLine();
   173 
   174       for (int drive = 0; drive < MAX_DRIVES; drive++) {
   175         IntPtr handle = SMART.OpenPhysicalDrive(drive);
   176 
   177         if (handle == SMART.INVALID_HANDLE_VALUE)
   178           continue;
   179 
   180         if (!SMART.EnableSmart(handle, drive)) {
   181           SMART.CloseHandle(handle);
   182           continue;
   183         }
   184 
   185         string name = SMART.ReadName(handle, drive);
   186         if (name == null) {
   187           SMART.CloseHandle(handle);
   188           continue;
   189         }
   190 
   191         List<SMART.DriveAttribute> attributes = SMART.ReadSmart(handle, drive);
   192 
   193         if (attributes != null) {
   194           r.AppendLine("Drive name: " + name);
   195           r.AppendLine();
   196           r.AppendFormat(CultureInfo.InvariantCulture, " {0}{1}{2}{3}{4}{5}",
   197             ("ID").PadRight(6),
   198             ("RawValue").PadRight(20),
   199             ("WorstValue").PadRight(12),
   200             ("AttrValue").PadRight(12),
   201             ("Name"),
   202             Environment.NewLine);
   203 
   204           foreach (SMART.DriveAttribute a in attributes) {
   205             if (a.ID == SMART.AttributeID.None) continue;
   206             string raw = BitConverter.ToString(a.RawValue);
   207             r.AppendFormat(CultureInfo.InvariantCulture, " {0}{1}{2}{3}{4}{5}",
   208               a.ID.ToString("d").PadRight(6), 
   209               raw.Replace("-", " ").PadRight(20),
   210               a.WorstValue.ToString(CultureInfo.InvariantCulture).PadRight(12),
   211               a.AttrValue.ToString(CultureInfo.InvariantCulture).PadRight(12),
   212               a.ID,
   213               Environment.NewLine);
   214           }
   215           r.AppendLine();
   216         }
   217 
   218         SMART.CloseHandle(handle);
   219       }
   220 
   221       return r.ToString();
   222     }
   223 
   224     public void Close() {
   225       foreach (HDD hdd in hardware) 
   226         hdd.Close();
   227     }
   228   }
   229 }