Added more report output to the kernel driver loading code. Hopefully this helps to find the problems in Issue 253.
3 This Source Code Form is subject to the terms of the Mozilla Public
4 License, v. 2.0. If a copy of the MPL was not distributed with this
5 file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 Copyright (C) 2009-2012 Michael Möller <mmoeller@openhardwaremonitor.org>
8 Copyright (C) 2010 Paul Werelds
9 Copyright (C) 2011 Roland Reinl <roland-reinl@gmx.de>
14 using System.Collections.Generic;
15 using System.Globalization;
17 using OpenHardwareMonitor.Collections;
19 namespace OpenHardwareMonitor.Hardware.HDD {
20 internal abstract class AbstractHarddrive : Hardware {
22 private const int UPDATE_DIVIDER = 30; // update only every 30s
24 // array of all harddrive types, matching type is searched in this order
25 private static Type[] hddTypes = {
32 typeof(GenericHarddisk)
35 private string firmwareRevision;
36 private readonly ISmart smart;
38 private readonly IntPtr handle;
39 private readonly int index;
42 private IList<SmartAttribute> smartAttributes;
43 private IDictionary<SmartAttribute, Sensor> sensors;
45 protected AbstractHarddrive(ISmart smart, string name,
46 string firmwareRevision, int index,
47 IEnumerable<SmartAttribute> smartAttributes, ISettings settings)
48 : base(name, new Identifier("hdd",
49 index.ToString(CultureInfo.InvariantCulture)), settings)
51 this.firmwareRevision = firmwareRevision;
53 handle = smart.OpenDrive(index);
55 smart.EnableSmart(handle, index);
60 this.smartAttributes = new List<SmartAttribute>(smartAttributes);
65 public static AbstractHarddrive CreateInstance(ISmart smart,
66 int driveIndex, ISettings settings)
68 IntPtr deviceHandle = smart.OpenDrive(driveIndex);
70 if (deviceHandle == smart.InvalidHandle)
74 string firmwareRevision;
75 bool nameValid = smart.ReadNameAndFirmwareRevision(deviceHandle,
76 driveIndex, out name, out firmwareRevision);
77 bool smartEnabled = smart.EnableSmart(deviceHandle, driveIndex);
79 DriveAttributeValue[] values = {};
81 values = smart.ReadSmartData(deviceHandle, driveIndex);
83 smart.CloseHandle(deviceHandle);
85 if (!nameValid || string.IsNullOrEmpty(name))
88 foreach (Type type in hddTypes) {
89 // get the array of name prefixes for the current type
90 NamePrefixAttribute[] namePrefixes = type.GetCustomAttributes(
91 typeof(NamePrefixAttribute), true) as NamePrefixAttribute[];
93 // get the array of the required SMART attributes for the current type
94 RequireSmartAttribute[] requiredAttributes = type.GetCustomAttributes(
95 typeof(RequireSmartAttribute), true) as RequireSmartAttribute[];
97 // check if all required attributes are present
98 bool allRequiredAttributesFound = true;
99 foreach (var requireAttribute in requiredAttributes) {
100 bool adttributeFound = false;
101 foreach (DriveAttributeValue value in values) {
102 if (value.Identifier == requireAttribute.AttributeId) {
103 adttributeFound = true;
107 if (!adttributeFound) {
108 allRequiredAttributesFound = false;
113 // if an attribute is missing, then try the next type
114 if (!allRequiredAttributesFound)
117 // check if there is a matching name prefix for this type
118 foreach (NamePrefixAttribute prefix in namePrefixes) {
119 if (name.StartsWith(prefix.Prefix, StringComparison.InvariantCulture))
120 return Activator.CreateInstance(type, smart, name, firmwareRevision,
121 driveIndex, settings) as AbstractHarddrive;
125 // no matching type has been found
129 private void CreateSensors() {
130 sensors = new Dictionary<SmartAttribute, Sensor>();
132 IList<Pair<SensorType, int>> sensorTypeAndChannels =
133 new List<Pair<SensorType, int>>();
135 DriveAttributeValue[] values = smart.ReadSmartData(handle, index);
137 foreach (SmartAttribute attribute in smartAttributes) {
138 if (!attribute.SensorType.HasValue)
142 foreach (DriveAttributeValue value in values) {
143 if (value.Identifier == attribute.Identifier) {
151 Pair<SensorType, int> pair = new Pair<SensorType, int>(
152 attribute.SensorType.Value, attribute.SensorChannel);
154 if (!sensorTypeAndChannels.Contains(pair)) {
155 Sensor sensor = new Sensor(attribute.Name,
156 attribute.SensorChannel, attribute.DefaultHiddenSensor,
157 attribute.SensorType.Value, this, null, settings);
159 sensors.Add(attribute, sensor);
160 ActivateSensor(sensor);
161 sensorTypeAndChannels.Add(pair);
166 public override HardwareType HardwareType {
167 get { return HardwareType.HDD; }
170 public virtual void UpdateAdditionalSensors(DriveAttributeValue[] values) {}
172 public override void Update() {
174 DriveAttributeValue[] values = smart.ReadSmartData(handle, index);
176 foreach (KeyValuePair<SmartAttribute, Sensor> keyValuePair in sensors) {
177 SmartAttribute attribute = keyValuePair.Key;
178 foreach (DriveAttributeValue value in values) {
179 if (value.Identifier == attribute.Identifier) {
180 Sensor sensor = keyValuePair.Value;
181 sensor.Value = attribute.ConvertValue(value);
186 UpdateAdditionalSensors(values);
190 count %= UPDATE_DIVIDER;
193 public override string GetReport() {
194 StringBuilder r = new StringBuilder();
195 DriveAttributeValue[] values = smart.ReadSmartData(handle, index);
196 DriveThresholdValue[] thresholds =
197 smart.ReadSmartThresholds(handle, index);
199 if (values.Length > 0) {
200 r.AppendLine(this.GetType().Name);
202 r.AppendLine("Drive name: " + name);
203 r.AppendLine("Firmware version: " + firmwareRevision);
205 r.AppendFormat(CultureInfo.InvariantCulture,
206 " {0}{1}{2}{3}{4}{5}{6}{7}",
208 ("Description").PadRight(35),
209 ("Raw Value").PadRight(13),
210 ("Worst").PadRight(6),
211 ("Value").PadRight(6),
212 ("Thres").PadRight(6),
213 ("Physical").PadRight(8),
214 Environment.NewLine);
216 foreach (DriveAttributeValue value in values) {
217 if (value.Identifier == 0x00)
220 byte? threshold = null;
221 foreach (DriveThresholdValue t in thresholds) {
222 if (t.Identifier == value.Identifier) {
223 threshold = t.Threshold;
227 string description = "Unknown";
228 float? physical = null;
229 foreach (SmartAttribute a in smartAttributes) {
230 if (a.Identifier == value.Identifier) {
231 description = a.Name;
232 if (a.HasRawValueConversion | a.SensorType.HasValue)
233 physical = a.ConvertValue(value);
239 string raw = BitConverter.ToString(value.RawValue);
240 r.AppendFormat(CultureInfo.InvariantCulture,
241 " {0}{1}{2}{3}{4}{5}{6}{7}",
242 value.Identifier.ToString("X2").PadRight(3),
243 description.PadRight(35),
244 raw.Replace("-", "").PadRight(13),
245 value.WorstValue.ToString(CultureInfo.InvariantCulture).PadRight(6),
246 value.AttrValue.ToString(CultureInfo.InvariantCulture).PadRight(6),
247 (threshold.HasValue ? threshold.Value.ToString(
248 CultureInfo.InvariantCulture) : "-").PadRight(6),
249 (physical.HasValue ? physical.Value.ToString(
250 CultureInfo.InvariantCulture) : "-").PadRight(8),
251 Environment.NewLine);
259 protected static float RawToInt(byte[] raw, byte value) {
260 return (raw[3] << 24) | (raw[2] << 16) | (raw[1] << 8) | raw[0];
263 public override void Close() {
264 smart.CloseHandle(handle);
268 public override void Traverse(IVisitor visitor) {
269 foreach (ISensor sensor in Sensors)
270 sensor.Accept(visitor);