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.
3 Version: MPL 1.1/GPL 2.0/LGPL 2.1
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
9 http://www.mozilla.org/MPL/
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.
15 The Original Code is the Open Hardware Monitor code.
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.
22 Contributor(s): Paul Werelds
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.
39 using System.Collections.Generic;
40 using System.Globalization;
43 namespace OpenHardwareMonitor.Hardware.HDD {
44 internal class HDDGroup : IGroup {
46 private const int MAX_DRIVES = 32;
48 private readonly List<HDD> hardware = new List<HDD>();
50 public HDDGroup(ISettings settings) {
51 int p = (int)Environment.OSVersion.Platform;
52 if (p == 4 || p == 128) return;
54 for (int drive = 0; drive < MAX_DRIVES; drive++) {
55 IntPtr handle = SMART.OpenPhysicalDrive(drive);
57 if (handle == SMART.INVALID_HANDLE_VALUE)
60 if (!SMART.EnableSmart(handle, drive)) {
61 SMART.CloseHandle(handle);
65 string name = SMART.ReadName(handle, drive);
67 SMART.CloseHandle(handle);
71 SMART.DriveAttribute[] attributes = SMART.ReadSmart(handle, drive);
73 if (attributes.Length < 1) {
74 SMART.CloseHandle(handle);
78 SMART.AttributeID ssdLifeID = GetSSDLifeID(attributes);
79 if (ssdLifeID == SMART.AttributeID.None) {
80 SMART.AttributeID temperatureID = GetTemperatureIndex(attributes);
82 if (temperatureID != SMART.AttributeID.None) {
83 hardware.Add(new HDD(name, handle, drive, temperatureID,
84 SMART.AttributeID.None, settings));
88 hardware.Add(new HDD(name, handle, drive, SMART.AttributeID.None,
89 ssdLifeID, settings));
93 SMART.CloseHandle(handle);
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.
103 Array.Exists(attributes, attr => attr.ID == new SMART.AttributeID(0xE9)) ||
104 Array.Exists(attributes, attr => attr.ID == new SMART.AttributeID(0xD2))
107 if (!isKnownSSD) return SMART.AttributeID.None;
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;
115 if (attributes[i].ID == SMART.SandForceAttributes.ProgramFailCount)
116 return SMART.SandForceAttributes.RemainingLife;
118 if (attributes[i].ID == SMART.IndilinxAttributes.UnknownUnique)
119 return SMART.IndilinxAttributes.RemainingLife;
122 // TODO: Find out JMicron's Life attribute ID; their unique ID = 0xE4
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
133 intelRegisterCount++;
136 return (intelRegisterCount == 3)
137 ? SMART.IntelAttributes.RemainingLife
138 : SMART.AttributeID.None;
141 private SMART.AttributeID GetTemperatureIndex(
142 SMART.DriveAttribute[] attributes)
144 SMART.AttributeID[] validIds = new[] {
145 SMART.CommonAttributes.Temperature,
146 SMART.CommonAttributes.DriveTemperature,
147 SMART.CommonAttributes.AirflowTemperature
150 foreach (SMART.AttributeID validId in validIds) {
151 SMART.AttributeID id = validId;
152 if (Array.Exists(attributes, attr => attr.ID == id))
156 return SMART.AttributeID.None;
159 public IHardware[] Hardware {
161 return hardware.ToArray();
165 public string GetReport() {
166 int p = (int)Environment.OSVersion.Platform;
167 if (p == 4 || p == 128) return null;
169 StringBuilder r = new StringBuilder();
171 r.AppendLine("S.M.A.R.T Data");
174 for (int drive = 0; drive < MAX_DRIVES; drive++) {
175 IntPtr handle = SMART.OpenPhysicalDrive(drive);
177 if (handle == SMART.INVALID_HANDLE_VALUE)
180 if (!SMART.EnableSmart(handle, drive)) {
181 SMART.CloseHandle(handle);
185 string name = SMART.ReadName(handle, drive);
187 SMART.CloseHandle(handle);
191 SMART.DriveAttribute[] attributes = SMART.ReadSmart(handle, drive);
193 if (attributes.Length > 0) {
194 r.AppendLine("Drive name: " + name);
196 r.AppendFormat(CultureInfo.InvariantCulture, " {0}{1}{2}{3}{4}{5}",
198 ("RawValue").PadRight(20),
199 ("WorstValue").PadRight(12),
200 ("AttrValue").PadRight(12),
202 Environment.NewLine);
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),
213 Environment.NewLine);
218 SMART.CloseHandle(handle);
224 public void Close() {
225 foreach (HDD hdd in hardware)