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