Hardware/HDD/HDDGroup.cs
author paulwerelds
Sat, 02 Oct 2010 14:39:25 +0000
changeset 204 59278dadc5c0
parent 195 0ee888c485d5
child 205 a38b51ef489c
permissions -rw-r--r--
Added a S.M.A.R.T dump for all drives, regardless of temperature presence so that we can start debugging SSD's and such.
moel@1
     1
/*
moel@1
     2
  
moel@1
     3
  Version: MPL 1.1/GPL 2.0/LGPL 2.1
moel@1
     4
moel@1
     5
  The contents of this file are subject to the Mozilla Public License Version
moel@1
     6
  1.1 (the "License"); you may not use this file except in compliance with
moel@1
     7
  the License. You may obtain a copy of the License at
moel@1
     8
 
moel@1
     9
  http://www.mozilla.org/MPL/
moel@1
    10
moel@1
    11
  Software distributed under the License is distributed on an "AS IS" basis,
moel@1
    12
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
moel@1
    13
  for the specific language governing rights and limitations under the License.
moel@1
    14
moel@1
    15
  The Original Code is the Open Hardware Monitor code.
moel@1
    16
moel@1
    17
  The Initial Developer of the Original Code is 
moel@1
    18
  Michael Möller <m.moeller@gmx.ch>.
moel@1
    19
  Portions created by the Initial Developer are Copyright (C) 2009-2010
moel@1
    20
  the Initial Developer. All Rights Reserved.
moel@1
    21
paulwerelds@204
    22
  Contributor(s): Paul Werelds
moel@1
    23
moel@1
    24
  Alternatively, the contents of this file may be used under the terms of
moel@1
    25
  either the GNU General Public License Version 2 or later (the "GPL"), or
moel@1
    26
  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
moel@1
    27
  in which case the provisions of the GPL or the LGPL are applicable instead
moel@1
    28
  of those above. If you wish to allow use of your version of this file only
moel@1
    29
  under the terms of either the GPL or the LGPL, and not to allow others to
moel@1
    30
  use your version of this file under the terms of the MPL, indicate your
moel@1
    31
  decision by deleting the provisions above and replace them with the notice
moel@1
    32
  and other provisions required by the GPL or the LGPL. If you do not delete
moel@1
    33
  the provisions above, a recipient may use your version of this file under
moel@1
    34
  the terms of any one of the MPL, the GPL or the LGPL.
moel@1
    35
 
moel@1
    36
*/
moel@1
    37
moel@1
    38
using System;
moel@1
    39
using System.Collections.Generic;
paulwerelds@204
    40
using System.Text;
moel@1
    41
moel@1
    42
namespace OpenHardwareMonitor.Hardware.HDD {
moel@165
    43
  internal class HDDGroup : IGroup {
moel@1
    44
moel@1
    45
    private const int MAX_DRIVES = 32;
moel@1
    46
moel@195
    47
    private readonly List<HDD> hardware = new List<HDD>();
moel@1
    48
paulwerelds@204
    49
    private readonly Dictionary<string, SMART.DriveAttribute[]> ignoredDrives = new Dictionary<string, SMART.DriveAttribute[]>();
paulwerelds@204
    50
moel@165
    51
    public HDDGroup(ISettings settings) {
paulwerelds@204
    52
      int p = (int)Environment.OSVersion.Platform;
paulwerelds@204
    53
      if (p == 4 || p == 128) return;
moel@1
    54
paulwerelds@204
    55
      for (int drive = 0; drive < MAX_DRIVES; drive++) {
paulwerelds@204
    56
        IntPtr handle = SMART.OpenPhysicalDrive(drive);
moel@1
    57
paulwerelds@204
    58
        if (handle == SMART.INVALID_HANDLE_VALUE)
paulwerelds@204
    59
          continue;
paulwerelds@204
    60
paulwerelds@204
    61
        if (!SMART.EnableSmart(handle, drive)) {
paulwerelds@204
    62
          SMART.CloseHandle(handle);
paulwerelds@204
    63
          continue;
paulwerelds@204
    64
        }
paulwerelds@204
    65
paulwerelds@204
    66
        string name = SMART.ReadName(handle, drive);
paulwerelds@204
    67
        if (name == null) {
paulwerelds@204
    68
          SMART.CloseHandle(handle);
paulwerelds@204
    69
          continue;
paulwerelds@204
    70
        }
paulwerelds@204
    71
paulwerelds@204
    72
        SMART.DriveAttribute[] attributes = SMART.ReadSmart(handle, drive);
paulwerelds@204
    73
paulwerelds@204
    74
        if (attributes != null) {
paulwerelds@204
    75
          int attribute = -1;
paulwerelds@204
    76
paulwerelds@204
    77
          int i = 0;
paulwerelds@204
    78
          foreach (SMART.DriveAttribute attr in attributes) {
paulwerelds@204
    79
            if (attr.ID == SMART.AttributeID.Temperature
paulwerelds@204
    80
                || attr.ID == SMART.AttributeID.DriveTemperature
paulwerelds@204
    81
                || attr.ID == SMART.AttributeID.AirflowTemperature) {
paulwerelds@204
    82
              attribute = i;
paulwerelds@204
    83
              break;
paulwerelds@204
    84
            }
paulwerelds@204
    85
            i++;
paulwerelds@204
    86
          }
paulwerelds@204
    87
paulwerelds@204
    88
          if (attribute >= 0)
paulwerelds@204
    89
          {
paulwerelds@204
    90
            hardware.Add(new HDD(name, handle, drive, attribute, settings));
moel@1
    91
            continue;
paulwerelds@204
    92
          }
paulwerelds@204
    93
        }
moel@1
    94
paulwerelds@204
    95
        SMART.CloseHandle(handle);
moel@1
    96
      }
moel@1
    97
    }
moel@1
    98
moel@1
    99
    public IHardware[] Hardware {
moel@1
   100
      get {
moel@1
   101
        return hardware.ToArray();
moel@1
   102
      }
moel@1
   103
    }
moel@1
   104
moel@1
   105
    public string GetReport() {
paulwerelds@204
   106
      int p = (int)Environment.OSVersion.Platform;
paulwerelds@204
   107
      if (p == 4 || p == 128) return null;
paulwerelds@204
   108
paulwerelds@204
   109
      StringBuilder r = new StringBuilder();
paulwerelds@204
   110
paulwerelds@204
   111
      r.AppendLine("S.M.A.R.T Data");
paulwerelds@204
   112
      r.AppendLine();
paulwerelds@204
   113
paulwerelds@204
   114
      for (int drive = 0; drive < MAX_DRIVES; drive++) {
paulwerelds@204
   115
        IntPtr handle = SMART.OpenPhysicalDrive(drive);
paulwerelds@204
   116
paulwerelds@204
   117
        if (handle == SMART.INVALID_HANDLE_VALUE)
paulwerelds@204
   118
          continue;
paulwerelds@204
   119
paulwerelds@204
   120
        if (!SMART.EnableSmart(handle, drive)) {
paulwerelds@204
   121
          SMART.CloseHandle(handle);
paulwerelds@204
   122
          continue;
paulwerelds@204
   123
        }
paulwerelds@204
   124
paulwerelds@204
   125
        string name = SMART.ReadName(handle, drive);
paulwerelds@204
   126
        if (name == null) {
paulwerelds@204
   127
          SMART.CloseHandle(handle);
paulwerelds@204
   128
          continue;
paulwerelds@204
   129
        }
paulwerelds@204
   130
paulwerelds@204
   131
        SMART.DriveAttribute[] attributes = SMART.ReadSmart(handle, drive);
paulwerelds@204
   132
paulwerelds@204
   133
        if (attributes != null) {
paulwerelds@204
   134
          r.AppendLine("Drive name: " + name);
paulwerelds@204
   135
          r.AppendLine();
paulwerelds@204
   136
          r.AppendFormat(" {0}{1}{2}{3}{4}{5}",
paulwerelds@204
   137
                          ("ID").PadRight(6),
paulwerelds@204
   138
                          ("RawValue").PadRight(20),
paulwerelds@204
   139
                          ("WorstValue").PadRight(12),
paulwerelds@204
   140
                          ("AttrValue").PadRight(12),
paulwerelds@204
   141
                          ("Name"),
paulwerelds@204
   142
                          Environment.NewLine);
paulwerelds@204
   143
paulwerelds@204
   144
          foreach (SMART.DriveAttribute attr in attributes) {
paulwerelds@204
   145
            if (attr.ID == 0) continue;
paulwerelds@204
   146
            string raw = BitConverter.ToString(attr.RawValue);
paulwerelds@204
   147
            r.AppendFormat(" {0}{1}{2}{3}{4}{5}",
paulwerelds@204
   148
                           attr.ID.ToString("d").PadRight(6), 
paulwerelds@204
   149
                           raw.Replace("-", " ").PadRight(20),
paulwerelds@204
   150
                           attr.WorstValue.ToString().PadRight(12),
paulwerelds@204
   151
                           attr.AttrValue.ToString().PadRight(12),
paulwerelds@204
   152
                           attr.ID,
paulwerelds@204
   153
                           Environment.NewLine)
paulwerelds@204
   154
              ;
paulwerelds@204
   155
          }
paulwerelds@204
   156
          r.AppendLine();
paulwerelds@204
   157
        }
paulwerelds@204
   158
paulwerelds@204
   159
        SMART.CloseHandle(handle);
paulwerelds@204
   160
      }
paulwerelds@204
   161
paulwerelds@204
   162
      return r.ToString();
moel@1
   163
    }
moel@1
   164
moel@1
   165
    public void Close() {
moel@1
   166
      foreach (HDD hdd in hardware) 
moel@1
   167
        hdd.Close();
moel@1
   168
    }
moel@1
   169
  }
moel@1
   170
}