moel@1: /* moel@1: moel@1: Version: MPL 1.1/GPL 2.0/LGPL 2.1 moel@1: moel@1: The contents of this file are subject to the Mozilla Public License Version moel@1: 1.1 (the "License"); you may not use this file except in compliance with moel@1: the License. You may obtain a copy of the License at moel@1: moel@1: http://www.mozilla.org/MPL/ moel@1: moel@1: Software distributed under the License is distributed on an "AS IS" basis, moel@1: WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License moel@1: for the specific language governing rights and limitations under the License. moel@1: moel@1: The Original Code is the Open Hardware Monitor code. moel@1: moel@1: The Initial Developer of the Original Code is moel@1: Michael Möller . moel@1: Portions created by the Initial Developer are Copyright (C) 2009-2010 moel@1: the Initial Developer. All Rights Reserved. moel@1: paulwerelds@204: Contributor(s): Paul Werelds moel@1: moel@1: Alternatively, the contents of this file may be used under the terms of moel@1: either the GNU General Public License Version 2 or later (the "GPL"), or moel@1: the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), moel@1: in which case the provisions of the GPL or the LGPL are applicable instead moel@1: of those above. If you wish to allow use of your version of this file only moel@1: under the terms of either the GPL or the LGPL, and not to allow others to moel@1: use your version of this file under the terms of the MPL, indicate your moel@1: decision by deleting the provisions above and replace them with the notice moel@1: and other provisions required by the GPL or the LGPL. If you do not delete moel@1: the provisions above, a recipient may use your version of this file under moel@1: the terms of any one of the MPL, the GPL or the LGPL. moel@1: moel@1: */ moel@1: moel@1: using System; moel@1: using System.Collections.Generic; moel@205: using System.Globalization; paulwerelds@204: using System.Text; moel@1: moel@1: namespace OpenHardwareMonitor.Hardware.HDD { moel@165: internal class HDDGroup : IGroup { moel@1: moel@1: private const int MAX_DRIVES = 32; moel@1: moel@195: private readonly List hardware = new List(); moel@1: moel@165: public HDDGroup(ISettings settings) { paulwerelds@204: int p = (int)Environment.OSVersion.Platform; paulwerelds@204: if (p == 4 || p == 128) return; moel@1: paulwerelds@204: for (int drive = 0; drive < MAX_DRIVES; drive++) { paulwerelds@204: IntPtr handle = SMART.OpenPhysicalDrive(drive); moel@1: paulwerelds@204: if (handle == SMART.INVALID_HANDLE_VALUE) paulwerelds@204: continue; paulwerelds@204: paulwerelds@204: if (!SMART.EnableSmart(handle, drive)) { paulwerelds@204: SMART.CloseHandle(handle); paulwerelds@204: continue; paulwerelds@204: } paulwerelds@204: paulwerelds@204: string name = SMART.ReadName(handle, drive); paulwerelds@204: if (name == null) { paulwerelds@204: SMART.CloseHandle(handle); paulwerelds@204: continue; paulwerelds@204: } paulwerelds@204: paulwerelds@204: SMART.DriveAttribute[] attributes = SMART.ReadSmart(handle, drive); moel@205: if (attributes == null) { moel@205: SMART.CloseHandle(handle); moel@205: continue; moel@205: } paulwerelds@204: moel@205: int attribute = -1; paulwerelds@204: moel@205: // search for the Temperature attribute moel@205: for (int i = 0; i < attributes.Length; i++) moel@205: if (attributes[i].ID == SMART.AttributeID.Temperature) { moel@205: attribute = i; moel@205: break; moel@205: } moel@205: moel@205: // if no temperature attribute is found, search for DriveTemperature moel@205: if (attribute == -1) moel@205: for (int i = 0; i < attributes.Length; i++) moel@205: if (attributes[i].ID == SMART.AttributeID.DriveTemperature) { paulwerelds@204: attribute = i; paulwerelds@204: break; paulwerelds@204: } paulwerelds@204: moel@205: // if no temperature attribute is found, search for AirflowTemperature moel@205: if (attribute == -1) moel@205: for (int i = 0; i < attributes.Length; i++) moel@205: if (attributes[i].ID == SMART.AttributeID.AirflowTemperature) { moel@205: attribute = i; moel@205: break; moel@205: } moel@205: moel@205: if (attribute >= 0) { moel@205: hardware.Add(new HDD(name, handle, drive, attribute, settings)); moel@205: continue; paulwerelds@204: } moel@1: paulwerelds@204: SMART.CloseHandle(handle); moel@1: } moel@1: } moel@1: moel@1: public IHardware[] Hardware { moel@1: get { moel@1: return hardware.ToArray(); moel@1: } moel@1: } moel@1: moel@1: public string GetReport() { paulwerelds@204: int p = (int)Environment.OSVersion.Platform; paulwerelds@204: if (p == 4 || p == 128) return null; paulwerelds@204: paulwerelds@204: StringBuilder r = new StringBuilder(); paulwerelds@204: paulwerelds@204: r.AppendLine("S.M.A.R.T Data"); paulwerelds@204: r.AppendLine(); paulwerelds@204: paulwerelds@204: for (int drive = 0; drive < MAX_DRIVES; drive++) { paulwerelds@204: IntPtr handle = SMART.OpenPhysicalDrive(drive); paulwerelds@204: paulwerelds@204: if (handle == SMART.INVALID_HANDLE_VALUE) paulwerelds@204: continue; paulwerelds@204: paulwerelds@204: if (!SMART.EnableSmart(handle, drive)) { paulwerelds@204: SMART.CloseHandle(handle); paulwerelds@204: continue; paulwerelds@204: } paulwerelds@204: paulwerelds@204: string name = SMART.ReadName(handle, drive); paulwerelds@204: if (name == null) { paulwerelds@204: SMART.CloseHandle(handle); paulwerelds@204: continue; paulwerelds@204: } paulwerelds@204: paulwerelds@204: SMART.DriveAttribute[] attributes = SMART.ReadSmart(handle, drive); paulwerelds@204: paulwerelds@204: if (attributes != null) { paulwerelds@204: r.AppendLine("Drive name: " + name); paulwerelds@204: r.AppendLine(); moel@205: r.AppendFormat(CultureInfo.InvariantCulture, " {0}{1}{2}{3}{4}{5}", moel@205: ("ID").PadRight(6), moel@205: ("RawValue").PadRight(20), moel@205: ("WorstValue").PadRight(12), moel@205: ("AttrValue").PadRight(12), moel@205: ("Name"), moel@205: Environment.NewLine); paulwerelds@204: moel@205: foreach (SMART.DriveAttribute a in attributes) { moel@205: if (a.ID == 0) continue; moel@205: string raw = BitConverter.ToString(a.RawValue); moel@205: r.AppendFormat(CultureInfo.InvariantCulture, " {0}{1}{2}{3}{4}{5}", moel@205: a.ID.ToString("d").PadRight(6), moel@205: raw.Replace("-", " ").PadRight(20), moel@205: a.WorstValue.ToString(CultureInfo.InvariantCulture).PadRight(12), moel@205: a.AttrValue.ToString(CultureInfo.InvariantCulture).PadRight(12), moel@205: a.ID, moel@205: Environment.NewLine); paulwerelds@204: } paulwerelds@204: r.AppendLine(); paulwerelds@204: } paulwerelds@204: paulwerelds@204: SMART.CloseHandle(handle); paulwerelds@204: } paulwerelds@204: paulwerelds@204: return r.ToString(); moel@1: } moel@1: moel@1: public void Close() { moel@1: foreach (HDD hdd in hardware) moel@1: hdd.Close(); moel@1: } moel@1: } moel@1: }