OTFindSerialPorts.c

/*
    File:       OTFindSerialPorts.c
 
    Contains:   Sample to show how to find all the serial ports using OT.
 
    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
                
 
*/
 
/////////////////////////////////////////////////////////////////////
// The OT debugging macros in <OTDebug.h> require this variable to
// be set.
 
#ifndef qDebug
#define qDebug  1
#endif
 
/////////////////////////////////////////////////////////////////////
// Pick up all the standard OT stuff.
 
#include <OpenTransport.h>
 
/////////////////////////////////////////////////////////////////////
// Pick up device type definitions.
 
#include <OpenTptLinks.h>
 
/////////////////////////////////////////////////////////////////////
// Pick up special APIs for getting port information.
 
#include <OpenTptConfig.h>
 
/////////////////////////////////////////////////////////////////////
// Pick up the OTDebugBreak and OTAssert macros.
 
#include <OTDebug.h>
 
/////////////////////////////////////////////////////////////////////
// Standard C prototypes.
 
#include <stdio.h>
 
/////////////////////////////////////////////////////////////////////
// OTDebugStr is not defined in any OT header files, but it is
// exported by the libraries, so we define the prototype here.
 
extern pascal void OTDebugStr(const char* str);
 
/////////////////////////////////////////////////////////////////////
 
static OSStatus PrintSerialPortInfo(const OTPortRecord *portRecord)
    // Prints information about the port with the given portRecord.
{
    Str255 userVisibleName;
    
    // OTGetUserPortNameFromPortRef is a little known routine
    // from <OpenTptConfig.h> that allows you to get a user
    // visible name for an Open Transport port.  You must
    // be running PPC code if you want to call this on a PPC machine.
    
    OTGetUserPortNameFromPortRef(portRecord->fRef, userVisibleName);
    
    printf("Found a serial port with port reference $%08lx:\n", portRecord->fRef);
    printf("  User visible name is                       Ò%#sÓ.\n", userVisibleName);
    printf("  String to pass to OTCreateConfiguration is Ò%sÓ.\n",  portRecord->fPortName);
    printf("  Name of provider module is                 Ò%sÓ.\n",  portRecord->fModuleName);
    printf("\n");
    
    return kOTNoError;
}
 
static OSStatus OTFindSerialPorts(void)
    // Lists all of the serial ports on the machine using Open Transport.
{
    OSStatus err;
    Boolean portValid;
    SInt32 portIndex;
    OTPortRecord portRecord;
    UInt16 deviceType;
 
    // Start portIndex at 0 and call OTGetIndexedPort until
    // we find there are no more ports.
        
    portIndex = 0;
    err = kOTNoError;
    do {
        portValid = OTGetIndexedPort(&portRecord, portIndex);
        if (portValid) {
        
            // For each valid port, get the deviceType and, if
            // it's a serial port and not an alias, call PrintSerialPort
            // to dump out its information.  Note that you don't want
            // to include aliases to the serial ports in the list, otherwise
            // a standard machine will have 3 serial ports, "serialA", "serialB"
            // and "serial".
        
            deviceType = OTGetDeviceTypeFromPortRef(portRecord.fRef);
            if (deviceType == kOTSerialDevice && 
                        (portRecord.fInfoFlags & kOTPortIsAlias) == 0) {
                err = PrintSerialPortInfo(&portRecord);
            }
        }
        portIndex += 1;
    } while ( portValid && err == kOTNoError);
 
    return err;
}
 
/////////////////////////////////////////////////////////////////////
 
void main(void)
{
    OSStatus err;
    
    printf("OTFindSerialPorts\n");
    printf("-- Lists all the serial ports on the machine using OT\n");
    printf("\n");
    
    err = InitOpenTransport();
    
    if (err == noErr) {
    
        err = OTFindSerialPorts();
        
        CloseOpenTransport();
    }
    
    if (err == noErr) {
        printf("Success.\n");
    } else {
        printf("Failed with error %d.\n", err);
    }
    printf("Done.  Press command-Q to Quit.\n");
}