OTDumpPortRegistry.c

/*
    File:       OTDumpPortRegistry.c
 
    Contains:   A simple program to display OT's list of registered ports.
 
    Written by: Quinn "The Eskimo!" 
 
    Copyright:  Copyright © 1997-1999 by Apple Computer, Inc., All Rights Reserved.
 
                You may incorporate this Apple sample source code into your program(s) without
                restriction. This Apple sample source code has been provided "AS IS" and the
                responsibility for its operation is yours. You are not permitted to redistribute
                this Apple sample source code as "Apple sample source code" after having made
                changes. If you're going to re-distribute the source, we require that you make
                it clear in the source that the code was descended from Apple sample source
                code, but that you've made changes.
 
    Change History (most recent first):
                7/22/1999   Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
                
 
*/
#include <Gestalt.h>
#include <OpenTransport.h>
#include <OpenTptInternet.h>
#include <OpenTptAppleTalk.h>
#include <OpenTptLinks.h>
#include <OpenTptConfig.h>
#include <OpenTptISDN.h>
 
#include <stdio.h>
 
enum {
    kNumDeviceTypeNames = 20
};
 
static char *gDeviceTypeNames[kNumDeviceTypeNames] = {
"kOTNoDeviceType",
"kOTADEVDevice",
"kOTMDEVDevice",
"kOTLocalTalkDevice",
"kOTIRTalkDevice",
"kOTTokenRingDevice",
"kOTISDNDevice",
"kOTATMDevice",
"kOTSMDSDevice",
 
"kOTSerialDevice",
"kOTEthernetDevice",
"kOTSLIPDevice",
"kOTPPPDevice",
"kOTModemDevice",
"kOTFastEthernetDevice",
"kOTFDDIDevice",
"kOTIrDADevice",
"kOTATMSNAPDevice",
"kOTFibreChannelDevice",
"kOTFireWireDevice"
};
 
static void PrintFlag(char *flagName, UInt32 flagField, UInt32 flagMask)
{
  printf("  %s = %d\n", flagName, (flagField & flagMask) != 0);
}
 
static void DumpPortRecord(OTPortRecord portInfoRecord)
{
  OTPortRecord  childPortInfoRecord;
    UInt16              deviceType;
    UInt16              busType;
    UInt16              slotNumber;
    UInt16              slotOtherInfo;
    SInt32              childIndex;
    Str255              userFriendlyName;
 
    /* Fields in an OTPortRecord...
    
        OTPortRef       fRef;
        UInt32          fPortFlags;
        UInt32          fInfoFlags;
        UInt32          fCapabilities;
        size_t          fNumChildPorts;
        OTPortRef*  fChildPorts;
        char                fPortName[kMaxProviderNameSize];
        char                fModuleName[kMaxModuleNameSize];
        char                fSlotID[kMaxSlotIDSize];
        char                fResourceInfo[kMaxResourceInfoSize];
        char                fReserved[164];
    */
    
    // Information we can get from the port reference, fRef
    
    busType = OTGetBusTypeFromPortRef(portInfoRecord.fRef);
    deviceType = OTGetDeviceTypeFromPortRef(portInfoRecord.fRef);
    slotNumber = OTGetSlotFromPortRef(portInfoRecord.fRef, &slotOtherInfo);
    
    // Let's print some info!!!
    
    printf("¥¥¥ Dumping information for port Ò%sÓ.\n\n", portInfoRecord.fPortName);
 
    printf("fPortFlags...\n");
    PrintFlag("kOTPortIsActive", portInfoRecord.fPortFlags, kOTPortIsActive);
    PrintFlag("kOTPortIsDisabled", portInfoRecord.fPortFlags, kOTPortIsDisabled);
    PrintFlag("kOTPortIsUnavailable", portInfoRecord.fPortFlags, kOTPortIsUnavailable);
    PrintFlag("kOTPortIsOffline", portInfoRecord.fPortFlags, kOTPortIsOffline);
    printf("\n");
    
    printf("fInfoFlags = 0x%08X...\n", portInfoRecord.fInfoFlags);
    PrintFlag("kOTPortIsDLPI", portInfoRecord.fInfoFlags, kOTPortIsDLPI);
    PrintFlag("kOTPortIsTPI", portInfoRecord.fInfoFlags, kOTPortIsTPI);
    PrintFlag("kOTPortCanYield", portInfoRecord.fInfoFlags, kOTPortCanYield);
    PrintFlag("kOTPortCanArbitrate", portInfoRecord.fInfoFlags, kOTPortCanArbitrate);
    PrintFlag("kOTPortIsTransitory", portInfoRecord.fInfoFlags, kOTPortIsTransitory);
    PrintFlag("kOTPortAutoConnects", portInfoRecord.fInfoFlags, kOTPortAutoConnects);
    PrintFlag("kOTPortIsSystemRegistered", portInfoRecord.fInfoFlags, kOTPortIsSystemRegistered);
    PrintFlag("kOTPortIsPrivate", portInfoRecord.fInfoFlags, kOTPortIsPrivate);
    PrintFlag("kOTPortIsAlias", portInfoRecord.fInfoFlags, kOTPortIsAlias);
    printf("\n");
 
    printf("fCapabilities = 0x%08X\n", portInfoRecord.fCapabilities);
    switch (deviceType) {
        case kOTEthernetDevice:
        case kOTFastEthernetDevice:
            PrintFlag("kOTFramingEthernet", portInfoRecord.fCapabilities, kOTFramingEthernet);
            PrintFlag("kOTFramingEthernetIPX", portInfoRecord.fCapabilities, kOTFramingEthernetIPX);
            PrintFlag("kOTFraming8023", portInfoRecord.fCapabilities, kOTFraming8023);
            PrintFlag("kOTFraming8022", portInfoRecord.fCapabilities, kOTFraming8022);
            break;
        case kOTISDNDevice:
            PrintFlag("kOTISDNFramingTransparentSupported", portInfoRecord.fCapabilities, kOTISDNFramingTransparentSupported);
            PrintFlag("kOTISDNFramingHDLCSupported", portInfoRecord.fCapabilities, kOTISDNFramingHDLCSupported);
            PrintFlag("kOTISDNFramingV110Supported", portInfoRecord.fCapabilities, kOTISDNFramingV110Supported);
            PrintFlag("kOTISDNFramingV14ESupported", portInfoRecord.fCapabilities, kOTISDNFramingV14ESupported);
            break;
        case kOTSerialDevice:
            PrintFlag("kOTSerialFramingAsync", portInfoRecord.fCapabilities, kOTSerialFramingAsync);
            PrintFlag("kOTSerialFramingHDLC", portInfoRecord.fCapabilities, kOTSerialFramingHDLC);
            PrintFlag("kOTSerialFramingSDLC", portInfoRecord.fCapabilities, kOTSerialFramingSDLC);
            PrintFlag("kOTSerialFramingAsyncPackets", portInfoRecord.fCapabilities, kOTSerialFramingAsyncPackets);
            break;
        default:
            break;
    }
    printf("\n");
    
    if (portInfoRecord.fNumChildPorts == 0) {
        printf("No child ports.\n");
    } else {
    printf("fChildPorts...\n");
    for (childIndex = 0; childIndex < portInfoRecord.fNumChildPorts; childIndex++) {
        if (OTFindPortByRef(&childPortInfoRecord, portInfoRecord.fChildPorts[childIndex])) {
                printf("  %s\n", childPortInfoRecord.fPortName);
        } else {
            printf("That's very strange.\n");
        };
    };
    };
    printf("\n");
        
    printf("fModuleName = %s\n", portInfoRecord.fModuleName);
    printf("fSlotID = %s (only set on PCI ports).\n", portInfoRecord.fSlotID);
    printf("fResourceInfo = %s (optional config library name).\n", portInfoRecord.fResourceInfo);
 
    // extra information gleaned from port ref
    
    printf("fPortRef = %08x\n\n", portInfoRecord.fRef);
 
    printf("Bus type is ");
    switch (busType) {
        case kOTUnknownBusPort:
            printf("kOTUnknownBusPort");
            break;
        case kOTMotherboardBus:
            printf("kOTMotherboardBus");
            break;
        case kOTNuBus:
            printf("kOTNuBus");
            break;
        case kOTPCIBus:
            printf("kOTPCIBus");
            break;
        case kOTGeoPort:
            printf("kOTGeoPort");
            break;
        case kOTPCCardBus:
            printf("kOTPCCardBus");
            break;
        case kOTFireWireBus:
            printf("kOTFireWireBus");
            break;
        default:
            printf("unknown");
    };
    printf(" (%d).\n", busType);
 
    printf("Device type is ");
    if (deviceType >= 0 && deviceType < kNumDeviceTypeNames) {
        printf("%s", gDeviceTypeNames[deviceType]);
    } else {
        if (deviceType == kOTPseudoDevice) {
            printf("kOTPseudoDevice");
        } else {
            printf("unknown");
        };
    };
    printf(" (%d).\n", deviceType);
    
    printf("Slot number is %d, other is %d.\n", slotNumber, slotOtherInfo);
 
    OTGetUserPortNameFromPortRef(portInfoRecord.fRef, userFriendlyName);
    printf("OTGetUserPortNameFromPortRef = %#s\n", userFriendlyName);
 
//  Returns the location for the icon familly representing the port.
//  Returns false if the port has no icon
//
// Boolean OTGetPortIconFromPortRef(OTPortRef ref, OTResourceLocator* iconLocation);
 
    printf("\n\n");
}
 
 
static void DumpPortRecords(void)
{
  OTPortRecord  portInfoRecord;
  size_t        portIndex;
    
  portIndex = 0;
  while (OTGetIndexedPort(&portInfoRecord, portIndex)) {
    DumpPortRecord(portInfoRecord);
    portIndex += 1;
  } 
}
 
 
void main(void)
{
    OSStatus err;
    long gestaltResponse;
    
    printf("OTDumpPortRegistry -- Dumps the Open Transport port registry to stdout\n\n");
 
    if (Gestalt(gestaltOpenTptVersions, &gestaltResponse) == noErr) {
        printf("gestaltOpenTptVersions = %08x\n", gestaltResponse);
    } else {
        printf("gestaltOpenTptVersions = <undefined> (version prior to OT 1.1)\n");
    }
    (void) Gestalt(gestaltMachineType, &gestaltResponse);
    printf("gestaltMachineType = %08x\n", gestaltResponse);
    (void) Gestalt(gestaltSystemVersion, &gestaltResponse);
    printf("gestaltSystemVersion = %08x\n", gestaltResponse);
 
    printf("\nHello Cruel World!\n\n\n");
    
    err = InitOpenTransport();
    
    if (err == noErr) {
    
        DumpPortRecords();
        
        CloseOpenTransport();
    }
    
    if (err == noErr) {
        printf("Success.\n");
    } else {
        printf("Failed with error %d.\n", err);
    }
    printf("Done.  Press command-Q to Quit.\n");
}