moel@136: /*
moel@136:   
moel@136:   Version: MPL 1.1/GPL 2.0/LGPL 2.1
moel@136: 
moel@136:   The contents of this file are subject to the Mozilla Public License Version
moel@136:   1.1 (the "License"); you may not use this file except in compliance with
moel@136:   the License. You may obtain a copy of the License at
moel@136:  
moel@136:   http://www.mozilla.org/MPL/
moel@136: 
moel@136:   Software distributed under the License is distributed on an "AS IS" basis,
moel@136:   WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
moel@136:   for the specific language governing rights and limitations under the License.
moel@136: 
moel@136:   The Original Code is the Open Hardware Monitor code.
moel@136: 
moel@136:   The Initial Developer of the Original Code is 
moel@136:   Michael Möller <m.moeller@gmx.ch>.
moel@266:   Portions created by the Initial Developer are Copyright (C) 2009-2011
moel@136:   the Initial Developer. All Rights Reserved.
moel@136: 
moel@136:   Contributor(s):
moel@136: 
moel@136:   Alternatively, the contents of this file may be used under the terms of
moel@136:   either the GNU General Public License Version 2 or later (the "GPL"), or
moel@136:   the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
moel@136:   in which case the provisions of the GPL or the LGPL are applicable instead
moel@136:   of those above. If you wish to allow use of your version of this file only
moel@136:   under the terms of either the GPL or the LGPL, and not to allow others to
moel@136:   use your version of this file under the terms of the MPL, indicate your
moel@136:   decision by deleting the provisions above and replace them with the notice
moel@136:   and other provisions required by the GPL or the LGPL. If you do not delete
moel@136:   the provisions above, a recipient may use your version of this file under
moel@136:   the terms of any one of the MPL, the GPL or the LGPL.
moel@136:  
moel@136: */
moel@136: 
moel@136: using System.Collections.Generic;
moel@166: using System.Globalization;
moel@136: using System.IO;
moel@268: using System.Text;
moel@136: 
moel@136: namespace OpenHardwareMonitor.Hardware.LPC {
moel@136: 
moel@165:   internal class LMSensors {
moel@136: 
moel@195:     private readonly List<LMChip> lmChips = new List<LMChip>();
moel@136: 
moel@136:     public LMSensors() {
moel@266:       string[] basePaths = Directory.GetDirectories("/sys/class/hwmon/");
moel@266:       foreach (string basePath in basePaths) {
moel@266:         foreach (string devicePath in new[] { "/device", "" }) {
moel@266:           string path = basePath + devicePath;
moel@136: 
moel@266:           string name = null;
moel@266:           try {
moel@266:             using (StreamReader reader = new StreamReader(path + "/name"))
moel@266:               name = reader.ReadLine();
moel@266:           } catch (IOException) { }
moel@136: 
moel@266:           switch (name) {
moel@266:             case "atk0110":
moel@266:               lmChips.Add(new LMChip(Chip.ATK0110, path)); break;
moel@266: 
moel@266:             case "f71858fg":
moel@266:               lmChips.Add(new LMChip(Chip.F71858, path)); break;
moel@266:             case "f71862fg":
moel@266:               lmChips.Add(new LMChip(Chip.F71862, path)); break;
moel@266:             case "f71882fg":
moel@266:               lmChips.Add(new LMChip(Chip.F71882, path)); break;
moel@266:             case "f71889fg":
moel@266:               lmChips.Add(new LMChip(Chip.F71889F, path)); break;
moel@266: 
moel@266:             case "it8712":
moel@266:               lmChips.Add(new LMChip(Chip.IT8712F, path)); break;
moel@266:             case "it8716":
moel@266:               lmChips.Add(new LMChip(Chip.IT8716F, path)); break;
moel@266:             case "it8718":
moel@266:               lmChips.Add(new LMChip(Chip.IT8718F, path)); break;
moel@266:             case "it8720":
moel@266:               lmChips.Add(new LMChip(Chip.IT8720F, path)); break;
moel@266: 
moel@266:             case "w83627ehf":
moel@266:               lmChips.Add(new LMChip(Chip.W83627EHF, path)); break;
moel@266:             case "w83627dhg":
moel@266:               lmChips.Add(new LMChip(Chip.W83627DHG, path)); break;
moel@266:             case "w83667hg":
moel@266:               lmChips.Add(new LMChip(Chip.W83667HG, path)); break;
moel@266:             case "w83627hf":
moel@266:               lmChips.Add(new LMChip(Chip.W83627HF, path)); break;
moel@266:             case "w83627thf":
moel@266:               lmChips.Add(new LMChip(Chip.W83627THF, path)); break;
moel@266:             case "w83687thf":
moel@266:               lmChips.Add(new LMChip(Chip.W83687THF, path)); break;
moel@266:           }
moel@136:         }
moel@136:       }
moel@136:     }
moel@136: 
moel@136:     public void Close() {
moel@136:       foreach (LMChip lmChip in lmChips)
moel@136:         lmChip.Close();
moel@136:     }
moel@136: 
moel@136:     public ISuperIO[] SuperIO {
moel@136:       get {
moel@136:         return lmChips.ToArray();
moel@136:       }
moel@136:     }
moel@136: 
moel@136:     private class LMChip : ISuperIO {
moel@136: 
moel@136:       private string path;
moel@195:       private readonly Chip chip;
moel@136: 
moel@195:       private readonly float?[] voltages;
moel@195:       private readonly float?[] temperatures;
moel@195:       private readonly float?[] fans;
moel@136: 
moel@268:       private readonly FileStream[] voltageStreams;
moel@268:       private readonly FileStream[] temperatureStreams;
moel@268:       private readonly FileStream[] fanStreams;
moel@136: 
moel@136:       public Chip Chip { get { return chip; } }
moel@136:       public float?[] Voltages { get { return voltages; } }
moel@136:       public float?[] Temperatures { get { return temperatures; } }
moel@136:       public float?[] Fans { get { return fans; } }
moel@136: 
moel@136: 
moel@136:       public LMChip(Chip chip, string path) {
moel@136:         this.path = path;
moel@136:         this.chip = chip;
moel@136: 
moel@136:         string[] voltagePaths = Directory.GetFiles(path, "in*_input");
moel@136:         this.voltages = new float?[voltagePaths.Length];
moel@268:         this.voltageStreams = new FileStream[voltagePaths.Length];
moel@136:         for (int i = 0; i < voltagePaths.Length; i++)
moel@268:           voltageStreams[i] = new FileStream(voltagePaths[i],
moel@268:             FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
moel@136: 
moel@136:         string[] temperaturePaths = Directory.GetFiles(path, "temp*_input");
moel@136:         this.temperatures = new float?[temperaturePaths.Length];
moel@268:         this.temperatureStreams = new FileStream[temperaturePaths.Length];
moel@136:         for (int i = 0; i < temperaturePaths.Length; i++)
moel@268:           temperatureStreams[i] = new FileStream(temperaturePaths[i],
moel@268:             FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
moel@136: 
moel@136:         string[] fanPaths = Directory.GetFiles(path, "fan*_input");
moel@136:         this.fans = new float?[fanPaths.Length];
moel@268:         this.fanStreams = new FileStream[fanPaths.Length];
moel@136:         for (int i = 0; i < fanPaths.Length; i++)
moel@268:           fanStreams[i] = new FileStream(fanPaths[i],
moel@268:             FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
moel@136:       }
moel@136: 
moel@228:       public byte? ReadGPIO(int index) {
moel@228:         return null;
moel@228:       }
moel@228: 
moel@228:       public void WriteGPIO(int index, byte value) { }
moel@228: 
moel@136:       public string GetReport() {
moel@136:         return null;
moel@136:       }
moel@136: 
moel@268:       private string ReadFirstLine(Stream stream) {
moel@268:         StringBuilder sb = new StringBuilder();
moel@268:         try {
moel@268:           stream.Seek(0, SeekOrigin.Begin);
moel@268:           int b = stream.ReadByte();
moel@268:           while (b != -1 && b != 10) {
moel@268:             sb.Append((char)b);
moel@268:             b = stream.ReadByte();
moel@268:           }
moel@268:         } catch { }
moel@268:         return sb.ToString();
moel@268:       }
moel@268: 
moel@136:       public void Update() {
moel@136:         for (int i = 0; i < voltages.Length; i++) {
moel@268:           string s = ReadFirstLine(voltageStreams[i]);
moel@136:           try {
moel@268:             voltages[i] = 0.001f *
moel@166:               long.Parse(s, CultureInfo.InvariantCulture);
moel@136:           } catch {
moel@136:             voltages[i] = null;
moel@136:           }
moel@136:         }
moel@136: 
moel@136:         for (int i = 0; i < temperatures.Length; i++) {
moel@268:           string s = ReadFirstLine(temperatureStreams[i]);
moel@136:           try {
moel@268:             temperatures[i] = 0.001f *
moel@166:               long.Parse(s, CultureInfo.InvariantCulture);
moel@136:           } catch {
moel@136:             temperatures[i] = null;
moel@136:           }
moel@136:         }
moel@136: 
moel@136:         for (int i = 0; i < fans.Length; i++) {
moel@268:           string s = ReadFirstLine(fanStreams[i]);
moel@136:           try {
moel@166:             fans[i] = long.Parse(s, CultureInfo.InvariantCulture);
moel@136:           } catch {
moel@136:             fans[i] = null;
moel@136:           }
moel@136:         }
moel@136:       }
moel@136: 
moel@136:       public void Close() {
moel@268:         foreach (FileStream stream in voltageStreams)
moel@268:           stream.Close();
moel@268:         foreach (FileStream stream in temperatureStreams)
moel@268:           stream.Close();
moel@268:         foreach (FileStream stream in fanStreams)
moel@268:           stream.Close();
moel@136:       }
moel@136:     }
moel@136:   }
moel@136: }