summaryrefslogtreecommitdiff
path: root/src/devkit-power-source.c
blob: 9172c595540d1e702cefaf414898240a2a837349 (plain)
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2008 David Zeuthen <david@fubar.dk>
 *
 * 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

#include <string.h>
#include <math.h>

#include <glib.h>
#include <glib/gstdio.h>
#include <glib/gi18n-lib.h>
#include <glib-object.h>
#include <dbus/dbus-glib.h>
#include <dbus/dbus-glib-lowlevel.h>
#include <devkit-gobject.h>
#include <polkit-dbus/polkit-dbus.h>

#include "sysfs-utils.h"
#include "devkit-power-source.h"
#include "devkit-power-marshal.h"

/*--------------------------------------------------------------------------------------------------------------*/
#include "devkit-power-source-glue.h"

typedef enum {
        DEVKIT_POWER_SOURCE_TYPE_LINE_POWER,
        DEVKIT_POWER_SOURCE_TYPE_BATTERY,
} DevkitPowerSourceType;

struct DevkitPowerSourcePrivate
{
        DBusGConnection *system_bus_connection;
        DBusGProxy      *system_bus_proxy;
        DevkitPowerDaemon *daemon;
        DevkitDevice *d;

        char *object_path;
        char *native_path;

        guint poll_timer_id;

        char *vendor;
        char *model;
        char *serial;
        GTimeVal update_time;
        DevkitPowerSourceType type;

        gboolean line_power_online;

        double battery_energy;
        double battery_energy_empty;
        double battery_energy_empty_design;
        double battery_energy_full;
        double battery_energy_full_design;
        double battery_energy_rate;
        gint64 battery_time_to_empty;
        gint64 battery_time_to_full;
        double battery_percentage;
        char *battery_technology;
};

static void     devkit_power_source_class_init  (DevkitPowerSourceClass *klass);
static void     devkit_power_source_init        (DevkitPowerSource      *seat);
static void     devkit_power_source_finalize    (GObject     *object);

static gboolean update (DevkitPowerSource *source);

enum
{
        PROP_0,
        PROP_NATIVE_PATH,
        PROP_VENDOR,
        PROP_MODEL,
        PROP_SERIAL,
        PROP_UPDATE_TIME,
        PROP_TYPE,
        PROP_LINE_POWER_ONLINE,
        PROP_BATTERY_ENERGY,
        PROP_BATTERY_ENERGY_EMPTY,
        PROP_BATTERY_ENERGY_EMPTY_DESIGN,
        PROP_BATTERY_ENERGY_FULL,
        PROP_BATTERY_ENERGY_FULL_DESIGN,
        PROP_BATTERY_ENERGY_RATE,
        PROP_BATTERY_TIME_TO_EMPTY,
        PROP_BATTERY_TIME_TO_FULL,
        PROP_BATTERY_PERCENTAGE,
        PROP_BATTERY_TECHNOLOGY,
};

enum
{
        CHANGED_SIGNAL,
        LAST_SIGNAL,
};

static guint signals[LAST_SIGNAL] = { 0 };

G_DEFINE_TYPE (DevkitPowerSource, devkit_power_source, DEVKIT_TYPE_POWER_DEVICE)
#define DEVKIT_POWER_SOURCE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), DEVKIT_TYPE_POWER_SOURCE, DevkitPowerSourcePrivate))

static const char *devkit_power_source_get_object_path (DevkitPowerDevice *device);
static void        devkit_power_source_removed         (DevkitPowerDevice *device);
static gboolean    devkit_power_source_changed         (DevkitPowerDevice *device,
                                                         DevkitDevice      *d,
                                                         gboolean           synthesized);

static void
get_property (GObject         *object,
              guint            prop_id,
              GValue          *value,
              GParamSpec      *pspec)
{
        DevkitPowerSource *source = DEVKIT_POWER_SOURCE (object);

        switch (prop_id) {
        case PROP_NATIVE_PATH:
                g_value_set_string (value, source->priv->native_path);
                break;
        case PROP_VENDOR:
                g_value_set_string (value, source->priv->vendor);
                break;
        case PROP_MODEL:
                g_value_set_string (value, source->priv->model);
                break;
        case PROP_SERIAL:
                g_value_set_string (value, source->priv->serial);
                break;
        case PROP_UPDATE_TIME:
                g_value_set_uint64 (value, source->priv->update_time.tv_sec);
                break;
        case PROP_TYPE:
                g_value_set_string (value, source->priv->type == DEVKIT_POWER_SOURCE_TYPE_LINE_POWER
                                    ? "line-power" : "battery");
                break;

        case PROP_LINE_POWER_ONLINE:
                g_value_set_boolean (value, source->priv->line_power_online);
                break;

        case PROP_BATTERY_ENERGY:
                g_value_set_double (value, source->priv->battery_energy);
                break;
        case PROP_BATTERY_ENERGY_EMPTY:
                g_value_set_double (value, source->priv->battery_energy_empty);
                break;
        case PROP_BATTERY_ENERGY_EMPTY_DESIGN:
                g_value_set_double (value, source->priv->battery_energy_empty_design);
                break;
        case PROP_BATTERY_ENERGY_FULL:
                g_value_set_double (value, source->priv->battery_energy_full);
                break;
        case PROP_BATTERY_ENERGY_FULL_DESIGN:
                g_value_set_double (value, source->priv->battery_energy_full_design);
                break;
        case PROP_BATTERY_ENERGY_RATE:
                g_value_set_double (value, source->priv->battery_energy_rate);
                break;
        case PROP_BATTERY_TIME_TO_EMPTY:
                g_value_set_int64 (value, source->priv->battery_time_to_empty);
                break;
        case PROP_BATTERY_TIME_TO_FULL:
                g_value_set_int64 (value, source->priv->battery_time_to_full);
                break;
        case PROP_BATTERY_PERCENTAGE:
                g_value_set_double (value, source->priv->battery_percentage);
                break;

        case PROP_BATTERY_TECHNOLOGY:
                g_value_set_string (value, source->priv->battery_technology);
                break;

        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
                break;
        }
}



static void
devkit_power_source_class_init (DevkitPowerSourceClass *klass)
{
        GObjectClass           *object_class = G_OBJECT_CLASS (klass);
        DevkitPowerDeviceClass *device_class = DEVKIT_POWER_DEVICE_CLASS (klass);

        object_class->finalize = devkit_power_source_finalize;
        object_class->get_property = get_property;
        device_class->changed = devkit_power_source_changed;
        device_class->removed = devkit_power_source_removed;
        device_class->get_object_path = devkit_power_source_get_object_path;

        g_type_class_add_private (klass, sizeof (DevkitPowerSourcePrivate));

        signals[CHANGED_SIGNAL] =
                g_signal_new ("changed",
                              G_OBJECT_CLASS_TYPE (klass),
                              G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
                              0,
                              NULL, NULL,
                              g_cclosure_marshal_VOID__VOID,
                              G_TYPE_NONE, 0);

        dbus_g_object_type_install_info (DEVKIT_TYPE_POWER_SOURCE, &dbus_glib_devkit_power_source_object_info);

        g_object_class_install_property (
                object_class,
                PROP_NATIVE_PATH,
                g_param_spec_string ("native-path", NULL, NULL, NULL, G_PARAM_READABLE));

        g_object_class_install_property (
                object_class,
                PROP_VENDOR,
                g_param_spec_string ("vendor", NULL, NULL, NULL, G_PARAM_READABLE));
        g_object_class_install_property (
                object_class,
                PROP_MODEL,
                g_param_spec_string ("model", NULL, NULL, NULL, G_PARAM_READABLE));
        g_object_class_install_property (
                object_class,
                PROP_SERIAL,
                g_param_spec_string ("serial", NULL, NULL, NULL, G_PARAM_READABLE));
        g_object_class_install_property (
                object_class,
                PROP_UPDATE_TIME,
                g_param_spec_uint64 ("update-time", NULL, NULL, 0, G_MAXUINT64, 0, G_PARAM_READABLE));
        g_object_class_install_property (
                object_class,
                PROP_TYPE,
                g_param_spec_string ("type", NULL, NULL, NULL, G_PARAM_READABLE));
        g_object_class_install_property (
                object_class,
                PROP_LINE_POWER_ONLINE,
                g_param_spec_boolean ("line-power-online", NULL, NULL, FALSE, G_PARAM_READABLE));
        g_object_class_install_property (
                object_class,
                PROP_BATTERY_ENERGY,
                g_param_spec_double ("battery-energy", NULL, NULL, 0, G_MAXDOUBLE, 0, G_PARAM_READABLE));
        g_object_class_install_property (
                object_class,
                PROP_BATTERY_ENERGY_EMPTY,
                g_param_spec_double ("battery-energy-empty", NULL, NULL, 0, G_MAXDOUBLE, 0, G_PARAM_READABLE));
        g_object_class_install_property (
                object_class,
                PROP_BATTERY_ENERGY_EMPTY_DESIGN,
                g_param_spec_double ("battery-energy-empty-design", NULL, NULL, 0, G_MAXDOUBLE, 0, G_PARAM_READABLE));
        g_object_class_install_property (
                object_class,
                PROP_BATTERY_ENERGY_FULL,
                g_param_spec_double ("battery-energy-full", NULL, NULL, 0, G_MAXDOUBLE, 0, G_PARAM_READABLE));
        g_object_class_install_property (
                object_class,
                PROP_BATTERY_ENERGY_FULL_DESIGN,
                g_param_spec_double ("battery-energy-full-design", NULL, NULL, 0, G_MAXDOUBLE, 0, G_PARAM_READABLE));
        g_object_class_install_property (
                object_class,
                PROP_BATTERY_ENERGY_RATE,
                g_param_spec_double ("battery-energy-rate", NULL, NULL, -G_MAXDOUBLE, G_MAXDOUBLE, 0, G_PARAM_READABLE));
        g_object_class_install_property (
                object_class,
                PROP_BATTERY_TIME_TO_EMPTY,
                g_param_spec_int64 ("battery-time-to-empty", NULL, NULL, -1, G_MAXINT64, -1, G_PARAM_READABLE));
        g_object_class_install_property (
                object_class,
                PROP_BATTERY_TIME_TO_FULL,
                g_param_spec_int64 ("battery-time-to-full", NULL, NULL, -1, G_MAXINT64, -1, G_PARAM_READABLE));
        g_object_class_install_property (
                object_class,
                PROP_BATTERY_PERCENTAGE,
                g_param_spec_double ("battery-percentage", NULL, NULL, -1, 100, -1, G_PARAM_READABLE));
        g_object_class_install_property (
                object_class,
                PROP_BATTERY_TECHNOLOGY,
                g_param_spec_string ("battery-technology", NULL, NULL, NULL, G_PARAM_READABLE));
}

static void
devkit_power_source_init (DevkitPowerSource *source)
{
        source->priv = DEVKIT_POWER_SOURCE_GET_PRIVATE (source);
        source->priv->battery_time_to_empty = -1;
        source->priv->battery_time_to_full = -1;
}

static void
devkit_power_source_finalize (GObject *object)
{
        DevkitPowerSource *source;

        g_return_if_fail (object != NULL);
        g_return_if_fail (DEVKIT_IS_POWER_SOURCE (object));

        source = DEVKIT_POWER_SOURCE (object);
        g_return_if_fail (source->priv != NULL);

        g_object_unref (source->priv->d);
        g_object_unref (source->priv->daemon);

        g_free (source->priv->native_path);

        g_free (source->priv->vendor);
        g_free (source->priv->model);
        g_free (source->priv->serial);

        if (source->priv->poll_timer_id > 0)
                g_source_remove (source->priv->poll_timer_id);

        G_OBJECT_CLASS (devkit_power_source_parent_class)->finalize (object);
}

static char *
compute_object_path_from_basename (const char *native_path_basename)
{
        char *basename;
        char *object_path;
        unsigned int n;

        /* TODO: need to be more thorough with making proper object
         * names that won't make D-Bus crash. This is just to cope
         * with dm-0...
         */
        basename = g_path_get_basename (native_path_basename);
        for (n = 0; basename[n] != '\0'; n++)
                if (basename[n] == '-')
                        basename[n] = '_';
        object_path = g_build_filename ("/sources/", basename, NULL);
        g_free (basename);

        return object_path;
}

static char *
compute_object_path (const char *native_path)
{
        char *basename;
        char *object_path;

        basename = g_path_get_basename (native_path);
        object_path = compute_object_path_from_basename (basename);
        g_free (basename);
        return object_path;
}

static gboolean
register_power_source (DevkitPowerSource *source)
{
        DBusConnection *connection;
        GError *error = NULL;

        source->priv->system_bus_connection = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
        if (source->priv->system_bus_connection == NULL) {
                if (error != NULL) {
                        g_critical ("error getting system bus: %s", error->message);
                        g_error_free (error);
                }
                goto error;
        }
        connection = dbus_g_connection_get_connection (source->priv->system_bus_connection);

        source->priv->object_path = compute_object_path (source->priv->native_path);

        dbus_g_connection_register_g_object (source->priv->system_bus_connection,
                                             source->priv->object_path,
                                             G_OBJECT (source));

        source->priv->system_bus_proxy = dbus_g_proxy_new_for_name (source->priv->system_bus_connection,
                                                                    DBUS_SERVICE_DBUS,
                                                                    DBUS_PATH_DBUS,
                                                                    DBUS_INTERFACE_DBUS);

        return TRUE;

error:
        return FALSE;
}

DevkitPowerSource *
devkit_power_source_new (DevkitPowerDaemon *daemon, DevkitDevice *d)
{
        DevkitPowerSource *source;
        const char *native_path;

        source = NULL;
        native_path = devkit_device_get_native_path (d);

        source = DEVKIT_POWER_SOURCE (g_object_new (DEVKIT_TYPE_POWER_SOURCE, NULL));
        source->priv->d = g_object_ref (d);
        source->priv->daemon = g_object_ref (daemon);
        source->priv->native_path = g_strdup (native_path);

        if (sysfs_file_exists (native_path, "online")) {
                source->priv->type = DEVKIT_POWER_SOURCE_TYPE_LINE_POWER;
        } else {
                source->priv->type = DEVKIT_POWER_SOURCE_TYPE_BATTERY;
        }

        if (!update (source)) {
                g_object_unref (source);
                source = NULL;
                goto out;
        }

        if (! register_power_source (DEVKIT_POWER_SOURCE (source))) {
                g_object_unref (source);
                source = NULL;
                goto out;
        }

out:
        return source;
}

static void
emit_changed (DevkitPowerSource *source)
{
        g_print ("emitting changed on %s\n", source->priv->native_path);
        g_signal_emit_by_name (source->priv->daemon,
                               "device-changed",
                               source->priv->object_path,
                               NULL);
        g_signal_emit (source, signals[CHANGED_SIGNAL], 0);
}

static gboolean
devkit_power_source_changed (DevkitPowerDevice *device, DevkitDevice *d, gboolean synthesized)
{
        DevkitPowerSource *source = DEVKIT_POWER_SOURCE (device);
        gboolean keep_source;

        g_object_unref (source->priv->d);
        source->priv->d = g_object_ref (d);

        keep_source = update (source);

        /* this 'change' event might prompt us to remove the source */
        if (!keep_source)
                goto out;

        /* no, it's good .. keep it */
        emit_changed (source);

out:
        return keep_source;
}

void
devkit_power_source_removed (DevkitPowerDevice *device)
{
}

static const char *
devkit_power_source_get_object_path (DevkitPowerDevice *device)
{
        DevkitPowerSource *source = DEVKIT_POWER_SOURCE (device);
        return source->priv->object_path;
}

/*--------------------------------------------------------------------------------------------------------------*/

static gboolean
update_line_power (DevkitPowerSource *source)
{
        source->priv->line_power_online = sysfs_get_int (source->priv->native_path, "online");
        return TRUE;
}

static gboolean
update_battery (DevkitPowerSource *source)
{
        char *status;
        gboolean is_charging;
        gboolean is_discharging;

        /* TODO: this needs to handle lots of special cases when certain
         *       files exist, it needs to prefer _avg to _now etc. etc. etc.
         *
         *       This is just a very quick hack for now.
         */

        status = g_strstrip (sysfs_get_string (source->priv->native_path, "status"));
        is_charging = strcasecmp (status, "charging") == 0;
        is_discharging = strcasecmp (status, "discharging") == 0;

        source->priv->battery_energy =
                sysfs_get_double (source->priv->native_path, "energy_now") / 1000000.0;
        source->priv->battery_energy_full =
                sysfs_get_double (source->priv->native_path, "energy_full") / 1000000.0;
        source->priv->battery_energy_full_design =
                sysfs_get_double (source->priv->native_path, "energy_full_design") / 1000000.0;
        source->priv->battery_energy_rate =
                fabs (sysfs_get_double (source->priv->native_path, "current_now") / 1000000.0);
        if (is_charging)
                source->priv->battery_energy_rate *= -1.0;

        source->priv->battery_percentage = 100.0 * source->priv->battery_energy / source->priv->battery_energy_full;
        if (source->priv->battery_percentage < 0)
                source->priv->battery_percentage = 0;
        if (source->priv->battery_percentage > 100.0)
                source->priv->battery_percentage = 100.0;

        g_free (status);
        return TRUE;
}

static gboolean
_poll_battery (DevkitPowerSource *source)
{
        g_warning ("No updates on source %s for 30 seconds; forcing update", source->priv->native_path);
        source->priv->poll_timer_id = 0;
        update (source);
        emit_changed (source);
        return FALSE;
}

static gboolean
update (DevkitPowerSource *source)
{
        gboolean ret;

        if (source->priv->poll_timer_id > 0) {
                g_source_remove (source->priv->poll_timer_id);
                source->priv->poll_timer_id = 0;
        }

        /* initial values */
        if (source->priv->vendor == NULL) {
                char *s;

                s = g_strstrip (sysfs_get_string (source->priv->native_path, "technology"));
                if (strcmp (s, "Unknown") != 0)
                        source->priv->battery_technology = s;
                else
                        g_free (s);

                source->priv->vendor = g_strstrip (sysfs_get_string (source->priv->native_path, "manufacturer"));
                source->priv->model = g_strstrip (sysfs_get_string (source->priv->native_path, "model_name"));
                source->priv->serial = g_strstrip (sysfs_get_string (source->priv->native_path, "serial_number"));
        }

        g_get_current_time (&(source->priv->update_time));

        switch (source->priv->type) {
        case DEVKIT_POWER_SOURCE_TYPE_LINE_POWER:
                ret = update_line_power (source);
                break;
        case DEVKIT_POWER_SOURCE_TYPE_BATTERY:

                ret = update_battery (source);

                /* Seems that we don't get change uevents from the
                 * kernel; set up a timer to poll
                 *
                 * TODO: perhaps only do this if running on battery.
                 */
                source->priv->poll_timer_id = g_timeout_add_seconds (30, (GSourceFunc) _poll_battery, source);

                break;
        default:
                g_assert_not_reached ();
                break;
        }

        return ret;
}

/*--------------------------------------------------------------------------------------------------------------*/

gboolean
devkit_power_source_refresh (DevkitPowerSource     *power_source,
                             DBusGMethodInvocation *context)
{
        update (power_source);
        dbus_g_method_return (context);
        return TRUE;
}