~iheino+ub/+junk/nut-upsconf-docfix

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
/* isbmex.c - model specific routines for SOLA/BASIC Mexico (ISBMEX) models

   Copyright (C) 2005 Ricardo Martinezgarza <ricardo@nexxis.com.mx>
   Copyright (C) 2002 Edscott Wilson Garcia <edscott@imp.mx>
   Copyright (C) 1999 Russell Kroll <rkroll@exploits.org>

   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 <math.h>		/* for sqrt */
#include <string.h>

#define DRIVER_NAME	"ISBMEX UPS driver"
#define DRIVER_VERSION	"0.06"

/* driver description structure */
upsdrv_info_t	upsdrv_info = {
	DRIVER_NAME,
	DRIVER_VERSION,
	"Ricardo Martinezgarza <ricardo@nexxis.com.mx>\n" \
	"Edscott Wilson Garcia <edscott@imp.mx>\n" \
	"Russell Kroll <rkroll@exploits.org>",
	DRV_STABLE,
	{ NULL }
};

#define xDEBUG

#ifdef DEBUG
#define D(x) x
#else
#define D(x)
#endif

/*#define ENDCHAR	'&'*/
#define MAXTRIES 15
/* #define IGNCHARS	""	*/

float lagrange(unsigned int vbyte)
{
	float f0, f1, f2, f3, f4, f5, f6;
	float a, b, c, d, e, g, h;
	const float x0=144.0, x1=154.0, x2=184.0, x3=195.0, x4=215.0, x5=228.0, x6=249.0;
	const float fX0=95.1, fX1=98.3, fX2=112.6, fX3=116.5, fX4=126.0, fX5=131.0, fX6=140.3;

	if(vbyte < 144) return 0.0;

	f0 = vbyte - x0;
	f1 = vbyte - x1;
	f2 = vbyte - x2;
	f3 = vbyte - x3;
	f4 = vbyte - x4;
	f5 = vbyte - x5;
	f6 = vbyte - x6;

	b = (f1 * f2 * f3 * f4 * f5 * f6 * fX0)/((x0 - x1) * (x0 - x2));
	b = b / ((x0 - x3) * (x0 - x4));
	b = b / ((x0 - x5) * (x0 - x6));

	c = (f0 * f2 * f3 * f4 * f5 * f6 * fX1)/((x1 - x0) * (x1 - x2));
	c = c / ((x1 - x3) * (x1 - x4));
	c = c / ((x1 - x5) * (x1 - x6));

	d = (f0 * f1 * f3 * f4 * f5 * f6 * fX2)/((x2 - x0) * (x2 - x1));
	d = d / ((x2 - x3) * (x2 - x4));
	d = d / ((x2 - x5) * (x2 - x6));

	e = (f0 * f1 * f2 * f4 * f5 * f6 * fX3)/((x3 - x0) * (x3 - x1));
	e = e / ((x3 - x2) * (x3 - x4));
	e = e / ((x3 - x5) * (x3 - x6));

	a = (f0 * f1 * f2 * f3 * f5 * f6 * fX4)/((x4 - x0) * (x4 - x1));
	a = a / ((x4 - x2) * (x4 - x3));
	a = a / ((x4 - x5) * (x4 - x6));

	g = (f0 * f1 * f2 * f3 * f4 * f6 * fX5)/((x5 - x0) * (x5 - x1));
	g = g / ((x5 - x2) * (x5 - x3));
	g = g / ((x5 - x4) * (x5 - x6));

	h = (f0 * f1 * f2 * f3 * f4 * f5 * fX6)/((x6 - x0) * (x6 - x1));
	h = h / ((x6 - x2) * (x6 - x3));
	h = h / ((x6 - x4) * (x6 - x5));

	return a + b + c + d + e + g + h;
}

float interpol(float vbytes)
{
	const int x[7]={75,83,87,98,103,118,145};
	const float f[7]={96.0,102.0,105.0,113.0,116.0,124.0,140.0};
	float l[7];
	float t, volts;
	const int n=6;
	int i, j;

	if(vbytes < x[0]) return 0.0;
	if(vbytes > x[6]) return f[6];
	for(i=0; i<=n; i++)
	{
		if((int)vbytes == x[i]) return f[i];
	}
	for(i=0; i<=n; i++)
	{
		l[i] = 1.0;
		for(j=0; j<=n; j++)
		{
			if(j!=i) l[i] *= (float)(x[i] - x[j]);
		}
		l[i] = f[i] / l[i];
	}
	t = 1.0;
	for(i=0; i<=n; i++) t *= (vbytes - (float)x[i]);
	volts = 0.0;
	for(i=0; i<=n; i++) volts += (l[i] * t / (vbytes - (float)x[i]));
	return volts;
}

void upsdrv_initinfo(void)
{
	dstate_setinfo("ups.mfr", "Sola/Basic Mexico");
	dstate_setinfo("ups.model", "SR-Inet 280/300/400/480/500/800/1000");

	/* high/low voltage */
	dstate_setinfo("input.transfer.low", "102.0");	/* defined */
	dstate_setinfo("input.transfer.high", "140.0");	/* defined */

	dstate_setinfo("output.voltage", "120.0");	/* defined */
	 
 	 /* addinfo(INFO_, "", 0, 0); */
	 /*printf("Using %s %s on %s\n", getdata(INFO_MFR), getdata(INFO_MODEL), device_path);*/
}

static const char *getpacket(int *we_know){
  fd_set readfds;
  struct timeval tv;
  int bytes_per_packet=0;
  int ret;
  static const char *packet_id=NULL;
  static char buf[256];
  const char *s;
  ssize_t r;

  
  bytes_per_packet=*we_know;
  D(printf("getpacket with %d\n",bytes_per_packet);)
  
  FD_ZERO(&readfds);
  FD_SET(upsfd,&readfds);
	/* Wait up to 2 seconds. */
  tv.tv_sec = 5;
  tv.tv_usec = 0;
 
  ret=select(upsfd+1,  &readfds, NULL, NULL, &tv);
  if (!ret) {
	s="Nothing received from UPS. Check cable conexion";
	upslogx(LOG_ERR, "%s", s);
	D(printf("%s\n",s);)
	return NULL;
  }

  r=read(upsfd,buf,255);
  D(printf("%d bytes read: ",r);)
  buf[r]=0;
  if (bytes_per_packet && r < bytes_per_packet){
	     ssize_t rr;
	     D(printf("short read...\n");)
	     usleep(500000);
             tv.tv_sec = 2;
             tv.tv_usec = 0;
             ret=select(upsfd+1,  &readfds, NULL, NULL, &tv);
             if (!ret) return NULL;
	     rr=read(upsfd,buf+r,255-r);
	     r += rr;
	     if (r < bytes_per_packet) return NULL;
  }
	     
  if (!bytes_per_packet){ /* packet size determination */ 
       /* if (r%10 && r%9) {
	   printf("disregarding incomplete packet\n");
  	   return NULL;
	}*/
	if (r%10==0) *we_know=10;
	else if (r%9==0)  *we_know=9;
	return NULL;
  }
     
     /* by here we have bytes_per_packet and a complete packet */
     /* lets check if within the complete packet we have a valid packet */
 if (bytes_per_packet == 10) packet_id="&&&";  else  packet_id="***";
 s=strstr(buf,packet_id);
     /* check validity of packet */
 if (!s) {
	s="isbmex: no valid packet signature!";
	upslogx(LOG_ERR, "%s", s);
	D(printf("%s\n",s);)
	*we_know=0;
	return NULL;
 }
 D(if (s != buf) printf("overlapping packet received\n");)
 if ((int) strlen(s) < bytes_per_packet) {
		    D(printf("incomplete packet information\n");)
		    return NULL; 
 }
#ifdef DEBUG
    printf("Got signal:");
    {int i;for (i=0;i<strlen(s);i++) printf(" <%d>",(unsigned char)s[i]);}
    printf("\n");
#endif
    
 return s;
}

void upsdrv_updateinfo(void)
{
  static  float high_volt=-1, low_volt=999;
  const char	*buf=NULL;
  char buf2[17];
  int i;
  static int bytes_per_packet=0;

  for (i=0;i<5;i++) {
	  if ((buf=getpacket(&bytes_per_packet)) != NULL) break;
  }
  if (!bytes_per_packet || !buf) {
	dstate_datastale();
	return;
  }
	  
  /* do the parsing */
  {
     float in_volt,battpct,acfreq;
     double d;
     D(printf("parsing (%d bytes per packet)\n",bytes_per_packet);)
     /* input voltage :*/
     if (bytes_per_packet==9) {
	 in_volt = lagrange((unsigned char)buf[3]);    
     } else {
	 in_volt = interpol(sqrt((float)((unsigned char)buf[3]*256+(unsigned char)buf[4])));    
     }
     snprintf(buf2,16,"%5.1f",in_volt);
     D(printf("utility=%s\n",buf2);)
     dstate_setinfo("input.voltage", "%s", buf2);     
     
     if (in_volt >= high_volt) high_volt=in_volt;
     snprintf(buf2,16,"%5.1f",high_volt);
     D(printf("highvolt=%s\n",buf2);)
     dstate_setinfo("input.voltage.maximum", "%s", buf2);       
     
     if (in_volt <= low_volt)  low_volt=in_volt;
     snprintf(buf2,16,"%5.1f",low_volt);
     D(printf("lowvolt=%s\n",buf2);)
     dstate_setinfo("input.voltage.minimum", "%s", buf2);      
 
     battpct = ((double)((unsigned char)buf[(bytes_per_packet==10)?5:4])-168.0)*(100.0/(215.0-168.0));
     snprintf(buf2,16,"%5.1f",battpct);
     D(printf("battpct=%s\n",buf2);)
     dstate_setinfo("battery.charge", "%s", buf2);    
 
     d=(unsigned char)buf[(bytes_per_packet==10)?6:5]*256
	     + (unsigned char)buf[(bytes_per_packet==10)?7:6];    
     acfreq = 1000000/d;
     snprintf(buf2,16,"%5.2f",acfreq);
     D(printf("acfreq=%s\n",buf2);)
     dstate_setinfo("input.frequency", "%s", buf2);    

     D(printf("status: ");)
     status_init();
     switch (buf[(bytes_per_packet==10)?8:7]){
	 case 48: break; /* normal operation */
	 case 49: D(printf("BOOST ");)
		  status_set("BOOST");
		  break;
	 case 50: D(printf("TRIM ");)
		  status_set("TRIM");
	 default: break;
     }
     switch (buf[(bytes_per_packet==10)?9:8]){
	 case 48: D(printf("OL ");)
		  status_set("OL");
		  break; 
	 case 50: D(printf("LB ");)
		  status_set("LB");
	 case 49: D(printf("OB ");)
		  status_set("OB");
		  break;
	 default: break;
     }
     D(printf("\n");)
     status_commit();	     
				  
	     
   } 
   dstate_dataok();
   return;
}

void upsdrv_shutdown(void)
{
	/* shutdown is supported on models with
	 * contact closure. Some ISB models with serial
	 * support support contact closure, some don't.
	 * If yours does support it, then a 12V signal
	 * on pin 9 does the trick (only when ups is 
	 * on OB condition) */
	/* 
	 * here try to do the pin 9 trick, if it does not
	 * work, else:*/
/*	fatalx(EXIT_FAILURE, "Shutdown only supported with the Generic Driver, type 6 and special cable");  */
	/*fatalx(EXIT_FAILURE, "shutdown not supported");*/
	int i;
	for(i=0;i<=5;i++)
	{
		ser_send_char(upsfd, '#');
		usleep(50000);
	}
}


void upsdrv_help(void)
{
}

/* list flags and values that you want to receive via -x */
void upsdrv_makevartable(void)
{
}

void upsdrv_initups(void)
{
	upsfd = ser_open(device_path);
	ser_set_speed(upsfd, device_path, B9600);
}

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