Hardware/LPC/LMSensors.cs
author moel.mich
Fri, 22 Jun 2012 10:05:23 +0000
changeset 352 0899f651d5cf
parent 323 3f2d9ebacf38
child 353 b4e37f5b2669
permissions -rwxr-xr-x
Integrated the patch for Fintek F71808E super I/O support developed by Andrey Mushatov.
     1 /*
     2  
     3   This Source Code Form is subject to the terms of the Mozilla Public
     4   License, v. 2.0. If a copy of the MPL was not distributed with this
     5   file, You can obtain one at http://mozilla.org/MPL/2.0/.
     6  
     7   Copyright (C) 2009-2011 Michael Möller <mmoeller@openhardwaremonitor.org>
     8 	
     9 */
    10 
    11 using System.Collections.Generic;
    12 using System.Globalization;
    13 using System.IO;
    14 using System.Text;
    15 
    16 namespace OpenHardwareMonitor.Hardware.LPC {
    17 
    18   internal class LMSensors {
    19 
    20     private readonly List<LMChip> lmChips = new List<LMChip>();
    21 
    22     public LMSensors() {
    23       string[] basePaths = Directory.GetDirectories("/sys/class/hwmon/");
    24       foreach (string basePath in basePaths) {
    25         foreach (string devicePath in new[] { "/device", "" }) {
    26           string path = basePath + devicePath;
    27 
    28           string name = null;
    29           try {
    30             using (StreamReader reader = new StreamReader(path + "/name"))
    31               name = reader.ReadLine();
    32           } catch (IOException) { }
    33 
    34           switch (name) {
    35             case "atk0110":
    36               lmChips.Add(new LMChip(Chip.ATK0110, path)); break;
    37 
    38             case "f71858fg":
    39               lmChips.Add(new LMChip(Chip.F71858, path)); break;
    40             case "f71862fg":
    41               lmChips.Add(new LMChip(Chip.F71862, path)); break;
    42             case "f71882fg":
    43               lmChips.Add(new LMChip(Chip.F71882, path)); break;
    44             case "f71889fg":
    45               lmChips.Add(new LMChip(Chip.F71889F, path)); break;
    46 
    47             case "it8712":
    48               lmChips.Add(new LMChip(Chip.IT8712F, path)); break;
    49             case "it8716":
    50               lmChips.Add(new LMChip(Chip.IT8716F, path)); break;
    51             case "it8718":
    52               lmChips.Add(new LMChip(Chip.IT8718F, path)); break;
    53             case "it8720":
    54               lmChips.Add(new LMChip(Chip.IT8720F, path)); break;
    55 
    56             case "w83627ehf":
    57               lmChips.Add(new LMChip(Chip.W83627EHF, path)); break;
    58             case "w83627dhg":
    59               lmChips.Add(new LMChip(Chip.W83627DHG, path)); break;
    60             case "w83667hg":
    61               lmChips.Add(new LMChip(Chip.W83667HG, path)); break;
    62             case "w83627hf":
    63               lmChips.Add(new LMChip(Chip.W83627HF, path)); break;
    64             case "w83627thf":
    65               lmChips.Add(new LMChip(Chip.W83627THF, path)); break;
    66             case "w83687thf":
    67               lmChips.Add(new LMChip(Chip.W83687THF, path)); break;
    68           }
    69         }
    70       }
    71     }
    72 
    73     public void Close() {
    74       foreach (LMChip lmChip in lmChips)
    75         lmChip.Close();
    76     }
    77 
    78     public ISuperIO[] SuperIO {
    79       get {
    80         return lmChips.ToArray();
    81       }
    82     }
    83 
    84     private class LMChip : ISuperIO {
    85 
    86       private string path;
    87       private readonly Chip chip;
    88 
    89       private readonly float?[] voltages;
    90       private readonly float?[] temperatures;
    91       private readonly float?[] fans;
    92       private readonly float?[] controls;
    93 
    94       private readonly FileStream[] voltageStreams;
    95       private readonly FileStream[] temperatureStreams;
    96       private readonly FileStream[] fanStreams;
    97 
    98       public Chip Chip { get { return chip; } }
    99       public float?[] Voltages { get { return voltages; } }
   100       public float?[] Temperatures { get { return temperatures; } }
   101       public float?[] Fans { get { return fans; } }
   102       public float?[] Controls { get { return controls; } }
   103 
   104       public LMChip(Chip chip, string path) {
   105         this.path = path;
   106         this.chip = chip;
   107 
   108         string[] voltagePaths = Directory.GetFiles(path, "in*_input");
   109         this.voltages = new float?[voltagePaths.Length];
   110         this.voltageStreams = new FileStream[voltagePaths.Length];
   111         for (int i = 0; i < voltagePaths.Length; i++)
   112           voltageStreams[i] = new FileStream(voltagePaths[i],
   113             FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
   114 
   115         string[] temperaturePaths = Directory.GetFiles(path, "temp*_input");
   116         this.temperatures = new float?[temperaturePaths.Length];
   117         this.temperatureStreams = new FileStream[temperaturePaths.Length];
   118         for (int i = 0; i < temperaturePaths.Length; i++)
   119           temperatureStreams[i] = new FileStream(temperaturePaths[i],
   120             FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
   121 
   122         string[] fanPaths = Directory.GetFiles(path, "fan*_input");
   123         this.fans = new float?[fanPaths.Length];
   124         this.fanStreams = new FileStream[fanPaths.Length];
   125         for (int i = 0; i < fanPaths.Length; i++)
   126           fanStreams[i] = new FileStream(fanPaths[i],
   127             FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
   128 
   129         this.controls = new float?[0];
   130       }
   131 
   132       public byte? ReadGPIO(int index) {
   133         return null;
   134       }
   135 
   136       public void WriteGPIO(int index, byte value) { }
   137 
   138       public string GetReport() {
   139         return null;
   140       }
   141 
   142       public void SetControl(int index, byte? value) { }   
   143 
   144       private string ReadFirstLine(Stream stream) {
   145         StringBuilder sb = new StringBuilder();
   146         try {
   147           stream.Seek(0, SeekOrigin.Begin);
   148           int b = stream.ReadByte();
   149           while (b != -1 && b != 10) {
   150             sb.Append((char)b);
   151             b = stream.ReadByte();
   152           }
   153         } catch { }
   154         return sb.ToString();
   155       }
   156 
   157       public void Update() {
   158         for (int i = 0; i < voltages.Length; i++) {
   159           string s = ReadFirstLine(voltageStreams[i]);
   160           try {
   161             voltages[i] = 0.001f *
   162               long.Parse(s, CultureInfo.InvariantCulture);
   163           } catch {
   164             voltages[i] = null;
   165           }
   166         }
   167 
   168         for (int i = 0; i < temperatures.Length; i++) {
   169           string s = ReadFirstLine(temperatureStreams[i]);
   170           try {
   171             temperatures[i] = 0.001f *
   172               long.Parse(s, CultureInfo.InvariantCulture);
   173           } catch {
   174             temperatures[i] = null;
   175           }
   176         }
   177 
   178         for (int i = 0; i < fans.Length; i++) {
   179           string s = ReadFirstLine(fanStreams[i]);
   180           try {
   181             fans[i] = long.Parse(s, CultureInfo.InvariantCulture);
   182           } catch {
   183             fans[i] = null;
   184           }
   185         }
   186       }
   187 
   188       public void Close() {
   189         foreach (FileStream stream in voltageStreams)
   190           stream.Close();
   191         foreach (FileStream stream in temperatureStreams)
   192           stream.Close();
   193         foreach (FileStream stream in fanStreams)
   194           stream.Close();
   195       }
   196     }
   197   }
   198 }