~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to ksysguard/ksysguardd/FreeBSD/acpi.c

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    KSysGuard, the KDE System Guard
 
3
 
 
4
    Copyright (c) 2011 David Naylor <naylor.b.david@gmail.com>
 
5
 
 
6
    This program is free software; you can redistribute it and/or
 
7
    modify it under the terms of version 2 or later of the GNU General Public
 
8
    License as published by the Free Software Foundation.
 
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
18
 
 
19
*/
 
20
 
 
21
/*
 
22
 * TODO
 
23
 * - battery
 
24
 * - fan
 
25
 */
 
26
 
 
27
#include "acpi.h"
 
28
 
 
29
#include <fcntl.h>
 
30
#include <stdio.h>
 
31
#include <sys/ioctl.h>
 
32
#include <sys/types.h>
 
33
#include <sys/sysctl.h>
 
34
#include <unistd.h>
 
35
 
 
36
#include <dev/acpica/acpiio.h>
 
37
 
 
38
#include "Command.h"
 
39
 
 
40
#define BUF_LEN 80
 
41
 
 
42
#define MAXTZ 6
 
43
#define TZ_MIB "hw.acpi.thermal.tz%i.temperature"
 
44
#define TZ_MIB_LEN 32
 
45
#define TZ_MONITOR "acpi/thermal_zone/temp%i"
 
46
 
 
47
#define MAXBAT 6
 
48
#define BAT_CHARGE_MONITOR "acpi/battery/batt%i/batterycharge"
 
49
#define BAT_CAPACITY_MONITOR "acpi/battery/batt%i/batterycapacity"
 
50
#define BAT_REMAINING_MONITOR "acpi/battery/batt%i/remainingtime"
 
51
#define BAT_VOLTAGE_MONITOR "acpi/battery/batt%i/batteryvoltage"
 
52
#define BAT_RATE_MONITOR "acpi/battery/batt%i/batteryrate"
 
53
#define BAT_UNIT(bat) (bat_bif[bat].bif.units ? "mA" : "mW")
 
54
 
 
55
static int acpifd = -1;
 
56
 
 
57
static int tz_temp[MAXTZ];
 
58
static int tz_cnt;
 
59
 
 
60
static int bat_cnt;
 
61
static union acpi_battery_ioctl_arg bat_bif[MAXBAT], bat_bst[MAXBAT], bat_battinfo[MAXBAT];
 
62
 
 
63
void initACPI(struct SensorModul *sm) {
 
64
    char name[BUF_LEN];
 
65
    size_t len;
 
66
 
 
67
    /* Assume thermal zones use hw.acpi.thermal.tz%i.temperature format */
 
68
    for (tz_cnt = 0; tz_cnt < MAXTZ; ++tz_cnt) {
 
69
        len = sizeof(int);
 
70
        snprintf(name, TZ_MIB_LEN, TZ_MIB, tz_cnt);
 
71
        if (sysctlbyname(name, &tz_temp[tz_cnt], &len, NULL, 0))
 
72
            break;
 
73
        else {
 
74
            snprintf(name, BUF_LEN, TZ_MONITOR, tz_cnt + 1);
 
75
            registerMonitor(name, "float", printThermal, printThermalInfo, sm);
 
76
        }
 
77
    }
 
78
 
 
79
    if ((acpifd = open("/dev/acpi", O_RDONLY)) == -1) {
 
80
        log_error("unable to open /dev/acpi");
 
81
        return;
 
82
    }
 
83
 
 
84
    for (bat_cnt = 0; bat_cnt < MAXBAT; ++bat_cnt) {
 
85
        bat_bif[bat_cnt].unit = bat_cnt;
 
86
        if (ioctl(acpifd, ACPIIO_BATT_GET_BIF, &bat_bif[bat_cnt]))
 
87
            break;
 
88
        else {
 
89
            snprintf(name, BUF_LEN, BAT_CHARGE_MONITOR, bat_cnt + 1);
 
90
            registerMonitor(name, "integer", printBatCharge, printBatChargeInfo, sm);
 
91
            snprintf(name, BUF_LEN, BAT_CAPACITY_MONITOR, bat_cnt + 1);
 
92
            registerMonitor(name, "integer", printBatCapacity, printBatCapacityInfo, sm);
 
93
            snprintf(name, BUF_LEN, BAT_REMAINING_MONITOR, bat_cnt + 1);
 
94
            registerMonitor(name, "integer", printBatRemaining, printBatRemainingInfo, sm);
 
95
            snprintf(name, BUF_LEN, BAT_VOLTAGE_MONITOR, bat_cnt + 1);
 
96
            registerMonitor(name, "integer", printBatVoltage, printBatVoltageInfo, sm);
 
97
            snprintf(name, BUF_LEN, BAT_RATE_MONITOR, bat_cnt + 1);
 
98
            registerMonitor(name, "integer", printBatRate, printBatRateInfo, sm);
 
99
        }
 
100
    }
 
101
}
 
102
 
 
103
void exitACPI(void) {
 
104
    int bat, tz;
 
105
    char name[BUF_LEN];
 
106
 
 
107
    for (tz = 0; tz < tz_cnt; ++tz) {
 
108
        snprintf(name, BUF_LEN, TZ_MONITOR, tz + 1);
 
109
        removeMonitor(name);
 
110
    }
 
111
 
 
112
    if (acpifd != -1) {
 
113
        for (bat = 0; bat < bat_cnt; ++bat) {
 
114
            snprintf(name, TZ_MIB_LEN, BAT_CHARGE_MONITOR, bat + 1);
 
115
            removeMonitor(name);
 
116
            snprintf(name, BUF_LEN, BAT_CAPACITY_MONITOR, bat + 1);
 
117
            removeMonitor(name);
 
118
            snprintf(name, BUF_LEN, BAT_REMAINING_MONITOR, bat + 1);
 
119
            removeMonitor(name);
 
120
            snprintf(name, BUF_LEN, BAT_VOLTAGE_MONITOR, bat + 1);
 
121
            removeMonitor(name);
 
122
        }
 
123
 
 
124
        close(acpifd);
 
125
        acpifd = -1;
 
126
    }
 
127
}
 
128
 
 
129
int updateACPI(void) {
 
130
    int bat, tz;
 
131
    char name[TZ_MIB_LEN];
 
132
    size_t len;
 
133
 
 
134
    for (tz = 0; tz < tz_cnt; ++tz) {
 
135
        len = sizeof(int);
 
136
        snprintf(name, TZ_MIB_LEN, TZ_MIB, tz);
 
137
        sysctlbyname(name, &tz_temp[tz], &len, NULL, 0);
 
138
    }
 
139
 
 
140
    for (bat = 0; bat < bat_cnt; ++bat) {
 
141
        bat_bst[bat].unit = bat;
 
142
        ioctl(acpifd, ACPIIO_BATT_GET_BST, &bat_bst[bat]);
 
143
        bat_battinfo[bat].unit = bat;
 
144
        ioctl(acpifd, ACPIIO_BATT_GET_BATTINFO, &bat_battinfo[bat]);
 
145
    }
 
146
 
 
147
    return (0);
 
148
}
 
149
 
 
150
void printThermal(const char *cmd) {
 
151
    int tz;
 
152
 
 
153
    sscanf(cmd + 22, "%i", &tz);
 
154
    fprintf(CurrentClient, "%f\n", (tz_temp[tz - 1] - 2732) / 10.0);
 
155
}
 
156
 
 
157
void printThermalInfo(const char *cmd) {
 
158
    int tz;
 
159
 
 
160
    sscanf(cmd + 22, "%i", &tz);
 
161
    fprintf(CurrentClient, "ACPI Thermal Zone %i\t0\t0\tC\n", tz);
 
162
}
 
163
 
 
164
/*
 
165
void printBat(const char *cmd) {
 
166
    int bat;
 
167
 
 
168
    sscanf(cmd + 17, "%i", &bat);
 
169
    fprintf(CurrentClient, "");
 
170
}
 
171
*/
 
172
 
 
173
void printBatCharge(const char *cmd) {
 
174
    int bat;
 
175
 
 
176
    sscanf(cmd + 17, "%i", &bat);
 
177
    fprintf(CurrentClient, "%i\n", bat_bst[bat - 1].bst.cap);
 
178
}
 
179
 
 
180
void printBatChargeInfo(const char *cmd) {
 
181
    int bat;
 
182
 
 
183
    sscanf(cmd + 17, "%i", &bat);
 
184
    fprintf(CurrentClient, "Battery %i charge\t0\t%i\t%sh\n", bat, bat_bif[bat - 1].bif.dcap, BAT_UNIT(bat - 1));
 
185
}
 
186
 
 
187
void printBatCapacity(const char *cmd) {
 
188
    int bat;
 
189
 
 
190
    sscanf(cmd + 17, "%i", &bat);
 
191
    fprintf(CurrentClient, "%i\n", bat_battinfo[bat - 1].battinfo.cap);
 
192
}
 
193
 
 
194
void printBatCapacityInfo(const char *cmd) {
 
195
    int bat;
 
196
 
 
197
    sscanf(cmd + 17, "%i", &bat);
 
198
    fprintf(CurrentClient, "Battery %i capacity\t0\t100\t%%\n", bat);
 
199
}
 
200
 
 
201
void printBatRemaining(const char *cmd) {
 
202
    int bat;
 
203
 
 
204
    sscanf(cmd + 17, "%i", &bat);
 
205
    fprintf(CurrentClient, "%i\n", bat_battinfo[bat - 1].battinfo.min);
 
206
}
 
207
 
 
208
void printBatRemainingInfo(const char *cmd) {
 
209
    int bat;
 
210
 
 
211
    sscanf(cmd + 17, "%i", &bat);
 
212
    fprintf(CurrentClient, "Battery %i remaining time\t0\t0\tmin\n", bat);
 
213
}
 
214
 
 
215
void printBatVoltage(const char *cmd) {
 
216
    int bat;
 
217
 
 
218
    sscanf(cmd + 17, "%i", &bat);
 
219
    fprintf(CurrentClient, "%i\n", bat_bst[bat - 1].bst.volt);
 
220
}
 
221
 
 
222
void printBatVoltageInfo(const char *cmd) {
 
223
    int bat;
 
224
 
 
225
    sscanf(cmd + 17, "%i", &bat);
 
226
    fprintf(CurrentClient, "Battery %i voltage\t0\t%i\tmV\n", bat, bat_bif[bat - 1].bif.dvol);
 
227
}
 
228
 
 
229
void printBatRate(const char *cmd) {
 
230
    int bat;
 
231
 
 
232
    sscanf(cmd + 17, "%i", &bat);
 
233
    fprintf(CurrentClient, "%i\n", bat_bst[bat - 1].bst.rate);
 
234
}
 
235
 
 
236
void printBatRateInfo(const char *cmd) {
 
237
    int bat;
 
238
 
 
239
    sscanf(cmd + 17, "%i", &bat);
 
240
    fprintf(CurrentClient, "Battery %i discharge rate\t0\t0\t%s\n", bat, BAT_UNIT(bat - 1));
 
241
}