-
-
Notifications
You must be signed in to change notification settings - Fork 374
/
Copy pathgenericups.c
340 lines (254 loc) · 7.46 KB
/
genericups.c
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
/* genericups.c - support for generic contact-closure UPS models
Copyright (C) 1999 Russell Kroll <[email protected]>
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 "main.h"
#include "serial.h"
#include "genericups.h"
#define DRIVER_NAME "Generic contact-closure UPS driver"
#define DRIVER_VERSION "1.36"
/* driver description structure */
upsdrv_info_t upsdrv_info = {
DRIVER_NAME,
DRIVER_VERSION,
"Russell Kroll <[email protected]>",
DRV_STABLE,
{ NULL }
};
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(EXIT_FAILURE, "Can't override output with CTS (not an output)");
}
if (strstr(value, "DCD")) {
fatalx(EXIT_FAILURE, "Can't override output with DCD (not an output)");
}
if (strstr(value, "RNG")) {
fatalx(EXIT_FAILURE, "Can't override output with RNG (not an output)");
}
if (strstr(value, "DSR")) {
fatalx(EXIT_FAILURE, "Can't override output with DSR (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, "DSR")) {
*line |= TIOCM_DSR;
if (!strstr(value, "-DSR")) {
*val |= TIOCM_DSR;
}
}
if (strstr(value, "DTR")) {
fatalx(EXIT_FAILURE, "Can't override input with DTR (not an input)");
}
if (strstr(value, "RTS")) {
fatalx(EXIT_FAILURE, "Can't override input with RTS (not an input)");
}
if (strstr(value, "ST")) {
fatalx(EXIT_FAILURE, "Can't override input with ST (not an input)");
}
}
void upsdrv_initinfo(void)
{
char *v;
/* 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);
}
/*
User wants to override the input signal definitions. See also upsdrv_initups().
*/
if ((v = getval("OL")) != NULL) {
parse_input_signals(v, &upstab[upstype].line_ol, &upstab[upstype].val_ol);
upsdebugx(2, "parse_input_signals: OL overridden 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 overridden 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(EXIT_FAILURE, "No upstype set - see help text / man page!");
}
upstype = atoi(getval("upstype"));
for (i = 0; upstab[i].mfr != NULL; i++) {
if (upstype == i) {
upslogx(LOG_INFO, "UPS type: %s\n", upstab[i].desc);
return;
}
}
listtypes();
fatalx(EXIT_FAILURE, "\nFatal error: unknown UPS type number");
}
/* power down the attached load immediately */
void upsdrv_shutdown(void)
{
int flags, ret;
if (upstype == -1) {
fatalx(EXIT_FAILURE, "No upstype set - see help text / man page!");
}
flags = upstab[upstype].line_sd;
if (flags == -1) {
fatalx(EXIT_FAILURE, "No shutdown command defined for this model!");
}
if (flags == TIOCM_ST) {
#ifndef HAVE_TCSENDBREAK
fatalx(EXIT_FAILURE, "Need to send a BREAK, but don't have tcsendbreak!");
#endif
ret = tcsendbreak(upsfd, 4901);
if (ret != 0) {
fatal_with_errno(EXIT_FAILURE, "tcsendbreak");
}
return;
}
ret = ioctl(upsfd, TIOCMSET, &flags);
if (ret != 0) {
fatal_with_errno(EXIT_FAILURE, "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_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)");
}
int upsdrv_initups(void)
{
struct termios tio;
char *v;
set_ups_type();
upsfd = ser_open(device_path);
if (tcgetattr(upsfd, &tio)) {
fatal_with_errno(EXIT_FAILURE, "tcgetattr");
}
/* don't hang up on last close */
tio.c_cflag &= ~HUPCL;
if (tcsetattr(upsfd, TCSANOW, &tio)) {
fatal_with_errno(EXIT_FAILURE, "tcsetattr");
}
/*
See if the user wants to override the output signal definitions
this must be done here, since we might go to upsdrv_shutdown()
immediately. Input signal definition override is handled in
upsdrv_initinfo()
*/
if ((v = getval("CP")) != NULL) {
parse_output_signals(v, &upstab[upstype].line_norm);
upsdebugx(2, "parse_output_signals: CP overridden with %s\n", v);
}
if ((v = getval("SD")) != NULL) {
parse_output_signals(v, &upstab[upstype].line_sd);
upsdebugx(2, "parse_output_signals: SD overridden with %s\n", v);
}
if (ioctl(upsfd, TIOCMSET, &upstab[upstype].line_norm)) {
fatal_with_errno(EXIT_FAILURE, "ioctl TIOCMSET");
}
return 1;
}
void upsdrv_cleanup(void)
{
ser_close(upsfd, device_path);
}