File: genericups.c

package info (click to toggle)
nut 0.45.5-rel-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,984 kB
  • ctags: 2,146
  • sloc: ansic: 22,216; sh: 1,138; makefile: 405
file content (165 lines) | stat: -rw-r--r-- 3,651 bytes parent folder | download
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
/* 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
*/

#include <sys/ioctl.h>
#include <sys/termios.h>

#include "main.h"
#include "genericups.h"

	int	upstype = -1;

void upsdrv_initinfo(void)
{
	/* setup the basics */

	if (getval("mfr"))
		addinfo(INFO_MFR, getval("mfr"), 0, 0);
	else
		addinfo(INFO_MFR, upstab[upstype].mfr, 0, 0);

	if (getval("model"))
		addinfo(INFO_MODEL, getval("model"), 0, 0);
	else
		addinfo(INFO_MODEL, upstab[upstype].model, 0, 0);

	addinfo(INFO_STATUS, "", 0, 0);
}

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

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

	if (ret != 0) {
		upslog(LOG_INFO, "ioctl failed");
		return;
	}

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

	strcpy(temp, "");

	if (bl)
		strcat(temp, "LB ");	/* low battery */

	if (ol)
		strcat(temp, "OL");	/* on line */
	else
		strcat(temp, "OB");	/* on battery */

	setinfo(INFO_STATUS, "%s", temp);

	writeinfo();
}

/* 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(1);
}			

/* 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!\n");

	open_serial_simple(device_path, 0, upstab[upstype].line_norm);

	upslogx(LOG_INFO, "Initiating UPS shutdown\n");

	flags = upstab[upstype].line_sd;

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

	if (flags == TIOCM_ST) {
#ifdef HAVE_TCSENDBREAK
		tcsendbreak (upsfd, 4901);
#else
		fatalx("Need to send a BREAK, but don't have tcsendbreak!\n");
#endif
	} else {
		ret = ioctl(upsfd, TIOCMSET, &flags);
		if (ret != 0)
			perror("ioctl");
	}
}

void upsdrv_help(void)
{
	listtypes();
}

void upsdrv_banner(void)
{
        printf("Network UPS Tools - Generic UPS driver 0.60 (%s)\n", 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");
}

void upsdrv_initups(void)
{
	set_ups_type();

	open_serial_simple(device_path, 0, upstab[upstype].line_norm);
}

int upsdrv_infomax(void)
{
	return 8;
}