1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
/********************************************************************
File: ups.c
Purpose: Read UPS status.
Copyright 1996, Bob Hauck
Copyright 1998, Michael Robinton
This program is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public
License as published by the Free Software Foundation;
either version 2 of the License, or (at your option) any
later version.
This program is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public
License along with this program; if not, write to the Free
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
USA.
Language: GCC 2.7.0
Author: Bob Hauck
Modified: Michael Robinton
Modified: Russ Magee
Revision 1.3 2000/06/09 14:54:00 Michael Robinton/Russ Magee
Integrate Russ Magee's pin remapping contribution.
RM..
Removed pin assignment #defines -- now read from upsd.conf
Changed all code to use logical UPS lines, supported by mapping.c,h
Revision 1.2 1998/05/22 10:39:00 miker
Add logging status string VERSION.
UPS_Setup UPS kill needs to be in the OFF
state after an attempt is made to kill the power.
This is a pulsed signal.
Revision 1.1 1996/11/23 16:45:12 bobh
Initial revision
**********************************************************************/
#include <sys/ioctl.h>
#include <errno.h>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include "common.h"
#include "mapping.h"
#include "ups.h"
/* Status strings for logging. The VERSIONs don't have to match
* since the VERSION message is sent but never decoded by NET_CHECK */
char *StatusString [] =
{
"OK",
"ON BATTERY",
"LOW BATTERY",
"UPS_ERROR",
VERSION
};
/*-------------------------------------------------------------------*
UPS_Open
Open the UPS monitor device (serial port).
Parameters: DevName - Name of the device to open.
Returns: File descriptor that was opened.
*-------------------------------------------------------------------*/
int UPS_Open (char *DevName)
{
int fd;
if ((fd = open (DevName, O_RDWR | O_NDELAY)) < 0)
{
LogError ("opendev", (char *) sys_errlist [errno]);
}
return fd;
}
/*-------------------------------------------------------------------*
UPS_Setup
Set up the port for monitoring. Optionally kill the UPS
power (only works if the UPS is on battery).
Parameters: fd - File descriptor of monitor device.
kill - TRUE to shut down UPS power.
Returns: Nothing.
*-------------------------------------------------------------------*/
void UPS_Setup (int fd, int kill)
{
/* Most UPS units need power from the PC for status lines so we set
it here */
UPS_Assert(fd, L_UPSPWR);
if (!kill)
{
UPS_Deassert(fd, L_KILL);
sleep (1);
}
if (kill)
{
LogError ("kill", "Attempting to kill the power!\n");
UPS_Deassert(fd, L_KILL);
sleep (5);
UPS_Assert(fd, L_KILL);
sleep (5);
UPS_Deassert(fd, L_KILL);
sleep (1);
/* Hmmm..... If you have a power outage, you won't make it!
*/
exit (0);
}
}
/*-------------------------------------------------------------------*
UPS_Check
Check the status of the UPS (Ok, On Battery, or Low Battery).
Parameters: fd - File descriptor of monitor device.
Returns: S_* status code.
*-------------------------------------------------------------------*/
int UPS_Check (int fd)
{
int flags;
int status;
flags = UPS_Read(fd);
/* printf("LogStatus: %04x\n", flags); fflush(stdout);*/
status = (flags & (B_ONBAT | B_LOBAT));
if ((flags & B_ERR) == 0)
{
return S_ERROR; /* bad UPS cable? */
}
switch (status)
{
/* On battery, but battery ok
*/
case B_ONBAT:
status = S_ONBAT;
break;
/* On battery, and battery is low
*/
case (B_LOBAT | B_ONBAT):
status = S_LOBAT;
break;
/* Everything is a-ok
*/
default:
status = S_OK;
}
return status;
}
|