Added support for the Fintek F71869A (F71868AD) super I/O chip.
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-2013 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;
18 using OpenHardwareMonitor.Collections;
20 namespace OpenHardwareMonitor.Hardware.HDD {
21 internal abstract class AbstractHarddrive : Hardware {
23 private const int UPDATE_DIVIDER = 30; // update only every 30s
25 // array of all harddrive types, matching type is searched in this order
26 private static Type[] hddTypes = {
33 typeof(GenericHarddisk)
36 private string firmwareRevision;
37 private readonly ISmart smart;
39 private readonly IntPtr handle;
40 private readonly int index;
43 private IList<SmartAttribute> smartAttributes;
44 private IDictionary<SmartAttribute, Sensor> sensors;
46 private DriveInfo[] driveInfos;
47 private Sensor usageSensor;
49 protected AbstractHarddrive(ISmart smart, string name,
50 string firmwareRevision, int index,
51 IEnumerable<SmartAttribute> smartAttributes, ISettings settings)
52 : base(name, new Identifier("hdd",
53 index.ToString(CultureInfo.InvariantCulture)), settings)
55 this.firmwareRevision = firmwareRevision;
57 handle = smart.OpenDrive(index);
59 if (handle != smart.InvalidHandle)
60 smart.EnableSmart(handle, index);
65 this.smartAttributes = new List<SmartAttribute>(smartAttributes);
67 string[] logicalDrives = smart.GetLogicalDrives(index);
68 List<DriveInfo> driveInfoList = new List<DriveInfo>(logicalDrives.Length);
69 foreach (string logicalDrive in logicalDrives) {
71 DriveInfo di = new DriveInfo(logicalDrive);
73 driveInfoList.Add(new DriveInfo(logicalDrive));
74 } catch (ArgumentException) { } catch (IOException) { }
76 driveInfos = driveInfoList.ToArray();
81 public static AbstractHarddrive CreateInstance(ISmart smart,
82 int driveIndex, ISettings settings)
84 IntPtr deviceHandle = smart.OpenDrive(driveIndex);
87 string firmwareRevision = null;
88 DriveAttributeValue[] values = { };
90 if (deviceHandle != smart.InvalidHandle) {
91 bool nameValid = smart.ReadNameAndFirmwareRevision(deviceHandle,
92 driveIndex, out name, out firmwareRevision);
93 bool smartEnabled = smart.EnableSmart(deviceHandle, driveIndex);
96 values = smart.ReadSmartData(deviceHandle, driveIndex);
98 smart.CloseHandle(deviceHandle);
102 firmwareRevision = null;
105 string[] logicalDrives = smart.GetLogicalDrives(driveIndex);
106 if (logicalDrives == null || logicalDrives.Length == 0)
110 if (string.IsNullOrEmpty(name))
111 name = "Generic Hard Disk";
113 if (string.IsNullOrEmpty(firmwareRevision))
114 firmwareRevision = "Unknown";
116 foreach (Type type in hddTypes) {
117 // get the array of name prefixes for the current type
118 NamePrefixAttribute[] namePrefixes = type.GetCustomAttributes(
119 typeof(NamePrefixAttribute), true) as NamePrefixAttribute[];
121 // get the array of the required SMART attributes for the current type
122 RequireSmartAttribute[] requiredAttributes = type.GetCustomAttributes(
123 typeof(RequireSmartAttribute), true) as RequireSmartAttribute[];
125 // check if all required attributes are present
126 bool allRequiredAttributesFound = true;
127 foreach (var requireAttribute in requiredAttributes) {
128 bool adttributeFound = false;
129 foreach (DriveAttributeValue value in values) {
130 if (value.Identifier == requireAttribute.AttributeId) {
131 adttributeFound = true;
135 if (!adttributeFound) {
136 allRequiredAttributesFound = false;
141 // if an attribute is missing, then try the next type
142 if (!allRequiredAttributesFound)
145 // check if there is a matching name prefix for this type
146 foreach (NamePrefixAttribute prefix in namePrefixes) {
147 if (name.StartsWith(prefix.Prefix, StringComparison.InvariantCulture))
148 return Activator.CreateInstance(type, smart, name, firmwareRevision,
149 driveIndex, settings) as AbstractHarddrive;
153 // no matching type has been found
157 private void CreateSensors() {
158 sensors = new Dictionary<SmartAttribute, Sensor>();
160 if (handle != smart.InvalidHandle) {
161 IList<Pair<SensorType, int>> sensorTypeAndChannels =
162 new List<Pair<SensorType, int>>();
164 DriveAttributeValue[] values = smart.ReadSmartData(handle, index);
166 foreach (SmartAttribute attribute in smartAttributes) {
167 if (!attribute.SensorType.HasValue)
171 foreach (DriveAttributeValue value in values) {
172 if (value.Identifier == attribute.Identifier) {
180 Pair<SensorType, int> pair = new Pair<SensorType, int>(
181 attribute.SensorType.Value, attribute.SensorChannel);
183 if (!sensorTypeAndChannels.Contains(pair)) {
184 Sensor sensor = new Sensor(attribute.Name,
185 attribute.SensorChannel, attribute.DefaultHiddenSensor,
186 attribute.SensorType.Value, this, attribute.ParameterDescriptions,
189 sensors.Add(attribute, sensor);
190 ActivateSensor(sensor);
191 sensorTypeAndChannels.Add(pair);
196 if (driveInfos.Length > 0) {
198 new Sensor("Used Space", 0, SensorType.Load, this, settings);
199 ActivateSensor(usageSensor);
203 public override HardwareType HardwareType {
204 get { return HardwareType.HDD; }
207 public virtual void UpdateAdditionalSensors(DriveAttributeValue[] values) {}
209 public override void Update() {
211 if (handle != smart.InvalidHandle) {
212 DriveAttributeValue[] values = smart.ReadSmartData(handle, index);
214 foreach (KeyValuePair<SmartAttribute, Sensor> keyValuePair in sensors)
216 SmartAttribute attribute = keyValuePair.Key;
217 foreach (DriveAttributeValue value in values) {
218 if (value.Identifier == attribute.Identifier) {
219 Sensor sensor = keyValuePair.Value;
220 sensor.Value = attribute.ConvertValue(value, sensor.Parameters);
225 UpdateAdditionalSensors(values);
228 if (usageSensor != null) {
230 long totalFreeSpace = 0;
232 for (int i = 0; i < driveInfos.Length; i++) {
233 if (!driveInfos[i].IsReady)
236 totalSize += driveInfos[i].TotalSize;
237 totalFreeSpace += driveInfos[i].TotalFreeSpace;
238 } catch (IOException) { } catch (UnauthorizedAccessException) { }
241 usageSensor.Value = 100.0f - (100.0f * totalFreeSpace) / totalSize;
243 usageSensor.Value = null;
249 count %= UPDATE_DIVIDER;
252 public override string GetReport() {
253 StringBuilder r = new StringBuilder();
255 r.AppendLine(this.GetType().Name);
257 r.AppendLine("Drive name: " + name);
258 r.AppendLine("Firmware version: " + firmwareRevision);
261 if (handle != smart.InvalidHandle) {
262 DriveAttributeValue[] values = smart.ReadSmartData(handle, index);
263 DriveThresholdValue[] thresholds =
264 smart.ReadSmartThresholds(handle, index);
266 if (values.Length > 0) {
267 r.AppendFormat(CultureInfo.InvariantCulture,
268 " {0}{1}{2}{3}{4}{5}{6}{7}",
270 ("Description").PadRight(35),
271 ("Raw Value").PadRight(13),
272 ("Worst").PadRight(6),
273 ("Value").PadRight(6),
274 ("Thres").PadRight(6),
275 ("Physical").PadRight(8),
276 Environment.NewLine);
278 foreach (DriveAttributeValue value in values) {
279 if (value.Identifier == 0x00)
282 byte? threshold = null;
283 foreach (DriveThresholdValue t in thresholds) {
284 if (t.Identifier == value.Identifier) {
285 threshold = t.Threshold;
289 string description = "Unknown";
290 float? physical = null;
291 foreach (SmartAttribute a in smartAttributes) {
292 if (a.Identifier == value.Identifier) {
293 description = a.Name;
294 if (a.HasRawValueConversion | a.SensorType.HasValue)
295 physical = a.ConvertValue(value, null);
301 string raw = BitConverter.ToString(value.RawValue);
302 r.AppendFormat(CultureInfo.InvariantCulture,
303 " {0}{1}{2}{3}{4}{5}{6}{7}",
304 value.Identifier.ToString("X2").PadRight(3),
305 description.PadRight(35),
306 raw.Replace("-", "").PadRight(13),
307 value.WorstValue.ToString(CultureInfo.InvariantCulture).PadRight(6),
308 value.AttrValue.ToString(CultureInfo.InvariantCulture).PadRight(6),
309 (threshold.HasValue ? threshold.Value.ToString(
310 CultureInfo.InvariantCulture) : "-").PadRight(6),
311 (physical.HasValue ? physical.Value.ToString(
312 CultureInfo.InvariantCulture) : "-").PadRight(8),
313 Environment.NewLine);
319 foreach (DriveInfo di in driveInfos) {
323 r.AppendLine("Logical drive name: " + di.Name);
324 r.AppendLine("Format: " + di.DriveFormat);
325 r.AppendLine("Total size: " + di.TotalSize);
326 r.AppendLine("Total free space: " + di.TotalFreeSpace);
328 } catch (IOException) { } catch (UnauthorizedAccessException) { }
334 protected static float RawToInt(byte[] raw, byte value,
335 IReadOnlyArray<IParameter> parameters)
337 return (raw[3] << 24) | (raw[2] << 16) | (raw[1] << 8) | raw[0];
340 public override void Close() {
341 if (handle != smart.InvalidHandle)
342 smart.CloseHandle(handle);
347 public override void Traverse(IVisitor visitor) {
348 foreach (ISensor sensor in Sensors)
349 sensor.Accept(visitor);