moel@171: /*
moel@171:  
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@171:  
moel@344:   Copyright (C) 2010 Michael Möller <mmoeller@openhardwaremonitor.org>
moel@344: 	
moel@171: */
moel@171: 
moel@171: using System;
moel@171: using System.Collections.Generic;
moel@182: using System.Globalization;
moel@171: using System.IO.Ports;
moel@172: using System.Security;
moel@171: using System.Text;
moel@171: using System.Threading;
moel@172: using Microsoft.Win32;
moel@171: 
moel@171: namespace OpenHardwareMonitor.Hardware.Heatmaster {
moel@171:   internal class HeatmasterGroup : IGroup {
moel@171: 
moel@195:     private readonly List<Heatmaster> hardware = new List<Heatmaster>();
moel@195:     private readonly StringBuilder report = new StringBuilder();
moel@171: 
moel@171:     private static string ReadLine(SerialPort port, int timeout) {
moel@171:       int i = 0;
moel@171:       StringBuilder builder = new StringBuilder();
moel@171:       while (i < timeout) {
moel@171:         while (port.BytesToRead > 0) {
moel@171:           byte b = (byte)port.ReadByte();
moel@171:           switch (b) {
moel@171:             case 0xAA: return ((char)b).ToString();
moel@171:             case 0x0D: return builder.ToString();
moel@171:             default: builder.Append((char)b); break;
moel@171:           }
moel@171:         }
moel@171:         i++;
moel@171:         Thread.Sleep(1);
moel@171:       }
moel@171:       throw new TimeoutException();
moel@172:     }
moel@172: 
moel@172:     private static string[] GetRegistryPortNames() {
moel@172:       List<string> result = new List<string>();
moel@175:       string[] paths = { "", "&MI_00" };
moel@172:       try {
moel@175:         foreach (string path in paths) {
moel@175:           RegistryKey key = Registry.LocalMachine.OpenSubKey(
moel@175:             @"SYSTEM\CurrentControlSet\Enum\USB\VID_10C4&PID_EA60" + path);
moel@175:           if (key != null) {
moel@175:             foreach (string subKeyName in key.GetSubKeyNames()) {
moel@175:               RegistryKey subKey =
moel@175:                 key.OpenSubKey(subKeyName + "\\" + "Device Parameters");
moel@175:               if (subKey != null) {
moel@175:                 string name = subKey.GetValue("PortName") as string;
moel@175:                 if (name != null && !result.Contains(name))
moel@195:                   result.Add(name);
moel@175:               }
moel@172:             }
moel@172:           }
moel@172:         }
moel@172:       } catch (SecurityException) { }
moel@172:       return result.ToArray();
moel@172:     }
moel@171: 
moel@171:     public HeatmasterGroup(ISettings settings) {
moel@172:       
moel@172:       // No implementation for Heatmaster on Unix systems
moel@195:       int p = (int)Environment.OSVersion.Platform;
moel@172:       if ((p == 4) || (p == 128))
moel@172:         return;
moel@171: 
moel@172:       string[] portNames = GetRegistryPortNames();      
moel@199:       for (int i = 0; i < portNames.Length; i++) {
moel@182:         bool isValid = false;
moel@182:         try {        
moel@182:           using (SerialPort serialPort =
moel@182:             new SerialPort(portNames[i], 38400, Parity.None, 8, StopBits.One)) {
moel@182:             serialPort.NewLine = ((char)0x0D).ToString();            
moel@182:             report.Append("Port Name: "); report.AppendLine(portNames[i]);
moel@171: 
moel@182:             try {
moel@182:               serialPort.Open();
moel@182:             } catch (UnauthorizedAccessException) {
moel@182:               report.AppendLine("Exception: Access Denied");
moel@182:             }
moel@171: 
moel@182:             if (serialPort.IsOpen) {
moel@182:               serialPort.DiscardInBuffer();
moel@182:               serialPort.DiscardOutBuffer();
moel@182:               serialPort.Write(new byte[] { 0xAA }, 0, 1);
moel@171: 
moel@182:               int j = 0;
moel@182:               while (serialPort.BytesToRead == 0 && j < 10) {
moel@182:                 Thread.Sleep(20);
moel@182:                 j++;
moel@171:               }
moel@182:               if (serialPort.BytesToRead > 0) {
moel@182:                 bool flag = false;
moel@182:                 while (serialPort.BytesToRead > 0 && !flag) {
moel@182:                   flag |= (serialPort.ReadByte() == 0xAA);
moel@182:                 }
moel@182:                 if (flag) {
moel@182:                   serialPort.WriteLine("[0:0]RH");
moel@182:                   try {
moel@182:                     int k = 0;
moel@182:                     int revision = 0;
moel@182:                     while (k < 5) {
moel@182:                       string line = ReadLine(serialPort, 100);
moel@182:                       if (line.StartsWith("-[0:0]RH:",
moel@182:                         StringComparison.Ordinal)) {
moel@182:                         revision = int.Parse(line.Substring(9), 
moel@182:                           CultureInfo.InvariantCulture);
moel@182:                         break;
moel@182:                       }
moel@182:                       k++;
moel@171:                     }
moel@182:                     isValid = (revision == 770);
moel@182:                     if (!isValid) {
moel@182:                       report.Append("Status: Wrong Hardware Revision " +
moel@182:                         revision.ToString(CultureInfo.InvariantCulture));
moel@182:                     }
moel@182:                   } catch (TimeoutException) {
moel@182:                     report.AppendLine("Status: Timeout Reading Revision");
moel@171:                   }
moel@182:                 } else {
moel@182:                   report.AppendLine("Status: Wrong Startflag");
moel@171:                 }
moel@171:               } else {
moel@182:                 report.AppendLine("Status: No Response");
moel@171:               }
moel@182:               serialPort.DiscardInBuffer();
moel@171:             } else {
moel@182:               report.AppendLine("Status: Port not Open");
moel@182:             }            
moel@171:           }
moel@171:         } catch (Exception e) {
moel@171:           report.AppendLine(e.ToString());
moel@182:         }
moel@182: 
moel@182:         if (isValid) {
moel@182:           report.AppendLine("Status: OK");
moel@182:           hardware.Add(new Heatmaster(portNames[i], settings));
moel@182:         }
moel@171:         report.AppendLine();
moel@171:       }
moel@171:     }
moel@171: 
moel@171:     public IHardware[] Hardware {
moel@171:       get {
moel@171:         return hardware.ToArray();
moel@171:       }
moel@171:     }
moel@171: 
moel@171:     public string GetReport() {
moel@171:       if (report.Length > 0) {
moel@256:         StringBuilder r = new StringBuilder();
moel@256:         r.AppendLine("Serial Port Heatmaster");
moel@256:         r.AppendLine();
moel@256:         r.Append(report);
moel@256:         r.AppendLine();
moel@256:         return r.ToString();
moel@171:       } else
moel@171:         return null;
moel@171:     }
moel@171: 
moel@171:     public void Close() {
moel@171:       foreach (Heatmaster heatmaster in hardware)
moel@171:         heatmaster.Close();
moel@171:     }
moel@171:   }
moel@171: }