1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/Hardware/HDD/AbstractHarddrive.cs Sat Dec 31 17:31:04 2011 +0000
1.3 @@ -0,0 +1,294 @@
1.4 +/*
1.5 +
1.6 + Version: MPL 1.1/GPL 2.0/LGPL 2.1
1.7 +
1.8 + The contents of this file are subject to the Mozilla Public License Version
1.9 + 1.1 (the "License"); you may not use this file except in compliance with
1.10 + the License. You may obtain a copy of the License at
1.11 +
1.12 + http://www.mozilla.org/MPL/
1.13 +
1.14 + Software distributed under the License is distributed on an "AS IS" basis,
1.15 + WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
1.16 + for the specific language governing rights and limitations under the License.
1.17 +
1.18 + The Original Code is the Open Hardware Monitor code.
1.19 +
1.20 + The Initial Developer of the Original Code is
1.21 + Michael Möller <m.moeller@gmx.ch>.
1.22 + Portions created by the Initial Developer are Copyright (C) 2009-2011
1.23 + the Initial Developer. All Rights Reserved.
1.24 +
1.25 + Contributor(s):
1.26 + Paul Werelds
1.27 + Roland Reinl <roland-reinl@gmx.de>
1.28 +
1.29 + Alternatively, the contents of this file may be used under the terms of
1.30 + either the GNU General Public License Version 2 or later (the "GPL"), or
1.31 + the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
1.32 + in which case the provisions of the GPL or the LGPL are applicable instead
1.33 + of those above. If you wish to allow use of your version of this file only
1.34 + under the terms of either the GPL or the LGPL, and not to allow others to
1.35 + use your version of this file under the terms of the MPL, indicate your
1.36 + decision by deleting the provisions above and replace them with the notice
1.37 + and other provisions required by the GPL or the LGPL. If you do not delete
1.38 + the provisions above, a recipient may use your version of this file under
1.39 + the terms of any one of the MPL, the GPL or the LGPL.
1.40 +
1.41 +*/
1.42 +
1.43 +using System;
1.44 +using System.Collections.Generic;
1.45 +using System.Globalization;
1.46 +using System.Text;
1.47 +using OpenHardwareMonitor.Collections;
1.48 +
1.49 +namespace OpenHardwareMonitor.Hardware.HDD {
1.50 + internal abstract class AbstractHarddrive : Hardware {
1.51 +
1.52 + private const int UPDATE_DIVIDER = 30; // update only every 30s
1.53 +
1.54 + // array of all harddrive types, matching type is searched in this order
1.55 + private static Type[] hddTypes = {
1.56 + typeof(SSDPlextor),
1.57 + typeof(SSDIntel),
1.58 + typeof(SSDSandforce),
1.59 + typeof(SSDIndilinx),
1.60 + typeof(GenericHarddisk)
1.61 + };
1.62 +
1.63 + private readonly ISmart smart;
1.64 +
1.65 + private readonly IntPtr handle;
1.66 + private readonly int index;
1.67 + private int count;
1.68 +
1.69 + private IList<SmartAttribute> smartAttributes;
1.70 + private IDictionary<SmartAttribute, Sensor> sensors;
1.71 +
1.72 + protected AbstractHarddrive(ISmart smart, string name, int index,
1.73 + IEnumerable<SmartAttribute> smartAttributes, ISettings settings)
1.74 + : base(name, new Identifier("hdd",
1.75 + index.ToString(CultureInfo.InvariantCulture)), settings)
1.76 + {
1.77 + this.smart = smart;
1.78 + handle = smart.OpenDrive(index);
1.79 +
1.80 + smart.EnableSmart(handle, index);
1.81 +
1.82 + this.index = index;
1.83 + this.count = 0;
1.84 +
1.85 + this.smartAttributes = new List<SmartAttribute>(smartAttributes);
1.86 +
1.87 + CreateSensors();
1.88 + }
1.89 +
1.90 + public static AbstractHarddrive CreateInstance(ISmart smart,
1.91 + int driveIndex, ISettings settings)
1.92 + {
1.93 + IntPtr deviceHandle = smart.OpenDrive(driveIndex);
1.94 +
1.95 + if (deviceHandle == smart.InvalidHandle)
1.96 + return null;
1.97 +
1.98 + string name = smart.ReadName(deviceHandle, driveIndex);
1.99 + bool smartEnabled = smart.EnableSmart(deviceHandle, driveIndex);
1.100 +
1.101 + DriveAttributeValue[] values = {};
1.102 + if (smartEnabled)
1.103 + values = smart.ReadSmartData(deviceHandle, driveIndex);
1.104 +
1.105 + smart.CloseHandle(deviceHandle);
1.106 +
1.107 + if (string.IsNullOrEmpty(name))
1.108 + return null;
1.109 +
1.110 + foreach (Type type in hddTypes) {
1.111 + // get the array of name prefixes for the current type
1.112 + NamePrefixAttribute[] namePrefixes = type.GetCustomAttributes(
1.113 + typeof(NamePrefixAttribute), true) as NamePrefixAttribute[];
1.114 +
1.115 + // get the array of the required SMART attributes for the current type
1.116 + RequireSmartAttribute[] requiredAttributes = type.GetCustomAttributes(
1.117 + typeof(RequireSmartAttribute), true) as RequireSmartAttribute[];
1.118 +
1.119 + // check if all required attributes are present
1.120 + bool allRequiredAttributesFound = true;
1.121 + foreach (var requireAttribute in requiredAttributes) {
1.122 + bool adttributeFound = false;
1.123 + foreach (DriveAttributeValue value in values) {
1.124 + if (value.Identifier == requireAttribute.AttributeId) {
1.125 + adttributeFound = true;
1.126 + break;
1.127 + }
1.128 + }
1.129 + if (!adttributeFound) {
1.130 + allRequiredAttributesFound = false;
1.131 + break;
1.132 + }
1.133 + }
1.134 +
1.135 + // if an attribute is missing, then try the next type
1.136 + if (!allRequiredAttributesFound)
1.137 + continue;
1.138 +
1.139 + // check if there is a matching name prefix for this type
1.140 + foreach (NamePrefixAttribute prefix in namePrefixes) {
1.141 + if (name.StartsWith(prefix.Prefix, StringComparison.InvariantCulture))
1.142 + return Activator.CreateInstance(type, smart, name, driveIndex,
1.143 + settings) as AbstractHarddrive;
1.144 + }
1.145 + }
1.146 +
1.147 + // no matching type has been found
1.148 + return null;
1.149 + }
1.150 +
1.151 + private void CreateSensors() {
1.152 + sensors = new Dictionary<SmartAttribute, Sensor>();
1.153 +
1.154 + IList<Pair<SensorType, int>> sensorTypeAndChannels =
1.155 + new List<Pair<SensorType, int>>();
1.156 +
1.157 + DriveAttributeValue[] values = smart.ReadSmartData(handle, index);
1.158 +
1.159 + foreach (SmartAttribute attribute in smartAttributes) {
1.160 + if (!attribute.SensorType.HasValue)
1.161 + continue;
1.162 +
1.163 + bool found = false;
1.164 + foreach (DriveAttributeValue value in values) {
1.165 + if (value.Identifier == attribute.Identifier) {
1.166 + found = true;
1.167 + break;
1.168 + }
1.169 + }
1.170 + if (!found)
1.171 + continue;
1.172 +
1.173 + Pair<SensorType, int> pair = new Pair<SensorType, int>(
1.174 + attribute.SensorType.Value, attribute.SensorChannel);
1.175 +
1.176 + if (!sensorTypeAndChannels.Contains(pair)) {
1.177 + Sensor sensor = new Sensor(attribute.Name,
1.178 + attribute.SensorChannel, attribute.SensorType.Value, this,
1.179 + settings);
1.180 +
1.181 + sensors.Add(attribute, sensor);
1.182 + sensorTypeAndChannels.Add(pair);
1.183 + }
1.184 + }
1.185 + }
1.186 +
1.187 + public override HardwareType HardwareType {
1.188 + get { return HardwareType.HDD; }
1.189 + }
1.190 +
1.191 + public override ISensor[] Sensors {
1.192 + get {
1.193 + Sensor[] array = new Sensor[sensors.Count];
1.194 + sensors.Values.CopyTo(array, 0);
1.195 + return array;
1.196 + }
1.197 + }
1.198 +
1.199 + public override void Update() {
1.200 + if (count == 0) {
1.201 + DriveAttributeValue[] values = smart.ReadSmartData(handle, index);
1.202 +
1.203 + foreach (KeyValuePair<SmartAttribute, Sensor> keyValuePair in sensors) {
1.204 + SmartAttribute attribute = keyValuePair.Key;
1.205 + foreach (DriveAttributeValue value in values) {
1.206 + if (value.Identifier == attribute.Identifier) {
1.207 + Sensor sensor = keyValuePair.Value;
1.208 + sensor.Value = attribute.ConvertValue(value);
1.209 + }
1.210 + }
1.211 + }
1.212 + }
1.213 +
1.214 + count++;
1.215 + count %= UPDATE_DIVIDER;
1.216 + }
1.217 +
1.218 + public override string GetReport() {
1.219 + StringBuilder r = new StringBuilder();
1.220 + DriveAttributeValue[] values = smart.ReadSmartData(handle, index);
1.221 + DriveThresholdValue[] thresholds =
1.222 + smart.ReadSmartThresholds(handle, index);
1.223 +
1.224 + if (values.Length > 0) {
1.225 + r.AppendLine(this.GetType().Name);
1.226 + r.AppendLine();
1.227 + r.AppendLine("Drive name: " + name);
1.228 + r.AppendLine();
1.229 + r.AppendFormat(CultureInfo.InvariantCulture,
1.230 + " {0}{1}{2}{3}{4}{5}{6}{7}",
1.231 + ("ID").PadRight(3),
1.232 + ("Description").PadRight(32),
1.233 + ("Raw Value").PadRight(13),
1.234 + ("Worst").PadRight(6),
1.235 + ("Value").PadRight(6),
1.236 + ("Thres").PadRight(6),
1.237 + ("Physical").PadRight(8),
1.238 + Environment.NewLine);
1.239 +
1.240 + foreach (DriveAttributeValue value in values) {
1.241 + if (value.Identifier == 0x00)
1.242 + break;
1.243 +
1.244 + byte? threshold = null;
1.245 + foreach (DriveThresholdValue t in thresholds) {
1.246 + if (t.Identifier == value.Identifier) {
1.247 + threshold = t.Threshold;
1.248 + }
1.249 + }
1.250 +
1.251 + string description = "Unknown";
1.252 + float? physical = null;
1.253 + foreach (SmartAttribute a in smartAttributes) {
1.254 + if (a.Identifier == value.Identifier) {
1.255 + description = a.Name;
1.256 + if (a.HasRawValueConversion | a.SensorType.HasValue)
1.257 + physical = a.ConvertValue(value);
1.258 + else
1.259 + physical = null;
1.260 + }
1.261 + }
1.262 +
1.263 + string raw = BitConverter.ToString(value.RawValue);
1.264 + r.AppendFormat(CultureInfo.InvariantCulture,
1.265 + " {0}{1}{2}{3}{4}{5}{6}{7}",
1.266 + value.Identifier.ToString("X2").PadRight(3),
1.267 + description.PadRight(32),
1.268 + raw.Replace("-", "").PadRight(13),
1.269 + value.WorstValue.ToString(CultureInfo.InvariantCulture).PadRight(6),
1.270 + value.AttrValue.ToString(CultureInfo.InvariantCulture).PadRight(6),
1.271 + (threshold.HasValue ? threshold.Value.ToString(
1.272 + CultureInfo.InvariantCulture) : "-").PadRight(6),
1.273 + (physical.HasValue ? physical.Value.ToString(
1.274 + CultureInfo.InvariantCulture) : "-").PadRight(8),
1.275 + Environment.NewLine);
1.276 + }
1.277 + r.AppendLine();
1.278 + }
1.279 +
1.280 + return r.ToString();
1.281 + }
1.282 +
1.283 + protected static float RawToInt(byte[] raw, byte value) {
1.284 + return (raw[3] << 24) | (raw[2] << 16) | (raw[1] << 8) | raw[0];
1.285 + }
1.286 +
1.287 + public override void Close() {
1.288 + smart.CloseHandle(handle);
1.289 + base.Close();
1.290 + }
1.291 +
1.292 + public override void Traverse(IVisitor visitor) {
1.293 + foreach (ISensor sensor in Sensors)
1.294 + sensor.Accept(visitor);
1.295 + }
1.296 + }
1.297 +}