Now fetching some of our Sound Graph DLL function pointers.
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/.
7 Copyright (C) 2009-2012 Michael Möller <mmoeller@openhardwaremonitor.org>
12 using System.Runtime.InteropServices;
14 namespace OpenHardwareMonitor.Hardware.TBalancer {
16 internal enum FT_DEVICE : uint {
27 internal enum FT_STATUS {
33 FT_INSUFFICIENT_RESOURCES,
36 FT_DEVICE_NOT_OPENED_FOR_ERASE,
37 FT_DEVICE_NOT_OPENED_FOR_WRITE,
38 FT_FAILED_TO_WRITE_DEVICE,
39 FT_EEPROM_READ_FAILED,
40 FT_EEPROM_WRITE_FAILED,
41 FT_EEPROM_ERASE_FAILED,
42 FT_EEPROM_NOT_PRESENT,
43 FT_EEPROM_NOT_PROGRAMMED,
48 internal enum FT_FLOW_CONTROL : ushort {
49 FT_FLOW_DTR_DSR = 512,
51 FT_FLOW_RTS_CTS = 256,
52 FT_FLOW_XON_XOFF = 1024,
55 internal enum FT_PURGE : uint {
61 [StructLayout(LayoutKind.Sequential)]
62 internal struct FT_HANDLE {
63 private readonly uint handle;
66 [StructLayout(LayoutKind.Sequential)]
67 internal struct FT_DEVICE_INFO_NODE {
69 public FT_DEVICE Type;
72 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
73 public string SerialNumber;
74 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
75 public string Description;
76 public FT_HANDLE Handle;
79 internal class FTD2XX {
81 public delegate FT_STATUS FT_CreateDeviceInfoListDelegate(
83 public delegate FT_STATUS FT_GetDeviceInfoListDelegate(
84 [Out] FT_DEVICE_INFO_NODE[] deviceInfoNodes, ref uint length);
85 public delegate FT_STATUS FT_OpenDelegate(int device, out FT_HANDLE handle);
86 public delegate FT_STATUS FT_CloseDelegate(FT_HANDLE handle);
87 public delegate FT_STATUS FT_SetBaudRateDelegate(FT_HANDLE handle,
89 public delegate FT_STATUS FT_SetDataCharacteristicsDelegate(
90 FT_HANDLE handle, byte wordLength, byte stopBits, byte parity);
91 public delegate FT_STATUS FT_SetFlowControlDelegate(FT_HANDLE handle,
92 FT_FLOW_CONTROL flowControl, byte xon, byte xoff);
93 public delegate FT_STATUS FT_SetTimeoutsDelegate(FT_HANDLE handle,
94 uint readTimeout, uint writeTimeout);
95 public delegate FT_STATUS FT_WriteDelegate(FT_HANDLE handle, byte[] buffer,
96 uint bytesToWrite, out uint bytesWritten);
97 public delegate FT_STATUS FT_PurgeDelegate(FT_HANDLE handle, FT_PURGE mask);
98 public delegate FT_STATUS FT_GetStatusDelegate(FT_HANDLE handle,
99 out uint amountInRxQueue, out uint amountInTxQueue, out uint eventStatus);
100 public delegate FT_STATUS FT_ReadDelegate(FT_HANDLE handle,
101 [Out] byte[] buffer, uint bytesToRead, out uint bytesReturned);
102 public delegate FT_STATUS FT_ReadByteDelegate(FT_HANDLE handle,
103 out byte buffer, uint bytesToRead, out uint bytesReturned);
105 public static readonly FT_CreateDeviceInfoListDelegate
106 FT_CreateDeviceInfoList = CreateDelegate<
107 FT_CreateDeviceInfoListDelegate>("FT_CreateDeviceInfoList");
108 public static readonly FT_GetDeviceInfoListDelegate
109 FT_GetDeviceInfoList = CreateDelegate<
110 FT_GetDeviceInfoListDelegate>("FT_GetDeviceInfoList");
111 public static readonly FT_OpenDelegate
112 FT_Open = CreateDelegate<
113 FT_OpenDelegate>("FT_Open");
114 public static readonly FT_CloseDelegate
115 FT_Close = CreateDelegate<
116 FT_CloseDelegate>("FT_Close");
117 public static readonly FT_SetBaudRateDelegate
118 FT_SetBaudRate = CreateDelegate<
119 FT_SetBaudRateDelegate>("FT_SetBaudRate");
120 public static readonly FT_SetDataCharacteristicsDelegate
121 FT_SetDataCharacteristics = CreateDelegate<
122 FT_SetDataCharacteristicsDelegate>("FT_SetDataCharacteristics");
123 public static readonly FT_SetFlowControlDelegate
124 FT_SetFlowControl = CreateDelegate<
125 FT_SetFlowControlDelegate>("FT_SetFlowControl");
126 public static readonly FT_SetTimeoutsDelegate
127 FT_SetTimeouts = CreateDelegate<
128 FT_SetTimeoutsDelegate>("FT_SetTimeouts");
129 public static readonly FT_WriteDelegate
130 FT_Write = CreateDelegate<
131 FT_WriteDelegate>("FT_Write");
132 public static readonly FT_PurgeDelegate
133 FT_Purge = CreateDelegate<
134 FT_PurgeDelegate>("FT_Purge");
135 public static readonly FT_GetStatusDelegate
136 FT_GetStatus = CreateDelegate<
137 FT_GetStatusDelegate>("FT_GetStatus");
138 public static readonly FT_ReadDelegate
139 FT_Read = CreateDelegate<
140 FT_ReadDelegate>("FT_Read");
141 public static readonly FT_ReadByteDelegate
142 FT_ReadByte = CreateDelegate<
143 FT_ReadByteDelegate>("FT_Read");
147 public static FT_STATUS Write(FT_HANDLE handle, byte[] buffer) {
149 FT_STATUS status = FT_Write(handle, buffer, (uint)buffer.Length,
151 if (bytesWritten != buffer.Length)
152 return FT_STATUS.FT_FAILED_TO_WRITE_DEVICE;
157 public static int BytesToRead(FT_HANDLE handle) {
158 uint amountInRxQueue;
159 uint amountInTxQueue;
161 if (FT_GetStatus(handle, out amountInRxQueue, out amountInTxQueue,
162 out eventStatus) == FT_STATUS.FT_OK) {
163 return (int)amountInRxQueue;
169 public static byte ReadByte(FT_HANDLE handle) {
172 FT_STATUS status = FT_ReadByte(handle, out buffer, 1, out bytesReturned);
173 if (status != FT_STATUS.FT_OK || bytesReturned != 1)
174 throw new InvalidOperationException();
178 public static void Read(FT_HANDLE handle, byte[] buffer) {
181 FT_Read(handle, buffer, (uint)buffer.Length, out bytesReturned);
182 if (status != FT_STATUS.FT_OK || bytesReturned != buffer.Length)
183 throw new InvalidOperationException();
186 private static string GetDllName() {
187 int p = (int)Environment.OSVersion.Platform;
188 if ((p == 4) || (p == 128))
189 return "libftd2xx.so";
194 private static T CreateDelegate<T>(string entryPoint)
196 DllImportAttribute attribute = new DllImportAttribute(GetDllName());
197 attribute.CallingConvention = CallingConvention.StdCall;
198 attribute.PreserveSig = true;
199 attribute.EntryPoint = entryPoint;
201 PInvokeDelegateFactory.CreateDelegate(attribute, out newDelegate);