File: hp.c

package info (click to toggle)
nut 0.45.5-rel-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,984 kB
  • ctags: 2,146
  • sloc: ansic: 22,216; sh: 1,138; makefile: 405
file content (328 lines) | stat: -rw-r--r-- 6,976 bytes parent folder | download
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
/* hp.c - support for HP Powertrust UPS

   Copyright (C) 2002  Richard Muratti <rick@ccoz.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
*/

/*
I have two HP Powertrust units
A2997A 1800VA Rack Mount
A2941A  600VA Floor Standing

Here's what i have discovered about these units.

+--------+-----+------+
|Cables  | UPS | DB25 |
|--------+-----+------+
|Receive |  1  |  3   |
|Transmit|  2  |  2   |
|Ground  |  9  |  7   |
|--------+-----+------+

Comms
1200N81

Commands
CAPS
	Online  Offline Comment
	
A	$	!	Status % Low Batt
B	066.7	066.7	Battery Voltage
C	027.0	027.0	Temp
D	??	??
E	??	??
F	050.0	050.0	Frequency
G	??	??
H	??	??
I	??	??	
G	??	??
K	>	<	KnnnKnnn Kill UPS after nnn seconds (ie dont restart)
L	233.0	000.0	Line Voltage
M	??	??
N	NM	NM	[CR] gives CM response
O	231.0	231.0	Output Voltage
P	ddd.d	ddd.d	unsupported/future values
Q	<	<	
R	ddd.d	ddd.d	unsupported/future values
S	<	>	SnnnSnnn Shutdown UPS after nnn is seconds
T	??	??
U	>	>	Self test 10 Sec
V	1.00	1.00	Firmware Version
W	HP^...  HP^...	ID String + Capablilities
X	>	>
Y	??	??
Z	>	<	ZnnnZnnn does something but what???


Notes:
On power up UPS will send '('
On power good UPS will send '$' ie after going off batt
On power fail UPS will send '!'
On low batt UPS will send '%'

It seems that they are sent only once
so its best to poll for them using 'A'

Shutdown/kill only work if UPS is on battery.
To force set UPS in test mode 'U' then issue
SnnnSnnn or KnnnKnnn

UPS sends ')' on successful shutdown



TODO:
What do P & R values represent.
What does ZnnnZnnn do.
What does N do.


if you can add to the above please feel fee to do so
but email me with any changes.

Cheers
Rick
*/


#include "main.h"
#include <sys/ioctl.h>

#define ENDCHAR 13
#define IGNCHARS  "\n"

#define NUM_MODELS  sizeof(modeltab)/sizeof(modeltab[0])
#define SD_RESTART  0
#define SD_HALT     1


/* used external variables */
extern int debug_level;      /* debug level, set by "-D" in main.c */
extern int sddelay;          /* shutdown delay, set by "-d $delay" in main.c */
extern int do_forceshutdown; /* shutdown delay, set by "-k" in main.c */



struct {
	char	*model;
	char	*id_string;
}	modeltab[] =
{
	{ "A2997A","HP^V230^F50^R01800^O230^P1"},
	{ "A2941A","HP^V230^F50^R00600^O230^P1"}
	/* 
	 Add More Models Here
	 Some Models may have more than one id string
	 ie 110 volt countries
	*/
};

int	sdtype;


/* TODO: roll this into upscommon ala bestups */
void sendstring(char * string, int len, int delay)
{
	int	i;

	for (i=0; (i<len); i++) {
	        upssendchar(string[i]);
		usleep(delay);
	}
}


void instcmd (int auxcmd, int dlen, char *data)
{

	/* TODO: reply to upsd? */

	switch (auxcmd) {
		/* Special cases */
		case CMD_BTEST0:	/* Stop Battery Test*/
		        
			break;
		case CMD_BTEST1:	/* Start Battery Test*/
			upssendchar ('U'); 
			break;
		default:
			upslogx(LOG_INFO,"instcmd: unknown or unimplemented type 0x%04x\n",auxcmd);
			return;
	}
}



void setuphandlers()
{
	upsh.instcmd = instcmd;
}



void init_serial(void)
{
        int     clr_bit = TIOCM_DTR | TIOCM_RTS;
        ioctl(upsfd, TIOCMBIC, &clr_bit);
}

void ups_model (void)
{
	int i;

        /* Get Ident String	*/
	installinfo(INFO_UPSIDENT, 'W', ENDCHAR, IGNCHARS);


        /* Lookup UPS Model */
        for (i=0; i < NUM_MODELS;i++) {
           if (strncmp (getdata(INFO_UPSIDENT),modeltab[i].id_string,strlen(modeltab[i].id_string)) == 0)
	      setinfo (INFO_MODEL,modeltab[i].model);
	}
	
	writeinfo();
}

void upsdrv_initinfo(void)
{
	addinfo (INFO_MFR, "HP", 0, 0);
	addinfo (INFO_MODEL, "", 0, 0);
	addinfo (INFO_UPSIDENT, "", 0, 0);
	addinfo (INFO_STATUS, "", 0, 0);
	addinfo (INFO_UTILITY, "",0,0);
	addinfo (INFO_ACFREQ,"",0,0);
	addinfo (INFO_UPSTEMP,"",0,0);
	addinfo (INFO_OUTVOLT,"",0,0);
	addinfo (INFO_BATTVOLT,"",0,0);


	addinfo(INFO_INSTCMD, "", 0, CMD_BTEST0);
	addinfo(INFO_INSTCMD, "", 0, CMD_BTEST1);

	
	ups_model ();

	printf("Detected %s [%s] on %s\n", getdata(INFO_MFR), 
		getdata(INFO_MODEL), device_path);

	setuphandlers();

}

void upsdrv_updateinfo(void)
{
	unsigned char recBuf[10];


	installinfo(INFO_UTILITY, 'L', ENDCHAR, IGNCHARS);
	installinfo(INFO_OUTVOLT, 'O', ENDCHAR, IGNCHARS);
	installinfo(INFO_ACFREQ,  'F', ENDCHAR, IGNCHARS);
	installinfo(INFO_UPSTEMP, 'C', ENDCHAR, IGNCHARS);
	installinfo(INFO_BATTVOLT,'B', ENDCHAR, IGNCHARS);

        upssendchar ('A');
	upsrecv(recBuf,sizeof(recBuf)-1,ENDCHAR,IGNCHARS);
		

        switch (recBuf[0]) {

	case '%':
	   setinfo (INFO_STATUS, "OB LB"); /* Low battery */
	   break;

	case '!':
	   setinfo (INFO_STATUS, "OB");	/* on battery */
	   break;
		
	case '$':
	   setinfo (INFO_STATUS, "OL");	/* on line */
	   break;
	   
        default:
          upslogx(LOG_INFO,"unknown UPS status byte 0x%04x\n",recBuf[0]);

        }
	
	writeinfo();
}

void upsdrv_shutdown(void)
{
	char  tmp [10];


        /* Put UPS in Self Test Mode */
        upssendchar ('U');
	upsrecvchars(tmp,1);
        usleep(100);


        /* Issue Shutdown/Kill Command */
	if (sdtype == SD_RESTART) {
	   printf("UPS shutdown in '%d' seconds.\nUPS will RESTART on AC Power Good\n", sddelay);
  	   sprintf (tmp,"S%03dS%03d",sddelay,sddelay);
	}else{
	   printf("UPS shutdown in '%d' seconds.\nUPS will NOT RESTART on AC Power Good\n", sddelay);
	   sprintf (tmp,"K%03dK%03d",sddelay,sddelay);   
	}
		   
        sendstring (tmp,9,10);
}

void upsdrv_help(void)
{
	printf("\nShutdown types:\n");
	printf("  restart: UPS Will shutdown and will RESTART on AC Power Good (default)\n");
	printf("  halt:    UPS Will shutdown and will NOT RESTART on AC Power Good\n");

}

/* 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, "shutdown", "halt/<restart>");
}

void upsdrv_banner(void)
{
	printf("Network UPS Tools - HP Powertrust UPS driver 0.01 (%s)\n", UPS_VERSION);
}

void upsdrv_initups(void)
{
	open_serial (device_path,B1200);
	init_serial();
	
	
         if (NULL == getval("shutdown"))
	    sdtype = SD_RESTART;
	 else
	    if (strcmp (getval("shutdown"),"halt") == 0)
	       sdtype = SD_HALT;

}

/* tell main how many entries we need */
int upsdrv_infomax(void)
{
        return 32;
}