Hardware/LPC/LMSensors.cs
author moel.mich
Sun, 06 Jun 2010 14:44:53 +0000
changeset 135 51a15cf90d22
child 136 fa2957aa0699
permissions -rwxr-xr-x
Added experimental lm-sensors super I/O support for Linux.
moel@135
     1
/*
moel@135
     2
  
moel@135
     3
  Version: MPL 1.1/GPL 2.0/LGPL 2.1
moel@135
     4
moel@135
     5
  The contents of this file are subject to the Mozilla Public License Version
moel@135
     6
  1.1 (the "License"); you may not use this file except in compliance with
moel@135
     7
  the License. You may obtain a copy of the License at
moel@135
     8
 
moel@135
     9
  http://www.mozilla.org/MPL/
moel@135
    10
moel@135
    11
  Software distributed under the License is distributed on an "AS IS" basis,
moel@135
    12
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
moel@135
    13
  for the specific language governing rights and limitations under the License.
moel@135
    14
moel@135
    15
  The Original Code is the Open Hardware Monitor code.
moel@135
    16
moel@135
    17
  The Initial Developer of the Original Code is 
moel@135
    18
  Michael Möller <m.moeller@gmx.ch>.
moel@135
    19
  Portions created by the Initial Developer are Copyright (C) 2009-2010
moel@135
    20
  the Initial Developer. All Rights Reserved.
moel@135
    21
moel@135
    22
  Contributor(s):
moel@135
    23
moel@135
    24
  Alternatively, the contents of this file may be used under the terms of
moel@135
    25
  either the GNU General Public License Version 2 or later (the "GPL"), or
moel@135
    26
  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
moel@135
    27
  in which case the provisions of the GPL or the LGPL are applicable instead
moel@135
    28
  of those above. If you wish to allow use of your version of this file only
moel@135
    29
  under the terms of either the GPL or the LGPL, and not to allow others to
moel@135
    30
  use your version of this file under the terms of the MPL, indicate your
moel@135
    31
  decision by deleting the provisions above and replace them with the notice
moel@135
    32
  and other provisions required by the GPL or the LGPL. If you do not delete
moel@135
    33
  the provisions above, a recipient may use your version of this file under
moel@135
    34
  the terms of any one of the MPL, the GPL or the LGPL.
moel@135
    35
 
moel@135
    36
*/
moel@135
    37
moel@135
    38
using System;
moel@135
    39
using System.Collections.Generic;
moel@135
    40
using System.IO;
moel@135
    41
moel@135
    42
namespace OpenHardwareMonitor.Hardware.LPC {
moel@135
    43
moel@135
    44
  public class LMSensors {
moel@135
    45
moel@135
    46
    private List<LMChip> lmChips = new List<LMChip>();
moel@135
    47
    
moel@135
    48
    public LMSensors () {
moel@135
    49
      string[] devicePaths = Directory.GetDirectories("/sys/class/hwmon/");
moel@135
    50
      foreach (string path in devicePaths) {
moel@135
    51
        string name = null;
moel@135
    52
        try {
moel@135
    53
          StreamReader reader = new StreamReader(path + "/device/name");
moel@135
    54
          name = reader.ReadLine();
moel@135
    55
          reader.Close();
moel@135
    56
        } catch (IOException) { }
moel@135
    57
        switch (name) {
moel@135
    58
          case "f71858fg": 
moel@135
    59
            lmChips.Add(new LMChip(Chip.F71858, path + "/device")); break; 
moel@135
    60
          case "f71862fg": 
moel@135
    61
            lmChips.Add(new LMChip(Chip.F71862, path + "/device")); break; 
moel@135
    62
          case "f71882fg": 
moel@135
    63
            lmChips.Add(new LMChip(Chip.F71882, path + "/device")); break; 
moel@135
    64
          case "f71889fg": 
moel@135
    65
            lmChips.Add(new LMChip(Chip.F71889F, path + "/device")); break; 
moel@135
    66
        
moel@135
    67
          case "it8712": 
moel@135
    68
            lmChips.Add(new LMChip(Chip.IT8712F, path + "/device")); break; 
moel@135
    69
          case "it8716": 
moel@135
    70
            lmChips.Add(new LMChip(Chip.IT8716F, path + "/device")); break; 
moel@135
    71
          case "it8718": 
moel@135
    72
            lmChips.Add(new LMChip(Chip.IT8718F, path + "/device")); break; 
moel@135
    73
          case "it8720": 
moel@135
    74
            lmChips.Add(new LMChip(Chip.IT8720F, path + "/device")); break; 
moel@135
    75
          
moel@135
    76
          case "w83627ehf": 
moel@135
    77
            lmChips.Add(new LMChip(Chip.W83627EHF, path + "/device")); break;                               
moel@135
    78
          case "w83627dhg": 
moel@135
    79
            lmChips.Add(new LMChip(Chip.W83627DHG, path + "/device")); break;     
moel@135
    80
          case "w83667hg": 
moel@135
    81
            lmChips.Add(new LMChip(Chip.W83667HG, path + "/device")); break;               
moel@135
    82
          case "w83627hf": 
moel@135
    83
            lmChips.Add(new LMChip(Chip.W83627HF, path + "/device")); break;
moel@135
    84
          case "w83627thf": 
moel@135
    85
            lmChips.Add(new LMChip(Chip.W83627THF, path + "/device")); break;   
moel@135
    86
          case "w83687thf": 
moel@135
    87
            lmChips.Add(new LMChip(Chip.W83687THF, path + "/device")); break;             
moel@135
    88
        }
moel@135
    89
      }
moel@135
    90
    }
moel@135
    91
    
moel@135
    92
    public void Close() {
moel@135
    93
      foreach (LMChip lmChip in lmChips)
moel@135
    94
        lmChip.Close();
moel@135
    95
    }
moel@135
    96
    
moel@135
    97
    public ISuperIO[] SuperIO {
moel@135
    98
      get {
moel@135
    99
        return lmChips.ToArray();
moel@135
   100
      }
moel@135
   101
    }
moel@135
   102
    
moel@135
   103
    private class LMChip : ISuperIO {
moel@135
   104
      
moel@135
   105
      private string path;
moel@135
   106
      private Chip chip;
moel@135
   107
      
moel@135
   108
      private float?[] voltages;
moel@135
   109
      private float?[] temperatures ;
moel@135
   110
      private float?[] fans;
moel@135
   111
      
moel@135
   112
      private StreamReader[] voltageReaders;
moel@135
   113
      private StreamReader[] temperatureReaders;
moel@135
   114
      private StreamReader[] fanReaders;
moel@135
   115
      
moel@135
   116
      public Chip Chip { get { return chip; } }
moel@135
   117
      public float?[] Voltages { get { return voltages; } }
moel@135
   118
      public float?[] Temperatures { get { return temperatures; } }
moel@135
   119
      public float?[] Fans { get { return fans; } }
moel@135
   120
      
moel@135
   121
      
moel@135
   122
      public LMChip(Chip chip, string path) {
moel@135
   123
        this.path = path;
moel@135
   124
        this.chip = chip;
moel@135
   125
        
moel@135
   126
        string[] voltagePaths = Directory.GetFiles(path, "in*_input");        
moel@135
   127
        this.voltages = new float?[voltagePaths.Length];
moel@135
   128
        this.voltageReaders = new StreamReader[voltagePaths.Length];
moel@135
   129
        for (int i = 0; i < voltagePaths.Length; i++) 
moel@135
   130
         voltageReaders[i] = new StreamReader(voltagePaths[i]); 
moel@135
   131
        
moel@135
   132
        string[] temperaturePaths = Directory.GetFiles(path, "temp*_input");        
moel@135
   133
        this.temperatures = new float?[temperaturePaths.Length];
moel@135
   134
        this.temperatureReaders = new StreamReader[temperaturePaths.Length];
moel@135
   135
        for (int i = 0; i < temperaturePaths.Length; i++) 
moel@135
   136
         temperatureReaders[i] = new StreamReader(temperaturePaths[i]);    
moel@135
   137
        
moel@135
   138
        string[] fanPaths = Directory.GetFiles(path, "fan*_input");        
moel@135
   139
        this.fans = new float?[fanPaths.Length];
moel@135
   140
        this.fanReaders = new StreamReader[fanPaths.Length];
moel@135
   141
        for (int i = 0; i < fanPaths.Length; i++) 
moel@135
   142
         fanReaders[i] = new StreamReader(fanPaths[i]);    
moel@135
   143
      }
moel@135
   144
      
moel@135
   145
      public string GetReport() {
moel@135
   146
        return null;
moel@135
   147
      }
moel@135
   148
      
moel@135
   149
      public void Update() {
moel@135
   150
        for (int i = 0; i < voltages.Length; i++) {
moel@135
   151
          voltageReaders[i].BaseStream.Seek(0, SeekOrigin.Begin);
moel@135
   152
          string s = voltageReaders[i].ReadLine();  
moel@135
   153
          try {
moel@135
   154
            voltages[i] = 0.001f * long.Parse(s);
moel@135
   155
          } catch {
moel@135
   156
            voltages[i] = null;
moel@135
   157
          }
moel@135
   158
        }
moel@135
   159
        
moel@135
   160
        for (int i = 0; i < temperatures.Length; i++) {
moel@135
   161
          temperatureReaders[i].BaseStream.Seek(0, SeekOrigin.Begin);
moel@135
   162
          string s = temperatureReaders[i].ReadLine();  
moel@135
   163
          try {
moel@135
   164
            temperatures[i] = 0.001f * long.Parse(s);           
moel@135
   165
          } catch {
moel@135
   166
            temperatures[i] = null; 
moel@135
   167
          }
moel@135
   168
        }
moel@135
   169
        
moel@135
   170
        for (int i = 0; i < fans.Length; i++) {
moel@135
   171
          fanReaders[i].BaseStream.Seek(0, SeekOrigin.Begin);
moel@135
   172
          string s = fanReaders[i].ReadLine();  
moel@135
   173
          try {
moel@135
   174
            fans[i] = long.Parse(s);           
moel@135
   175
          } catch {
moel@135
   176
            fans[i] = null; 
moel@135
   177
          }
moel@135
   178
        }
moel@135
   179
      }
moel@135
   180
      
moel@135
   181
      public void Close() {
moel@135
   182
        foreach (StreamReader reader in voltageReaders)
moel@135
   183
          reader.Close();
moel@135
   184
        foreach (StreamReader reader in temperatureReaders)
moel@135
   185
          reader.Close();        
moel@135
   186
        foreach (StreamReader reader in fanReaders)
moel@135
   187
          reader.Close();
moel@135
   188
      }      
moel@135
   189
    }
moel@135
   190
  }
moel@135
   191
}