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