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