download genericups.c
Language: C
License: GPL
Copyright: (C) 1999 Russell Kroll
LOC: 188
Project Info
nut
Server: Debian-SVN
Type: svn
...an‑SVN\n\nut\trunk\drivers\
   al175.c
   al175.h
   apc-hid.c
   apc-hid.h
   apccmib.h
   apcsmart.c
   apcsmart.h
   bcmxcp.c
   bcmxcp.h
   bcmxcp_io.h
   bcmxcp_ser.c
   bcmxcp_usb.c
   belkin-hid.c
   belkin-hid.h
   belkin.c
   belkin.h
   belkinunv.c
   belkinunv.h
   bestfcom.c
   bestfcom.h
   bestuferrups.c
   bestuferrups.h
   bestups.c
   bestups.h
   cpsups.c
   cpsups.h
   cyberpower.c
   cyberpower.h
   dstate-hal.c
   dstate-hal.h
   dstate.c
   dstate.h
   dummy-ups.c
   dummy-ups.h
   energizerups.c
   etapro.c
   etapro.h
   everups.c
   everups.h
   explore-hid.c
   explore-hid.h
   gamatronic.c
   gamatronic.h
   genericups.c
   genericups.h
   hidparser.c
   hidparser.h
   hidtypes.h
   ietfmib.h
   isbmex.c
   isbmex.h
   libhid.c
   libhid.h
   libshut.c
   libshut.h
   libusb.c
   libusb.h
   liebert.c
   liebert.h
   main-hal.c
   main-hal.h
   main.c
   main.h
   Makefile.am
   masterguard.c
   masterguard.h
   megatec.c
   megatec.h
   megatec_usb.c
   metasys.c
   metasys.h
   mge-hid.c
   mge-hid.h
   mge-shut.c
   mge-shut.h
   mge-utalk.c
   mge-utalk.h
   mgemib.h
   netvisionmib.h
   nitram.c
   nitram.h
   nut_usb.c
   nut_usb.h
   oneac.c
   oneac.h
   optiups.c
   optiups.h
   powercom.c
   powercom.h
   powerpanel.c
   powerpanel.h
   pwmib.h
   rhino.c
   safenet.c
   safenet.h
   serial.c
   serial.h
   skel.c
   snmp-ups.c
   snmp-ups.h
   solis.c
   solis.h
   tripplite-hid.c
   tripplite-hid.h
   tripplite.c
   tripplite.h
   tripplite_usb.c
   tripplitesu.c
   tripplitesu.h
   upscode2.c
   upscode2.h
   upsdrvctl.c
   usbhid-ups.c
   usbhid-ups.h
   victronups.c
   victronups.h

/* genericups.c - support for generic contact-closure UPS models

   Copyright (C) 1999  Russell Kroll <rkroll@exploits.org>

   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 DRV_VERSION "1.33"

#include <sys/ioctl.h>

#include "main.h"
#include "serial.h"

#include "genericups.h"

	static	int	upstype = -1;

static void parse_output_signals(const char *value, int *line)
{ 
	/* parse signals the serial port can output */
 
	*line = 0;
 
	if (strstr(value, "DTR") && !strstr(value, "-DTR"))
		*line |= TIOCM_DTR;
 
	if (strstr(value, "RTS") && !strstr(value, "-RTS"))
		*line |= TIOCM_RTS;
 
	if (strstr(value, "ST"))
		*line |= TIOCM_ST;

	if (strstr(value, "CTS"))
		fatalx("Can't override output with CTS (not an output)");

	if (strstr(value, "DCD"))
		fatalx("Can't override output with DCD (not an output)");

	if (strstr(value, "RNG"))
		fatalx("Can't override output with RNG (not an output)");
} 
 
static void parse_input_signals(const char *value, int *line, int *val)
{ 
	/* parse signals the serial port can input */
 
	*line = 0;
	*val = 0;
 
	if (strstr(value, "CTS"))
	{
		*line |= TIOCM_CTS;

		if (!strstr(value, "-CTS"))
			*val |= TIOCM_CTS;
 	}

	if (strstr(value, "DCD"))
	{
		*line |= TIOCM_CD;

		if (!strstr(value, "-DCD"))
			*val |= TIOCM_CD;
 	}

	if (strstr(value, "RNG"))
	{
		*line |= TIOCM_RNG; 

		if (!strstr(value, "-RNG"))
			*val |= TIOCM_RNG;
	}

	if (strstr(value, "DTR"))
		fatalx("Can't override input with DTR (not an input)");

	if (strstr(value, "RTS"))
		fatalx("Can't override input with RTS (not an input)");

	if (strstr(value, "ST"))
		fatalx("Can't override input with ST (not an input)");
}

void upsdrv_initinfo(void)
{
	char	*v;

	dstate_setinfo("driver.version.internal", "%s", DRV_VERSION);

	/* setup the basics */

	dstate_setinfo("ups.mfr", "%s", ((v = getval("mfr")) != NULL) ? v : upstab[upstype].mfr);
	dstate_setinfo("ups.model", "%s", ((v = getval("model")) != NULL) ? v : upstab[upstype].model);

	if ((v = getval("serial")) != NULL)
		dstate_setinfo("ups.serial", "%s", v);

	/* see if the user wants to override the signal definitions */

	if ((v = getval("CP")) != NULL)
	{
		parse_output_signals(v, &upstab[upstype].line_norm);
		upsdebugx(2, "parse_output_signals: CP overriden with %s\n", v);
	}

	if ((v = getval("OL")) != NULL)
	{
		parse_input_signals(v, &upstab[upstype].line_ol, &upstab[upstype].val_ol);
		upsdebugx(2, "parse_input_signals: OL overriden with %s\n", v);
	}

	if ((v = getval("LB")) != NULL)
	{
		parse_input_signals(v, &upstab[upstype].line_bl, &upstab[upstype].val_bl);
 		upsdebugx(2, "parse_input_signals: LB overriden with %s\n", v);
	}

	if ((v = getval("SD")) != NULL)
	{
		parse_output_signals(v, &upstab[upstype].line_sd);
		upsdebugx(2, "parse_output_signals: SD overriden with %s\n", v);
	}
}

/* normal idle loop - keep up with the current state of the UPS */
void upsdrv_updateinfo(void)
{
	int	flags, ol, bl, ret;

	ret = ioctl(upsfd, TIOCMGET, &flags);

	if (ret != 0) {
		upslog_with_errno(LOG_INFO, "ioctl failed");
		ser_comm_fail("Status read failed");
		dstate_datastale();
		return;
	}

	ol = ((flags & upstab[upstype].line_ol) == upstab[upstype].val_ol);
	bl = ((flags & upstab[upstype].line_bl) == upstab[upstype].val_bl);

	status_init();

	if (bl)
		status_set("LB");	/* low battery */

	if (ol)
		status_set("OL");	/* on line */
	else
		status_set("OB");	/* on battery */

	status_commit();

	upsdebugx(5, "ups.status: %s %s\n", ol ? "OL" : "OB", bl ? "BL" : "");

	ser_comm_good();
	dstate_dataok();
}

/* show all possible UPS types */
static void listtypes(void)
{
	int	i;

	printf("Valid UPS types:\n\n");

	for (i = 0; upstab[i].mfr != NULL; i++)
		printf("%i: %s\n", i, upstab[i].desc);
}

/* set the flags for this UPS type */
static void set_ups_type(void)
{
	int	i;

	if (!getval("upstype"))
		fatalx("No upstype set - see help text / man page!\n");
	
	upstype = atoi(getval("upstype"));

	for (i = 0; upstab[i].mfr != NULL; i++)
	{
		if (upstype == i) {
			printf("UPS type: %s\n", upstab[i].desc);
			return;
		}
	}

	printf("\nFatal error: unknown UPS type number\n");

	listtypes();

	exit(EXIT_FAILURE);
}

/* power down the attached load immediately */
void upsdrv_shutdown(void)
{
	int	flags, ret;

	if (upstype == -1)
		fatalx("No upstype set - see help text / man page!");

	flags = upstab[upstype].line_sd;

	if (flags == -1)
		fatalx("No shutdown command defined for this model!");

	if (flags == TIOCM_ST) {

#ifndef HAVE_TCSENDBREAK
		fatalx("Need to send a BREAK, but don't have tcsendbreak!");
#endif

		ret = tcsendbreak(upsfd, 4901);

		if (ret != 0)
			fatal_with_errno("tcsendbreak");

		return;
	}

	ret = ioctl(upsfd, TIOCMSET, &flags);

	if (ret != 0)
		fatal_with_errno("ioctl TIOCMSET");

	if (getval("sdtime")) {
		int	sdtime;

		sdtime = strtol(getval("sdtime"), (char **) NULL, 10);

		upslogx(LOG_INFO, "Holding shutdown signal for %d seconds...\n",
			sdtime);

		sleep(sdtime);
	}
}

void upsdrv_help(void)
{
	listtypes();
}

void upsdrv_banner(void)
{
	printf("Network UPS Tools - Generic UPS driver %s (%s)\n",
		DRV_VERSION, UPS_VERSION);
}

void upsdrv_makevartable(void)
{
	addvar(VAR_VALUE, "upstype", "Set UPS type (required)");
	addvar(VAR_VALUE, "mfr", "Override manufacturer name");
	addvar(VAR_VALUE, "model", "Override model name");
	addvar(VAR_VALUE, "serial", "Specify the serial number");
	addvar(VAR_VALUE, "CP", "Override cable power setting");
	addvar(VAR_VALUE, "OL", "Override on line signal");
	addvar(VAR_VALUE, "LB", "Override low battery signal");
	addvar(VAR_VALUE, "SD", "Override shutdown setting");
	addvar(VAR_VALUE, "sdtime", "Hold time for shutdown value (seconds)");
}

void upsdrv_initups(void)
{
	set_ups_type();

	upsfd = ser_open(device_path);

	if (ioctl(upsfd, TIOCMSET, &upstab[upstype].line_norm))
		fatal_with_errno("ioctl TIOCMSET");
}

void upsdrv_cleanup(void)
{
	ser_close(upsfd, device_path);
}

About Koders | Resources | Downloads | Support | Black Duck | Submit Project | Terms of Service | DMCA | Privacy Policy | Site Map| Contact Us