Hardware/LPC/LMSensors.cs
author StephaneLenclud
Mon, 04 Feb 2013 00:47:01 +0100
branchMiniDisplay
changeset 435 9ffcd8ed8537
parent 408 bbeb9004c491
permissions -rwxr-xr-x
Now calling display init. Not getting the expected error code though.
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@413
    72
            case "nct6779":
moel@413
    73
              lmChips.Add(new LMChip(Chip.NCT6779D, path)); break;
moel@413
    74
            case "nct6791":
moel@413
    75
              lmChips.Add(new LMChip(Chip.NCT6791D, path)); break;
moel@266
    76
moel@266
    77
            case "w83627ehf":
moel@266
    78
              lmChips.Add(new LMChip(Chip.W83627EHF, path)); break;
moel@266
    79
            case "w83627dhg":
moel@266
    80
              lmChips.Add(new LMChip(Chip.W83627DHG, path)); break;
moel@266
    81
            case "w83667hg":
moel@266
    82
              lmChips.Add(new LMChip(Chip.W83667HG, path)); break;
moel@266
    83
            case "w83627hf":
moel@266
    84
              lmChips.Add(new LMChip(Chip.W83627HF, path)); break;
moel@266
    85
            case "w83627thf":
moel@266
    86
              lmChips.Add(new LMChip(Chip.W83627THF, path)); break;
moel@266
    87
            case "w83687thf":
moel@266
    88
              lmChips.Add(new LMChip(Chip.W83687THF, path)); break;
moel@266
    89
          }
moel@136
    90
        }
moel@136
    91
      }
moel@136
    92
    }
moel@136
    93
moel@136
    94
    public void Close() {
moel@136
    95
      foreach (LMChip lmChip in lmChips)
moel@136
    96
        lmChip.Close();
moel@136
    97
    }
moel@136
    98
moel@136
    99
    public ISuperIO[] SuperIO {
moel@136
   100
      get {
moel@136
   101
        return lmChips.ToArray();
moel@136
   102
      }
moel@136
   103
    }
moel@136
   104
moel@136
   105
    private class LMChip : ISuperIO {
moel@136
   106
moel@136
   107
      private string path;
moel@195
   108
      private readonly Chip chip;
moel@136
   109
moel@195
   110
      private readonly float?[] voltages;
moel@195
   111
      private readonly float?[] temperatures;
moel@195
   112
      private readonly float?[] fans;
moel@323
   113
      private readonly float?[] controls;
moel@136
   114
moel@268
   115
      private readonly FileStream[] voltageStreams;
moel@268
   116
      private readonly FileStream[] temperatureStreams;
moel@268
   117
      private readonly FileStream[] fanStreams;
moel@136
   118
moel@136
   119
      public Chip Chip { get { return chip; } }
moel@136
   120
      public float?[] Voltages { get { return voltages; } }
moel@136
   121
      public float?[] Temperatures { get { return temperatures; } }
moel@136
   122
      public float?[] Fans { get { return fans; } }
moel@323
   123
      public float?[] Controls { get { return controls; } }
moel@136
   124
moel@136
   125
      public LMChip(Chip chip, string path) {
moel@136
   126
        this.path = path;
moel@136
   127
        this.chip = chip;
moel@136
   128
moel@136
   129
        string[] voltagePaths = Directory.GetFiles(path, "in*_input");
moel@136
   130
        this.voltages = new float?[voltagePaths.Length];
moel@268
   131
        this.voltageStreams = new FileStream[voltagePaths.Length];
moel@136
   132
        for (int i = 0; i < voltagePaths.Length; i++)
moel@268
   133
          voltageStreams[i] = new FileStream(voltagePaths[i],
moel@268
   134
            FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
moel@136
   135
moel@136
   136
        string[] temperaturePaths = Directory.GetFiles(path, "temp*_input");
moel@136
   137
        this.temperatures = new float?[temperaturePaths.Length];
moel@268
   138
        this.temperatureStreams = new FileStream[temperaturePaths.Length];
moel@136
   139
        for (int i = 0; i < temperaturePaths.Length; i++)
moel@268
   140
          temperatureStreams[i] = new FileStream(temperaturePaths[i],
moel@268
   141
            FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
moel@136
   142
moel@136
   143
        string[] fanPaths = Directory.GetFiles(path, "fan*_input");
moel@136
   144
        this.fans = new float?[fanPaths.Length];
moel@268
   145
        this.fanStreams = new FileStream[fanPaths.Length];
moel@136
   146
        for (int i = 0; i < fanPaths.Length; i++)
moel@268
   147
          fanStreams[i] = new FileStream(fanPaths[i],
moel@268
   148
            FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
moel@323
   149
moel@323
   150
        this.controls = new float?[0];
moel@136
   151
      }
moel@136
   152
moel@228
   153
      public byte? ReadGPIO(int index) {
moel@228
   154
        return null;
moel@228
   155
      }
moel@228
   156
moel@228
   157
      public void WriteGPIO(int index, byte value) { }
moel@228
   158
moel@136
   159
      public string GetReport() {
moel@136
   160
        return null;
moel@136
   161
      }
moel@136
   162
moel@323
   163
      public void SetControl(int index, byte? value) { }   
moel@323
   164
moel@268
   165
      private string ReadFirstLine(Stream stream) {
moel@268
   166
        StringBuilder sb = new StringBuilder();
moel@268
   167
        try {
moel@268
   168
          stream.Seek(0, SeekOrigin.Begin);
moel@268
   169
          int b = stream.ReadByte();
moel@268
   170
          while (b != -1 && b != 10) {
moel@268
   171
            sb.Append((char)b);
moel@268
   172
            b = stream.ReadByte();
moel@268
   173
          }
moel@268
   174
        } catch { }
moel@268
   175
        return sb.ToString();
moel@268
   176
      }
moel@268
   177
moel@136
   178
      public void Update() {
moel@136
   179
        for (int i = 0; i < voltages.Length; i++) {
moel@268
   180
          string s = ReadFirstLine(voltageStreams[i]);
moel@136
   181
          try {
moel@268
   182
            voltages[i] = 0.001f *
moel@166
   183
              long.Parse(s, CultureInfo.InvariantCulture);
moel@136
   184
          } catch {
moel@136
   185
            voltages[i] = null;
moel@136
   186
          }
moel@136
   187
        }
moel@136
   188
moel@136
   189
        for (int i = 0; i < temperatures.Length; i++) {
moel@268
   190
          string s = ReadFirstLine(temperatureStreams[i]);
moel@136
   191
          try {
moel@268
   192
            temperatures[i] = 0.001f *
moel@166
   193
              long.Parse(s, CultureInfo.InvariantCulture);
moel@136
   194
          } catch {
moel@136
   195
            temperatures[i] = null;
moel@136
   196
          }
moel@136
   197
        }
moel@136
   198
moel@136
   199
        for (int i = 0; i < fans.Length; i++) {
moel@268
   200
          string s = ReadFirstLine(fanStreams[i]);
moel@136
   201
          try {
moel@166
   202
            fans[i] = long.Parse(s, CultureInfo.InvariantCulture);
moel@136
   203
          } catch {
moel@136
   204
            fans[i] = null;
moel@136
   205
          }
moel@136
   206
        }
moel@136
   207
      }
moel@136
   208
moel@136
   209
      public void Close() {
moel@268
   210
        foreach (FileStream stream in voltageStreams)
moel@268
   211
          stream.Close();
moel@268
   212
        foreach (FileStream stream in temperatureStreams)
moel@268
   213
          stream.Close();
moel@268
   214
        foreach (FileStream stream in fanStreams)
moel@268
   215
          stream.Close();
moel@136
   216
      }
moel@136
   217
    }
moel@136
   218
  }
moel@136
   219
}