home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / disks / misc / hdoff / getstats.c < prev    next >
C/C++ Source or Header  |  1996-08-30  |  5KB  |  151 lines

  1. ; /* Execute me to compile!
  2. sc GetStats.c link nomath nodebug cpu=any ignore=73 stripdebug ansi strict
  3. quit
  4. */
  5.  
  6. /*GetStats - a demonstration on how to use HDOff's messageport
  7.   © by Gideon Zenz, 1996 - freely distributable
  8.   debugged and improved by Matthias Andree, 1996
  9.   This was written to give the user a tool to have a look at HDOff's
  10.   actual status and to have at least one example how to use the
  11.   messyport :)
  12.  
  13.   You may use this to build up your own programms.
  14.   This program was compiled using the great SAS/C v6.56 © by SAS Institute Inc.
  15.  
  16.   History:
  17.   1.00 initial by G. Zenz (12.1.96)
  18.   1.01 improved by M. Andree (9.3.96)
  19.        corrected typos, implemented second display for remaining time
  20.        float math no more needed, thus reducing executable size by 4k.
  21.   1.02 Implemented output for hd_HDoffVersion (21.6.96)
  22.   1.03 Implemented output for hd_QueryCtrl (29.6.96)
  23. */
  24.  
  25. /*** include all stuff */
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <exec/exec.h>
  29. #include <proto/exec.h>
  30. #include <dos/dos.h>
  31. #include <proto/dos.h>
  32. #include <clib/alib_protos.h>
  33.  
  34. #pragma msg 73 warn
  35.  
  36. /* define HDOff's commands*/
  37. #define hd_GetStats     0x0
  38. #define hd_SetStats     0x1
  39. #define hd_Subscribe    0x2
  40. #define hd_Unsubscribe  0x3
  41. #define hd_StopDrive    0x4
  42. #define hd_Quit         0x5
  43. #define hd_ForceQuit    0x6
  44. #define hd_HDoffVersion 0x7
  45. #define hd_QueryCtrl    0x8
  46. #define hd_Failure      -2
  47. #define hd_Die          -1
  48.  
  49. /* The message-structure*/
  50.  struct HD {
  51.      struct  Message HD_Msg;
  52.      WORD    HD_Cmd;
  53.      UWORD   HD_TimeHD0;
  54.      UWORD   HD_TimeHD1;
  55.      UWORD   HD_TimeLeftHD0;
  56.      UWORD   HD_TimeLeftHD1;
  57.      BOOL    HD_StatHD0;
  58.      BOOL    HD_StatHD1;
  59.      ULONG   HD_PortVer;
  60.      LONG    HD_Reserved;
  61.      };
  62.  
  63. /*the obligatory version string :)*/
  64. const UBYTE *version = "$VER: GetStats 1.03 (29.6.96)";
  65.  
  66. /*Our very own structures*/
  67. struct HD      *MyMsg;
  68. struct MsgPort *MyPort;
  69. struct MsgPort *HDPort;
  70.  
  71. /*This function does the message handling*/
  72. void DoMessage(WORD Command) {
  73.     MyMsg->HD_Msg.mn_ReplyPort=MyPort;         /*Where HDOff should reply to...*/
  74.     MyMsg->HD_Msg.mn_Node.ln_Type=NT_MESSAGE;
  75.     MyMsg->HD_Msg.mn_Length=sizeof(struct HD);
  76.     MyMsg->HD_Reserved=0L;                     /*To be sure!*/
  77.     MyMsg->HD_Cmd=Command;                     /*Our command*/
  78.  
  79.     /*NOTE: Forbid'ing is essential! HDOff could go away right after we
  80.       got its port, and we would post a message to an invalid address!*/
  81.     Forbid();
  82.     if((HDPort=FindPort("HDOFF_PORT"))) {    /*Find HDOff's port address*/
  83.         PutMsg(HDPort, (struct Message  *) MyMsg);
  84.         Permit();
  85.         }
  86.     else {              /*We couldn't find HDOff!*/
  87.         Permit();
  88.         printf("HDOff isn't active!\n");
  89.         FreeMem(MyMsg, sizeof(struct HD));
  90.         DeletePort(MyPort);
  91.         exit(RETURN_FAIL);
  92.         }
  93. }
  94.  
  95. main() {
  96.  
  97.     /* Try to create a messageport without name and the priority -1*/
  98.     if( !(MyPort=CreatePort(NULL, -1))) {
  99.     printf("Couldn't create messageport!\n");
  100.     exit(RETURN_FAIL);
  101.     }
  102.  
  103.     /*Now we allocate the memory used for our messages*/
  104.     if (MyMsg = (struct HD *) AllocMem(sizeof(struct HD), MEMF_PUBLIC | MEMF_CLEAR))
  105.     {
  106.     /*Call GetStats*/
  107.     DoMessage(hd_GetStats);
  108.     WaitPort(MyPort);      /*Wait for an answer...*/
  109.  
  110.     MyMsg = (struct HD *)GetMsg(MyPort);        /*Get the answer from the waiting stack*/
  111.  
  112.     /*Print every information we got:*/
  113.     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",
  114.         (MyMsg->HD_TimeHD0/60), (MyMsg->HD_TimeHD1/60),
  115.         (MyMsg->HD_TimeLeftHD0/60), (MyMsg->HD_TimeLeftHD0%60),
  116.         (MyMsg->HD_TimeLeftHD1/60), (MyMsg->HD_TimeLeftHD1%60),
  117.                      ((MyMsg->HD_StatHD0)?"OFF":"ON"), ((MyMsg->HD_StatHD1)?"OFF":"ON"),
  118.         ((MyMsg->HD_PortVer)/100), ((MyMsg->HD_PortVer)%100));
  119.  
  120.     /*Check if our HDOff is new enough*/
  121.     if(MyMsg->HD_PortVer>=104) {
  122.         /*Let`s see which Amiga we use*/
  123.         DoMessage(hd_QueryCtrl);
  124.         WaitPort(MyPort);      /*Wait for an answer...*/
  125.  
  126.         MyMsg = (struct HD *)GetMsg(MyPort);        /*Get the answer from the waiting stack*/
  127.  
  128.         /*And output what we got...*/
  129.         printf("Controller type: %s\n",(MyMsg->HD_StatHD0)?"A4000":"A600/A1200");
  130.  
  131.         /*Now get HDOff`s version string*/
  132.         DoMessage(hd_HDoffVersion);
  133.         WaitPort(MyPort);      /*Wait for an answer...*/
  134.  
  135.         MyMsg = (struct HD *)GetMsg(MyPort);        /*Get the answer from the waiting stack*/
  136.  
  137.         /*Output the version without leading $VER:*/
  138.         printf("Program version: %s\n", ((char *)(MyMsg->HD_PortVer))+6);
  139.         }
  140.  
  141.     FreeMem(MyMsg, sizeof(struct HD));
  142.     } else
  143.         printf("Couldn't allocate message!\n");
  144.  
  145.     Forbid();
  146.     while(MyMsg = (struct HD *)GetMsg(MyPort));
  147.  
  148.     DeletePort(MyPort);
  149.     Permit();
  150.     }
  151.