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

« back to all changes in this revision

Viewing changes to drivers/etapro.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:
50
50
      correctly connected to the serial port).
51
51
 */
52
52
 
53
 
#define DRV_VERSION "0.02"
 
53
#define DRV_VERSION "0.03"
54
54
 
55
55
#include <sys/ioctl.h>
56
56
#include "main.h"
 
57
#include "serial.h"
57
58
 
58
59
static int
59
60
etapro_get_response(const char *resp_type)
60
61
{
61
62
        char tmp[256];
62
63
        char *cp;
63
 
        int ret;
64
 
        unsigned int val;
 
64
        unsigned int n, val;
65
65
 
66
 
        ret = upsrecv(tmp, sizeof(tmp), '\n', "");
67
 
        if (ret < 0) {
 
66
        /* Read until a newline is found or there is no room in the buffer.
 
67
           Unlike ser_get_line(), don't discard the following characters
 
68
           because we have to handle multi-line responses.  */
 
69
        n = 0;
 
70
        while (ser_get_char(upsfd, &tmp[n], 1, 0) == 1) {
 
71
                if (n >= sizeof(tmp) - 1 || tmp[n] == '\n')
 
72
                        break;
 
73
                n++;
 
74
        }
 
75
        tmp[n] = '\0';
 
76
        if (n == 0) {
68
77
                upslogx(LOG_ERR, "no response from UPS");
69
78
                return -1;
70
79
        }
103
112
        int x;
104
113
 
105
114
        if (seconds == 0) {  /* cancel the running timer */
106
 
                upssend("RS\r");
 
115
                ser_send(upsfd, "RS\r");
107
116
                x = etapro_get_response("SV");
108
117
                if (x == 0x30)
109
118
                        return;  /* OK */
118
127
                        printf("UPS on in %d seconds\n", seconds);
119
128
                }
120
129
 
121
 
                upssend("RN%04X\r", seconds);
 
130
                ser_send(upsfd, "RN%04X\r", seconds);
122
131
                x = etapro_get_response("SV");
123
132
                if (x == 0x20)
124
133
                        return;  /* OK */
132
141
        int x;
133
142
 
134
143
        if (seconds == 0) {  /* cancel the running timer */
135
 
                upssend("RR\r");
 
144
                ser_send(upsfd, "RR\r");
136
145
                x = etapro_get_response("SV");
137
146
                if (x == 0x10)
138
147
                        return;  /* OK */
147
156
                        printf("UPS off in %d seconds\n", seconds);
148
157
                }
149
158
 
150
 
                upssend("RO%04X\r", seconds);
 
159
                ser_send(upsfd, "RO%04X\r", seconds);
151
160
                x = etapro_get_response("SV");
152
161
                if (x == 0)
153
162
                        return;  /* OK */
155
164
        upslogx(LOG_ERR, "etapro_set_off_timer: error, status=0x%02x", x);
156
165
}
157
166
 
158
 
int instcmd(const char *cmdname, const char *extra)
 
167
static int instcmd(const char *cmdname, const char *extra)
159
168
{
160
169
        if (!strcasecmp(cmdname, "load.off")) {
161
170
                etapro_set_off_timer(1);
184
193
        dstate_addcmd("shutdown.return");
185
194
 
186
195
        /* First command after power on returns junk - ignore it.  */
187
 
        upssend("RI\r");
 
196
        ser_send(upsfd, "RI\r");
188
197
        sleep(1);
189
 
        upsflushin(1, nut_debug_level, "");
190
198
 
191
199
        upsdrv_updateinfo();
192
200
 
193
 
        upsh.new_instcmd = instcmd;
 
201
        upsh.instcmd = instcmd;
194
202
}
195
203
 
196
204
void
200
208
        int x, flags;
201
209
        double utility, outvolt, battvolt, loadpct;
202
210
 
203
 
        upssend("RI\r");  /* identify */
 
211
        ser_flush_in(upsfd, "", nut_debug_level);
 
212
        ser_send(upsfd, "RI\r");  /* identify */
204
213
 
205
214
        x = etapro_get_response("SR");  /* manufacturer */
206
215
        if (x < 0) {
223
232
                return;
224
233
        }
225
234
 
226
 
        upssend("RP\r");  /* read measurements */
 
235
        ser_send(upsfd, "RP\r");  /* read measurements */
227
236
 
228
237
        x = etapro_get_response("SO");  /* status flags */
229
238
        if (x < 0) {
347
356
        int dtr_bit = TIOCM_DTR;
348
357
        int rts_bit = TIOCM_RTS;
349
358
 
350
 
        open_serial(device_path, B1200);
 
359
        upsfd = ser_open(device_path);
 
360
        ser_set_speed(upsfd, device_path, B1200);
 
361
 
351
362
        ioctl(upsfd, TIOCMBIC, &dtr_bit);
352
363
        ioctl(upsfd, TIOCMBIS, &rts_bit);
353
364
}
354
365
 
355
366
void upsdrv_cleanup(void)
356
367
{
 
368
        ser_close(upsfd, device_path);
357
369
}