; /* Execute me to compile! 
sc GetStats.c link nomath cpu=any ignore=73
quit
*/

/*GetStats - a demonstration on how to use HDOff's messageport
  © by Gideon Zenz, 1996 - freely distributable
  debugged and improved by Matthias Andree, 1996
  This was written to give the user a tool to have a look at HDOff's
  actual status and to have at least one example how to use the
  messyport :)

  You may use this to build up your own programms.
  This program was compiled using the great SAS/C v6.56 © by SAS Institute Inc.

  History:
  1.00 initial by G. Zenz
  1.01 improved by M. Andree
       corrected typos, implemented second display for remaining time
       float math no more needed, thus reducing executable size by 4k.
*/

/*** include all stuff */
#include <stdio.h>
#include <stdlib.h>
#include <exec/exec.h>
#include <proto/exec.h>
#include <dos/dos.h>
#include <proto/dos.h>
#include <clib/alib_protos.h>

#pragma msg 73 warn

/* define HDOff's commands*/
#define hd_GetStats     0
#define hd_SetStats     1
#define hd_Subscribe    2
#define hd_Unsubscribe  3
#define hd_StopDrive    4
#define hd_Quit         5
#define hd_ForceQuit    6
#define hd_Die          -1

/* The message-structure*/
 struct HD {
     struct  Message HD_Msg;
     WORD    HD_Cmd;
     UWORD   HD_TimeHD0;
     UWORD   HD_TimeHD1;
     UWORD   HD_TimeLeftHD0;
     UWORD   HD_TimeLeftHD1;
     BOOL    HD_StatHD0;
     BOOL    HD_StatHD1;
     ULONG   HD_PortVer;
     LONG    HD_Reserved;
     };

/*the obligatory version string :)*/
const UBYTE *version = "$VER: GetStats 1.01 (09.03.96)";

main() {
    struct MsgPort *MyPort;
    struct MsgPort *HDPort;
    struct HD      *MyMsg;

    /* Try to create a messageport without name and the priority -1*/
    if( !(MyPort=CreatePort(NULL, -1))) {
        printf("Couldn't create messageport!\n");
        exit(RETURN_FAIL);
        }

    /*Now we allocate the memory used for our messages*/
    if (MyMsg = (struct HD *) AllocMem(sizeof(struct HD), MEMF_PUBLIC | MEMF_CLEAR)) 
    {
        MyMsg->HD_Msg.mn_ReplyPort=MyPort;         /*Where HDOff should reply to...*/
        MyMsg->HD_Msg.mn_Node.ln_Type=NT_MESSAGE;
        MyMsg->HD_Msg.mn_Length=sizeof(struct HD);
        MyMsg->HD_Reserved=0L;                     /*To be sure!*/
        MyMsg->HD_Cmd=hd_GetStats;                 /*Our command*/

        /*NOTE: Forbid'ing is essential! HDOff could go away right after we
          got its port, and we would post a message to an invalid address!*/
        Forbid();
        if((HDPort=FindPort("HDOFF_PORT"))) {    /*Find HDOff's port address*/
            PutMsg(HDPort, (struct Message  *) MyMsg);
            Permit();
            }
        else {              /*We couldn't find HDOff!*/
            Permit();
            printf("HDOff isn't active!\n");
            FreeMem(MyMsg, sizeof(struct HD));
            DeletePort(MyPort);
            exit(RETURN_FAIL);
            }

        WaitPort(MyPort);      /*Wait for an answer...*/

        MyMsg = (struct HD *)GetMsg(MyPort);        /*Get the answer from the waiting stack*/

        /*Print every information we got:*/
        printf("Current status of HDOff:\nStart time HD0: %d min\nStart time HD1: %d min\nTime left HD0: %d:%02d min\nTime left HD1: %d:%02d min\nStatus HD0: %s\nStatus HD1: %s\nPort version: %d.%02d\n",
                (MyMsg->HD_TimeHD0/60), (MyMsg->HD_TimeHD1/60),
                (MyMsg->HD_TimeLeftHD0/60), (MyMsg->HD_TimeLeftHD0%60),
                (MyMsg->HD_TimeLeftHD1/60), (MyMsg->HD_TimeLeftHD1%60),
					 ((MyMsg->HD_StatHD0)?"OFF":"ON"), ((MyMsg->HD_StatHD1)?"OFF":"ON"), 
                ((MyMsg->HD_PortVer)/100), ((MyMsg->HD_PortVer)%100));

        FreeMem(MyMsg, sizeof(struct HD));
        } else
            printf("Couldn't allocate message!\n");

    Forbid();
    while(MyMsg = (struct HD *)GetMsg(MyPort));

    DeletePort(MyPort);
    Permit();
    }
