Hardware/TBalancer/TBalancerGroup.cs
author moel.mich
Mon, 28 May 2012 10:39:30 +0000
changeset 350 6de77245e32b
parent 256 6dc6410489f4
child 351 d389607e74ca
permissions -rw-r--r--
Added support for Intel Ivy Bridge based CPUs. Added code to prevent displaying wrong information on unknown (future) Intel CPUs.
     1 /*
     2  
     3   This Source Code Form is subject to the terms of the Mozilla Public
     4   License, v. 2.0. If a copy of the MPL was not distributed with this
     5   file, You can obtain one at http://mozilla.org/MPL/2.0/.
     6  
     7   Copyright (C) 2009-2010 Michael Möller <mmoeller@openhardwaremonitor.org>
     8 	
     9 */
    10 
    11 using System;
    12 using System.Collections.Generic;
    13 using System.Globalization;
    14 using System.Text;
    15 using System.Threading;
    16 
    17 namespace OpenHardwareMonitor.Hardware.TBalancer {
    18   internal class TBalancerGroup : IGroup {
    19 
    20     private readonly List<TBalancer> hardware = new List<TBalancer>();
    21     private readonly StringBuilder report = new StringBuilder();
    22 
    23     public TBalancerGroup(ISettings settings) {
    24 
    25       uint numDevices;
    26       try {
    27         if (FTD2XX.FT_CreateDeviceInfoList(out numDevices) != FT_STATUS.FT_OK) {
    28           report.AppendLine("Status: FT_CreateDeviceInfoList failed");
    29           return;
    30         }
    31       } catch (DllNotFoundException) { return; } 
    32         catch (ArgumentNullException) { return; }
    33         catch (EntryPointNotFoundException) { return; }
    34         catch (BadImageFormatException) { return; }
    35      
    36       FT_DEVICE_INFO_NODE[] info = new FT_DEVICE_INFO_NODE[numDevices];
    37       if (FTD2XX.FT_GetDeviceInfoList(info, ref numDevices) != FT_STATUS.FT_OK) 
    38       {
    39         report.AppendLine("Status: FT_GetDeviceInfoList failed");        
    40         return;
    41       }
    42 
    43       // make sure numDevices is not larger than the info array
    44       if (numDevices > info.Length)
    45         numDevices = (uint)info.Length;
    46 
    47       for (int i = 0; i < numDevices; i++) {
    48         report.Append("Device Index: ");
    49         report.AppendLine(i.ToString(CultureInfo.InvariantCulture));
    50         report.Append("Device Type: ");
    51         report.AppendLine(info[i].Type.ToString());
    52 
    53         // the T-Balancer always uses an FT232BM
    54         if (info[i].Type != FT_DEVICE.FT_DEVICE_232BM) {
    55           report.AppendLine("Status: Wrong device type");
    56           continue;
    57         }
    58 
    59         FT_HANDLE handle;
    60         FT_STATUS status = FTD2XX.FT_Open(i, out handle);
    61         if (status != FT_STATUS.FT_OK) {
    62           report.AppendLine("Open Status: " + status);
    63           continue;
    64         }
    65 
    66         FTD2XX.FT_SetBaudRate(handle, 19200);
    67         FTD2XX.FT_SetDataCharacteristics(handle, 8, 1, 0);
    68         FTD2XX.FT_SetFlowControl(handle, FT_FLOW_CONTROL.FT_FLOW_RTS_CTS, 0x11, 
    69           0x13);
    70         FTD2XX.FT_SetTimeouts(handle, 1000, 1000);
    71         FTD2XX.FT_Purge(handle, FT_PURGE.FT_PURGE_ALL);
    72         
    73         status = FTD2XX.Write(handle, new byte[] { 0x38 });
    74         if (status != FT_STATUS.FT_OK) {
    75           report.AppendLine("Write Status: " + status);
    76           FTD2XX.FT_Close(handle);
    77           continue;
    78         }
    79 
    80         bool isValid = false;
    81         byte protocolVersion = 0;
    82 
    83         int j = 0;
    84         while (FTD2XX.BytesToRead(handle) == 0 && j < 2) {
    85           Thread.Sleep(100);
    86           j++;
    87         }
    88         if (FTD2XX.BytesToRead(handle) > 0) {
    89           if (FTD2XX.ReadByte(handle) == TBalancer.STARTFLAG) {
    90             while (FTD2XX.BytesToRead(handle) < 284 && j < 5) {
    91               Thread.Sleep(100);
    92               j++;
    93             }
    94             int length = FTD2XX.BytesToRead(handle);
    95             if (length >= 284) {
    96               byte[] data = new byte[285];
    97               data[0] = TBalancer.STARTFLAG;
    98               for (int k = 1; k < data.Length; k++)
    99                 data[k] = FTD2XX.ReadByte(handle);
   100 
   101               // check protocol version 2X (protocols seen: 2C, 2A, 28)
   102               isValid = (data[274] & 0xF0) == 0x20;
   103               protocolVersion = data[274];
   104               if (!isValid) {
   105                 report.Append("Status: Wrong Protocol Version: 0x");
   106                 report.AppendLine(
   107                   protocolVersion.ToString("X", CultureInfo.InvariantCulture));
   108               }
   109             } else {
   110               report.AppendLine("Status: Wrong Message Length: " + length);
   111             }
   112           } else {
   113             report.AppendLine("Status: Wrong Startflag");
   114           }
   115         } else {
   116           report.AppendLine("Status: No Response");
   117         }
   118 
   119         FTD2XX.FT_Purge(handle, FT_PURGE.FT_PURGE_ALL);
   120         FTD2XX.FT_Close(handle);
   121 
   122         if (isValid) {
   123           report.AppendLine("Status: OK");
   124           hardware.Add(new TBalancer(i, protocolVersion, settings));
   125           return;
   126         }
   127         report.AppendLine();
   128       }
   129     }
   130 
   131     public IHardware[] Hardware {
   132       get {
   133         return hardware.ToArray();
   134       }
   135     }
   136 
   137     public string GetReport() {
   138       if (report.Length > 0) {
   139         StringBuilder r = new StringBuilder();
   140         r.AppendLine("FTD2XX");
   141         r.AppendLine();
   142         r.Append(report);
   143         r.AppendLine();
   144         return r.ToString();
   145       } else
   146         return null;
   147     }
   148 
   149     public void Close() {
   150       foreach (TBalancer tbalancer in hardware)
   151         tbalancer.Close();
   152     }
   153   }
   154 }