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@136:   Portions created by the Initial Developer are Copyright (C) 2009-2010
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;
moel@136: using System.Collections.Generic;
moel@136: using System.IO;
moel@136: 
moel@136: namespace OpenHardwareMonitor.Hardware.LPC {
moel@136: 
moel@136:   public class LMSensors {
moel@136: 
moel@136:     private List<LMChip> lmChips = new List<LMChip>();
moel@136: 
moel@136:     public LMSensors() {
moel@136:       string[] devicePaths = Directory.GetDirectories("/sys/class/hwmon/");
moel@136:       foreach (string path in devicePaths) {
moel@136:         string name = null;
moel@136:         try {
moel@136:           StreamReader reader = new StreamReader(path + "/device/name");
moel@136:           name = reader.ReadLine();
moel@136:           reader.Close();
moel@136:         } catch (IOException) { }
moel@136:         switch (name) {
moel@136:           case "f71858fg":
moel@136:             lmChips.Add(new LMChip(Chip.F71858, path + "/device")); break;
moel@136:           case "f71862fg":
moel@136:             lmChips.Add(new LMChip(Chip.F71862, path + "/device")); break;
moel@136:           case "f71882fg":
moel@136:             lmChips.Add(new LMChip(Chip.F71882, path + "/device")); break;
moel@136:           case "f71889fg":
moel@136:             lmChips.Add(new LMChip(Chip.F71889F, path + "/device")); break;
moel@136: 
moel@136:           case "it8712":
moel@136:             lmChips.Add(new LMChip(Chip.IT8712F, path + "/device")); break;
moel@136:           case "it8716":
moel@136:             lmChips.Add(new LMChip(Chip.IT8716F, path + "/device")); break;
moel@136:           case "it8718":
moel@136:             lmChips.Add(new LMChip(Chip.IT8718F, path + "/device")); break;
moel@136:           case "it8720":
moel@136:             lmChips.Add(new LMChip(Chip.IT8720F, path + "/device")); break;
moel@136: 
moel@136:           case "w83627ehf":
moel@136:             lmChips.Add(new LMChip(Chip.W83627EHF, path + "/device")); break;
moel@136:           case "w83627dhg":
moel@136:             lmChips.Add(new LMChip(Chip.W83627DHG, path + "/device")); break;
moel@136:           case "w83667hg":
moel@136:             lmChips.Add(new LMChip(Chip.W83667HG, path + "/device")); break;
moel@136:           case "w83627hf":
moel@136:             lmChips.Add(new LMChip(Chip.W83627HF, path + "/device")); break;
moel@136:           case "w83627thf":
moel@136:             lmChips.Add(new LMChip(Chip.W83627THF, path + "/device")); break;
moel@136:           case "w83687thf":
moel@136:             lmChips.Add(new LMChip(Chip.W83687THF, path + "/device")); break;
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@136:       private Chip chip;
moel@136: 
moel@136:       private float?[] voltages;
moel@136:       private float?[] temperatures;
moel@136:       private float?[] fans;
moel@136: 
moel@136:       private StreamReader[] voltageReaders;
moel@136:       private StreamReader[] temperatureReaders;
moel@136:       private StreamReader[] fanReaders;
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@136:         this.voltageReaders = new StreamReader[voltagePaths.Length];
moel@136:         for (int i = 0; i < voltagePaths.Length; i++)
moel@136:           voltageReaders[i] = new StreamReader(voltagePaths[i]);
moel@136: 
moel@136:         string[] temperaturePaths = Directory.GetFiles(path, "temp*_input");
moel@136:         this.temperatures = new float?[temperaturePaths.Length];
moel@136:         this.temperatureReaders = new StreamReader[temperaturePaths.Length];
moel@136:         for (int i = 0; i < temperaturePaths.Length; i++)
moel@136:           temperatureReaders[i] = new StreamReader(temperaturePaths[i]);
moel@136: 
moel@136:         string[] fanPaths = Directory.GetFiles(path, "fan*_input");
moel@136:         this.fans = new float?[fanPaths.Length];
moel@136:         this.fanReaders = new StreamReader[fanPaths.Length];
moel@136:         for (int i = 0; i < fanPaths.Length; i++)
moel@136:           fanReaders[i] = new StreamReader(fanPaths[i]);
moel@136:       }
moel@136: 
moel@136:       public string GetReport() {
moel@136:         return null;
moel@136:       }
moel@136: 
moel@136:       public void Update() {
moel@136:         for (int i = 0; i < voltages.Length; i++) {
moel@136:           voltageReaders[i].BaseStream.Seek(0, SeekOrigin.Begin);
moel@136:           string s = voltageReaders[i].ReadLine();
moel@136:           try {
moel@136:             voltages[i] = 0.001f * long.Parse(s);
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@136:           temperatureReaders[i].BaseStream.Seek(0, SeekOrigin.Begin);
moel@136:           string s = temperatureReaders[i].ReadLine();
moel@136:           try {
moel@136:             temperatures[i] = 0.001f * long.Parse(s);
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@136:           fanReaders[i].BaseStream.Seek(0, SeekOrigin.Begin);
moel@136:           string s = fanReaders[i].ReadLine();
moel@136:           try {
moel@136:             fans[i] = long.Parse(s);
moel@136:           } catch {
moel@136:             fans[i] = null;
moel@136:           }
moel@136:         }
moel@136:       }
moel@136: 
moel@136:       public void Close() {
moel@136:         foreach (StreamReader reader in voltageReaders)
moel@136:           reader.Close();
moel@136:         foreach (StreamReader reader in temperatureReaders)
moel@136:           reader.Close();
moel@136:         foreach (StreamReader reader in fanReaders)
moel@136:           reader.Close();
moel@136:       }
moel@136:     }
moel@136:   }
moel@136: }