1
/* Written to work with MicroDowell BBox UPS.
2
This model is based on MicroDowell (www.microdowell.com) models.
4
Copyright (C) 2000 Gilberto Iob <research@microdowell.com>
5
Copyright (C) 2002 Aleksandar Topuzovic <aleksandar.topuzovic@avl.com>
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.
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.
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
22
#define DRV_VERSION "0.02"
25
#include "microdowell.h"
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);
33
void set_flag (int *val, int flag)
35
*val = (*val |= flag);
38
void clear_flag (int *val, int flag)
40
*val = (*val ^= (*val & flag));
43
int is_set (int num, int flag)
45
return ((num & flag) == flag);
48
static int polling_ok (char *buf, unsigned buflen) /* Everything OK!!! */
50
if ((buflen==NUM_POLLING_BYTES) && (buf[0]==91) && (buf[NUM_POLLING_BYTES-1]==93) && (buf[1]==1))
52
return (1); /* Polling OK */
56
return (0); /* Polling FAILED */
61
Indetify UPS Model and revision
63
static int ups_model_ok (char *buf, unsigned buflen) /* Everything OK!!! */
68
if ((buflen==NUM_MODEL_BYTES) && (buf[0]==91) &&
69
(buf[NUM_MODEL_BYTES-1]==93) && (buf[1]==10))
71
if ((buf[2] == 'S') && (buf[3] == 'H'))
73
upsRelease = (buf[4] - '0') * 10 + buf[6] - '0';
76
dstate_setinfo("ups.model", "BBox Pro PnP");
77
upslogx(LOG_NOTICE, "BBox Pro PnP rev. %d on %s.\n", upsRelease, device_path);
79
dstate_setinfo("ups.model", "BBox Pro USB");
80
upslogx(LOG_NOTICE, "BBox Pro USB rev. %d on %s.\n", upsRelease, device_path);
82
sprintf (upsSerial, "SH%c.%c", buf[4], buf[6]);
83
dstate_setinfo("ups.serial", "%s", upsSerial);
84
return (100 + upsRelease);
86
else if ((buf[2] == 'M') && (buf[3] == 'D'))
88
upsRelease = (buf[4] - '0') * 10 + buf[6] - '0';
91
dstate_setinfo("ups.model", "BBox Interactive PnP");
92
upslogx(LOG_NOTICE, "BBox Interactive PnP rev. %d on %s.\n", upsRelease, device_path);
94
dstate_setinfo("ups.model",
95
"BBox Interactive USB");
96
upslogx(LOG_NOTICE, "BBox Interactive USB rev. %d on %s.\n", upsRelease, device_path);
98
sprintf (upsSerial, "MD%c.%c", buf[4], buf[6]);
99
dstate_setinfo("ups.serial", "%s", upsSerial);
102
else if ((buf[2] == 'B') && (buf[3] == '5'))
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);
118
#if 0 /* not being used right now */
119
static int setupstime () /* Everything OK!!! */
123
unsigned char upshour;
126
timeptr = localtime (&now);
128
if (upssendchar (CMD_SETTIMER) != 1)
131
upshour = (unsigned char) ((timeptr->tm_wday - 1) * 24 + timeptr->tm_hour);
132
if (upssendchar (upshour) != 1)
135
if (upssendchar ((unsigned char)timeptr->tm_min) != 1)
138
if (upssendchar ((unsigned char)timeptr->tm_sec) != 1)
147
void upsdrv_initinfo(void) /* Everything OK!!! */
149
unsigned char i, ret;
151
char raw_data[NUM_OF_BYTES_FROM_UPS];
153
/* write constant data for this model */
155
dstate_setinfo("driver.version.internal", "%s", DRV_VERSION);
156
dstate_setinfo("ups.mfr", "MicroDowell");
158
upsh.new_instcmd = instcmd;
160
for (i = 0; (i < 5) && (ret0 == 0); i++) /* Try to read from UPS 5 times */
162
if (upssendchar (SEND_MODEL) != 1) { /* Get model information */
163
upslogx(LOG_NOTICE, "%s writing error", device_path);
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 */
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;
175
for (i = 0; (i < 5) && (ret0 == 0); i++) /* Try to read from UPS 5 times */
177
if (upssendchar (SEND_DATA) != 1) { /* Get UPS status (POLL) */
178
upslogx(LOG_NOTICE, "%s writing error", device_path);
180
ret = upsrecvchars(raw_data, NUM_POLLING_BYTES);
181
ret0 = polling_ok(raw_data, ret);
187
printf ("Unable to communicate with MicroDowell UPS connected. \n");
188
fatalx("Unable to communicate with MicroDowell UPS connected");
190
printf ("Communication with MicroDowell UPS started on %s.\n", device_path);
193
/* now add the instant commands */
195
/* these don't actually do anything... */
197
dstate_addcmd("load.on");
198
dstate_addcmd("load.off");
201
dstate_addcmd("test.battery.start");
203
upsdrv_updateinfo ();
206
void upsdrv_updateinfo(void) /* Everything OK!!! */
208
char raw_data[NUM_OF_BYTES_FROM_UPS];
209
unsigned char i, ret;
213
unsigned char loadpct;
217
for (i = 0; (i < 5) && (ret0 == 0); i++)
219
if (upssendchar (SEND_DATA) != 1) {
220
upslogx(LOG_NOTICE, "%s writing error", device_path);
222
ret = upsrecvchars(raw_data, NUM_POLLING_BYTES);
223
ret0 = polling_ok(raw_data, ret);
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);
230
myups.utility = 1.84 * (unsigned char)raw_data[UTILITY];
231
dstate_setinfo("input.voltage", "%05.1f", myups.utility);
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);
238
dstate_setinfo("battery.charge", "%05.2f", 24/myups.battvolt*100);
241
if (myups.upsmodel > 100)
244
loadpct = (unsigned char)raw_data[LOADPCT];
245
if (loadpct < LOAD_T0)
249
else if ((loadpct >= LOAD_T0) && (loadpct < LOAD_T1))
254
else if ((loadpct >= LOAD_T1) && (loadpct < LOAD_T2))
259
else if ((loadpct >= LOAD_T2) && (loadpct < LOAD_T3))
264
else if ((loadpct >= LOAD_T3) && (loadpct < LOAD_T4))
267
myups.loadpct += 100;
269
else if (loadpct >= LOAD_T4)
272
myups.loadpct += 120;
274
/*setinfo(INFO_LOADPCT, "%s", val);*/
275
dstate_setinfo("ups.load", "%d", (int)(100./47 * loadpct));
280
myups.upstemp = 27.0;
282
dstate_setinfo("ups.temperature", "%s", val);
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];
295
if (!(raw_data[STATUS] & MAINS_FAILURE))
298
myups.battruntime = 0;
299
if (!is_set(myups.status, ST_ONLINE))
301
set_flag (&myups.status, ST_ONLINE);
302
clear_flag (&myups.status, ST_ONBATT);
308
if (!is_set(myups.status, ST_ONBATT))
310
set_flag (&myups.status, ST_ONBATT);
311
clear_flag (&myups.status, ST_ONLINE);
313
if (myups.maxbattruntime > 0) {
314
if (++myups.battruntime > myups.maxbattruntime) {
316
set_flag (&myups.status, ST_LOWBATT);
322
if (raw_data[STATUS] & LOW_BAT)
325
if (!is_set(myups.status, ST_LOWBATT))
327
set_flag (&myups.status, ST_LOWBATT);
331
/* and these are...? */
333
if (raw_data[STATUS] & END_BAT)
336
if (raw_data[STATUS] & HIGH_TEMP)
339
if (raw_data[STATUS] & DANGER_TEMP)
346
void upsdrv_shutdown(void) /* Everything OK!!! */
350
printf ("Initiating forced UPS shutdown!\n");
352
if (upssendchar (CMD_SCHEDULING) != 1)
356
if (upssendchar (data) != 1)
360
if (upssendchar (data) != 1)
364
if (upssendchar (data) != 1)
369
if ((myups.upsmodel > 21) && (myups.upsmodel <100))
370
sddelay = sddelay / 2 + 1;
371
else if (myups.upsmodel > 130)
372
sddelay = sddelay / 4 + 1;
374
data = (unsigned char)sddelay;
375
if (upssendchar (data) != 1)
379
if (upssendchar (data) != 1)
382
if (upssendchar (CMD_STDBY) != 1)
384
if (is_set(myups.status, ST_ONBATT)) {
385
if (upssendchar (CMD_BEND) != 1)
390
int instcmd(const char *cmdname, const char *extra)
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 */
398
return STAT_INSTCMD_HANDLED; /* FUTURE: success */
401
upslogx(LOG_NOTICE, "instcmd: unknown command [%s]", cmdname);
402
return STAT_INSTCMD_UNKNOWN;
405
void upsdrv_help(void)
407
printf ("Not implemented yet:\n" );
410
void upsdrv_makevartable(void)
412
addvar(VAR_VALUE, "linevoltage", "Specify line voltage.");
413
addvar(VAR_VALUE, "batruntime", "Specify battery run time.");
416
void upsdrv_banner(void)
418
printf("Network UPS Tools - Microdowell UPS driver %s (%s)\n\n",
419
DRV_VERSION, UPS_VERSION);
422
void upsdrv_initups(void)
424
/* this driver doesn't do sanity checks in upsdrv_updateinfo */
428
open_serial(device_path, B1200);
431
void upsdrv_cleanup(void)