File: HARDWARE\HWIO.CPP

    1 /*
    2 File      : hwio.cpp
    3 Project   : XCTL
    4 Author    : Jan Picard <picard@informatik.hu-berlin.de> 2001-2002
    5    Institut fuer Informatik,
    6    Humboldt-Universitaet Berlin
    7 Content   : another level of indirection:
    8          providing port I/O depending on Windows version
    9          - for win16 direct inp/outp calls are allowed
   10          - for win32 we use xport, a free generic kernel mode driver
   11            downloadable from www.simtel.net, search for "xport"
   12 */
   13 
   14 #include "utils\u_utils.h"
   15 #include "porting\types.h"
   16 #include "hardware\hwguids.h"
   17 #include "hardware\hwio.h"
   18 #include "motrstrg\motorcontroller.h"
   19 #include "detecuse\detectorcontroller.h"
   20 
   21 #include "utils\CheckMemory.h"  // CHECKMEM
   22 
   23 HINSTANCE hModuleInstance;
   24 
   25 char _str_[512];
   26 // dll version numbering scheme is still not decided
   27 static char hwVersion[ ]= "V 1.00 ("__DATE__")";
   28 
   29 BOOL WINAPI DllMain(HINSTANCE hModule, DWORD dwFunction, LPVOID)
   30 {
   31         switch (dwFunction)
   32         {
   33                 case DLL_PROCESS_ATTACH:
   34                         // Initialize function array.
   35                         hModuleInstance= hModule;
   36                         break;
   37 
   38                 case DLL_PROCESS_DETACH:
   39                         //CHECKMEM("HWIO.DLL"); //hp
   40                         break;
   41         };
   42         return TRUE;
   43 }
   44 
   45 //############################################################################
   46 // Code for dll handling
   47 //############################################################################
   48 
   49 LPCSTR _HWIOCLASS WINAPI hwGetVersion( void )
   50 {
   51         return ( LPCSTR ) hwVersion;
   52 };
   53 
   54 HINSTANCE _HWIOCLASS WINAPI hwGetInstance( void )
   55 {
   56         return hModuleInstance;
   57 };
   58 
   59 /////////////////////////////////////////////////////////////////
   60 _HWIOCLASS void PrintError(DWORD dwError)
   61 {
   62         LPVOID lpMsgBuf;
   63 
   64         if (!dwError)
   65                 dwError= GetLastError();
   66 
   67         //Systemfehler und entsprechende Meldung ermitteln
   68         FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
   69                                   0, dwError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0, 0);
   70 
   71         //Fehlermeldung ausgeben
   72         MessageBox(0, (LPCTSTR)lpMsgBuf, "XControl", MBFAILURE);
   73 
   74         //Puffer freigeben
   75         if(lpMsgBuf)
   76                 HeapFree(GetProcessHeap(), 0, lpMsgBuf);
   77         lpMsgBuf= 0;
   78 }
   79 
   80 /////////////////////////////////////////////////////////////////
   81 _HWIOCLASS DWORD EnumDevices(const GUID& guid)
   82 {
   83         LPGUID pGUID= const_cast<LPGUID>(&guid);
   84         DWORD devIndex= 0;
   85         HDEVINFO info= SetupDiGetClassDevs(pGUID, 0, 0, DIGCF_PRESENT | DIGCF_INTERFACEDEVICE);
   86 
   87         if (info == INVALID_HANDLE_VALUE)
   88         {
   89                 PrintError();
   90                 return ( -1);
   91         }
   92 
   93         SP_INTERFACE_DEVICE_DATA ifData;
   94         ifData.cbSize= sizeof(ifData);
   95 
   96         while (SetupDiEnumDeviceInterfaces(info, 0, pGUID, devIndex, &ifData))
   97                 devIndex++;
   98 
   99         DWORD dwError= GetLastError();
  100         if (dwError != ERROR_NO_MORE_ITEMS)
  101         {
  102                 PrintError(dwError);
  103                 SetupDiDestroyDeviceInfoList(info);
  104                 return ( -1);
  105         }
  106 
  107         SetupDiDestroyDeviceInfoList(info);
  108         return (devIndex);
  109 }
  110 
  111 /////////////////////////////////////////////////////////////////
  112 _HWIOCLASS bool GetDeviceDriver(const GUID& guid, DWORD devIndex, TCHAR** pDriverName)
  113 {
  114         LPGUID pGUID= const_cast<LPGUID>(&guid);
  115         HDEVINFO info= SetupDiGetClassDevs(pGUID, 0, 0, DIGCF_PRESENT | DIGCF_INTERFACEDEVICE);
  116 
  117         if (info == INVALID_HANDLE_VALUE)
  118         {
  119                 PrintError();
  120                 return (false);
  121         }
  122 
  123         SP_INTERFACE_DEVICE_DATA ifData;
  124         ifData.cbSize= sizeof(ifData);
  125 
  126         if (!SetupDiEnumDeviceInterfaces(info, 0, pGUID, devIndex, &ifData))
  127         {
  128                 PrintError();
  129                 SetupDiDestroyDeviceInfoList(info);
  130                 return (false);
  131         }
  132 
  133         DWORD dwSize= 0;
  134         SetupDiGetDeviceInterfaceDetail(info, &ifData, 0, 0, &dwSize, 0);
  135 
  136         PSP_INTERFACE_DEVICE_DETAIL_DATA devDetail= (PSP_INTERFACE_DEVICE_DETAIL_DATA)HeapAlloc(GetProcessHeap(), 0, dwSize);
  137         if (!devDetail)
  138         {
  139                 MessageBox(0, "Zu wenig Hauptspeicher", "XControl::GetDeviceDriver", MBFAILURE);
  140                 SetupDiDestroyDeviceInfoList(info);
  141                 return (false);
  142         }
  143         devDetail->cbSize= sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
  144 
  145         SP_DEVINFO_DATA devData;
  146         devData.cbSize= sizeof(SP_DEVINFO_DATA);
  147 
  148         if (!SetupDiGetDeviceInterfaceDetail(info, &ifData, devDetail, dwSize, 0, &devData))
  149         {
  150                 PrintError();
  151                 if(devDetail)
  152                         HeapFree(GetProcessHeap(), 0, devDetail);
  153                 devDetail= 0;
  154                 SetupDiDestroyDeviceInfoList(info);
  155                 return (false);
  156         }
  157 
  158         dwSize= _tcsclen(devDetail->DevicePath) + 1;
  159         *pDriverName= (TCHAR*)HeapAlloc(GetProcessHeap(), 0, dwSize);
  160         if (!pDriverName)
  161         {
  162                 MessageBox(0, "Zu wenig Hauptspeicher", "XControl::GetDEviceDriver", MBFAILURE);
  163                 if(devDetail)
  164                         HeapFree(GetProcessHeap(), 0, devDetail);
  165                 devDetail= 0;
  166                 SetupDiDestroyDeviceInfoList(info);
  167                 return (false);
  168         }
  169         ZeroMemory(*pDriverName, dwSize);
  170         //_tcsnccpy(*pDriverName,devDetail->DevicePath,dwSize);
  171         _tcscpy(*pDriverName, devDetail->DevicePath);
  172 
  173         if(devDetail)
  174                 HeapFree(GetProcessHeap(), 0, devDetail);
  175         devDetail= 0;
  176         SetupDiDestroyDeviceInfoList(info);
  177 
  178         return (true);
  179 }
  180 
  181 /////////////////////////////////////////////////////////////////
  182 bool RegisterDrivers(DeviceList* Drivers, const GUID& guid, EDeviceType DeviceID)
  183 {
  184         HANDLE hFile= INVALID_HANDLE_VALUE;
  185         DWORD ReturnedLength= 0;
  186 
  187         DWORD devices= EnumDevices(guid);
  188         for (DWORD i= 0;i < devices;i++)
  189         {
  190                 TCHAR* DriverName= 0;
  191                 GetDeviceDriver(guid, i, &DriverName);
  192                 if (DriverName)
  193                 {
  194                         hFile= CreateFile(DriverName, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
  195                         if (hFile == INVALID_HANDLE_VALUE)
  196                         {
  197                                 MessageBox(0, "Treiber konnte nicht geöffnet werden.", "XCONTROL", MBFAILURE);
  198                                 return (false);
  199                         }
  200 
  201                         DWORD ioCode= IOCTL_MC_REPORT_ID;
  202                         DWORD dwHardwareID;
  203 
  204                         // Handle to device, IO Control code, Buffer to driver, Length of buffer in bytes,
  205                         // Buffer from driver, Length of buffer in bytes, Bytes placed in DataBuffer,
  206                         // NULL means wait till op. completes.
  207                         if (!DeviceIoControl(hFile, ioCode, 0, 0, &dwHardwareID, sizeof(DWORD), &ReturnedLength, NULL))
  208                         {
  209                                 MessageBox(0, "IOCTL_MC_REPORT_ID fehlgeschlagen.", "XCONTROL", MBFAILURE);
  210                                 return (false);
  211                         }
  212 
  213                         if (!CloseHandle(hFile))
  214                         {
  215                                 MessageBox(0, "CloseHandle() fehlgeschlagen.", "XCONTROL", MBFAILURE);
  216                         }
  217 
  218                         char szHardwareID[9];
  219                         memset(szHardwareID, '\0', 9);
  220                         itoa(dwHardwareID, szHardwareID, 16);
  221 
  222                         Drivers->Add(DeviceID, szHardwareID, DriverName);
  223                 }
  224         }
  225 
  226         return (true);
  227 }
  228 
  229 /////////////////////////////////////////////////////////////////
  230 DeviceListEntry::DeviceListEntry(EDeviceType _DeviceID, LPTSTR _HardwareID, LPTSTR _FileName)
  231                 : prev(0), next(0), DeviceID(_DeviceID)
  232 {
  233         memset(HardwareID, '\0', 9);
  234         memset(FileName, '\0', 257);
  235 
  236         if (strnicmp("_HardwareID", "0x", 2) == 0)      //ggf. führendes "0x" entfernen
  237                 _HardwareID += 2;
  238         strncpy(HardwareID, _HardwareID, 8);    //00000000 - FFFFFFFF
  239 
  240         strncpy(FileName, _FileName, 256);
  241 }
  242 
  243 void DeviceListEntry::SetPrevEntry(DeviceListEntry* pEntry)
  244 {
  245         prev= pEntry;
  246 }
  247 
  248 void DeviceListEntry::SetNextEntry(DeviceListEntry* pEntry)
  249 {
  250         next= pEntry;
  251 }
  252 
  253 DeviceListEntry* DeviceListEntry::GetPrevEntry()
  254 {
  255         return (prev);
  256 }
  257 
  258 DeviceListEntry* DeviceListEntry::GetNextEntry()
  259 {
  260         return (next);
  261 }
  262 
  263 int DeviceListEntry::GetDeviceID()
  264 {
  265         return (DeviceID);
  266 }
  267 
  268 LPTSTR DeviceListEntry::GetHardwareID()
  269 {
  270         return (HardwareID);
  271 }
  272 
  273 LPTSTR DeviceListEntry::GetFileName()
  274 {
  275         return (FileName);
  276 }
  277 
  278 /////////////////////////////////////////////////////////////////
  279 DeviceList::DeviceList() : first(0), last(0)
  280 {}
  281 
  282 DeviceList::~DeviceList()
  283 {
  284         Clear();
  285 }
  286 
  287 void DeviceList::Add(EDeviceType DeviceID, LPTSTR HardwareID, LPTSTR FileName)
  288 {
  289         DeviceListEntry* pEntry= new DeviceListEntry(DeviceID, HardwareID, FileName);
  290         if (!first)
  291         {
  292                 first= pEntry;
  293                 last= pEntry;
  294         }
  295         else
  296         {
  297                 pEntry->SetPrevEntry(last);
  298                 last->SetNextEntry(pEntry);
  299                 last= pEntry;
  300         }
  301 }
  302 
  303 void DeviceList::Add(DeviceListEntry* pEntry)
  304 {
  305         if (!first)
  306         {
  307                 first= pEntry;
  308                 last= pEntry;
  309         }
  310         else
  311         {
  312                 pEntry->SetPrevEntry(last);
  313                 last->SetNextEntry(pEntry);
  314                 last= pEntry;
  315         }
  316 }
  317 
  318 void DeviceList::Clear()
  319 {
  320         DeviceListEntry* temp= first;
  321         DeviceListEntry* next= 0;
  322         if (temp)
  323                 next= temp->GetNextEntry();
  324 
  325         while (temp)
  326         {
  327                 _FREEOBJ(temp);
  328                 temp= next;
  329                 if (temp)
  330                         next= temp->GetNextEntry();
  331         }
  332 }
  333 
  334 LPTSTR DeviceList::GetFileName(int DeviceID, LPTSTR HardwareID)
  335 {
  336         DeviceListEntry* temp= first;
  337 
  338         if (!temp)
  339                 return (0);
  340 
  341         do
  342         {
  343                 //DeviceID vergleichen
  344                 if (DeviceID == temp->GetDeviceID())
  345                 {
  346                         //HardwareID vergleichen
  347                         if (strnicmp(HardwareID, "0x", 2) == 0)
  348                         {
  349                                 HardwareID= HardwareID + 2;     //Vergleich ohne "0x"
  350                         }
  351 
  352                         if (stricmp(HardwareID, temp->GetHardwareID()) == 0)
  353                                 return (temp->GetFileName());
  354                 }
  355 
  356                 temp= temp->GetNextEntry();
  357         } while (temp);
  358 
  359         return (0);
  360 }
  361 
  362 /////////////////////////////////////////////////////////////////
  363 HardwareIo::HardwareIo() : hDevice(0)
  364 {
  365 }
  366 
  367 HardwareIo::HardwareIo(char* _szDeviceName) : hDevice(0)
  368 {
  369         memset(szDeviceName, '\0', 257);
  370         if (_szDeviceName)
  371         {
  372                 strncpy(szDeviceName, _szDeviceName, 256);
  373 
  374                 hDevice= CreateFile(szDeviceName, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_DELETE, 0, OPEN_EXISTING, 0, 0);
  375 
  376                 if (!hDevice || hDevice == INVALID_HANDLE_VALUE)        // Was the device opened?
  377                 {
  378                         PrintError();
  379                         //throw;
  380                 }
  381         }
  382         else
  383         {
  384                 strncpy(szDeviceName, "", 1);
  385         }
  386 }
  387 
  388 HardwareIo::~HardwareIo()
  389 {
  390         if (hDevice)
  391         {
  392                 if (CloseHandle(hDevice) == FALSE)
  393                         PrintError();
  394                 hDevice= 0;
  395         }
  396 }
  397 
  398 BOOL HardwareIo::Read(DWORD Offset, BYTE* Data)
  399 {
  400         DWORD ReturnedLength;     // Number of bytes returned
  401 
  402         if(!hDevice)
  403                 return(FALSE);
  404 
  405         BOOL IoctlResult= DeviceIoControl(
  406                                                    hDevice,              // Handle to device
  407                                                    IOCTL_MC_READ_BYTE,          // Control code for Read
  408                                                    &Offset,                     // Buffer to driver.
  409                                                    sizeof(Offset),              // Length of buffer in bytes.
  410                                                    Data,                                // Buffer from driver.
  411                                                    sizeof(BYTE),         // Length of buffer in bytes.
  412                                                    &ReturnedLength,      // Bytes placed in DataBuffer.
  413                                                    NULL                // NULL means wait till op. completes.
  414                                            );
  415 
  416         if (!IoctlResult)
  417         {
  418                 PrintError();
  419         }
  420 
  421         return (IoctlResult);
  422 }
  423 
  424 BOOL HardwareIo::Read(DWORD Offset, WORD* Data)
  425 {
  426         DWORD ReturnedLength;     // Number of bytes returned
  427 
  428         if(!hDevice)
  429                 return(FALSE);
  430 
  431         BOOL IoctlResult= DeviceIoControl(
  432                                                    hDevice,              // Handle to device
  433                                                    IOCTL_MC_READ_WORD,          // IO Control code for Read
  434                                                    &Offset,                     // Buffer to driver.
  435                                                    sizeof(Offset),              // Length of buffer in bytes.
  436                                                    Data,                        // Buffer from driver.
  437                                                    sizeof(WORD),         // Length of buffer in bytes.
  438                                                    &ReturnedLength,      // Bytes placed in DataBuffer.
  439                                                    NULL                // NULL means wait till op. completes.
  440                                            );
  441 
  442         if (!IoctlResult)
  443         {
  444                 PrintError();
  445         }
  446 
  447         return (IoctlResult);
  448 }
  449 
  450 BOOL HardwareIo::Read(DWORD Offset, DWORD* Data)
  451 {
  452         DWORD ReturnedLength;     // Number of bytes returned
  453 
  454         if(!hDevice)
  455                 return(FALSE);
  456 
  457         BOOL IoctlResult= DeviceIoControl(
  458                                                    hDevice,              // Handle to device
  459                                                    IOCTL_MC_READ_DWORD,  // IO Control code for Read
  460                                                    &Offset,                     // Buffer to driver.
  461                                                    sizeof(Offset),              // Length of buffer in bytes.
  462                                                    Data,                        // Buffer from driver.
  463                                                    sizeof(DWORD),        // Length of buffer in bytes.
  464                                                    &ReturnedLength,      // Bytes placed in DataBuffer.
  465                                                    NULL                // NULL means wait till op. completes.
  466                                            );
  467 
  468         if (!IoctlResult)
  469         {
  470                 PrintError();
  471         }
  472 
  473         return (IoctlResult);
  474 }
  475 
  476 BOOL HardwareIo::Write(DWORD Offset, BYTE Data)
  477 {
  478         DWORD ReturnedLength;     // Number of bytes returned
  479 
  480         if(!hDevice)
  481                 return(FALSE);
  482 
  483         WriteBuffer DataBuf;
  484         DataBuf.Offset= Offset;
  485         DataBuf.bData= Data;
  486 
  487         BOOL IoctlResult= DeviceIoControl(
  488                                                    hDevice,              // Handle to device
  489                                                    IOCTL_MC_WRITE_BYTE,  // IO Control code for Read
  490                                                    &DataBuf,                    // Buffer to driver.
  491                                                    sizeof(DataBuf),     // Length of buffer in bytes.
  492                                                    0,                           // Buffer from driver.
  493                                                    0,                                   // Length of buffer in bytes.
  494                                                    &ReturnedLength,      // Bytes placed in DataBuffer.
  495                                                    NULL                // NULL means wait till op. completes.
  496                                            );
  497 
  498         if (!IoctlResult)
  499         {
  500                 PrintError();
  501         }
  502 
  503         return (IoctlResult);
  504 }
  505 
  506 BOOL HardwareIo::Write(DWORD Offset, WORD Data)
  507 {
  508         DWORD ReturnedLength;     // Number of bytes returned
  509 
  510         if(!hDevice)
  511                 return(FALSE);
  512 
  513         WriteBuffer DataBuf;
  514         DataBuf.Offset= Offset;
  515         DataBuf.wData= Data;
  516 
  517         BOOL IoctlResult= DeviceIoControl(
  518                                                    hDevice,              // Handle to device
  519                                                    IOCTL_MC_WRITE_WORD,  // IO Control code for Read
  520                                                    &DataBuf,                    // Buffer to driver.
  521                                                    sizeof(DataBuf),     // Length of buffer in bytes.
  522                                                    0,                           // Buffer from driver.
  523                                                    0,                                   // Length of buffer in bytes.
  524                                                    &ReturnedLength,      // Bytes placed in DataBuffer.
  525                                                    NULL                // NULL means wait till op. completes.
  526                                            );
  527 
  528         if (!IoctlResult)
  529         {
  530                 PrintError();
  531         }
  532 
  533         return (IoctlResult);
  534 }
  535 
  536 BOOL HardwareIo::Write(DWORD Offset, DWORD Data)
  537 {
  538         DWORD ReturnedLength;     // Number of bytes returned
  539 
  540         if(!hDevice)
  541                 return(FALSE);
  542 
  543         WriteBuffer DataBuf;
  544         DataBuf.Offset= Offset;
  545         DataBuf.dwData= Data;
  546 
  547         BOOL IoctlResult= DeviceIoControl(
  548                                                    hDevice,              // Handle to device
  549                                                    IOCTL_MC_WRITE_DWORD,  // IO Control code for Read
  550                                                    &DataBuf,                    // Buffer to driver.
  551                                                    sizeof(DataBuf),     // Length of buffer in bytes.
  552                                                    0,                           // Buffer from driver.
  553                                                    0,                                   // Length of buffer in bytes.
  554                                                    &ReturnedLength,      // Bytes placed in DataBuffer.
  555                                                    NULL                // NULL means wait till op. completes.
  556                                            );
  557 
  558         if (!IoctlResult)
  559         {
  560                 PrintError();
  561         }
  562 
  563         return (IoctlResult);
  564 }
  565 
  566 /////////////////////////////////////////////////////////////////
  567 DummyIo::DummyIo(int DeviceID, LPTSTR HardwareID, bool DoMessage) : HardwareIo(0)
  568 {
  569         char DeviceName[257];
  570         memset(DeviceName, '\0', 257);
  571 
  572         switch (DeviceID)
  573         {
  574                 case C812:
  575                         strcpy(DeviceName, "C-812");
  576                         break;
  577 
  578                 case C832:
  579                         strcpy(DeviceName, "C-832");
  580                         break;
  581 
  582                 case RADICON:
  583                         strcpy(DeviceName, "Radicon");
  584                         break;
  585 
  586                 case GENERIC:
  587                         strcpy(DeviceName, "TAm9513");
  588                         break;
  589 
  590                 case BRAUN:
  591                         strcpy(DeviceName, "BraunPsd");
  592                         break;
  593 
  594                 case STOE:
  595                         strcpy(DeviceName, "StoePsd");
  596                         break;
  597 
  598                 default:
  599                         strcpy(DeviceName, "Unbekanntes Gerät");
  600         }
  601 
  602         if (DoMessage)
  603                 MessageBox(0, "Kein Treiber gefunden.", DeviceName, MBFAILURE);
  604 }
  605 
  606 /////////////////////////////////////////////////////////////////
  607 BraunPsdIo::BraunPsdIo()
  608 {
  609 }
  610 
  611 BraunPsdIo::BraunPsdIo(char* _szDeviceName) : HardwareIo(_szDeviceName)
  612 {}
  613 
  614 BraunPsdIo::~BraunPsdIo()
  615 {
  616         if (hDevice)
  617         {
  618                 if (CloseHandle(hDevice) == FALSE)
  619                         PrintError();
  620                 hDevice= 0;
  621         }
  622 }
  623 
  624 BOOL BraunPsdIo::Read(DWORD Offset, BYTE* Data)
  625 {
  626         DWORD ReturnedLength;     // Number of bytes returned
  627 
  628         BOOL IoctlResult= DeviceIoControl(
  629                                                    hDevice,              // Handle to device
  630                                                    IOCTL_MC_READ_BYTE,          // Control code for Read
  631                                                    &Offset,                     // Buffer to driver.
  632                                                    sizeof(Offset),              // Length of buffer in bytes.
  633                                                    Data,                                // Buffer from driver.
  634                                                    sizeof(BYTE),         // Length of buffer in bytes.
  635                                                    &ReturnedLength,      // Bytes placed in DataBuffer.
  636                                                    NULL                // NULL means wait till op. completes.
  637                                            );
  638 
  639         if (!IoctlResult)
  640         {
  641                 PrintError();
  642         }
  643 
  644         return (IoctlResult);
  645 }
  646 
  647 BOOL BraunPsdIo::Write(DWORD Offset, BYTE Data)
  648 {
  649         DWORD ReturnedLength;     // Number of bytes returned
  650 
  651         WriteBuffer DataBuf;
  652         DataBuf.Offset= Offset;
  653         DataBuf.bData= Data;
  654 
  655         BOOL IoctlResult= DeviceIoControl(
  656                                                    hDevice,              // Handle to device
  657                                                    IOCTL_MC_WRITE_BYTE,  // IO Control code for Read
  658                                                    &DataBuf,                    // Buffer to driver.
  659                                                    sizeof(DataBuf),     // Length of buffer in bytes.
  660                                                    0,                           // Buffer from driver.
  661                                                    0,                                   // Length of buffer in bytes.
  662                                                    &ReturnedLength,      // Bytes placed in DataBuffer.
  663                                                    NULL                // NULL means wait till op. completes.
  664                                            );
  665 
  666         if (!IoctlResult)
  667         {
  668                 PrintError();
  669         }
  670 
  671         return (IoctlResult);
  672 }
  673 
  674 BOOL BraunPsdIo::SetTimeout(unsigned long Cycles)
  675 {
  676         DWORD ReturnedLength;     // Number of bytes returned
  677 
  678         BOOL IoctlResult= DeviceIoControl(
  679                                                    hDevice,                             // Handle to device
  680                                                    IOCTL_DC_SET_WFR_CYCLES,  // IO Control code
  681                                                    &Cycles,                             // Buffer to driver.
  682                                                    sizeof(Cycles),                      // Length of buffer in bytes.
  683                                                    0,                                           // Buffer from driver.
  684                                                    0,                                           // Length of buffer in bytes.
  685                                                    &ReturnedLength,             // Bytes placed in DataBuffer.
  686                                                    NULL                                 // NULL means wait till op. completes.
  687                                            );
  688 
  689         if (!IoctlResult)
  690         {
  691                 PrintError();
  692         }
  693 
  694         return (IoctlResult);
  695 }
  696 
  697 BOOL BraunPsdIo::GetData(IrpParamsGetData *Params) // 31.06.2004 MEMCORRECT (IrpParamsGetData *Params)
  698 {
  699         DWORD ReturnedLength;     // Number of bytes returned
  700 
  701         DWORD Offset= 0;
  702         unsigned long Count= 0;
  703 
  704         BOOL IoctlResult= DeviceIoControl(
  705                                                    hDevice,                                     // Handle to device
  706                                                    IOCTL_DC_GET_DATA,                   // IO Control code
  707                                                    Params,                                              // Buffer to driver.
  708                                                    sizeof(IrpParamsGetData),    // Length of buffer in bytes.
  709                                                    &Count,                                              // Buffer from driver.
  710                                                    sizeof(Count),                               // Length of buffer in bytes.
  711                                                    &ReturnedLength,                     // Bytes placed in DataBuffer.
  712                                                    NULL                                         // NULL means wait till op. completes.
  713                                            );
  714 
  715         if (!IoctlResult)
  716         {
  717                 PrintError();
  718         }
  719 
  720         if ( !ReturnedLength )
  721         {
  722                 IoctlResult= FALSE;
  723                 MessageBox(0, "Zeitüberschreitung beim Auslesen der Messwerte.", "BraunPsd", MBFAILURE);
  724         }
  725 
  726         Params->MaxCount= Count;
  727 
  728         return (IoctlResult);
  729 }
  730 
  731 /////////////////////////////////////////////////////////////////
  732 BraunPsdDummyIo::BraunPsdDummyIo()
  733 {
  734         MessageBox(0, "Kein Treiber gefunden.", "BraunPsd", MBFAILURE);
  735 }
  736 
  737 /////////////////////////////////////////////////////////////////
  738 BraunPsdDummyIo::BraunPsdDummyIo(LPTSTR HardwareID)
  739 {
  740         MessageBox(0, "Kein Treiber gefunden.", "BraunPsd", MBFAILURE);
  741 }
  742 
  743 /////////////////////////////////////////////////////////////////
  744 /////////////////////////////////////////////////////////////////
  745 Controller::Controller(EDeviceType _DeviceID, LPTSTR _HardwareID, unsigned _MaxClients)
  746                 : Hardware(0), DeviceID(_DeviceID), MaxClients(_MaxClients), Clients(0)
  747 {
  748         memset(HardwareID, '\0', 9);
  749 
  750         if (strnicmp(_HardwareID, "0x", 2) == 0)        //ggf. führendes "0x" entfernen
  751                 _HardwareID += 2;
  752 
  753         strncpy(HardwareID, _HardwareID, 8);
  754 }
  755 
  756 int Controller::GetDeviceID()
  757 {
  758         return (DeviceID);
  759 }
  760 
  761 LPTSTR Controller::GetHardwareID()
  762 {
  763         return (HardwareID);
  764 }
  765 
  766 BOOL Controller::AddClient()
  767 {
  768         if (Clients < MaxClients)
  769         {
  770                 Clients++;
  771                 return (TRUE);
  772         }
  773         else
  774                 return (FALSE);
  775 }
  776 
  777 BOOL Controller::Read(DWORD Offset, BYTE* Data)
  778 {
  779         if (!Hardware)
  780                 return (FALSE);
  781 
  782         return (Hardware->Read(Offset, Data));
  783 }
  784 
  785 BOOL Controller::Read(DWORD Offset, WORD* Data)
  786 {
  787         if (!Hardware)
  788                 return (FALSE);
  789 
  790         return (Hardware->Read(Offset, Data));
  791 }
  792 
  793 BOOL Controller::Read(DWORD Offset, DWORD* Data)
  794 {
  795         if (!Hardware)
  796                 return (FALSE);
  797 
  798         return (Hardware->Read(Offset, Data));
  799 }
  800 
  801 BOOL Controller::Write(DWORD Offset, BYTE Data)
  802 {
  803         if (!Hardware)
  804                 return (FALSE);
  805 
  806         return (Hardware->Write(Offset, Data));
  807 }
  808 
  809 BOOL Controller::Write(DWORD Offset, WORD Data)
  810 {
  811         if (!Hardware)
  812                 return (FALSE);
  813 
  814         return (Hardware->Write(Offset, Data));
  815 }
  816 
  817 BOOL Controller::Write(DWORD Offset, DWORD Data)
  818 {
  819         if (!Hardware)
  820                 return (FALSE);
  821 
  822         return (Hardware->Write(Offset, Data));
  823 }
  824 
  825 /////////////////////////////////////////////////////////////////
  826 StandardController::StandardController(EDeviceType _DeviceID, LPTSTR _HardwareID, LPTSTR szDeviceName)
  827                 : Controller(_DeviceID, _HardwareID, 1)
  828 {
  829         Hardware= new HardwareIo(szDeviceName);
  830 }
  831 
  832 StandardController::StandardController(EDeviceType _DeviceID, LPTSTR _HardwareID, DeviceList* Devices)
  833                 : Controller(_DeviceID, _HardwareID, 1)
  834 {
  835         char* szDeviceName= Devices->GetFileName(DeviceID, HardwareID);
  836         if (!szDeviceName)
  837         {
  838                 Hardware= new DummyIo(DeviceID, HardwareID);
  839         }
  840         else
  841         {
  842                 Hardware= new HardwareIo(szDeviceName);
  843         }
  844 
  845 }
  846 
  847 StandardController::~StandardController()
  848 {
  849         _FREEOBJ(Hardware);
  850 }
  851 
  852 BOOL StandardController::Check()
  853 {
  854         return (TRUE);
  855 }
  856 
  857 /////////////////////////////////////////////////////////////////
  858 ControllerListEntry::ControllerListEntry(Controller* _asController)
  859                 : prev(0), next(0), asController(_asController)
  860 {}
  861 
  862 ControllerListEntry::~ControllerListEntry()
  863 {
  864         _FREEOBJ(asController);
  865 }
  866 
  867 void ControllerListEntry::SetPrevEntry(ControllerListEntry* pEntry)
  868 {
  869         prev= pEntry;
  870 }
  871 
  872 void ControllerListEntry::SetNextEntry(ControllerListEntry* pEntry)
  873 {
  874         next= pEntry;
  875 }
  876 
  877 ControllerListEntry* ControllerListEntry::GetPrevEntry()
  878 {
  879         return (prev);
  880 }
  881 
  882 ControllerListEntry* ControllerListEntry::GetNextEntry()
  883 {
  884         return (next);
  885 }
  886 
  887 int ControllerListEntry::GetDeviceID()
  888 {
  889         return (asController->GetDeviceID());
  890 }
  891 
  892 LPTSTR ControllerListEntry::GetHardwareID()
  893 {
  894         return (asController->GetHardwareID());
  895 }
  896 
  897 Controller* ControllerListEntry::GetController()
  898 {
  899         return (asController);
  900 }
  901 
  902 /////////////////////////////////////////////////////////////////
  903 ControllerList::ControllerList() : first(0), last(0)
  904 {}
  905 
  906 ControllerList::~ControllerList()
  907 {
  908         Clear();
  909 }
  910 
  911 void ControllerList::Add(Controller* asController)
  912 {
  913         ControllerListEntry* pEntry= new ControllerListEntry(asController);
  914         if (!first)
  915         {
  916                 first= pEntry;
  917                 last= pEntry;
  918         }
  919         else
  920         {
  921                 pEntry->SetPrevEntry(last);
  922                 last->SetNextEntry(pEntry);
  923                 last= pEntry;
  924         }
  925 }
  926 
  927 void ControllerList::Add(ControllerListEntry* pEntry)
  928 {
  929         if (!first)
  930         {
  931                 first= pEntry;
  932                 last= pEntry;
  933         }
  934         else
  935         {
  936                 pEntry->SetPrevEntry(last);
  937                 last->SetNextEntry(pEntry);
  938                 last= pEntry;
  939         }
  940 }
  941 
  942 void ControllerList::Clear()
  943 {
  944         ControllerListEntry* temp= first;
  945         ControllerListEntry* next= 0;
  946         if (temp)
  947                 next= temp->GetNextEntry();
  948 
  949         while (temp)
  950         {
  951                 _FREEOBJ(temp);
  952                 temp= next;
  953                 if (temp)
  954                         next= temp->GetNextEntry();
  955         }
  956 }
  957 
  958 Controller* ControllerList::GetController(int DeviceID, LPTSTR HardwareID)
  959 {
  960         ControllerListEntry* temp= first;
  961 
  962         if (!temp)
  963                 return (0);
  964 
  965         if (strnicmp(HardwareID, "0x", 2) == 0)
  966                 HardwareID += 2;
  967 
  968         do
  969         {
  970                 //DeviceID vergleichen
  971                 if (DeviceID == temp->GetDeviceID())
  972                 {
  973                         //HardwareID vergleichen
  974                         if (stricmp(HardwareID, temp->GetHardwareID()) == 0)
  975                                 return (temp->GetController());
  976                 }
  977 
  978                 temp= temp->GetNextEntry();
  979         } while (temp);
  980 
  981         return (0);
  982 }
  983 
  984 Controller* ControllerList::GetController(unsigned index)
  985 { //index ist Null-basiert
  986         bool bNull= false;
  987         ControllerListEntry* temp= first;
  988 
  989         if (!temp)
  990                 return (0);
  991 
  992         for (unsigned i= 0;i < index;i++)
  993         {
  994                 temp= temp->GetNextEntry();
  995                 if (!temp)
  996                 {
  997                         bNull= true;
  998                         break;
  999                 }
 1000         }
 1001 
 1002         if (bNull)
 1003                 return (0);
 1004 
 1005         return (temp->GetController());
 1006 }
 1007 
 1008 DWORD ControllerList::GetControllerIndex(int DeviceID, LPTSTR HardwareID)
 1009 {
 1010         DWORD index= 0;
 1011         ControllerListEntry* temp= first;
 1012 
 1013         if (!temp)
 1014                 return (0);
 1015 
 1016         if (strnicmp(HardwareID, "0x", 2) == 0)
 1017                 HardwareID += 2;
 1018 
 1019         do
 1020         {
 1021                 //DeviceID vergleichen
 1022                 if (DeviceID == temp->GetDeviceID())
 1023                 {
 1024                         //HardwareID vergleichen
 1025                         if (stricmp(HardwareID, temp->GetHardwareID()) == 0)
 1026                                 return (index);
 1027                 }
 1028 
 1029                 temp= temp->GetNextEntry();
 1030                 index++;
 1031         } while (temp);
 1032 
 1033         return ( -1);
 1034 }
 1035 
 1036 /////////////////////////////////////////////////////////////////
 1037 
 1038