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
|
/* optiups.c - OptiSafe UPS (very loosely based on the nut 0.45.5 driver)
Copyright (C) 1999 Russell Kroll <rkroll@exploits.org>
Copyright (C) 2006 Scott Heavner [Use my alioth acct: sheavner]
Support for Zinto D from ONLINE USV (only minor differences to OptiSafe UPS)
added by Matthias Goebl <matthias.goebl@goebl.net>
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
==================================================================================
*/
#include "main.h"
#include "serial.h"
#define DRIVER_NAME "Opti-UPS driver"
#define DRIVER_VERSION "1.01"
/* driver description structure */
upsdrv_info_t upsdrv_info = {
DRIVER_NAME,
DRIVER_VERSION,
"Russell Kroll <rkroll@exploits.org>\n" \
"Scott Heavner <debian@scottheavner.com>\n" \
"Matthias Goebl <matthias.goebl@goebl.net>",
DRV_STABLE,
{ NULL }
};
#define HELP "\n" \
"**********************************************************" "\n" \
"This driver has been tested only with the OPTI SAFE 420E using" "\n" \
"the custom cable described in the NUT Opti-UPS protocol page." "\n" \
"Seriously, it does _NOT_ work with the [windows] cable provided with" "\n" \
"the UPS or a standard serial cable. (I don't know if OPTI makes other" "\n" \
"UPS models that will work with another cable?) Standard linux serial" "\n" \
"port drivers do not support the required DTR handshaking." "\n" \
"" "\n" \
" UPS 6 -> PC 3 This 3 wire cable pinout maps DTR to CTS." "\n" \
" UPS 9 -> PC 2" "\n" \
" UPS 4 -> PC 5" "\n" \
"" "\n" \
"This driver has also been tested with a Zinto D from Online-USV AG," "\n" \
"using their special cable:" "\n" \
" UPS 6 -> PC 3" "\n" \
" UPS 9 -> PC 2" "\n" \
" UPS 7 -> PC 5" "\n" \
"It works even with a pl2303 usb-serial converter." "\n" \
"**********************************************************" "\n"
/* See http://www.networkupstools.org/protocols/optiups.html and the end of this
* file for more information on the cable and the OPTI-UPS serial protocol used on
* at least the older OPTI UPS models (420E, 820ES).
*/
#define ENDCHAR '\n'
#define IGNCHARS "\r"
/* Our custom options available with -x */
#define OPTI_MINPOLL "status_only"
#define OPTI_FAKELOW "fake_lowbatt"
#define OPTI_NOWARN_NOIMP "nowarn_noimp"
#define OPTI_POWERUP "powerup"
/* All serial commands put their response in the same buffer space */
static char _buf[256];
/* Model */
static int optimodel = 0;
enum {
OPTIMODEL_DEFAULT = 0,
OPTIMODEL_ZINTO =1
};
/* Status bits returned by the "AG" command */
enum {
OPTISBIT_NOOUTPUT = 2,
OPTISBIT_OVERLOAD = 8,
OPTISBIT_REPLACE_BATTERY = 16,
OPTISBIT_ON_BATTERY_POWER = 32,
OPTISBIT_LOW_BATTERY = 64
};
/* Helper struct for the optifill() function */
typedef struct ezfill_s {
const char *cmd;
const char *var;
const float scale;
} ezfill_t;
/* These can be polled right into a string usable by NUT.
* Others such as "AG" and "BV" require some transformation of the return value */
static ezfill_t _pollv[] = {
{ "NV", "input.voltage" },
{ "OL", "ups.load", 1.0 },
{ "OV", "output.voltage" },
{ "FF", "input.frequency", 0.1 },
{ "BT", "ups.temperature" },
};
static ezfill_t _pollv_zinto[] = {
{ "NV", "input.voltage", 2.0 },
{ "OL", "ups.load", 1.0 },
{ "OV", "output.voltage", 2.0 },
{ "OF", "output.frequency", 0.1 },
{ "NF", "input.frequency", 0.1 },
{ "BT", "ups.temperature" },
};
/* model "IO" is parsed differently in upsdrv_initinfo() */
static ezfill_t _initv[] = {
{ "IM", "ups.mfr" },
{ "IZ", "ups.serial" },
{ "IS", "ups.firmware" },
};
/* All serial reads of the OPTI-UPS go through here. We always expect a CR/LF terminated
* response. Unknown/Unimplemented commands return ^U (0x15). Actions that complete
* successfully return ^F (0x06). */
static inline int optireadline(void)
{
int r;
usleep(150000);
r = ser_get_line(upsfd, _buf, sizeof(_buf), ENDCHAR, IGNCHARS, 0, 500000 );
_buf[sizeof(_buf)-1] = 0;
if ( r > 0 )
{
if ( r < (int)sizeof(_buf) )
_buf[r] = 0;
if ( _buf[0] == 0x15 )
{
r=-2;
upsdebugx(1, "READ: <unsupported command>");
}
if ( _buf[0] == 0x06 )
{
upsdebugx(2, "READ: <command done>");
}
else
{
upsdebugx(2, "READ: \"%s\"", _buf );
}
}
else
upsdebugx(1, "READ ERROR: %d", r );
return r;
}
/* Send a command and read the response. Command response is in global _buf.
* Return
* > 0 implies success.
* -1 serial timeout
* -2 unknown/unimplemented command
*/
static inline int optiquery( const char *cmd )
{
upsdebugx(2, "SEND: \"%s\"", cmd );
ser_send( upsfd, "%s", cmd );
if ( optimodel == OPTIMODEL_ZINTO )
ser_send( upsfd, "\r\n" );
return optireadline();
}
/* Uses the ezfill_t structure to map UPS commands to the NUT variable destinations */
static void optifill( ezfill_t *a, int len )
{
int i, r;
/* Some things are easy to poll and store */
for ( i=0; i<len; ++i )
{
if ( ( a[i].cmd == NULL ) || ( a[i].cmd[0] == 0 ) )
continue;
r = optiquery( a[i].cmd );
if ( r < 1 )
{
if ( r == -2 )
{
if ( ! testvar(OPTI_NOWARN_NOIMP) )
upslogx( LOG_NOTICE,
"disabling poll of unsupported var %s",
a[i].var );
dstate_setinfo( a[i].var, "N/A" );
a[i].cmd = NULL;
}
else
upslogx( LOG_WARNING, "can't retrieve %s", a[i].var );
continue;
}
if ( a[i].scale > 1e-20 )
{
float f = strtol( _buf, NULL, 10 ) * a[i].scale;
dstate_setinfo( a[i].var, "%.1f", f );
}
else
{
dstate_setinfo( a[i].var, "%s", _buf);
}
}
}
/* Handle custom (but standardized) NUT commands */
static int instcmd(const char *cmdname, const char *extra)
{
if (!strcasecmp(cmdname, "test.failure.start"))
{
optiquery( "Ts" );
return STAT_INSTCMD_HANDLED;
}
else if (!strcasecmp(cmdname, "load.off"))
{
/* You do realize this will kill power to ourself. Would probably only
* be useful for killing power for a slave computer */
if ( optimodel == OPTIMODEL_ZINTO )
{
optiquery( "Ct1" );
optiquery( "Cs0000000" );
sleep(2);
return STAT_INSTCMD_HANDLED;
}
optiquery( "Ct0" );
optiquery( "Cs00000000" );
return STAT_INSTCMD_HANDLED;
}
else if (!strcasecmp(cmdname, "load.on"))
{
if ( optimodel == OPTIMODEL_ZINTO )
{
optiquery( "Ct1" );
optiquery( "Cu0000000" );
sleep(2);
return STAT_INSTCMD_HANDLED;
}
optiquery( "Ct0" );
optiquery( "Cu00000000" );
return STAT_INSTCMD_HANDLED;
}
else if (!strcasecmp(cmdname, "shutdown.return"))
{
/* This shuts down the UPS. When the power returns to the UPS,
* it will power back up in its default state. */
if ( optimodel == OPTIMODEL_ZINTO )
{
optiquery( "Ct1" );
optiquery( "Cu0000010" );
optiquery( "Cs0000001" );
return STAT_INSTCMD_HANDLED;
}
optiquery( "Ct1" );
optiquery( "Cs00000010" );
return STAT_INSTCMD_HANDLED;
}
else if (!strcasecmp(cmdname, "shutdown.stayoff"))
{
/* This actually stays off as long as the batteries hold,
* if the line power comes back before the batteries die,
* the UPS will never powerup its output stage!!! */
if ( optimodel == OPTIMODEL_ZINTO )
{
optiquery( "Ct1" );
optiquery( "Cs0000001" );
return STAT_INSTCMD_HANDLED;
}
optiquery( "Ct0" );
optiquery( "Cs00000010" );
return STAT_INSTCMD_HANDLED;
}
else if (!strcasecmp(cmdname, "shutdown.stop"))
{
/* Aborts a shutdown that is couting down via the Cs command */
optiquery( "Cs-0000001" );
return STAT_INSTCMD_HANDLED;
}
upslogx(LOG_NOTICE, "instcmd: unknown command [%s]", cmdname);
return STAT_INSTCMD_UNKNOWN;
}
/* Handle variable setting */
static int setvar(const char *varname, const char *val)
{
int status;
if (sscanf(val, "%d", &status) != 1) {
return STAT_SET_UNKNOWN;
}
if (strcasecmp(varname, "outlet.1.switch") == 0) {
status = status==1 ? 1 : 0;
dstate_setinfo( "outlet.1.switch", "%d", status);
optiquery(status ? "Oi11" : "Oi10");
dstate_dataok();
return STAT_SET_HANDLED;
}
return STAT_SET_UNKNOWN;
}
void upsdrv_initinfo(void)
{
int r;
/* If an Zinto Online-USV is off, switch it on first. */
/* It sends only "2" when off, without "\r\n", and doesn't */
/* answer other commands. Therefore without power we'll be */
/* unable to identify the ups. */
if ( testvar(OPTI_POWERUP) && optiquery( "AG" ) < 1 )
{
ser_send( upsfd, "AG\r\n" );
r = ser_get_char(upsfd, &_buf[0], 1, 0);
if ( r == 1 && _buf[0] == '2' )
{
upslogx( LOG_WARNING, "ups was off, switching it on" );
optiquery( "Ct1" );
optiquery( "Cu0000000" );
/* wait for power up */
sleep(15);
}
}
/* Autodetect an Online-USV (only Zinto D is known to work) */
r = optiquery( "IM" );
if ( r > 0 && !strcasecmp(_buf, "ONLINE") )
{
optimodel = OPTIMODEL_ZINTO;
optiquery( "Om11" );
optiquery( "Om21" );
optiquery( "ON" );
}
optifill( _initv, sizeof(_initv)/sizeof(_initv[0]) );
/* Parse out model into longer string -- is this really USEFUL??? */
r = optiquery( "IO" );
if ( r < 1 )
fatal_with_errno(EXIT_FAILURE, "can't retrieve model" );
else
{
switch ( _buf[r-1] )
{
case 'E':
case 'P':
case 'V':
dstate_setinfo("ups.model", "Power%cS %s", _buf[r-1], _buf );
break;
default:
dstate_setinfo("ups.model", "%s", _buf );
break;
}
}
/* Parse out model into longer string */
r = optiquery( "IM" );
if ( r > 0 && !strcasecmp(_buf, "ONLINE") )
{
dstate_setinfo("ups.mfr", "ONLINE USV-Systeme AG");
r = optiquery( "IO" );
if ( r < 1 )
fatal_with_errno(EXIT_FAILURE, "can't retrieve model" );
switch ( _buf[0] )
{
case 'D':
dstate_setinfo("ups.model", "Zinto %s", _buf );
break;
default:
dstate_setinfo("ups.model", "%s", _buf );
break;
}
}
dstate_addcmd("test.failure.start");
dstate_addcmd("load.off");
dstate_addcmd("load.on");
if( optimodel != OPTIMODEL_ZINTO )
dstate_addcmd("shutdown.stop");
dstate_addcmd("shutdown.return");
dstate_addcmd("shutdown.stayoff");
upsh.instcmd = instcmd;
if ( optimodel == OPTIMODEL_ZINTO )
{
dstate_setinfo("outlet.desc", "%s", "Main Outlet 1+2");
dstate_setinfo("outlet.1.desc", "%s", "Switchable Outlet 3+4");
dstate_setinfo("outlet.id", "%d", 1);
dstate_setinfo("outlet.1.id", "%d", 2);
dstate_setinfo("outlet.switchable", "%d", 0);
dstate_setinfo("outlet.1.switchable", "%d", 1);
dstate_setinfo("outlet.1.switch", "%d", 1);
dstate_setflags("outlet.1.switch", ST_FLAG_RW | ST_FLAG_STRING);
dstate_setaux("outlet.1.switch", 1);
upsh.setvar = setvar;
}
}
void upsdrv_updateinfo(void)
{
int r = optiquery( "AG" );
/* Online-UPS send only "2" when off, without "\r\n" */
if ( r < 1 && optimodel == OPTIMODEL_ZINTO )
{
ser_send( upsfd, "AG\r\n" );
r = ser_get_char(upsfd, &_buf[0], 1, 0);
if ( r == 1 && _buf[0] == '2' )
{
status_init();
status_set("OFF");
status_commit();
return;
}
}
if ( r < 1 )
{
upslogx(LOG_ERR, "can't retrieve ups status" );
dstate_datastale();
}
else
{
int s = strtol( _buf, NULL, 16 );
status_init();
if ( s & OPTISBIT_OVERLOAD )
status_set("OVER");
if ( s & OPTISBIT_REPLACE_BATTERY )
status_set("RB");
if ( s & OPTISBIT_ON_BATTERY_POWER )
status_set("OB");
else
status_set("OL");
if ( s & OPTISBIT_NOOUTPUT )
status_set("OFF");
if ( s & OPTISBIT_LOW_BATTERY )
status_set("LB");
if ( testvar(OPTI_FAKELOW) ) /* FOR TESTING */
status_set("LB");
status_commit();
dstate_dataok();
}
/* Get out of here now if minimum polling is desired */
if ( testvar(OPTI_MINPOLL) )
return;
/* read some easy settings */
if ( optimodel == OPTIMODEL_ZINTO )
optifill( _pollv_zinto, sizeof(_pollv_zinto)/sizeof(_pollv_zinto[0]) );
else
optifill( _pollv, sizeof(_pollv)/sizeof(_pollv[0]) );
/* Battery voltage is harder */
r = optiquery( "BV" );
if ( r < 1 )
upslogx( LOG_WARNING, "cannot retrieve battery voltage" );
else
{
float p, v = strtol( _buf, NULL, 10 ) / 10.0;
dstate_setinfo("battery.voltage", "%.1f", v );
/* battery voltage range: 10.4 - 13.0 VDC */
p = ((v - 10.4) / 2.6) * 100.0;
if ( p > 100.0 )
p = 100.0;
dstate_setinfo("battery.charge", "%.1f", p );
}
}
void upsdrv_shutdown(void)
{
/* OL: this must power cycle the load if possible */
/* OB: the load must remain off until the power returns */
/* If get no response, assume on battery & battery low */
int s = OPTISBIT_ON_BATTERY_POWER | OPTISBIT_LOW_BATTERY;
int r = optiquery( "AG" );
if ( r < 1 )
{
upslogx(LOG_ERR, "can't retrieve ups status during shutdown" );
}
else
{
s = strtol( _buf, NULL, 16 );
}
/* Turn output stage back on if power returns - but really means
* turn off ups if on battery */
optiquery( "Ct1" );
/* What happens, if the power comes back *after* reading the ups status and
* before the shutdown command? For "Online-UPS Zinto D" *always* asking for
* "shutdown shortly and power-up later" works perfectly, because it forces
* a power cycle, even for the named race condition.
* For Opti-UPS I have no information, so I wouldn't dare to change it.
* BTW, Zinto expects only 7 digits after Cu/Cs.
* (Matthias Goebl)
*/
if ( optimodel == OPTIMODEL_ZINTO )
{
/* On line power: Power up in 60 seconds (30 seconds after the following shutdown) */
/* On battery: Power up when the line power returns */
optiquery( "Cu0000060" );
/* Shutdown in 30 seconds */
optiquery( "Cs0000030" );
return;
}
/* Just cycling power, schedule output stage to come back on in 60 seconds */
if ( !(s&OPTISBIT_ON_BATTERY_POWER) )
optiquery( "Cu00000600" );
/* Shutdown in 8 seconds */
optiquery( "Cs00000080" );
}
void upsdrv_help(void)
{
printf(HELP);
}
/* list flags and values that you want to receive via -x */
void upsdrv_makevartable(void)
{
addvar(VAR_FLAG, OPTI_MINPOLL, "Only poll for critical status variables");
addvar(VAR_FLAG, OPTI_FAKELOW, "Fake a low battery status" );
addvar(VAR_FLAG, OPTI_NOWARN_NOIMP, "Supress warnings of unsupported commands");
addvar(VAR_FLAG, OPTI_POWERUP, "(Zinto D) Power-up UPS at start (cannot identify a powered-down Zinto D)");
}
void upsdrv_initups(void)
{
upsfd = ser_open(device_path);
ser_set_speed(upsfd, device_path, B2400);
/* NO PARSED COMMAND LINE VARIABLES */
}
void upsdrv_cleanup(void)
{
ser_close(upsfd, device_path);
}
/*******************************************
* COMMANDS THAT QUERY THE UPS
*******************************************
* IM - manufacturer <ViewSonic>
* IO - model <UPS-420E>
* IS - firmware version <V1.2B>
* IZ - serial number <> (unsupported on 420E, returns ^U)
*
* BS - ??? returns <2>
* BV - battery voltage (in deciVolts) <0140>
* BT - battery temperature (deg C) <0033> (returns ^U on OptiUPS 420E)
*
* NF - input line frequency <600>
* NV - input line voltage <116>
*
* OS - ?? return <3>
* OF - output stage frequency (in 0.1 Hz) <600>
* OV - output stage voltage <118>
* OL - output stage load <027>
*
* FV - Input voltage <120>
* FF - Input Frequency (0.1Hz) <600>
* FO - Output volts <120>
* FR - Output Frequency (0.1Hz) <600>
* FA - Output VA <420>
* FP - Ouptu power <252>
* FU - ?? returns <2>
* FB - ??
* FH - High Transfer Point <144>
* FL - Low Transfer Point <093>
* FT - Transfer point? <121>
*
* AG - UPS status (bitmapped ASCII hex value) <00>
* bit 2: 1 = <set when output stage off?>
* bit 3: 1 = overload
* bit 4: 1 = replace battery
* bit 5: 1 = on battery, 0 = on line
* bit 6: 1 = low battery
* TR - Test results <00>
* 00 = Unknown
* 01 = Passed
* 02 = Warning
* 03 = Error
* 04 = Aborted
* 05 = In Progress
* 06 = No test init
*
*******************************************
* ACTIONS
*******************************************
*
* Ts - Start test
* Ct0 - set power down mode (when running on battery)
* Ct0 = power down only ouput stage
* Ct1 = complete power down
* Cs00000000 - power down after delay (count in 0.1s)
* Cs00000100 = power down in 10s
* Cs-0000001 = cancel power down request
* Cu00000000 - power down after delay (count in 0.1s)
* Cu00000050 = power up in 5s
* Cu00000000 = power up now
*
* CT - returns last setting passed to Ct command <0>
*
*******************************************
* UNKNOWN COMMANDS (on 420e anyways)
*******************************************
* Fu
* Fv
* Ff
* Fo
* Fr
* Fb
* Fl
* Fh
*/
|