moel@1: /*
moel@1:  
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@1:  
moel@351:   Copyright (C) 2009-2012 Michael Möller <mmoeller@openhardwaremonitor.org>
moel@344: 	
moel@1: */
moel@1: 
moel@1: using System;
moel@1: using System.Collections.Generic;
moel@166: using System.Globalization;
moel@32: using System.Text;
moel@1: using System.Threading;
moel@1: 
moel@1: namespace OpenHardwareMonitor.Hardware.TBalancer {
moel@165:   internal class TBalancerGroup : IGroup {
moel@1: 
moel@195:     private readonly List<TBalancer> hardware = new List<TBalancer>();
moel@195:     private readonly StringBuilder report = new StringBuilder();
moel@1: 
moel@165:     public TBalancerGroup(ISettings settings) {
moel@32: 
moel@87:       uint numDevices;
moel@87:       try {
moel@216:         if (FTD2XX.FT_CreateDeviceInfoList(out numDevices) != FT_STATUS.FT_OK) {
moel@216:           report.AppendLine("Status: FT_CreateDeviceInfoList failed");
moel@216:           return;
moel@216:         }
moel@87:       } catch (DllNotFoundException) { return; } 
moel@87:         catch (ArgumentNullException) { return; }
moel@131:         catch (EntryPointNotFoundException) { return; }
moel@160:         catch (BadImageFormatException) { return; }
moel@351:    
moel@87:       FT_DEVICE_INFO_NODE[] info = new FT_DEVICE_INFO_NODE[numDevices];
moel@216:       if (FTD2XX.FT_GetDeviceInfoList(info, ref numDevices) != FT_STATUS.FT_OK) 
moel@216:       {
moel@216:         report.AppendLine("Status: FT_GetDeviceInfoList failed");        
moel@216:         return;
moel@216:       }
moel@351:  
moel@216:       // make sure numDevices is not larger than the info array
moel@216:       if (numDevices > info.Length)
moel@216:         numDevices = (uint)info.Length;
moel@32: 
moel@87:       for (int i = 0; i < numDevices; i++) {
moel@166:         report.Append("Device Index: ");
moel@166:         report.AppendLine(i.ToString(CultureInfo.InvariantCulture));
moel@186:         report.Append("Device Type: ");
moel@186:         report.AppendLine(info[i].Type.ToString());
moel@186: 
moel@186:         // the T-Balancer always uses an FT232BM
moel@186:         if (info[i].Type != FT_DEVICE.FT_DEVICE_232BM) {
moel@186:           report.AppendLine("Status: Wrong device type");
moel@186:           continue;
moel@186:         }
moel@186: 
moel@87:         FT_HANDLE handle;
moel@195:         FT_STATUS status = FTD2XX.FT_Open(i, out handle);
moel@87:         if (status != FT_STATUS.FT_OK) {
moel@87:           report.AppendLine("Open Status: " + status);
moel@87:           continue;
moel@87:         }
moel@32: 
moel@87:         FTD2XX.FT_SetBaudRate(handle, 19200);
moel@87:         FTD2XX.FT_SetDataCharacteristics(handle, 8, 1, 0);
moel@87:         FTD2XX.FT_SetFlowControl(handle, FT_FLOW_CONTROL.FT_FLOW_RTS_CTS, 0x11, 
moel@87:           0x13);
moel@87:         FTD2XX.FT_SetTimeouts(handle, 1000, 1000);
moel@87:         FTD2XX.FT_Purge(handle, FT_PURGE.FT_PURGE_ALL);
moel@87:         
moel@87:         status = FTD2XX.Write(handle, new byte[] { 0x38 });
moel@87:         if (status != FT_STATUS.FT_OK) {
moel@87:           report.AppendLine("Write Status: " + status);
moel@87:           FTD2XX.FT_Close(handle);
moel@87:           continue;
moel@87:         }
moel@87: 
moel@87:         bool isValid = false;
moel@87:         byte protocolVersion = 0;
moel@87: 
moel@87:         int j = 0;
moel@87:         while (FTD2XX.BytesToRead(handle) == 0 && j < 2) {
moel@87:           Thread.Sleep(100);
moel@87:           j++;
moel@87:         }
moel@87:         if (FTD2XX.BytesToRead(handle) > 0) {
moel@87:           if (FTD2XX.ReadByte(handle) == TBalancer.STARTFLAG) {
moel@87:             while (FTD2XX.BytesToRead(handle) < 284 && j < 5) {
moel@87:               Thread.Sleep(100);
moel@87:               j++;
moel@87:             }
moel@87:             int length = FTD2XX.BytesToRead(handle);
moel@87:             if (length >= 284) {
moel@87:               byte[] data = new byte[285];
moel@87:               data[0] = TBalancer.STARTFLAG;
moel@87:               for (int k = 1; k < data.Length; k++)
moel@87:                 data[k] = FTD2XX.ReadByte(handle);
moel@87: 
moel@87:               // check protocol version 2X (protocols seen: 2C, 2A, 28)
moel@87:               isValid = (data[274] & 0xF0) == 0x20;
moel@87:               protocolVersion = data[274];
moel@87:               if (!isValid) {
moel@87:                 report.Append("Status: Wrong Protocol Version: 0x");
moel@166:                 report.AppendLine(
moel@166:                   protocolVersion.ToString("X", CultureInfo.InvariantCulture));
moel@1:               }
moel@32:             } else {
moel@87:               report.AppendLine("Status: Wrong Message Length: " + length);
moel@1:             }
moel@87:           } else {
moel@87:             report.AppendLine("Status: Wrong Startflag");
moel@1:           }
moel@87:         } else {
moel@87:           report.AppendLine("Status: No Response");
moel@87:         }
moel@87: 
moel@87:         FTD2XX.FT_Purge(handle, FT_PURGE.FT_PURGE_ALL);
moel@87:         FTD2XX.FT_Close(handle);
moel@87: 
moel@87:         if (isValid) {
moel@87:           report.AppendLine("Status: OK");
moel@351:           hardware.Add(new TBalancer(i, protocolVersion, settings));          
moel@87:         }
moel@351: 
moel@351:         if (i < numDevices - 1)
moel@351:           report.AppendLine();
moel@1:       }
moel@1:     }
moel@1: 
moel@1:     public IHardware[] Hardware {
moel@1:       get {
moel@1:         return hardware.ToArray();
moel@1:       }
moel@1:     }
moel@1: 
moel@1:     public string GetReport() {
moel@32:       if (report.Length > 0) {
moel@256:         StringBuilder r = new StringBuilder();
moel@256:         r.AppendLine("FTD2XX");
moel@256:         r.AppendLine();
moel@256:         r.Append(report);
moel@256:         r.AppendLine();
moel@256:         return r.ToString();
moel@32:       } else
moel@32:         return null;
moel@1:     }
moel@1: 
moel@1:     public void Close() {
moel@1:       foreach (TBalancer tbalancer in hardware)
moel@1:         tbalancer.Close();
moel@1:     }
moel@1:   }
moel@1: }