Hardware/HDD/AbstractHarddrive.cs
changeset 403 c540cf36b7ce
parent 385 8f16f03797f5
child 404 1201889d413a
     1.1 --- a/Hardware/HDD/AbstractHarddrive.cs	Sun Jun 09 19:25:44 2013 +0000
     1.2 +++ b/Hardware/HDD/AbstractHarddrive.cs	Sun Jun 09 19:57:00 2013 +0000
     1.3 @@ -4,7 +4,7 @@
     1.4    License, v. 2.0. If a copy of the MPL was not distributed with this
     1.5    file, You can obtain one at http://mozilla.org/MPL/2.0/.
     1.6   
     1.7 -  Copyright (C) 2009-2012 Michael Möller <mmoeller@openhardwaremonitor.org>
     1.8 +  Copyright (C) 2009-2013 Michael Möller <mmoeller@openhardwaremonitor.org>
     1.9  	Copyright (C) 2010 Paul Werelds
    1.10    Copyright (C) 2011 Roland Reinl <roland-reinl@gmx.de>
    1.11  	
    1.12 @@ -56,7 +56,8 @@
    1.13        this.smart = smart;
    1.14        handle = smart.OpenDrive(index);
    1.15  
    1.16 -      smart.EnableSmart(handle, index);
    1.17 +      if (handle != smart.InvalidHandle)
    1.18 +        smart.EnableSmart(handle, index);
    1.19  
    1.20        this.index = index;
    1.21        this.count = 0;
    1.22 @@ -82,23 +83,35 @@
    1.23      {
    1.24        IntPtr deviceHandle = smart.OpenDrive(driveIndex);
    1.25  
    1.26 -      if (deviceHandle == smart.InvalidHandle) 
    1.27 -        return null;
    1.28 +      string name = null;
    1.29 +      string firmwareRevision = null;
    1.30 +      DriveAttributeValue[] values = { };
    1.31  
    1.32 -      string name;
    1.33 -      string firmwareRevision;
    1.34 -      bool nameValid = smart.ReadNameAndFirmwareRevision(deviceHandle, 
    1.35 +      if (deviceHandle != smart.InvalidHandle) {
    1.36 +        bool nameValid = smart.ReadNameAndFirmwareRevision(deviceHandle,
    1.37          driveIndex, out name, out firmwareRevision);
    1.38 -      bool smartEnabled = smart.EnableSmart(deviceHandle, driveIndex);
    1.39 +        bool smartEnabled = smart.EnableSmart(deviceHandle, driveIndex);
    1.40  
    1.41 -      DriveAttributeValue[] values = {};
    1.42 -      if (smartEnabled)
    1.43 -        values = smart.ReadSmartData(deviceHandle, driveIndex);
    1.44 +        if (smartEnabled)
    1.45 +          values = smart.ReadSmartData(deviceHandle, driveIndex);
    1.46  
    1.47 -      smart.CloseHandle(deviceHandle);
    1.48 +        smart.CloseHandle(deviceHandle);
    1.49  
    1.50 -      if (!nameValid || string.IsNullOrEmpty(name)) 
    1.51 -        return null;      
    1.52 +        if (!nameValid) {
    1.53 +          name = null;
    1.54 +          firmwareRevision = null;
    1.55 +        }
    1.56 +      } else {
    1.57 +        string[] logicalDrives = smart.GetLogicalDrives(driveIndex);
    1.58 +        if (logicalDrives == null || logicalDrives.Length == 0)
    1.59 +          return null;
    1.60 +      }
    1.61 +
    1.62 +      if (string.IsNullOrEmpty(name))
    1.63 +        name = "Generic Hard Disk";
    1.64 +
    1.65 +      if (string.IsNullOrEmpty(firmwareRevision))
    1.66 +        firmwareRevision = "Unknown";
    1.67  
    1.68        foreach (Type type in hddTypes) {
    1.69          // get the array of name prefixes for the current type
    1.70 @@ -144,38 +157,40 @@
    1.71      private void CreateSensors() {
    1.72        sensors = new Dictionary<SmartAttribute, Sensor>();
    1.73  
    1.74 -      IList<Pair<SensorType, int>> sensorTypeAndChannels = 
    1.75 -        new List<Pair<SensorType, int>>();
    1.76 +      if (handle != smart.InvalidHandle) {
    1.77 +        IList<Pair<SensorType, int>> sensorTypeAndChannels =
    1.78 +          new List<Pair<SensorType, int>>();
    1.79  
    1.80 -      DriveAttributeValue[] values = smart.ReadSmartData(handle, index);
    1.81 +        DriveAttributeValue[] values = smart.ReadSmartData(handle, index);
    1.82  
    1.83 -      foreach (SmartAttribute attribute in smartAttributes) {
    1.84 -        if (!attribute.SensorType.HasValue) 
    1.85 -          continue;
    1.86 +        foreach (SmartAttribute attribute in smartAttributes) {
    1.87 +          if (!attribute.SensorType.HasValue)
    1.88 +            continue;
    1.89  
    1.90 -        bool found = false;
    1.91 -        foreach (DriveAttributeValue value in values) {
    1.92 -          if (value.Identifier == attribute.Identifier) {
    1.93 -            found = true;
    1.94 -            break;
    1.95 +          bool found = false;
    1.96 +          foreach (DriveAttributeValue value in values) {
    1.97 +            if (value.Identifier == attribute.Identifier) {
    1.98 +              found = true;
    1.99 +              break;
   1.100 +            }
   1.101 +          }
   1.102 +          if (!found)
   1.103 +            continue;
   1.104 +
   1.105 +          Pair<SensorType, int> pair = new Pair<SensorType, int>(
   1.106 +            attribute.SensorType.Value, attribute.SensorChannel);
   1.107 +
   1.108 +          if (!sensorTypeAndChannels.Contains(pair)) {
   1.109 +            Sensor sensor = new Sensor(attribute.Name,
   1.110 +              attribute.SensorChannel, attribute.DefaultHiddenSensor,
   1.111 +              attribute.SensorType.Value, this, attribute.ParameterDescriptions,
   1.112 +              settings);
   1.113 +
   1.114 +            sensors.Add(attribute, sensor);
   1.115 +            ActivateSensor(sensor);
   1.116 +            sensorTypeAndChannels.Add(pair);
   1.117            }
   1.118          }
   1.119 -        if (!found)
   1.120 -          continue;
   1.121 -
   1.122 -        Pair<SensorType, int> pair = new Pair<SensorType, int>(
   1.123 -          attribute.SensorType.Value, attribute.SensorChannel);
   1.124 -
   1.125 -        if (!sensorTypeAndChannels.Contains(pair)) {
   1.126 -          Sensor sensor = new Sensor(attribute.Name, 
   1.127 -            attribute.SensorChannel, attribute.DefaultHiddenSensor, 
   1.128 -            attribute.SensorType.Value, this, attribute.ParameterDescriptions, 
   1.129 -            settings);
   1.130 -
   1.131 -          sensors.Add(attribute, sensor);
   1.132 -          ActivateSensor(sensor);
   1.133 -          sensorTypeAndChannels.Add(pair);
   1.134 -        }     
   1.135        }
   1.136  
   1.137        if (driveInfos.Length > 0) {
   1.138 @@ -193,20 +208,23 @@
   1.139  
   1.140      public override void Update() {
   1.141        if (count == 0) {
   1.142 -        DriveAttributeValue[] values = smart.ReadSmartData(handle, index);
   1.143 +        if (handle != smart.InvalidHandle) {
   1.144 +          DriveAttributeValue[] values = smart.ReadSmartData(handle, index);
   1.145  
   1.146 -        foreach (KeyValuePair<SmartAttribute, Sensor> keyValuePair in sensors) {
   1.147 -          SmartAttribute attribute = keyValuePair.Key;
   1.148 -          foreach (DriveAttributeValue value in values) {
   1.149 -            if (value.Identifier == attribute.Identifier) {
   1.150 -              Sensor sensor = keyValuePair.Value;
   1.151 -              sensor.Value = attribute.ConvertValue(value, sensor.Parameters);
   1.152 +          foreach (KeyValuePair<SmartAttribute, Sensor> keyValuePair in sensors) 
   1.153 +          {
   1.154 +            SmartAttribute attribute = keyValuePair.Key;
   1.155 +            foreach (DriveAttributeValue value in values) {
   1.156 +              if (value.Identifier == attribute.Identifier) {
   1.157 +                Sensor sensor = keyValuePair.Value;
   1.158 +                sensor.Value = attribute.ConvertValue(value, sensor.Parameters);
   1.159 +              }
   1.160              }
   1.161            }
   1.162 +
   1.163 +          UpdateAdditionalSensors(values);
   1.164          }
   1.165  
   1.166 -        UpdateAdditionalSensors(values);
   1.167 -
   1.168          if (usageSensor != null) {
   1.169            long totalSize = 0;
   1.170            long totalFreeSpace = 0;
   1.171 @@ -233,65 +251,69 @@
   1.172  
   1.173      public override string GetReport() {
   1.174        StringBuilder r = new StringBuilder();
   1.175 -      DriveAttributeValue[] values = smart.ReadSmartData(handle, index);
   1.176 -      DriveThresholdValue[] thresholds = 
   1.177 -        smart.ReadSmartThresholds(handle, index);
   1.178  
   1.179 -      if (values.Length > 0) {
   1.180 -        r.AppendLine(this.GetType().Name);
   1.181 -        r.AppendLine();
   1.182 -        r.AppendLine("Drive name: " + name);
   1.183 -        r.AppendLine("Firmware version: " + firmwareRevision);
   1.184 -        r.AppendLine();    
   1.185 -        r.AppendFormat(CultureInfo.InvariantCulture, 
   1.186 -          " {0}{1}{2}{3}{4}{5}{6}{7}",
   1.187 -          ("ID").PadRight(3),
   1.188 -          ("Description").PadRight(35),
   1.189 -          ("Raw Value").PadRight(13),
   1.190 -          ("Worst").PadRight(6),
   1.191 -          ("Value").PadRight(6),
   1.192 -          ("Thres").PadRight(6),
   1.193 -          ("Physical").PadRight(8),
   1.194 -          Environment.NewLine);
   1.195 +      r.AppendLine(this.GetType().Name);
   1.196 +      r.AppendLine();
   1.197 +      r.AppendLine("Drive name: " + name);
   1.198 +      r.AppendLine("Firmware version: " + firmwareRevision);
   1.199 +      r.AppendLine();
   1.200  
   1.201 -        foreach (DriveAttributeValue value in values) {
   1.202 -          if (value.Identifier == 0x00) 
   1.203 -            break;
   1.204 +      if (handle != smart.InvalidHandle) {
   1.205 +        DriveAttributeValue[] values = smart.ReadSmartData(handle, index);
   1.206 +        DriveThresholdValue[] thresholds =
   1.207 +          smart.ReadSmartThresholds(handle, index);
   1.208  
   1.209 -          byte? threshold = null;
   1.210 -          foreach (DriveThresholdValue t in thresholds) {
   1.211 -            if (t.Identifier == value.Identifier) {
   1.212 -              threshold = t.Threshold;
   1.213 +        if (values.Length > 0) {
   1.214 +          r.AppendFormat(CultureInfo.InvariantCulture,
   1.215 +            " {0}{1}{2}{3}{4}{5}{6}{7}",
   1.216 +            ("ID").PadRight(3),
   1.217 +            ("Description").PadRight(35),
   1.218 +            ("Raw Value").PadRight(13),
   1.219 +            ("Worst").PadRight(6),
   1.220 +            ("Value").PadRight(6),
   1.221 +            ("Thres").PadRight(6),
   1.222 +            ("Physical").PadRight(8),
   1.223 +            Environment.NewLine);
   1.224 +
   1.225 +          foreach (DriveAttributeValue value in values) {
   1.226 +            if (value.Identifier == 0x00)
   1.227 +              break;
   1.228 +
   1.229 +            byte? threshold = null;
   1.230 +            foreach (DriveThresholdValue t in thresholds) {
   1.231 +              if (t.Identifier == value.Identifier) {
   1.232 +                threshold = t.Threshold;
   1.233 +              }
   1.234              }
   1.235 +
   1.236 +            string description = "Unknown";
   1.237 +            float? physical = null;
   1.238 +            foreach (SmartAttribute a in smartAttributes) {
   1.239 +              if (a.Identifier == value.Identifier) {
   1.240 +                description = a.Name;
   1.241 +                if (a.HasRawValueConversion | a.SensorType.HasValue)
   1.242 +                  physical = a.ConvertValue(value, null);
   1.243 +                else
   1.244 +                  physical = null;
   1.245 +              }
   1.246 +            }
   1.247 +
   1.248 +            string raw = BitConverter.ToString(value.RawValue);
   1.249 +            r.AppendFormat(CultureInfo.InvariantCulture,
   1.250 +              " {0}{1}{2}{3}{4}{5}{6}{7}",
   1.251 +              value.Identifier.ToString("X2").PadRight(3),
   1.252 +              description.PadRight(35),
   1.253 +              raw.Replace("-", "").PadRight(13),
   1.254 +              value.WorstValue.ToString(CultureInfo.InvariantCulture).PadRight(6),
   1.255 +              value.AttrValue.ToString(CultureInfo.InvariantCulture).PadRight(6),
   1.256 +              (threshold.HasValue ? threshold.Value.ToString(
   1.257 +                CultureInfo.InvariantCulture) : "-").PadRight(6),
   1.258 +              (physical.HasValue ? physical.Value.ToString(
   1.259 +                CultureInfo.InvariantCulture) : "-").PadRight(8),
   1.260 +              Environment.NewLine);
   1.261            }
   1.262 -
   1.263 -          string description = "Unknown";
   1.264 -          float? physical = null;
   1.265 -          foreach (SmartAttribute a in smartAttributes) {
   1.266 -            if (a.Identifier == value.Identifier) {
   1.267 -              description = a.Name;
   1.268 -              if (a.HasRawValueConversion | a.SensorType.HasValue)
   1.269 -                physical = a.ConvertValue(value, null);
   1.270 -              else
   1.271 -                physical = null;
   1.272 -            }
   1.273 -          }
   1.274 -
   1.275 -          string raw = BitConverter.ToString(value.RawValue);
   1.276 -          r.AppendFormat(CultureInfo.InvariantCulture, 
   1.277 -            " {0}{1}{2}{3}{4}{5}{6}{7}",
   1.278 -            value.Identifier.ToString("X2").PadRight(3),
   1.279 -            description.PadRight(35),
   1.280 -            raw.Replace("-", "").PadRight(13),
   1.281 -            value.WorstValue.ToString(CultureInfo.InvariantCulture).PadRight(6),
   1.282 -            value.AttrValue.ToString(CultureInfo.InvariantCulture).PadRight(6),
   1.283 -            (threshold.HasValue ? threshold.Value.ToString(
   1.284 -              CultureInfo.InvariantCulture) : "-").PadRight(6),
   1.285 -            (physical.HasValue ? physical.Value.ToString(
   1.286 -              CultureInfo.InvariantCulture) : "-").PadRight(8),
   1.287 -            Environment.NewLine);
   1.288 +          r.AppendLine();
   1.289          }
   1.290 -        r.AppendLine();
   1.291        }
   1.292  
   1.293        foreach (DriveInfo di in driveInfos) {
   1.294 @@ -312,7 +334,9 @@
   1.295      }
   1.296  
   1.297      public override void Close() {
   1.298 -      smart.CloseHandle(handle);
   1.299 +      if (handle != smart.InvalidHandle)
   1.300 +        smart.CloseHandle(handle);
   1.301 +
   1.302        base.Close();
   1.303      }
   1.304