home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 January: Mac OS SDK / Dev.CD Jan 99 SDK2.toast / Development Kits / USBDDK_v1.0.1_updated / Examples / HIDModuleTest / HIDModuleTest.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-09-29  |  9.6 KB  |  303 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        CompoundClassTest.c
  3.  
  4.     Contains:    Demo/Test code for HID API inside of CompositeClassDriver
  5.  
  6.     Version:    xxx put version here xxx
  7.  
  8. */
  9.  
  10. #include <string.h>
  11. #include <CType.h>
  12. #include <StdLib.h>
  13. #include <CursorCtl.h>
  14. #include <CodeFragments.h>
  15. #include <stdio.h>
  16. #include <USB.h>
  17.  
  18.  
  19. void myKeyboardInterruptProc(UInt32 theDeviceClass, void * theData);
  20. void myMouseInterruptProc(UInt32 theDeviceClass, void * theData);
  21.  
  22. USBHIDModuleDispatchTablePtr    pTheKeyboardDispatchTable;
  23. USBHIDModuleDispatchTablePtr    pTheMouseDispatchTable;
  24. USBDriverDescriptionPtr            pTheUSBDriverDescription;
  25. HIDInterruptProcPtr                saveInterruptProcPtr;
  26.  
  27. /*
  28.     Important note for USB Interrupt procs
  29.     The following code is not VM safe.  If this were for a
  30.     real application, it's important to ensure that the interrupt proc code
  31.     is not paged out - if the process is placed into the 
  32.     background and VM is enabled.
  33.     
  34. */
  35. void myKeyboardInterruptProc(UInt32 theDeviceClass, void * theData)
  36. {
  37.     DebugStr("\pk;g");
  38.     if (saveInterruptProcPtr != nil)
  39.         (*saveInterruptProcPtr)(theDeviceClass, theData);
  40. };
  41.  
  42.  
  43. void myMouseInterruptProc(UInt32 theDeviceClass, void * theData)
  44. {
  45.     DebugStr("\pm;g");
  46.     if (saveInterruptProcPtr != nil)
  47.         (*saveInterruptProcPtr)(theDeviceClass, theData);
  48. };
  49.  
  50. main()
  51. {
  52.   
  53. OSStatus         status;
  54.  
  55. UInt16            hidDeviceClass = 0x0003;
  56. UInt16            hidDeviceSubClass = 0x0001;
  57. UInt16            keyboardDeviceProtocol = 0x0001;
  58. UInt16            mouseDeviceProtocol = 0x0002;
  59. UInt8            ledbits = 0x0000;
  60. UInt32            mousedpi;
  61. char            stage;
  62.  
  63. USBHIDDescriptorPtr    pMyHIDDescriptor;
  64.  
  65. CFragConnectionID            keyboardConnID, mouseConnID, gamepadConnID;
  66. CFragSymbolClass            symClass;
  67. USBDeviceRef                kbdDevRef, mouseDevRef, gamepadDevRef;
  68. THz                            currentZone;
  69. HIDInterruptProcPtr            saveInterruptProcPtr;
  70. UInt32                        savedRefCon;
  71.  
  72.     currentZone = GetZone ();    // save the current zone so that we can restore it later
  73.  
  74.     printf("Locate Keyboard HID Module and install an interrupt service routine\n");
  75.     
  76.     kbdDevRef = kNoDeviceRef;    // initialize this input ref so that we can find the first keyboard device
  77.                                 // if it exists.
  78.     status = USBGetNextDeviceByClass(&kbdDevRef, &keyboardConnID, hidDeviceClass, hidDeviceSubClass, keyboardDeviceProtocol);
  79.  
  80.     
  81.     if (status)
  82.     {
  83.         printf("!!Keyboard HID Module not found!!\n");
  84.         printf("  USBGetDriverConnectionID Status: %d\n", status);
  85.     }
  86.     else
  87.     {
  88.         printf("     USB Keyboard connection ID: %d\n", keyboardConnID);
  89.         SetZone (SystemZone ());        // need to set the zone to the system zone to call FindSymbol
  90.                                         // for a symbol of a USB driver that is loaded into the
  91.                                         // system zone.
  92.         status = FindSymbol (keyboardConnID, "\pTheHIDModuleDispatchTable", (Ptr *)&pTheKeyboardDispatchTable, &symClass);
  93.         SetZone (currentZone);            // restore the zone
  94.         if (status)
  95.         {
  96.             printf("!!HID Dispatch Table not found!!\n");
  97.             printf("              FindSymbol Status: %d\n", status);
  98.         }
  99.         else
  100.         {
  101.             printf("           HID Dispatch Table @: %0p\n\n", pTheKeyboardDispatchTable);
  102.             
  103.             SetZone (SystemZone ());    // need to set the zone to the system zone to call FindSymbol
  104.                                         // for a symbol of a USB driver that is loaded into the
  105.                                         // system zone.
  106.             status = FindSymbol (keyboardConnID, "\pTheUSBDriverDescription", (Ptr *)&pTheUSBDriverDescription, &symClass);
  107.             SetZone (currentZone);        // restore the zone
  108.             if (!status)
  109.             {
  110.                 switch(pTheUSBDriverDescription->usbDriverType.usbDriverVersion.stage)
  111.                 {
  112.                     case developStage:
  113.                         stage = 'd';
  114.                         break;
  115.                         
  116.                     case alphaStage:
  117.                         stage = 'a';
  118.                         break;
  119.                         
  120.                     case betaStage:
  121.                         stage = 'b';
  122.                         break;
  123.                         
  124.                     case finalStage:
  125.                         stage = 'f';
  126.                         break;
  127.                 };
  128.                 printf("*** Name:    %s\n", pTheUSBDriverDescription->usbDriverType.nameInfoStr);
  129.                 printf("*** Version: %d.%x%c%x\n",  pTheUSBDriverDescription->usbDriverType.usbDriverVersion.majorRev,
  130.                                                     pTheUSBDriverDescription->usbDriverType.usbDriverVersion.minorAndBugRev,
  131.                                                     stage,
  132.                                                     pTheUSBDriverDescription->usbDriverType.usbDriverVersion.nonRelRev);
  133.                 fflush(stdout);
  134.             }
  135.         
  136.                 // before we install an interrupt handler, we need to obtain the current handler setting
  137.                 // so that we can restore it later                
  138.             status = (*(pTheKeyboardDispatchTable->pUSBHIDGetDeviceInfo))(kHIDGetInterruptHandler, &saveInterruptProcPtr);
  139.             
  140.             if (status)
  141.                 printf("*** Keyboard Interrupt Handler not returned\n");
  142.             else
  143.             {
  144.                     // get the saved interrupt refcon
  145.                 status = (*(pTheKeyboardDispatchTable->pUSBHIDGetDeviceInfo))(kHIDGetInterruptRefcon, &savedRefCon);
  146.                 if (status)
  147.                     printf("*** Keyboard Interrupt refcon not returned\n");
  148.             }
  149.             
  150.             if (!status)
  151.             {
  152.                     // now install our specified interrupt proc
  153.                 status = (*(pTheKeyboardDispatchTable->pUSBHIDInstallInterrupt))(myKeyboardInterruptProc, 0);
  154.                 if (status)
  155.                     printf("*** Keyboard Interrupt could not be set\n");
  156.             }
  157.             
  158.             if (!status)
  159.             {
  160.                 printf("*** Keyboard Interrupt Handler successfully installed\n");
  161.                 printf("*** Setting LED bit #1\n", (int)ledbits);
  162.                 fflush(stdout);
  163.                 ledbits = 0x01;
  164.                 (*pTheKeyboardDispatchTable->pUSBHIDControlDevice)  (  (UInt32)1, (void *)&ledbits  );
  165.                     
  166.                 printf("*** Setting LED bit #2\n", (int)ledbits);
  167.                 fflush(stdout);
  168.                 ledbits = 0x02;
  169.                 (*pTheKeyboardDispatchTable->pUSBHIDControlDevice)  (  (UInt32)1, (void *)&ledbits  );
  170.  
  171.                 printf("*** Setting LED bit #3\n", (int)ledbits);
  172.                 fflush(stdout);
  173.                 ledbits = 0x04;
  174.                 (*pTheKeyboardDispatchTable->pUSBHIDControlDevice)  (  (UInt32)1, (void *)&ledbits  );
  175.  
  176.                 printf("*** Clearing LED bits\n", (int)ledbits);
  177.                 fflush(stdout);
  178.                 ledbits = 0x00;
  179.                 (*pTheKeyboardDispatchTable->pUSBHIDControlDevice)  (  (UInt32)1, (void *)&ledbits  );
  180.  
  181.                 printf("*** Enable keyboard raw report mode\n");
  182.                 ledbits = 0x03;
  183.                 (*pTheKeyboardDispatchTable->pUSBHIDControlDevice)  (  (UInt32)100, (void *)0);
  184.                 fflush(stdout);
  185.             
  186.                 (*(pTheKeyboardDispatchTable->pUSBHIDInstallInterrupt))(saveInterruptProcPtr, savedRefCon);
  187.                 printf("*** Keyboard Interrupt Handler successfully restored\n");
  188.             }
  189.  
  190.         }
  191.     }
  192.     fflush(stdout);
  193.  
  194.     printf("\n\n\n");
  195.  
  196.     printf("Locate Mouse HID Module and install an interrupt service routine\n");
  197.  
  198.     mouseDevRef = kNoDeviceRef;    // initialize this input ref so that we can find the first usb mouse
  199.                                 // if it exists.
  200.     status = USBGetNextDeviceByClass (&mouseDevRef, &mouseConnID, hidDeviceClass, hidDeviceSubClass, mouseDeviceProtocol);
  201.     if (status)
  202.     {
  203.         printf("!!Mouse HID Module not found!!\n");
  204.         printf("  USBGetDriverConnectionID Status: %d\n", status);
  205.     }
  206.     else
  207.     {
  208.         printf("        USB Mouse connection ID: %d\n", mouseConnID);
  209.         SetZone (SystemZone ());        // need to set the zone to the system zone to call FindSymbol
  210.                                         // for a symbol of a USB driver that is loaded into the
  211.                                         // system zone.
  212.         status = FindSymbol (mouseConnID, "\pTheHIDModuleDispatchTable", (Ptr *)&pTheMouseDispatchTable, &symClass);
  213.         SetZone (currentZone);            // restore the zone
  214.         if (status)
  215.         {
  216.             printf("!!HID Dispatch Table not found!!\n");
  217.             printf("              FindSymbol Status: %d\n", status);
  218.         }
  219.         else
  220.         {
  221.             printf("           HID Dispatch Table @: %0p\n\n", pTheMouseDispatchTable);
  222.             
  223.             SetZone (SystemZone ());    // need to set the zone to the system zone to call FindSymbol
  224.                                         // for a symbol of a USB driver that is loaded into the
  225.                                         // system zone.
  226.             status = FindSymbol (mouseConnID, "\pTheUSBDriverDescription", (Ptr *)&pTheUSBDriverDescription, &symClass);
  227.             SetZone (currentZone);        // restore the zone
  228.             if (!status)
  229.             {
  230.                 switch(pTheUSBDriverDescription->usbDriverType.usbDriverVersion.stage)
  231.                 {
  232.                     case developStage:
  233.                         stage = 'd';
  234.                         break;
  235.                         
  236.                     case alphaStage:
  237.                         stage = 'a';
  238.                         break;
  239.                         
  240.                     case betaStage:
  241.                         stage = 'b';
  242.                         break;
  243.                         
  244.                     case finalStage:
  245.                         stage = 'f';
  246.                         break;
  247.                 };
  248.                 printf("*** Name:    %s\n", pTheUSBDriverDescription->usbDriverType.nameInfoStr);
  249.                 printf("*** Version: %d.%x%c%x\n",  pTheUSBDriverDescription->usbDriverType.usbDriverVersion.majorRev,
  250.                                                     pTheUSBDriverDescription->usbDriverType.usbDriverVersion.minorAndBugRev,
  251.                                                     stage,
  252.                                                     pTheUSBDriverDescription->usbDriverType.usbDriverVersion.nonRelRev);
  253.                 fflush(stdout);
  254.             }
  255.         
  256.             printf("*** Get DPI from the mouse...\n");
  257.             status = (*pTheMouseDispatchTable->pUSBHIDGetDeviceInfo)  ((UInt32)kHIDGetDeviceUnitsPerInch, (void *)&mousedpi  );
  258.             if (status == noErr)
  259.             {
  260.                 printf("*** DPI ('fixed') = %ld (0x%lx)\n",mousedpi,mousedpi);
  261.                 printf("*** DPI ('int')   = %ld (0x%lx)\n",(mousedpi >> 16), (mousedpi >> 16));
  262.             }
  263.             else
  264.                 printf("*** pUSBHIDGetDeviceInfo failed to mouse\n");
  265.                 
  266.             fflush(stdout);
  267.  
  268.                 // get the current interrupt handler
  269.             status = (*(pTheMouseDispatchTable->pUSBHIDGetDeviceInfo))(kHIDGetInterruptHandler, &saveInterruptProcPtr);
  270.  
  271.             if (status)
  272.                 printf("*** mouse Interrupt Handler not returned\n");
  273.             else
  274.             {
  275.                     // get the saved interrupt refcon
  276.                 status = (*(pTheMouseDispatchTable->pUSBHIDGetDeviceInfo))(kHIDGetInterruptRefcon, &savedRefCon);
  277.                 if (status)
  278.                     printf("*** mouse Interrupt refcon not returned\n");
  279.             }
  280.             
  281.             if (!status)
  282.             {
  283.                 printf("*** Installing mouse interrupt handler\n");
  284.                 fflush(stdout);
  285.                 status = (*(pTheMouseDispatchTable->pUSBHIDInstallInterrupt))(myMouseInterruptProc, 0);
  286.                 if (!status)
  287.                     printf("*** mouse Interrupt could not be set\n");
  288.             }
  289.             
  290.             if (!status)
  291.             {
  292.                 printf("*** Mouse Interrupt Handler successfully installed\n");
  293.                 fflush(stdout);
  294.  
  295.                 (*(pTheMouseDispatchTable->pUSBHIDInstallInterrupt))(saveInterruptProcPtr, savedRefCon);
  296.                 printf("*** mouse Interrupt Handler successfully restored\n");
  297.             }
  298.         }
  299.     }
  300.         
  301.     fflush(stdout);
  302.     
  303. }