home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 8 Other / 08-Other.zip / os2upsd.zip / upsd.cpp < prev    next >
C/C++ Source or Header  |  1997-10-23  |  5KB  |  230 lines

  1. /* OS/2 UPS Daemon
  2.  * For Trust 400/650 Energy Protector
  3.  *
  4.  * Copyright (C) 1997 by M.W. Pieters Software en Advies
  5.  *                    Zoetermeer, the Netherlands
  6.  * Version 0.1
  7.  * Copying policy: GNU General Public License version 2.0 or higher
  8.  */
  9.  
  10. #include <string.h>
  11. #include <process.h>
  12. #include <setjmp.h>
  13. #include <signal.h>
  14. #include "upsd.h"
  15. #include "upsctrl.h"
  16.  
  17. #define COUNT_INIT 6000        // about 10 minutes
  18.  
  19. #define SIG_COUNT_STOP SIGUSR1
  20. #define SIG_SHUTD_STOP SIGUSR2
  21.  
  22. static int countdownRunning = 0;
  23. static int shutdownRunning = 0;
  24. static int counter = COUNT_INIT;
  25. static int doShutdown = 0;
  26.  
  27. /* Temporary until commandline parameters are handled
  28.  * 1: COM1, 2: COM2, etc.
  29.  */
  30. #define UPS_DEVICE 2
  31.  
  32. // -----------------
  33. // Shutdown routines
  34. // -----------------
  35.  
  36. void shutdown(HFILE com)
  37. {
  38.   HAB        hAnchorBlock;
  39.   ULONG        ulRetCode;
  40.   ULONG        ulSendResult;
  41.   HMQ        hMsgQWindow;
  42.   QMSG        rMsgQWin;
  43.   QMSG        rMsgDESK;
  44.   int         x;
  45.  
  46.   if ((hAnchorBlock = WinInitialize(0)) == NULLHANDLE)
  47.     return;
  48.   if ((hMsgQWindow = WinCreateMsgQueue(hAnchorBlock, 0)) == NULLHANDLE)
  49.     return;
  50.   rMsgDESK.hwnd = HWND_DESKTOP;
  51.   rMsgDESK.msg = 0;
  52.   rMsgDESK.mp1 = MPVOID;
  53.   rMsgDESK.mp2 = MPVOID;
  54.   rMsgDESK.time = 0;
  55.   rMsgDESK.reserved = 0;
  56.   ulSendResult = WinBroadcastMsg(HWND_DESKTOP,
  57.                  WM_SYSCOMMAND,
  58.                  MPFROMSHORT(SC_CLOSE),
  59.                  MPFROM2SHORT(CMDSRC_MENU,FALSE),
  60.                  (ULONG) BMSG_FRAMEONLY);
  61.  
  62.   for (int i=0; i < 60; i++)    // One minute for a clean stop of all programs
  63.   {
  64.     x = CheckState(com);
  65.     if (x != state) state = x;
  66.     if (state == 0) { counter = COUNT_INIT; return; };
  67.     if (state == 2) break;
  68.     DosSleep(1000);
  69.   }
  70.  
  71.   DosBeep(4000, 100);
  72.   DosBeep(3000, 100);
  73.   DosBeep(2000, 100);
  74.   DosBeep(1000, 100);
  75.   DosShutdown(0);
  76.   UPSPowerOff(com);
  77.   WinDestroyMsgQueue(hMsgQWindow);
  78.   WinTerminate(hAnchorBlock);
  79.   return;
  80. }
  81.  
  82. // -----------------
  83. // Threaded routines
  84. // -----------------
  85.  
  86. void countdownThread(void * pcom)
  87. {
  88.   HFILE * xcom = (HFILE *) pcom;
  89.   countdownRunning = 1;
  90.  
  91.   while(state != 0)
  92.   {
  93.     DosSleep(10);
  94.     if (--counter <= 0) break;
  95.   }
  96.   if (state != 0)
  97.   // do a shutdown...
  98.     doShutdown = 1;
  99.   while (state != 0) DosSleep(100);
  100.   countdownRunning = 0;
  101.  
  102.   _endthread();
  103. }
  104.  
  105. // --->PowerFail<---
  106.  
  107. void Powerfail(int event, HFILE com)
  108. {
  109.   ULONG reserved = 0;
  110.   switch(event)
  111.   {
  112.     case 0:     /* Power is back */
  113.       state = ST_OK;
  114.       doShutdown = 0;
  115.       counter = COUNT_INIT;
  116.       break;
  117.     case 1:    /* Power is failing, UPS active */
  118.       {
  119.     state = ST_POWERFAIL;
  120.     if (countdownRunning == 0)
  121.       _beginthread(countdownThread, 4096, (void*)&com);
  122.       }
  123.       break;
  124.     case 2:    /* Power is failing, battery low */
  125.     default:
  126.       {
  127.     state = ST_POWERLOW;
  128.     doShutdown = 2;
  129.       }
  130.       break;
  131.   }
  132. }
  133.  
  134. // --->HELP<---
  135.  
  136. void help()
  137. {
  138.   printf("Usage:\n\n");
  139.   printf("UPSD.EXE <port>\n");
  140.   printf("    <port> can be one of COM1..COM4\n\n");
  141. }
  142.  
  143. typedef struct
  144. {
  145.   WORD writeTimeout;
  146.   WORD readTimeout;
  147.   BYTE flags1;
  148.   BYTE flags2;
  149.   BYTE flags3;
  150.   BYTE errReplChr;
  151.   BYTE brkReplChr;
  152.   BYTE xon;
  153.   BYTE xoff;
  154. } DCBparam;
  155.  
  156. /* Main procedure */
  157. void main(int argc, char * argv[])
  158. {
  159.   int newState = 0;
  160.   HFILE com;
  161.   ULONG action;
  162.   DCBparam DCB;
  163.  
  164.   if (argc != 2)
  165.   {
  166.     fprintf(stderr, "Incorrect number of parameters!\n");
  167.     help();
  168.     exit(1);
  169.   }
  170.  
  171.   if ((strcmpi(argv[1], "COM1")<0) ||
  172.       (strcmpi(argv[1], "COM4")>0))
  173.   {
  174.     fprintf(stderr, "Serial port name required!\n");
  175.     help();
  176.     exit(2);
  177.   }
  178.  
  179.   unsigned long datasize = sizeof(DCB);
  180.   if (DosOpen(argv[1], &com, &action,
  181.            0L,    // logical size
  182.            0L,    // file attributes
  183.            0x01,    // action on existence
  184.            0x0012,    // openbits
  185.            0L)) exit(1);
  186.  
  187.   RaiseDTR(com);
  188.   if (!CheckDSR(com))
  189.     DosBeep(1000, 1000L);
  190.  
  191.   DosDevIOCtl(com, 0x01, 0x73, NULL, 0, NULL, &DCB, datasize, &datasize);
  192.   DCB.writeTimeout = 1000;
  193.   DCB.readTimeout = 100;
  194.   DCB.flags1 = 0x00;
  195.   DCB.flags3 = (DCB.flags3 & 0xF9) + 0x04;
  196.   SetBitRate(com, 110);
  197.  
  198.   while (CheckDSR(com))
  199.   {
  200.     newState = CheckState(com);
  201.     if (newState != state)
  202.       Powerfail(newState, com);
  203.     switch(state)
  204.     {
  205.       case 1:
  206.     DosBeep(500, 10L);
  207.     break;
  208.       case 2:
  209.     DosBeep(4000, 10L);
  210.     break;
  211.       default:
  212.     break;
  213.     }
  214.     switch (doShutdown)
  215.     {
  216.       case 1: shutdown(com); break;
  217.       case 2:
  218.     {
  219.       DosShutdown(0);
  220.       UPSPowerOff(com);
  221.     };
  222.     break;
  223.       default: break;
  224.     }
  225.     DosSleep(1000);
  226.   }
  227.   DosClose(com);
  228.   exit(-1);
  229. }
  230.