Hardware/LPC/LMSensors.cs
author moel.mich
Thu, 12 Aug 2010 20:53:27 +0000
changeset 166 fa9dfbfc4145
parent 165 813d8bc3192f
child 167 b7cc9d09aefe
permissions -rwxr-xr-x
Changed the project files to Visual Studio 2010. Fixed some Code Analysis warnings.
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@136
    54
          StreamReader reader = new StreamReader(path + "/device/name");
moel@136
    55
          name = reader.ReadLine();
moel@136
    56
          reader.Close();
moel@136
    57
        } catch (IOException) { }
moel@136
    58
        switch (name) {
moel@136
    59
          case "f71858fg":
moel@136
    60
            lmChips.Add(new LMChip(Chip.F71858, path + "/device")); break;
moel@136
    61
          case "f71862fg":
moel@136
    62
            lmChips.Add(new LMChip(Chip.F71862, path + "/device")); break;
moel@136
    63
          case "f71882fg":
moel@136
    64
            lmChips.Add(new LMChip(Chip.F71882, path + "/device")); break;
moel@136
    65
          case "f71889fg":
moel@136
    66
            lmChips.Add(new LMChip(Chip.F71889F, path + "/device")); break;
moel@136
    67
moel@136
    68
          case "it8712":
moel@136
    69
            lmChips.Add(new LMChip(Chip.IT8712F, path + "/device")); break;
moel@136
    70
          case "it8716":
moel@136
    71
            lmChips.Add(new LMChip(Chip.IT8716F, path + "/device")); break;
moel@136
    72
          case "it8718":
moel@136
    73
            lmChips.Add(new LMChip(Chip.IT8718F, path + "/device")); break;
moel@136
    74
          case "it8720":
moel@136
    75
            lmChips.Add(new LMChip(Chip.IT8720F, path + "/device")); break;
moel@136
    76
moel@136
    77
          case "w83627ehf":
moel@136
    78
            lmChips.Add(new LMChip(Chip.W83627EHF, path + "/device")); break;
moel@136
    79
          case "w83627dhg":
moel@136
    80
            lmChips.Add(new LMChip(Chip.W83627DHG, path + "/device")); break;
moel@136
    81
          case "w83667hg":
moel@136
    82
            lmChips.Add(new LMChip(Chip.W83667HG, path + "/device")); break;
moel@136
    83
          case "w83627hf":
moel@136
    84
            lmChips.Add(new LMChip(Chip.W83627HF, path + "/device")); break;
moel@136
    85
          case "w83627thf":
moel@136
    86
            lmChips.Add(new LMChip(Chip.W83627THF, path + "/device")); break;
moel@136
    87
          case "w83687thf":
moel@136
    88
            lmChips.Add(new LMChip(Chip.W83687THF, path + "/device")); break;
moel@136
    89
        }
moel@136
    90
      }
moel@136
    91
    }
moel@136
    92
moel@136
    93
    public void Close() {
moel@136
    94
      foreach (LMChip lmChip in lmChips)
moel@136
    95
        lmChip.Close();
moel@136
    96
    }
moel@136
    97
moel@136
    98
    public ISuperIO[] SuperIO {
moel@136
    99
      get {
moel@136
   100
        return lmChips.ToArray();
moel@136
   101
      }
moel@136
   102
    }
moel@136
   103
moel@136
   104
    private class LMChip : ISuperIO {
moel@136
   105
moel@136
   106
      private string path;
moel@136
   107
      private Chip chip;
moel@136
   108
moel@136
   109
      private float?[] voltages;
moel@136
   110
      private float?[] temperatures;
moel@136
   111
      private float?[] fans;
moel@136
   112
moel@136
   113
      private StreamReader[] voltageReaders;
moel@136
   114
      private StreamReader[] temperatureReaders;
moel@136
   115
      private StreamReader[] fanReaders;
moel@136
   116
moel@136
   117
      public Chip Chip { get { return chip; } }
moel@136
   118
      public float?[] Voltages { get { return voltages; } }
moel@136
   119
      public float?[] Temperatures { get { return temperatures; } }
moel@136
   120
      public float?[] Fans { get { return fans; } }
moel@136
   121
moel@136
   122
moel@136
   123
      public LMChip(Chip chip, string path) {
moel@136
   124
        this.path = path;
moel@136
   125
        this.chip = chip;
moel@136
   126
moel@136
   127
        string[] voltagePaths = Directory.GetFiles(path, "in*_input");
moel@136
   128
        this.voltages = new float?[voltagePaths.Length];
moel@136
   129
        this.voltageReaders = new StreamReader[voltagePaths.Length];
moel@136
   130
        for (int i = 0; i < voltagePaths.Length; i++)
moel@136
   131
          voltageReaders[i] = new StreamReader(voltagePaths[i]);
moel@136
   132
moel@136
   133
        string[] temperaturePaths = Directory.GetFiles(path, "temp*_input");
moel@136
   134
        this.temperatures = new float?[temperaturePaths.Length];
moel@136
   135
        this.temperatureReaders = new StreamReader[temperaturePaths.Length];
moel@136
   136
        for (int i = 0; i < temperaturePaths.Length; i++)
moel@136
   137
          temperatureReaders[i] = new StreamReader(temperaturePaths[i]);
moel@136
   138
moel@136
   139
        string[] fanPaths = Directory.GetFiles(path, "fan*_input");
moel@136
   140
        this.fans = new float?[fanPaths.Length];
moel@136
   141
        this.fanReaders = new StreamReader[fanPaths.Length];
moel@136
   142
        for (int i = 0; i < fanPaths.Length; i++)
moel@136
   143
          fanReaders[i] = new StreamReader(fanPaths[i]);
moel@136
   144
      }
moel@136
   145
moel@136
   146
      public string GetReport() {
moel@136
   147
        return null;
moel@136
   148
      }
moel@136
   149
moel@136
   150
      public void Update() {
moel@136
   151
        for (int i = 0; i < voltages.Length; i++) {
moel@136
   152
          voltageReaders[i].BaseStream.Seek(0, SeekOrigin.Begin);
moel@136
   153
          string s = voltageReaders[i].ReadLine();
moel@136
   154
          try {
moel@166
   155
            voltages[i] = 0.001f * 
moel@166
   156
              long.Parse(s, CultureInfo.InvariantCulture);
moel@136
   157
          } catch {
moel@136
   158
            voltages[i] = null;
moel@136
   159
          }
moel@136
   160
        }
moel@136
   161
moel@136
   162
        for (int i = 0; i < temperatures.Length; i++) {
moel@136
   163
          temperatureReaders[i].BaseStream.Seek(0, SeekOrigin.Begin);
moel@136
   164
          string s = temperatureReaders[i].ReadLine();
moel@136
   165
          try {
moel@166
   166
            temperatures[i] = 0.001f * 
moel@166
   167
              long.Parse(s, CultureInfo.InvariantCulture);
moel@136
   168
          } catch {
moel@136
   169
            temperatures[i] = null;
moel@136
   170
          }
moel@136
   171
        }
moel@136
   172
moel@136
   173
        for (int i = 0; i < fans.Length; i++) {
moel@136
   174
          fanReaders[i].BaseStream.Seek(0, SeekOrigin.Begin);
moel@136
   175
          string s = fanReaders[i].ReadLine();
moel@136
   176
          try {
moel@166
   177
            fans[i] = long.Parse(s, CultureInfo.InvariantCulture);
moel@136
   178
          } catch {
moel@136
   179
            fans[i] = null;
moel@136
   180
          }
moel@136
   181
        }
moel@136
   182
      }
moel@136
   183
moel@136
   184
      public void Close() {
moel@136
   185
        foreach (StreamReader reader in voltageReaders)
moel@136
   186
          reader.Close();
moel@136
   187
        foreach (StreamReader reader in temperatureReaders)
moel@136
   188
          reader.Close();
moel@136
   189
        foreach (StreamReader reader in fanReaders)
moel@136
   190
          reader.Close();
moel@136
   191
      }
moel@136
   192
    }
moel@136
   193
  }
moel@136
   194
}