A
download victronups.c
Language: C
License: GPL
Copyright: (C) 2004 Gert Lynge (C) 2000 Radek Benedikt (C) 2001 Daniel.Prynych (C) 2003 Gert Lynge (C) 1999 Russell Kroll
LOC: 380
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
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
/* victronups.c - Model specific routines for GE/IMV/Victron  units
 * Match, Match Lite, NetUps
 *
 * Copyright (C) 1999  Russell Kroll <rkroll@exploits.org>
 * Copyright (C) 2000  Radek Benedikt <benedikt@lphard.cz>
 * old style "victronups"
 * Copyright (C) 2001  Daniel.Prynych <Daniel.Prynych@hornet.cz>
 * porting to now style "newvictron"
 * Copyright (C) 2003  Gert Lynge <gert@lynge.org>
 * Porting to new serial functions. Now removes \n from data (was causing
 * periodic misreadings of temperature and voltage levels)
 * Copyright (C) 2004  Gert Lynge <gert@lynge.org>
 * Implemented some Instant Commands.
 *
 * 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 "victronups.h"

#define ENDCHAR	'\r'
#define IGNCHARS "\n"

#define UPS_DELAY 150000
#define UPS_LONG_DELAY 450000

#define VICTRON_OVER 128
#define VICTRON_RB 1
#define VICTRON_OB 2
#define VICTRON_LB 4

#define VICTRON_NO_TEST		1
#define VICTRON_ABORT_TEST	2
#define VICTRON_SYSTEM_TEST	3
#define VICTRON_BATTERY_TEST	4
#define VICTRON_CALIBRATION	5
#define VICTRON_BYPASS_TEST	101

#define LENGTH_TEMP 256

int  sdwdelay = 0;  /* shutdown after 0 second */

char *model_name;

static int start_is_datastale = 1;

static int exist_ups_serial = 0;
static int exist_ups_temperature = 0;
static int exist_output_current = 0;
static int exist_battery_charge = 0;
static int exist_battery_current = 0;
static int exist_battery_temperature = 0;
static int exist_battery_runtime = 0;

static int test_in_progress = VICTRON_NO_TEST;

static int get_data (const char *out_string, char *in_string)
{
	int ret_code;
	ser_send(upsfd, "%s%c", out_string, ENDCHAR);
	usleep (UPS_DELAY);
	ret_code = ser_get_line(upsfd, in_string, LENGTH_TEMP, ENDCHAR, 
				IGNCHARS, 3, 0);
	if (ret_code < 1) {            
		dstate_datastale();
		return -1;
	}
	return 0;
}

static int instcmd(const char *cmdname, const char *extra)
{
	char temp[ LENGTH_TEMP ]; 

	if(!strcasecmp(cmdname, "calibrate.start"))
	{
		if(get_data("vTi5!",temp))
		{
			upsdebugx(1, "instcmd: ser_send calibrate.start failed");
			return STAT_INSTCMD_UNKNOWN; /* Or Failed when it get defined */
		}
		else
		{
			upsdebugx(1, "instcmd: calibrate.start returned: %s", temp);
			test_in_progress = VICTRON_CALIBRATION;
			return STAT_INSTCMD_HANDLED;
		}
	}		
	else if(!strcasecmp(cmdname, "calibrate.stop"))
	{
		if(get_data("vTi2!",temp))
		{
			upsdebugx(1, "instcmd: ser_send calibrate.stop failed");
			return STAT_INSTCMD_UNKNOWN; /* Or Failed when it get defined */
		}
		else
		{
			upsdebugx(1, "instcmd: calibrate.stop returned: %s", temp);
			return STAT_INSTCMD_HANDLED;
		}
	}		
	else if(!strcasecmp(cmdname, "test.battery.stop"))
	{
		if(get_data("vTi2!",temp))
		{
			upsdebugx(1, "instcmd: ser_send test.battery.stop failed");
			return STAT_INSTCMD_UNKNOWN; /* Or Failed when it get defined */
		}
		else
		{
			upsdebugx(1, "instcmd: test.battery.stop returned: %s", temp);
			return STAT_INSTCMD_HANDLED;
		}
	}
	else if(!strcasecmp(cmdname, "test.battery.start"))
	{
		if(get_data("vTi4!",temp))
		{
			upsdebugx(1, "instcmd: ser_send test.battery.start failed");
			return STAT_INSTCMD_UNKNOWN; /* Or Failed when it get defined */
		}
		else
		{
			upsdebugx(1, "instcmd: test.battery.start returned: %s", temp);
			test_in_progress = VICTRON_BATTERY_TEST;
			return STAT_INSTCMD_HANDLED;
		}
	}
	else if(!strcasecmp(cmdname, "test.panel.stop"))
	{
		if(get_data("vTi2!",temp))
		{
			upsdebugx(1, "instcmd: ser_send test.panel.stop failed");
			return STAT_INSTCMD_UNKNOWN; /* Or Failed when it get defined */
		}
		else
		{
			upsdebugx(1, "instcmd: test.panel.stop returned: %s", temp);
			return STAT_INSTCMD_HANDLED;
		}
	}
	else if(!strcasecmp(cmdname, "test.panel.start"))
	{
		if(get_data("vTi3!",temp))
		{
			upsdebugx(1, "instcmd: ser_send test.panel.start failed");
			return STAT_INSTCMD_UNKNOWN; /* Or Failed when it get defined */
		}
		else
		{
			upsdebugx(1, "instcmd: test.panel.start returned: %s", temp);
			test_in_progress = VICTRON_SYSTEM_TEST;
			return STAT_INSTCMD_HANDLED;
		}
	}
	else if(!strcasecmp(cmdname, "bypass.stop"))
	{
		if(get_data("vTi2!",temp))
		{
			upsdebugx(1, "instcmd: ser_send bypass.stop failed");
			return STAT_INSTCMD_UNKNOWN; /* Or Failed when it get defined */
		}
		else
		{
			upsdebugx(1, "instcmd: bypass.stop returned: %s", temp);
			return STAT_INSTCMD_HANDLED;
		}
	}
	else if(!strcasecmp(cmdname, "bypass.start"))
	{
		if(get_data("vTi101!",temp))
		{
			upsdebugx(1, "instcmd: ser_send bypass.start failed");
			return STAT_INSTCMD_UNKNOWN; /* Or Failed when it get defined */
		}
		else
		{
			upsdebugx(1, "instcmd: bypass.start returned: %s", temp);
			test_in_progress = VICTRON_BYPASS_TEST;
			return STAT_INSTCMD_HANDLED;
		}
	}
	else
	{
		upsdebugx(1, "instcmd: unknown command: %s", cmdname);
		return STAT_INSTCMD_UNKNOWN;
	}
}
	
void upsdrv_initinfo(void)
{
       	
	dstate_setinfo("driver.version.internal", "%s", DRV_VERSION);
	if (model_name)
		dstate_setinfo("ups.model", "%s", model_name);

	upsh.instcmd = instcmd;

	dstate_addcmd("test.battery.start");
	dstate_addcmd("test.battery.stop");
	dstate_addcmd("calibrate.start");
	dstate_addcmd("calibrate.stop");
	dstate_addcmd("test.panel.start");	/* We need a GeneralSystemTest, but use this one instead */
	dstate_addcmd("test.panel.stop");	/* We need a GeneralSystemTest, but use this one instead */
	dstate_addcmd("bypass.start");
	dstate_addcmd("bypass.stop");
}


void upsdrv_updateinfo(void)
{
	int flags; 
	char temp[ LENGTH_TEMP ]; 
	char test_result[ 255 ];
	int runtime_sec = -1;

        if (start_is_datastale)
		{
		if (get_data("vDS?",temp)) return;
		if (strcmp(temp+3,"NA"))
			exist_ups_serial=1;

		if (get_data("vBT?",temp)) return;
		if (strcmp(temp+3,"NA"))
			exist_ups_temperature =1;
	
		if (get_data("vO0I?",temp)) return;
		if (strcmp(temp+4,"NA"))
			exist_output_current =1;

		if (get_data("vBC?",temp)) return;
		if (strcmp(temp+3,"NA"))
			exist_battery_charge = 1;
	
		if (get_data("vBI?",temp)) return;
		if (strcmp(temp+3,"NA"))
			exist_battery_charge = 1;

		if (get_data("vBT?",temp)) return;
		if (strcmp(temp+3,"NA"))
			exist_battery_temperature = 1;

		if (get_data("vBt?",temp)) return;
		if (strcmp(temp+3,"NA"))
			exist_battery_runtime = 1;

		start_is_datastale = 0;
		}



	
	/* ups.status */ 
	if (get_data("vAa?",temp)) return;
	flags = atoi (temp+3);

	status_init();

	if (flags & VICTRON_OVER)
		status_set("OVER");

	if (flags & VICTRON_RB)
		status_set("RB");

	if (flags & VICTRON_LB)
		status_set("LB");

	if (flags & VICTRON_OB)
		status_set("OB");
	else
		status_set("OL");
	  
	/* Get UPS test results */
	if (get_data("vTr?",temp)) return;
	if (get_data("vTd?",test_result)) return;
	
	switch(atoi(temp+3))
	{
		case 1:
		upsdebugx(1, "upsdrv_updateinfo: test %i result = Done, Passed: %s",test_in_progress,test_result+3);
		test_in_progress = VICTRON_NO_TEST;
		break;

		case 2:
		upsdebugx(1, "upsdrv_updateinfo: test %i result = Done, Warning: %s",test_in_progress,test_result+3);
		test_in_progress = VICTRON_NO_TEST;
		break;

		case 3:
		upsdebugx(1, "upsdrv_updateinfo: test %i result = Done, Error: %s",test_in_progress,test_result+3);
		test_in_progress = VICTRON_NO_TEST;
		break;

		case 4:
		upsdebugx(1, "upsdrv_updateinfo: test %i result = Aborted: %s",test_in_progress,test_result+3);
		test_in_progress = VICTRON_NO_TEST;
		break;

		case 5:
		if(test_in_progress==VICTRON_CALIBRATION)
	                status_set("CAL");              /* calibration in progress*/
		upsdebugx(1, "upsdrv_updateinfo: test %i result = In Progress: %s",
			test_in_progress,test_result+3);
		break;

		case 6:
		upsdebugx(1, "upsdrv_updateinfo: test result = No test initiated: %s",
			test_result+3);
		break;
		
		default:
		upsdebugx(1, "upsdrv_updateinfo: unknown test result: %s / %s",temp+3,test_result+3);
		break;
	}

	status_commit();
	/* dstate_dataok(); */

	upsdebugx(1, "upsdrv_updateinfo: ups.status = %s\n", dstate_getinfo("ups.status"));
  

	/************** ups.x ***************************/
	 
	/* ups model */ 
	if (!model_name)
	{
		if (get_data("vDM?",temp)) return;
		dstate_setinfo("ups.model", "%s", temp+3);
		upsdebugx(1, "ups.model >%s<>%s<\n",temp,temp+3);   
	}
 


	/* ups.mfr */
	if (get_data("vDm?",temp)) return;
	dstate_setinfo("ups.mfr", "%s", temp+3);
	upsdebugx(1, "ups.mfr >%s<>%s<\n",temp,temp+3);   

 
	/* ups.serial */
	if (exist_ups_serial)
		{
		if (get_data("vDS?",temp)) return;
		dstate_setinfo("ups.serial", "%s", temp+3);
		}
	upsdebugx(1, "ups.serial >%s<>%s<\n",temp,temp+3);   

	/* ups.firmware */ 
	if (get_data("vDV?",temp)) return;
	dstate_setinfo("ups.firmware", "%s", temp+3);
	upsdebugx(1, "ups.firmware >%s<>%s<\n",temp,temp+3);   

	/* ups.temperature */
	if (exist_ups_temperature)
		{
		if (get_data("vBT?",temp)) return;
		dstate_setinfo("ups.temperature", "%s", temp+3);
		}
	upsdebugx(1, "ups.temperature >%s<>%s<\n",temp,temp+3);   

	/* ups.load */
	if (get_data("vO0L?",temp)) return;
	dstate_setinfo("ups.load", "%s", temp+4);
	upsdebugx(1, "ups.load >%s<>%s<\n",temp,temp+4);   



	/* ups protocol */ 
	/*if (get_data("vDC?",temp)) return;
	dstate_setinfo("ups.protocol", "%s", temp+3;
	upsdebugx(1, "ups.protocol >%s<>%s<\n",temp,temp+3;   
	*/


	/************** input.x *****************/
   
	/* input.voltage */
	if (get_data("vI0U?",temp)) return;
	dstate_setinfo("input.voltage", "%s", temp+4);
	upsdebugx(1, "input.voltage >%s<>%s<\n",temp,temp+4);   

                                                                               
	/* input.transfer.low */
	if (get_data("vFi?",temp)) return;
	dstate_setinfo("input.transfer.low", "%s", temp+3);
	upsdebugx(1, "input.transfer.low >%s<>%s<\n",temp,temp+3);   

   
	/* input.transfer.high */
	if (get_data("vFj?",temp)) return;
	dstate_setinfo("input.transfer.high", "%s", temp+3);
	upsdebugx(1, "input.transfer.high >%s<>%s<\n",temp,temp+3);


	/* input.frequency */
	if (get_data("vI0f?",temp)) return;
	dstate_setinfo("input.frequency", "%2.1f", atof(temp+4) / 10.0);
	upsdebugx(1, "input.frequency >%s<>%s<\n",temp,temp+4);   


	/*************** output.x ********************************/ 
   
   
	/* output.voltage */
	if (get_data("vO0U?",temp)) return;
	dstate_setinfo("output.voltage", "%s", temp+4);
	upsdebugx(1, "output.voltage >%s<>%s<\n",temp,temp+4);   

   
	/* output.frequency */
	if (get_data("vOf?",temp)) return;
	dstate_setinfo("output.frequency", "%2.1f", atof(temp+3) / 10.0);
	upsdebugx(1, "output.frequency >%s<>%s<\n",temp,temp+3);   


	/* output.current */
	if (exist_output_current)
		{
		if (get_data("vO0I?",temp)) return;
		dstate_setinfo("output.current", "%2.1f", atof(temp+4) / 10.0);
		}
	upsdebugx(1, "output.current >%s<>%s<\n",temp,temp+4);   
   
   
	/*************** battery.x *******************************/
   
	/* battery charge */
	if (exist_battery_charge)
		{
		if (get_data("vBC?",temp)) return;
		dstate_setinfo("battery.charge", "%s", temp+3);
		}
	upsdebugx(1, "battery.charge >%s<>%s<\n",temp,temp+3);   
                                                                    
	/* battery.voltage */
	if (get_data("vBU?",temp)) return;
	dstate_setinfo("battery.voltage", "%2.1f", atof(temp+3) / 10.0);
	upsdebugx(1, "battery.voltage >%s<>%s<\n",temp,temp+3);   
        

	/* battery.current */
	if (exist_battery_current)
		{
		if (get_data("vBI?",temp)) return;
		dstate_setinfo("battery.current", "%2.1f", atof(temp+3) / 10.0);
		}
	upsdebugx(1, "battery.current >%s<>%s<\n",temp,temp+3);   

	/* battery.temperature */
	if (exist_battery_temperature)
		{
		if (get_data("vBT?",temp)) return;
		dstate_setinfo("battery.temperature", "%s", temp+3);
		}
	upsdebugx(1, "battery.temperature >%s<>%s<\n",temp,temp+3);   


	/* battery.runtime */
	if (exist_battery_runtime)
		{
		if (get_data("vBt?",temp)) return;
		runtime_sec = strtol(temp+3, NULL, 10)*60;
		snprintf(temp, sizeof(temp), "%d", runtime_sec);
		dstate_setinfo("battery.runtime", "%s", temp);
		}
	upsdebugx(1, "battery.runtime >%s<>%d<\n",temp,runtime_sec);   

	dstate_dataok();
}

void upsdrv_shutdown(void)
{
	ser_send(upsfd, "vCc0!%c", ENDCHAR);
	usleep(UPS_DELAY);

	ser_send(upsfd, "vCb%i!%c", sdwdelay, ENDCHAR); 
}

void upsdrv_help(void)
{
}

/* list flags and values that you want to receive via -x */
void upsdrv_makevartable(void)
{
	addvar(VAR_VALUE, "usd", "Seting delay before shutdown");
	addvar(VAR_VALUE, "modelname", "Seting model name"); 
}

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

void upsdrv_initups(void)
{
	char temp[ LENGTH_TEMP ], *usd = NULL;  /* = NULL je dulezite jen pro prekladac */
	
   
	upsfd = ser_open(device_path);
	ser_set_speed(upsfd, device_path, B1200);


	if ((usd = getval("usd"))) sdwdelay=atoi(usd);
      
	model_name = getval("modelname"); /* kdyz modelname nebylo zadano je vraceno NULL*/ 

	upsdebugx(1, "(-x) Delay before shutdown %i",sdwdelay);
	upsdebugx(1, "(-x) UPS Name %s",model_name);
    
	/* inicializace a synchronizace UPS */

	ser_send_char(upsfd, ENDCHAR);
	usleep (UPS_LONG_DELAY);
	ser_send(upsfd, "?%c", ENDCHAR);
	usleep (UPS_LONG_DELAY);
	ser_get_line(upsfd, temp, sizeof(temp), ENDCHAR, IGNCHARS, 3, 0);
	ser_send(upsfd, "?%c", ENDCHAR);
	usleep (UPS_LONG_DELAY);
	ser_get_line(upsfd, temp, sizeof(temp), ENDCHAR, IGNCHARS, 3, 0);
	ser_send(upsfd, "?%c", ENDCHAR);
	usleep (UPS_DELAY);
	ser_get_line(upsfd, temp, sizeof(temp), ENDCHAR, IGNCHARS, 3, 0);


	/* the upsh handlers can't be done here, as they get initialized
	 * shortly after upsdrv_initups returns to main.
	 */
}

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

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