Mostly minor fixes up until version 0.8.10.
[irreco.git] / irtrans / irserver / src / winusbio.c
blob3a591a2f943ade0b67954983d5326a4c4c948e58
1 /*
2 * Copyright (c) 2007, IRTrans GmbH
3 * All rights reserved.
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.
30 #ifdef WIN32
33 #include <windows.h>
34 #include <winbase.h>
35 #include <stdio.h>
36 #include <signal.h>
38 #include "errcode.h"
39 #include "remote.h"
40 #include "lowlevel.h"
41 #include "global.h"
42 #include "winusbio.h"
45 FT_HANDLE usb;
48 char usbcomports[20][5];
49 int usbcomcnt;
51 int GetOSInfo (void)
53 int ver;
54 OSVERSIONINFO os;
55 char *cpu;
57 memset (&os,0,sizeof (OSVERSIONINFO));
58 os.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
59 GetVersionEx (&os);
60 ver = os.dwMajorVersion * 100 + os.dwMinorVersion * 10;
62 cpu = getenv ("PROCESSOR_ARCHITEW6432");
64 if (cpu) ver |= 1;
66 return (ver);
70 void GetComPorts (void)
72 int res,pos,cnt;
73 char buf[20000];
75 res = QueryDosDevice (NULL,buf,20000);
77 cnt = pos = 0;
79 while (pos < res) {
80 if (!memcmp (buf+pos,"COM",3)) cnt++;
81 while (buf[pos]) pos++;
82 pos++;
85 if (cnt > 20) cnt = 20;
86 usbcomcnt = cnt;
88 pos = 0;
90 while (pos < res) {
91 if (!memcmp (buf+pos,"COM",3)) {
92 strcpy (usbcomports[cnt-1],buf+pos);
93 cnt--;
95 while (buf[pos]) pos++;
96 pos++;
102 int OpenUSBPort (void)
104 enum FT_STATUS stat;
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);
112 return (0);
116 int WriteUSBString (byte pnt[],int len)
118 DWORD num;
119 enum FT_STATUS stat;
121 stat = F_Write (usb,pnt,len,&num);
122 if (stat) return (0);
124 return (num);
128 int ReadUSBString (byte pnt[],int len,word timeout)
130 DWORD num;
131 enum FT_STATUS stat;
133 F_SetTimeouts (usb,timeout,0);
135 stat = F_Read (usb,pnt,len,&num);
137 if (stat) return (0);
139 return (num);
143 int GetUSBAvailableEx (DEVICEINFO *dev)
145 DWORD num;
146 enum FT_STATUS stat;
148 stat = F_GetQueueStatus (dev->io.usbport,&num);
150 if (stat) return (0);
152 return (num);
156 int ReadUSBStringEx (DEVICEINFO *dev,byte pnt[],int len,word timeout)
158 DWORD num;
159 enum FT_STATUS stat;
161 F_SetTimeouts (dev->io.usbport,timeout,0);
163 stat = F_Read (dev->io.usbport,pnt,len,&num);
165 if (stat) return (0);
167 return (num);
171 void FlushUSB (void)
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)
184 DWORD num;
185 enum FT_STATUS stat;
187 stat = F_Write (dev->io.usbport,pnt,len,&num);
188 if (stat == 4) {
189 F_Close (dev->io.usbport);
190 if (mode_flag & NO_RECONNECT) {
191 log_print ("IRTrans Connection lost. Aborting ...\n",LOG_FATAL);
192 exit (-1);
194 while (stat) {
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);
198 else {
199 Sleep (2000);
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);
215 exit (0);
219 void cleanup_exit (void)
221 #ifndef _STANDALONE
222 int i;
223 for (i=0;i < device_cnt;i++) if (IRDevices[i].io.if_type == IF_USB) F_Close (IRDevices[i].io.usbport);
224 #endif
225 if (hdll) FreeLibrary(hdll);
229 int LoadUSBLibrary (void)
232 char msg[256];
234 atexit (cleanup_exit);
235 signal (SIGINT,break_signal);
237 hdll = LoadLibrary("Ftd2xx.dll");
238 if(hdll == NULL)
240 sprintf (msg,"Error: Can't Load ftd2xx.dll\n");
241 log_print (msg,LOG_FATAL);
242 return (-1);
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");
269 return (0);
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);
335 #endif