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

« back to all changes in this revision

Viewing changes to drivers/everups.c

  • Committer: Tuomas Heino
  • Author(s): Laurent Bigonville
  • Date: 2014-04-22 20:46:12 UTC
  • Revision ID: iheino+ub@cc.hut.fi-20140422204612-1x2gh3nkezfsdao4
Tags: upstream-2.7.2
ImportĀ upstreamĀ versionĀ 2.7.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* everups.c - support for Ever UPS models
 
2
 
 
3
   Copyright (C) 2001  Bartek Szady <bszx@bszxdomain.edu.eu.org>
 
4
 
 
5
   This program is free software; you can redistribute it and/or modify
 
6
   it under the terms of the GNU General Public License as published by
 
7
   the Free Software Foundation; either version 2 of the License, or
 
8
   (at your option) any later version.
 
9
 
 
10
   This program is distributed in the hope that it will be useful,
 
11
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
   GNU General Public License for more details.
 
14
 
 
15
   You should have received a copy of the GNU General Public License
 
16
   along with this program; if not, write to the Free Software
 
17
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
18
*/
 
19
 
 
20
#include "main.h"
 
21
#include "serial.h"
 
22
 
 
23
#define DRIVER_NAME     "Ever UPS driver"
 
24
#define DRIVER_VERSION  "0.03"
 
25
 
 
26
/* driver description structure */
 
27
upsdrv_info_t upsdrv_info = {
 
28
        DRIVER_NAME,
 
29
        DRIVER_VERSION,
 
30
        "Bartek Szady <bszx@bszxdomain.edu.eu.org>",
 
31
        DRV_STABLE,
 
32
        { NULL }
 
33
};
 
34
 
 
35
static  unsigned char   upstype = 0;
 
36
 
 
37
static void init_serial(void)
 
38
{
 
39
        ser_set_dtr(upsfd, 0);
 
40
        ser_set_rts(upsfd, 0);
 
41
}
 
42
 
 
43
static int Code(int tries)
 
44
{
 
45
        unsigned char cRecv;
 
46
        do {
 
47
                ser_send_char(upsfd, 208);
 
48
                ser_get_char(upsfd, &cRecv, 3, 0);
 
49
                if (cRecv==208)
 
50
                        return 1;
 
51
        } while (--tries>0);
 
52
        return 0;
 
53
}
 
54
 
 
55
static int InitUpsType(void)
 
56
{
 
57
        if (Code(1)) {
 
58
                ser_send_char(upsfd, 173);
 
59
                ser_get_char(upsfd, &upstype, 3, 0);
 
60
                return 1;
 
61
        } else
 
62
                return 0;
 
63
}
 
64
 
 
65
static const char *GetTypeUpsName(void)
 
66
{
 
67
        switch(upstype)
 
68
        {
 
69
                case 67: return "NET 500-DPC";
 
70
                case 68: return "NET 700-DPC";
 
71
                case 69: return "NET 1000-DPC";
 
72
                case 70: return "NET 1400-DPC";
 
73
                case 71: return "NET 2200-DPC";
 
74
                case 73: return "NET 700-DPC (new)";
 
75
                case 74: return "NET 1000-DPC (new)";
 
76
                case 75: return "NET 1400-DPC (new)";
 
77
                case 76: return "NET 500-DPC (new)";
 
78
                case 81: return "AP 450-PRO";
 
79
                case 82: return "AP 650-PRO";
 
80
                default:
 
81
                        return "Unknown";
 
82
        }
 
83
}
 
84
 
 
85
void upsdrv_initinfo(void)
 
86
{
 
87
        dstate_setinfo("ups.mfr", "Ever");
 
88
        dstate_setinfo("ups.model", "%s", GetTypeUpsName());
 
89
}
 
90
 
 
91
void upsdrv_updateinfo(void)
 
92
{
 
93
        int     battery=0,standby=0;
 
94
        unsigned char recBuf[2];
 
95
        unsigned long acuV;
 
96
        unsigned long lineV;
 
97
        double  fVal;
 
98
        
 
99
        if (!Code(2)) {
 
100
                upslog_with_errno(LOG_INFO, "Code failed");
 
101
                dstate_datastale();
 
102
                return;
 
103
        }
 
104
        /*Line status*/
 
105
        ser_send_char(upsfd, 175);
 
106
        ser_get_char(upsfd, recBuf, 3, 0);
 
107
        if ((recBuf[0] & 1) !=0)
 
108
                standby=1;
 
109
        else 
 
110
                battery=(recBuf[0] &4) !=0;
 
111
        if (Code(1)) {  /*Accumulator voltage value*/
 
112
                ser_send_char(upsfd, 189);
 
113
                ser_get_char(upsfd, recBuf, 3, 0);
 
114
                acuV=((unsigned long)recBuf[0])*150;
 
115
                acuV/=255;
 
116
        } else {
 
117
                upslog_with_errno(LOG_INFO, "Code failed");
 
118
                dstate_datastale();
 
119
                return;
 
120
        }
 
121
        if (Code(1)) {  /*Line voltage*/
 
122
                ser_send_char(upsfd, 245);
 
123
                ser_get_buf_len(upsfd, recBuf, 2, 3, 0);
 
124
                if ( upstype > 72 && upstype < 77)
 
125
                        lineV=(recBuf[0]*100+recBuf[1]*25600)/352;
 
126
                else
 
127
                        lineV=(recBuf[0]*100+recBuf[1]*25600)/372;
 
128
        } else {
 
129
                upslog_with_errno(LOG_INFO, "Code failed");
 
130
                dstate_datastale();
 
131
                return;
 
132
        }
 
133
 
 
134
        status_init();
 
135
 
 
136
        if (battery && acuV<105)
 
137
                status_set("LB");       /* low battery */
 
138
 
 
139
        if (battery)
 
140
                status_set("OB");       /* on battery */
 
141
        else
 
142
                status_set("OL");       /* on line */
 
143
 
 
144
        status_commit();
 
145
 
 
146
        dstate_setinfo("input.voltage", "%03ld", lineV);
 
147
        dstate_setinfo("battery.voltage", "%03.2f", (double)acuV /10.0);
 
148
 
 
149
        fVal=((double)acuV-95.0)*100.0;
 
150
        if (standby)
 
151
          fVal/=(135.5-95.0);
 
152
        else
 
153
          fVal/=(124.5-95.0);
 
154
        if (fVal>100)
 
155
                fVal=100;
 
156
        else if (fVal<0)
 
157
                fVal=0;
 
158
 
 
159
        dstate_setinfo("battery.charge", "%03.1f", fVal);
 
160
 
 
161
        dstate_dataok();
 
162
}
 
163
 
 
164
void upsdrv_shutdown(void)
 
165
{
 
166
        if (!Code(2)) {
 
167
                upslog_with_errno(LOG_INFO, "Code failed");
 
168
                return;
 
169
        }
 
170
        ser_send_char(upsfd, 28);
 
171
        ser_send_char(upsfd, 1);  /* 1.28 sec */
 
172
        if (!Code(1)) {
 
173
                upslog_with_errno(LOG_INFO, "Code failed");
 
174
                return;
 
175
        }
 
176
        ser_send_char(upsfd, 13);
 
177
        ser_send_char(upsfd, 8);
 
178
}
 
179
 
 
180
void upsdrv_help(void)
 
181
{
 
182
}
 
183
 
 
184
/* list flags and values that you want to receive via -x */
 
185
void upsdrv_makevartable(void)
 
186
{
 
187
}
 
188
 
 
189
void upsdrv_initups(void)
 
190
{
 
191
        upsfd = ser_open(device_path);
 
192
        ser_set_speed(upsfd, device_path, B300);
 
193
 
 
194
        init_serial();
 
195
        InitUpsType();
 
196
}
 
197
 
 
198
void upsdrv_cleanup(void)
 
199
{
 
200
        ser_close(upsfd, device_path);
 
201
}