~ubuntu-branches/ubuntu/saucy/nut/saucy

« back to all changes in this revision

Viewing changes to drivers/victronups.c

  • Committer: Bazaar Package Importer
  • Author(s): Arnaud Quette
  • Date: 2004-05-28 13:10:01 UTC
  • mto: (16.1.1 squeeze)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20040528131001-yj2m9qcez4ya2w14
Tags: upstream-1.4.2
ImportĀ upstreamĀ versionĀ 1.4.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *
 
3
 *  newvictron.c - Model specific routines for IMV/Victron  units
 
4
 *  Match, Match Lite, NetUps
 
5
 *  Revision 0.1.3.2
 
6
 *           
 
7
 * Copyright (C) 1999  Russell Kroll <rkroll@exploits.org>
 
8
 * Copyright (C) 2000  Radek Benedikt <benedikt@lphard.cz>
 
9
 * old style "victronups"
 
10
 * Copyright (C) 2001  Daniel.Prynych <Daniel.Prynych@hornet.cz>
 
11
 * porting to now style "newvictron"
 
12
 * Copyright (C) 2003  Gert Lynge <gert@lynge.org>
 
13
 * Removes \n from data (was causing periodic misreadings of temperature
 
14
 * and voltage levels)
 
15
 * This program is free software; you can redistribute it and/or modify
 
16
 * it under the terms of the GNU General Public License as published by
 
17
 * the Free Software Foundation; either version 2 of the License, or
 
18
 * (at your option) any later version.
 
19
 *                                       
 
20
 * This program is distributed in the hope that it will be useful,
 
21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
23
 * GNU General Public License for more details.
 
24
 *                                       
 
25
 * You should have received a copy of the GNU General Public License
 
26
 * along with this program; if not, write to the Free Software
 
27
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
28
 *
 
29
 *
 
30
 */
 
31
 
 
32
#define DRV_VERSION "0.1.3.2"
 
33
 
 
34
#include "main.h"
 
35
 
 
36
#define ENDCHAR '\r'
 
37
#define IGNCHARS        "\n"
 
38
 
 
39
#define UPS_DELAY 150000
 
40
#define UPS_LONG_DELAY 450000
 
41
 
 
42
#define VICTRON_OVER 128
 
43
#define VICTRON_RB 1
 
44
#define VICTRON_OB 2
 
45
#define VICTRON_LB 4
 
46
 
 
47
#define LENGTH_TEMP 256
 
48
 
 
49
int  sdwdelay = 0;  /* shutdown after 0 second */
 
50
 
 
51
extern  int     sddelay; /* doba po kterou ceka driver po vypnuti ups */
 
52
extern  char    upssend_endchar;
 
53
extern  int     upsc_debug;
 
54
char *model_name;
 
55
 
 
56
 
 
57
void upsdrv_initinfo(void)
 
58
{
 
59
        dstate_setinfo("driver.version.internal", "%s", DRV_VERSION);
 
60
        if (model_name)
 
61
                dstate_setinfo("ups.model", "%s", model_name);
 
62
 
 
63
}
 
64
 
 
65
int get_data (const char *out_string, char *in_string)
 
66
{
 
67
        int ret_code;   
 
68
        upssend (out_string);
 
69
        usleep (UPS_DELAY);
 
70
        ret_code = upsrecv (in_string, LENGTH_TEMP, ENDCHAR, IGNCHARS);
 
71
 
 
72
        if (ret_code < 1) {            
 
73
                dstate_datastale();
 
74
                return -1;
 
75
        }
 
76
        return 0;
 
77
}
 
78
 
 
79
void upsdrv_updateinfo(void)
 
80
{
 
81
        int flags; 
 
82
        char temp[ LENGTH_TEMP ]; 
 
83
 
 
84
        /* ups.status */ 
 
85
        if (get_data("vAa?",temp)) return;
 
86
        flags = atoi (temp+3);
 
87
 
 
88
        status_init();
 
89
 
 
90
        if (flags & VICTRON_OVER)
 
91
                status_set("OVER");
 
92
 
 
93
        if (flags & VICTRON_RB)
 
94
                status_set("RB");
 
95
 
 
96
        if (flags & VICTRON_LB)
 
97
                status_set("LB");
 
98
 
 
99
        if (flags & VICTRON_OB)
 
100
                status_set("OB");
 
101
        else
 
102
                status_set("OL");
 
103
 
 
104
        status_commit();
 
105
        /* dstate_dataok(); */
 
106
 
 
107
        upsdebugx(1, "ups.status = %s\n", dstate_getinfo("ups.status"));
 
108
  
 
109
 
 
110
        /************** ups.x ***************************/
 
111
         
 
112
        /* ups model */ 
 
113
        if (!model_name)
 
114
        {
 
115
                if (get_data("vDM?",temp)) return;
 
116
                dstate_setinfo("ups.model", "%s", temp+3);
 
117
                upsdebugx(1, "ups.model >%s<>%s<\n",temp,temp+3);   
 
118
        }
 
119
 
 
120
 
 
121
 
 
122
        /* ups.mfr */
 
123
        if (get_data("vDm?",temp)) return;
 
124
        dstate_setinfo("ups.mfr", "%s", temp+3);
 
125
        upsdebugx(1, "ups.mfr >%s<>%s<\n",temp,temp+3);   
 
126
 
 
127
 
 
128
        /* ups.serial */
 
129
        if (get_data("vDS?",temp)) return;
 
130
        if (strcmp(temp+3,"NA"))
 
131
                dstate_setinfo("ups.serial", "%s", temp+3);
 
132
        upsdebugx(1, "ups.serial >%s<>%s<\n",temp,temp+3);   
 
133
 
 
134
        /* ups.firmware */ 
 
135
        if (get_data("vDV?",temp)) return;
 
136
        dstate_setinfo("ups.firmware", "%s", temp+3);
 
137
        upsdebugx(1, "ups.firmware >%s<>%s<\n",temp,temp+3);   
 
138
 
 
139
        /* ups.temperature */
 
140
        if (get_data("vBT?",temp)) return;
 
141
        if (strcmp(temp+3,"NA"))
 
142
                dstate_setinfo("ups.temperature", "%s", temp+3);
 
143
        upsdebugx(1, "ups.temperature >%s<>%s<\n",temp,temp+3);   
 
144
 
 
145
        /* ups.load */
 
146
        if (get_data("vO0L?",temp)) return;
 
147
        dstate_setinfo("ups.load", "%s", temp+4);
 
148
        upsdebugx(1, "ups.load >%s<>%s<\n",temp,temp+4);   
 
149
 
 
150
 
 
151
 
 
152
        /* ups protocol */ 
 
153
        /*if (get_data("vDC?",temp)) return;
 
154
        dstate_setinfo("ups.protocol", "%s", temp+3;
 
155
        upsdebugx(1, "ups.protocol >%s<>%s<\n",temp,temp+3;   
 
156
        */
 
157
 
 
158
 
 
159
        /************** input.x *****************/
 
160
   
 
161
        /* input.voltage */
 
162
        if (get_data("vI0U?",temp)) return;
 
163
        dstate_setinfo("input.voltage", "%s", temp+4);
 
164
        upsdebugx(1, "input.voltage >%s<>%s<\n",temp,temp+4);   
 
165
 
 
166
                                                                               
 
167
        /* input.transfer.low */
 
168
        if (get_data("vFi?",temp)) return;
 
169
        dstate_setinfo("input.transfer.low", "%s", temp+3);
 
170
        upsdebugx(1, "input.transfer.low >%s<>%s<\n",temp,temp+3);   
 
171
 
 
172
   
 
173
        /* input.transfer.high */
 
174
        if (get_data("vFj?",temp)) return;
 
175
        dstate_setinfo("input.transfer.high", "%s", temp+3);
 
176
        upsdebugx(1, "input.transfer.high >%s<>%s<\n",temp,temp+3);
 
177
 
 
178
 
 
179
        /* input.frequency */
 
180
        if (get_data("vI0f?",temp)) return;
 
181
        dstate_setinfo("input.frequency", "%2.1f", atof(temp+4) / 10.0);
 
182
        upsdebugx(1, "input.frequency >%s<>%s<\n",temp,temp+4);   
 
183
 
 
184
 
 
185
        /*************** output.x ********************************/ 
 
186
   
 
187
   
 
188
        /* output.voltage */
 
189
        if (get_data("vO0U?",temp)) return;
 
190
        dstate_setinfo("output.voltage", "%s", temp+4);
 
191
        upsdebugx(1, "output.voltage >%s<>%s<\n",temp,temp+4);   
 
192
 
 
193
   
 
194
        /* output.frequency */
 
195
        if (get_data("vOf?",temp)) return;
 
196
        dstate_setinfo("output.frequency", "%2.1f", atof(temp+3) / 10.0);
 
197
        upsdebugx(1, "output.frequency >%s<>%s<\n",temp,temp+3);   
 
198
 
 
199
 
 
200
        /* output.current */
 
201
        if (get_data("vO0I?",temp)) return;
 
202
        if (strcmp(temp+4,"NA"))
 
203
                dstate_setinfo("output.current", "%2.1f", atof(temp+4) / 10.0);
 
204
        upsdebugx(1, "output.current >%s<>%s<\n",temp,temp+4);   
 
205
   
 
206
   
 
207
        /*************** battery.x *******************************/
 
208
   
 
209
        /* battery charge */
 
210
        if (get_data("vBC?",temp)) return;
 
211
        if (strcmp(temp+3,"NA"))
 
212
                dstate_setinfo("battery.charge", "%s", temp+3);
 
213
        upsdebugx(1, "battery.charge >%s<>%s<\n",temp,temp+3);   
 
214
                                                                    
 
215
        /* battery.voltage */
 
216
        if (get_data("vBU?",temp)) return;
 
217
        dstate_setinfo("battery.voltage", "%2.1f", atof(temp+3) / 10.0);
 
218
        upsdebugx(1, "battery.voltage >%s<>%s<\n",temp,temp+3);   
 
219
        
 
220
 
 
221
        /* battery.current */
 
222
        if (get_data("vBI?",temp)) return;
 
223
        if (strcmp(temp+3,"NA"))
 
224
                dstate_setinfo("battery.current", "%2.1f", atof(temp+3) / 10.0);
 
225
        upsdebugx(1, "battery.current >%s<>%s<\n",temp,temp+3);   
 
226
 
 
227
        /* battery.temperature */
 
228
        if (get_data("vBT?",temp)) return;
 
229
        if (strcmp(temp+3,"NA"))
 
230
                dstate_setinfo("battery.temperature", "%s", temp+3);
 
231
        upsdebugx(1, "battery.temperature >%s<>%s<\n",temp,temp+3);   
 
232
 
 
233
        /* battery.runtime */
 
234
        if (get_data("vBt?",temp)) return;
 
235
        if (strcmp(temp+3,"NA"))
 
236
                dstate_setinfo("battery.runtime", "%s", temp+3);
 
237
        upsdebugx(1, "battery.runtime >%s<>%s<\n",temp,temp+3);   
 
238
 
 
239
        dstate_dataok();
 
240
}
 
241
 
 
242
void upsdrv_shutdown(void)
 
243
{
 
244
        upssend ("vCc0!");
 
245
        usleep (UPS_DELAY);
 
246
                 
 
247
        upssend ("vCb%i!",sdwdelay);
 
248
}
 
249
 
 
250
 
 
251
void instcmd (int auxcmd, int dlen, char *data)
 
252
{
 
253
        /* TODO: reply to upsd? */
 
254
 
 
255
        switch (auxcmd) {
 
256
                /* case test.battery.stop:      * stop battery test *
 
257
                 *      upssend("vTi2");
 
258
                 *      break;
 
259
                 * case test.battery.start:     * start battery test *
 
260
                 *      upssend("vTi4");
 
261
                 *      break;
 
262
                 * case calibrate.stop:         * stop calibration *
 
263
                 *      upssend("vTi2");
 
264
                 *      break;
 
265
                 * case calibrate.start:        * start calibration *
 
266
                 *      upssend("vTi5");
 
267
                 *      break;
 
268
                 */
 
269
                default:
 
270
                        upslogx(LOG_INFO, "instcmd: unknown type 0x%04x", auxcmd);
 
271
        }
 
272
}
 
273
 
 
274
 
 
275
void upsdrv_help(void)
 
276
{
 
277
}
 
278
 
 
279
/* list flags and values that you want to receive via -x */
 
280
void upsdrv_makevartable(void)
 
281
{
 
282
        addvar(VAR_VALUE, "usd", "Seting delay before shutdown");
 
283
        addvar(VAR_VALUE, "modelname", "Seting model name"); 
 
284
}
 
285
 
 
286
void upsdrv_banner(void)
 
287
{
 
288
        printf("Network UPS Tools - New Victron UPS driver %s (%s)\n\n",
 
289
                DRV_VERSION, UPS_VERSION);
 
290
}
 
291
 
 
292
void upsdrv_initups(void)
 
293
{
 
294
        char temp[ LENGTH_TEMP ], *usd = NULL;  /* = NULL je dulezite jen pro prekladac */
 
295
 
 
296
        upssend_endchar=ENDCHAR;
 
297
        if (nut_debug_level > 1) upsc_debug = nut_debug_level - 1; /* docasne reseni */
 
298
   
 
299
        upssend_delay = 0; /* zpozdeni mezi odeslanim jednotlivych znaku bude 0 sec */ 
 
300
        open_serial(device_path, B1200); 
 
301
 
 
302
    
 
303
        if ((usd = getval("usd"))) sdwdelay=atoi(usd);
 
304
      
 
305
        model_name = getval("modelname"); /* kdyz modelname nebylo zadano je vraceno NULL*/ 
 
306
 
 
307
        upsdebugx(1, "(-x) Delay before shutdown %i",sdwdelay);
 
308
        upsdebugx(1, "(-x) UPS Name %s",model_name);
 
309
    
 
310
        /* inicializace a synchronizace UPS */
 
311
 
 
312
        upssendchar (ENDCHAR);
 
313
        usleep (UPS_LONG_DELAY);
 
314
        upssend ("?");
 
315
        usleep (UPS_LONG_DELAY);
 
316
        upsrecv (temp, sizeof(temp), ENDCHAR, IGNCHARS);
 
317
        upssend ("?");
 
318
        usleep (UPS_LONG_DELAY);
 
319
        upsrecv (temp, sizeof(temp), ENDCHAR, IGNCHARS);
 
320
        upssend ("?");
 
321
        usleep (UPS_DELAY);
 
322
        upsrecv (temp, sizeof(temp), ENDCHAR, IGNCHARS);
 
323
 
 
324
 
 
325
        /* the upsh handlers can't be done here, as they get initialized
 
326
         * shortly after upsdrv_initups returns to main.
 
327
         */
 
328
}
 
329
 
 
330
void upsdrv_cleanup(void)
 
331
{
 
332
}