forked from networkupstools/nut
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgamatronic.c
415 lines (360 loc) · 10.2 KB
/
gamatronic.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/* gamatronic.c
*
* SEC UPS Driver ported to the new NUT API for Gamatronic UPS Usage.
*
* TODO: Replace lots of printf() by upslogx() or upsdebugx() below!
*
* Copyright (C)
* 2001 John Marley <John.Marley@alcatel.com.au>
* 2002 Jules Taplin <jules@netsitepro.co.uk>
* 2002 Eric Lawson <elawson@inficad.com>
* 2005 Arnaud Quette <arnaud.quette@gmail.com>
* 2005 Nadav Moskovitch <blutz@walla.com / http://www.gamatronic.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include "main.h"
#include "serial.h"
#include "gamatronic.h"
#include "nut_stdint.h"
#define DRIVER_NAME "Gamatronic UPS driver"
#define DRIVER_VERSION "0.04"
/* driver description structure */
upsdrv_info_t upsdrv_info = {
DRIVER_NAME,
DRIVER_VERSION,
"John Marley <John.Marley@alcatel.com.au>\n" \
"Jules Taplin <jules@netsitepro.co.uk>\n" \
"Eric Lawson <elawson@inficad.com>\n" \
"Arnaud Quette <arnaud.quette@gmail.com>\n" \
"Nadav Moskovitch <blutz@walla.com / http://www.gamatronic.com>",
DRV_STABLE,
{ NULL }
};
#define ENDCHAR '\r'
#define IGNCHARS ""
#define SER_WAIT_SEC 1 /* allow 3.0 sec for ser_get calls */
#define SER_WAIT_USEC 0
static int sec_upsrecv (char *buf)
{
char lenbuf[4];
int ret;
ser_get_line(upsfd, buf, 140, ENDCHAR, IGNCHARS,SER_WAIT_SEC, SER_WAIT_USEC);
if (buf[0] == SEC_MSG_STARTCHAR) {
switch (buf[1]) {
case SEC_NAK:
return(-1);
case SEC_ACK:
return(0);
case SEC_DATAMSG:
strncpy(lenbuf, buf+2, 3);
lenbuf[3] = '\0';
ret = atoi(lenbuf);
if (ret > 0) {
strcpy(buf,buf+5);
return(ret);
}
else return (-2);
default:
return(-2);
}
}
else
return (-2);
}
static ssize_t sec_cmd(const char mode, const char *command, char *msgbuf, ssize_t *buflen)
{
char msg[140];
ssize_t ret;
memset(msg, 0, sizeof(msg));
/* create the message string */
if (*buflen > 0) {
snprintf(msg, sizeof(msg), "%c%c%03zd%s%s", SEC_MSG_STARTCHAR,
mode, (*buflen)+3, command, msgbuf);
}
else {
snprintf(msg, sizeof(msg), "%c%c003%s", SEC_MSG_STARTCHAR,
mode, command);
}
upsdebugx(1, "PC-->UPS: \"%s\"",msg);
ret = ser_send(upsfd, "%s", msg);
upsdebugx(1, " send returned: %" PRIiSIZE, ret);
if (ret == -1) return -1;
ret = sec_upsrecv(msg);
if (ret < 0) return -1;
strncpy(msgbuf, msg, (size_t)ret);
upsdebugx(1, "UPS<--PC: \"%s\"",msg);
/*
*(msgbuf+ret) = '\0';
*/
*buflen = ret;
return ret;
}
static void addquery(const char *cmd, int field, int varnum, int pollflag)
{
int q;
for (q=0; q<SEC_QUERYLIST_LEN; q++) {
if (sec_querylist[q].command == NULL) {
/* command has not been recorded yet */
sec_querylist[q].command = cmd;
sec_querylist[q].pollflag = pollflag;
upsdebugx(1, " Query %d is %s",q,cmd);
}
if (sec_querylist[q].command == cmd) {
sec_querylist[q].varnum[field-1] = varnum;
upsdebugx(1, " Querying varnum %d",varnum);
break;
}
}
}
static void sec_setinfo(int varnum, char *value)
{
if (*sec_varlist[varnum].setcmd)
{ /*Not empty*/
if (sec_varlist[varnum].flags == FLAG_STRING) {
dstate_setinfo(sec_varlist[varnum].setcmd,"%s", value);
}
else if (sec_varlist[varnum].unit == 1) {
dstate_setinfo(sec_varlist[varnum].setcmd,"%s", value);
}
else if (sec_varlist[varnum].flags == FLAG_MULTI) {
if (atoi(value) < 0) {
dstate_setinfo(sec_varlist[varnum].setcmd,"0");
}
else {
dstate_setinfo(sec_varlist[varnum].setcmd,"%d", atoi(value) * sec_varlist[varnum].unit);
}
}
else {
dstate_setinfo(sec_varlist[varnum].setcmd,"%.1f", atof(value) / sec_varlist[varnum].unit);
}
}
}
static void update_pseudovars( void )
{
status_init();
if(strcmp(sec_varlist[9].value,"1")== 0) {
status_set("OFF");
}
if(strcmp(sec_varlist[76].value,"0")== 0) {
status_set("OL");
}
if(strcmp(sec_varlist[76].value,"1")== 0) {
status_set("OB");
}
if(strcmp(sec_varlist[76].value,"2")== 0) {
status_set("BYPASS");
}
if(strcmp(sec_varlist[76].value,"3")== 0) {
status_set("TRIM");
}
if(strcmp(sec_varlist[76].value,"4")== 0) {
status_set("BOOST");
}
if(strcmp(sec_varlist[10].value,"1")== 0) {
status_set("OVER");
}
if(strcmp(sec_varlist[22].value,"1")== 0) {
status_set("LB");
}
if(strcmp(sec_varlist[19].value,"2")== 0) {
status_set("RB");
}
status_commit();
}
static void sec_poll ( int pollflag ) {
ssize_t msglen;
int f, q;
char retbuf[140], *n, *r;
for (q=0; q<SEC_QUERYLIST_LEN; q++) {
if (sec_querylist[q].command == NULL) break;
if (sec_querylist[q].pollflag != pollflag) continue;
msglen = 0;
sec_cmd(SEC_POLLCMD, sec_querylist[q].command, retbuf, &msglen);
r = retbuf;
*(r+msglen) = '\0';
for (f=0; f<SEC_MAXFIELDS; f++) {
n = strchr(r, ',');
if (n != NULL) *n = '\0';
if (sqv(q,f) > 0) {
if (strcmp(sec_varlist[sqv(q,f)].value, r) != 0) {
#ifdef HAVE_PRAGMAS_FOR_GCC_DIAGNOSTIC_IGNORED_FORMAT_TRUNCATION
#pragma GCC diagnostic push
#endif
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_FORMAT_TRUNCATION
#pragma GCC diagnostic ignored "-Wformat-truncation"
#endif
/* NOTE: We intentionally limit the amount
* of characters picked from "r" buffer
* into respectively sized "*.value"
*/
int len = snprintf(sec_varlist[sqv(q,f)].value,
sizeof(sec_varlist[sqv(q,f)].value), "%s", r);
if (len < 0) {
upsdebugx(1, "%s: got an error while extracting value", __func__);
}
if ((intmax_t)len > (intmax_t)sizeof(sec_varlist[sqv(q,f)].value)
|| (intmax_t)strnlen(r, sizeof(retbuf)) > (intmax_t)sizeof(sec_varlist[sqv(q,f)].value)
) {
upsdebugx(1, "%s: value was truncated", __func__);
}
#ifdef HAVE_PRAGMAS_FOR_GCC_DIAGNOSTIC_IGNORED_FORMAT_TRUNCATION
#pragma GCC diagnostic pop
#endif
sec_setinfo(sqv(q,f), r);
}
/* If SEC VAR is alarm and it's on, add it to the alarm property */
if (sec_varlist[sqv(q,f)].flags & FLAG_ALARM && strcmp(r,"1")== 0) {
alarm_set(sec_varlist[sqv(q,f)].name);
}
}
if (n == NULL) break;
r = n+1;
}
}
}
void upsdrv_initinfo(void)
{
ssize_t msglen;
int v;
char *a, *p, avail_list[300];
/* find out which variables/commands this UPS supports */
msglen = 0;
sec_cmd(SEC_POLLCMD, SEC_AVAILP1, avail_list, &msglen);
p = avail_list + msglen;
if (p != avail_list) *p++ = ',';
msglen = 0;
sec_cmd(SEC_POLLCMD, SEC_AVAILP2, p, &msglen);
*(p+msglen) = '\0';
if (strlen(avail_list) == 0) {
fatalx(EXIT_FAILURE, "No available variables found!");
}
a = avail_list;
while ((p = strtok(a, ",")) != NULL) {
a = NULL;
v = atoi(p);
/* don't bother adding a write-only variable */
if (sec_varlist[v].flags == FLAG_WONLY) continue;
addquery(sec_varlist[v].cmd, sec_varlist[v].field, v, sec_varlist[v].poll);
}
/* poll one time values */
sec_poll(FLAG_POLLONCE);
printf("UPS: %s %s\n", dstate_getinfo("ups.mfr"), dstate_getinfo("ups.model"));
}
void upsdrv_updateinfo(void)
{
alarm_init();
/* poll status values values */
sec_poll(FLAG_POLL);
alarm_commit();
update_pseudovars();
dstate_dataok();
}
void upsdrv_shutdown(void)
{
ssize_t msglen;
char msgbuf[SMALLBUF];
msglen = snprintf(msgbuf, sizeof(msgbuf), "-1");
sec_cmd(SEC_SETCMD, SEC_SHUTDOWN, msgbuf, &msglen);
msglen = snprintf(msgbuf, sizeof(msgbuf), "1");
sec_cmd(SEC_SETCMD, SEC_AUTORESTART, msgbuf, &msglen);
msglen = snprintf(msgbuf, sizeof(msgbuf), "2");
sec_cmd(SEC_SETCMD, SEC_SHUTTYPE,msgbuf, &msglen);
msglen = snprintf(msgbuf, sizeof(msgbuf), "5");
sec_cmd(SEC_SETCMD, SEC_SHUTDOWN, msgbuf, &msglen);
}
/*
int instcmd(const char *cmdname, const char *extra)
{
if (!strcasecmp(cmdname, "test.battery.stop")) {
ser_send_buf(upsfd, ...);
return STAT_INSTCMD_HANDLED;
}
upslogx(LOG_NOTICE, "instcmd: unknown command [%s]", cmdname);
return STAT_INSTCMD_UNKNOWN;
}
*/
void upsdrv_help(void)
{
}
/* list flags and values that you want to receive via -x */
void upsdrv_makevartable(void)
{
/* allow '-x xyzzy' */
/* addvar(VAR_FLAG, "xyzzy", "Enable xyzzy mode"); */
/* allow '-x foo=<some value>' */
/* addvar(VAR_VALUE, "foo", "Override foo setting"); */
}
static void setup_serial(const char *port)
{
char temp[140];
int i;
ssize_t ret;
/* Detect the ups baudrate */
for (i=0; i<5; i++) {
ser_set_speed(upsfd, device_path, baud_rates[i].rate);
ret = ser_send(upsfd, "^P003MAN");
ret = sec_upsrecv(temp);
if (ret >= -1) break;
}
if (i == 5) {
printf("Can't talk to UPS on port %s!\n",port);
printf("Check the cabling and portname and try again\n");
printf("Please note that this driver only support UPS Models with SEC Protocol\n");
ser_close(upsfd, device_path);
exit (1);
}
else
printf("Connected to UPS on %s baudrate: %" PRIuSIZE "\n",
port, baud_rates[i].name);
}
void upsdrv_initups(void)
{
upsfd = ser_open(device_path);
setup_serial(device_path);
/* upsfd = ser_open(device_path); */
/* ser_set_speed(upsfd, device_path, B1200); */
/* probe ups type */
/* to get variables and flags from the command line, use this:
*
* first populate with upsdrv_buildvartable above, then...
*
* set flag foo : /bin/driver -x foo
* set variable 'cable' to '1234' : /bin/driver -x cable=1234
*
* to test flag foo in your code:
*
* if (testvar("foo"))
* do_something();
*
* to show the value of cable:
*
* if ((cable == getval("cable")))
* printf("cable is set to %s\n", cable);
* else
* printf("cable is not set!\n");
*
* don't use NULL pointers - test the return result first!
*/
/* the upsh handlers can't be done here, as they get initialized
* shortly after upsdrv_initups returns to main.
*/
}
void upsdrv_cleanup(void)
{
/* free(dynamic_mem); */
ser_close(upsfd, device_path);
}