Filter:   InfoImg
download gamatronic.c
Language: C
License: GPL
LOC: 239
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
/* gamatronic.c
 *
 * SEC UPS Driver ported to the new NUT API for Gamatronic UPS Usage.
 *
 * 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 <http://arnaud.quette.free.fr/contact.html>
 *   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"


#define ENDCHAR	'\r'	
#define IGNCHARS ""
#define SER_WAIT_SEC    1	/* allow 3.0 sec for ser_get calls */
#define SER_WAIT_USEC	0
#define DRV_VERSION	"0.01"

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);
		ret = atoi(lenbuf);
		if (ret > 0){
		strcpy(buf,buf+5);
		return(ret);}
		else return (-2);
		default:
		return(-2);
		}
	}	
	else 
         { return (-2); }
}

int sec_cmd(const char mode, const char *command, char *msgbuf, int *buflen)
{
    char msg[140];
    int ret;

    memset(msg, 0, sizeof(msg));

    /* create the message string */
    if (*buflen > 0) {
	sprintf(msg, "%c%c%03d%s%s", SEC_MSG_STARTCHAR,
		mode, (*buflen)+3, command, msgbuf);
    }
    else {
	sprintf(msg, "%c%c003%s", SEC_MSG_STARTCHAR,
		mode, command);
    }	
    upsdebugx(1, "PC-->UPS: \"%s\"",msg);
    ret = ser_send(upsfd,msg);
    
    upsdebugx(1, " send returned: %d",ret);

    if (ret == -1) return -1;

    ret = sec_upsrecv(msg);


    if (ret < 0) return -1;

    if (ret >= 0) {
	strncpy(msgbuf, msg, ret);
	upsdebugx(1, "UPS<--PC: \"%s\"",msg);
    }
/*    *(msgbuf+ret) = '\0';*/

    *buflen = ret;
    return ret;
}

void addquery(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;
	}
    }
}

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);}
			
		}
		
}
	
	
	
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();


}

void sec_poll ( int pollflag ) {
	
	int msglen,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  ) {

		    snprintf(sec_varlist[sqv(q,f)].value, 
			sizeof(sec_varlist[sqv(q,f)].value), "%s", r);
                  
		    sec_setinfo(sqv(q,f), r);
		}
		
	/* If SEC VAR is alarm and its 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)
{

    int msglen, e, v;
    char *a,*p,avail_list[300];
 
    dstate_setinfo("driver.version.internal", "%s", DRV_VERSION);
 
    /* 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("No available variables found!");}
    a = avail_list;
    e = 0;
   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);
	update_pseudovars();
	alarm_commit();
	dstate_dataok();
	
}

void upsdrv_shutdown(void)
{

  int msg_len;

   msg_len = 2;
   sec_cmd (SEC_SETCMD,SEC_SHUTDOWN,"-1",&msg_len);
   msg_len = 1 ;
   sec_cmd (SEC_SETCMD,SEC_AUTORESTART,"1",&msg_len);    
   msg_len = 1;
   sec_cmd (SEC_SETCMD, SEC_SHUTTYPE,"2",&msg_len);
   msg_len = 1;
   sec_cmd (SEC_SETCMD,SEC_SHUTDOWN,"5",&msg_len);
}

/*
static 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"); */
}

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

void setup_serial(const char *port)
{
    char temp[140];
    int i,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 Protorol\n");
	ser_close(upsfd, device_path);
	exit (1);
    }
printf("Connected to UPS on %s baudrate: %d\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); 
}