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

« back to all changes in this revision

Viewing changes to drivers/sms.c

  • Committer: Bazaar Package Importer
  • Author(s): Reinhard Tartler
  • Date: 2005-07-20 19:48:50 UTC
  • mto: (16.1.1 squeeze)
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20050720194850-oo61wjr33rrx2mre
Tags: upstream-2.0.2
ImportĀ upstreamĀ versionĀ 2.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
46
46
 
47
47
*/
48
48
 
49
 
#define DRV_VERSION "0.71"
 
49
#define DRV_VERSION "0.72"
50
50
 
51
51
#include "main.h"
 
52
#include "serial.h"
52
53
#include "sms.h"
53
54
 
54
55
#define ENDCHAR 13      /* replies end with CR */
57
58
static  float   lowvolt = 0, voltrange;
58
59
static  int     lownorm, highnorm, poll_failures = 0;
59
60
 
60
 
void guessmodel(const char *raw)
 
61
static void guessmodel(const char *raw)
61
62
{
62
 
        char    mch, mstr[256];
 
63
        char    mch, *mstr;
63
64
 
64
65
        mch = raw[17];
65
66
 
66
67
        printf ("0         1         2        3         \n");
67
68
        printf ("012345678901234567890123567890123456789\n");
68
69
        printf ("%s\n", raw);
69
 
        strlcpy(mstr, &raw[17], sizeof(mstr));
 
70
        mstr = xstrdup(&raw[18]);
70
71
        mstr[10] = '\0';        /* 10 chars max, per the protocol */
71
72
       
72
73
        /* trim whitespace */
74
75
     
75
76
        dstate_setinfo("ups.model", "SMS %s", mstr);   
76
77
        cap_upstemp = 1;
 
78
 
 
79
        free(mstr);
77
80
}
78
81
 
79
 
void getbaseinfo(void)
 
82
static void getbaseinfo(void)
80
83
{
81
84
        char    temp[256], model[32], *raw;
82
85
        int     modelnum, i;
83
86
 
84
87
        /* dummy read attempt to sync - throw it out */
85
 
        upssend("I\r");
86
 
        upsrecv(temp, sizeof(temp), ENDCHAR, "");
 
88
        ser_send(upsfd, "I\r");
 
89
        ser_get_line(upsfd, temp, sizeof(temp), ENDCHAR, "", 3, 0);
87
90
 
88
91
        /* now retrieve information and parse */
89
 
        upssend("I\r");
90
 
        upsrecv(temp, sizeof(temp), ENDCHAR, "");
 
92
        ser_send(upsfd, "I\r");
 
93
        ser_get_line(upsfd, temp, sizeof(temp), ENDCHAR, "", 3, 0);
91
94
        raw = xstrdup(temp);
92
95
 
93
96
        if (temp[0] != '#')
155
158
        free(raw);
156
159
 
157
160
        /* paranoia - cancel any shutdown that might already be running */
158
 
        upssend("C\r");
 
161
        ser_send(upsfd, "C\r");
159
162
}
160
163
 
161
 
int instcmd(const char *cmdname, const char *extra)
 
164
static int instcmd(const char *cmdname, const char *extra)
162
165
{
163
166
        if (!strcasecmp(cmdname, "test.battery.stop")) {
164
 
                upssend("CT\r");
 
167
                ser_send(upsfd, "CT\r");
165
168
                return STAT_INSTCMD_HANDLED;
166
169
        }
167
170
 
168
171
        if (!strcasecmp(cmdname, "test.battery.start")) {
169
 
                upssend("TL\r");/* start battery test until bat low */
 
172
                ser_send(upsfd, "TL\r");/* start battery test until bat low */
170
173
                return STAT_INSTCMD_HANDLED;
171
174
        }
172
175
 
173
176
        if (!strcasecmp(cmdname, "test.failure.start")) {
174
 
                upssend("T\r");
 
177
                ser_send(upsfd, "T\r");
175
178
                return STAT_INSTCMD_HANDLED;
176
179
        }
177
180
 
178
181
        if (!strcasecmp(cmdname, "shutdown.return")) {
179
182
                /* shutdown and restart */
180
 
                upssend("C\r");
181
 
                upssend("S.3R0003\r");
 
183
                ser_send(upsfd, "C\r");
 
184
                ser_send(upsfd, "S.3R0003\r");
182
185
                return STAT_INSTCMD_HANDLED;
183
186
        }
184
187
 
185
188
        if (!strcasecmp(cmdname, "shutdown.stayoff")) {
186
189
                /* shutdown now (one way) */
187
 
                upssend("C\r");
188
 
                upssend("S.3\r");
 
190
                ser_send(upsfd, "C\r");
 
191
                ser_send(upsfd, "S.3\r");
189
192
                return STAT_INSTCMD_HANDLED;
190
193
        }
191
194
 
192
195
        if (!strcasecmp(cmdname, "shutdown.stop")) {
193
196
                /* Cancel Shutdown  */
194
 
                upssend("C\r");
 
197
                ser_send(upsfd, "C\r");
195
198
                return STAT_INSTCMD_HANDLED;
196
199
        }
197
200
 
198
201
        if (!strcasecmp(cmdname, "reset.watchdog")) {
199
202
                /* WATCHDOG Crontab Function */
200
 
                upssend("C\r");
201
 
                upssend("S05R0003\r");
 
203
                ser_send(upsfd, "C\r");
 
204
                ser_send(upsfd, "S05R0003\r");
202
205
                return STAT_INSTCMD_HANDLED;
203
206
        }
204
207
 
210
213
{
211
214
        getbaseinfo();
212
215
 
213
 
        upsh.new_instcmd = instcmd;
 
216
        upsh.instcmd = instcmd;
214
217
        dstate_setinfo("driver.version.internal", "%s", DRV_VERSION);
215
218
}
216
219
 
217
 
void pollfail(char *why)
 
220
static void pollfail(const char *why)
218
221
{
219
222
        poll_failures++;
220
223
 
228
231
void upsdrv_updateinfo(void)
229
232
{
230
233
        char    temp[256], utility[16], loadpct[16], acfreq[16], battvolt[16],
231
 
                upstemp[16], stat[16], outvolt[16];
 
234
                upstemp[16], pstat[16], outvolt[16];
232
235
        int     util, ret;
233
236
        double  bvoltp;
234
237
 
235
 
        upssend("Q1\r");
 
238
        ser_send(upsfd, "Q1\r");
236
239
 
237
 
        ret = upsrecv (temp, sizeof(temp), ENDCHAR, "");
 
240
        ret = ser_get_line(upsfd, temp, sizeof(temp), ENDCHAR, "", 3, 0);
238
241
 
239
242
        /* sanity checks for poll data */
240
243
        if (strlen(temp) < 46) {
268
271
         */
269
272
 
270
273
        sscanf(temp, "%*c%s %*s %s %s %s %s %s %s", utility, outvolt, loadpct,
271
 
                acfreq, battvolt, upstemp, stat);
 
274
                acfreq, battvolt, upstemp, pstat);
272
275
 
273
276
        dstate_setinfo("output.voltage", "%s", outvolt);
274
277
        dstate_setinfo("input.voltage", "%s", utility);
285
288
 
286
289
        util = atoi(utility);
287
290
 
288
 
        if (stat[0] == '0') {
 
291
        if (pstat[0] == '0') {
289
292
                status_set("OL");               /* on line */
290
293
 
291
294
                /* only allow these when OL since they're bogus when OB */
292
 
                if (stat[2] == '1') {           /* boost or trim in effect */
 
295
                if (pstat[2] == '1') {          /* boost or trim in effect */
293
296
                        if (util < lownorm)
294
297
                                status_set("BOOST");
295
298
 
301
304
                status_set("OB");               /* on battery */
302
305
        }
303
306
 
304
 
        if (stat[1] == '1')
 
307
        if (pstat[1] == '1')
305
308
                status_set("LB");               /* low battery */
306
309
 
307
310
        status_commit();
318
321
/* power down the attached load immediately */
319
322
void upsdrv_shutdown(void)
320
323
{
321
 
        char    temp[256], stat[32];
 
324
        char    temp[256], pstat[32];
322
325
 
323
326
        /* basic idea: find out line status and send appropriate command */
324
327
 
325
 
        upssend("Q1\r");
326
 
        upsrecv (temp, sizeof(temp), ENDCHAR, "");
327
 
        sscanf (temp, "%*s %*s %*s %*s %*s %*s %*s %s", stat);
 
328
        ser_send(upsfd, "Q1\r");
 
329
        ser_get_line(upsfd, temp, sizeof(temp), ENDCHAR, "", 3, 0);
 
330
        sscanf (temp, "%*s %*s %*s %*s %*s %*s %*s %s", pstat);
328
331
 
329
332
        /* on battery: send S01<cr>, ups will return by itself on utility */
330
333
        /* on line: send S01R0003<cr>, ups will cycle and return soon */
331
334
 
332
 
        upssend("S01");
 
335
        ser_send(upsfd, "S01");
333
336
 
334
 
        if (stat[0] == '0') {                   /* on line */
 
337
        if (pstat[0] == '0') {                  /* on line */
335
338
                printf("On line, sending shutdown+return command...\n");
336
 
                upssend("R0003");
 
339
                ser_send(upsfd, "R0003");
337
340
        } else
338
341
                printf("On battery, sending normal shutdown command...\n");
339
342
 
340
 
        upssendchar(13);        /* end sequence */
 
343
        ser_send_char(upsfd, 13);       /* end sequence */
341
344
}
342
345
 
343
346
void upsdrv_help(void)
357
360
 
358
361
void upsdrv_initups(void)
359
362
{
360
 
        open_serial(device_path, B2400);
 
363
        upsfd = ser_open(device_path);
 
364
        ser_set_speed(upsfd, device_path, B2400);
361
365
}
362
366
 
363
367
void upsdrv_cleanup(void)
364
368
{
 
369
        ser_close(upsfd, device_path);
365
370
}