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

« back to all changes in this revision

Viewing changes to models/toshiba1500.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
 
/* toshiba1500.c - model specific routines for Toshiba 1500 series units
2
 
 
3
 
$Id: toshiba1500.c,v 1.2 2000/11/24 10:41:30 cvs Exp $
4
 
 
5
 
Revision 1.2  2000/09/08 05:55:45  rpm
6
 
Rewrite receive path to make it deterministic, not timeout-driven.
7
 
Reduce getupsmsg wait to 1 second for faster updates.
8
 
Reduce stack-based buffer sizes to something reasonable.
9
 
 
10
 
 
11
 
Copyright (C) 2000  Kenneth Porter <shiva@well.com>
12
 
Copyright (C) 1999  Russell Kroll <rkroll@exploits.org>
13
 
 
14
 
Based on fentonups.c by Russell Kroll.
15
 
 
16
 
Thanks go to Dennis Kruep and Greg Mack of Toshiba for making the
17
 
protocol documentation available.
18
 
 
19
 
This program is free software; you can redistribute it and/or modify
20
 
it under the terms of the GNU General Public License as published by
21
 
the Free Software Foundation; either version 2 of the License, or
22
 
(at your option) any later version.
23
 
 
24
 
This program is distributed in the hope that it will be useful,
25
 
but WITHOUT ANY WARRANTY; without even the implied warranty of
26
 
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27
 
GNU General Public License for more details.
28
 
 
29
 
You should have received a copy of the GNU General Public License
30
 
along with this program; if not, write to the Free Software
31
 
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32
 
*/
33
 
 
34
 
#include <errno.h>
35
 
#include <fcntl.h>
36
 
#include <stdio.h>
37
 
#include <signal.h>
38
 
#include <stdlib.h>
39
 
#include <string.h>
40
 
#include <unistd.h>
41
 
#include <sys/file.h>
42
 
#include <sys/stat.h>
43
 
#include <sys/types.h>
44
 
#include <sys/termios.h>
45
 
 
46
 
#include "config.h"
47
 
#include "proto.h"
48
 
#include "shared.h"
49
 
#include "version.h"
50
 
#include "upscommon.h"
51
 
#include "common.h"
52
 
 
53
 
#define INFOMAX 16
54
 
 
55
 
extern int upsc_debug; /* set to debug protocol */
56
 
 
57
 
int     shmok = 1;
58
 
int     sddelay = 90;   /* wait 90 seconds for shutdown */
59
 
 
60
 
/* character codes */
61
 
#define STX 0x02
62
 
#define ETX 0x03
63
 
#define EOT 0x04
64
 
#define ENQ 0x05
65
 
#define ACK 0x06
66
 
#define NAK 0x15
67
 
 
68
 
void initinfo (void)
69
 
{
70
 
        create_info(INFOMAX, shmok);
71
 
 
72
 
        /* setup variables that should always exist */
73
 
        addinfo (INFO_MFR, "Toshiba", FLAG_STRING, 0);
74
 
        addinfo (INFO_MODEL, "1500", FLAG_STRING, 0);
75
 
        addinfo (INFO_STATUS, "", FLAG_STRING, 0);
76
 
        addinfo (INFO_OUTVOLT, "", 0, 0);
77
 
        addinfo (INFO_UTILITY, "", 0, 0);
78
 
        addinfo (INFO_LOADPCT, "", 0, 0);
79
 
        addinfo (INFO_BATTPCT, "", 0, 0);
80
 
        addinfo (INFO_STATUS, "", 0, 0);
81
 
        addinfo (INFO_ACFREQ, "", 0, 0);
82
 
        /* addinfo (INFO_OUTFREQ, "", 0, 0); */
83
 
}
84
 
 
85
 
/* retry sending message 3 times until ACK received or timeout
86
 
 * return 0 on ACK, -1 on timeout
87
 
 */
88
 
 
89
 
int waitforack (const char* message)
90
 
{
91
 
        int try_count;
92
 
        for (try_count = 0; try_count < 3; ++try_count)
93
 
        {
94
 
                char temp[4];
95
 
                upssend("%s", message);
96
 
                if (upsrecv (temp, 3, ACK, "") > 0)
97
 
                        return 0; /* success */
98
 
        }
99
 
        return -1; /* retries exhausted */
100
 
}
101
 
 
102
 
void updatestatus (unsigned status)
103
 
{
104
 
        static unsigned last_status = 0;
105
 
        if (upsc_debug)
106
 
                printf ("updatestatus(%X)\n", status);
107
 
        if (status != last_status) {
108
 
                char status_string[256] = "";
109
 
                last_status = status;
110
 
                if (0x10 & status) /* input undervoltage or blackout */
111
 
                        strcat (status_string,"OB ");
112
 
                else
113
 
                        strcat (status_string,"OL "); /* online */
114
 
                if (0x08 & status) /* low battery */
115
 
                        strcat (status_string,"LB ");
116
 
                if (0x04 & status) /* bypass operation */
117
 
                        strcat (status_string,"BYP ");
118
 
                if (0x02 & status) /* inverter operation */
119
 
                        strcat (status_string,"INV ");
120
 
                if (0x01 & status) /* synchronous operation */
121
 
                        strcat (status_string,"SYNC ");
122
 
                /* todo: check 0x20 bit (fault) and read fault word
123
 
                 * trim trailing blank
124
 
                 */
125
 
                if (status_string[0])
126
 
                        /* something was appended */
127
 
                        status_string[strlen (status_string) - 1] = '\0';
128
 
                setinfo(INFO_STATUS, "%s", status_string);
129
 
        }
130
 
}
131
 
 
132
 
/* return 0 on success */
133
 
 
134
 
int select_toshiba (void)
135
 
{
136
 
        /* send 1-1-ENQ ("select") */
137
 
        if (-1 == waitforack ("11\005")) {
138
 
                if (upsc_debug)
139
 
                        printf ("Bad reply to select\n");
140
 
                return -1;
141
 
        }
142
 
        return 0;
143
 
}
144
 
 
145
 
/* create value selection command followed by BCC */
146
 
 
147
 
void compose_toshiba_command (char* buf, int buflen,
148
 
                                                          char command_type,
149
 
                                                          const char* address)
150
 
{
151
 
        char        bcc;
152
 
        char*       p;
153
 
 
154
 
        snprintf (buf, buflen - 1, "\002%c%s\003", command_type, address);
155
 
        /* STX does not participate in BCC computation */
156
 
        bcc = 0;
157
 
        for (p = buf + 1; *p; ++p)
158
 
                bcc ^= *p;
159
 
        /* append BCC */
160
 
        *p++ = bcc;
161
 
        *p = '\0';
162
 
}
163
 
 
164
 
/* get one char (may be high ascii), with timeout */
165
 
 
166
 
int upsgetch (void)
167
 
{
168
 
        char    in;
169
 
        int             ret;
170
 
        struct  sigaction sa;
171
 
        sigset_t sigmask;
172
 
 
173
 
        sa.sa_handler = timeout;
174
 
        sigemptyset (&sigmask);
175
 
        sa.sa_mask = sigmask;
176
 
        sa.sa_flags = 0;
177
 
        sigaction (SIGALRM, &sa, NULL);
178
 
 
179
 
        alarm (3);
180
 
 
181
 
        ret = read (upsfd, &in, 1);
182
 
 
183
 
        alarm (0);
184
 
        signal (SIGALRM, SIG_IGN);
185
 
 
186
 
        if (ret > 0) {
187
 
                nolongertimeout (); /* toggle flag */
188
 
                return in;
189
 
        }
190
 
        else
191
 
                return (-1);
192
 
}
193
 
 
194
 
/* return -1 on failure, 0 on success
195
 
 * stores null-terminated result string in buf,
196
 
 * stores status byte in *status
197
 
 */
198
 
 
199
 
int get_toshiba_result (char buf[5],
200
 
                                                char* status,
201
 
                                                char command_type,
202
 
                                                const char* address)
203
 
{
204
 
        char        temp[10];
205
 
        char*       statusp;
206
 
        int         bcc;
207
 
        char*       p;
208
 
 
209
 
        /* wait for up to 10 chars of response */
210
 
        if (upsrecv (temp, sizeof temp, ETX, "") < 1)
211
 
                return -1;
212
 
 
213
 
        /* attempt to parse value */
214
 
        if (STX != temp[0] || command_type != temp[1] ||
215
 
                  address[0] != temp[2] || address[1] != temp[3]) {
216
 
                if (upsc_debug)
217
 
                        printf ("prefix wrong\n");
218
 
                return -1;
219
 
        }
220
 
        /* find the ETX (preceded by UPS status, followed by BCC) */
221
 
        /* check BCC */
222
 
        bcc = upsgetch ();
223
 
        if (bcc < 0)
224
 
                return -1;
225
 
        for (p = temp + 1; *p; ++p)
226
 
                bcc ^= *p;
227
 
        bcc ^= ETX; /* account for ETX eaten by upsrecv */
228
 
        if (bcc) {
229
 
                if (upsc_debug)
230
 
                        printf ("bad BCC, result = %02X\n", bcc);
231
 
                return -1;
232
 
        }
233
 
        /* looks good, store it */
234
 
        statusp = temp + strlen (temp) - 1; /* status at end of message */
235
 
        *status = *statusp;
236
 
        *statusp = '\0'; /* terminate result string */
237
 
        strcpy (buf, temp + 4);
238
 
        return 0;
239
 
}
240
 
 
241
 
/* query a value from the UPS and update status bits
242
 
 * returns value (0-999) on success, -1 on failure
243
 
 */
244
 
 
245
 
int pollvalue (const char* address)
246
 
{
247
 
        char        temp[10];
248
 
        unsigned    try_count;
249
 
        unsigned    value;
250
 
        char    status;
251
 
        if (upsc_debug)
252
 
                printf ("pollvalue(%s)\n", address);
253
 
 
254
 
        memset (temp, '\0', sizeof temp);
255
 
 
256
 
        select_toshiba ();
257
 
 
258
 
        /* send command */
259
 
 
260
 
        compose_toshiba_command (temp, sizeof temp, 'M', address);
261
 
 
262
 
        /* now send command and wait for ACK */
263
 
        if (-1 == waitforack (temp)) {
264
 
                if (upsc_debug)
265
 
                        printf ("Bad reply to command\n");
266
 
                return -1;
267
 
        }
268
 
 
269
 
        upssendchar (EOT); /* done selecting */
270
 
 
271
 
        /* now poll for value */
272
 
 
273
 
        upssend("%s", "10\005"); /* 1-0-ENQ */
274
 
 
275
 
        for (try_count = 0; try_count < 3; ++try_count) {
276
 
                if (-1 == get_toshiba_result (temp, &status, 'M', address)) {
277
 
                        upssend("%s", "10\025"); /* 1-0-NAK */
278
 
                        continue;
279
 
                }
280
 
                break; /* result ok */
281
 
        }
282
 
        upssendchar (EOT); /* signal UPS we're quitting */
283
 
        if (3 == try_count) {
284
 
                if (upsc_debug)
285
 
                        printf ("giving up on parameter %s\n", address);
286
 
                return -1;
287
 
        }
288
 
        updatestatus (status); /* status at end of message */
289
 
        sscanf (temp,"%u",&value);
290
 
        if (upsc_debug)
291
 
                printf ("parameter %s has value '%s'\n", address, temp);
292
 
        return value;
293
 
}
294
 
 
295
 
/* normal idle loop - keep up with the current state of the UPS */
296
 
void updateinfo (void)
297
 
{
298
 
        unsigned value;         /* parsed result */
299
 
        double  dvalue;         /* same, but as a double */
300
 
 
301
 
#if 0 /* no poll result on 1500 */
302
 
        /* fault bits */
303
 
        pollvalue ("05");
304
 
#endif
305
 
 
306
 
        /* output voltage in percent */
307
 
        dvalue = 120 * pollvalue ("10");
308
 
        if (dvalue >= 0) {
309
 
                /* convert % to volts of rated voltage */
310
 
                setinfo(INFO_OUTVOLT, "%4.1f", dvalue / 100.0);
311
 
                writeinfo();
312
 
        }
313
 
 
314
 
#if 0 /* can't select on 1500 */
315
 
        /* input voltage in percent */
316
 
        dvalue = pollvalue ("20");
317
 
        if (dvalue >= 0) {
318
 
                /* convert % to volts of rated voltage */
319
 
                setinfo(INFO_UTILITY, "%4.1f", dvalue / 100.0);
320
 
                writeinfo();
321
 
        }
322
 
#endif
323
 
 
324
 
        /* output current in percent (not used) */
325
 
        value = pollvalue ("30");
326
 
        if (value >= 0) {
327
 
                setinfo(INFO_LOADPCT, "%u", value);
328
 
                writeinfo();
329
 
        }
330
 
 
331
 
        /* battery voltage in percent */
332
 
        value = pollvalue ("40");
333
 
        if (value >= 0) {
334
 
                setinfo(INFO_BATTPCT, "%u", value);
335
 
                writeinfo();
336
 
        }
337
 
 
338
 
        /* input frequency in Hz * 10 */
339
 
        dvalue = pollvalue ("50") / 10.0;
340
 
        if (dvalue >= 0) {
341
 
                setinfo(INFO_ACFREQ, "%3.1f", dvalue);
342
 
                writeinfo();
343
 
        }
344
 
 
345
 
#if 0
346
 
        /* output frequency in Hz * 10 (not used) */
347
 
        pollvalue ("51");
348
 
#endif
349
 
        
350
 
}
351
 
 
352
 
/* open port at 1200 bps, 7E1 framing */
353
 
 
354
 
void open_toshiba (char* port)
355
 
{
356
 
        struct  termios tio;
357
 
 
358
 
        open_serial (port, B1200);
359
 
 
360
 
        /* switch to 7E1 */
361
 
        tcgetattr (upsfd, &tio);
362
 
        tio.c_cflag = CS7 | CLOCAL | CREAD | PARENB;
363
 
#ifdef HAVE_CFSETISPEED
364
 
        cfsetispeed (&tio, B1200);
365
 
        cfsetospeed (&tio, B1200);
366
 
#endif
367
 
        tcsetattr (upsfd, TCSANOW, &tio);
368
 
 
369
 
        /* timeouts are normal for this model,
370
 
         * so don't log them
371
 
         */
372
 
        flag_timeoutfailure = -1;
373
 
}
374
 
 
375
 
/* power down the attached load immediately */
376
 
void forceshutdown (char *port)
377
 
{
378
 
        unsigned    try_count;
379
 
        char    command[10], reply[10], status;
380
 
 
381
 
        /* address is 90 plus delay in 30 second intervals
382
 
         * valid delay range is 30 seconds to 8 minutes 
383
 
         */
384
 
        static const char address[] = "91";
385
 
 
386
 
        upslogx(LOG_INFO, "Initiating UPS shutdown");
387
 
        printf ("Initiating forced UPS shutdown!\n");
388
 
 
389
 
        open_toshiba (port);
390
 
 
391
 
        for (try_count = 0; try_count < 3; ++try_count) {
392
 
 
393
 
                if (-1 == select_toshiba ()) {
394
 
                        upssendchar (EOT);
395
 
                        continue;
396
 
                }
397
 
 
398
 
                compose_toshiba_command (command, sizeof command, 'W', address);
399
 
                upssend("%s", command);
400
 
 
401
 
                if (-1 == get_toshiba_result (reply, &status, 'C', address))
402
 
                        continue;
403
 
 
404
 
                /* send confirmation */
405
 
                compose_toshiba_command (command, sizeof command, 'C', address);
406
 
                upssend("%s", command);
407
 
 
408
 
                /* check for acknowledge of confirmation */
409
 
                if (-1 == get_toshiba_result (reply, &status, 'W', address))
410
 
                        continue;
411
 
 
412
 
                break; /* result ok */
413
 
        }
414
 
        upssendchar (EOT); /* signal UPS we're quitting */
415
 
        if (3 == try_count) {
416
 
                printf ("giving up on shutdown attempt\n");
417
 
                exit (1);
418
 
        }
419
 
 
420
 
        if (sddelay > 0) {
421
 
                printf ("Waiting for poweroff...\n");
422
 
                sleep (sddelay);
423
 
                printf ("Hmm, did the shutdown fail?  Oh well...\n");
424
 
                exit (1);
425
 
        }
426
 
        exit (0);
427
 
}
428
 
 
429
 
void usage (char *prog)
430
 
{
431
 
        printf ("usage: %s [-h] [-d <num>] [-k] <device>\n", prog);
432
 
        printf ("Example: %s /dev/ttyS0\n", prog);
433
 
        exit (1);
434
 
}
435
 
 
436
 
void help (char *prog)
437
 
{
438
 
        printf ("usage: %s [-h] [-d <num>] [-k] <device>\n", prog);
439
 
        printf ("\n");
440
 
        printf ("-d <num> - wait <num> seconds after sending shutdown command\n");
441
 
        printf ("-h       - display this help\n");
442
 
        printf ("-k       - force shutdown\n");
443
 
        printf ("<device> - /dev entry corresponding to UPS port\n");
444
 
}
445
 
 
446
 
int main (int argc, char **argv)
447
 
{
448
 
        char    *portname, *prog;
449
 
        int     i;
450
 
 
451
 
        printf ("Network UPS Tools - Toshiba 1500 series UPS driver 0.1 (%s)\n", UPS_VERSION);
452
 
        openlog ("toshiba1500", LOG_PID, LOG_FACILITY);
453
 
 
454
 
        prog = argv[0];
455
 
 
456
 
        while ((i = getopt (argc, argv, "+d:ghk:")) != EOF) {
457
 
                switch (i) {
458
 
                        case 'd':
459
 
                                sddelay = atoi (optarg);
460
 
                                break;
461
 
                        case 'k':
462
 
                                forceshutdown (optarg);
463
 
                                break;
464
 
                        case 'h':
465
 
                                help (prog);
466
 
                                break;
467
 
                        case 'g':
468
 
                                upsc_debug = 1; /* turn on port debugging */
469
 
                                break;
470
 
                        default:
471
 
                                usage (prog);
472
 
                                break;
473
 
                }
474
 
        }
475
 
 
476
 
        argc -= optind;
477
 
        argv += optind;
478
 
 
479
 
        if (argc != 1) {
480
 
                help (prog);
481
 
                exit (1);
482
 
        }
483
 
 
484
 
        droproot ();
485
 
 
486
 
        portname = NULL;
487
 
 
488
 
        for (i = strlen (argv[0]); i >= 0; i--)
489
 
                if (argv[0][i] == '/') {
490
 
                        portname = &argv[0][i+1];
491
 
                        break;
492
 
                }
493
 
 
494
 
        if (portname == NULL) {
495
 
                printf ("Unable to abbreviate %s\n", argv[0]);
496
 
                exit (1);
497
 
        }
498
 
 
499
 
        snprintf (statefn, sizeof statefn, "%s/toshiba1500-%s", STATEPATH,
500
 
                          portname);
501
 
 
502
 
        open_toshiba (argv[0]);
503
 
 
504
 
        initinfo ();
505
 
 
506
 
        createmsgq ();  /* try to create IPC message queue */
507
 
 
508
 
        if (!upsc_debug)
509
 
                background ();
510
 
 
511
 
        for (;;) {
512
 
                updateinfo ();
513
 
 
514
 
                /* wait up to 1 second for a message from the upsd */
515
 
                if (getupsmsg (1))
516
 
                        upslogx(LOG_INFO, "Received a message from upsd");
517
 
        }
518
 
}