GetPPPStatus.c

/*
    File:       GetPPPStatus.c
 
    Contains:   
 
    Written by:     
 
    Copyright:  Copyright © 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 "AEHelpers.h"
#include <ASRegistry.h>
#include <AppleEvents.h>
#include <Fonts.h>
#include <Events.h>
#include <Files.h>
#include <StandardFile.h>
#include <Types.h>
#include <Gestalt.h>
 
#include <stdio.h>
#include <String.h>
#include <stdlib.h>
#include <Events.h>
#include "OpenTptPPP.h"
#include "MoreFinderEvents.h"
#include "RAScriptingAdditions.h"
 
#define kFinderType         'FNDR'
#define kFinderCreator      'MACS'
 
//************** Global definitions *********************//
AEEventID   gPPPEventID;
    
//****************** Prototypes *************************//
Boolean NetInit(void);
pascal Boolean MyIdleFunction(EventRecord *event, long *sleep, RgnHandle mousergn);
void Initialize(void);
void ShowRAStatusResults(AppleEvent *aevent);
 
Boolean NetInit()
{
    OSStatus err;
    UInt32 gResult;
 
        // determine whether OT/PPP or ARA 3.0 is present
    err = Gestalt(gestaltOpenTptRemoteAccess, (long*) &gResult);
    
    if (err || (gResult == 0))
    {
        fprintf(stderr, "\nThis sample requires OT/PPP 1.0 or ARA 3.0");
        return false;
    }
    
        // if gestaltOpenTptRemoteAccess is present, determine whether OT/PPP or ARA 3.0 is present
 
    err = Gestalt(gestaltOpenTptRemoteAccessVersion, (long*) &gResult);
    if (err)
    {
        fprintf(stderr, "\nThere was a problem determining the OT/PPP version present %ld", err);
        return false;
    }
    
    if (gResult >= kARA30Version)
        gPPPEventID = kARAStatusRequestID;  // ARA 3.0 is present
    else if (gResult != 0)
        gPPPEventID = kPPPStatusRequestID;
    else
    {
        fprintf(stderr, "\nThere was a problem determining the OT/PPP version present");
        return false;
    }
    
    return true;
}
 
 
void Initialize(void)
{
    /* Initialize all the needed managers. */
    InitGraf(&qd.thePort);
    InitFonts();
    InitWindows();
    InitMenus();
    TEInit();
    InitDialogs(nil);
    InitCursor();
}
 
void main(void)
{
    AppleEvent          reqEvent = { typeNull, nil };
    AppleEvent          retEvent = { typeNull, nil };
    AEDesc              firstRec = { typeNull, nil };
    AEDesc              aeRec = { typeNull, nil };
    AEDesc              baudRateDesc = { typeNull, nil };
    AEDescList          aelist;
    AERecord            aerecord;
    AEIdleUPP           idleUPP;
    OSErr               err;
    
    Initialize();
 
    if (NetInit() == false)
        return;
    
        // set up the Finder as the target of the OT/PPP or ARA Apple Event call
    err = AEHMakeAppleEventSignatureTarget(kFinderType, kFinderCreator, kNetworkAccessClass,
                                            gPPPEventID, &reqEvent);
    
    if (err != noErr)
    {
        printf("Error calling AEHMakeAppleEventSignatureTarget: %d", err);
        return;
    }       
 
        // send the Apple event to the Finder for processing.
    idleUPP = NewAEIdleProc(MyIdleFunction);
    err = AESend(&reqEvent, &retEvent, kAEWaitReply+kAENeverInteract, kAENormalPriority,
                    kAEDefaultTimeout, idleUPP, nil);
                    
    if (err != noErr)
    {
        printf("Error calling AESend = %d", err);
    }
    else
    {
            // get the apple event object
        err = AEGetParamDesc(&retEvent, keyDirectObject, typeAEList, &aelist);
        if (err == noErr)
        {
                // get the apple event record 
            err = AEGetParamDesc(&aelist, typeAERecord, typeAERecord, &aerecord);
            
            if (err == noErr)
            {
                    // call the function that will parse the return info
                ShowRAStatusResults(&aerecord);
            }
            else
            {
                printf("Error calling AEGetParamDesc to get aerecord %d", err);
            }
            
            AEDisposeDesc(&aelist);
            
        }
        else
            printf("Error calling AEGetParamDesc to getaelist %d", err);
 
        
        AEDisposeDesc(&retEvent);
    }
    
    AEDisposeDesc(&reqEvent);
}
 
pascal Boolean MyIdleFunction(EventRecord *event, long *sleep, RgnHandle mousergn)
{
    #pragma unused(event,sleep,mousergn)
    Boolean result = false;
    
//  DebugStr("\pIn idle function");
    
    return result;
}       
 
void ShowRAStatusResults(AppleEvent *aerecord)
{
    AEDesc              desc = { typeNull, nil };
    UInt32              *l;
    Size                size;
    OSErr               err;
    char                str[256];
    
    err = AEGetParamDesc(aerecord, kRAStateKey, typeChar, &desc);
    if (err == noErr)
    {
        size = GetHandleSize(desc.dataHandle);
        if (size != 0)
        {
                // get the connect state into a local string
            BlockMove(*(desc.dataHandle), str, size);
            str[size] = 0;
            printf("\nConnection State - %s", str);
        }
        AEDisposeDesc(&desc);
    }
    else
    {
        printf("\nError calling AEGetParamDesc to get connection: %d", err);
        return;
    }
 
    err = AEGetParamDesc(aerecord, kRASecsConnectKey, typeInteger, &desc);
    if (err == noErr)
    {
        l = (UInt32*)*desc.dataHandle;
        printf("\nSeconds Connected - %ld", *l);
        AEDisposeDesc(&desc);
    }
    else
    {
        printf("\nError calling AEGetParamDesc to get kRASecsConnectKey: %d", err);
    }
    
    err = AEGetParamDesc(aerecord, kRASecsRemainKey, typeInteger, &desc);
    if (err == noErr)
    {
        l = (UInt32*)*desc.dataHandle;
        printf("\nSeconds Reamining - %ld", *l);
        AEDisposeDesc(&desc);
    }
    else
    {
        printf("\nError calling AEGetParamDesc to get kRASecsConnectKey: %d", err);
    }
 
    err = AEGetParamDesc(aerecord, kRAMessageKey, typeChar, &desc);
    if (err == noErr)
    {
        size = GetHandleSize(desc.dataHandle);
        if (size != 0)
        {
                // get the message text into a local string
            BlockMove(*(desc.dataHandle), str, size);
            str[size] = 0;
            printf("\nConnection Message - %s", str);
        }
        AEDisposeDesc(&desc);
    }
    else
    {
        printf("\nError calling AEGetParamDesc to get the message: %d", err);
    }
 
    if (gPPPEventID >= kARAStatusRequestID)  // to check for protocol, check if ARA 3.0 is present
    {
        err = AEGetParamDesc(aerecord, kRAProtocolKey, typeChar, &desc);
        if (err == noErr)
        {
            size = GetHandleSize(desc.dataHandle);
            if (size != 0)
            {
                    // get the protocol type text into a local string
                BlockMove(*(desc.dataHandle), str, size);
                str[size] = 0;
                printf("\nProtocol - %s", str);
            }
            AEDisposeDesc(&desc);
        }
        else
        {
            printf("\nError calling AEGetParamDesc to get protocol: %d", err);
        }
    }
    
    err = AEGetParamDesc(aerecord, kBaudRateKey, typeChar, &desc);
    if (err == noErr)
    {
        size = GetHandleSize(desc.dataHandle);
        if (size != 0)
        {
                // get the baud rate text into a local string
            BlockMove(*(desc.dataHandle), str, size);
            str[size] = 0;
            printf("\Baud Rate - %s", str);
        }
        AEDisposeDesc(&desc);
    }
    else
    {
        printf("\nError calling AEGetParamDesc to get baud rate: %d", err);
    }
    
    err = AEGetParamDesc(aerecord, kRABytesInKey, typeInteger, &desc);
    if (err == noErr)
    {
        l = (UInt32*)*desc.dataHandle;
        printf("\nNum Bytes In - %ld", *l);
        AEDisposeDesc(&desc);
    }
    else
    {
        printf("\nError calling AEGetParamDesc to get the number of bytes in: %d", err);
    }
    
    err = AEGetParamDesc(aerecord, kRABytesOutKey, typeInteger, &desc);
    if (err == noErr)
    {
        l = (UInt32*)*desc.dataHandle;
        printf("\nBytes Out - %ld", *l);
        AEDisposeDesc(&desc);
    }
    else
    {
        printf("\nError calling AEGetParamDesc to get the number of bytes out: %d", err);
    }
    
}