~iheino+ub/+junk/nut-upsconf-docfix

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
370
371
372
373
374
375
376
377
378
379
/* nutdrv_qx_blazer-common.c - Common functions/settings for nutdrv_qx_{mecer,megatec,megatec-old,mustek,q1,voltronic-qs,zinto}.{c,h}
 *
 * Copyright (C)
 *   2013 Daniele Pezzini <hyouko@gmail.com>
 *
 * 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 "main.h"
#include "nutdrv_qx.h"
#include "nutdrv_qx_blazer-common.h"

/* == Ranges == */

/* Range for ups.delay.start */
info_rw_t	blazer_r_ondelay[] = {
	{ "0", 0 },
	{ "599940", 0 },
	{ "", 0 }
};

/* Range for ups.delay.shutdown */
info_rw_t	blazer_r_offdelay[] = {
	{ "12", 0 },
	{ "600", 0 },
	{ "", 0 }
};

/* == Support functions == */

/* This function allows the subdriver to "claim" a device: return 1 if the device is supported by this subdriver, else 0. */
int	blazer_claim(void) {

	/* To tell whether the UPS is supported or not, we'll check both status (Q1/QS/D) and vendor (I/FW?) - provided that we were not told not to do it with the ups.conf flag 'novendor'. */

	item_t	*item = find_nut_info("input.voltage", 0, 0);

	/* Don't know what happened */
	if (!item)
		return 0;

	/* No reply/Unable to get value */
	if (qx_process(item, NULL))
		return 0;

	/* Unable to process value */
	if (ups_infoval_set(item) != 1)
		return 0;

	if (testvar("novendor"))
		return 1;

	/* Vendor */
	item = find_nut_info("ups.firmware", 0, 0);

	/* Don't know what happened */
	if (!item) {
		dstate_delinfo("input.voltage");
		return 0;
	}

	/* No reply/Unable to get value */
	if (qx_process(item, NULL)) {
		dstate_delinfo("input.voltage");
		return 0;
	}

	/* Unable to process value */
	if (ups_infoval_set(item) != 1) {
		dstate_delinfo("input.voltage");
		return 0;
	}

	return 1;

}

/* This function allows the subdriver to "claim" a device: return 1 if the device is supported by this subdriver, else 0.
 * NOTE: this 'light' version only checks for status (Q1/QS/D/..) */
int	blazer_claim_light(void) {

	/* To tell whether the UPS is supported or not, we'll check just status (Q1/QS/D/..). */

	item_t	*item = find_nut_info("input.voltage", 0, 0);

	/* Don't know what happened */
	if (!item)
		return 0;

	/* No reply/Unable to get value */
	if (qx_process(item, NULL))
		return 0;

	/* Unable to process value */
	if (ups_infoval_set(item) != 1)
		return 0;

	return 1;

}

/* Subdriver-specific flags/vars */
void	blazer_makevartable(void)
{
	addvar(VAR_FLAG, "norating", "Skip reading rating information from UPS");
	addvar(VAR_FLAG, "novendor", "Skip reading vendor information from UPS");
}

/* Subdriver-specific initups */
void	blazer_initups(item_t *qx2nut)
{
	int	nr, nv;
	item_t	*item;

	nr = testvar("norating");
	nv = testvar("novendor");

	if (!nr && !nv)
		return;

	for (item = qx2nut; item->info_type != NULL; item++) {

		if (!item->command)
			continue;

		/* norating */
		if (nr && !strcasecmp(item->command, "F\r")) {
			upsdebugx(2, "%s: skipping %s", __func__, item->info_type);
			item->qxflags |= QX_FLAG_SKIP;
		}

		/* novendor */
		if (nv && (!strcasecmp(item->command, "I\r") || !strcasecmp(item->command, "FW?\r"))) {
			upsdebugx(2, "%s: skipping %s", __func__, item->info_type);
			item->qxflags |= QX_FLAG_SKIP;
		}

	}
}

/* == Preprocess functions == */

/* Preprocess setvars */
int	blazer_process_setvar(item_t *item, char *value, size_t valuelen)
{
	if (!strlen(value)) {
		upsdebugx(2, "%s: value not given for %s", __func__, item->info_type);
		return -1;
	}

	if (!strcasecmp(item->info_type, "ups.delay.start")) {

		int	ondelay = strtol(value, NULL, 10);

		/* Truncate to minute */
		ondelay -= (ondelay % 60);

		snprintf(value, valuelen, "%d", ondelay);

	} else if (!strcasecmp(item->info_type, "ups.delay.shutdown")) {

		int	offdelay = strtol(value, NULL, 10);

		/* Truncate to nearest settable value */
		if (offdelay < 60) {
			offdelay -= (offdelay % 6);
		} else {
			offdelay -= (offdelay % 60);
		}

		snprintf(value, valuelen, "%d", offdelay);

	} else {

		/* Don't know what happened */
		return -1;

	}

	return 0;
}

/* Preprocess instant commands */
int	blazer_process_command(item_t *item, char *value, size_t valuelen)
{
	if (!strcasecmp(item->info_type, "shutdown.return")) {

		/* Sn: Shutdown after n minutes and then turn on when mains is back
		 * SnRm: Shutdown after n minutes and then turn on after m minutes
		 * Accepted values for n: .2 -> .9 , 01 -> 10
		 * Accepted values for m: 0001 -> 9999
		 * Note: "S01R0001" and "S01R0002" may not work on early (GE) firmware versions.
		 * The failure mode is that the UPS turns off and never returns.
		 * The fix is to push the return value up by 2, i.e. S01R0003, and it will return online properly.
		 * (thus the default of ondelay=3 mins) */

		int	offdelay = strtol(dstate_getinfo("ups.delay.shutdown"), NULL, 10),
			ondelay = strtol(dstate_getinfo("ups.delay.start"), NULL, 10) / 60;
		char	buf[SMALLBUF] = "";

		if (ondelay == 0) {

			if (offdelay < 60) {
				snprintf(buf, sizeof(buf), ".%d", offdelay / 6);
			} else {
				snprintf(buf, sizeof(buf), "%02d", offdelay / 60);
			}

		} else if (offdelay < 60) {

			snprintf(buf, sizeof(buf), ".%dR%04d", offdelay / 6, ondelay);

		} else {

			snprintf(buf, sizeof(buf), "%02dR%04d", offdelay / 60, ondelay);

		}

		snprintf(value, valuelen, item->command, buf);

	} else if (!strcasecmp(item->info_type, "shutdown.stayoff")) {

		/* SnR0000
		 * Shutdown after n minutes and stay off
		 * Accepted values for n: .2 -> .9 , 01 -> 10 */

		int	offdelay = strtol(dstate_getinfo("ups.delay.shutdown"), NULL, 10);
		char	buf[SMALLBUF] = "";

		if (offdelay < 60) {
			snprintf(buf, sizeof(buf), ".%d", offdelay / 6);
		} else {
			snprintf(buf, sizeof(buf), "%02d", offdelay / 60);
		}

		snprintf(value, valuelen, item->command, buf);

	} else if (!strcasecmp(item->info_type, "test.battery.start")) {

		int	delay = strlen(value) > 0 ? strtol(value, NULL, 10) : 600;

		if ((delay < 60) || (delay > 5940)) {
			upslogx(LOG_ERR, "%s: battery test time '%d' out of range [60..5940] seconds", item->info_type, delay);
			return -1;
		}

		delay = delay / 60;

		snprintf(value, valuelen, item->command, delay);

	} else {

		/* Don't know what happened */
		return -1;

	}

	return 0;
}

/* Process status bits */
int	blazer_process_status_bits(item_t *item, char *value, size_t valuelen)
{
	char	*val = "";

	if (strspn(item->value, "01") != 1) {
		upsdebugx(3, "%s: unexpected value %s@%d->%s", __func__, item->value, item->from, item->value);
		return -1;
	}

	switch (item->from)
	{
	case 38:	/* Utility Fail (Immediate) */

		if (item->value[0] == '1')
			val = "!OL";
		else
			val = "OL";
		break;

	case 39:	/* Battery Low */

		if (item->value[0] == '1')
			val = "LB";
		else
			val = "!LB";
		break;

	case 40:	/* Bypass/Boost or Buck Active */

		if (item->value[0] == '1') {

			double	vi, vo;

			vi = strtod(dstate_getinfo("input.voltage"), NULL);
			vo = strtod(dstate_getinfo("output.voltage"), NULL);

			if (vo < 0.5 * vi) {
				upsdebugx(2, "%s: output voltage too low", __func__);
				return -1;
			} else if (vo < 0.95 * vi) {
				val = "TRIM";
			} else if (vo < 1.05 * vi) {
				val = "BYPASS";
			} else if (vo < 1.5 * vi) {
				val = "BOOST";
			} else {
				upsdebugx(2, "%s: output voltage too high", __func__);
				return -1;
			}

		}

		break;

	case 41:	/* UPS Failed - ups.alarm */

		if (item->value[0] == '1')
			val = "UPS selftest failed!";
		break;

	case 42:	/* UPS Type - ups.type */

		if (item->value[0] == '1')
			val = "offline / line interactive";
		else
			val = "online";
		break;

	case 43:	/* Test in Progress */

		if (item->value[0] == '1')
			val = "CAL";
		else
			val = "!CAL";
		break;

	case 44:	/* Shutdown Active */

		if (item->value[0] == '1') {
			if (!strcasecmp(item->info_type, "ups.status"))
				val = "FSD";
			else	/* ups.alarm */
				val = "Shutdown imminent!";
		} else if (!strcasecmp(item->info_type, "ups.status")) {
			val = "!FSD";
		}
		break;

	case 45:	/* Beeper status - ups.beeper.status */

		if (item->value[0] == '1')
			val = "enabled";
		else
			val = "disabled";
		break;

	default:
		/* Don't know what happened */
		return -1;
	}

	snprintf(value, valuelen, "%s", val);

	return 0;
}