# HG changeset patch # User moel.mich # Date 1370807820 0 # Node ID c540cf36b7ce1d17a251a9d309903ec59c0f7b65 # Parent 0bad639907a30b5a2a65296796079977945fb8c3 Changed the HDD detection to list hard drives without SMART support as well (at least if they have any visible partitions on them). diff -r 0bad639907a3 -r c540cf36b7ce Hardware/HDD/AbstractHarddrive.cs --- a/Hardware/HDD/AbstractHarddrive.cs Sun Jun 09 19:25:44 2013 +0000 +++ b/Hardware/HDD/AbstractHarddrive.cs Sun Jun 09 19:57:00 2013 +0000 @@ -4,7 +4,7 @@ License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. - Copyright (C) 2009-2012 Michael Möller + Copyright (C) 2009-2013 Michael Möller Copyright (C) 2010 Paul Werelds Copyright (C) 2011 Roland Reinl @@ -56,7 +56,8 @@ this.smart = smart; handle = smart.OpenDrive(index); - smart.EnableSmart(handle, index); + if (handle != smart.InvalidHandle) + smart.EnableSmart(handle, index); this.index = index; this.count = 0; @@ -82,23 +83,35 @@ { IntPtr deviceHandle = smart.OpenDrive(driveIndex); - if (deviceHandle == smart.InvalidHandle) - return null; + string name = null; + string firmwareRevision = null; + DriveAttributeValue[] values = { }; - string name; - string firmwareRevision; - bool nameValid = smart.ReadNameAndFirmwareRevision(deviceHandle, + if (deviceHandle != smart.InvalidHandle) { + bool nameValid = smart.ReadNameAndFirmwareRevision(deviceHandle, driveIndex, out name, out firmwareRevision); - bool smartEnabled = smart.EnableSmart(deviceHandle, driveIndex); + bool smartEnabled = smart.EnableSmart(deviceHandle, driveIndex); - DriveAttributeValue[] values = {}; - if (smartEnabled) - values = smart.ReadSmartData(deviceHandle, driveIndex); + if (smartEnabled) + values = smart.ReadSmartData(deviceHandle, driveIndex); - smart.CloseHandle(deviceHandle); + smart.CloseHandle(deviceHandle); - if (!nameValid || string.IsNullOrEmpty(name)) - return null; + if (!nameValid) { + name = null; + firmwareRevision = null; + } + } else { + string[] logicalDrives = smart.GetLogicalDrives(driveIndex); + if (logicalDrives == null || logicalDrives.Length == 0) + return null; + } + + if (string.IsNullOrEmpty(name)) + name = "Generic Hard Disk"; + + if (string.IsNullOrEmpty(firmwareRevision)) + firmwareRevision = "Unknown"; foreach (Type type in hddTypes) { // get the array of name prefixes for the current type @@ -144,38 +157,40 @@ private void CreateSensors() { sensors = new Dictionary(); - IList> sensorTypeAndChannels = - new List>(); + if (handle != smart.InvalidHandle) { + IList> sensorTypeAndChannels = + new List>(); - DriveAttributeValue[] values = smart.ReadSmartData(handle, index); + DriveAttributeValue[] values = smart.ReadSmartData(handle, index); - foreach (SmartAttribute attribute in smartAttributes) { - if (!attribute.SensorType.HasValue) - continue; + foreach (SmartAttribute attribute in smartAttributes) { + if (!attribute.SensorType.HasValue) + continue; - bool found = false; - foreach (DriveAttributeValue value in values) { - if (value.Identifier == attribute.Identifier) { - found = true; - break; + bool found = false; + foreach (DriveAttributeValue value in values) { + if (value.Identifier == attribute.Identifier) { + found = true; + break; + } + } + if (!found) + continue; + + Pair pair = new Pair( + attribute.SensorType.Value, attribute.SensorChannel); + + if (!sensorTypeAndChannels.Contains(pair)) { + Sensor sensor = new Sensor(attribute.Name, + attribute.SensorChannel, attribute.DefaultHiddenSensor, + attribute.SensorType.Value, this, attribute.ParameterDescriptions, + settings); + + sensors.Add(attribute, sensor); + ActivateSensor(sensor); + sensorTypeAndChannels.Add(pair); } } - if (!found) - continue; - - Pair pair = new Pair( - attribute.SensorType.Value, attribute.SensorChannel); - - if (!sensorTypeAndChannels.Contains(pair)) { - Sensor sensor = new Sensor(attribute.Name, - attribute.SensorChannel, attribute.DefaultHiddenSensor, - attribute.SensorType.Value, this, attribute.ParameterDescriptions, - settings); - - sensors.Add(attribute, sensor); - ActivateSensor(sensor); - sensorTypeAndChannels.Add(pair); - } } if (driveInfos.Length > 0) { @@ -193,20 +208,23 @@ public override void Update() { if (count == 0) { - DriveAttributeValue[] values = smart.ReadSmartData(handle, index); + if (handle != smart.InvalidHandle) { + DriveAttributeValue[] values = smart.ReadSmartData(handle, index); - foreach (KeyValuePair keyValuePair in sensors) { - SmartAttribute attribute = keyValuePair.Key; - foreach (DriveAttributeValue value in values) { - if (value.Identifier == attribute.Identifier) { - Sensor sensor = keyValuePair.Value; - sensor.Value = attribute.ConvertValue(value, sensor.Parameters); + foreach (KeyValuePair keyValuePair in sensors) + { + SmartAttribute attribute = keyValuePair.Key; + foreach (DriveAttributeValue value in values) { + if (value.Identifier == attribute.Identifier) { + Sensor sensor = keyValuePair.Value; + sensor.Value = attribute.ConvertValue(value, sensor.Parameters); + } } } + + UpdateAdditionalSensors(values); } - UpdateAdditionalSensors(values); - if (usageSensor != null) { long totalSize = 0; long totalFreeSpace = 0; @@ -233,65 +251,69 @@ public override string GetReport() { StringBuilder r = new StringBuilder(); - DriveAttributeValue[] values = smart.ReadSmartData(handle, index); - DriveThresholdValue[] thresholds = - smart.ReadSmartThresholds(handle, index); - if (values.Length > 0) { - r.AppendLine(this.GetType().Name); - r.AppendLine(); - r.AppendLine("Drive name: " + name); - r.AppendLine("Firmware version: " + firmwareRevision); - r.AppendLine(); - r.AppendFormat(CultureInfo.InvariantCulture, - " {0}{1}{2}{3}{4}{5}{6}{7}", - ("ID").PadRight(3), - ("Description").PadRight(35), - ("Raw Value").PadRight(13), - ("Worst").PadRight(6), - ("Value").PadRight(6), - ("Thres").PadRight(6), - ("Physical").PadRight(8), - Environment.NewLine); + r.AppendLine(this.GetType().Name); + r.AppendLine(); + r.AppendLine("Drive name: " + name); + r.AppendLine("Firmware version: " + firmwareRevision); + r.AppendLine(); - foreach (DriveAttributeValue value in values) { - if (value.Identifier == 0x00) - break; + if (handle != smart.InvalidHandle) { + DriveAttributeValue[] values = smart.ReadSmartData(handle, index); + DriveThresholdValue[] thresholds = + smart.ReadSmartThresholds(handle, index); - byte? threshold = null; - foreach (DriveThresholdValue t in thresholds) { - if (t.Identifier == value.Identifier) { - threshold = t.Threshold; + if (values.Length > 0) { + r.AppendFormat(CultureInfo.InvariantCulture, + " {0}{1}{2}{3}{4}{5}{6}{7}", + ("ID").PadRight(3), + ("Description").PadRight(35), + ("Raw Value").PadRight(13), + ("Worst").PadRight(6), + ("Value").PadRight(6), + ("Thres").PadRight(6), + ("Physical").PadRight(8), + Environment.NewLine); + + foreach (DriveAttributeValue value in values) { + if (value.Identifier == 0x00) + break; + + byte? threshold = null; + foreach (DriveThresholdValue t in thresholds) { + if (t.Identifier == value.Identifier) { + threshold = t.Threshold; + } } + + string description = "Unknown"; + float? physical = null; + foreach (SmartAttribute a in smartAttributes) { + if (a.Identifier == value.Identifier) { + description = a.Name; + if (a.HasRawValueConversion | a.SensorType.HasValue) + physical = a.ConvertValue(value, null); + else + physical = null; + } + } + + string raw = BitConverter.ToString(value.RawValue); + r.AppendFormat(CultureInfo.InvariantCulture, + " {0}{1}{2}{3}{4}{5}{6}{7}", + value.Identifier.ToString("X2").PadRight(3), + description.PadRight(35), + raw.Replace("-", "").PadRight(13), + value.WorstValue.ToString(CultureInfo.InvariantCulture).PadRight(6), + value.AttrValue.ToString(CultureInfo.InvariantCulture).PadRight(6), + (threshold.HasValue ? threshold.Value.ToString( + CultureInfo.InvariantCulture) : "-").PadRight(6), + (physical.HasValue ? physical.Value.ToString( + CultureInfo.InvariantCulture) : "-").PadRight(8), + Environment.NewLine); } - - string description = "Unknown"; - float? physical = null; - foreach (SmartAttribute a in smartAttributes) { - if (a.Identifier == value.Identifier) { - description = a.Name; - if (a.HasRawValueConversion | a.SensorType.HasValue) - physical = a.ConvertValue(value, null); - else - physical = null; - } - } - - string raw = BitConverter.ToString(value.RawValue); - r.AppendFormat(CultureInfo.InvariantCulture, - " {0}{1}{2}{3}{4}{5}{6}{7}", - value.Identifier.ToString("X2").PadRight(3), - description.PadRight(35), - raw.Replace("-", "").PadRight(13), - value.WorstValue.ToString(CultureInfo.InvariantCulture).PadRight(6), - value.AttrValue.ToString(CultureInfo.InvariantCulture).PadRight(6), - (threshold.HasValue ? threshold.Value.ToString( - CultureInfo.InvariantCulture) : "-").PadRight(6), - (physical.HasValue ? physical.Value.ToString( - CultureInfo.InvariantCulture) : "-").PadRight(8), - Environment.NewLine); + r.AppendLine(); } - r.AppendLine(); } foreach (DriveInfo di in driveInfos) { @@ -312,7 +334,9 @@ } public override void Close() { - smart.CloseHandle(handle); + if (handle != smart.InvalidHandle) + smart.CloseHandle(handle); + base.Close(); }