Hardware/LPC/LMSensors.cs
author moel.mich
Fri, 22 Jun 2012 10:05:23 +0000
changeset 352 0899f651d5cf
parent 323 3f2d9ebacf38
child 353 b4e37f5b2669
permissions -rwxr-xr-x
Integrated the patch for Fintek F71808E super I/O support developed by Andrey Mushatov.
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@344
     7
  Copyright (C) 2009-2011 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@266
    47
            case "it8712":
moel@266
    48
              lmChips.Add(new LMChip(Chip.IT8712F, path)); break;
moel@266
    49
            case "it8716":
moel@266
    50
              lmChips.Add(new LMChip(Chip.IT8716F, path)); break;
moel@266
    51
            case "it8718":
moel@266
    52
              lmChips.Add(new LMChip(Chip.IT8718F, path)); break;
moel@266
    53
            case "it8720":
moel@266
    54
              lmChips.Add(new LMChip(Chip.IT8720F, path)); break;
moel@266
    55
moel@266
    56
            case "w83627ehf":
moel@266
    57
              lmChips.Add(new LMChip(Chip.W83627EHF, path)); break;
moel@266
    58
            case "w83627dhg":
moel@266
    59
              lmChips.Add(new LMChip(Chip.W83627DHG, path)); break;
moel@266
    60
            case "w83667hg":
moel@266
    61
              lmChips.Add(new LMChip(Chip.W83667HG, path)); break;
moel@266
    62
            case "w83627hf":
moel@266
    63
              lmChips.Add(new LMChip(Chip.W83627HF, path)); break;
moel@266
    64
            case "w83627thf":
moel@266
    65
              lmChips.Add(new LMChip(Chip.W83627THF, path)); break;
moel@266
    66
            case "w83687thf":
moel@266
    67
              lmChips.Add(new LMChip(Chip.W83687THF, path)); break;
moel@266
    68
          }
moel@136
    69
        }
moel@136
    70
      }
moel@136
    71
    }
moel@136
    72
moel@136
    73
    public void Close() {
moel@136
    74
      foreach (LMChip lmChip in lmChips)
moel@136
    75
        lmChip.Close();
moel@136
    76
    }
moel@136
    77
moel@136
    78
    public ISuperIO[] SuperIO {
moel@136
    79
      get {
moel@136
    80
        return lmChips.ToArray();
moel@136
    81
      }
moel@136
    82
    }
moel@136
    83
moel@136
    84
    private class LMChip : ISuperIO {
moel@136
    85
moel@136
    86
      private string path;
moel@195
    87
      private readonly Chip chip;
moel@136
    88
moel@195
    89
      private readonly float?[] voltages;
moel@195
    90
      private readonly float?[] temperatures;
moel@195
    91
      private readonly float?[] fans;
moel@323
    92
      private readonly float?[] controls;
moel@136
    93
moel@268
    94
      private readonly FileStream[] voltageStreams;
moel@268
    95
      private readonly FileStream[] temperatureStreams;
moel@268
    96
      private readonly FileStream[] fanStreams;
moel@136
    97
moel@136
    98
      public Chip Chip { get { return chip; } }
moel@136
    99
      public float?[] Voltages { get { return voltages; } }
moel@136
   100
      public float?[] Temperatures { get { return temperatures; } }
moel@136
   101
      public float?[] Fans { get { return fans; } }
moel@323
   102
      public float?[] Controls { get { return controls; } }
moel@136
   103
moel@136
   104
      public LMChip(Chip chip, string path) {
moel@136
   105
        this.path = path;
moel@136
   106
        this.chip = chip;
moel@136
   107
moel@136
   108
        string[] voltagePaths = Directory.GetFiles(path, "in*_input");
moel@136
   109
        this.voltages = new float?[voltagePaths.Length];
moel@268
   110
        this.voltageStreams = new FileStream[voltagePaths.Length];
moel@136
   111
        for (int i = 0; i < voltagePaths.Length; i++)
moel@268
   112
          voltageStreams[i] = new FileStream(voltagePaths[i],
moel@268
   113
            FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
moel@136
   114
moel@136
   115
        string[] temperaturePaths = Directory.GetFiles(path, "temp*_input");
moel@136
   116
        this.temperatures = new float?[temperaturePaths.Length];
moel@268
   117
        this.temperatureStreams = new FileStream[temperaturePaths.Length];
moel@136
   118
        for (int i = 0; i < temperaturePaths.Length; i++)
moel@268
   119
          temperatureStreams[i] = new FileStream(temperaturePaths[i],
moel@268
   120
            FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
moel@136
   121
moel@136
   122
        string[] fanPaths = Directory.GetFiles(path, "fan*_input");
moel@136
   123
        this.fans = new float?[fanPaths.Length];
moel@268
   124
        this.fanStreams = new FileStream[fanPaths.Length];
moel@136
   125
        for (int i = 0; i < fanPaths.Length; i++)
moel@268
   126
          fanStreams[i] = new FileStream(fanPaths[i],
moel@268
   127
            FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
moel@323
   128
moel@323
   129
        this.controls = new float?[0];
moel@136
   130
      }
moel@136
   131
moel@228
   132
      public byte? ReadGPIO(int index) {
moel@228
   133
        return null;
moel@228
   134
      }
moel@228
   135
moel@228
   136
      public void WriteGPIO(int index, byte value) { }
moel@228
   137
moel@136
   138
      public string GetReport() {
moel@136
   139
        return null;
moel@136
   140
      }
moel@136
   141
moel@323
   142
      public void SetControl(int index, byte? value) { }   
moel@323
   143
moel@268
   144
      private string ReadFirstLine(Stream stream) {
moel@268
   145
        StringBuilder sb = new StringBuilder();
moel@268
   146
        try {
moel@268
   147
          stream.Seek(0, SeekOrigin.Begin);
moel@268
   148
          int b = stream.ReadByte();
moel@268
   149
          while (b != -1 && b != 10) {
moel@268
   150
            sb.Append((char)b);
moel@268
   151
            b = stream.ReadByte();
moel@268
   152
          }
moel@268
   153
        } catch { }
moel@268
   154
        return sb.ToString();
moel@268
   155
      }
moel@268
   156
moel@136
   157
      public void Update() {
moel@136
   158
        for (int i = 0; i < voltages.Length; i++) {
moel@268
   159
          string s = ReadFirstLine(voltageStreams[i]);
moel@136
   160
          try {
moel@268
   161
            voltages[i] = 0.001f *
moel@166
   162
              long.Parse(s, CultureInfo.InvariantCulture);
moel@136
   163
          } catch {
moel@136
   164
            voltages[i] = null;
moel@136
   165
          }
moel@136
   166
        }
moel@136
   167
moel@136
   168
        for (int i = 0; i < temperatures.Length; i++) {
moel@268
   169
          string s = ReadFirstLine(temperatureStreams[i]);
moel@136
   170
          try {
moel@268
   171
            temperatures[i] = 0.001f *
moel@166
   172
              long.Parse(s, CultureInfo.InvariantCulture);
moel@136
   173
          } catch {
moel@136
   174
            temperatures[i] = null;
moel@136
   175
          }
moel@136
   176
        }
moel@136
   177
moel@136
   178
        for (int i = 0; i < fans.Length; i++) {
moel@268
   179
          string s = ReadFirstLine(fanStreams[i]);
moel@136
   180
          try {
moel@166
   181
            fans[i] = long.Parse(s, CultureInfo.InvariantCulture);
moel@136
   182
          } catch {
moel@136
   183
            fans[i] = null;
moel@136
   184
          }
moel@136
   185
        }
moel@136
   186
      }
moel@136
   187
moel@136
   188
      public void Close() {
moel@268
   189
        foreach (FileStream stream in voltageStreams)
moel@268
   190
          stream.Close();
moel@268
   191
        foreach (FileStream stream in temperatureStreams)
moel@268
   192
          stream.Close();
moel@268
   193
        foreach (FileStream stream in fanStreams)
moel@268
   194
          stream.Close();
moel@136
   195
      }
moel@136
   196
    }
moel@136
   197
  }
moel@136
   198
}