57
58
static float lowvolt = 0, voltrange;
58
59
static int lownorm, highnorm, poll_failures = 0;
60
void guessmodel(const char *raw)
61
static void guessmodel(const char *raw)
66
67
printf ("0 1 2 3 \n");
67
68
printf ("012345678901234567890123567890123456789\n");
68
69
printf ("%s\n", raw);
69
strlcpy(mstr, &raw[17], sizeof(mstr));
70
mstr = xstrdup(&raw[18]);
70
71
mstr[10] = '\0'; /* 10 chars max, per the protocol */
72
73
/* trim whitespace */
75
76
dstate_setinfo("ups.model", "SMS %s", mstr);
79
void getbaseinfo(void)
82
static void getbaseinfo(void)
81
84
char temp[256], model[32], *raw;
84
87
/* dummy read attempt to sync - throw it out */
86
upsrecv(temp, sizeof(temp), ENDCHAR, "");
88
ser_send(upsfd, "I\r");
89
ser_get_line(upsfd, temp, sizeof(temp), ENDCHAR, "", 3, 0);
88
91
/* now retrieve information and parse */
90
upsrecv(temp, sizeof(temp), ENDCHAR, "");
92
ser_send(upsfd, "I\r");
93
ser_get_line(upsfd, temp, sizeof(temp), ENDCHAR, "", 3, 0);
91
94
raw = xstrdup(temp);
93
96
if (temp[0] != '#')
157
160
/* paranoia - cancel any shutdown that might already be running */
161
ser_send(upsfd, "C\r");
161
int instcmd(const char *cmdname, const char *extra)
164
static int instcmd(const char *cmdname, const char *extra)
163
166
if (!strcasecmp(cmdname, "test.battery.stop")) {
167
ser_send(upsfd, "CT\r");
165
168
return STAT_INSTCMD_HANDLED;
168
171
if (!strcasecmp(cmdname, "test.battery.start")) {
169
upssend("TL\r");/* start battery test until bat low */
172
ser_send(upsfd, "TL\r");/* start battery test until bat low */
170
173
return STAT_INSTCMD_HANDLED;
173
176
if (!strcasecmp(cmdname, "test.failure.start")) {
177
ser_send(upsfd, "T\r");
175
178
return STAT_INSTCMD_HANDLED;
178
181
if (!strcasecmp(cmdname, "shutdown.return")) {
179
182
/* shutdown and restart */
181
upssend("S.3R0003\r");
183
ser_send(upsfd, "C\r");
184
ser_send(upsfd, "S.3R0003\r");
182
185
return STAT_INSTCMD_HANDLED;
185
188
if (!strcasecmp(cmdname, "shutdown.stayoff")) {
186
189
/* shutdown now (one way) */
190
ser_send(upsfd, "C\r");
191
ser_send(upsfd, "S.3\r");
189
192
return STAT_INSTCMD_HANDLED;
192
195
if (!strcasecmp(cmdname, "shutdown.stop")) {
193
196
/* Cancel Shutdown */
197
ser_send(upsfd, "C\r");
195
198
return STAT_INSTCMD_HANDLED;
198
201
if (!strcasecmp(cmdname, "reset.watchdog")) {
199
202
/* WATCHDOG Crontab Function */
201
upssend("S05R0003\r");
203
ser_send(upsfd, "C\r");
204
ser_send(upsfd, "S05R0003\r");
202
205
return STAT_INSTCMD_HANDLED;
228
231
void upsdrv_updateinfo(void)
230
233
char temp[256], utility[16], loadpct[16], acfreq[16], battvolt[16],
231
upstemp[16], stat[16], outvolt[16];
234
upstemp[16], pstat[16], outvolt[16];
238
ser_send(upsfd, "Q1\r");
237
ret = upsrecv (temp, sizeof(temp), ENDCHAR, "");
240
ret = ser_get_line(upsfd, temp, sizeof(temp), ENDCHAR, "", 3, 0);
239
242
/* sanity checks for poll data */
240
243
if (strlen(temp) < 46) {
270
273
sscanf(temp, "%*c%s %*s %s %s %s %s %s %s", utility, outvolt, loadpct,
271
acfreq, battvolt, upstemp, stat);
274
acfreq, battvolt, upstemp, pstat);
273
276
dstate_setinfo("output.voltage", "%s", outvolt);
274
277
dstate_setinfo("input.voltage", "%s", utility);
318
321
/* power down the attached load immediately */
319
322
void upsdrv_shutdown(void)
321
char temp[256], stat[32];
324
char temp[256], pstat[32];
323
326
/* basic idea: find out line status and send appropriate command */
326
upsrecv (temp, sizeof(temp), ENDCHAR, "");
327
sscanf (temp, "%*s %*s %*s %*s %*s %*s %*s %s", stat);
328
ser_send(upsfd, "Q1\r");
329
ser_get_line(upsfd, temp, sizeof(temp), ENDCHAR, "", 3, 0);
330
sscanf (temp, "%*s %*s %*s %*s %*s %*s %*s %s", pstat);
329
332
/* on battery: send S01<cr>, ups will return by itself on utility */
330
333
/* on line: send S01R0003<cr>, ups will cycle and return soon */
335
ser_send(upsfd, "S01");
334
if (stat[0] == '0') { /* on line */
337
if (pstat[0] == '0') { /* on line */
335
338
printf("On line, sending shutdown+return command...\n");
339
ser_send(upsfd, "R0003");
338
341
printf("On battery, sending normal shutdown command...\n");
340
upssendchar(13); /* end sequence */
343
ser_send_char(upsfd, 13); /* end sequence */
343
346
void upsdrv_help(void)
358
361
void upsdrv_initups(void)
360
open_serial(device_path, B2400);
363
upsfd = ser_open(device_path);
364
ser_set_speed(upsfd, device_path, B2400);
363
367
void upsdrv_cleanup(void)
369
ser_close(upsfd, device_path);