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