Hardware/HDD/HDDGroup.cs
author paulwerelds
Sun, 17 Oct 2010 19:25:50 +0000
changeset 233 c5139c236200
parent 231 30f5a06f5d8a
child 250 c19d56a0bcad
permissions -rw-r--r--
Updated attribute maps for the 4 SSD controllers we figured out so far.
Also switched back to an array of DriveAttribute rather than a generic list.
     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         SMART.DriveAttribute[] attributes = SMART.ReadSmart(handle, drive);
    72         
    73         if (attributes.Length < 1) {
    74           SMART.CloseHandle(handle);
    75           continue;
    76         }
    77 
    78         SMART.AttributeID ssdLifeID = GetSSDLifeID(attributes);
    79         if (ssdLifeID == SMART.AttributeID.None) {
    80           SMART.AttributeID temperatureID = GetTemperatureIndex(attributes);
    81 
    82           if (temperatureID != SMART.AttributeID.None) {
    83             hardware.Add(new HDD(name, handle, drive, temperatureID, 
    84               SMART.AttributeID.None, settings));
    85             continue;
    86           }
    87         } else {
    88           hardware.Add(new HDD(name, handle, drive, SMART.AttributeID.None, 
    89             ssdLifeID, settings));
    90           continue;
    91         }
    92         
    93         SMART.CloseHandle(handle);
    94       }
    95     }
    96 
    97     private SMART.AttributeID GetSSDLifeID(SMART.DriveAttribute[] attributes) {
    98       // ID E9 is present on Intel, JM, SF and Samsung (different meanings)
    99       // ID D2 is present on Indilinx
   100       // Neither ID has been found on a mechanical hard drive (yet),
   101       // so this seems like a good way to check if it's an SSD.
   102       bool isKnownSSD = (
   103         Array.Exists(attributes, attr => attr.ID == new SMART.AttributeID(0xE9)) ||
   104         Array.Exists(attributes, attr => attr.ID == new SMART.AttributeID(0xD2))
   105       );
   106 
   107       if (!isKnownSSD) return SMART.AttributeID.None;
   108 
   109       // We start with a traditional loop, because there are 4 unique ID's
   110       // that potentially identify one of the vendors
   111       for (int i = 0; i < attributes.Length; i++) {
   112         if (attributes[i].ID == SMART.SamsungAttributes.RemainingLife)
   113           return SMART.SamsungAttributes.RemainingLife;
   114         
   115         if (attributes[i].ID == SMART.SandForceAttributes.ProgramFailCount)
   116           return  SMART.SandForceAttributes.RemainingLife;
   117         
   118         if (attributes[i].ID == SMART.IndilinxAttributes.UnknownUnique)   
   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 == SMART.IntelAttributes.HostWrites ||
   130           attribute.ID == SMART.IntelAttributes.RemainingLife ||
   131           attribute.ID == SMART.IntelAttributes.MediaWearOutIndicator
   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       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 (Array.Exists(attributes, 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         SMART.DriveAttribute[] attributes = SMART.ReadSmart(handle, drive);
   192 
   193         if (attributes.Length > 0) {
   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 }