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
|
/*
* ivtscd.c - model specific routines for the IVT Solar Controller driver
*
* Copyright (C) 2009 Arjen de Korte <adkorte-guest@alioth.debian.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"
#define DRIVER_NAME "IVT Solar Controller driver"
#define DRIVER_VERSION "0.02"
/* driver description structure */
upsdrv_info_t upsdrv_info = {
DRIVER_NAME,
DRIVER_VERSION,
"Arjen de Korte <adkorte-guest@alioth.debian.org>",
DRV_EXPERIMENTAL,
{ NULL }
};
static struct {
struct {
float act;
float low;
float min;
float nom;
float max;
} voltage;
struct {
float min;
float act;
float max;
} current;
float temperature;
} battery;
static int ivt_status(void)
{
char reply[SMALLBUF];
int ret, i, j = 0;
ser_flush_io(upsfd);
/*
* send: F\n
*/
ret = ser_send(upsfd, "F");
if (ret < 0) {
upsdebug_with_errno(3, "send");
return -1;
}
if (ret == 0) {
upsdebug_with_errno(3, "send: timeout");
return -1;
}
upsdebugx(3, "send: F");
sleep(1); /* allow controller some time to digest this */
/*
* read: R:12,57;- 1,1;20;12,57;13,18;- 2,1; 1,5;\n
*/
ret = ser_get_buf(upsfd, reply, sizeof(reply), 1, 0);
if (ret < 0) {
upsdebug_with_errno(3, "read");
return -1;
}
if (ret == 0) {
upsdebugx(3, "read: timeout");
return -1;
}
upsdebugx(3, "read: %.*s", (int)strcspn(reply, "\r\n"), reply);
upsdebug_hex(4, " \\_", reply, ret);
for (i = 0; i < ret; i++) {
switch(reply[i])
{
case ',': /* convert ',' to '.' */
reply[j++] = '.';
break;
case ' ': /* skip over white space */
case '\0': /* skip over null characters */
break;
default: /* leave the rest as is */
reply[j++] = reply[i];
break;
}
}
reply[j++] = '\0';
ret = sscanf(reply, "R:%f;%f;%f;%f;%f;%f;%f;", &battery.voltage.act, &battery.current.act, &battery.temperature,
&battery.voltage.min, &battery.voltage.max, &battery.current.min, &battery.current.max);
upsdebugx(3, "Parsed %d parameters from reply", ret);
return ret;
}
static int instcmd(const char *cmdname, const char *extra)
{
if (!strcasecmp(cmdname, "reset.input.minmax")) {
ser_send(upsfd, "L");
return STAT_INSTCMD_HANDLED;
}
upslogx(LOG_NOTICE, "instcmd: unknown command [%s]", cmdname);
return STAT_INSTCMD_UNKNOWN;
}
void upsdrv_initinfo(void)
{
if (ivt_status() < 7) {
fatal_with_errno(EXIT_FAILURE, "IVT Solar Controller not detected");
}
/* set the device general information */
dstate_setinfo("device.mfr", "IVT");
dstate_setinfo("device.model", "Solar Controller Device");
dstate_setinfo("device.type", "scd");
dstate_addcmd("reset.input.minmax");
upsh.instcmd = instcmd;
}
void upsdrv_updateinfo(void)
{
if (ivt_status() < 7) {
dstate_datastale();
return;
}
dstate_setinfo("battery.voltage", "%.2f", battery.voltage.act);
dstate_setinfo("battery.voltage.minimum", "%.2f", battery.voltage.min);
dstate_setinfo("battery.voltage.maximum", "%.2f", battery.voltage.max);
dstate_setinfo("battery.current", "%.1f", battery.current.act);
dstate_setinfo("battery.current.minimum", "%.1f", battery.current.min);
dstate_setinfo("battery.current.maximum", "%.1f", battery.current.max);
dstate_setinfo("battery.temperature", "%.0f", battery.temperature);
status_init();
if (battery.current.act > 0) {
status_set("OL"); /* charging */
} else {
status_set("OB"); /* discharging */
}
if (battery.voltage.act < battery.voltage.low) {
status_set("LB");
}
status_commit();
dstate_dataok();
}
void upsdrv_shutdown(void)
{
while (1) {
if (ivt_status() < 7) {
continue;
}
if (battery.voltage.act < battery.voltage.nom) {
continue;
}
fatalx(EXIT_SUCCESS, "Power is back!");
}
}
void upsdrv_help(void)
{
}
void upsdrv_makevartable(void)
{
}
void upsdrv_initups(void)
{
struct termios tio;
const char *val;
upsfd = ser_open(device_path);
ser_set_speed(upsfd, device_path, B1200);
if (tcgetattr(upsfd, &tio)) {
fatal_with_errno(EXIT_FAILURE, "tcgetattr");
}
/*
* Use canonical mode input processing (to read reply line)
*/
tio.c_lflag |= ICANON; /* Canonical input (erase and kill processing) */
tio.c_iflag |= IGNCR; /* Ignore CR */
tio.c_iflag |= IGNBRK; /* Ignore break condition */
tio.c_oflag |= ONLCR; /* Map NL to CR-NL on output */
tio.c_cc[VEOF] = _POSIX_VDISABLE;
tio.c_cc[VEOL] = _POSIX_VDISABLE;
tio.c_cc[VERASE] = _POSIX_VDISABLE;
tio.c_cc[VINTR] = _POSIX_VDISABLE;
tio.c_cc[VKILL] = _POSIX_VDISABLE;
tio.c_cc[VQUIT] = _POSIX_VDISABLE;
tio.c_cc[VSUSP] = _POSIX_VDISABLE;
tio.c_cc[VSTART] = _POSIX_VDISABLE;
tio.c_cc[VSTOP] = _POSIX_VDISABLE;
if (tcsetattr(upsfd, TCSANOW, &tio)) {
fatal_with_errno(EXIT_FAILURE, "tcsetattr");
}
/*
* Set DTR and clear RTS to provide power for the serial interface.
*/
ser_set_dtr(upsfd, 1);
ser_set_rts(upsfd, 0);
val = dstate_getinfo("battery.voltage.nominal");
battery.voltage.nom = (val) ? strtod(val, NULL) : 12.00;
val = dstate_getinfo("battery.voltage.low");
battery.voltage.low = (val) ? strtod(val, NULL) : 10.80;
if (battery.voltage.nom <= battery.voltage.low) {
fatalx(EXIT_FAILURE, "Nominal battery voltage must be higher than low battery voltage!");
}
}
void upsdrv_cleanup(void)
{
ser_set_dtr(upsfd, 0);
ser_close(upsfd, device_path);
}
|