2 * Copyright (c) 2007, IRTrans GmbH
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of IRTrans GmbH nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY IRTrans GmbH ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL IRTrans GmbH BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48 char usbcomports
[20][5];
57 memset (&os
,0,sizeof (OSVERSIONINFO
));
58 os
.dwOSVersionInfoSize
= sizeof (OSVERSIONINFO
);
60 ver
= os
.dwMajorVersion
* 100 + os
.dwMinorVersion
* 10;
62 cpu
= getenv ("PROCESSOR_ARCHITEW6432");
70 void GetComPorts (void)
75 res
= QueryDosDevice (NULL
,buf
,20000);
80 if (!memcmp (buf
+pos
,"COM",3)) cnt
++;
81 while (buf
[pos
]) pos
++;
85 if (cnt
> 20) cnt
= 20;
91 if (!memcmp (buf
+pos
,"COM",3)) {
92 strcpy (usbcomports
[cnt
-1],buf
+pos
);
95 while (buf
[pos
]) pos
++;
102 int OpenUSBPort (void)
106 stat
= F_OpenEx ("IRTrans USB",FT_OPEN_BY_DESCRIPTION
,&usb
);
107 if (stat
) stat
= F_OpenEx ("FB401",FT_OPEN_BY_DESCRIPTION
,&usb
);
108 if (stat
) stat
= F_OpenEx ("IRTrans USB B",FT_OPEN_BY_DESCRIPTION
,&usb
);
110 if (stat
) return (ERR_OPENUSB
);
116 int WriteUSBString (byte pnt
[],int len
)
121 stat
= F_Write (usb
,pnt
,len
,&num
);
122 if (stat
) return (0);
128 int ReadUSBString (byte pnt
[],int len
,word timeout
)
133 F_SetTimeouts (usb
,timeout
,0);
135 stat
= F_Read (usb
,pnt
,len
,&num
);
137 if (stat
) return (0);
143 int GetUSBAvailableEx (DEVICEINFO
*dev
)
148 stat
= F_GetQueueStatus (dev
->io
.usbport
,&num
);
150 if (stat
) return (0);
156 int ReadUSBStringEx (DEVICEINFO
*dev
,byte pnt
[],int len
,word timeout
)
161 F_SetTimeouts (dev
->io
.usbport
,timeout
,0);
163 stat
= F_Read (dev
->io
.usbport
,pnt
,len
,&num
);
165 if (stat
) return (0);
173 F_Purge (usb
,FT_PURGE_RX
| FT_PURGE_TX
);
177 void FlushUSBEx (FT_HANDLE hndl
)
179 F_Purge (hndl
,FT_PURGE_RX
| FT_PURGE_TX
);
182 void WriteUSBStringEx (DEVICEINFO
*dev
,byte pnt
[],int len
)
187 stat
= F_Write (dev
->io
.usbport
,pnt
,len
,&num
);
189 F_Close (dev
->io
.usbport
);
190 if (mode_flag
& NO_RECONNECT
) {
191 log_print ("IRTrans Connection lost. Aborting ...\n",LOG_FATAL
);
195 log_print ("Trying reconnect ...\n",LOG_DEBUG
);
196 stat
= F_OpenEx (dev
->usb_serno
,FT_OPEN_BY_SERIAL_NUMBER
,&dev
->io
.usbport
);
197 if (stat
) Sleep (1000);
200 log_print ("Reconnected ...\n",LOG_DEBUG
);
207 void SetUSBEventEx (DEVICEINFO
*dev
,DWORD mask
)
209 F_SetEventNotification (dev
->io
.usbport
,mask
,dev
->io
.event
);
212 void break_signal (int sig
)
214 log_print ("Abort ...\n",LOG_FATAL
);
219 void cleanup_exit (void)
223 for (i
=0;i
< device_cnt
;i
++) if (IRDevices
[i
].io
.if_type
== IF_USB
) F_Close (IRDevices
[i
].io
.usbport
);
225 if (hdll
) FreeLibrary(hdll
);
229 int LoadUSBLibrary (void)
234 atexit (cleanup_exit
);
235 signal (SIGINT
,break_signal
);
237 hdll
= LoadLibrary("Ftd2xx.dll");
240 sprintf (msg
,"Error: Can't Load ftd2xx.dll\n");
241 log_print (msg
,LOG_FATAL
);
245 m_pListDevices
= (PtrToListDevices
)GetProcAddress(hdll
, "FT_ListDevices");
247 m_pOpen
= (PtrToOpen
)GetProcAddress(hdll
, "FT_Open");
249 m_pOpenEx
= (PtrToOpenEx
)GetProcAddress(hdll
, "FT_OpenEx");
251 m_pRead
= (PtrToRead
)GetProcAddress(hdll
, "FT_Read");
253 m_pClose
= (PtrToClose
)GetProcAddress(hdll
, "FT_Close");
255 m_pGetQueueStatus
= (PtrToGetQueueStatus
)GetProcAddress(hdll
, "FT_GetQueueStatus");
257 m_pWrite
= (PtrToWrite
)GetProcAddress(hdll
, "FT_Write");
259 m_pResetDevice
= (PtrToResetDevice
)GetProcAddress(hdll
, "FT_ResetDevice");
261 m_pPurge
= (PtrToPurge
)GetProcAddress(hdll
, "FT_Purge");
263 m_pSetTimeouts
= (PtrToSetTimeouts
)GetProcAddress(hdll
, "FT_SetTimeouts");
265 m_pSetEvent
= (PtrToSetEvent
)GetProcAddress(hdll
, "FT_SetEventNotification");
267 m_pGetDeviceInfo
= (PtrToGetDeviceInfo
)GetProcAddress(hdll
, "FT_GetDeviceInfo");
272 enum FT_STATUS
F_GetDeviceInfo(FT_HANDLE usb
,FT_DEVICE
*device
,DWORD
*id
,char *serno
,char *desc
,PVOID dummy
)
274 return (*m_pGetDeviceInfo
)(usb
, device
, id
, serno
, desc
, dummy
);
277 enum FT_STATUS
F_SetEventNotification(FT_HANDLE usb
,DWORD mask
,PVOID event
)
279 return (*m_pSetEvent
)(usb
, mask
, event
);
283 enum FT_STATUS
F_ListDevices(PVOID pArg1
, PVOID pArg2
, DWORD dwFlags
)
285 return (*m_pListDevices
)(pArg1
, pArg2
, dwFlags
);
288 enum FT_STATUS
F_Open(PVOID pvDevice
,FT_HANDLE
*usb
)
290 return (*m_pOpen
)(pvDevice
, usb
);
293 enum FT_STATUS
F_OpenEx(PVOID pArg1
, DWORD dwFlags
,FT_HANDLE
*usb
)
295 return (*m_pOpenEx
)(pArg1
, dwFlags
, usb
);
298 enum FT_STATUS
F_Read(FT_HANDLE usb
,LPVOID lpvBuffer
, DWORD dwBuffSize
, LPDWORD lpdwBytesRead
)
300 return (*m_pRead
)(usb
, lpvBuffer
, dwBuffSize
, lpdwBytesRead
);
303 enum FT_STATUS
F_Close(FT_HANDLE usb
)
305 return (*m_pClose
)(usb
);
308 enum FT_STATUS
F_GetQueueStatus(FT_HANDLE usb
,LPDWORD lpdwAmountInRxQueue
)
310 return (*m_pGetQueueStatus
)(usb
, lpdwAmountInRxQueue
);
313 enum FT_STATUS
F_Write(FT_HANDLE usb
,LPVOID lpvBuffer
, DWORD dwBuffSize
, LPDWORD lpdwBytes
)
315 return (*m_pWrite
)(usb
, lpvBuffer
, dwBuffSize
, lpdwBytes
);
319 enum FT_STATUS
F_ResetDevice(FT_HANDLE usb
)
321 return (*m_pResetDevice
)(usb
);
324 enum FT_STATUS
F_Purge(FT_HANDLE usb
,ULONG dwMask
)
326 return (*m_pPurge
)(usb
, dwMask
);
330 enum FT_STATUS
F_SetTimeouts(FT_HANDLE usb
,ULONG dwReadTimeout
, ULONG dwWriteTimeout
)
332 return (*m_pSetTimeouts
)(usb
, dwReadTimeout
, dwWriteTimeout
);