Hardware/LPC/LMSensors.cs
author moel.mich
Wed, 18 Jul 2012 22:47:30 +0000
changeset 368 1036b453f1f6
parent 344 3145aadca3d2
child 388 c25816e366bd
permissions -rwxr-xr-x
Added a missing default case in the mainboard specific configuration selection. The missing default case prevented sensor data from getting displayed on unknown Asus mainboards with NCT6779D super I/O.
moel@136
     1
/*
moel@136
     2
 
moel@344
     3
  This Source Code Form is subject to the terms of the Mozilla Public
moel@344
     4
  License, v. 2.0. If a copy of the MPL was not distributed with this
moel@344
     5
  file, You can obtain one at http://mozilla.org/MPL/2.0/.
moel@136
     6
 
moel@353
     7
  Copyright (C) 2009-2012 Michael Möller <mmoeller@openhardwaremonitor.org>
moel@344
     8
	
moel@136
     9
*/
moel@136
    10
moel@136
    11
using System.Collections.Generic;
moel@166
    12
using System.Globalization;
moel@136
    13
using System.IO;
moel@268
    14
using System.Text;
moel@136
    15
moel@136
    16
namespace OpenHardwareMonitor.Hardware.LPC {
moel@136
    17
moel@165
    18
  internal class LMSensors {
moel@136
    19
moel@195
    20
    private readonly List<LMChip> lmChips = new List<LMChip>();
moel@136
    21
moel@136
    22
    public LMSensors() {
moel@266
    23
      string[] basePaths = Directory.GetDirectories("/sys/class/hwmon/");
moel@266
    24
      foreach (string basePath in basePaths) {
moel@266
    25
        foreach (string devicePath in new[] { "/device", "" }) {
moel@266
    26
          string path = basePath + devicePath;
moel@136
    27
moel@266
    28
          string name = null;
moel@266
    29
          try {
moel@266
    30
            using (StreamReader reader = new StreamReader(path + "/name"))
moel@266
    31
              name = reader.ReadLine();
moel@266
    32
          } catch (IOException) { }
moel@136
    33
moel@266
    34
          switch (name) {
moel@266
    35
            case "atk0110":
moel@266
    36
              lmChips.Add(new LMChip(Chip.ATK0110, path)); break;
moel@266
    37
moel@266
    38
            case "f71858fg":
moel@266
    39
              lmChips.Add(new LMChip(Chip.F71858, path)); break;
moel@266
    40
            case "f71862fg":
moel@266
    41
              lmChips.Add(new LMChip(Chip.F71862, path)); break;
moel@266
    42
            case "f71882fg":
moel@266
    43
              lmChips.Add(new LMChip(Chip.F71882, path)); break;
moel@266
    44
            case "f71889fg":
moel@266
    45
              lmChips.Add(new LMChip(Chip.F71889F, path)); break;
moel@266
    46
moel@353
    47
            case "it8705":
moel@353
    48
              lmChips.Add(new LMChip(Chip.IT8705F, path)); break;
moel@266
    49
            case "it8712":
moel@266
    50
              lmChips.Add(new LMChip(Chip.IT8712F, path)); break;
moel@266
    51
            case "it8716":
moel@266
    52
              lmChips.Add(new LMChip(Chip.IT8716F, path)); break;
moel@266
    53
            case "it8718":
moel@266
    54
              lmChips.Add(new LMChip(Chip.IT8718F, path)); break;
moel@266
    55
            case "it8720":
moel@266
    56
              lmChips.Add(new LMChip(Chip.IT8720F, path)); break;
moel@266
    57
moel@266
    58
            case "w83627ehf":
moel@266
    59
              lmChips.Add(new LMChip(Chip.W83627EHF, path)); break;
moel@266
    60
            case "w83627dhg":
moel@266
    61
              lmChips.Add(new LMChip(Chip.W83627DHG, path)); break;
moel@266
    62
            case "w83667hg":
moel@266
    63
              lmChips.Add(new LMChip(Chip.W83667HG, path)); break;
moel@266
    64
            case "w83627hf":
moel@266
    65
              lmChips.Add(new LMChip(Chip.W83627HF, path)); break;
moel@266
    66
            case "w83627thf":
moel@266
    67
              lmChips.Add(new LMChip(Chip.W83627THF, path)); break;
moel@266
    68
            case "w83687thf":
moel@266
    69
              lmChips.Add(new LMChip(Chip.W83687THF, path)); break;
moel@266
    70
          }
moel@136
    71
        }
moel@136
    72
      }
moel@136
    73
    }
moel@136
    74
moel@136
    75
    public void Close() {
moel@136
    76
      foreach (LMChip lmChip in lmChips)
moel@136
    77
        lmChip.Close();
moel@136
    78
    }
moel@136
    79
moel@136
    80
    public ISuperIO[] SuperIO {
moel@136
    81
      get {
moel@136
    82
        return lmChips.ToArray();
moel@136
    83
      }
moel@136
    84
    }
moel@136
    85
moel@136
    86
    private class LMChip : ISuperIO {
moel@136
    87
moel@136
    88
      private string path;
moel@195
    89
      private readonly Chip chip;
moel@136
    90
moel@195
    91
      private readonly float?[] voltages;
moel@195
    92
      private readonly float?[] temperatures;
moel@195
    93
      private readonly float?[] fans;
moel@323
    94
      private readonly float?[] controls;
moel@136
    95
moel@268
    96
      private readonly FileStream[] voltageStreams;
moel@268
    97
      private readonly FileStream[] temperatureStreams;
moel@268
    98
      private readonly FileStream[] fanStreams;
moel@136
    99
moel@136
   100
      public Chip Chip { get { return chip; } }
moel@136
   101
      public float?[] Voltages { get { return voltages; } }
moel@136
   102
      public float?[] Temperatures { get { return temperatures; } }
moel@136
   103
      public float?[] Fans { get { return fans; } }
moel@323
   104
      public float?[] Controls { get { return controls; } }
moel@136
   105
moel@136
   106
      public LMChip(Chip chip, string path) {
moel@136
   107
        this.path = path;
moel@136
   108
        this.chip = chip;
moel@136
   109
moel@136
   110
        string[] voltagePaths = Directory.GetFiles(path, "in*_input");
moel@136
   111
        this.voltages = new float?[voltagePaths.Length];
moel@268
   112
        this.voltageStreams = new FileStream[voltagePaths.Length];
moel@136
   113
        for (int i = 0; i < voltagePaths.Length; i++)
moel@268
   114
          voltageStreams[i] = new FileStream(voltagePaths[i],
moel@268
   115
            FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
moel@136
   116
moel@136
   117
        string[] temperaturePaths = Directory.GetFiles(path, "temp*_input");
moel@136
   118
        this.temperatures = new float?[temperaturePaths.Length];
moel@268
   119
        this.temperatureStreams = new FileStream[temperaturePaths.Length];
moel@136
   120
        for (int i = 0; i < temperaturePaths.Length; i++)
moel@268
   121
          temperatureStreams[i] = new FileStream(temperaturePaths[i],
moel@268
   122
            FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
moel@136
   123
moel@136
   124
        string[] fanPaths = Directory.GetFiles(path, "fan*_input");
moel@136
   125
        this.fans = new float?[fanPaths.Length];
moel@268
   126
        this.fanStreams = new FileStream[fanPaths.Length];
moel@136
   127
        for (int i = 0; i < fanPaths.Length; i++)
moel@268
   128
          fanStreams[i] = new FileStream(fanPaths[i],
moel@268
   129
            FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
moel@323
   130
moel@323
   131
        this.controls = new float?[0];
moel@136
   132
      }
moel@136
   133
moel@228
   134
      public byte? ReadGPIO(int index) {
moel@228
   135
        return null;
moel@228
   136
      }
moel@228
   137
moel@228
   138
      public void WriteGPIO(int index, byte value) { }
moel@228
   139
moel@136
   140
      public string GetReport() {
moel@136
   141
        return null;
moel@136
   142
      }
moel@136
   143
moel@323
   144
      public void SetControl(int index, byte? value) { }   
moel@323
   145
moel@268
   146
      private string ReadFirstLine(Stream stream) {
moel@268
   147
        StringBuilder sb = new StringBuilder();
moel@268
   148
        try {
moel@268
   149
          stream.Seek(0, SeekOrigin.Begin);
moel@268
   150
          int b = stream.ReadByte();
moel@268
   151
          while (b != -1 && b != 10) {
moel@268
   152
            sb.Append((char)b);
moel@268
   153
            b = stream.ReadByte();
moel@268
   154
          }
moel@268
   155
        } catch { }
moel@268
   156
        return sb.ToString();
moel@268
   157
      }
moel@268
   158
moel@136
   159
      public void Update() {
moel@136
   160
        for (int i = 0; i < voltages.Length; i++) {
moel@268
   161
          string s = ReadFirstLine(voltageStreams[i]);
moel@136
   162
          try {
moel@268
   163
            voltages[i] = 0.001f *
moel@166
   164
              long.Parse(s, CultureInfo.InvariantCulture);
moel@136
   165
          } catch {
moel@136
   166
            voltages[i] = null;
moel@136
   167
          }
moel@136
   168
        }
moel@136
   169
moel@136
   170
        for (int i = 0; i < temperatures.Length; i++) {
moel@268
   171
          string s = ReadFirstLine(temperatureStreams[i]);
moel@136
   172
          try {
moel@268
   173
            temperatures[i] = 0.001f *
moel@166
   174
              long.Parse(s, CultureInfo.InvariantCulture);
moel@136
   175
          } catch {
moel@136
   176
            temperatures[i] = null;
moel@136
   177
          }
moel@136
   178
        }
moel@136
   179
moel@136
   180
        for (int i = 0; i < fans.Length; i++) {
moel@268
   181
          string s = ReadFirstLine(fanStreams[i]);
moel@136
   182
          try {
moel@166
   183
            fans[i] = long.Parse(s, CultureInfo.InvariantCulture);
moel@136
   184
          } catch {
moel@136
   185
            fans[i] = null;
moel@136
   186
          }
moel@136
   187
        }
moel@136
   188
      }
moel@136
   189
moel@136
   190
      public void Close() {
moel@268
   191
        foreach (FileStream stream in voltageStreams)
moel@268
   192
          stream.Close();
moel@268
   193
        foreach (FileStream stream in temperatureStreams)
moel@268
   194
          stream.Close();
moel@268
   195
        foreach (FileStream stream in fanStreams)
moel@268
   196
          stream.Close();
moel@136
   197
      }
moel@136
   198
    }
moel@136
   199
  }
moel@136
   200
}