/*
apcsmart.c - driver for APC smart protocol units (originally "newapc")
Copyright (C) 1999 Russell Kroll <rkroll@exploits.org>
(C) 2000 Nigel Metheringham <Nigel.Metheringham@Intechnology.co.uk>
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
*/
#define APC_DRIVER_VERSION "1.99.8"
#include "main.h"
#include "serial.h"
#include "apcsmart.h"
#define ALT_CABLE_1 "940-0095B"
static int ups_status = 0, quirk_capability_overflow = 0;
static struct apc_vartab_t *vartab_lookup_char(char cmdchar)
{
int i;
for (i = 0; apc_vartab[i].name != NULL; i++)
if (apc_vartab[i].cmd == cmdchar)
return &apc_vartab[i];
return NULL;
}
static struct apc_vartab_t *vartab_lookup_name(const char *var)
{
int i;
for (i = 0; apc_vartab[i].name != NULL; i++)
if (!strcasecmp(apc_vartab[i].name, var))
return &apc_vartab[i];
return NULL;
}
/* FUTURE: change to use function pointers */
/* convert APC formatting to NUT formatting */
static char *convert_data(struct apc_vartab_t *cmd_entry, char *upsval)
{
static char tmp[128];
int tval;
switch(cmd_entry->flags & APC_FORMATMASK) {
case APC_F_PERCENT:
case APC_F_VOLT:
case APC_F_AMP:
case APC_F_CELSIUS:
case APC_F_HEX:
case APC_F_DEC:
case APC_F_SECONDS:
case APC_F_LEAVE:
/* no conversion for any of these */
return upsval;
case APC_F_HOURS:
/* convert to seconds */
tval = 60 * 60 * strtol(upsval, NULL, 10);
snprintf(tmp, sizeof(tmp), "%d", tval);
return tmp;
case APC_F_MINUTES:
/* Convert to seconds - NUT standard time measurement */
tval = 60 * strtol(upsval, NULL, 10);
/* Ignore errors - Theres not much we can do */
snprintf(tmp, sizeof(tmp), "%d", tval);
return tmp;
default:
upslogx(LOG_NOTICE, "Unable to handle conversion of %s",
cmd_entry->name);
return upsval;
}
/* NOTREACHED */
return upsval;
}
static void ups_status_set(void)
{
status_init();
if (ups_status & APC_STAT_CAL)
status_set("CAL"); /* calibration */
if (ups_status & APC_STAT_TRIM)
status_set("TRIM"); /* SmartTrim */
if (ups_status & APC_STAT_BOOST)
status_set("BOOST"); /* SmartBoost */
if (ups_status & APC_STAT_OL)
status_set("OL"); /* on line */
if (ups_status & APC_STAT_OB)
status_set("OB"); /* on battery */
if (ups_status & APC_STAT_OVER)
status_set("OVER"); /* overload */
if (ups_status & APC_STAT_LB)
status_set("LB"); /* low battery */
if (ups_status & APC_STAT_RB)
status_set("RB"); /* replace batt */
if (ups_status == 0)
status_set("OFF");
status_commit();
}
static void alert_handler(char ch)
{
switch (ch) {
case '!': /* clear OL, set OB */
upsdebugx(4, "alert_handler: OB");
ups_status &= ~APC_STAT_OL;
ups_status |= APC_STAT_OB;
break;
case '$': /* clear OB, set OL */
upsdebugx(4, "alert_handler: OL");
ups_status &= ~APC_STAT_OB;
ups_status |= APC_STAT_OL;
break;
case '%': /* set LB */
upsdebugx(4, "alert_handler: LB");
ups_status |= APC_STAT_LB;
break;
case '+': /* clear LB */
upsdebugx(4, "alert_handler: not LB");
ups_status &= ~APC_STAT_LB;
break;
case '#': /* set RB */
upsdebugx(4, "alert_handler: RB");
ups_status |= APC_STAT_RB;
break;
default:
upsdebugx(4, "alert_handler got 0x%02x (unhandled)", ch);
break;
}
ups_status_set();
}
static int read_buf(char *buf, size_t buflen)
{
int ret;
ret = ser_get_line_alert(upsfd, buf, buflen, ENDCHAR, POLL_IGNORE,
POLL_ALERT, alert_handler, SER_WAIT_SEC, SER_WAIT_USEC);
if (ret < 1) {
ser_comm_fail(NULL);
return ret;
}
ser_comm_good();
return ret;
}
static int poll_data(struct apc_vartab_t *vt)
{
int ret;
char tmp[SMALLBUF];
if ((vt->flags & APC_PRESENT) == 0)
return 1;
upsdebugx(4, "poll_data: %s", vt->name);
ret = ser_send_char(upsfd, vt->cmd);
if (ret != 1) {
upslogx(LOG_ERR, "poll_data: ser_send_char failed");
dstate_datastale();
return 0;
}
if (read_buf(tmp, sizeof(tmp)) < 1) {
dstate_datastale();
return 0;
}
/* no longer supported by the hardware somehow */
if (!strcmp(tmp, "NA")) {
dstate_delinfo(vt->name);
return 1;
}
dstate_setinfo(vt->name, "%s", convert_data(vt, tmp));
dstate_dataok();
return 1;
}
/* check for support or just update a named variable */
static int query_ups(const char *var, int first)
{
int ret;
char temp[256], *ptr;
struct apc_vartab_t *vt;
vt = vartab_lookup_name(var);
if (!vt) {
upsdebugx(1, "query_ups: unknown variable %s", var);
return 0;
}
/* already known to not be supported? */
if (vt->flags & APC_IGNORE)
return 0;
/* empty the input buffer (while allowing the alert handler to run) */
ret = ser_get_line_alert(upsfd, temp, sizeof(temp), ENDCHAR,
POLL_IGNORE, POLL_ALERT, alert_handler, 0, 0);
ret = ser_send_char(upsfd, vt->cmd);
if (ret != 1) {
upslog_with_errno(LOG_ERR, "query_ups: ser_send_char failed");
return 0;
}
ret = ser_get_line_alert(upsfd, temp, sizeof(temp), ENDCHAR,
POLL_IGNORE, POLL_ALERT, alert_handler, SER_WAIT_SEC,
SER_WAIT_USEC);
if ((ret < 1) && (first == 0)) {
ser_comm_fail(NULL);
return 0;
}
ser_comm_good();
if ((ret < 1) || (!strcmp(temp, "NA"))) { /* not supported */
vt->flags |= APC_IGNORE;
return 0;
}
ptr = convert_data(vt, temp);
dstate_setinfo(vt->name, "%s", ptr);
return 1; /* success */
}
static void do_capabilities(void)
{
const char *ptr, *entptr;
char upsloc, temp[512], cmd, loc, etmp[16], *endtemp;
int nument, entlen, i, matrix, ret;
struct apc_vartab_t *vt;
upsdebugx(1, "APC - About to get capabilities string");
/* If we can do caps, then we need the Firmware revision which has
the locale descriptor as the last character (ugh)
*/
ptr = dstate_getinfo("ups.firmware");
if (ptr)
upsloc = ptr[strlen(ptr) - 1];
else
upsloc = 0;
/* get capability string */
ret = ser_send_char(upsfd, APC_CAPABILITY); /* ^Z */
if (ret != 1) {
upslog_with_errno(LOG_ERR, "do_capabilities: ser_send_char failed");
return;
}
/* note different IGN set since ^Z returns things like # */
ret = ser_get_line(upsfd, temp, sizeof(temp), ENDCHAR,
MINIGNCHARS, SER_WAIT_SEC, SER_WAIT_USEC);
if ((ret < 1) || (!strcmp(temp, "NA"))) {
/* Early Smart-UPS, not as smart as later ones */
/* This should never happen since we only call
this if the REQ_CAPABILITIES command is supported
*/
upslogx(LOG_ERR, "ERROR: APC cannot do capabilites but said it could!");
return;
}
/* recv always puts a \0 at the end, so this is safe */
/* however it assumes a zero byte cannot be embedded */
endtemp = &temp[0] + strlen(temp);
if (temp[0] != '#') {
printf("Unrecognized capability start char %c\n", temp[0]);
printf("Please report this error [%s]\n", temp);
upslogx(LOG_ERR, "ERROR: unknown capability start char %c!",
temp[0]);
return;
}
if (temp[1] == '#') { /* Matrix-UPS */
matrix = 1;
ptr = &temp[0];
}
else {
ptr = &temp[1];
matrix = 0;
}
/* command char, location, # of entries, entry length */
while (ptr[0] != '\0') {
if (matrix)
ptr += 2; /* jump over repeating ## */
/* check for idiocy */
if (ptr >= endtemp) {
/* if we expected this, just ignore it */
if (quirk_capability_overflow)
return;
printf("Capability string has overflowed\n");
printf("Please report this error\n");
fatalx("ERROR: capability overflow!");
}
cmd = ptr[0];
loc = ptr[1];
nument = ptr[2] - 48;
entlen = ptr[3] - 48;
entptr = &ptr[4];
vt = vartab_lookup_char(cmd);
/* mark this as writable */
if (vt && ((loc == upsloc) || (loc == '4'))) {
upsdebugx(1, "Supported capability: %02x (%c) - %s",
cmd, loc, vt->name);
dstate_setflags(vt->name, ST_FLAG_RW);
/* make sure setvar knows what this is */
vt->flags |= APC_RW | APC_ENUM;
}
for (i = 0; i < nument; i++) {
snprintf(etmp, entlen + 1, "%s", entptr);
if (vt && ((loc == upsloc) || (loc == '4')))
dstate_addenum(vt->name, "%s",
convert_data(vt, etmp));
entptr += entlen;
}
ptr = entptr;
}
}
static int update_status(void)
{
int ret;
char buf[SMALLBUF];
upsdebugx(4, "update_status");
ser_flush_in(upsfd, IGNCHARS, nut_debug_level);
ret = ser_send_char(upsfd, APC_STATUS);
if (ret != 1) {
upslog_with_errno(LOG_ERR, "update_status: ser_send_char failed");
dstate_datastale();
return 0;
}
ret = read_buf(buf, sizeof(buf));
if ((ret < 1) || (!strcmp(buf, "NA"))) {
dstate_datastale();
return 0;
}
ups_status = strtol(buf, 0, 16) & 0xff;
ups_status_set();
status_commit();
dstate_dataok();
return 1;
}
static void oldapcsetup(void)
{
int ret = 0;
/* really old models ignore REQ_MODEL, so find them first */
ret = query_ups("ups.model", 1);
if (ret != 1) {
/* force the model name */
dstate_setinfo("ups.model", "Smart-UPS");
}
/* see if this might be an old Matrix-UPS instead */
if (query_ups("output.current", 1))
dstate_setinfo("ups.model", "Matrix-UPS");
query_ups("ups.serial", 1);
query_ups("input.voltage", 1); /* This one may fail... no problem */
update_status();
/* If we have come down this path then we dont do capabilities and
other shiny features
*/
}
static void protocol_verify(unsigned char cmd)
{
int i, found;
/* we might not care about this one */
if (strchr(CMD_IGN_CHARS, cmd))
return;
/* see if it's a variable */
for (i = 0; apc_vartab[i].name != NULL; i++) {
/* 1:1 here, so the first match is the only match */
if (apc_vartab[i].cmd == cmd) {
upsdebugx(3, "UPS supports variable [%s]",
apc_vartab[i].name);
/* load initial data */
apc_vartab[i].flags |= APC_PRESENT;
poll_data(&apc_vartab[i]);
/* handle special data for our two strings */
if (apc_vartab[i].flags & APC_STRING) {
dstate_setflags(apc_vartab[i].name,
ST_FLAG_RW | ST_FLAG_STRING);
dstate_setaux(apc_vartab[i].name, APC_STRLEN);
apc_vartab[i].flags |= APC_RW;
}
return;
}
}
/* check the command list */
/* some cmdchars map onto multiple commands (start and stop) */
found = 0;
for (i = 0; apc_cmdtab[i].name != NULL; i++) {
if (apc_cmdtab[i].cmd == cmd) {
upsdebugx(2, "UPS supports command [%s]",
apc_cmdtab[i].name);
dstate_addcmd(apc_cmdtab[i].name);
apc_cmdtab[i].flags |= APC_PRESENT;
found = 1;
}
}
if (found)
return;
if (isprint(cmd))
upsdebugx(1, "protocol_verify: 0x%02x [%c] unrecognized",
cmd, cmd);
else
upsdebugx(1, "protocol_verify: 0x%02x unrecognized", cmd);
}
/* some hardware is a special case - hotwire the list of cmdchars */
static int firmware_table_lookup(void)
{
int ret;
unsigned int i, j;
char buf[SMALLBUF];
upsdebugx(1, "Attempting firmware lookup");
ret = ser_send_char(upsfd, 'b');
if (ret != 1) {
upslog_with_errno(LOG_ERR, "getbaseinfo: ser_send_char failed");
return 0;
}
ret = ser_get_line(upsfd, buf, sizeof(buf), ENDCHAR, IGNCHARS,
SER_WAIT_SEC, SER_WAIT_USEC);
/* see if this is an older version like an APC600 which doesn't
* response to 'a' or 'b' queries
*/
if ((ret < 1) || (!strcmp(buf, "NA"))) {
upsdebugx(1, "Attempting to contact older Smart-UPS version");
ret = ser_send_char(upsfd, 'V');
if (ret != 1) {
upslog_with_errno(LOG_ERR, "getbaseinfo: ser_send_char failed");
return 0;
}
ret = ser_get_line(upsfd, buf, sizeof(buf), ENDCHAR, IGNCHARS,
SER_WAIT_SEC, SER_WAIT_USEC);
/* found one, force the model information */
if (!strcmp(buf, "6QD")) {
upsdebugx(1, "Found Smart-UPS");
dstate_setinfo("ups.model", "Smart-UPS");
}
else if (!strcmp(buf, "6TI")) {
upsdebugx(1, "Found Smart-UPS");
dstate_setinfo("ups.model", "Smart-UPS");
}
else return 0;
}
upsdebugx(2, "Firmware: [%s]", buf);
/* this will be reworked if we get a lot of these things */
if (!strcmp(buf, "451.2.I")) {
quirk_capability_overflow = 1;
return 0;
}
for (i = 0; compat_tab[i].firmware != NULL; i++) {
if (!strcmp(compat_tab[i].firmware, buf)) {
upsdebugx(2, "Matched - cmdchars: %s",
compat_tab[i].cmdchars);
/* matched - run the cmdchars from the table */
for (j = 0; j < strlen(compat_tab[i].cmdchars); j++)
protocol_verify(compat_tab[i].cmdchars[j]);
return 1; /* matched */
}
}
upsdebugx(2, "Not found in table - trying normal method");
return 0;
}
static void getbaseinfo(void)
{
unsigned int i;
int ret = 0;
char *alrts, *cmds, temp[512];
if (firmware_table_lookup() == 1)
return;
upsdebugx(1, "APC - Attempting to find command set");
/* Initially we ask the UPS what commands it takes
If this fails we are going to need an alternate
strategy - we can deal with that if it happens
*/
ret = ser_send_char(upsfd, APC_CMDSET);
if (ret != 1) {
upslog_with_errno(LOG_ERR, "getbaseinfo: ser_send_char failed");
return;
}
ret = ser_get_line(upsfd, temp, sizeof(temp), ENDCHAR, IGNCHARS,
SER_WAIT_SEC, SER_WAIT_USEC);
if ((ret < 1) || (!strcmp(temp, "NA"))) {
/* We have an old dumb UPS - go to specific code for old stuff */
oldapcsetup();
return;
}
upsdebugx(1, "APC - Parsing out command set");
/* We have the version.alert.cmdchars string
NB the alert chars are normally in IGNCHARS
so will have been pretty much edited out.
You will need to change the ser_get_line above if
you want to check those out too....
*/
alrts = strchr(temp, '.');
if (alrts == NULL) {
printf("Unable to split APC version string\n");
printf("Bailing out\n");
exit(EXIT_FAILURE);
}
*alrts++ = 0;
cmds = strchr(alrts, '.');
if (cmds == NULL) {
printf("Unable to find APC command string\n");
printf("Bailing out\n");
exit(EXIT_FAILURE);
}
*cmds++ = 0;
for (i = 0; i < strlen(cmds); i++)
protocol_verify(cmds[i]);
/* if capabilities are supported, add them here */
if (strchr(cmds, APC_CAPABILITY))
do_capabilities();
upsdebugx(1, "APC - UPS capabilities determined");
}
/* check for calibration status and either start or stop */
static int do_cal(int start)
{
char temp[256];
int tval, ret;
ret = ser_send_char(upsfd, APC_STATUS);
if (ret != 1) {
upslog_with_errno(LOG_ERR, "do_cal: ser_send_char failed");
return STAT_INSTCMD_HANDLED; /* FUTURE: failure */
}
ret = read_buf(temp, sizeof(temp));
/* if we can't check the current calibration status, bail out */
if ((ret < 1) || (!strcmp(temp, "NA")))
return STAT_INSTCMD_HANDLED; /* FUTURE: failure */
tval = strtol(temp, 0, 16);
if (tval & APC_STAT_CAL) { /* calibration currently happening */
if (start == 1) {
/* requested start while calibration still running */
upslogx(LOG_INFO, "Runtime calibration already in progress");
return STAT_INSTCMD_HANDLED; /* FUTURE: failure */
}
/* stop requested */
upslogx(LOG_INFO, "Stopping runtime calibration");
ret = ser_send_char(upsfd, APC_CMD_CALTOGGLE);
if (ret != 1) {
upslog_with_errno(LOG_ERR, "do_cal: ser_send_char failed");
return STAT_INSTCMD_HANDLED; /* FUTURE: failure */
}
ret = read_buf(temp, sizeof(temp));
if ((ret < 1) || (!strcmp(temp, "NA")) || (!strcmp(temp, "NO"))) {
upslogx(LOG_WARNING, "Stop calibration failed: %s",
temp);
return STAT_INSTCMD_HANDLED; /* FUTURE: failure */
}
return STAT_INSTCMD_HANDLED; /* FUTURE: success */
}
/* calibration not happening */
if (start == 0) { /* stop requested */
upslogx(LOG_INFO, "Runtime calibration not occurring");
return STAT_INSTCMD_HANDLED; /* FUTURE: failure */
}
upslogx(LOG_INFO, "Starting runtime calibration");
ret = ser_send_char(upsfd, APC_CMD_CALTOGGLE);
if (ret != 1) {
upslog_with_errno(LOG_ERR, "do_cal: ser_send_char failed");
return STAT_INSTCMD_HANDLED; /* FUTURE: failure */
}
ret = read_buf(temp, sizeof(temp));
if ((ret < 1) || (!strcmp(temp, "NA")) || (!strcmp(temp, "NO"))) {
upslogx(LOG_WARNING, "Start calibration failed: %s", temp);
return STAT_INSTCMD_HANDLED; /* FUTURE: failure */
}
return STAT_INSTCMD_HANDLED; /* FUTURE: success */
}
/* get the UPS talking to us in smart mode */
static int smartmode(void)
{
int ret, tries;
char temp[256];
for (tries = 0; tries < 5; tries++) {
ret = ser_send_char(upsfd, APC_GOSMART);
if (ret != 1) {
upslog_with_errno(LOG_ERR, "smartmode: ser_send_char failed");
return 0;
}
ret = ser_get_line(upsfd, temp, sizeof(temp), ENDCHAR,
IGNCHARS, SER_WAIT_SEC, SER_WAIT_USEC);
if (ret > 0)
if (!strcmp(temp, "SM"))
return 1; /* success */
sleep(1); /* wait before trying again */
/* it failed, so try to bail out of menus on newer units */
ret = ser_send_char(upsfd, 27); /* ESC */
if (ret != 1) {
upslog_with_errno(LOG_ERR, "smartmode: ser_send_char failed");
return 0;
}
/* eat the response (might be NA, might be something else) */
ret = ser_get_line(upsfd, temp, sizeof(temp), ENDCHAR,
IGNCHARS, SER_WAIT_SEC, SER_WAIT_USEC);
}
return 0; /* failure */
}
/* power down the attached load immediately */
void upsdrv_shutdown(void)
{
char temp[32];
int ret, tval, sdtype = 0;
if (!smartmode())
printf("Detection failed. Trying a shutdown command anyway.\n");
/* check the line status */
ret = ser_send_char(upsfd, APC_STATUS);
if (ret == 1) {
ret = ser_get_line(upsfd, temp, sizeof(temp), ENDCHAR,
IGNCHARS, SER_WAIT_SEC, SER_WAIT_USEC);
if (ret < 1) {
printf("Status read failed! Assuming on battery state\n");
tval = APC_STAT_LB | APC_STAT_OB;
} else {
tval = strtol(temp, 0, 16);
}
} else {
printf("Status request failed; assuming on battery state\n");
tval = APC_STAT_LB | APC_STAT_OB;
}
if (testvar("sdtype"))
sdtype = atoi(getval("sdtype"));
switch (sdtype) {
case 4: /* special hack for CS 350 and similar models */
printf("Using CS 350 'force OB' shutdown method\n");
if (tval & APC_STAT_OL) {
printf("On line - forcing OB temporarily\n");
ser_send_char(upsfd, 'U');
}
ser_send_char(upsfd, '