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

« back to all changes in this revision

Viewing changes to drivers/oneac.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
/*vim ts=4*/
 
2
 
 
3
/*
 
4
 * NUT Oneac EG and ON model specific drivers for UPS units using
 
5
 * the Oneac Advanced Interface.  If your UPS is equipped with the
 
6
 * Oneac Basic Interface, use the genericups driver
 
7
*/
 
8
 
 
9
/*
 
10
   Copyright (C) 2002  by Eric Lawson <elawson@inficad.com>
 
11
 
 
12
   This program is free software; you can redistribute it and/or modify
 
13
   it under the terms of the GNU General Public License as published by
 
14
   the Free Software Foundation; either version 2 of the License, or
 
15
   (at your option) any later version.
 
16
 
 
17
   This program is distributed in the hope that it will be useful,
 
18
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
19
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
20
   GNU General Public License for more details.
 
21
 
 
22
   You should have received a copy of the GNU General Public License
 
23
   along with this program; if not, write to the Free Software
 
24
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
25
*/
 
26
 
 
27
/*
 
28
 * 13 Sept 2002.  Eric Lawson
 
29
 *      added BTEST0, BTEST1, RESETVOLT and SIMPWF instant commands.
 
30
 *              RESETVOLT resets the max and min measured voltage that
 
31
 *              has been measured by the UPS.
 
32
 *
 
33
 * 11 Sept 2002.  Eric Lawson
 
34
 *      Cleaned up code to take full advantage of upssend().
 
35
 *
 
36
 *      Added support for gathering ON specific values.
 
37
 *      The driver reports nominal utility voltage, nominal operating
 
38
 *      frequency, per cent of full load, current utility voltage,
 
39
 *      minimum and maximum utility voltage since the UPS last shut
 
40
 *      down, output UPS voltage, and if the UPS is boosting low utility
 
41
 *      voltage.
 
42
 *
 
43
 * 08 Sept 2002.
 
44
 *       Initial release.
 
45
*/
 
46
 
 
47
#define DRV_VERSION "0.4"
 
48
 
 
49
#include "main.h"
 
50
#include "oneac.h"
 
51
 
 
52
void do_battery_test(void)
 
53
{
 
54
        char buffer[256];
 
55
 
 
56
        if (getval("testtime") == NULL)
 
57
                strlcpy(buffer, DEFAULT_BAT_TEST_TIME, 3);
 
58
        else {
 
59
                strlcpy(buffer, getval("testtime"),3);
 
60
 
 
61
        /*the UPS wants this value to always be two characters long*/
 
62
        /*so put a zero in front of the string, if needed....      */
 
63
                if (strlen(buffer) < 2) {
 
64
                        buffer[2] = '\0';
 
65
                        buffer[1] = buffer[0];
 
66
                        buffer[0] = '0';
 
67
                }
 
68
        }
 
69
        upssend("%s%s%s",BAT_TEST_PREFIX,buffer,COMMAND_END);
 
70
}
 
71
 
 
72
 
 
73
/****************************************************************
 
74
 *below are the commands that are called by main (part of the   *
 
75
 *Above, are functions used only in this oneac driver           *
 
76
 ***************************************************************/
 
77
 
 
78
int instcmd(const char *cmdname, const char *extra)
 
79
{
 
80
        if (!strcasecmp(cmdname, "test.failure.start")) {
 
81
                upssend("%s%s",SIM_PWR_FAIL,COMMAND_END);
 
82
                return STAT_INSTCMD_HANDLED;
 
83
        }
 
84
 
 
85
        if (!strcasecmp(cmdname, "test.battery.start")) {
 
86
                do_battery_test();
 
87
                return STAT_INSTCMD_HANDLED;
 
88
        }
 
89
 
 
90
        if (!strcasecmp(cmdname, "test.battery.stop")) {
 
91
                upssend("%s00%s",BAT_TEST_PREFIX,COMMAND_END);
 
92
                return STAT_INSTCMD_HANDLED;
 
93
        }
 
94
 
 
95
        if (!strcasecmp(cmdname, "reset.input.minmax")) {
 
96
                upssend("%c%s",RESET_MIN_MAX, COMMAND_END);
 
97
                return STAT_INSTCMD_HANDLED;
 
98
        }
 
99
 
 
100
        upslogx(LOG_NOTICE, "instcmd: unknown command [%s]", cmdname);
 
101
        return STAT_INSTCMD_UNKNOWN;
 
102
}
 
103
 
 
104
 
 
105
void upsdrv_initinfo(void)
 
106
{
 
107
        char buffer[256];
 
108
 
 
109
        dstate_setinfo("ups.mfr", "ONEAC");
 
110
 
 
111
        dstate_addcmd("test.battery.start");
 
112
        dstate_addcmd("test.battery.stop");
 
113
        dstate_addcmd("test.failure.start");
 
114
        dstate_addcmd("reset.input.minmax");
 
115
 
 
116
        /*set some stuff that shouldn't change after initialization*/
 
117
        /*this stuff is common to both the EG and ON family of UPS */
 
118
 
 
119
        /*firmware revision*/
 
120
        upssend ("%c%s",GET_VERSION, COMMAND_END);
 
121
        upsrecv (buffer, sizeof(buffer),ENDCHAR,IGNCHARS);
 
122
        dstate_setinfo("ups.firmware", "%.3s",buffer);
 
123
 
 
124
        /*nominal AC frequency setting --either 50 or 60*/
 
125
        upssend ("%c%s",GET_NOM_FREQ,COMMAND_END);
 
126
        upsrecv (buffer, sizeof(buffer), ENDCHAR,IGNCHARS);
 
127
        dstate_setinfo("input.frequency", "%.2s", buffer);
 
128
 
 
129
 
 
130
        /*UPS Family */
 
131
 
 
132
        upssend ("%c%s",GET_FAMILY,COMMAND_END);
 
133
        upsrecv (buffer, sizeof(buffer), ENDCHAR, IGNCHARS);
 
134
        dstate_setinfo("ups.model", "%.2s",buffer);
 
135
        printf("Found %.2s family of Oneac UPS\n", buffer);
 
136
 
 
137
        if (strncmp(buffer,FAMILY_ON,2) || strncmp(buffer,FAMILY_EG,2) == 0)
 
138
                printf("Unknown family of UPS. Assuming EG capabilities.\n");
 
139
 
 
140
        upsh.new_instcmd = instcmd;
 
141
 
 
142
        /*The ON series of UPS supports more stuff than does the EG.
 
143
         *Take care of the ON only stuff here
 
144
        */
 
145
        if(strncmp (dstate_getinfo("ups.model"), FAMILY_ON, 2) == 0) {
 
146
                /*now set the ON specific "static" parameters*/
 
147
 
 
148
                        /*nominal input voltage*/
 
149
 
 
150
                upssend("%c%s", GET_NOM_VOLTAGE, COMMAND_END);
 
151
                upsrecv(buffer,sizeof(buffer),ENDCHAR,IGNCHARS);
 
152
 
 
153
                switch (buffer[0]) {
 
154
                        case '1':
 
155
                                dstate_setinfo("output.voltage.target.line", 
 
156
                                        "120");
 
157
                                break;
 
158
 
 
159
                        case '2':
 
160
                                dstate_setinfo("output.voltage.target.line", 
 
161
                                        "240");
 
162
                                break;
 
163
 
 
164
                        default:
 
165
                                upslogx(LOG_INFO,"Oneac: "
 
166
                                        "Invalid voltage parameter from UPS");
 
167
                }
 
168
        }
 
169
}
 
170
 
 
171
void upsdrv_updateinfo(void)
 
172
{
 
173
        char buffer[256];
 
174
 
 
175
        upssend ("%c%s",GET_ALL,COMMAND_END);
 
176
        upsrecv (buffer, sizeof(buffer), ENDCHAR, IGNCHARS);
 
177
 
 
178
        upsdebugx (2,"upsrecv_updateinfo: upsrecv returned: %s\n",buffer);
 
179
 
 
180
        status_init();
 
181
        /*take care of the UPS status information*/
 
182
        switch (buffer[12]) {
 
183
                case NORMAL :
 
184
                        status_set("OL");
 
185
                        break;
 
186
                case ON_BAT_LOW_LINE :
 
187
                case ON_BAT_HI_LINE  :
 
188
                        status_set("OB");
 
189
                        break;
 
190
                case LO_BAT_LOW_LINE :
 
191
                case LO_BAT_HI_LINE  :
 
192
                        status_set("OB LB");
 
193
                        break;
 
194
                case TOO_HOT :
 
195
                        status_set("OVER OB LB");
 
196
                        break;
 
197
                case FIX_ME :
 
198
 
 
199
/* this is not defined */
 
200
#if 0
 
201
                        setinfo (INFO_ALRM_GENERAL, "ALARM ");
 
202
#endif
 
203
                        break;
 
204
                case BAD_BAT :
 
205
                        status_set("RB");
 
206
                        break;
 
207
                default :                               /*cry for attention, fake a status*/
 
208
                                                                /*Would another status be better?*/
 
209
                        upslogx (LOG_ERR, "Oneac: Unknown UPS status");
 
210
                        status_set("OL");
 
211
        }
 
212
 
 
213
        /*take care of the reason why the UPS last transfered to battery*/
 
214
        switch (buffer[13]) {
 
215
                case XFER_BLACKOUT :
 
216
                        dstate_setinfo("input.transfer.reason", "Blackout");
 
217
                        break;
 
218
                case XFER_LOW_VOLT :
 
219
                        dstate_setinfo("input.transfer.reason",
 
220
                                "Low Input Voltage");
 
221
                        break;
 
222
                case XFER_HI_VOLT :
 
223
                        dstate_setinfo("input.transfer.reason",
 
224
                                "High Input Voltage");
 
225
                        break;
 
226
                case NO_VALUE_YET :
 
227
                        dstate_setinfo("input.transfer.reason", 
 
228
                                "No transfer yet.");
 
229
                        break;
 
230
                default :
 
231
                        upslogx(LOG_INFO,"Oneac: Unknown reason for UPS battery transfer");
 
232
        }
 
233
        /* now update info for only the ON family of UPS*/
 
234
 
 
235
        if (strncmp(dstate_getinfo("ups.model"), FAMILY_ON, 2) == 0) {
 
236
                dstate_setinfo("ups.load", "0%.2s",buffer+31);
 
237
 
 
238
                /*run time left. */
 
239
 
 
240
/* I've commented this out until I either figure out how to convert
 
241
 * to an actual time, or I decide I can't do that and remove this code
 
242
 
 
243
                if(buffer[10] == YES)
 
244
                        dstate_setinfo("batt.runtime", "0%.2s",buffer+33);
 
245
                else dstate_setinfo("batt.runtime", "100");
 
246
*/
 
247
 
 
248
                dstate_setinfo("input.voltage", "%.3s",buffer+35);
 
249
                dstate_setinfo("input.voltage.minimum", "%.3s",buffer+38);
 
250
                dstate_setinfo("input.voltage.maximum", "%.3s",buffer+41);
 
251
                dstate_setinfo("output.voltage", "%.3s",buffer+44);
 
252
                if (buffer[47] == YES) status_set("BOOST");
 
253
        }
 
254
 
 
255
        status_commit();
 
256
}
 
257
 
 
258
void upsdrv_shutdown(void)
 
259
{
 
260
        upssend (SHUTDOWN);
 
261
}
 
262
 
 
263
 
 
264
void upsdrv_help(void)
 
265
{
 
266
        printf("\n---------\nNOTE:\n");
 
267
        printf("You must set the UPS interface card DIP switch to 9600BPS\n");
 
268
}
 
269
 
 
270
void upsdrv_makevartable(void)
 
271
{
 
272
        addvar(VAR_VALUE,"testtime",
 
273
                "Change battery test time from 2 minute default.");
 
274
}
 
275
 
 
276
void upsdrv_banner(void)
 
277
{
 
278
        printf("Network UPS Tools - Oneac EG/ON UPS driver %s (%s)\n\n", 
 
279
                DRV_VERSION, UPS_VERSION);
 
280
        experimental_driver = 1;        /*causes a warning to be printed*/
 
281
}
 
282
 
 
283
void upsdrv_initups(void)
 
284
{
 
285
        char buffer[256];
 
286
 
 
287
        /* this driver doesn't do sanity checks in upsdrv_updateinfo */
 
288
        broken_driver = 1;
 
289
        return;
 
290
        
 
291
        open_serial(device_path, B9600);
 
292
 
 
293
        /*Is there anybody out there?*/
 
294
 
 
295
        upssend (COMMAND_END);
 
296
        sleep(1);
 
297
        upssend (COMMAND_END);
 
298
        sleep(1);
 
299
        upssend("%c%s",GET_MFR, COMMAND_END);
 
300
        upsrecv (buffer, sizeof(buffer), ENDCHAR, IGNCHARS);
 
301
        if (strncmp(buffer, MFGR, sizeof(MFGR)))
 
302
                fatalx("Unable to contact ONEAC UPS on %s\n",device_path);
 
303
}
 
304
 
 
305
void upsdrv_cleanup(void)
 
306
{
 
307
}