Added a check if all required files are available at startup. Improved the crash reporting.
3 Version: MPL 1.1/GPL 2.0/LGPL 2.1
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
9 http://www.mozilla.org/MPL/
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.
15 The Original Code is the Open Hardware Monitor code.
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.
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.
39 using System.Collections.Generic;
40 using System.Runtime.InteropServices;
42 namespace OpenHardwareMonitor.Hardware.TBalancer {
44 public enum FT_DEVICE : uint {
55 public enum FT_STATUS {
61 FT_INSUFFICIENT_RESOURCES,
64 FT_DEVICE_NOT_OPENED_FOR_ERASE,
65 FT_DEVICE_NOT_OPENED_FOR_WRITE,
66 FT_FAILED_TO_WRITE_DEVICE,
67 FT_EEPROM_READ_FAILED,
68 FT_EEPROM_WRITE_FAILED,
69 FT_EEPROM_ERASE_FAILED,
70 FT_EEPROM_NOT_PRESENT,
71 FT_EEPROM_NOT_PROGRAMMED,
76 public enum FT_FLOW_CONTROL : ushort {
77 FT_FLOW_DTR_DSR = 512,
79 FT_FLOW_RTS_CTS = 256,
80 FT_FLOW_XON_XOFF = 1024,
83 public enum FT_PURGE : uint {
89 [StructLayout(LayoutKind.Sequential)]
90 public struct FT_HANDLE {
94 [StructLayout(LayoutKind.Sequential)]
95 public struct FT_DEVICE_INFO_NODE {
97 public FT_DEVICE Type;
100 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
101 public string SerialNumber;
102 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
103 public string Description;
104 public FT_HANDLE Handle;
107 public class FTD2XX {
109 public delegate FT_STATUS FT_CreateDeviceInfoListDelegate(
110 out uint numDevices);
111 public delegate FT_STATUS FT_GetDeviceInfoListDelegate(
112 [Out] FT_DEVICE_INFO_NODE[] deviceInfoNodes, ref uint length);
113 public delegate FT_STATUS FT_OpenDelegate(int device, out FT_HANDLE handle);
114 public delegate FT_STATUS FT_CloseDelegate(FT_HANDLE handle);
115 public delegate FT_STATUS FT_SetBaudRateDelegate(FT_HANDLE handle,
117 public delegate FT_STATUS FT_SetDataCharacteristicsDelegate(
118 FT_HANDLE handle, byte wordLength, byte stopBits, byte parity);
119 public delegate FT_STATUS FT_SetFlowControlDelegate(FT_HANDLE handle,
120 FT_FLOW_CONTROL flowControl, byte xon, byte xoff);
121 public delegate FT_STATUS FT_SetTimeoutsDelegate(FT_HANDLE handle,
122 uint readTimeout, uint writeTimeout);
123 public delegate FT_STATUS FT_WriteDelegate(FT_HANDLE handle, byte[] buffer,
124 uint bytesToWrite, out uint bytesWritten);
125 public delegate FT_STATUS FT_PurgeDelegate(FT_HANDLE handle, FT_PURGE mask);
126 public delegate FT_STATUS FT_GetStatusDelegate(FT_HANDLE handle,
127 out uint amountInRxQueue, out uint amountInTxQueue, out uint eventStatus);
128 public delegate FT_STATUS FT_ReadDelegate(FT_HANDLE handle,
129 [Out] byte[] buffer, uint bytesToRead, out uint bytesReturned);
131 public static FT_CreateDeviceInfoListDelegate FT_CreateDeviceInfoList;
132 public static FT_GetDeviceInfoListDelegate FT_GetDeviceInfoList;
133 public static FT_OpenDelegate FT_Open;
134 public static FT_CloseDelegate FT_Close;
135 public static FT_SetBaudRateDelegate FT_SetBaudRate;
136 public static FT_SetDataCharacteristicsDelegate FT_SetDataCharacteristics;
137 public static FT_SetFlowControlDelegate FT_SetFlowControl;
138 public static FT_SetTimeoutsDelegate FT_SetTimeouts;
139 public static FT_WriteDelegate FT_Write;
140 public static FT_PurgeDelegate FT_Purge;
141 public static FT_GetStatusDelegate FT_GetStatus;
142 public static FT_ReadDelegate FT_Read;
144 public static FT_STATUS Write(FT_HANDLE handle, byte[] buffer) {
146 FT_STATUS status = FT_Write(handle, buffer, (uint)buffer.Length,
148 if (bytesWritten != buffer.Length)
149 return FT_STATUS.FT_FAILED_TO_WRITE_DEVICE;
154 public static int BytesToRead(FT_HANDLE handle) {
155 uint amountInRxQueue;
156 uint amountInTxQueue;
158 if (FT_GetStatus(handle, out amountInRxQueue, out amountInTxQueue,
159 out eventStatus) == FT_STATUS.FT_OK) {
160 return (int)amountInRxQueue;
166 public static byte ReadByte(FT_HANDLE handle) {
167 byte[] buffer = new byte[1];
169 FT_STATUS status = FT_Read(handle, buffer, 1, out bytesReturned);
170 if (status != FT_STATUS.FT_OK || bytesReturned != 1)
171 throw new Exception();
175 private static string dllName;
177 private static void GetDelegate<T>(string entryPoint, out T newDelegate)
179 DllImportAttribute attribute = new DllImportAttribute(dllName);
180 attribute.CallingConvention = CallingConvention.StdCall;
181 attribute.PreserveSig = true;
182 attribute.EntryPoint = entryPoint;
183 PInvokeDelegateFactory.CreateDelegate(attribute, out newDelegate);
187 int p = (int)System.Environment.OSVersion.Platform;
188 if ((p == 4) || (p == 128))
189 dllName = "libftd2xx.so";
191 dllName = "ftd2xx.dll";
193 GetDelegate("FT_CreateDeviceInfoList", out FT_CreateDeviceInfoList);
194 GetDelegate("FT_GetDeviceInfoList", out FT_GetDeviceInfoList);
195 GetDelegate("FT_Open", out FT_Open);
196 GetDelegate("FT_Close", out FT_Close);
197 GetDelegate("FT_SetBaudRate", out FT_SetBaudRate);
198 GetDelegate("FT_SetDataCharacteristics", out FT_SetDataCharacteristics);
199 GetDelegate("FT_SetFlowControl", out FT_SetFlowControl);
200 GetDelegate("FT_SetTimeouts", out FT_SetTimeouts);
201 GetDelegate("FT_Write", out FT_Write);
202 GetDelegate("FT_Purge", out FT_Purge);
203 GetDelegate("FT_GetStatus", out FT_GetStatus);
204 GetDelegate("FT_Read", out FT_Read);