UPS: apcupsd clean sources
[tomato.git] / release / src / router / apcupsd / src / drivers / snmp / drv_rfc1628.c
blobf1421964752364b246995ce1f646668c266b7f3c
1 /*
2 * drv_rfc1628.c
4 * rfc1628 aka UPS-MIB driver
5 */
7 /*
8 * Copyright (C) 2000-2004 Kern Sibbald
9 * Copyright (C) 1999-2002 Riccardo Facchetti <riccardo@apcupsd.org>
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of version 2 of the GNU General
13 * Public License as published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public
21 * License along with this program; if not, write to the Free
22 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
23 * MA 02111-1307, USA.
26 #include "apc.h"
27 #include "snmp.h"
28 #include "snmp_private.h"
30 static int rfc_1628_check_alarms(UPSINFO *ups)
32 struct snmp_ups_internal_data *Sid =
33 (struct snmp_ups_internal_data *)ups->driver_internal_data;
34 struct snmp_session *s = &Sid->session;
35 ups_mib_t *data = (ups_mib_t *)Sid->MIB;
38 * Check the Ethernet COMMLOST first, then check the
39 * Agent/SNMP->UPS serial COMMLOST together with all the other
40 * alarms.
42 if (ups_mib_mgr_get_upsAlarmEntry(s, &(data->upsAlarmEntry)) == -1) {
43 ups->set_commlost();
44 free(data->upsAlarmEntry);
45 return 0;
46 } else {
47 ups->clear_commlost();
50 free(data->upsAlarmEntry);
51 return 1;
54 int rfc1628_snmp_kill_ups_power(UPSINFO *ups)
56 return 0;
59 int rfc1628_snmp_ups_get_capabilities(UPSINFO *ups)
61 int i = 0;
64 * Assume that an UPS with SNMP control has all the capabilities.
65 * We know that the RFC1628 doesn't even implement some of the
66 * capabilities. We do this way for sake of simplicity.
68 for (i = 0; i <= CI_MAX_CAPS; i++)
69 ups->UPS_Cap[i] = TRUE;
71 return 1;
74 int rfc1628_snmp_ups_read_static_data(UPSINFO *ups)
76 struct snmp_ups_internal_data *Sid =
77 (struct snmp_ups_internal_data *)ups->driver_internal_data;
78 struct snmp_session *s = &Sid->session;
79 ups_mib_t *data = (ups_mib_t *)Sid->MIB;
81 if (rfc_1628_check_alarms(ups) == 0) {
82 return 0;
85 data->upsIdent = NULL;
86 ups_mib_mgr_get_upsIdent(s, &(data->upsIdent));
87 if (data->upsIdent) {
88 SNMP_STRING(upsIdent, Model, upsmodel);
89 SNMP_STRING(upsIdent, Name, upsname);
90 free(data->upsIdent);
93 return 1;
96 int rfc1628_snmp_ups_read_volatile_data(UPSINFO *ups)
98 struct snmp_ups_internal_data *Sid =
99 (struct snmp_ups_internal_data *)ups->driver_internal_data;
100 struct snmp_session *s = &Sid->session;
101 ups_mib_t *data = (ups_mib_t *)Sid->MIB;
103 if (rfc_1628_check_alarms(ups) == 0) {
104 return 0;
107 data->upsBattery = NULL;
108 ups_mib_mgr_get_upsBattery(s, &(data->upsBattery));
109 if (data->upsBattery) {
110 switch (data->upsBattery->__upsBatteryStatus) {
111 case 2:
112 ups->clear_battlow();
113 break;
114 case 3:
115 ups->set_battlow();
116 break;
117 default: /* Unknown, assume battery is ok */
118 ups->clear_battlow();
119 break;
122 ups->BattChg = data->upsBattery->__upsEstimatedChargeRemaining;
123 ups->UPSTemp = data->upsBattery->__upsBatteryTemperature;
124 ups->TimeLeft = data->upsBattery->__upsEstimatedMinutesRemaining;
126 free(data->upsBattery);
129 data->upsInputEntry = NULL;
130 ups_mib_mgr_get_upsInputEntry(s, &(data->upsInputEntry));
131 if (data->upsInputEntry) {
132 ups->LineVoltage = data->upsInputEntry->__upsInputVoltage;
133 ups->LineFreq = data->upsInputEntry->__upsInputFrequency / 10;
135 if (ups->LineMax < ups->LineVoltage) {
136 ups->LineMax = ups->LineVoltage;
139 if (ups->LineMin > ups->LineVoltage || ups->LineMin == 0) {
140 ups->LineMin = ups->LineVoltage;
143 free(data->upsInputEntry);
146 data->upsOutputEntry = NULL;
147 ups_mib_mgr_get_upsOutputEntry(s, &(data->upsOutputEntry));
148 if (data->upsOutputEntry) {
149 ups->OutputVoltage = data->upsOutputEntry->__upsOutputVoltage;
150 ups->UPSLoad = data->upsOutputEntry->__upsOutputPercentLoad;
151 ups->OutputCurrent = data->upsOutputEntry->__upsOutputCurrent;
153 free(data->upsOutputEntry);
156 return 1;
159 int rfc1628_snmp_ups_check_state(UPSINFO *ups)
161 /* Wait the required amount of time before bugging the device. */
162 sleep(ups->wait_time);
164 write_lock(ups);
165 rfc_1628_check_alarms(ups);
166 write_unlock(ups);
168 return 1;