~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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/* belkin.c - model specific routines for Belkin Smart-UPS units.

   Copyright (C) 2000 Marcus Müller <marcus@ebootis.de>

   based on:

   apcsmart.c - model specific routines for APC smart protocol units

   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 "main.h"
#include "serial.h"

#include "belkin.h"

#define DRV_VERSION "0.21"

static void send_belkin_command(char cmd, const char *subcmd, const char *data)
{
	ser_send(upsfd, "~00%c%03d%s%s", cmd, strlen(data) + 3, subcmd, data);
}

static int init_communication(void)
{
	int	i;
	int	res;
	char	temp[SMALLBUF];

	res = -1;
	for (i = 1; i <= 10 && res == -1; i++) {
		send_belkin_command(STATUS,MANUFACTURER,"");
		res = get_belkin_reply(temp);
	}

	if (res == -1 || strcmp(temp,"BELKIN")) 
		return res;

	return 0;
}

static char *get_belkin_field(const char *in, char *out, size_t outlen, 
	size_t num)
{
	size_t	i, c = 1;
	char	*ptr;

	/* special case */
	if (num == 1) {
		snprintf(out, outlen, "%s", in);
		ptr = strchr(out, ';');

		if (ptr)
			*ptr = '\0';

		return out;
	}		

	for (i = 0; i < strlen(in); i++) {
		if (in[i] == ';')
			c++;

		if (c == num) {
			snprintf(out, outlen, "%s", &in[i + 1]);
			ptr = strchr(out, ';');

			if (ptr)
				*ptr = '\0';
			return out;
		}
	}

	return NULL;
}

static int do_status(void)
{
	char	temp[SMALLBUF], st[SMALLBUF];
	int	res;

	send_belkin_command(STATUS,STAT_STATUS,"");
	res = get_belkin_reply(temp);
	if (res == -1) {
		dstate_datastale();
		return 0;
	}

	status_init();

	get_belkin_field(temp, st, sizeof(st), 6);
	if (*st == '1') {
		status_set("OFF");

	} else {	/* (OFF) and (OB | OL) are mutually exclusive */

		get_belkin_field(temp, st, sizeof(st), 2);
		if (*st == '1') {
			status_set("OB");

			send_belkin_command(STATUS,STAT_BATTERY,"");
			res = get_belkin_reply(temp);

			if (res == -1) {
				dstate_datastale();
				return 0;
			}

			get_belkin_field(temp, st, sizeof(st), 10);
			res = atoi(st);
			get_belkin_field(temp, st, sizeof(st), 2);

			if (*st == '1' || res < LOW_BAT) 
				status_set("LB");	/* low battery */
		}
		else
			status_set("OL");	/* on line */
	}

	status_commit();
	dstate_dataok();

	return 1;
}

static int do_broken_rat(char *buf)
{
	int	ret, cnt;
	char	tmp[8];

	usleep(25000);

	ret = ser_get_buf_len(upsfd, tmp, 7, 3, 0);

	if (ret != 7)
		return -1;

	tmp[7] = '\0';
	cnt = atoi(tmp + 4);

	if ((cnt < 1) || (cnt > 255))
		return -1;

	usleep(5000 * cnt);

	/* firmware 001 only sends 50 bytes instead of the proper 53 */
	if (cnt == 53)
		cnt = 50;

	ret = ser_get_buf_len(upsfd, buf, 50, cnt, 0);
	buf[cnt] = 0;

	return ret;
}	

static int init_ups_data(void)
{
	int	res;
	double	low, high;
	char	temp[SMALLBUF], st[SMALLBUF];

	send_belkin_command(STATUS, MODEL, "");
	res = get_belkin_reply(temp);
	if (res == -1)
		return res;

	dstate_setinfo("ups.model", "%s", temp);

	send_belkin_command(STATUS, VERSION, "");
	res = get_belkin_reply(temp);
	if (res == -1)
		return res;

	dstate_setinfo("ups.firmware", "%s", temp);

	/* deal with stupid firmware that breaks RAT */

	send_belkin_command(STATUS, RATING, "");

	if (!strcmp(temp, "001"))
		res = do_broken_rat(temp);
	else
		res = get_belkin_reply(temp);

	if (res > 0) {
		get_belkin_field(temp, st, sizeof(st), 8);
		low = atof(st) / 0.88;

		get_belkin_field(temp, st, sizeof(st), 9);
		high = atof(st) * 0.88;

		dstate_setinfo("input.transfer.low", "%03.1f", low);
		dstate_setinfo("input.transfer.high", "%03.1f", high);
	}

	tcflush(upsfd,TCIOFLUSH);

	dstate_addcmd("load.off");
	dstate_addcmd("load.on");
	upsdrv_updateinfo();
	return 0;
}

/* normal idle loop - keep up with the current state of the UPS */
void upsdrv_updateinfo(void)
{
	int	res;
	double	val;
	char	temp[SMALLBUF], st[SMALLBUF];

	if (!do_status())
		return;

	send_belkin_command(STATUS, STAT_INPUT, "");
	res = get_belkin_reply(temp);
	if (res == -1)
		return;

	get_belkin_field(temp, st, sizeof(st), 3);
	val = atof(st) / 10;
	dstate_setinfo("input.voltage", "%05.1f", val);

	get_belkin_field(temp, st, sizeof(st), 2);
	val = atof(st) / 10;
	dstate_setinfo("input.frequency", "%.1f", val);

	send_belkin_command(STATUS,STAT_BATTERY, "");
	res = get_belkin_reply(temp);
	if (res == -1)
		return;

	get_belkin_field(temp, st, sizeof(st), 10);
	val = atof(st);
	dstate_setinfo("battery.charge", "%03.0f", val);

	get_belkin_field(temp, st, sizeof(st), 9);
	val = atof(st);
	dstate_setinfo("battery.temperature", "%03.0f", val);

	get_belkin_field(temp, st, sizeof(st), 7);
	val = atof(st) / 10;
	dstate_setinfo("battery.voltage", "%4.1f", val);

	get_belkin_field(temp, st, sizeof(st), 9);
	val = atof(st);
	dstate_setinfo("ups.temperature", "%03.0f", val);

	send_belkin_command(STATUS, STAT_OUTPUT, "");
	res = get_belkin_reply(temp);
	if (res == -1)
		return;

	get_belkin_field(temp, st, sizeof(st), 2);
	val = atof(st) / 10;
	dstate_setinfo("output.frequency", "%.1f", val);

	get_belkin_field(temp, st, sizeof(st), 4);
	val = atof(st) / 10;
	dstate_setinfo("output.voltage", "%05.1f", val);

	get_belkin_field(temp, st, sizeof(st), 7);
	val = atof(st);
	dstate_setinfo("ups.load", "%03.0f", val);
}

static int get_belkin_reply(char *buf)
{
	int	ret, cnt;
	char	tmp[8];

	usleep(25000);

	/* pull first 7 bytes to get data length - like ~00S004 */
	ret = ser_get_buf_len(upsfd, tmp, 7, 3, 0);

	if (ret != 7) {
		ser_comm_fail("Initial read returned %d bytes", ret);
		return -1;
	}

	tmp[7] = 0;
	cnt = atoi(tmp + 4);

	if ((cnt < 1) || (cnt > 255))
		return -1;

	/* give it time to respond to us */
	usleep(5000 * cnt);

	ret = ser_get_buf_len(upsfd, buf, cnt, 3, 0);

	buf[cnt] = 0;

	if (ret != cnt) {
		ser_comm_fail("Second read returned %d bytes, expected %d",
			ret, cnt);
		return -1;
	}

	ser_comm_good();

	return ret;
}

/* power down the attached load immediately */
void upsdrv_shutdown(void)
{
	int	res;

	res = init_communication();
	if (res == -1)
		printf("Detection failed.  Trying a shutdown command anyway.\n");

	/* tested on a F6C525-SER: this works when OL and OB */

	/* shutdown type 2 (UPS system) */
	send_belkin_command(CONTROL, "SDT", "2");

	/* SDR means "do SDT and SDA, then reboot after n minutes" */
	send_belkin_command(CONTROL, "SDR", "1");

	printf("UPS should power off load in 5 seconds\n");

	/* shutdown in 5 seconds */
	send_belkin_command(CONTROL, "SDA", "5");
}

/* handle the "load.off" with some paranoia */
static void do_off(void)
{
	static	time_t lastcmd = 0;
	time_t	now, elapsed;
#ifdef CONFIRM_DANGEROUS_COMMANDS
	time(&now);
	elapsed = now - lastcmd;

	/* reset the timer every call - this means if you call it too      *
	 * early, then you have to wait MINCMDTIME again before sending #2 */
	lastcmd = now;

	if ((elapsed < MINCMDTIME) || (elapsed > MAXCMDTIME)) {

		/* FUTURE: tell the user (via upsd) to try it again */
		return;
	}
#endif

	upslogx(LOG_INFO, "Sending powerdown command to UPS\n");
	send_belkin_command(CONTROL,POWER_OFF,"1;1");
	usleep(1500000);
	send_belkin_command(CONTROL,POWER_OFF,"1;1");
}

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

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

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

static void set_serialDTR0RTS1(void)
{
	int dtr_bit = TIOCM_DTR;
	int rts_bit = TIOCM_RTS;

	/* set DTR to low and RTS to high */
	ioctl(upsfd, TIOCMBIC, &dtr_bit);
	ioctl(upsfd, TIOCMBIS, &rts_bit);
}

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

void upsdrv_help(void)
{
}

void upsdrv_makevartable(void)
{
}

/* prep the serial port */
void upsdrv_initups(void)
{
	upsfd = ser_open(device_path);
	ser_set_speed(upsfd, device_path, B2400);
	
	set_serialDTR0RTS1();

	sleep(1);
  	tcflush(upsfd,TCIOFLUSH);
}

void upsdrv_initinfo(void)
{
	int	res;

	res = init_communication();
	if (res == -1) {
		printf("Unable to detect an Belkin Smart protocol UPS on port %s\n", 
			device_path);
		printf("Check the cabling, port name or model name and try again\n");
		exit(EXIT_FAILURE);
	}

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

	/* see what's out there */
	init_ups_data();

	printf("Detected %s on %s\n", dstate_getinfo("ups.model"),
		device_path);

	upsh.instcmd = instcmd;
}

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