Hardware/LPC/LMSensors.cs
author moel.mich
Thu, 16 Feb 2012 22:10:30 +0000
changeset 341 5172a92c5c20
parent 268 844ba72c11de
child 344 3145aadca3d2
permissions -rwxr-xr-x
Added experimental support for ITE IT8771E super I/O chips.
     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-2011
    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 using System.Text;
    42 
    43 namespace OpenHardwareMonitor.Hardware.LPC {
    44 
    45   internal class LMSensors {
    46 
    47     private readonly List<LMChip> lmChips = new List<LMChip>();
    48 
    49     public LMSensors() {
    50       string[] basePaths = Directory.GetDirectories("/sys/class/hwmon/");
    51       foreach (string basePath in basePaths) {
    52         foreach (string devicePath in new[] { "/device", "" }) {
    53           string path = basePath + devicePath;
    54 
    55           string name = null;
    56           try {
    57             using (StreamReader reader = new StreamReader(path + "/name"))
    58               name = reader.ReadLine();
    59           } catch (IOException) { }
    60 
    61           switch (name) {
    62             case "atk0110":
    63               lmChips.Add(new LMChip(Chip.ATK0110, path)); break;
    64 
    65             case "f71858fg":
    66               lmChips.Add(new LMChip(Chip.F71858, path)); break;
    67             case "f71862fg":
    68               lmChips.Add(new LMChip(Chip.F71862, path)); break;
    69             case "f71882fg":
    70               lmChips.Add(new LMChip(Chip.F71882, path)); break;
    71             case "f71889fg":
    72               lmChips.Add(new LMChip(Chip.F71889F, path)); break;
    73 
    74             case "it8712":
    75               lmChips.Add(new LMChip(Chip.IT8712F, path)); break;
    76             case "it8716":
    77               lmChips.Add(new LMChip(Chip.IT8716F, path)); break;
    78             case "it8718":
    79               lmChips.Add(new LMChip(Chip.IT8718F, path)); break;
    80             case "it8720":
    81               lmChips.Add(new LMChip(Chip.IT8720F, path)); break;
    82 
    83             case "w83627ehf":
    84               lmChips.Add(new LMChip(Chip.W83627EHF, path)); break;
    85             case "w83627dhg":
    86               lmChips.Add(new LMChip(Chip.W83627DHG, path)); break;
    87             case "w83667hg":
    88               lmChips.Add(new LMChip(Chip.W83667HG, path)); break;
    89             case "w83627hf":
    90               lmChips.Add(new LMChip(Chip.W83627HF, path)); break;
    91             case "w83627thf":
    92               lmChips.Add(new LMChip(Chip.W83627THF, path)); break;
    93             case "w83687thf":
    94               lmChips.Add(new LMChip(Chip.W83687THF, path)); break;
    95           }
    96         }
    97       }
    98     }
    99 
   100     public void Close() {
   101       foreach (LMChip lmChip in lmChips)
   102         lmChip.Close();
   103     }
   104 
   105     public ISuperIO[] SuperIO {
   106       get {
   107         return lmChips.ToArray();
   108       }
   109     }
   110 
   111     private class LMChip : ISuperIO {
   112 
   113       private string path;
   114       private readonly Chip chip;
   115 
   116       private readonly float?[] voltages;
   117       private readonly float?[] temperatures;
   118       private readonly float?[] fans;
   119       private readonly float?[] controls;
   120 
   121       private readonly FileStream[] voltageStreams;
   122       private readonly FileStream[] temperatureStreams;
   123       private readonly FileStream[] fanStreams;
   124 
   125       public Chip Chip { get { return chip; } }
   126       public float?[] Voltages { get { return voltages; } }
   127       public float?[] Temperatures { get { return temperatures; } }
   128       public float?[] Fans { get { return fans; } }
   129       public float?[] Controls { get { return controls; } }
   130 
   131       public LMChip(Chip chip, string path) {
   132         this.path = path;
   133         this.chip = chip;
   134 
   135         string[] voltagePaths = Directory.GetFiles(path, "in*_input");
   136         this.voltages = new float?[voltagePaths.Length];
   137         this.voltageStreams = new FileStream[voltagePaths.Length];
   138         for (int i = 0; i < voltagePaths.Length; i++)
   139           voltageStreams[i] = new FileStream(voltagePaths[i],
   140             FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
   141 
   142         string[] temperaturePaths = Directory.GetFiles(path, "temp*_input");
   143         this.temperatures = new float?[temperaturePaths.Length];
   144         this.temperatureStreams = new FileStream[temperaturePaths.Length];
   145         for (int i = 0; i < temperaturePaths.Length; i++)
   146           temperatureStreams[i] = new FileStream(temperaturePaths[i],
   147             FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
   148 
   149         string[] fanPaths = Directory.GetFiles(path, "fan*_input");
   150         this.fans = new float?[fanPaths.Length];
   151         this.fanStreams = new FileStream[fanPaths.Length];
   152         for (int i = 0; i < fanPaths.Length; i++)
   153           fanStreams[i] = new FileStream(fanPaths[i],
   154             FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
   155 
   156         this.controls = new float?[0];
   157       }
   158 
   159       public byte? ReadGPIO(int index) {
   160         return null;
   161       }
   162 
   163       public void WriteGPIO(int index, byte value) { }
   164 
   165       public string GetReport() {
   166         return null;
   167       }
   168 
   169       public void SetControl(int index, byte? value) { }   
   170 
   171       private string ReadFirstLine(Stream stream) {
   172         StringBuilder sb = new StringBuilder();
   173         try {
   174           stream.Seek(0, SeekOrigin.Begin);
   175           int b = stream.ReadByte();
   176           while (b != -1 && b != 10) {
   177             sb.Append((char)b);
   178             b = stream.ReadByte();
   179           }
   180         } catch { }
   181         return sb.ToString();
   182       }
   183 
   184       public void Update() {
   185         for (int i = 0; i < voltages.Length; i++) {
   186           string s = ReadFirstLine(voltageStreams[i]);
   187           try {
   188             voltages[i] = 0.001f *
   189               long.Parse(s, CultureInfo.InvariantCulture);
   190           } catch {
   191             voltages[i] = null;
   192           }
   193         }
   194 
   195         for (int i = 0; i < temperatures.Length; i++) {
   196           string s = ReadFirstLine(temperatureStreams[i]);
   197           try {
   198             temperatures[i] = 0.001f *
   199               long.Parse(s, CultureInfo.InvariantCulture);
   200           } catch {
   201             temperatures[i] = null;
   202           }
   203         }
   204 
   205         for (int i = 0; i < fans.Length; i++) {
   206           string s = ReadFirstLine(fanStreams[i]);
   207           try {
   208             fans[i] = long.Parse(s, CultureInfo.InvariantCulture);
   209           } catch {
   210             fans[i] = null;
   211           }
   212         }
   213       }
   214 
   215       public void Close() {
   216         foreach (FileStream stream in voltageStreams)
   217           stream.Close();
   218         foreach (FileStream stream in temperatureStreams)
   219           stream.Close();
   220         foreach (FileStream stream in fanStreams)
   221           stream.Close();
   222       }
   223     }
   224   }
   225 }