1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
|
/***************************************************************************
* CVSID: $Id$
*
* addon-hid-ups.c : Detect UPS'es using the USB HID interface and
* add and maintain battery.* properties
*
* Copyright (C) 2005 David Zeuthen, <david@fubar.dk>
*
* Based on hid-ups.c: Copyright (c) 2001 Vojtech Pavlik
* <vojtech@ucw.cz>, Copyright (c) 2001 Paul
* Stewart <hiddev@wetlogic.net>, Tweaked by Kern
* Sibbald <kern@sibbald.com> to learn about USB
* UPSes. hid-ups.c is GPLv2 and available from
* examples directory of version 3.10.16 of the
* acpupsd project; see http://www.apcupsd.com.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
**************************************************************************/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
/* asm/types.h required for __s32 in linux/hiddev.h */
#include <asm/types.h>
#include <errno.h>
#include <fcntl.h>
#include <linux/hiddev.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <glib.h>
#include "libhal/libhal.h"
#include "../../util_helper.h"
#include "../../util_pm.h"
#include "../../logger.h"
#define UPS_USAGE 0x840000
#define UPS_SERIAL 0x8400fe
#define UPS_CHEMISTRY 0x850089
#define UPS_CAPACITY_MODE 0x85002c
#define UPS_BATTERY_VOLTAGE 0x840030
#define UPS_BELOW_RCL 0x840042
#define UPS_SHUTDOWN_IMMINENT 0x840069
#define UPS_PRODUCT 0x8400fe
#define UPS_SERIALNUMBER 0x8400ff
#define UPS_CHARGING 0x850044
#define UPS_DISCHARGING 0x850045
#define UPS_REMAINING_CAPACITY 0x850066
#define UPS_RUNTIME_TO_EMPTY 0x850068
#define UPS_AC_PRESENT 0x8500d0
#define UPS_BATTERYPRESENT 0x8500d1
#define UPS_DESIGNCAPACITY 0x850083
#define UPS_DEVICENAME 0x850088
#define UPS_DEVICECHEMISTRY 0x850089
#define UPS_RECHARGEABLE 0x85008b
#define UPS_OEMINFORMATION 0x85008f
#define STATE_NORMAL 0 /* unit powered */
#define STATE_DEBOUNCE 1 /* power failure */
#define STATE_BATTERY 2 /* power failure confirmed */
static dbus_bool_t
is_ups (int fd)
{
unsigned int i;
dbus_bool_t ret;
struct hiddev_devinfo device_info;
ret = FALSE;
if (ioctl (fd, HIDIOCGDEVINFO, &device_info) < 0)
goto out;
for (i = 0; i < device_info.num_applications; i++) {
if ((ioctl(fd, HIDIOCAPPLICATION, i) & 0xff0000) == UPS_USAGE) {
ret = TRUE;
goto out;
}
}
out:
return ret;
}
static char *
ups_get_string (int fd, int sindex)
{
static struct hiddev_string_descriptor sdesc;
if (sindex == 0) {
return "";
}
sdesc.index = sindex;
if (ioctl (fd, HIDIOCGSTRING, &sdesc) < 0) {
return "";
}
HAL_DEBUG (("foo: '%s'", sdesc.value));
return sdesc.value;
}
static dbus_bool_t
ups_get_static (LibHalContext *ctx, const char *udi, int fd,
dbus_int32_t *prop_remaining,
dbus_int32_t *prop_runtime,
dbus_bool_t *prop_charging,
dbus_bool_t *prop_discharging)
{
int ret;
struct hiddev_report_info rinfo;
struct hiddev_field_info finfo;
struct hiddev_usage_ref uref;
int rtype;
unsigned int i, j;
char *type;
DBusError error;
LibHalChangeSet *cs;
/* set to failure */
ret = FALSE;
cs = libhal_device_new_changeset (udi);
if (cs == NULL) {
HAL_ERROR (("Cannot initialize changeset"));
goto out;
}
/* first check that we are an UPS */
if (!is_ups (fd))
goto out;
for (rtype = HID_REPORT_TYPE_MIN; rtype <= HID_REPORT_TYPE_MAX; rtype++) {
rinfo.report_type = rtype;
rinfo.report_id = HID_REPORT_ID_FIRST;
while (ioctl (fd, HIDIOCGREPORTINFO, &rinfo) >= 0) {
for (i = 0; i < rinfo.num_fields; i++) {
memset (&finfo, 0, sizeof (finfo));
finfo.report_type = rinfo.report_type;
finfo.report_id = rinfo.report_id;
finfo.field_index = i;
ioctl (fd, HIDIOCGFIELDINFO, &finfo);
memset (&uref, 0, sizeof (uref));
for (j = 0; j < finfo.maxusage; j++) {
uref.report_type = finfo.report_type;
uref.report_id = finfo.report_id;
uref.field_index = i;
uref.usage_index = j;
ioctl (fd, HIDIOCGUCODE, &uref);
ioctl (fd, HIDIOCGUSAGE, &uref);
switch (uref.usage_code) {
case UPS_REMAINING_CAPACITY:
libhal_changeset_set_property_int (
cs, "battery.charge_level.current", uref.value);
libhal_changeset_set_property_int (
cs, "battery.charge_level.percentage", uref.value);
libhal_changeset_set_property_string (
cs, "battery.charge_level.unit", "percent");
libhal_changeset_set_property_int (
cs, "battery.reporting.current", uref.value);
libhal_changeset_set_property_int (
cs, "battery.reporting.percentage", uref.value);
libhal_changeset_set_property_string (
cs, "battery.reporting.unit", "percent");
*prop_remaining = uref.value;
break;
case UPS_RUNTIME_TO_EMPTY:
libhal_changeset_set_property_int (
cs, "battery.remaining_time", uref.value);
*prop_runtime = uref.value;
break;
case UPS_CHARGING:
libhal_changeset_set_property_bool (
cs, "battery.rechargeable.is_charging", uref.value != 0);
*prop_charging = uref.value != 0;
break;
case UPS_DISCHARGING:
libhal_changeset_set_property_bool (
cs, "battery.rechargeable.is_discharging", uref.value != 0);
*prop_discharging = uref.value != 0;
break;
case UPS_BATTERYPRESENT:
libhal_changeset_set_property_bool (
cs, "battery.present", uref.value != 0);
break;
case UPS_DEVICENAME:
libhal_changeset_set_property_string (
cs, "foo",
ups_get_string (fd, uref.value));
break;
case UPS_CHEMISTRY:
type = ups_get_string (fd, uref.value);
libhal_changeset_set_property_string (
cs, "battery.reporting.technology",
type);
libhal_changeset_set_property_string (
cs, "battery.technology",
util_get_battery_technology (type));
break;
case UPS_RECHARGEABLE:
libhal_changeset_set_property_bool (
cs, "battery.is_rechargeable", uref.value != 0);
break;
case UPS_OEMINFORMATION:
libhal_changeset_set_property_string (
cs, "battery.vendor",
ups_get_string (fd, uref.value));
break;
case UPS_PRODUCT:
libhal_changeset_set_property_string (
cs, "battery.model",
ups_get_string (fd, uref.value));
break;
case UPS_SERIALNUMBER:
libhal_changeset_set_property_string (
cs, "battery.serial",
ups_get_string (fd, uref.value));
break;
case UPS_DESIGNCAPACITY:
libhal_changeset_set_property_int (
cs, "battery.charge_level.design", uref.value);
libhal_changeset_set_property_int (
cs, "battery.charge_level.last_full", uref.value);
libhal_changeset_set_property_int (
cs, "battery.reporting.design", uref.value);
libhal_changeset_set_property_int (
cs, "battery.reporting.last_full", uref.value);
break;
default:
break;
}
}
}
rinfo.report_id |= HID_REPORT_ID_NEXT;
}
}
libhal_changeset_set_property_bool (cs, "battery.present", TRUE);
libhal_changeset_set_property_string (cs, "battery.type", "ups");
dbus_error_init (&error);
libhal_device_commit_changeset (ctx, cs, &error);
LIBHAL_FREE_DBUS_ERROR (&error);
libhal_device_add_capability (ctx, udi, "battery", &error);
ret = TRUE;
out:
LIBHAL_FREE_DBUS_ERROR (&error);
libhal_device_free_changeset (cs);
return ret;
}
int
main (int argc, char *argv[])
{
int fd;
char *udi;
char *device_file;
LibHalContext *ctx = NULL;
DBusError error;
unsigned int i;
fd_set fdset;
struct hiddev_event ev[64];
int rd;
dbus_int32_t prop_remaining = 0;
dbus_int32_t prop_runtime = 0;
dbus_bool_t prop_charging = FALSE;
dbus_bool_t prop_discharging = FALSE;
hal_set_proc_title_init (argc, argv);
setup_logger ();
udi = getenv ("UDI");
if (udi == NULL)
goto out;
dbus_error_init (&error);
if ((ctx = libhal_ctx_init_direct (&error)) == NULL)
goto out;
device_file = getenv ("HAL_PROP_HIDDEV_DEVICE");
if (device_file == NULL)
goto out;
fd = open (device_file, O_RDONLY);
if (fd < 0)
goto out;
if (!ups_get_static (ctx, udi, fd, &prop_remaining, &prop_runtime, &prop_charging, &prop_discharging))
goto out;
hal_set_proc_title ("hald-addon-hid-ups: listening on %s", device_file);
if (!libhal_device_addon_is_ready (ctx, udi, &error)) {
goto out;
}
FD_ZERO(&fdset);
while (1) {
FD_SET(fd, &fdset);
rd = select(fd+1, &fdset, NULL, NULL, NULL);
if (rd > 0) {
LibHalChangeSet *cs;
rd = read(fd, ev, sizeof(ev));
if (rd < (int) sizeof(ev[0])) {
close(fd);
goto out;
}
cs = libhal_device_new_changeset (udi);
if (cs == NULL) {
HAL_ERROR (("Cannot initialize changeset"));
goto out;
}
for (i = 0; i < rd / sizeof(ev[0]); i++) {
switch (ev[i].hid) {
case UPS_REMAINING_CAPACITY:
if (ev[i].value != prop_remaining) {
prop_remaining = ev[i].value;
libhal_changeset_set_property_int (
cs, "battery.charge_level.current", ev[i].value);
libhal_changeset_set_property_int (
cs, "battery.charge_level.percentage", ev[i].value);
libhal_changeset_set_property_int (
cs, "battery.reporting.current", ev[i].value);
libhal_changeset_set_property_int (
cs, "battery.reporting.percentage", ev[i].value);
}
break;
case UPS_RUNTIME_TO_EMPTY:
if (ev[i].value != prop_runtime) {
prop_runtime = ev[i].value;
libhal_changeset_set_property_int (
cs, "battery.remaining_time", ev[i].value);
}
break;
case UPS_CHARGING:
if ((ev[i].value != 0) != prop_charging) {
prop_charging = (ev[i].value != 0);
libhal_changeset_set_property_bool (
cs, "battery.rechargeable.is_charging", ev[i].value != 0);
}
break;
case UPS_DISCHARGING:
if ((ev[i].value != 0) != prop_discharging) {
prop_discharging = (ev[i].value != 0);
libhal_changeset_set_property_bool (
cs, "battery.rechargeable.is_discharging", ev[i].value != 0);
}
break;
default:
break;
}
}
LIBHAL_FREE_DBUS_ERROR (&error);
/* NOTE: commit_changeset won't do IPC if set is empty */
libhal_device_commit_changeset (ctx, cs, &error);
LIBHAL_FREE_DBUS_ERROR (&error);
libhal_device_free_changeset (cs);
}
}
out:
LIBHAL_FREE_DBUS_ERROR (&error);
if (ctx != NULL) {
libhal_ctx_shutdown (ctx, &error);
LIBHAL_FREE_DBUS_ERROR (&error);
libhal_ctx_free (ctx);
}
return 0;
}
|