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