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

« back to all changes in this revision

Viewing changes to drivers/microdowell.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
/* Written to work with MicroDowell BBox UPS.
 
2
   This model is based on MicroDowell (www.microdowell.com) models.
 
3
    
 
4
   Copyright (C) 2000  Gilberto Iob <research@microdowell.com>
 
5
   Copyright (C) 2002  Aleksandar Topuzovic <aleksandar.topuzovic@avl.com>
 
6
 
 
7
   This program is free software; you can redistribute it and/or modify
 
8
   it under the terms of the GNU General Public License as published by
 
9
   the Free Software Foundation; either version 2 of the License, or
 
10
   (at your option) any later version.
 
11
 
 
12
   This program is distributed in the hope that it will be useful,
 
13
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
   GNU General Public License for more details.
 
16
 
 
17
   You should have received a copy of the GNU General Public License
 
18
   along with this program; if not, write to the Free Software
 
19
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
20
*/
 
21
 
 
22
#define DRV_VERSION "0.02"
 
23
 
 
24
#include "main.h"
 
25
#include "microdowell.h"
 
26
 
 
27
extern  int     sddelay;
 
28
 
 
29
static int ups_model_ok (char *buf, unsigned buflen);
 
30
static int polling_ok (char *buf, unsigned buflen);
 
31
int instcmd(const char *cmdname, const char *extra);
 
32
 
 
33
void set_flag (int *val, int flag)
 
34
{
 
35
        *val = (*val |= flag);
 
36
}
 
37
        
 
38
void clear_flag (int *val, int flag)  
 
39
{
 
40
        *val = (*val ^= (*val & flag));
 
41
}
 
42
 
 
43
int is_set (int num, int flag)
 
44
{
 
45
        return ((num & flag) == flag);
 
46
}
 
47
 
 
48
static int polling_ok (char *buf, unsigned buflen) /* Everything OK!!! */
 
49
{
 
50
        if ((buflen==NUM_POLLING_BYTES) && (buf[0]==91) && (buf[NUM_POLLING_BYTES-1]==93) && (buf[1]==1))
 
51
        {
 
52
                return (1);     /* Polling OK */
 
53
        }
 
54
        else
 
55
        {
 
56
                return (0);     /* Polling FAILED */
 
57
        }
 
58
}
 
59
 
 
60
/*
 
61
        Indetify UPS Model and revision
 
62
*/
 
63
static int ups_model_ok (char *buf, unsigned buflen) /* Everything OK!!! */
 
64
{
 
65
        char upsSerial[5];
 
66
        int upsRelease = 0;
 
67
        
 
68
        if ((buflen==NUM_MODEL_BYTES) && (buf[0]==91) &&
 
69
            (buf[NUM_MODEL_BYTES-1]==93) && (buf[1]==10))
 
70
        {
 
71
                if ((buf[2] == 'S') && (buf[3] == 'H'))
 
72
                {
 
73
                        upsRelease = (buf[4] - '0') * 10 + buf[6] - '0';
 
74
                        if (upsRelease < 40)
 
75
                        {
 
76
                                dstate_setinfo("ups.model", "BBox Pro PnP");
 
77
                                upslogx(LOG_NOTICE, "BBox Pro PnP rev. %d on %s.\n", upsRelease, device_path);
 
78
                        } else {
 
79
                                dstate_setinfo("ups.model", "BBox Pro USB");
 
80
                                upslogx(LOG_NOTICE, "BBox Pro USB rev. %d on %s.\n", upsRelease, device_path);
 
81
                        }
 
82
                        sprintf (upsSerial, "SH%c.%c", buf[4], buf[6]);
 
83
                        dstate_setinfo("ups.serial", "%s", upsSerial);
 
84
                        return (100 + upsRelease);
 
85
                }
 
86
                else if ((buf[2] == 'M') && (buf[3] == 'D'))
 
87
                {
 
88
                        upsRelease = (buf[4] - '0') * 10 + buf[6] - '0';
 
89
                        if (upsRelease < 30)
 
90
                        {
 
91
                                dstate_setinfo("ups.model", "BBox Interactive PnP");
 
92
                                upslogx(LOG_NOTICE, "BBox Interactive PnP rev. %d on %s.\n", upsRelease, device_path);
 
93
                        } else {
 
94
                                dstate_setinfo("ups.model", 
 
95
                                        "BBox Interactive USB");
 
96
                                upslogx(LOG_NOTICE, "BBox Interactive USB rev. %d on %s.\n", upsRelease, device_path);
 
97
                        }
 
98
                        sprintf (upsSerial, "MD%c.%c", buf[4], buf[6]);
 
99
                        dstate_setinfo("ups.serial", "%s", upsSerial);
 
100
                        return (upsRelease);
 
101
                }
 
102
                else if ((buf[2] == 'B') && (buf[3] == '5'))
 
103
                {
 
104
                        upsRelease = (buf[4] - '0') * 10 + buf[6] - '0';
 
105
                        dstate_setinfo("ups.model", "HiBox USB");
 
106
                        upslogx(LOG_NOTICE, "HiBox USB rev. %d on %s.\n", upsRelease, device_path);
 
107
                        sprintf (upsSerial, "B5%c.%c", buf[4], buf[6]);                 
 
108
                        dstate_setinfo("ups.serial", "%s", upsSerial);
 
109
                        return (upsRelease);
 
110
                }
 
111
                else       
 
112
                        return (0);
 
113
        }
 
114
        else
 
115
                return (0);
 
116
}
 
117
 
 
118
#if 0   /* not being used right now */
 
119
static int setupstime () /* Everything OK!!! */
 
120
{
 
121
        time_t          now;
 
122
        struct tm       *timeptr;
 
123
        unsigned char   upshour;
 
124
        
 
125
        time (&now);
 
126
        timeptr = localtime (&now);
 
127
 
 
128
        if (upssendchar (CMD_SETTIMER) != 1)
 
129
                return (-1);
 
130
        usleep (CHAR_DELAY);
 
131
        upshour = (unsigned char) ((timeptr->tm_wday - 1) * 24 + timeptr->tm_hour);
 
132
        if (upssendchar (upshour) != 1)
 
133
                return (-1);
 
134
        usleep (CHAR_DELAY);
 
135
        if (upssendchar ((unsigned char)timeptr->tm_min) != 1)
 
136
                return (-1);
 
137
        usleep (CHAR_DELAY);
 
138
        if (upssendchar ((unsigned char)timeptr->tm_sec) != 1)
 
139
                return (-1);
 
140
        usleep (CHAR_DELAY);
 
141
        
 
142
        return (1);     
 
143
}
 
144
#endif
 
145
 
 
146
 
 
147
void upsdrv_initinfo(void) /* Everything OK!!! */
 
148
{
 
149
        unsigned char   i, ret;
 
150
        int             ret0 = 0;
 
151
        char            raw_data[NUM_OF_BYTES_FROM_UPS];
 
152
 
 
153
        /* write constant data for this model */
 
154
 
 
155
        dstate_setinfo("driver.version.internal", "%s", DRV_VERSION);   
 
156
        dstate_setinfo("ups.mfr", "MicroDowell");
 
157
 
 
158
        upsh.new_instcmd = instcmd;
 
159
 
 
160
        for (i = 0; (i < 5) && (ret0 == 0); i++)  /* Try to read from UPS 5 times */
 
161
        {
 
162
                if (upssendchar (SEND_MODEL) != 1) { /* Get model information */
 
163
                        upslogx(LOG_NOTICE, "%s writing error", device_path);
 
164
                } else {
 
165
                        ret = upsrecvchars(raw_data, NUM_MODEL_BYTES);  /* Read data from UPS */
 
166
                        ret0 = ups_model_ok(raw_data, ret);             /* Test if UPS is OK */
 
167
                }
 
168
        }
 
169
        if (ret0 == 0) { 
 
170
                printf ("Unable to identify MicroDowell UPS connected: %d \n",ret0);
 
171
                fatalx("Unable to identify MicroDowell UPS connected");
 
172
        } else {        /* ret0 is the UPS revision number */
 
173
                myups.upsmodel = ret0;
 
174
                ret0 = 0;
 
175
                for (i = 0; (i < 5) && (ret0 == 0); i++)  /* Try to read from UPS 5 times */
 
176
                {
 
177
                        if (upssendchar (SEND_DATA) != 1) {             /* Get UPS status (POLL) */
 
178
                                upslogx(LOG_NOTICE, "%s writing error", device_path);
 
179
                        } else {
 
180
                                ret = upsrecvchars(raw_data, NUM_POLLING_BYTES);
 
181
                                ret0 = polling_ok(raw_data, ret);
 
182
                        }
 
183
                }       
 
184
        }
 
185
        
 
186
        if (ret0 == 0) {
 
187
                printf ("Unable to communicate with MicroDowell UPS connected. \n");
 
188
                fatalx("Unable to communicate with MicroDowell UPS connected");
 
189
        } else {
 
190
                printf ("Communication with MicroDowell UPS started on %s.\n", device_path);
 
191
        }
 
192
        
 
193
        /* now add the instant commands */
 
194
 
 
195
/* these don't actually do anything... */
 
196
#if 0
 
197
        dstate_addcmd("load.on");
 
198
        dstate_addcmd("load.off");
 
199
#endif
 
200
 
 
201
        dstate_addcmd("test.battery.start");
 
202
 
 
203
        upsdrv_updateinfo ();
 
204
}
 
205
 
 
206
void upsdrv_updateinfo(void) /* Everything OK!!! */
 
207
{
 
208
        char            raw_data[NUM_OF_BYTES_FROM_UPS];
 
209
        unsigned char   i, ret;
 
210
        int             ret0 = 0;
 
211
 
 
212
        char    val[32];
 
213
        unsigned char loadpct;
 
214
 
 
215
        nolongertimeout ();
 
216
 
 
217
        for (i = 0; (i < 5) && (ret0 == 0); i++) 
 
218
                {
 
219
                if (upssendchar (SEND_DATA) != 1) { 
 
220
                   upslogx(LOG_NOTICE, "%s writing error", device_path);
 
221
                } else {
 
222
                   ret = upsrecvchars(raw_data, NUM_POLLING_BYTES);
 
223
                   ret0 = polling_ok(raw_data, ret);
 
224
                }
 
225
        }
 
226
        
 
227
        myups.acfreq = 61440.0 / ((255 - (unsigned char)raw_data[ACFREQ_H]) * 256 + (256 - (unsigned char)raw_data[ACFREQ_L]));
 
228
        dstate_setinfo("input.frequency", "%04.1f", myups.acfreq); 
 
229
        
 
230
        myups.utility = 1.84 * (unsigned char)raw_data[UTILITY]; 
 
231
        dstate_setinfo("input.voltage", "%05.1f", myups.utility);
 
232
 
 
233
        myups.battvolt = (1.0 * (unsigned char)raw_data[BATTVOLT]) / 16.82;
 
234
        if (myups.upsmodel > 100)
 
235
                myups.battvolt *= 2.0;
 
236
        dstate_setinfo("battery.voltage", "%05.2f", myups.battvolt);
 
237
 
 
238
        dstate_setinfo("battery.charge", "%05.2f", 24/myups.battvolt*100);
 
239
        
 
240
        myups.loadpct = 0;
 
241
        if (myups.upsmodel > 100)
 
242
        {
 
243
                *val = 0;
 
244
                loadpct = (unsigned char)raw_data[LOADPCT];
 
245
                if (loadpct < LOAD_T0)
 
246
                {
 
247
                        strcat(val, "0  ");
 
248
                }
 
249
                else if ((loadpct >= LOAD_T0) && (loadpct < LOAD_T1))
 
250
                {
 
251
                        strcat(val, "25 ");
 
252
                        myups.loadpct += 25;
 
253
                }
 
254
                else if ((loadpct >= LOAD_T1) && (loadpct < LOAD_T2))
 
255
                {
 
256
                        strcat(val, "50 ");
 
257
                        myups.loadpct += 50;
 
258
                }
 
259
                else if ((loadpct >= LOAD_T2) && (loadpct < LOAD_T3))
 
260
                {
 
261
                        strcat(val, "75 ");
 
262
                        myups.loadpct += 75;
 
263
                }
 
264
                else if ((loadpct >= LOAD_T3) && (loadpct < LOAD_T4))
 
265
                {
 
266
                        strcat(val, "100");
 
267
                        myups.loadpct += 100;
 
268
                }
 
269
                else if (loadpct >= LOAD_T4) 
 
270
                {
 
271
                        strcat(val, "120");
 
272
                        myups.loadpct += 120;
 
273
                }
 
274
                /*setinfo(INFO_LOADPCT, "%s", val);*/
 
275
                dstate_setinfo("ups.load", "%d", (int)(100./47 * loadpct));
 
276
        }
 
277
        else
 
278
        {
 
279
                *val = 0;
 
280
                myups.upstemp = 27.0;
 
281
                strcat(val, "27.0");
 
282
                dstate_setinfo("ups.temperature", "%s", val);
 
283
        }
 
284
        
 
285
        myups.upshour = (unsigned char)raw_data[UPS_HOUR];      /* UPS internal clock */
 
286
        myups.upsmin = (unsigned char)raw_data[UPS_MIN];
 
287
        myups.upssec = (unsigned char)raw_data[UPS_SEC];
 
288
        myups.houron = (unsigned char)raw_data[UPS_HOUR_ON];
 
289
        myups.minon = (unsigned char)raw_data[UPS_MIN_ON];
 
290
        myups.houroff = (unsigned char)raw_data[UPS_HOUR_OFF];
 
291
        myups.sddelay = (unsigned char)raw_data[SD_DELAY];
 
292
 
 
293
        status_init();
 
294
        
 
295
        if (!(raw_data[STATUS] & MAINS_FAILURE))
 
296
        {
 
297
                status_set("OL");
 
298
                myups.battruntime = 0;
 
299
                if (!is_set(myups.status, ST_ONLINE))
 
300
                {
 
301
                        set_flag (&myups.status, ST_ONLINE);
 
302
                        clear_flag (&myups.status, ST_ONBATT);
 
303
                }
 
304
        }
 
305
        else 
 
306
        {
 
307
                status_set("OB");
 
308
                if (!is_set(myups.status, ST_ONBATT))
 
309
                {
 
310
                        set_flag (&myups.status, ST_ONBATT);
 
311
                        clear_flag (&myups.status, ST_ONLINE);
 
312
                }
 
313
                if (myups.maxbattruntime > 0) {
 
314
                        if (++myups.battruntime > myups.maxbattruntime) {
 
315
                                status_set("LB");
 
316
                                set_flag (&myups.status, ST_LOWBATT);
 
317
                        }
 
318
                                
 
319
                }
 
320
        }
 
321
        
 
322
        if (raw_data[STATUS] & LOW_BAT)
 
323
        {
 
324
                status_set("LB");
 
325
                if (!is_set(myups.status, ST_LOWBATT))
 
326
                {
 
327
                        set_flag (&myups.status, ST_LOWBATT);
 
328
                }
 
329
        }
 
330
 
 
331
/* and these are...? */
 
332
#if 0
 
333
        if (raw_data[STATUS] & END_BAT)
 
334
                status_set("EB");
 
335
 
 
336
        if (raw_data[STATUS] & HIGH_TEMP)
 
337
                status_set("HT");
 
338
 
 
339
        if (raw_data[STATUS] & DANGER_TEMP)
 
340
                status_set("DT");
 
341
#endif
 
342
 
 
343
        status_commit ();
 
344
}
 
345
 
 
346
void upsdrv_shutdown(void) /* Everything OK!!! */
 
347
{
 
348
        unsigned char data;
 
349
        
 
350
        printf ("Initiating forced UPS shutdown!\n");
 
351
        
 
352
        if (upssendchar (CMD_SCHEDULING) != 1)
 
353
        usleep (CHAR_DELAY);
 
354
        
 
355
        data = 255;
 
356
        if (upssendchar (data) != 1)
 
357
        usleep (CHAR_DELAY);
 
358
        
 
359
        data = 0;
 
360
        if (upssendchar (data) != 1)
 
361
        usleep (CHAR_DELAY);
 
362
        
 
363
        data = 255;
 
364
        if (upssendchar (data) != 1)
 
365
        usleep (CHAR_DELAY);
 
366
        
 
367
        if (sddelay == 0)
 
368
                sddelay = 60;
 
369
        if ((myups.upsmodel > 21) && (myups.upsmodel <100))
 
370
                sddelay = sddelay / 2 + 1;
 
371
        else if (myups.upsmodel > 130) 
 
372
                sddelay = sddelay / 4 + 1;
 
373
        
 
374
        data = (unsigned char)sddelay;
 
375
        if (upssendchar (data) != 1)
 
376
        usleep (CHAR_DELAY);
 
377
        
 
378
        data = 0;
 
379
        if (upssendchar (data) != 1)
 
380
        sleep (2);
 
381
        
 
382
        if (upssendchar (CMD_STDBY) != 1)
 
383
        usleep (CHAR_DELAY);
 
384
        if (is_set(myups.status, ST_ONBATT)) {
 
385
                if (upssendchar (CMD_BEND) != 1)
 
386
                usleep (CHAR_DELAY);
 
387
        }
 
388
}
 
389
 
 
390
int instcmd(const char *cmdname, const char *extra)
 
391
{
 
392
        if (!strcasecmp(cmdname, "test.battery.start")) {
 
393
                if (upssendchar (CMD_BATTEST) != 1) {
 
394
                        upslogx(LOG_ERR, "Unable to start battery test");
 
395
                        return STAT_INSTCMD_HANDLED;    /* FUTURE: failure */
 
396
                }
 
397
 
 
398
                return STAT_INSTCMD_HANDLED;    /* FUTURE: success */
 
399
        }
 
400
 
 
401
        upslogx(LOG_NOTICE, "instcmd: unknown command [%s]", cmdname);
 
402
        return STAT_INSTCMD_UNKNOWN;
 
403
}
 
404
 
 
405
void upsdrv_help(void)
 
406
{
 
407
        printf ("Not implemented yet:\n" );
 
408
}
 
409
 
 
410
void upsdrv_makevartable(void)
 
411
{
 
412
        addvar(VAR_VALUE, "linevoltage", "Specify line voltage.");
 
413
        addvar(VAR_VALUE, "batruntime", "Specify battery run time.");
 
414
}
 
415
 
 
416
void upsdrv_banner(void)
 
417
{
 
418
        printf("Network UPS Tools - Microdowell UPS driver %s (%s)\n\n", 
 
419
                DRV_VERSION, UPS_VERSION);
 
420
}
 
421
 
 
422
void upsdrv_initups(void)
 
423
{
 
424
        /* this driver doesn't do sanity checks in upsdrv_updateinfo */
 
425
        broken_driver = 1;
 
426
        return;
 
427
 
 
428
        open_serial(device_path, B1200);
 
429
}
 
430
 
 
431
void upsdrv_cleanup(void)
 
432
{
 
433
}