moel@171: /*
moel@171:   
moel@171:   Version: MPL 1.1/GPL 2.0/LGPL 2.1
moel@171: 
moel@171:   The contents of this file are subject to the Mozilla Public License Version
moel@171:   1.1 (the "License"); you may not use this file except in compliance with
moel@171:   the License. You may obtain a copy of the License at
moel@171:  
moel@171:   http://www.mozilla.org/MPL/
moel@171: 
moel@171:   Software distributed under the License is distributed on an "AS IS" basis,
moel@171:   WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
moel@171:   for the specific language governing rights and limitations under the License.
moel@171: 
moel@171:   The Original Code is the Open Hardware Monitor code.
moel@171: 
moel@171:   The Initial Developer of the Original Code is 
moel@171:   Michael Möller <m.moeller@gmx.ch>.
moel@171:   Portions created by the Initial Developer are Copyright (C) 2010
moel@171:   the Initial Developer. All Rights Reserved.
moel@171: 
moel@171:   Contributor(s):
moel@171: 
moel@171:   Alternatively, the contents of this file may be used under the terms of
moel@171:   either the GNU General Public License Version 2 or later (the "GPL"), or
moel@171:   the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
moel@171:   in which case the provisions of the GPL or the LGPL are applicable instead
moel@171:   of those above. If you wish to allow use of your version of this file only
moel@171:   under the terms of either the GPL or the LGPL, and not to allow others to
moel@171:   use your version of this file under the terms of the MPL, indicate your
moel@171:   decision by deleting the provisions above and replace them with the notice
moel@171:   and other provisions required by the GPL or the LGPL. If you do not delete
moel@171:   the provisions above, a recipient may use your version of this file under
moel@171:   the terms of any one of the MPL, the GPL or the LGPL.
moel@171:  
moel@171: */
moel@171: 
moel@171: using System;
moel@171: using System.Collections.Generic;
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@171:     private List<Heatmaster> hardware = new List<Heatmaster>();
moel@171:     private 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@175:                   result.Add((string)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@172:       int p = (int)System.Environment.OSVersion.Platform;
moel@172:       if ((p == 4) || (p == 128))
moel@172:         return;
moel@171: 
moel@172:       string[] portNames = GetRegistryPortNames();      
moel@171:       for (int i = portNames.Length - 1; i >= 0; i--) {
moel@171:         try {
moel@171: 
moel@171:           SerialPort serialPort =
moel@171:             new SerialPort(portNames[i], 38400, Parity.None, 8, StopBits.One);
moel@171:           serialPort.NewLine = ((char)0x0D).ToString();
moel@171: 
moel@171:           bool isValid = false;
moel@171:           report.Append("Port Name: "); report.AppendLine(portNames[i]);
moel@171: 
moel@171:           try {
moel@171:             serialPort.Open();
moel@171:           } catch (UnauthorizedAccessException) {
moel@171:             report.AppendLine("Exception: Access Denied");
moel@171:           }
moel@171: 
moel@171:           if (serialPort.IsOpen) {
moel@172:             serialPort.DiscardInBuffer();
moel@172:             serialPort.DiscardOutBuffer(); 
moel@172:             serialPort.Write(new byte[] { 0xAA }, 0, 1);
moel@171: 
moel@172:             int j = 0;
moel@172:             while (serialPort.BytesToRead == 0 && j < 10) {
moel@172:               Thread.Sleep(20);
moel@172:               j++;
moel@172:             }
moel@172:             if (serialPort.BytesToRead > 0) {
moel@172:               bool flag = false;
moel@172:               while (serialPort.BytesToRead > 0 && !flag) {
moel@172:                 flag |= (serialPort.ReadByte() == 0xAA);
moel@171:               }
moel@172:               if (flag) {
moel@172:                 serialPort.WriteLine("[0:0]RH");
moel@172:                 try {
moel@172:                   int k = 0;
moel@172:                   int revision = 0;
moel@172:                   while (k < 5) {
moel@172:                     string line = ReadLine(serialPort, 100);
moel@172:                     if (line.StartsWith("-[0:0]RH:")) {
moel@172:                       int.TryParse(line.Substring(9), out revision);
moel@172:                       break;
moel@171:                     }
moel@172:                     k++;
moel@171:                   }
moel@172:                   isValid = (revision == 770); 
moel@172:                   if (!isValid) {
moel@172:                     report.Append("Status: Wrong Hardware Revision " + 
moel@172:                       revision.ToString());
moel@172:                   }                 
moel@172:                 } catch (TimeoutException) {
moel@172:                   report.AppendLine("Status: Timeout Reading Revision");
moel@171:                 }
moel@171:               } else {
moel@172:                 report.AppendLine("Status: Wrong Startflag");
moel@171:               }
moel@171:             } else {
moel@172:               report.AppendLine("Status: No Response");
moel@171:             }
moel@171:             serialPort.DiscardInBuffer();
moel@171:             serialPort.Close();
moel@171:             serialPort.Dispose();
moel@171:           } else {
moel@171:             report.AppendLine("Status: Port not Open");
moel@171:           }                          
moel@171:           if (isValid) {
moel@171:             report.AppendLine("Status: OK");
moel@171:             hardware.Add(new Heatmaster(portNames[i], settings));
moel@171:             return;
moel@171:           }
moel@171:         } catch (Exception e) {
moel@171:           report.AppendLine(e.ToString());
moel@171:         } 
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@171:         report.Insert(0, "Serial Port Heatmaster" + Environment.NewLine +
moel@171:           Environment.NewLine);
moel@171:         report.AppendLine();
moel@171:         return report.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: }