Hardware/LPC/LMSensors.cs
author moel.mich
Mon, 23 Aug 2010 20:00:06 +0000
changeset 172 c9d8de472546
parent 166 fa9dfbfc4145
child 182 4801e9eaf979
permissions -rwxr-xr-x
Fixed a few problems with the Heatmaster.
moel@136
     1
/*
moel@136
     2
  
moel@136
     3
  Version: MPL 1.1/GPL 2.0/LGPL 2.1
moel@136
     4
moel@136
     5
  The contents of this file are subject to the Mozilla Public License Version
moel@136
     6
  1.1 (the "License"); you may not use this file except in compliance with
moel@136
     7
  the License. You may obtain a copy of the License at
moel@136
     8
 
moel@136
     9
  http://www.mozilla.org/MPL/
moel@136
    10
moel@136
    11
  Software distributed under the License is distributed on an "AS IS" basis,
moel@136
    12
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
moel@136
    13
  for the specific language governing rights and limitations under the License.
moel@136
    14
moel@136
    15
  The Original Code is the Open Hardware Monitor code.
moel@136
    16
moel@136
    17
  The Initial Developer of the Original Code is 
moel@136
    18
  Michael Möller <m.moeller@gmx.ch>.
moel@136
    19
  Portions created by the Initial Developer are Copyright (C) 2009-2010
moel@136
    20
  the Initial Developer. All Rights Reserved.
moel@136
    21
moel@136
    22
  Contributor(s):
moel@136
    23
moel@136
    24
  Alternatively, the contents of this file may be used under the terms of
moel@136
    25
  either the GNU General Public License Version 2 or later (the "GPL"), or
moel@136
    26
  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
moel@136
    27
  in which case the provisions of the GPL or the LGPL are applicable instead
moel@136
    28
  of those above. If you wish to allow use of your version of this file only
moel@136
    29
  under the terms of either the GPL or the LGPL, and not to allow others to
moel@136
    30
  use your version of this file under the terms of the MPL, indicate your
moel@136
    31
  decision by deleting the provisions above and replace them with the notice
moel@136
    32
  and other provisions required by the GPL or the LGPL. If you do not delete
moel@136
    33
  the provisions above, a recipient may use your version of this file under
moel@136
    34
  the terms of any one of the MPL, the GPL or the LGPL.
moel@136
    35
 
moel@136
    36
*/
moel@136
    37
moel@136
    38
using System;
moel@136
    39
using System.Collections.Generic;
moel@166
    40
using System.Globalization;
moel@136
    41
using System.IO;
moel@136
    42
moel@136
    43
namespace OpenHardwareMonitor.Hardware.LPC {
moel@136
    44
moel@165
    45
  internal class LMSensors {
moel@136
    46
moel@136
    47
    private List<LMChip> lmChips = new List<LMChip>();
moel@136
    48
moel@136
    49
    public LMSensors() {
moel@136
    50
      string[] devicePaths = Directory.GetDirectories("/sys/class/hwmon/");
moel@136
    51
      foreach (string path in devicePaths) {
moel@136
    52
        string name = null;
moel@136
    53
        try {
moel@167
    54
          using (StreamReader reader = new StreamReader(path + "/device/name")) 
moel@167
    55
            name = reader.ReadLine();
moel@136
    56
        } catch (IOException) { }
moel@136
    57
        switch (name) {
moel@136
    58
          case "f71858fg":
moel@136
    59
            lmChips.Add(new LMChip(Chip.F71858, path + "/device")); break;
moel@136
    60
          case "f71862fg":
moel@136
    61
            lmChips.Add(new LMChip(Chip.F71862, path + "/device")); break;
moel@136
    62
          case "f71882fg":
moel@136
    63
            lmChips.Add(new LMChip(Chip.F71882, path + "/device")); break;
moel@136
    64
          case "f71889fg":
moel@136
    65
            lmChips.Add(new LMChip(Chip.F71889F, path + "/device")); break;
moel@136
    66
moel@136
    67
          case "it8712":
moel@136
    68
            lmChips.Add(new LMChip(Chip.IT8712F, path + "/device")); break;
moel@136
    69
          case "it8716":
moel@136
    70
            lmChips.Add(new LMChip(Chip.IT8716F, path + "/device")); break;
moel@136
    71
          case "it8718":
moel@136
    72
            lmChips.Add(new LMChip(Chip.IT8718F, path + "/device")); break;
moel@136
    73
          case "it8720":
moel@136
    74
            lmChips.Add(new LMChip(Chip.IT8720F, path + "/device")); break;
moel@136
    75
moel@136
    76
          case "w83627ehf":
moel@136
    77
            lmChips.Add(new LMChip(Chip.W83627EHF, path + "/device")); break;
moel@136
    78
          case "w83627dhg":
moel@136
    79
            lmChips.Add(new LMChip(Chip.W83627DHG, path + "/device")); break;
moel@136
    80
          case "w83667hg":
moel@136
    81
            lmChips.Add(new LMChip(Chip.W83667HG, path + "/device")); break;
moel@136
    82
          case "w83627hf":
moel@136
    83
            lmChips.Add(new LMChip(Chip.W83627HF, path + "/device")); break;
moel@136
    84
          case "w83627thf":
moel@136
    85
            lmChips.Add(new LMChip(Chip.W83627THF, path + "/device")); break;
moel@136
    86
          case "w83687thf":
moel@136
    87
            lmChips.Add(new LMChip(Chip.W83687THF, path + "/device")); break;
moel@136
    88
        }
moel@136
    89
      }
moel@136
    90
    }
moel@136
    91
moel@136
    92
    public void Close() {
moel@136
    93
      foreach (LMChip lmChip in lmChips)
moel@136
    94
        lmChip.Close();
moel@136
    95
    }
moel@136
    96
moel@136
    97
    public ISuperIO[] SuperIO {
moel@136
    98
      get {
moel@136
    99
        return lmChips.ToArray();
moel@136
   100
      }
moel@136
   101
    }
moel@136
   102
moel@136
   103
    private class LMChip : ISuperIO {
moel@136
   104
moel@136
   105
      private string path;
moel@136
   106
      private Chip chip;
moel@136
   107
moel@136
   108
      private float?[] voltages;
moel@136
   109
      private float?[] temperatures;
moel@136
   110
      private float?[] fans;
moel@136
   111
moel@136
   112
      private StreamReader[] voltageReaders;
moel@136
   113
      private StreamReader[] temperatureReaders;
moel@136
   114
      private StreamReader[] fanReaders;
moel@136
   115
moel@136
   116
      public Chip Chip { get { return chip; } }
moel@136
   117
      public float?[] Voltages { get { return voltages; } }
moel@136
   118
      public float?[] Temperatures { get { return temperatures; } }
moel@136
   119
      public float?[] Fans { get { return fans; } }
moel@136
   120
moel@136
   121
moel@136
   122
      public LMChip(Chip chip, string path) {
moel@136
   123
        this.path = path;
moel@136
   124
        this.chip = chip;
moel@136
   125
moel@136
   126
        string[] voltagePaths = Directory.GetFiles(path, "in*_input");
moel@136
   127
        this.voltages = new float?[voltagePaths.Length];
moel@136
   128
        this.voltageReaders = new StreamReader[voltagePaths.Length];
moel@136
   129
        for (int i = 0; i < voltagePaths.Length; i++)
moel@136
   130
          voltageReaders[i] = new StreamReader(voltagePaths[i]);
moel@136
   131
moel@136
   132
        string[] temperaturePaths = Directory.GetFiles(path, "temp*_input");
moel@136
   133
        this.temperatures = new float?[temperaturePaths.Length];
moel@136
   134
        this.temperatureReaders = new StreamReader[temperaturePaths.Length];
moel@136
   135
        for (int i = 0; i < temperaturePaths.Length; i++)
moel@136
   136
          temperatureReaders[i] = new StreamReader(temperaturePaths[i]);
moel@136
   137
moel@136
   138
        string[] fanPaths = Directory.GetFiles(path, "fan*_input");
moel@136
   139
        this.fans = new float?[fanPaths.Length];
moel@136
   140
        this.fanReaders = new StreamReader[fanPaths.Length];
moel@136
   141
        for (int i = 0; i < fanPaths.Length; i++)
moel@136
   142
          fanReaders[i] = new StreamReader(fanPaths[i]);
moel@136
   143
      }
moel@136
   144
moel@136
   145
      public string GetReport() {
moel@136
   146
        return null;
moel@136
   147
      }
moel@136
   148
moel@136
   149
      public void Update() {
moel@136
   150
        for (int i = 0; i < voltages.Length; i++) {
moel@136
   151
          voltageReaders[i].BaseStream.Seek(0, SeekOrigin.Begin);
moel@136
   152
          string s = voltageReaders[i].ReadLine();
moel@136
   153
          try {
moel@166
   154
            voltages[i] = 0.001f * 
moel@166
   155
              long.Parse(s, CultureInfo.InvariantCulture);
moel@136
   156
          } catch {
moel@136
   157
            voltages[i] = null;
moel@136
   158
          }
moel@136
   159
        }
moel@136
   160
moel@136
   161
        for (int i = 0; i < temperatures.Length; i++) {
moel@136
   162
          temperatureReaders[i].BaseStream.Seek(0, SeekOrigin.Begin);
moel@136
   163
          string s = temperatureReaders[i].ReadLine();
moel@136
   164
          try {
moel@166
   165
            temperatures[i] = 0.001f * 
moel@166
   166
              long.Parse(s, CultureInfo.InvariantCulture);
moel@136
   167
          } catch {
moel@136
   168
            temperatures[i] = null;
moel@136
   169
          }
moel@136
   170
        }
moel@136
   171
moel@136
   172
        for (int i = 0; i < fans.Length; i++) {
moel@136
   173
          fanReaders[i].BaseStream.Seek(0, SeekOrigin.Begin);
moel@136
   174
          string s = fanReaders[i].ReadLine();
moel@136
   175
          try {
moel@166
   176
            fans[i] = long.Parse(s, CultureInfo.InvariantCulture);
moel@136
   177
          } catch {
moel@136
   178
            fans[i] = null;
moel@136
   179
          }
moel@136
   180
        }
moel@136
   181
      }
moel@136
   182
moel@136
   183
      public void Close() {
moel@136
   184
        foreach (StreamReader reader in voltageReaders)
moel@136
   185
          reader.Close();
moel@136
   186
        foreach (StreamReader reader in temperatureReaders)
moel@136
   187
          reader.Close();
moel@136
   188
        foreach (StreamReader reader in fanReaders)
moel@136
   189
          reader.Close();
moel@136
   190
      }
moel@136
   191
    }
moel@136
   192
  }
moel@136
   193
}