More refactoring.
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 List<SMART.DriveAttribute> attributes =
72 new List<SMART.DriveAttribute>(SMART.ReadSmart(handle, drive));
74 if (!(attributes.Count > 0)) {
75 SMART.CloseHandle(handle);
79 SMART.SSDLifeID ssdLifeID = GetSSDLifeID(attributes);
80 if (ssdLifeID == SMART.SSDLifeID.None) {
81 SMART.AttributeID temperatureID = GetTemperatureIndex(attributes);
83 if (temperatureID != 0x00) {
84 hardware.Add(new HDD(name, handle, drive, temperatureID, settings));
88 hardware.Add(new HDD(name, handle, drive, ssdLifeID, settings));
92 SMART.CloseHandle(handle);
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)
105 if (!isKnownSSD) return SMART.SSDLifeID.None;
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++) {
111 switch ((int)attributes[i].ID) {
113 return SMART.SSDLifeID.Samsung;
115 return SMART.SSDLifeID.SandForce;
117 return SMART.SSDLifeID.Indilinx;
121 // TODO: Find out JMicron's Life attribute ID; their unique ID = 0xE4
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
132 intelRegisterCount++;
135 return (intelRegisterCount == 3)
136 ? SMART.SSDLifeID.Intel
137 : SMART.SSDLifeID.None;
140 private SMART.AttributeID GetTemperatureIndex(
141 List<SMART.DriveAttribute> attributes)
143 SMART.AttributeID[] validIds = new[] {
144 SMART.AttributeID.Temperature,
145 SMART.AttributeID.DriveTemperature,
146 SMART.AttributeID.AirflowTemperature
149 foreach (SMART.AttributeID validId in validIds) {
150 SMART.AttributeID id = validId;
151 if (attributes.Exists(attr => attr.ID == id))
158 public IHardware[] Hardware {
160 return hardware.ToArray();
164 public string GetReport() {
165 int p = (int)Environment.OSVersion.Platform;
166 if (p == 4 || p == 128) return null;
168 StringBuilder r = new StringBuilder();
170 r.AppendLine("S.M.A.R.T Data");
173 for (int drive = 0; drive < MAX_DRIVES; drive++) {
174 IntPtr handle = SMART.OpenPhysicalDrive(drive);
176 if (handle == SMART.INVALID_HANDLE_VALUE)
179 if (!SMART.EnableSmart(handle, drive)) {
180 SMART.CloseHandle(handle);
184 string name = SMART.ReadName(handle, drive);
186 SMART.CloseHandle(handle);
190 List<SMART.DriveAttribute> attributes = SMART.ReadSmart(handle, drive);
192 if (attributes != null) {
193 r.AppendLine("Drive name: " + name);
195 r.AppendFormat(CultureInfo.InvariantCulture, " {0}{1}{2}{3}{4}{5}",
197 ("RawValue").PadRight(20),
198 ("WorstValue").PadRight(12),
199 ("AttrValue").PadRight(12),
201 Environment.NewLine);
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),
212 Environment.NewLine);
217 SMART.CloseHandle(handle);
223 public void Close() {
224 foreach (HDD hdd in hardware)