Hardware/Mainboard/GigabyteTAMG.cs
author moel.mich
Tue, 02 Aug 2011 21:05:17 +0000
changeset 319 35cda448fc5f
child 344 3145aadca3d2
permissions -rw-r--r--
Added initial support for the ITE IT8772E super I/O chip.
     1 /*
     2   
     3   Version: MPL 1.1/GPL 2.0/LGPL 2.1
     4 
     5   The contents of this file are subject to the Mozilla Public License Version
     6   1.1 (the "License"); you may not use this file except in compliance with
     7   the License. You may obtain a copy of the License at
     8  
     9   http://www.mozilla.org/MPL/
    10 
    11   Software distributed under the License is distributed on an "AS IS" basis,
    12   WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
    13   for the specific language governing rights and limitations under the License.
    14 
    15   The Original Code is the Open Hardware Monitor code.
    16 
    17   The Initial Developer of the Original Code is 
    18   Michael Möller <m.moeller@gmx.ch>.
    19   Portions created by the Initial Developer are Copyright (C) 2011
    20   the Initial Developer. All Rights Reserved.
    21 
    22   Contributor(s):
    23 
    24   Alternatively, the contents of this file may be used under the terms of
    25   either the GNU General Public License Version 2 or later (the "GPL"), or
    26   the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
    27   in which case the provisions of the GPL or the LGPL are applicable instead
    28   of those above. If you wish to allow use of your version of this file only
    29   under the terms of either the GPL or the LGPL, and not to allow others to
    30   use your version of this file under the terms of the MPL, indicate your
    31   decision by deleting the provisions above and replace them with the notice
    32   and other provisions required by the GPL or the LGPL. If you do not delete
    33   the provisions above, a recipient may use your version of this file under
    34   the terms of any one of the MPL, the GPL or the LGPL.
    35  
    36 */
    37 
    38 using System;
    39 using System.Collections.Generic;
    40 using System.IO;
    41 using System.IO.Compression;
    42 using System.Runtime.InteropServices;
    43 using System.Text;
    44 
    45 namespace OpenHardwareMonitor.Hardware.Mainboard {
    46   
    47   internal class GigabyteTAMG {
    48     private byte[] table;
    49 
    50     private Sensor[] sensors;
    51 
    52     private struct Sensor {
    53       public string Name;
    54       public SensorType Type;
    55       public int Channel;
    56       public float Value;
    57     }
    58 
    59     private enum SensorType {
    60       Voltage = 1,
    61       Temperature = 2,      
    62       Fan = 4,
    63       Case = 8,
    64     }
    65 
    66     public GigabyteTAMG(byte[] table) {
    67       if (table == null)
    68         throw new ArgumentNullException("table");
    69 
    70       this.table = table;
    71       
    72       int index = IndexOf(table, Encoding.ASCII.GetBytes("$HEALTH$"), 0);
    73 
    74       if (index >= 0) {
    75         index += 8;
    76         using (MemoryStream m =
    77           new MemoryStream(table, index, table.Length - index))
    78         using (BinaryReader r = new BinaryReader(m)) {
    79           try {
    80             r.ReadInt64();
    81             int count = r.ReadInt32();
    82             r.ReadInt64();
    83             r.ReadInt32();
    84             sensors = new Sensor[count];
    85             for (int i = 0; i < sensors.Length; i++) {
    86               sensors[i].Name = new string(r.ReadChars(32)).TrimEnd('\0');
    87               sensors[i].Type = (SensorType)r.ReadByte();
    88               sensors[i].Channel = r.ReadInt16();
    89               sensors[i].Channel |= r.ReadByte() << 24;
    90               r.ReadInt64();
    91               int value = r.ReadInt32();
    92               switch (sensors[i].Type) {
    93                 case SensorType.Voltage:
    94                   sensors[i].Value = 1e-3f * value; break;
    95                 default:
    96                   sensors[i].Value = value; break;
    97               }
    98               r.ReadInt64();
    99             }
   100           } catch (IOException) { sensors = new Sensor[0]; }
   101         }
   102       } else {
   103         sensors = new Sensor[0]; 
   104       }
   105     }
   106 
   107     public static int IndexOf(byte[] array, byte[] pattern, int startIndex) {
   108       if (array == null || pattern == null || pattern.Length > array.Length)
   109         return -1;
   110 
   111       for (int i = startIndex; i < array.Length - pattern.Length; i++) {
   112         bool found = true;
   113         for (int j = 0; j < pattern.Length; j++) {
   114           if (array[i + j] != pattern[j]) {
   115             found = false;
   116             break;
   117           }
   118         }
   119         if (found) 
   120           return i;
   121       }
   122       return -1;
   123     }
   124 
   125     private string GetCompressedAndEncodedTable() {
   126       string base64;
   127       using (MemoryStream m = new MemoryStream()) {
   128         using (GZipStream c = new GZipStream(m, CompressionMode.Compress)) {
   129           c.Write(table, 0, table.Length);          
   130         }
   131         base64 = Convert.ToBase64String(m.ToArray());
   132       }
   133 
   134       StringBuilder r = new StringBuilder();
   135       for (int i = 0; i < Math.Ceiling(base64.Length / 64.0); i++) {
   136         r.Append(" ");
   137         for (int j = 0; j < 0x40; j++) {
   138           int index = (i << 6) | j;
   139           if (index < base64.Length) {
   140             r.Append(base64[index]);
   141           }
   142         }
   143         r.AppendLine();
   144       }
   145 
   146       return r.ToString();
   147     }
   148 
   149     public string GetReport() {
   150       StringBuilder r = new StringBuilder();
   151 
   152       if (sensors.Length > 0) {
   153         r.AppendLine("Gigabyte TAMG Sensors");
   154         r.AppendLine();
   155 
   156         foreach (Sensor sensor in sensors) {
   157           r.AppendFormat(" {0,-10}: {1,8:G6} ({2})", sensor.Name, sensor.Value,
   158             sensor.Type);
   159           r.AppendLine();
   160         }
   161         r.AppendLine();
   162       }
   163 
   164       if (table.Length > 0) {
   165         r.AppendLine("Gigabyte TAMG Table");
   166         r.AppendLine();
   167         r.Append(GetCompressedAndEncodedTable());
   168         r.AppendLine();
   169       }
   170 
   171       return r.ToString();
   172     }
   173   }
   174 }