download nitram.c
Language: C
License: GPL
Copyright: (C) 2005 Nadine Albiez (C) 2005 Olivier Albiez
LOC: 263
Project Info
nut
Server: Debian-SVN
Type: svn
...an‑SVN\n\nut\trunk\drivers\
   al175.c
   al175.h
   apc-hid.c
   apc-hid.h
   apccmib.h
   apcsmart.c
   apcsmart.h
   bcmxcp.c
   bcmxcp.h
   bcmxcp_io.h
   bcmxcp_ser.c
   bcmxcp_usb.c
   belkin-hid.c
   belkin-hid.h
   belkin.c
   belkin.h
   belkinunv.c
   belkinunv.h
   bestfcom.c
   bestfcom.h
   bestuferrups.c
   bestuferrups.h
   bestups.c
   bestups.h
   cpsups.c
   cpsups.h
   cyberpower.c
   cyberpower.h
   dstate-hal.c
   dstate-hal.h
   dstate.c
   dstate.h
   dummy-ups.c
   dummy-ups.h
   energizerups.c
   etapro.c
   etapro.h
   everups.c
   everups.h
   explore-hid.c
   explore-hid.h
   gamatronic.c
   gamatronic.h
   genericups.c
   genericups.h
   hidparser.c
   hidparser.h
   hidtypes.h
   ietfmib.h
   isbmex.c
   isbmex.h
   libhid.c
   libhid.h
   libshut.c
   libshut.h
   libusb.c
   libusb.h
   liebert.c
   liebert.h
   main-hal.c
   main-hal.h
   main.c
   main.h
   Makefile.am
   masterguard.c
   masterguard.h
   megatec.c
   megatec.h
   megatec_usb.c
   metasys.c
   metasys.h
   mge-hid.c
   mge-hid.h
   mge-shut.c
   mge-shut.h
   mge-utalk.c
   mge-utalk.h
   mgemib.h
   netvisionmib.h
   nitram.c
   nitram.h
   nut_usb.c
   nut_usb.h
   oneac.c
   oneac.h
   optiups.c
   optiups.h
   powercom.c
   powercom.h
   powerpanel.c
   powerpanel.h
   pwmib.h
   rhino.c
   safenet.c
   safenet.h
   serial.c
   serial.h
   skel.c
   snmp-ups.c
   snmp-ups.h
   solis.c
   solis.h
   tripplite-hid.c
   tripplite-hid.h
   tripplite.c
   tripplite.h
   tripplite_usb.c
   tripplitesu.c
   tripplitesu.h
   upscode2.c
   upscode2.h
   upsdrvctl.c
   usbhid-ups.c
   usbhid-ups.h
   victronups.c
   victronups.h

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
/* nitram.c - Network UPS Tools driver for Nitram Systems units
   This driver support:
    - Nitram Elite 2005 (manufactured by Cyber Power)

   Copyrights:
   (C) 2005 Olivier Albiez <oalbiez@free.fr>
   (C) 2005 Nadine Albiez <oalbiez@free.fr>

   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 <string.h>
#include <sys/ioctl.h>

#include "main.h"
#include "serial.h"
#include "nitram.h"

#define ENDCHAR	'\r'
#define IGNCHARS ""
#define UPSDELAY 50000

#define VOLTAGE_RANGE 3


struct status_t
{
	char input_tag;
	char input[5];
	char output_tag;
	char output[5];
	char load_tag;
	char load[3];
	char battery_tag;
	char battery[3];
	char temperature_tag;
	char temperature[3];
	char frequency_tag;
	char frequency[5];
	char flags_tag;
	char flags[2];
};


struct fields_t
{
	char* field[10];
	int count;
};


struct buffer_t
{
	char frame[128];
	char* payload;
	size_t length;
};


static void send_command(const char* command)
{
	ser_send_pace(upsfd, UPSDELAY, "%s\r", command);
}


static void send_command_and_flush(const char* command)
{
	struct buffer_t reply;
	send_command(command);
	usleep(100000);
	ser_get_line(upsfd, reply.frame, sizeof(reply.frame), ENDCHAR, IGNCHARS, 3, 0);
}


static int execute_command(const char* command, struct buffer_t* reply)
{
	int status;
	send_command(command);
	usleep(100000);
	status = ser_get_line(upsfd, reply->frame, sizeof(reply->frame), ENDCHAR, IGNCHARS, 3, 0);

	if (status <= 1)
	{
		ser_comm_fail("Invalid frame size");
		memset(reply, 0, sizeof(struct buffer_t));
		return 0;
	}
	else if (reply->frame[0] != '#')
	{
		ser_comm_fail("Invalid frame marker 0x%02x", reply->frame[0]);
		memset(reply, 0, sizeof(struct buffer_t));
		return 0;
	}

	ser_comm_good();
	reply->payload = reply->frame+1;
	reply->length = status-1;
	return 1;
}


static struct fields_t extract_fields(struct buffer_t* reply)
{
	struct fields_t result;
	char* payload = reply->payload;
	char* position;
	result.count = 0;
	while ((position = strchr(payload, ',')) != NULL)
	{
		*position++ = 0;
		result.field[result.count] = payload;
		++result.count;
		payload = position;
	}
	result.field[result.count] = payload;
	++result.count;
	return result;
}


static int extract_status(struct buffer_t* reply, struct status_t** status)
{
	if (reply->length != sizeof(struct status_t))
	{
		return 0;
	}

	*status = (struct status_t*)(reply->payload);
	#define CHECK_AND_CLEAR_TAG(name, value) if ((*status)->name != (value)) return 0; else (*status)->name = 0
	CHECK_AND_CLEAR_TAG(input_tag, 'I');
	CHECK_AND_CLEAR_TAG(output_tag, 'O');
	CHECK_AND_CLEAR_TAG(load_tag, 'L');
	CHECK_AND_CLEAR_TAG(battery_tag, 'B');
	CHECK_AND_CLEAR_TAG(temperature_tag, 'T');
	CHECK_AND_CLEAR_TAG(frequency_tag, 'F');
	CHECK_AND_CLEAR_TAG(flags_tag, 'S');
	#undef CHECK_AND_CLEAR_TAG
	return 1;
}


static int get_identification(struct buffer_t* reply)
{
	int tries;
	ser_flush_in(upsfd, IGNCHARS, 0);
	for (tries = 0; tries < 3; tries++)
	{
		if (execute_command("P4", reply) == 1)
			return 1;
	}
	upslogx(LOG_INFO, "Giving up on hardware detection after 3 tries");
	return 0;
}


static int instcmd(const char *command, const char *extra)
{
	#define DEFINE_COMMAND(name, nitram_command) \
		if (strcasecmp(command, (name)) == 0) \
		{ \
			send_command_and_flush(nitram_command); \
			return STAT_INSTCMD_HANDLED; \
		}
	DEFINE_COMMAND("test.battery.start", "T.1");
	DEFINE_COMMAND("test.battery.stop", "CT");
	DEFINE_COMMAND("beeper.on", "C7:1");
	DEFINE_COMMAND("beeper.off", "C7:0");
	#undef DEFINE_COMMAND

	upslogx(LOG_NOTICE, "instcmd: unknown command [%s]", command);
	return STAT_INSTCMD_UNKNOWN;
}


static int setvar(const char *varname, const char *val)
{
	struct buffer_t reply;
	char command[50];

	if (strcasecmp(varname, "battery.charge.low") == 0)
	{
		snprintf(command, sizeof(command), "C4:%s", val);
		command[sizeof(command)] = 0;
		if (execute_command(command, &reply) == 1)
		{
			dstate_setinfo("battery.charge.low", val);
		}
		return STAT_INSTCMD_HANDLED;
	}

	upslogx(LOG_NOTICE, "setvar: unknown var [%s]", varname);
	return STAT_SET_UNKNOWN;
}


void upsdrv_initinfo(void)
{
	struct buffer_t reply;
	struct fields_t fields;

	if (get_identification(&reply) == 1)
	{
		fields = extract_fields(&reply);
		dstate_setinfo("ups.model", "%s", fields.field[0]);
		dstate_setinfo("ups.firmware", "%s", fields.field[1]);
		dstate_setinfo("ups.mfr", "CyberPower for Nitram");
		dstate_setinfo("driver.version.internal", "%s", DRV_VERSION);
	}
	else
	{
		fatalx("Unable to get initial hardware info string");
	}

	dstate_setinfo("battery.charge.low", "20");

	if (execute_command("P1", &reply) == 1)
	{
		fields = extract_fields(&reply);
		dstate_setinfo("output.voltage.nominal", "%s", fields.field[0]);
		dstate_setinfo("input.voltage.maximum", "%s", fields.field[1]);
		dstate_setinfo("input.voltage.minimum", "%s", fields.field[2]);
		dstate_setinfo("battery.charge.low", "%s", fields.field[3]);
	}

	if (execute_command("P3", &reply) == 1)
	{
		fields = extract_fields(&reply);
		dstate_setinfo("battery.voltage", "%s", fields.field[0]);
		dstate_setinfo("battery.packs", "%s", fields.field[1]);
		dstate_setinfo("battery.current", "%s", fields.field[2]);
	}

	dstate_setflags("battery.charge.low", ST_FLAG_STRING | ST_FLAG_RW);
	dstate_setaux("battery.charge.low", 20);
	dstate_addcmd("test.battery.start");
	dstate_addcmd("test.battery.stop");
	dstate_addcmd("beeper.on");
	dstate_addcmd("beeper.off");
	upsh.instcmd = instcmd;
	upsh.setvar = setvar;
}


void upsdrv_updateinfo(void)
{
	struct buffer_t reply;
	struct status_t* status;

	if (execute_command("D", &reply) == 0)
	{
		dstate_datastale();
		return;
	}

	if (extract_status(&reply, &status) == 0)
	{
		dstate_datastale();
		return;
	}

	dstate_setinfo("input.frequency", "%s", status->frequency);
	dstate_setinfo("ups.temperature", "%s", status->temperature);
	dstate_setinfo("battery.charge", "%s", status->battery);
	dstate_setinfo("ups.load", "%s", status->load);
	dstate_setinfo("input.voltage", "%s", status->input);
	dstate_setinfo("output.voltage", "%s", status->output);

	status_init();
	if (status->flags[0] & 0x40)
		status_set("OB");
	else
		status_set("OL");

	if (status->flags[0] & 0x20)
		status_set("LB");

	if (atoi(status->input) > atoi(status->output) + VOLTAGE_RANGE)
		status_set("TRIM");

	if (atoi(status->input) < atoi(status->output) - VOLTAGE_RANGE)
		status_set("BOOST");

	if ((atoi(status->battery) == 100) && !(status->flags[0] & 0x40))
		status_set("BYPASS");

	status_commit();
	dstate_dataok();

	upsdebugx(LOG_DEBUG, "status:%s; load:%s; battery:%s; input:%s; output:%s; frequency:%s;",
		dstate_getinfo("ups.status"),
		status->load,
		status->battery,
		status->input,
		status->output,
		status->frequency);
}


void upsdrv_shutdown(void)
{
	fatalx("shutdown not supported");
}


void upsdrv_help(void)
{
}


void upsdrv_makevartable(void)
{
}


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


void upsdrv_initups(void)
{
	int dtr_bit = TIOCM_DTR;
	int rts_bit = TIOCM_RTS;

	upsfd = ser_open(device_path);
	ser_set_speed(upsfd, device_path, B2400);

	/* dtr high, rts high */
	ioctl(upsfd, TIOCMBIS, &rts_bit);
	ioctl(upsfd, TIOCMBIS, &dtr_bit);
}


void upsdrv_cleanup(void)
{
	ser_close(upsfd, device_path);
}



About Koders | Resources | Downloads | Support | Black Duck | Submit Project | Terms of Service | DMCA | Privacy Policy | Site Map| Contact Us