Hardware/TBalancer/FTD2XX.cs
author moel.mich
Tue, 14 Feb 2012 23:07:55 +0000
changeset 340 600962f8a298
parent 186 010d719f9245
child 344 3145aadca3d2
permissions -rw-r--r--
Added a new sensor type "Factor" for dimensionless values (and similar) that are not to be shown as percent ("Level" type). Changed the write amplification sensor to use the new "Factor" sensor type. Added the temperature SMART attribute for Sandforce SSDs as hidden sensor (as it may show fake results on some hardware).
moel@87
     1
/*
moel@87
     2
  
moel@87
     3
  Version: MPL 1.1/GPL 2.0/LGPL 2.1
moel@87
     4
moel@87
     5
  The contents of this file are subject to the Mozilla Public License Version
moel@87
     6
  1.1 (the "License"); you may not use this file except in compliance with
moel@87
     7
  the License. You may obtain a copy of the License at
moel@87
     8
 
moel@87
     9
  http://www.mozilla.org/MPL/
moel@87
    10
moel@87
    11
  Software distributed under the License is distributed on an "AS IS" basis,
moel@87
    12
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
moel@87
    13
  for the specific language governing rights and limitations under the License.
moel@87
    14
moel@87
    15
  The Original Code is the Open Hardware Monitor code.
moel@87
    16
moel@87
    17
  The Initial Developer of the Original Code is 
moel@87
    18
  Michael Möller <m.moeller@gmx.ch>.
moel@87
    19
  Portions created by the Initial Developer are Copyright (C) 2009-2010
moel@87
    20
  the Initial Developer. All Rights Reserved.
moel@87
    21
moel@87
    22
  Contributor(s):
moel@87
    23
moel@87
    24
  Alternatively, the contents of this file may be used under the terms of
moel@87
    25
  either the GNU General Public License Version 2 or later (the "GPL"), or
moel@87
    26
  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
moel@87
    27
  in which case the provisions of the GPL or the LGPL are applicable instead
moel@87
    28
  of those above. If you wish to allow use of your version of this file only
moel@87
    29
  under the terms of either the GPL or the LGPL, and not to allow others to
moel@87
    30
  use your version of this file under the terms of the MPL, indicate your
moel@87
    31
  decision by deleting the provisions above and replace them with the notice
moel@87
    32
  and other provisions required by the GPL or the LGPL. If you do not delete
moel@87
    33
  the provisions above, a recipient may use your version of this file under
moel@87
    34
  the terms of any one of the MPL, the GPL or the LGPL.
moel@87
    35
 
moel@87
    36
*/
moel@87
    37
moel@87
    38
using System;
moel@87
    39
using System.Runtime.InteropServices;
moel@87
    40
moel@87
    41
namespace OpenHardwareMonitor.Hardware.TBalancer {
moel@87
    42
moel@165
    43
  internal enum FT_DEVICE : uint {
moel@186
    44
    FT_DEVICE_232BM,
moel@186
    45
    FT_DEVICE_232AM,
moel@87
    46
    FT_DEVICE_100AX,
moel@87
    47
    FT_DEVICE_UNKNOWN,
moel@186
    48
    FT_DEVICE_2232C,
moel@87
    49
    FT_DEVICE_232R,
moel@87
    50
    FT_DEVICE_2232H,
moel@87
    51
    FT_DEVICE_4232H
moel@87
    52
  }
moel@87
    53
moel@165
    54
  internal enum FT_STATUS {
moel@87
    55
    FT_OK,
moel@87
    56
    FT_INVALID_HANDLE,
moel@87
    57
    FT_DEVICE_NOT_FOUND,
moel@87
    58
    FT_DEVICE_NOT_OPENED,
moel@87
    59
    FT_IO_ERROR,
moel@87
    60
    FT_INSUFFICIENT_RESOURCES,
moel@87
    61
    FT_INVALID_PARAMETER,
moel@87
    62
    FT_INVALID_BAUD_RATE,
moel@87
    63
    FT_DEVICE_NOT_OPENED_FOR_ERASE,
moel@87
    64
    FT_DEVICE_NOT_OPENED_FOR_WRITE,
moel@87
    65
    FT_FAILED_TO_WRITE_DEVICE,
moel@87
    66
    FT_EEPROM_READ_FAILED,
moel@87
    67
    FT_EEPROM_WRITE_FAILED,
moel@87
    68
    FT_EEPROM_ERASE_FAILED,
moel@87
    69
    FT_EEPROM_NOT_PRESENT,
moel@87
    70
    FT_EEPROM_NOT_PROGRAMMED,
moel@87
    71
    FT_INVALID_ARGS,
moel@87
    72
    FT_OTHER_ERROR
moel@87
    73
  }
moel@87
    74
moel@165
    75
  internal enum FT_FLOW_CONTROL : ushort {
moel@87
    76
    FT_FLOW_DTR_DSR = 512,
moel@87
    77
    FT_FLOW_NONE = 0,
moel@87
    78
    FT_FLOW_RTS_CTS = 256,
moel@87
    79
    FT_FLOW_XON_XOFF = 1024,
moel@87
    80
  }
moel@87
    81
moel@165
    82
  internal enum FT_PURGE : uint {
moel@87
    83
    FT_PURGE_RX = 1,
moel@87
    84
    FT_PURGE_TX = 2,
moel@87
    85
    FT_PURGE_ALL = 3,
moel@87
    86
  }
moel@87
    87
moel@87
    88
  [StructLayout(LayoutKind.Sequential)]
moel@165
    89
  internal struct FT_HANDLE {
moel@195
    90
    private readonly uint handle;
moel@87
    91
  }
moel@87
    92
moel@87
    93
  [StructLayout(LayoutKind.Sequential)]
moel@165
    94
  internal struct FT_DEVICE_INFO_NODE {    
moel@87
    95
    public uint Flags;
moel@87
    96
    public FT_DEVICE Type; 
moel@87
    97
    public uint ID; 
moel@87
    98
    public uint LocId; 
moel@87
    99
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
moel@87
   100
    public string SerialNumber; 
moel@87
   101
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
moel@87
   102
    public string Description;
moel@87
   103
    public FT_HANDLE Handle;
moel@87
   104
  }
moel@87
   105
moel@165
   106
  internal class FTD2XX {
moel@87
   107
moel@87
   108
    public delegate FT_STATUS FT_CreateDeviceInfoListDelegate(
moel@87
   109
      out uint numDevices);
moel@87
   110
    public delegate FT_STATUS FT_GetDeviceInfoListDelegate(
moel@87
   111
      [Out] FT_DEVICE_INFO_NODE[] deviceInfoNodes, ref uint length);
moel@87
   112
    public delegate FT_STATUS FT_OpenDelegate(int device, out FT_HANDLE handle);
moel@87
   113
    public delegate FT_STATUS FT_CloseDelegate(FT_HANDLE handle);
moel@87
   114
    public delegate FT_STATUS FT_SetBaudRateDelegate(FT_HANDLE handle,
moel@87
   115
      uint baudRate);
moel@87
   116
    public delegate FT_STATUS FT_SetDataCharacteristicsDelegate(
moel@87
   117
      FT_HANDLE handle, byte wordLength, byte stopBits, byte parity);
moel@87
   118
    public delegate FT_STATUS FT_SetFlowControlDelegate(FT_HANDLE handle,
moel@87
   119
      FT_FLOW_CONTROL flowControl, byte xon, byte xoff);
moel@87
   120
    public delegate FT_STATUS FT_SetTimeoutsDelegate(FT_HANDLE handle,
moel@87
   121
      uint readTimeout, uint writeTimeout);
moel@87
   122
    public delegate FT_STATUS FT_WriteDelegate(FT_HANDLE handle, byte[] buffer,
moel@87
   123
      uint bytesToWrite, out uint bytesWritten);
moel@87
   124
    public delegate FT_STATUS FT_PurgeDelegate(FT_HANDLE handle, FT_PURGE mask);
moel@87
   125
    public delegate FT_STATUS FT_GetStatusDelegate(FT_HANDLE handle,
moel@87
   126
      out uint amountInRxQueue, out uint amountInTxQueue, out uint eventStatus);
moel@87
   127
    public delegate FT_STATUS FT_ReadDelegate(FT_HANDLE handle, 
moel@87
   128
      [Out] byte[] buffer, uint bytesToRead, out uint bytesReturned);
moel@87
   129
moel@167
   130
    public static readonly FT_CreateDeviceInfoListDelegate 
moel@167
   131
      FT_CreateDeviceInfoList = CreateDelegate<
moel@167
   132
      FT_CreateDeviceInfoListDelegate>("FT_CreateDeviceInfoList");
moel@167
   133
    public static readonly FT_GetDeviceInfoListDelegate 
moel@167
   134
      FT_GetDeviceInfoList = CreateDelegate<
moel@167
   135
      FT_GetDeviceInfoListDelegate>("FT_GetDeviceInfoList");
moel@167
   136
    public static readonly FT_OpenDelegate 
moel@167
   137
      FT_Open = CreateDelegate<
moel@167
   138
      FT_OpenDelegate>("FT_Open");
moel@167
   139
    public static readonly FT_CloseDelegate 
moel@167
   140
      FT_Close = CreateDelegate<
moel@167
   141
      FT_CloseDelegate>("FT_Close");
moel@167
   142
    public static readonly FT_SetBaudRateDelegate 
moel@167
   143
      FT_SetBaudRate = CreateDelegate<
moel@167
   144
      FT_SetBaudRateDelegate>("FT_SetBaudRate");
moel@167
   145
    public static readonly FT_SetDataCharacteristicsDelegate 
moel@167
   146
      FT_SetDataCharacteristics = CreateDelegate<
moel@167
   147
      FT_SetDataCharacteristicsDelegate>("FT_SetDataCharacteristics");
moel@167
   148
    public static readonly FT_SetFlowControlDelegate 
moel@167
   149
      FT_SetFlowControl = CreateDelegate<
moel@167
   150
      FT_SetFlowControlDelegate>("FT_SetFlowControl");
moel@167
   151
    public static readonly FT_SetTimeoutsDelegate 
moel@167
   152
      FT_SetTimeouts = CreateDelegate<
moel@167
   153
      FT_SetTimeoutsDelegate>("FT_SetTimeouts");
moel@167
   154
    public static readonly FT_WriteDelegate 
moel@167
   155
      FT_Write = CreateDelegate<
moel@167
   156
      FT_WriteDelegate>("FT_Write");
moel@167
   157
    public static readonly FT_PurgeDelegate 
moel@167
   158
      FT_Purge = CreateDelegate<
moel@167
   159
      FT_PurgeDelegate>("FT_Purge");
moel@167
   160
    public static readonly FT_GetStatusDelegate 
moel@167
   161
      FT_GetStatus = CreateDelegate<
moel@167
   162
      FT_GetStatusDelegate>("FT_GetStatus");
moel@167
   163
    public static readonly FT_ReadDelegate 
moel@167
   164
      FT_Read = CreateDelegate<
moel@167
   165
      FT_ReadDelegate>("FT_Read");
moel@167
   166
moel@167
   167
    private FTD2XX() { }
moel@87
   168
moel@87
   169
    public static FT_STATUS Write(FT_HANDLE handle, byte[] buffer) {
moel@87
   170
      uint bytesWritten;
moel@87
   171
      FT_STATUS status = FT_Write(handle, buffer, (uint)buffer.Length, 
moel@87
   172
        out bytesWritten);
moel@87
   173
      if (bytesWritten != buffer.Length)
moel@87
   174
        return FT_STATUS.FT_FAILED_TO_WRITE_DEVICE;
moel@87
   175
      else
moel@87
   176
        return status;
moel@87
   177
    }
moel@87
   178
moel@87
   179
    public static int BytesToRead(FT_HANDLE handle) {
moel@87
   180
      uint amountInRxQueue;
moel@87
   181
      uint amountInTxQueue;
moel@87
   182
      uint eventStatus;
moel@87
   183
      if (FT_GetStatus(handle, out amountInRxQueue, out amountInTxQueue,
moel@87
   184
        out eventStatus) == FT_STATUS.FT_OK) {
moel@87
   185
        return (int)amountInRxQueue;
moel@87
   186
      } else {
moel@87
   187
        return 0;
moel@87
   188
      }
moel@87
   189
    }
moel@87
   190
moel@87
   191
    public static byte ReadByte(FT_HANDLE handle) {
moel@87
   192
      byte[] buffer = new byte[1];
moel@87
   193
      uint bytesReturned;
moel@87
   194
      FT_STATUS status = FT_Read(handle, buffer, 1, out bytesReturned);
moel@87
   195
      if (status != FT_STATUS.FT_OK || bytesReturned != 1)
moel@167
   196
        throw new InvalidOperationException();
moel@87
   197
      return buffer[0];
moel@87
   198
    }
moel@87
   199
moel@167
   200
    private static string GetDllName() {
moel@195
   201
      int p = (int)Environment.OSVersion.Platform;
moel@167
   202
      if ((p == 4) || (p == 128))
moel@167
   203
        return "libftd2xx.so";
moel@167
   204
      else
moel@167
   205
        return "ftd2xx.dll";
moel@167
   206
    }
moel@87
   207
moel@167
   208
    private static T CreateDelegate<T>(string entryPoint)
moel@87
   209
      where T : class {
moel@167
   210
      DllImportAttribute attribute = new DllImportAttribute(GetDllName());
moel@87
   211
      attribute.CallingConvention = CallingConvention.StdCall;
moel@87
   212
      attribute.PreserveSig = true;
moel@87
   213
      attribute.EntryPoint = entryPoint;
moel@167
   214
      T newDelegate;
moel@87
   215
      PInvokeDelegateFactory.CreateDelegate(attribute, out newDelegate);
moel@167
   216
      return newDelegate;
moel@87
   217
    }
moel@87
   218
  }
moel@87
   219
}