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