Hardware/Heatmaster/HeatmasterGroup.cs
author moel.mich
Thu, 07 Jul 2011 20:41:09 +0000
changeset 308 d882720734bf
parent 199 7026bb2e1f7d
child 344 3145aadca3d2
permissions -rw-r--r--
Added support for reading the TAMG ACPI table on Gigabyte mainboards. Fixed a small problem with HDD/SSD names (0 chars are trimmed now as well).
     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) 2010
    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.Globalization;
    41 using System.IO.Ports;
    42 using System.Security;
    43 using System.Text;
    44 using System.Threading;
    45 using Microsoft.Win32;
    46 
    47 namespace OpenHardwareMonitor.Hardware.Heatmaster {
    48   internal class HeatmasterGroup : IGroup {
    49 
    50     private readonly List<Heatmaster> hardware = new List<Heatmaster>();
    51     private readonly StringBuilder report = new StringBuilder();
    52 
    53     private static string ReadLine(SerialPort port, int timeout) {
    54       int i = 0;
    55       StringBuilder builder = new StringBuilder();
    56       while (i < timeout) {
    57         while (port.BytesToRead > 0) {
    58           byte b = (byte)port.ReadByte();
    59           switch (b) {
    60             case 0xAA: return ((char)b).ToString();
    61             case 0x0D: return builder.ToString();
    62             default: builder.Append((char)b); break;
    63           }
    64         }
    65         i++;
    66         Thread.Sleep(1);
    67       }
    68       throw new TimeoutException();
    69     }
    70 
    71     private static string[] GetRegistryPortNames() {
    72       List<string> result = new List<string>();
    73       string[] paths = { "", "&MI_00" };
    74       try {
    75         foreach (string path in paths) {
    76           RegistryKey key = Registry.LocalMachine.OpenSubKey(
    77             @"SYSTEM\CurrentControlSet\Enum\USB\VID_10C4&PID_EA60" + path);
    78           if (key != null) {
    79             foreach (string subKeyName in key.GetSubKeyNames()) {
    80               RegistryKey subKey =
    81                 key.OpenSubKey(subKeyName + "\\" + "Device Parameters");
    82               if (subKey != null) {
    83                 string name = subKey.GetValue("PortName") as string;
    84                 if (name != null && !result.Contains(name))
    85                   result.Add(name);
    86               }
    87             }
    88           }
    89         }
    90       } catch (SecurityException) { }
    91       return result.ToArray();
    92     }
    93 
    94     public HeatmasterGroup(ISettings settings) {
    95       
    96       // No implementation for Heatmaster on Unix systems
    97       int p = (int)Environment.OSVersion.Platform;
    98       if ((p == 4) || (p == 128))
    99         return;
   100 
   101       string[] portNames = GetRegistryPortNames();      
   102       for (int i = 0; i < portNames.Length; i++) {
   103         bool isValid = false;
   104         try {        
   105           using (SerialPort serialPort =
   106             new SerialPort(portNames[i], 38400, Parity.None, 8, StopBits.One)) {
   107             serialPort.NewLine = ((char)0x0D).ToString();            
   108             report.Append("Port Name: "); report.AppendLine(portNames[i]);
   109 
   110             try {
   111               serialPort.Open();
   112             } catch (UnauthorizedAccessException) {
   113               report.AppendLine("Exception: Access Denied");
   114             }
   115 
   116             if (serialPort.IsOpen) {
   117               serialPort.DiscardInBuffer();
   118               serialPort.DiscardOutBuffer();
   119               serialPort.Write(new byte[] { 0xAA }, 0, 1);
   120 
   121               int j = 0;
   122               while (serialPort.BytesToRead == 0 && j < 10) {
   123                 Thread.Sleep(20);
   124                 j++;
   125               }
   126               if (serialPort.BytesToRead > 0) {
   127                 bool flag = false;
   128                 while (serialPort.BytesToRead > 0 && !flag) {
   129                   flag |= (serialPort.ReadByte() == 0xAA);
   130                 }
   131                 if (flag) {
   132                   serialPort.WriteLine("[0:0]RH");
   133                   try {
   134                     int k = 0;
   135                     int revision = 0;
   136                     while (k < 5) {
   137                       string line = ReadLine(serialPort, 100);
   138                       if (line.StartsWith("-[0:0]RH:",
   139                         StringComparison.Ordinal)) {
   140                         revision = int.Parse(line.Substring(9), 
   141                           CultureInfo.InvariantCulture);
   142                         break;
   143                       }
   144                       k++;
   145                     }
   146                     isValid = (revision == 770);
   147                     if (!isValid) {
   148                       report.Append("Status: Wrong Hardware Revision " +
   149                         revision.ToString(CultureInfo.InvariantCulture));
   150                     }
   151                   } catch (TimeoutException) {
   152                     report.AppendLine("Status: Timeout Reading Revision");
   153                   }
   154                 } else {
   155                   report.AppendLine("Status: Wrong Startflag");
   156                 }
   157               } else {
   158                 report.AppendLine("Status: No Response");
   159               }
   160               serialPort.DiscardInBuffer();
   161             } else {
   162               report.AppendLine("Status: Port not Open");
   163             }            
   164           }
   165         } catch (Exception e) {
   166           report.AppendLine(e.ToString());
   167         }
   168 
   169         if (isValid) {
   170           report.AppendLine("Status: OK");
   171           hardware.Add(new Heatmaster(portNames[i], settings));
   172         }
   173         report.AppendLine();
   174       }
   175     }
   176 
   177     public IHardware[] Hardware {
   178       get {
   179         return hardware.ToArray();
   180       }
   181     }
   182 
   183     public string GetReport() {
   184       if (report.Length > 0) {
   185         StringBuilder r = new StringBuilder();
   186         r.AppendLine("Serial Port Heatmaster");
   187         r.AppendLine();
   188         r.Append(report);
   189         r.AppendLine();
   190         return r.ToString();
   191       } else
   192         return null;
   193     }
   194 
   195     public void Close() {
   196       foreach (Heatmaster heatmaster in hardware)
   197         heatmaster.Close();
   198     }
   199   }
   200 }