home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Networking / TPIFile / TPIFileRegister.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  5.4 KB  |  171 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        TPIFileRegister.c
  3.  
  4.     Contains:    A trivial application to register a port for the TPIFile module.
  5.  
  6.     Written by:    Quinn "The Eskimo!"
  7.  
  8.     Copyright:    © 1997 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.     You may incorporate this sample code into your applications without
  13.     restriction, though the sample code has been provided "AS IS" and the
  14.     responsibility for its operation is 100% yours.  However, what you are
  15.     not permitted to do is to redistribute the source as "DSC Sample Code"
  16.     after having made changes. If you're going to re-distribute the source,
  17.     we require that you make it clear in the source that the code was
  18.     descended from Apple Sample Code, but that you've made changes.
  19. */
  20.  
  21. /////////////////////////////////////////////////////////////////////
  22. // Pick up all the OT client info, specifically the OTRegisterPort.
  23.  
  24. #include <OpenTransport.h>
  25.  
  26. /////////////////////////////////////////////////////////////////////
  27. // Pick up the OTAllocPortMem routine.
  28.  
  29. #include <OpenTptClient.h>
  30.  
  31. /////////////////////////////////////////////////////////////////////
  32. // Pick up kOTSerialDevice.
  33.  
  34. #include <OpenTptLinks.h>
  35.  
  36. /////////////////////////////////////////////////////////////////////
  37. // OK, so it's yet another console based sample from Quinn!
  38.  
  39. #include <stdio.h>
  40.  
  41. /////////////////////////////////////////////////////////////////////
  42. // Pick up the TPIFilePortInfoRecord stuff.
  43.  
  44. #include "TPIFile.h"
  45.  
  46. /////////////////////////////////////////////////////////////////////
  47.  
  48. static OSErr RegisterTPIFilePort()
  49.     // Register a port named "TPIFile" with Open Transport.
  50.     // This allows clients to OTOpenEndpoint(OTCreateConfiguration("TPIFile"), ...)
  51.     // to open up the TPIFile module.
  52. {
  53.     OSErr                        err;
  54.     UInt16                         otherFieldOfPortRef;
  55.     OTPortRef                     candidatePortRef;
  56.     TPIFilePortInfoRecordPtr    portInfoStorage;
  57.     OTPortRecord                 portRecord;
  58.     OTPortRecord                junkPortRecord;
  59.  
  60.     // First, figure out which port ref we're going to register with.  We
  61.     //  do this by building candidatePortRef's until we get one that works.
  62.     
  63.     otherFieldOfPortRef = 0;
  64.     do {
  65.         candidatePortRef = OTCreatePortRef(kOTUnknownBusPort,
  66.                                 kOTSerialDevice,
  67.                                 0,
  68.                                 otherFieldOfPortRef);
  69.         otherFieldOfPortRef += 1;
  70.     } while ( OTFindPortByRef(&junkPortRecord, candidatePortRef) );
  71.  
  72.     // Now create the port info record for this port.  It's important that this info
  73.     //    be allocated using OTAllocPortMem, so that the memory goes into the right
  74.     //    memory pool, namely the "port pool".  This memory must be shared between the
  75.     //  port scanner and the port driver, because portInfoStorage will be passed in
  76.     //  as the contextPtr for the driver's InitStreamModule routine.
  77.     
  78.     err = noErr;
  79.     portInfoStorage = OTAllocSharedClientMem(sizeof(TPIFilePortInfoRecord));
  80.     if (portInfoStorage == nil) {
  81.         err = memFullErr;
  82.     }
  83.     
  84.     if (err == noErr) {
  85.  
  86.         // Now fill out the fields in the port info data structure.
  87.         //  Obviously if you're talking to a real driver, you'd have more
  88.         //  info to pass along here.
  89.         
  90.         portInfoStorage->magic1 = kTPIFilePortInfoMagic1;
  91.         portInfoStorage->portRef = candidatePortRef;
  92.         portInfoStorage->magic2 = kTPIFilePortInfoMagic2;
  93.         
  94.         // See "Open Tpt Module Dev. Note" for a description of all the fields
  95.         //  of OTPortRecord.  Note that I've initialised some of these fields
  96.         //  to 0, even though I've already block cleared the whole thing.  This
  97.         //  makes it easier for me to experiment with weird values in the fields
  98.         //  but you don't need it in production code.
  99.  
  100.         OTMemzero(&portRecord, sizeof(portRecord));
  101.  
  102.         portRecord.fRef                    = candidatePortRef;
  103.         portRecord.fPortFlags            = 0;
  104.         portRecord.fInfoFlags            = kOTPortIsTPI;
  105.         portRecord.fCapabilities        = 0;
  106.         portRecord.fNumChildPorts        = 0;
  107.         
  108.         // fPortName is the name of the port.
  109.         OTStrCopy(portRecord.fPortName, "TPIFile");
  110.  
  111.         // fModuleName is the name of the module (ie driver) that controls
  112.         //  the port.  OT finds the driver by looking up "OTModl$fModuleName".
  113.         OTStrCopy(portRecord.fModuleName, kTPIFilePortName);
  114.  
  115.         // fSlotID is used to describe which slot a device is in.  In
  116.         //  this case, the TPIFile port is not in a slot, so it doesn't make sense
  117.         //  to init this field.  If you're a port scanner for a real device,
  118.         //  you might want to set this up.
  119.         portRecord.fSlotID[0]            = 0;
  120.  
  121.         // fResourceInfo holds a zero terminated string that is the name
  122.         //  of the config helper library for this port.  The library
  123.         //  is responsible for providing a user-visible name and icon
  124.         //  for the port.
  125.         // There is no helper for this port!
  126.         // OTStrCopy(portRecord.fResourceInfo, "");
  127.  
  128.         err = OTRegisterPort( &portRecord, portInfoStorage );
  129.         if (err == noErr) {
  130.             portInfoStorage = nil;                    // record that we don't need to free portInfoStorage
  131.         } else {
  132.             DebugStr("\pRegisterTPIFilePort: OTRegisterPort failed.");
  133.         }
  134.     }
  135.     
  136.     if (portInfoStorage != nil) {
  137.         OTFreeSharedClientMem(portInfoStorage);
  138.     }
  139.     
  140.     return (err);
  141. }
  142.  
  143. /////////////////////////////////////////////////////////////////////
  144.  
  145. void main(void)
  146. {
  147.     OSStatus err;
  148.     OTPortRecord junkPortRecord;
  149.     
  150.     printf("Hello Cruel World!\n");
  151.     
  152.     err = InitOpenTransport();
  153.     
  154.     if (err == noErr) {
  155.     
  156.         if ( OTFindPort(&junkPortRecord, kTPIFilePortName) ) {
  157.             printf("The “TPIFile” port is already registered.\n");
  158.         } else {
  159.             err = RegisterTPIFilePort();
  160.         }
  161.  
  162.         CloseOpenTransport();
  163.     }
  164.     
  165.     if (err == noErr) {
  166.         printf("Success.\n");
  167.     } else {
  168.         printf("Failed with error %d.\n", err);
  169.     }
  170.     printf("Done.  Press command-Q to Quit.\n");
  171. }