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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
|
/*
* $Id: hid-ups.c,v 1.12 2005/08/31 17:07:31 adk0212 Exp $
*
* Copyright (c) 2001 Vojtech Pavlik <vojtech@ucw.cz>
* Copyright (c) 2001 Paul Stewart <hiddev@wetlogic.net>
*
* Tweaked by Kern Sibbald <kern@sibbald.com> to learn
* about USB UPSes.
*
* HID UPS device test program
*/
/*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Should you need to contact me, the author, you can do so either by
* e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
* Vojtech Pavlik, Simunkova 1492, Prague 8, 182 00 Czech Republic
*/
#define DEBUG 1 /* if set prints full reports */
#define TESTING 1 /* if set disables actual operation */
#define HID_MAX_USAGES 1024
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <sys/signal.h>
#include <asm/types.h>
#include <linux/hiddev.h>
#include <errno.h>
#define PWRSTAT "/etc/powerstatus"
#define DEBOUNCE_TIMEOUT 15 /* increase this if you get false alerts */
#define UPS_USAGE 0x840000
#define UPS_SERIAL 0x8400fe
#define BAT_CHEMISTRY 0x850089
#define UPS_CAPACITY_MODE 0x85002c
#define UPS_SHUTDOWN_IMMINENT 0x840069
#define UPS_BATTERY_VOLTAGE 0x840030
#define UPS_BELOW_RCL 0x840042
#define UPS_CHARING 0x840044
#define UPS_DISCHARGING 0x850045
#define UPS_REMAINING_CAPACITY 0x850066
#define UPS_RUNTIME_TO_EMPTY 0x850068
#define UPS_AC_PRESENT 0x8500d0
#define STATE_NORMAL 0 /* unit powered */
#define STATE_DEBOUNCE 1 /* power failure */
#define STATE_BATTERY 2 /* power failure confirmed */
/* USB Vendor ID's */
#define Vendor_APC 0x51D
#define Vendor_MGE 0x463
#ifdef DEBUG
/*
* The type field indicates the type or units to be
* applied to the value.
*/
#define T_NONE 0 /* No units */
#define T_INDEX 1 /* String index */
#define T_CAPACITY 2 /* Capacity (usually %) */
#define T_BITS 3 /* bit field */
#define T_UNITS 4 /* use units/exponent field */
#define T_DATE 5 /* date */
#define T_APCDATE 6 /* APC date format */
struct s_ups_info {
unsigned usage;
int type;
char *label;
} ups_info[] = {
/* MGE & APC */
{ 0x000000, T_NONE, "---" },
/* Page 0x84 is the Power Device Page */
{ 0x840000, T_NONE, "UPS-Power" },
{ 0x840001, T_INDEX, "iName" },
{ 0x840004, T_NONE, "UPS" },
{ 0x840012, T_NONE, "Battery" },
{ 0x840016, T_NONE, "PowerConverter" },
{ 0x840018, T_NONE, "OutletSystem" },
{ 0x840017, T_NONE, "PowerConverterID" },
{ 0x840019, T_NONE, "OutletSystemID" },
{ 0x84001a, T_NONE, "Input" },
{ 0x84001c, T_NONE, "Output" },
{ 0x84001e, T_NONE, "Flow" },
{ 0x84001d, T_NONE, "OutputID" },
{ 0x84001f, T_NONE, "FlowID" },
{ 0x840020, T_NONE, "Outlet" },
{ 0x840021, T_NONE, "OutletID" },
{ 0x840024, T_NONE, "PowerSummary" },
{ 0x840025, T_NONE, "PowerSummaryID" },
{ 0x840030, T_UNITS, "Voltage" },
{ 0x840031, T_UNITS, "Current" },
{ 0x840032, T_UNITS, "Frequency" },
{ 0x840033, T_UNITS, "ApparentPower" },
{ 0x840034, T_UNITS, "ActivePower" },
{ 0x840035, T_UNITS, "PercentLoad" },
{ 0x840036, T_UNITS, "Temperature" },
{ 0x840037, T_UNITS, "Humidity" },
{ 0x840040, T_UNITS, "ConfigVoltage" },
{ 0x840042, T_UNITS, "ConfigFrequency" },
{ 0x840043, T_UNITS, "ConfigApparentPower" },
{ 0x840044, T_UNITS, "ConfigActivePower" },
{ 0x840053, T_UNITS, "LowVoltageTransfer" },
{ 0x840054, T_UNITS, "HighVoltageTransfer" },
{ 0x840055, T_UNITS, "DelayBeforeReboot" },
{ 0x840056, T_UNITS, "DelayBeforeStartup" },
{ 0x840057, T_NONE, "DelayBeforeShutdown" },
{ 0x840058, T_NONE, "Test" },
{ 0x84005a, T_NONE, "AudibleAlarmControl" },
{ 0x840061, T_NONE, "Good" },
{ 0x840062, T_NONE, "InternalFailure" },
{ 0x840065, T_NONE, "Overload" },
{ 0x840068, T_NONE, "ShutdownRequested" },
{ 0x840069, T_NONE, "ShutdownImminent" },
{ 0x84006b, T_NONE, "Switch On/Off" },
{ 0x84006c, T_NONE, "Switchable" },
{ 0x84006e, T_NONE, "Boost" },
{ 0x84006f, T_NONE, "Trim" },
{ 0x840073, T_NONE, "CommunicationLost" },
{ 0x8400fd, T_INDEX, "iManufacturer" },
{ 0x8400fe, T_INDEX, "iProduct" },
{ 0x8400ff, T_INDEX, "iSerialNumber" },
/* Page 0x85 is the Battery System Page */
{ 0x850000, T_NONE, "UPS-Batt" },
{ 0x850029, T_CAPACITY, "RemainingCapacityLimit" },
{ 0x85002a, T_UNITS, "RemainingTimeLimit" },
{ 0x85002c, T_CAPACITY, "CapacityMode" },
{ 0x850042, T_NONE, "BelowRemainingCapacityLimit" },
{ 0x850043, T_NONE, "RemainingTimeLimitExpired" },
{ 0x850044, T_NONE, "Charging" },
{ 0x850045, T_NONE, "Discharging" },
{ 0x85004b, T_NONE, "NeedReplacement" },
{ 0x850058, T_NONE, "BUPHibernate" }, /* APC proprietary */
{ 0x850066, T_CAPACITY, "RemainingCapacity" },
{ 0x850067, T_CAPACITY, "FullChargeCapacity" },
{ 0x850068, T_UNITS, "RunTimeToEmpty" },
{ 0x85006b, T_NONE, "CycleCount" },
{ 0x850080, T_NONE, "BattPackLevel" },
{ 0x850083, T_CAPACITY, "DesignCapacity" },
{ 0x850085, T_DATE, "ManufactureDate" },
{ 0x850088, T_INDEX, "iDeviceName" },
{ 0x850089, T_INDEX, "iDeviceChemistry" },
{ 0x85008b, T_NONE, "Rechargeable" },
{ 0x85008c, T_CAPACITY, "WarningCapacityLimit" },
{ 0x85008d, T_CAPACITY, "CapacityGranularity1" },
{ 0x85008e, T_CAPACITY, "CapacityGranularity2" },
{ 0x85008f, T_INDEX, "iOEMInformation" },
{ 0x8500d0, T_NONE, "ACPresent" },
{ 0x8500d1, T_NONE, "BatteryPresent" },
{ 0x8500db, T_NONE, "VoltageNotRegulated" },
/*
* Page 0x86 is reserved for Power Devices, but not defined in the HID
* standard. APC has defined a few usages on this page for themselves.
*/
{ 0x860010, T_NONE, "BUPSelfTest" }, /* APC proprietary */
{ 0x860012, T_NONE, "BUPBattCapBeforeStartup" }, /* APC proprietary */
{ 0x860076, T_NONE, "BUPDelayBeforeStartup" }, /* APC proprietary */
/* Pages 0xFF00 to 0xFFFF are vendor specific */
{ 0xFF860005, T_NONE, "APCGeneralCollection" },
{ 0xFF860013, T_NONE, "APC860013_SetMinReturn?" },
{ 0xFF860016, T_APCDATE,"APCBattReplacementDate" },
{ 0xFF860019, T_UNITS, "APCBattCapBeforeStartup" },
{ 0xFF860023, T_NONE, "APC860023_??????" },
{ 0xFF860024, T_NONE, "APC860024_??????" },
{ 0xFF860025, T_NONE, "APC860025_??????" },
{ 0xFF860026, T_NONE, "APC860026_??????" },
{ 0xFF860029, T_NONE, "APC860029_??????" },
{ 0xFF86002A, T_NONE, "APC86002A_??????" },
{ 0xFF860042, T_NONE, "APC_UPS_FirmwareRevision" },
{ 0xFF860052, T_NONE, "APCLineFailCause" },
{ 0xFF860060, T_BITS, "APCStatusFlag" },
{ 0xFF860061, T_NONE, "APC860061_??????" },
{ 0xFF860062, T_NONE, "APC860062_SetHiTransV?" },
{ 0xFF860064, T_NONE, "APC860064_SetLoTransV?" },
{ 0xFF860072, T_NONE, "APCPanelTest" },
{ 0xFF860074, T_NONE, "APC860074_SetSens?" },
{ 0xFF860076, T_UNITS, "APCShutdownAfterDelay" },
{ 0xFF860077, T_NONE, "APC860077_SetWakeUpDelay?" },
{ 0xFF860079, T_NONE, "APC_USB_FirmwareRevision" },
{ 0xFF86007C, T_NONE, "APCForceShutdown" },
{ 0xFF86007D, T_UNITS, "APCDelayBeforeShutdown" },
{ 0xFF86007E, T_UNITS, "APCDelayBeforeStartup" },
};
#define UPS_INFO_SZ (sizeof(ups_info)/sizeof(ups_info[0]))
char *reports[] = { "Unknown", "Input", "Output", "Feature" };
static int CapacityMode = 2; /* default = % */
char unknown[24];
#define MADDR "stewart@wetlogic.net"
void log_status(char *msg) {
#ifndef TESTING
char buf[256];
printf("[Log message \"%s\"]\n", msg);
sprintf(buf, "/bin/echo %s | /bin/mail -s \"UPS System\" %s", msg, MADDR);
system(buf);
#else
printf("[Log message \"%s\"]\n", msg);
#endif
}
static inline char* info(unsigned int detail) {
int i;
for (i = 0; i < UPS_INFO_SZ; i++) {
if (ups_info[i].usage == detail) {
return ups_info[i].label;
}
}
sprintf(unknown, "[%06x]", detail);
return unknown;
}
static struct s_ups_info *info_entry(unsigned int detail) {
int i;
static struct s_ups_info info = {0, T_NONE, unknown};
for (i = 0; i < UPS_INFO_SZ; i++) {
if (ups_info[i].usage == detail) {
return &ups_info[i];
}
}
sprintf(unknown, "[%06x]", detail);
return &info;
}
#else /* DEBUG */
#define log_status(s)
#endif /* DEBUG */
/* Tell init the power has either gone or is back. */
void powerfail(int state) {
#ifndef TESTING
int fd;
/* Create an info file needed by init to shutdown/cancel shutdown */
unlink(PWRSTAT);
if ((fd = open(PWRSTAT, O_CREAT|O_WRONLY, 0644)) >= 0) {
if (state > 0)
write(fd, "FAIL\n", 5);
else if (state < 0)
write(fd, "LOW\n", 4);
else
write(fd, "OK\n", 3);
close(fd);
}
kill(1, SIGPWR);
#else
printf("We are in powerfail() with state=%d ", state);
if (state > 0)
printf("POWER FAILURE\n");
else if (state < 0)
printf("BATTERY LOW\n");
else
printf("OK\n");
#endif
}
static inline int find_application(int fd, unsigned usage) {
int i = 0, ret;
while ((ret = ioctl(fd, HIDIOCAPPLICATION, i)) > 0 &&
(ret & 0xffff0000) != (usage & 0xffff0000)) i++;
return ((ret & 0xffff000) == (usage & 0xffff0000));
}
/*
* Get a string from the device given the string's index
*/
static char *get_string(int fd, int sindex) {
static struct hiddev_string_descriptor sdesc;
static char buf[200];
if (sindex == 0) {
return "";
}
sdesc.index = sindex;
if (ioctl(fd, HIDIOCGSTRING, &sdesc) < 0) {
sprintf(buf, "String index %d returned ERR=%s\n", sindex,
strerror(errno));
return buf;
}
return sdesc.value;
}
/*
* Give units code and exponent, returns string
* describing the units used. (doesn't work for percentages).
*/
static char *get_units(unsigned unit, unsigned exponent) {
static char buf[200];
if (exponent > 7) {
exponent = exponent - 16;
}
switch (unit) {
case 1: /* special kludge for CapacityMode */
switch (exponent) {
case 0:
return "maH";
case 1:
return "mwH";
case 2:
return "percent";
case 3:
return "boolean";
default:
return "";
}
case 0x00F0D121:
if (exponent == 7) {
return "Volts";
} else if (exponent == 5) {
return "CentiVolts";
} else if (exponent == 6) {
return "DeciVolts";
} else {
sprintf(buf, "Volts with %d exponent", exponent);
return buf;
}
case 0x00100001:
if (exponent == -2) {
return "CentiAmps";
} else if (exponent == 0) {
return "Amps";
} else {
sprintf(buf, "Amps with %d exponent", exponent);
return buf;
}
case 0xF001:
if (exponent == 0) {
return "Hertz";
} else if (exponent == -2) {
return "CentiHertz";
} else {
sprintf(buf, "Hertz with %d exponent", exponent);
return buf;
}
case 0x1001:
if (exponent == 0) {
return "Seconds";
} else {
sprintf(buf, "Seconds with %d exponent", exponent);
return buf;
}
case 0xD121:
return "Watts";
case 0x010001:
if (exponent == 0) {
return "Degrees K";
} else {
sprintf(buf, "Degrees K with %d exponent", exponent);
return buf;
}
case 0x0101001:
return "AmpSecs";
case 0:
return "";
default:
sprintf(buf, "0x%x", unit);
return buf;
}
}
static char evdev[50];
static int vendor = 0;
int main (int argc, char **argv) {
int fd = -1, rd, i, j, RemainingCapacity;
struct hiddev_event ev[64];
struct hiddev_devinfo dinfo;
char name[256] = "Unknown";
int state = 0;
fd_set fdset;
struct timeval timev, *tv = NULL;
struct hiddev_usage_ref uref;
if (argc < 2) {
struct hiddev_usage_ref uref;
/* deal with either standard location or Red Hat's */
const char *hid_dirs[] = {"/dev/usb/hid", "/dev/usb",};
for (i = 0; i < sizeof(hid_dirs)/sizeof(hid_dirs[0]); i++) {
for (j = 0; j < 4; j++) {
sprintf(evdev, "%s/hiddev%d", hid_dirs[i], j);
if ((fd = open(evdev, O_RDONLY)) < 0) {
if (errno == EACCES) {
fprintf(stderr, "No permission, try this as root.\n");
exit(1);
}
} else {
if (find_application(fd, UPS_USAGE)) goto foundit;
close(fd);
}
}
}
fprintf(stderr, "Couldn't find USB UPS device, check your /dev.\n");
exit(1);
foundit:
printf("Found UPS at %s\n", evdev);
} else {
strncpy(evdev, argv[argc -1], sizeof(evdev)-1);
printf("Found UPS at %s\n", evdev);
if ((fd = open(evdev, O_RDONLY)) < 0) {
perror("hiddev open");
exit(1);
}
if (!find_application(fd, UPS_USAGE)) {
fprintf(stderr, "%s is not a UPS\n", argv[argc - 1]);
exit(1);
}
}
#ifdef DEBUG
{
unsigned version;
ioctl(fd, HIDIOCGVERSION, &version);
printf("hiddev driver version is %d.%d.%d\n",
version >> 16, (version >> 8) & 0xff, version & 0xff);
ioctl(fd, HIDIOCGDEVINFO, &dinfo);
printf("HID: vendor 0x%x product 0x%x version 0x%x ",
dinfo.vendor, dinfo.product & 0xffff, dinfo.version);
printf("app %s", info(ioctl(fd, HIDIOCAPPLICATION, 0)));
for (i = 1; i < dinfo.num_applications; i++)
printf(", %s", info(ioctl(fd, HIDIOCAPPLICATION, i)));
printf("\n");
printf("HID: bus: %d devnum: %d ifnum: %d\n",
dinfo.busnum, dinfo.devnum, dinfo.ifnum);
vendor = dinfo.vendor;
}
#endif
ioctl(fd, HIDIOCINITREPORT, 0);
ioctl(fd, HIDIOCGNAME(sizeof(name)), name);
printf("UPS HID device name: \"%s\"\n", name);
memset(&uref, 0, sizeof(uref));
uref.report_type = HID_REPORT_TYPE_FEATURE;
uref.report_id = HID_REPORT_ID_UNKNOWN;
uref.usage_code = BAT_CHEMISTRY;
if (ioctl(fd, HIDIOCGUSAGE, &uref) == 0) {
printf("Battery Chemistry: \"%s\" (%d)\n", get_string(fd, uref.value),
uref.value);
}
memset(&uref, 0, sizeof(uref));
uref.report_type = HID_REPORT_TYPE_FEATURE;
uref.report_id = HID_REPORT_ID_UNKNOWN;
uref.usage_code = UPS_CAPACITY_MODE;
if (ioctl(fd, HIDIOCGUSAGE, &uref) == 0) {
CapacityMode = uref.value;
}
#ifdef DEBUG
/* To traverse the report descriptor info
*/
{
struct hiddev_report_info rinfo;
struct hiddev_field_info finfo;
struct hiddev_usage_ref uref;
int rtype, i, j;
for (rtype = HID_REPORT_TYPE_MIN; rtype <= HID_REPORT_TYPE_MAX;
rtype++) {
rinfo.report_type = rtype;
rinfo.report_id = HID_REPORT_ID_FIRST;
while (ioctl(fd, HIDIOCGREPORTINFO, &rinfo) >= 0) {
printf("\n%sReport %d\n",
reports[rinfo.report_type], rinfo.report_id);
for (i = 0; i < rinfo.num_fields; i++) {
struct s_ups_info *p;
memset(&finfo, 0, sizeof(finfo));
finfo.report_type = rinfo.report_type;
finfo.report_id = rinfo.report_id;
finfo.field_index = i;
ioctl(fd, HIDIOCGFIELDINFO, &finfo);
printf(" Field %d, app %s, phys %s\n",
i,
info(finfo.application), info(finfo.physical));
memset(&uref, 0, sizeof(uref));
for (j = 0; j < finfo.maxusage; j++) {
unsigned unit, exponent;
int v, yy, mm, dd;
uref.report_type = finfo.report_type;
uref.report_id = finfo.report_id;
uref.field_index = i;
uref.usage_index = j;
ioctl(fd, HIDIOCGUCODE, &uref);
ioctl(fd, HIDIOCGUSAGE, &uref);
p = info_entry(uref.usage_code);
switch (p->type) {
case T_CAPACITY:
unit = 1;
printf("Exponent %d lost.\n", exponent);
exponent = CapacityMode;
break;
case T_UNITS:
unit = finfo.unit;
exponent = finfo.unit_exponent;
break;
default:
unit = 0;
exponent = 0;
break;
}
printf(" Usage %d, %s = %d %s", j, p->label, uref.value,
get_units(unit, exponent));
switch (p->type) {
case T_INDEX:
printf(" %s\n", get_string(fd, uref.value));
break;
case T_BITS: /* binary bits */
printf(" 0x%x\n", uref.value);
break;
case T_DATE: /* packed integer date */
printf(" %4d-%02d-%02d\n", (uref.value >> 9) + 1980,
(uref.value >> 5) & 0xF, uref.value & 0x1F);
break;
case T_APCDATE: /* APC date */
v = uref.value;
yy = ((v>>4) & 0xF)*10 + (v&0xF) + 2000;
v >>= 8;
dd = ((v>>4) & 0xF)*10 + (v&0xF);
v >>= 8;
mm = ((v>>4) & 0xF)*10 + (v&0xF);
printf(" %4d-%02d-%02d\n", yy, mm, dd);
default:
printf("\n");
break;
}
}
}
rinfo.report_id |= HID_REPORT_ID_NEXT;
}
}
}
printf("\nWaiting for events ... (interrupt to exit)\n");
fflush(stdout);
#endif
FD_ZERO(&fdset);
while (1) {
if (fd < 0) {
sleep(5);
fd = open(evdev, O_RDONLY);
if (fd < 0) {
perror("\nOpen error");
continue;
}
if (!find_application(fd, UPS_USAGE)) {
fprintf(stderr, "\nCould not find_application.\n");
close(fd);
fd = -1;
}
continue;
}
switch (state) {
case STATE_NORMAL:
tv = NULL;
break;
case STATE_BATTERY:
case STATE_DEBOUNCE:
timev.tv_sec = DEBOUNCE_TIMEOUT;
timev.tv_usec = 0;
tv = &timev;
break;
}
FD_SET(fd, &fdset);
rd = select(fd+1, &fdset, NULL, NULL, tv);
if (rd > 0) {
rd = read(fd, ev, sizeof(ev));
if (rd < (int) sizeof(ev[0])) {
if (rd < 0)
perror("\nevtest: error reading");
else
fprintf(stderr, "\nevtest: got short read from device!\n");
// exit (1);
close(fd);
fd = -1;
continue;
}
for (i = 0; i < rd / sizeof(ev[0]); i++) {
#ifdef DEBUG
printf("Event: %s = %d\n",
info(ev[i].hid), ev[i].value);
#endif /* DEBUG */
if (ev[i].hid == UPS_SHUTDOWN_IMMINENT && ev[i].value == 1) {
log_status("UPS shutdown imminent!");
powerfail(-1);
state = STATE_BATTERY;
RemainingCapacity = -1;
}
switch (state) {
case STATE_BATTERY:
if (ev[i].hid == UPS_DISCHARGING && ev[i].value == 0) {
log_status("System back on AC power");
powerfail(0);
state = STATE_NORMAL;
RemainingCapacity = -1;
}
break;
case STATE_DEBOUNCE:
if (ev[i].hid == UPS_DISCHARGING && ev[i].value == 0) {
state = STATE_NORMAL;
RemainingCapacity = -1;
}
break;
case STATE_NORMAL:
if (ev[i].hid == UPS_DISCHARGING && ev[i].value == 1) {
state = STATE_DEBOUNCE;
RemainingCapacity = -1;
}
break;
}
}
} else {
/* Our timer has expired */
switch (state) {
case STATE_DEBOUNCE:
log_status("System switched to battery power");
state = STATE_BATTERY;
powerfail(1);
break;
default:
break;
}
}
fflush(stdout);
}
}
|