Hardware/TBalancer/TBalancerGroup.cs
author moel.mich
Thu, 07 Jul 2011 20:41:09 +0000
changeset 308 d882720734bf
parent 216 b5b076457b68
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) 2009-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.Text;
    42 using System.Threading;
    43 
    44 namespace OpenHardwareMonitor.Hardware.TBalancer {
    45   internal class TBalancerGroup : IGroup {
    46 
    47     private readonly List<TBalancer> hardware = new List<TBalancer>();
    48     private readonly StringBuilder report = new StringBuilder();
    49 
    50     public TBalancerGroup(ISettings settings) {
    51 
    52       uint numDevices;
    53       try {
    54         if (FTD2XX.FT_CreateDeviceInfoList(out numDevices) != FT_STATUS.FT_OK) {
    55           report.AppendLine("Status: FT_CreateDeviceInfoList failed");
    56           return;
    57         }
    58       } catch (DllNotFoundException) { return; } 
    59         catch (ArgumentNullException) { return; }
    60         catch (EntryPointNotFoundException) { return; }
    61         catch (BadImageFormatException) { return; }
    62      
    63       FT_DEVICE_INFO_NODE[] info = new FT_DEVICE_INFO_NODE[numDevices];
    64       if (FTD2XX.FT_GetDeviceInfoList(info, ref numDevices) != FT_STATUS.FT_OK) 
    65       {
    66         report.AppendLine("Status: FT_GetDeviceInfoList failed");        
    67         return;
    68       }
    69 
    70       // make sure numDevices is not larger than the info array
    71       if (numDevices > info.Length)
    72         numDevices = (uint)info.Length;
    73 
    74       for (int i = 0; i < numDevices; i++) {
    75         report.Append("Device Index: ");
    76         report.AppendLine(i.ToString(CultureInfo.InvariantCulture));
    77         report.Append("Device Type: ");
    78         report.AppendLine(info[i].Type.ToString());
    79 
    80         // the T-Balancer always uses an FT232BM
    81         if (info[i].Type != FT_DEVICE.FT_DEVICE_232BM) {
    82           report.AppendLine("Status: Wrong device type");
    83           continue;
    84         }
    85 
    86         FT_HANDLE handle;
    87         FT_STATUS status = FTD2XX.FT_Open(i, out handle);
    88         if (status != FT_STATUS.FT_OK) {
    89           report.AppendLine("Open Status: " + status);
    90           continue;
    91         }
    92 
    93         FTD2XX.FT_SetBaudRate(handle, 19200);
    94         FTD2XX.FT_SetDataCharacteristics(handle, 8, 1, 0);
    95         FTD2XX.FT_SetFlowControl(handle, FT_FLOW_CONTROL.FT_FLOW_RTS_CTS, 0x11, 
    96           0x13);
    97         FTD2XX.FT_SetTimeouts(handle, 1000, 1000);
    98         FTD2XX.FT_Purge(handle, FT_PURGE.FT_PURGE_ALL);
    99         
   100         status = FTD2XX.Write(handle, new byte[] { 0x38 });
   101         if (status != FT_STATUS.FT_OK) {
   102           report.AppendLine("Write Status: " + status);
   103           FTD2XX.FT_Close(handle);
   104           continue;
   105         }
   106 
   107         bool isValid = false;
   108         byte protocolVersion = 0;
   109 
   110         int j = 0;
   111         while (FTD2XX.BytesToRead(handle) == 0 && j < 2) {
   112           Thread.Sleep(100);
   113           j++;
   114         }
   115         if (FTD2XX.BytesToRead(handle) > 0) {
   116           if (FTD2XX.ReadByte(handle) == TBalancer.STARTFLAG) {
   117             while (FTD2XX.BytesToRead(handle) < 284 && j < 5) {
   118               Thread.Sleep(100);
   119               j++;
   120             }
   121             int length = FTD2XX.BytesToRead(handle);
   122             if (length >= 284) {
   123               byte[] data = new byte[285];
   124               data[0] = TBalancer.STARTFLAG;
   125               for (int k = 1; k < data.Length; k++)
   126                 data[k] = FTD2XX.ReadByte(handle);
   127 
   128               // check protocol version 2X (protocols seen: 2C, 2A, 28)
   129               isValid = (data[274] & 0xF0) == 0x20;
   130               protocolVersion = data[274];
   131               if (!isValid) {
   132                 report.Append("Status: Wrong Protocol Version: 0x");
   133                 report.AppendLine(
   134                   protocolVersion.ToString("X", CultureInfo.InvariantCulture));
   135               }
   136             } else {
   137               report.AppendLine("Status: Wrong Message Length: " + length);
   138             }
   139           } else {
   140             report.AppendLine("Status: Wrong Startflag");
   141           }
   142         } else {
   143           report.AppendLine("Status: No Response");
   144         }
   145 
   146         FTD2XX.FT_Purge(handle, FT_PURGE.FT_PURGE_ALL);
   147         FTD2XX.FT_Close(handle);
   148 
   149         if (isValid) {
   150           report.AppendLine("Status: OK");
   151           hardware.Add(new TBalancer(i, protocolVersion, settings));
   152           return;
   153         }
   154         report.AppendLine();
   155       }
   156     }
   157 
   158     public IHardware[] Hardware {
   159       get {
   160         return hardware.ToArray();
   161       }
   162     }
   163 
   164     public string GetReport() {
   165       if (report.Length > 0) {
   166         StringBuilder r = new StringBuilder();
   167         r.AppendLine("FTD2XX");
   168         r.AppendLine();
   169         r.Append(report);
   170         r.AppendLine();
   171         return r.ToString();
   172       } else
   173         return null;
   174     }
   175 
   176     public void Close() {
   177       foreach (TBalancer tbalancer in hardware)
   178         tbalancer.Close();
   179     }
   180   }
   181 }