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