~ubuntu-branches/ubuntu/saucy/nut/saucy

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
/* etapro.c - model specific routines for ETA UPS

   Copyright (C) 2002  Marek Michalkiewicz  <marekm@amelek.gda.pl>

   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
 */

/*
   This driver is for the ETA UPS (http://www.eta.com.pl/) with the
   "PRO" option (available at small added cost, highly recommended).
   All units (even without that option) should also work in "dumb"
   mode with the genericups driver (type 7 or 10), but in that mode
   shutdown only works when running on battery.

   Tested with ETA mini+UPS 720 PRO.  Thanks to ETA for help with
   protocol documentation, no free UPS though (but they still can
   send me another one if they like me ;-).

   Shutdown should work even when on line, so this should help avoid
   power races (system remaining in halted or "ATX standby" state,
   requiring manual intervention).  Delay from power off to power on
   can be set in software, currently hardcoded to 15 seconds.

   Instant commands CMD_OFF and CMD_ON should work (not tested yet).
   Be careful with CMD_OFF - it turns off the load after one second.

   Known issues:
    - larger units (>= 1000VA) have a 24V battery, so the battery
      voltage reported should be multiplied by 2 if the model
      string indicates such a larger unit.
    - load percentage is only measured when running on battery, and
      is reported as 0 when on line.  This seems to be a hardware
      limitation of the UPS, so we can't do much about it...
    - UPS does not provide any remaining battery charge (or time at
      current load) information, but we should be able to estimate it
      based on battery voltage, load percentage and UPS model.
    - error handling not tested (we assume that the UPS is always
      correctly connected to the serial port).
 */

#define DRV_VERSION "0.03"

#include <sys/ioctl.h>
#include "main.h"
#include "serial.h"

static int
etapro_get_response(const char *resp_type)
{
	char tmp[256];
	char *cp;
	unsigned int n, val;

	/* Read until a newline is found or there is no room in the buffer.
	   Unlike ser_get_line(), don't discard the following characters
	   because we have to handle multi-line responses.  */
	n = 0;
	while (ser_get_char(upsfd, &tmp[n], 1, 0) == 1) {
		if (n >= sizeof(tmp) - 1 || tmp[n] == '\n')
			break;
		n++;
	}
	tmp[n] = '\0';
	if (n == 0) {
		upslogx(LOG_ERR, "no response from UPS");
		return -1;
	}
	/* Search for start of response (skip any echoed back command).  */
	cp = strstr(tmp, resp_type);
	if (!cp || *cp == '\0' || cp[strlen(cp) - 1] != '\r') {
		upslogx(LOG_ERR, "bad response (%s)", tmp);
		return -1;
	}
	cp[strlen(cp) - 1] = '\0';  /* remove the CR */

	switch (cp[1]) {
		/* Handle ASCII text responses directly here.  */
	case 'R':
		dstate_setinfo("ups.mfr", "%s", cp + 2);
		return 0;
	case 'S':
		dstate_setinfo("ups.model", "%s", cp + 2);
		return 0;
	case 'T':
		dstate_setinfo("ups.mfr.date", "%s", cp + 2);
		return 0;
	}
	/* Handle all other responses as hexadecimal numbers.  */
	val = 0;
	if (sscanf(cp + 2, "%x", &val) != 1) {
		upslogx(LOG_ERR, "bad response format (%s)", tmp);
		return -1;
	}
	return val;
}

static void
etapro_set_on_timer(int seconds)
{
	int x;

	if (seconds == 0) {  /* cancel the running timer */
		ser_send(upsfd, "RS\r");
		x = etapro_get_response("SV");
		if (x == 0x30)
			return;  /* OK */
	} else {
		if (seconds > 0x7fff) {  /* minutes */
			seconds = (seconds + 59) / 60;
			if (seconds > 0x7fff)
				seconds = 0x7fff;
			printf("UPS on in %d minutes\n", seconds);
			seconds |= 0x8000;
		} else {
			printf("UPS on in %d seconds\n", seconds);
		}

		ser_send(upsfd, "RN%04X\r", seconds);
		x = etapro_get_response("SV");
		if (x == 0x20)
			return;  /* OK */
	}
	upslogx(LOG_ERR, "etapro_set_on_timer: error, status=0x%02x", x);
}

static void
etapro_set_off_timer(int seconds)
{
	int x;

	if (seconds == 0) {  /* cancel the running timer */
		ser_send(upsfd, "RR\r");
		x = etapro_get_response("SV");
		if (x == 0x10)
			return;  /* OK */
	} else {
		if (seconds > 0x7fff) {  /* minutes */
			seconds /= 60;
			if (seconds > 0x7fff)
				seconds = 0x7fff;
			printf("UPS off in %d minutes\n", seconds);
			seconds |= 0x8000;
		} else {
			printf("UPS off in %d seconds\n", seconds);
		}

		ser_send(upsfd, "RO%04X\r", seconds);
		x = etapro_get_response("SV");
		if (x == 0)
			return;  /* OK */
	}
	upslogx(LOG_ERR, "etapro_set_off_timer: error, status=0x%02x", x);
}

static int instcmd(const char *cmdname, const char *extra)
{
	if (!strcasecmp(cmdname, "load.off")) {
		etapro_set_off_timer(1);
		return STAT_INSTCMD_HANDLED;
	}

	if (!strcasecmp(cmdname, "load.on")) {
		etapro_set_on_timer(1);
		return STAT_INSTCMD_HANDLED;
	}

	if (!strcasecmp(cmdname, "shutdown.return")) {
		upsdrv_shutdown();
		return STAT_INSTCMD_HANDLED;
	}

	upslogx(LOG_NOTICE, "instcmd: unknown command [%s]", cmdname);
	return STAT_INSTCMD_UNKNOWN;
}

void
upsdrv_initinfo(void)
{
	dstate_addcmd("load.off");
	dstate_addcmd("load.on");
	dstate_addcmd("shutdown.return");

	/* First command after power on returns junk - ignore it.  */
	ser_send(upsfd, "RI\r");
	sleep(1);

	upsdrv_updateinfo();

	upsh.instcmd = instcmd;
}

void
upsdrv_updateinfo(void)
{
	char status[256];
	int x, flags;
	double utility, outvolt, battvolt, loadpct;

	ser_flush_in(upsfd, "", nut_debug_level);
	ser_send(upsfd, "RI\r");  /* identify */

	x = etapro_get_response("SR");  /* manufacturer */
	if (x < 0) {
		dstate_datastale();
		return;
	}
	x = etapro_get_response("SS");  /* model */
	if (x < 0) {
		dstate_datastale();
		return;
	}
	x = etapro_get_response("ST");  /* mfr date */
	if (x < 0) {
		dstate_datastale();
		return;
	}
	x = etapro_get_response("SU");  /* UPS ident */
	if (x < 0) {
		dstate_datastale();
		return;
	}

	ser_send(upsfd, "RP\r");  /* read measurements */

	x = etapro_get_response("SO");  /* status flags */
	if (x < 0) {
		dstate_datastale();
		return;
	}
	flags = x;

	x = etapro_get_response("SG");  /* input voltage, 0xFF = 270V */
	if (x < 0) {
		dstate_datastale();
		return;
	}
	utility = (270.0 / 255) * x;

	x = etapro_get_response("SH");  /* output voltage, 0xFF = 270V */
	if (x < 0) {
		dstate_datastale();
		return;
	}
	outvolt = (270.0 / 255) * x;

	x = etapro_get_response("SI");  /* battery voltage, 0xFF = 14V */
	if (x < 0) {
		dstate_datastale();
		return;
	}

	/* TODO: >= 1000VA models have a 24V battery (max 28V) - check
	   the model string returned by the RI command.  */
	battvolt = (14.0 / 255) * x;

	x = etapro_get_response("SL");  /* load (on battery), 0xFF = 150% */
	if (x < 0) {
		dstate_datastale();
		return;
	}
	loadpct = (150.0 / 255) * x;

	x = etapro_get_response("SN");  /* time running on battery */
	if (x < 0) {
		dstate_datastale();
		return;
	}
	/* This is the time how long the UPS has been running on battery
	   (in seconds, reset to zero after power returns), but there
	   seems to be no variable defined for this yet...  */

	status_init();

	status[0] = '\0';
	if (!(flags & 0x02))
		status_set("OFF");
	else if (flags & 0x01)
		status_set("OL");
	else
		status_set("OB");

	if (!(flags & 0x04))
		status_set("LB");

	/* TODO bit 3: 1 = ok, 0 = fault */

	if (flags & 0x10)
		status_set("BOOST");

	if (loadpct > 100.0)
		status_set("OVER");

	/* Battery voltage out of range (lower than LB, or too high).  */
	if (flags & 0x20)
		status_set("RB");

	/* TODO bit 6: 1 = charging, 0 = full */

	status_commit();

	dstate_setinfo("input.voltage", "%03.1f", utility);
	dstate_setinfo("output.voltage", "%03.1f", outvolt);
	dstate_setinfo("battery.voltage", "%02.2f", battvolt);
	dstate_setinfo("ups.load", "%03.1f", loadpct);

	dstate_dataok();
}

/* TODO: delays should be tunable, the UPS supports max 32767 minutes.  */

/* Shutdown command to off delay in seconds.  */
#define SHUTDOWN_GRACE_TIME 10

/* Shutdown to return delay in seconds.  */
#define SHUTDOWN_TO_RETURN_TIME 15

void
upsdrv_shutdown(void)
{
	etapro_set_on_timer(SHUTDOWN_GRACE_TIME + SHUTDOWN_TO_RETURN_TIME);
	etapro_set_off_timer(SHUTDOWN_GRACE_TIME);
}

void
upsdrv_help(void)
{
}

void
upsdrv_makevartable(void)
{
}

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

void
upsdrv_initups(void)
{
	int dtr_bit = TIOCM_DTR;
	int rts_bit = TIOCM_RTS;

	upsfd = ser_open(device_path);
	ser_set_speed(upsfd, device_path, B1200);

	ioctl(upsfd, TIOCMBIC, &dtr_bit);
	ioctl(upsfd, TIOCMBIS, &rts_bit);
}

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