moel@136: /*
moel@136:  
moel@344:   This Source Code Form is subject to the terms of the Mozilla Public
moel@344:   License, v. 2.0. If a copy of the MPL was not distributed with this
moel@344:   file, You can obtain one at http://mozilla.org/MPL/2.0/.
moel@136:  
moel@353:   Copyright (C) 2009-2012 Michael Möller <mmoeller@openhardwaremonitor.org>
moel@344: 	
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@353:             case "it8705":
moel@353:               lmChips.Add(new LMChip(Chip.IT8705F, path)); break;
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@323:       private readonly float?[] controls;
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@323:       public float?[] Controls { get { return controls; } }
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@323: 
moel@323:         this.controls = new float?[0];
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@323:       public void SetControl(int index, byte? value) { }   
moel@323: 
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: }