Hardware/LPC/LMSensors.cs
author moel.mich
Sat, 19 Mar 2011 22:55:05 +0000
changeset 265 961c07a3bd78
parent 195 0ee888c485d5
child 266 2687ac753d90
permissions -rwxr-xr-x
Fixed Issue 156.
     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):
    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.Collections.Generic;
    39 using System.Globalization;
    40 using System.IO;
    41 
    42 namespace OpenHardwareMonitor.Hardware.LPC {
    43 
    44   internal class LMSensors {
    45 
    46     private readonly List<LMChip> lmChips = new List<LMChip>();
    47 
    48     public LMSensors() {
    49       string[] devicePaths = Directory.GetDirectories("/sys/class/hwmon/");
    50       foreach (string path in devicePaths) {
    51         string name = null;
    52         try {
    53           using (StreamReader reader = new StreamReader(path + "/device/name")) 
    54             name = reader.ReadLine();
    55         } catch (IOException) { }
    56         switch (name) {
    57           case "f71858fg":
    58             lmChips.Add(new LMChip(Chip.F71858, path + "/device")); break;
    59           case "f71862fg":
    60             lmChips.Add(new LMChip(Chip.F71862, path + "/device")); break;
    61           case "f71882fg":
    62             lmChips.Add(new LMChip(Chip.F71882, path + "/device")); break;
    63           case "f71889fg":
    64             lmChips.Add(new LMChip(Chip.F71889F, path + "/device")); break;
    65 
    66           case "it8712":
    67             lmChips.Add(new LMChip(Chip.IT8712F, path + "/device")); break;
    68           case "it8716":
    69             lmChips.Add(new LMChip(Chip.IT8716F, path + "/device")); break;
    70           case "it8718":
    71             lmChips.Add(new LMChip(Chip.IT8718F, path + "/device")); break;
    72           case "it8720":
    73             lmChips.Add(new LMChip(Chip.IT8720F, path + "/device")); break;
    74 
    75           case "w83627ehf":
    76             lmChips.Add(new LMChip(Chip.W83627EHF, path + "/device")); break;
    77           case "w83627dhg":
    78             lmChips.Add(new LMChip(Chip.W83627DHG, path + "/device")); break;
    79           case "w83667hg":
    80             lmChips.Add(new LMChip(Chip.W83667HG, path + "/device")); break;
    81           case "w83627hf":
    82             lmChips.Add(new LMChip(Chip.W83627HF, path + "/device")); break;
    83           case "w83627thf":
    84             lmChips.Add(new LMChip(Chip.W83627THF, path + "/device")); break;
    85           case "w83687thf":
    86             lmChips.Add(new LMChip(Chip.W83687THF, path + "/device")); break;
    87         }
    88       }
    89     }
    90 
    91     public void Close() {
    92       foreach (LMChip lmChip in lmChips)
    93         lmChip.Close();
    94     }
    95 
    96     public ISuperIO[] SuperIO {
    97       get {
    98         return lmChips.ToArray();
    99       }
   100     }
   101 
   102     private class LMChip : ISuperIO {
   103 
   104       private string path;
   105       private readonly Chip chip;
   106 
   107       private readonly float?[] voltages;
   108       private readonly float?[] temperatures;
   109       private readonly float?[] fans;
   110 
   111       private readonly StreamReader[] voltageReaders;
   112       private readonly StreamReader[] temperatureReaders;
   113       private readonly StreamReader[] fanReaders;
   114 
   115       public Chip Chip { get { return chip; } }
   116       public float?[] Voltages { get { return voltages; } }
   117       public float?[] Temperatures { get { return temperatures; } }
   118       public float?[] Fans { get { return fans; } }
   119 
   120 
   121       public LMChip(Chip chip, string path) {
   122         this.path = path;
   123         this.chip = chip;
   124 
   125         string[] voltagePaths = Directory.GetFiles(path, "in*_input");
   126         this.voltages = new float?[voltagePaths.Length];
   127         this.voltageReaders = new StreamReader[voltagePaths.Length];
   128         for (int i = 0; i < voltagePaths.Length; i++)
   129           voltageReaders[i] = new StreamReader(voltagePaths[i]);
   130 
   131         string[] temperaturePaths = Directory.GetFiles(path, "temp*_input");
   132         this.temperatures = new float?[temperaturePaths.Length];
   133         this.temperatureReaders = new StreamReader[temperaturePaths.Length];
   134         for (int i = 0; i < temperaturePaths.Length; i++)
   135           temperatureReaders[i] = new StreamReader(temperaturePaths[i]);
   136 
   137         string[] fanPaths = Directory.GetFiles(path, "fan*_input");
   138         this.fans = new float?[fanPaths.Length];
   139         this.fanReaders = new StreamReader[fanPaths.Length];
   140         for (int i = 0; i < fanPaths.Length; i++)
   141           fanReaders[i] = new StreamReader(fanPaths[i]);
   142       }
   143 
   144       public byte? ReadGPIO(int index) {
   145         return null;
   146       }
   147 
   148       public void WriteGPIO(int index, byte value) { }
   149 
   150       public string GetReport() {
   151         return null;
   152       }
   153 
   154       public void Update() {
   155         for (int i = 0; i < voltages.Length; i++) {
   156           voltageReaders[i].BaseStream.Seek(0, SeekOrigin.Begin);
   157           string s = voltageReaders[i].ReadLine();
   158           try {
   159             voltages[i] = 0.001f * 
   160               long.Parse(s, CultureInfo.InvariantCulture);
   161           } catch {
   162             voltages[i] = null;
   163           }
   164         }
   165 
   166         for (int i = 0; i < temperatures.Length; i++) {
   167           temperatureReaders[i].BaseStream.Seek(0, SeekOrigin.Begin);
   168           string s = temperatureReaders[i].ReadLine();
   169           try {
   170             temperatures[i] = 0.001f * 
   171               long.Parse(s, CultureInfo.InvariantCulture);
   172           } catch {
   173             temperatures[i] = null;
   174           }
   175         }
   176 
   177         for (int i = 0; i < fans.Length; i++) {
   178           fanReaders[i].BaseStream.Seek(0, SeekOrigin.Begin);
   179           string s = fanReaders[i].ReadLine();
   180           try {
   181             fans[i] = long.Parse(s, CultureInfo.InvariantCulture);
   182           } catch {
   183             fans[i] = null;
   184           }
   185         }
   186       }
   187 
   188       public void Close() {
   189         foreach (StreamReader reader in voltageReaders)
   190           reader.Close();
   191         foreach (StreamReader reader in temperatureReaders)
   192           reader.Close();
   193         foreach (StreamReader reader in fanReaders)
   194           reader.Close();
   195       }
   196     }
   197   }
   198 }