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

« back to all changes in this revision

Viewing changes to drivers/energizerups.c

  • Committer: Bazaar Package Importer
  • Author(s): Arnaud Quette
  • Date: 2004-05-28 13:10:01 UTC
  • mto: (16.1.1 squeeze)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20040528131001-yj2m9qcez4ya2w14
Tags: upstream-1.4.2
ImportĀ upstreamĀ versionĀ 1.4.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* energizerups.c - model specific routines for Energizer USB units
 
2
 
 
3
   Copyright (C) 2003  Viktor T. Toth <vttoth@vttoth.com>
 
4
 
 
5
   Based in part on the fentonups.c driver by Russell Kroll:
 
6
 
 
7
   Copyright (C) 1999  Russell Kroll <rkroll@exploits.org>
 
8
 
 
9
   This program is free software; you can redistribute it and/or modify
 
10
   it under the terms of the GNU General Public License as published by
 
11
   the Free Software Foundation; either version 2 of the License, or
 
12
   (at your option) any later version.
 
13
 
 
14
   This program is distributed in the hope that it will be useful,
 
15
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
   GNU General Public License for more details.
 
18
 
 
19
   You should have received a copy of the GNU General Public License
 
20
   along with this program; if not, write to the Free Software
 
21
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
22
*/
 
23
 
 
24
#include <asm/types.h>
 
25
#include <sys/ioctl.h>
 
26
#include <linux/hiddev.h>
 
27
#include <fcntl.h>
 
28
#include <stdio.h>
 
29
#include <signal.h>
 
30
#include <sys/select.h>
 
31
 
 
32
#include "main.h"
 
33
 
 
34
#define DRV_VERSION     "0.01"
 
35
 
 
36
#define NUM_EVTS 64
 
37
 
 
38
/* Response to I identification queries is in the following format:
 
39
 *
 
40
 * 012345678901234567890123456789012345678
 
41
 * #Energizer       ER-HMOF600 A0        ^M
 
42
 * #Energizer       ER-OF800   A0        ^M
 
43
 */
 
44
#define MANUFR  (buf+1)
 
45
#define MDLNUM  (buf+17)
 
46
#define HWVERS  (buf+28)
 
47
 
 
48
/* Response to Q1 queries is in the following format:
 
49
 *
 
50
 * 01234567890123456789012345678901234567890123456
 
51
 * (118.6 118.6 118.6 020 60.0 13.8 32.0 00001000^M
 
52
 */
 
53
#define INVOLT  (buf+1)
 
54
#define OUTVOLT (buf+13)
 
55
#define LOADPCT (buf+19)
 
56
#define ACFREQ  (buf+23)
 
57
#define BATVOLT (buf+28)
 
58
#define UPSTEMP (buf+33)
 
59
#define STATUS  (buf+38)
 
60
 
 
61
#define OBFLAG  (STATUS[0]=='1'||STATUS[5]=='1')
 
62
#define LBFLAG  (STATUS[1]=='1')
 
63
#define BYPASS  (STATUS[2]=='1')
 
64
#define BEEPER  (STATUS[7]=='1')
 
65
 
 
66
/* A calibration test of the ER-HMOF600 shows its maximum voltage at
 
67
 * 14.0V during charging, falling down to 13.6 if the UPS is turned off
 
68
 * for three minutes (but connected to line power) and to 12.8V
 
69
 * immediately after starting a test with a 60W load. The 'low battery'
 
70
 * warning came at 11.0V, and a discharge time to LB with a 60W light
 
71
 * bulb took 35 minutes. The UPS ran another 5 minutes on LB before
 
72
 * shutting down, at which time the battery voltage was 10.1V. When
 
73
 * charging resumed at the moment the LB warning came on, the battery
 
74
 * started at 12.0V.
 
75
 *
 
76
 * With my ER-OF800, the maximum voltage is 13.8V. The difference is
 
77
 * probably within circuit tolerance and not due to a substantial design
 
78
 * difference between the units.
 
79
 *
 
80
 * For now, these are the only two units I have. Both are 115VAC units
 
81
 * with a 12V internal battery and an identical software interface. The
 
82
 * characteristic AC voltage values here are reasonable guesses based on
 
83
 * the values of other similar UPSs.
 
84
 */
 
85
 
 
86
#define LOWVOLT (OBFLAG?11.0:12.0)
 
87
#define VOLTRNG 1.8
 
88
#define LOXFER  84
 
89
#define LONORM  98
 
90
#define HINORM  126
 
91
#define HIXFER  142
 
92
 
 
93
#define hidsend(x) hidcmd(x,NULL,0)
 
94
 
 
95
 
 
96
/*
 
97
 * Encode and send a string to the UPS. A trailing <cr> (0x0D) is appended.
 
98
 *
 
99
 * Commands are encoded in 8-byte reports, each consisting of 64 settable
 
100
 * bits. The last report is padded with null bits, just to be safe and not
 
101
 * send the UPS garbage by accident.
 
102
 */
 
103
 
 
104
void sendstring(int fd, char *psz)
 
105
{
 
106
        struct hiddev_report_info rinfo;
 
107
        struct hiddev_usage_ref uref;
 
108
        int i, j;
 
109
        unsigned char c;
 
110
 
 
111
        uref.usage_index = 0;
 
112
        i = 0;
 
113
 
 
114
        do
 
115
        {
 
116
                c = (*psz) ? *psz :0x0D;
 
117
                for (j = 0; j < 8; j++)
 
118
                {
 
119
                        uref.report_type = HID_REPORT_TYPE_OUTPUT;
 
120
                        uref.report_id = 0;
 
121
                        uref.field_index = 0;
 
122
                        uref.usage_code = 0x90001 + uref.usage_index;
 
123
                        uref.value = (c & 1);
 
124
                        if (ioctl(fd, HIDIOCSUSAGE, &uref) < 0)
 
125
                                fatalx("Error (SUSAGE) while talking to the UPS");
 
126
                        uref.usage_index++;
 
127
                        c >>= 1;
 
128
                }
 
129
                if (*psz == 0 || ++i == 8)
 
130
                {
 
131
                        i = 0;
 
132
                        while (uref.usage_index++ < 63)
 
133
                        {
 
134
                                uref.report_type = HID_REPORT_TYPE_OUTPUT;
 
135
                                uref.report_id = 0;
 
136
                                uref.field_index = 0;
 
137
                                uref.usage_code = 0x90001 + uref.usage_index;
 
138
                                uref.value = 0;
 
139
                                if (ioctl(fd, HIDIOCSUSAGE, &uref) < 0)
 
140
                                        fatalx("Error (SUSAGE) while talking to the UPS");
 
141
                        }
 
142
                        uref.usage_index = 0;
 
143
 
 
144
                        rinfo.report_type = HID_REPORT_TYPE_OUTPUT;
 
145
                        rinfo.report_id = 0;
 
146
                        rinfo.num_fields = 1;
 
147
                        if (ioctl(fd, HIDIOCSREPORT, &rinfo) < 0)
 
148
                                fatalx("Error (SREPORT) while talking to the UPS");
 
149
                }
 
150
        } while (*psz++);
 
151
}
 
152
 
 
153
/*
 
154
 * Send a command to the UPS and (optionally) receive a reply. The parameter
 
155
 * pRsp can be zero, in which case no reply is expected.
 
156
 *
 
157
 * A new file descriptor is opened as experience has shown that a previously
 
158
 * opened descriptor cannot be reused reliably for commanding.
 
159
 *
 
160
 * Data is received in the form of an 8-byte buffer. After the initial report
 
161
 * has been read, HID events are received indicating bit changes in the 8-byte
 
162
 * buffer. When the bit counter rolls over, we know we have received a full
 
163
 * buffer. This doesn't seem to be a 100% reliable method, since it'd not tell
 
164
 * us, for instance, if the same 8 bytes are sent twice in a row, but in
 
165
 * practice, it does appear to reliably transmit all UPS response messages.
 
166
 */
 
167
 
 
168
int hidcmd(unsigned char *pCmd, unsigned char *pRsp, int l)
 
169
{
 
170
        int fd, rd, i, j, k, hid;
 
171
        fd_set rdfs;
 
172
        struct timeval tv;
 
173
        struct hiddev_event ev[NUM_EVTS];
 
174
        struct hiddev_usage_ref uref;
 
175
        unsigned char data[8];
 
176
 
 
177
        if ((fd = open(device_path, O_RDWR)) < 0)
 
178
                fatalx("Cannot communicate with UPS at %s", device_path);
 
179
 
 
180
        FD_ZERO(&rdfs);
 
181
        FD_SET(fd, &rdfs);
 
182
 
 
183
        memset(data, 0, sizeof(data));
 
184
        for (i = 0; i < 64; i++)
 
185
        {
 
186
                uref.report_type = HID_REPORT_TYPE_INPUT;
 
187
                uref.report_id = 0;
 
188
                uref.field_index = 0;
 
189
                uref.usage_index = i;
 
190
                ioctl(fd, HIDIOCGUCODE, &uref);
 
191
                ioctl(fd, HIDIOCGUSAGE, &uref);
 
192
 
 
193
                if (uref.value) data[i >> 3] |= 1 << (i & 7);
 
194
        }
 
195
 
 
196
        sendstring(fd, pCmd);
 
197
 
 
198
        k = 0;
 
199
 
 
200
        if (pRsp != NULL)
 
201
        {
 
202
                hid = -1;
 
203
                while (l > 0)
 
204
                {
 
205
                        tv.tv_sec = 0;
 
206
                        tv.tv_usec = 500000;
 
207
                        if (select(fd+1, &rdfs, 0, 0, &tv) <= 0) break;
 
208
                        rd = read(fd, ev, sizeof(ev));
 
209
                        if (rd < (int) sizeof(ev[0]))
 
210
                                fatalx("Communication failure with UPS");
 
211
 
 
212
                        for (i = 0; i < rd / sizeof(ev[0]); i++)
 
213
                        {
 
214
                                if (hid >= 0 && ev[i].hid <= hid)
 
215
                                {
 
216
                                        for (j = 0; j < 8; j++)
 
217
                                        {
 
218
                                                if (k < l - 1)
 
219
                                                {
 
220
                                                        if (k > 0 || data[j] != 0xFF) pRsp[k++] = data[j];
 
221
                                                }
 
222
                                                else break;
 
223
                                        }
 
224
                                }
 
225
                                j = (ev[i].hid - 1) & 0x3F;
 
226
                                if (ev[i].value) data[j >> 3] |= 1 << (j & 7);
 
227
                                else data[j >> 3] &= ~(1 << (j & 7));
 
228
                                hid = ev[i].hid;
 
229
                        }
 
230
                }
 
231
                if (hid >= 0) for (j = 0; j < ((hid - 1) & 0x3F) >> 3; j++)
 
232
                {
 
233
                        if (k < l - 1)
 
234
                        {
 
235
                                if (k > 0 || data[j] != 0xFF) pRsp[k++] = data[j];
 
236
                        }
 
237
                        else break;
 
238
                }
 
239
                pRsp[k] = '\0';
 
240
        }
 
241
        close(fd);
 
242
        return k;
 
243
}
 
244
 
 
245
 
 
246
int instcmd(const char *cmdname, const char *extra)
 
247
{
 
248
        if (!strcasecmp(cmdname, "test.battery.start"))
 
249
        {
 
250
                hidsend("TL");
 
251
                upslogx(LOG_NOTICE, "UPS test start");
 
252
                return STAT_INSTCMD_HANDLED;
 
253
        }
 
254
 
 
255
        if (!strcasecmp(cmdname, "test.battery.stop"))
 
256
        {
 
257
                hidsend("CT");
 
258
                upslogx(LOG_NOTICE, "UPS test stop");
 
259
                return STAT_INSTCMD_HANDLED;
 
260
        }
 
261
 
 
262
        if (!strcasecmp(cmdname, "beeper.on") ||
 
263
                !strcasecmp(cmdname, "beeper.off"))
 
264
        {
 
265
                char buf[128];
 
266
                int r;
 
267
 
 
268
                /* Find out beeper status and send appropriate command. */
 
269
 
 
270
                r = hidcmd("Q1", buf, sizeof(buf));
 
271
                if (r < 46 || r > 47 || buf[0] != '(')
 
272
                        r = hidcmd("Q1", buf, sizeof(buf));
 
273
                if (r >= 46 && r <= 47 && buf[0] == '(')
 
274
                {
 
275
                        if (OBFLAG && !LBFLAG)  /* Otherwise there's not much we can do */
 
276
                        {
 
277
                                if ((BEEPER && !strcasecmp(cmdname, "beeper.off")) ||
 
278
                                        (!BEEPER && !strcasecmp(cmdname, "beeper.on")))
 
279
                                                hidsend("Q");
 
280
                                return STAT_INSTCMD_HANDLED;
 
281
                        }
 
282
                }
 
283
                return STAT_INSTCMD_HANDLED;    /* FUTURE: failure */
 
284
        }
 
285
 
 
286
        upslogx(LOG_NOTICE, "instcmd: unknown command [%s]", cmdname);
 
287
        return STAT_INSTCMD_UNKNOWN;
 
288
}
 
289
 
 
290
/*
 
291
 * Some trivial initialization. Let's detect the UPS.
 
292
 */
 
293
 
 
294
void upsdrv_initinfo(void)
 
295
{
 
296
        char buf[128];
 
297
        int i, r;
 
298
 
 
299
        /* Yes, sometimes it took as many as 6-7 tries after the UPS has just
 
300
         * been turned on or after a communications problem.
 
301
         */
 
302
        for (i = 0; i < 10; i++)
 
303
        {
 
304
                r = hidcmd("I", buf, sizeof(buf));
 
305
                if (r == 39 && buf[0] == '#') break;
 
306
                sleep(3);
 
307
        }
 
308
        if (r != 39 || buf[0] != '#') fatalx("No Energizer UPS detected");
 
309
        buf[16]=buf[27]=buf[38] = '\0';
 
310
        rtrim(MANUFR, ' ');
 
311
        rtrim(MDLNUM, ' ');
 
312
        rtrim(HWVERS, ' ');
 
313
 
 
314
        dstate_setinfo("driver.version.internal", "%s", DRV_VERSION);
 
315
        dstate_setinfo("ups.mfr", MANUFR);
 
316
        dstate_setinfo("ups.model", MDLNUM);
 
317
 
 
318
        dstate_setinfo("input.transfer.low", "%d", LOXFER);
 
319
        dstate_setinfo("input.transfer.high", "%d", HIXFER);
 
320
 
 
321
        hidsend("C");
 
322
 
 
323
        /* now add instant command support info */
 
324
        dstate_addcmd("test.battery.start");
 
325
        dstate_addcmd("test.battery.stop");
 
326
        dstate_addcmd("beeper.on");
 
327
        dstate_addcmd("beeper.off");
 
328
        upsh.new_instcmd = instcmd;
 
329
}
 
330
 
 
331
/*
 
332
 * Query the UPS and update values.
 
333
 */
 
334
 
 
335
void upsdrv_updateinfo(void)
 
336
{
 
337
        char buf[128];
 
338
        int r;
 
339
        static int f;   /* To prevent excessive logging */
 
340
        double v;
 
341
 
 
342
 
 
343
        r = hidcmd("Q1", buf, sizeof(buf));
 
344
        if (r < 46 || r > 47)
 
345
        {
 
346
                if (f++ < 3)
 
347
                        upslogx(LOG_ERR, "Invalid response length from UPS [%s]", buf);
 
348
                dstate_datastale();
 
349
                return;
 
350
        }
 
351
        if (buf[0] != '(')
 
352
        {
 
353
                if (f++ < 3)
 
354
                        upslogx(LOG_ERR, "Invalid response data from UPS [%s]", buf);
 
355
                dstate_datastale();
 
356
                return;
 
357
        }
 
358
        f = 0;
 
359
 
 
360
        buf[6]=buf[12]=buf[18]=buf[22]=buf[27]=buf[32]=buf[37]=buf[46] = '\0';
 
361
 
 
362
        dstate_setinfo("input.voltage", "%s", INVOLT);
 
363
        dstate_setinfo("output.voltage", "%s", OUTVOLT);
 
364
        dstate_setinfo("battery.voltage", "%s", BATVOLT);
 
365
 
 
366
        v = ((atof(BATVOLT) - LOWVOLT) / VOLTRNG) * 100.0;
 
367
        if (v > 100.0) v = 100.0;
 
368
        if (v < 0.0) v = 0.0;
 
369
        dstate_setinfo("battery.charge", "%02.1f", v);
 
370
 
 
371
        status_init();
 
372
        if (OBFLAG) status_set("OB");                           /* on battery */
 
373
        else
 
374
        {
 
375
                status_set("OL");                                               /* on line */
 
376
                /* only allow these when OL since they're bogus when OB */
 
377
                if (BYPASS)                                                             /* boost or trim in effect */
 
378
                {
 
379
                        if (atoi(INVOLT) < LONORM) status_set("BOOST");
 
380
                        else if (atoi(INVOLT) > HINORM) status_set("TRIM");
 
381
                }
 
382
        }
 
383
        if (LBFLAG) status_set("LB");           /* low battery */
 
384
 
 
385
        status_commit();
 
386
 
 
387
        dstate_setinfo("ups.temperature", "%s", UPSTEMP);
 
388
        dstate_setinfo("input.frequency", "%s", ACFREQ);
 
389
        dstate_setinfo("ups.load", "%s", LOADPCT);
 
390
        dstate_dataok();
 
391
}
 
392
 
 
393
void upsdrv_shutdown(void)
 
394
{
 
395
        char buf[128];
 
396
        int r;
 
397
        int b = 1;
 
398
 
 
399
        /* Basic idea: find out line status and send appropriate command.
 
400
         * If the status query fails, we do not retry more than once (we
 
401
         * may be short on time with a dying UPS) but assume that we're
 
402
         * on battery.
 
403
         */
 
404
 
 
405
        r = hidcmd("Q1", buf, sizeof(buf));
 
406
        if (r < 46 || r > 47 || buf[0] != '(') r = hidcmd("Q1", buf, sizeof(buf));
 
407
 
 
408
        if (r >= 46 && r <= 47 && buf[0] == '(')
 
409
        {
 
410
                if (OBFLAG)
 
411
                        upslogx(LOG_WARNING, "On battery, sending shutdown command...");
 
412
                else
 
413
                {
 
414
                        b = 0;
 
415
                        upslogx(LOG_WARNING, "On line, sending shutdown+return command...");
 
416
                }
 
417
        }
 
418
        else upslogx(LOG_WARNING, "Status undetermined, assuming battery power, "
 
419
                                "sending shutdown command...");
 
420
 
 
421
        hidsend(b ? "S01" : "S01R0003");
 
422
}
 
423
 
 
424
 
 
425
void upsdrv_help(void)
 
426
{
 
427
}
 
428
 
 
429
/* list flags and values that you want to receive via -x */
 
430
void upsdrv_makevartable(void)
 
431
{
 
432
        /* allow '-x xyzzy' */
 
433
        /* addvar(VAR_FLAG, "xyzzy", "Enable xyzzy mode"); */
 
434
 
 
435
        /* allow '-x foo=<some value>' */
 
436
        /* addvar(VAR_VALUE, "foo", "Override foo setting"); */
 
437
}
 
438
 
 
439
void upsdrv_banner(void)
 
440
{
 
441
        printf("Network UPS Tools - Energizer USB UPS driver %s (%s)\n\n", 
 
442
                DRV_VERSION, UPS_VERSION);
 
443
        experimental_driver = 1;        /* Causes a warning to be printed */
 
444
}
 
445
 
 
446
void upsdrv_initups(void)
 
447
{
 
448
        /* No initialization needed */
 
449
}
 
450
 
 
451
void upsdrv_cleanup(void)
 
452
{
 
453
        /* No cleanup needed */
 
454
}