summaryrefslogtreecommitdiff
path: root/src/dkp-supply.c
blob: b68ecd368f041c790a3fb3f95314d506c889f4c6 (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2008 David Zeuthen <davidz@redhat.com>
 * Copyright (C) 2008 Richard Hughes <richard@hughsie.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

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

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

#include "sysfs-utils.h"
#include "egg-debug.h"
#include "egg-string.h"

#include "dkp-enum.h"
#include "dkp-object.h"
#include "dkp-supply.h"

#define DKP_SUPPLY_REFRESH_TIMEOUT	10L

struct DkpSupplyPrivate
{
	guint			 poll_timer_id;
	gboolean		 has_coldplug_values;
	gdouble			 energy_old;
	GTimeVal		 energy_old_timespec;
};

static void	dkp_supply_class_init	(DkpSupplyClass	*klass);

G_DEFINE_TYPE (DkpSupply, dkp_supply, DKP_TYPE_DEVICE)
#define DKP_SUPPLY_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), DKP_TYPE_SUPPLY, DkpSupplyPrivate))

static gboolean		 dkp_supply_refresh	 	(DkpDevice *device);

/**
 * dkp_supply_refresh_line_power:
 **/
static gboolean
dkp_supply_refresh_line_power (DkpSupply *supply)
{
	DkpDevice *device = DKP_DEVICE (supply);
	DkpObject *obj = dkp_device_get_obj (device);

	/* force true */
	obj->power_supply = TRUE;

	/* get new AC value */
	obj->online = sysfs_get_int (obj->native_path, "online");

	return TRUE;
}

/**
 * dkp_supply_reset_values:
 **/
static void
dkp_supply_reset_values (DkpSupply *supply)
{
	gchar *native_path;
	DkpDeviceType type;
	DkpDevice *device = DKP_DEVICE (supply);
	DkpObject *obj = dkp_device_get_obj (device);

	/* some stuff we copy */
	type = obj->type;
	native_path = g_strdup (obj->native_path);

	supply->priv->has_coldplug_values = FALSE;
	supply->priv->energy_old = 0;
	supply->priv->energy_old_timespec.tv_sec = 0;
	dkp_object_clear (obj);

	/* restore the saved stuff */
	obj->type = type;
	obj->native_path = native_path;
}

/**
 * dkp_supply_get_on_battery:
 **/
static gboolean
dkp_supply_get_on_battery (DkpDevice *device, gboolean *on_battery)
{
	DkpSupply *supply = DKP_SUPPLY (device);
	DkpObject *obj = dkp_device_get_obj (device);

	g_return_val_if_fail (DKP_IS_SUPPLY (supply), FALSE);
	g_return_val_if_fail (on_battery != NULL, FALSE);

	if (obj->type != DKP_DEVICE_TYPE_BATTERY)
		return FALSE;
	if (!obj->is_present)
		return FALSE;

	*on_battery = (obj->state == DKP_DEVICE_STATE_DISCHARGING);
	return TRUE;
}

/**
 * dkp_supply_get_low_battery:
 **/
static gboolean
dkp_supply_get_low_battery (DkpDevice *device, gboolean *low_battery)
{
	gboolean ret;
	gboolean on_battery;
	DkpSupply *supply = DKP_SUPPLY (device);
	DkpObject *obj = dkp_device_get_obj (device);

	g_return_val_if_fail (DKP_IS_SUPPLY (supply), FALSE);
	g_return_val_if_fail (low_battery != NULL, FALSE);

	/* reuse the common checks */
	ret = dkp_supply_get_on_battery (device, &on_battery);
	if (!ret)
		return FALSE;

	/* shortcut */
	if (!on_battery) {
		*low_battery = FALSE;
		return TRUE;
	}

	*low_battery = (obj->percentage < 10);
	return TRUE;
}

/**
 * dkp_supply_calculate_rate:
 **/
static void
dkp_supply_calculate_rate (DkpSupply *supply)
{
	guint time;
	gdouble energy;
	GTimeVal now;
	DkpDevice *device = DKP_DEVICE (supply);
	DkpObject *obj = dkp_device_get_obj (device);

	if (obj->energy < 0)
		return;

	if (supply->priv->energy_old < 0)
		return;

	if (supply->priv->energy_old == obj->energy)
		return;

	/* get the time difference */
	g_get_current_time (&now);
	time = now.tv_sec - supply->priv->energy_old_timespec.tv_sec;

	if (time == 0)
		return;

	/* get the difference in charge */
	energy = supply->priv->energy_old - obj->energy;
	if (energy < 0.1)
		return;

	/* probably okay */
	obj->energy_rate = energy * 3600 / time;
}

/**
 * dkp_supply_convert_device_technology:
 **/
static DkpDeviceTechnology
dkp_supply_convert_device_technology (const gchar *type)
{
	if (type == NULL)
		return DKP_DEVICE_TECHNOLGY_UNKNOWN;
	/* every case combination of Li-Ion is commonly used.. */
	if (strcasecmp (type, "li-ion") == 0 ||
	    strcasecmp (type, "lion") == 0)
		return DKP_DEVICE_TECHNOLGY_LITHIUM_ION;
	if (strcasecmp (type, "pb") == 0 ||
	    strcasecmp (type, "pbac") == 0)
		return DKP_DEVICE_TECHNOLGY_LEAD_ACID;
	if (strcasecmp (type, "lip") == 0 ||
	    strcasecmp (type, "lipo") == 0)
		return DKP_DEVICE_TECHNOLGY_LITHIUM_POLYMER;
	if (strcasecmp (type, "nimh") == 0)
		return DKP_DEVICE_TECHNOLGY_NICKEL_METAL_HYDRIDE;
	if (strcasecmp (type, "lifo") == 0)
		return DKP_DEVICE_TECHNOLGY_LITHIUM_IRON_PHOSPHATE;
	return DKP_DEVICE_TECHNOLGY_UNKNOWN;
}

/**
 * dkp_supply_refresh_battery:
 *
 * Return value: TRUE if we changed
 **/
static gboolean
dkp_supply_refresh_battery (DkpSupply *supply)
{
	gchar *status = NULL;
	gboolean ret = TRUE;
	DkpDeviceState state;
	DkpDevice *device = DKP_DEVICE (supply);
	DkpObject *obj = dkp_device_get_obj (device);

	/* have we just been removed? */
	obj->is_present = sysfs_get_bool (obj->native_path, "present");
	if (!obj->is_present) {
		dkp_supply_reset_values (supply);
		obj->type = DKP_DEVICE_TYPE_BATTERY;
		goto out;
	}

	/* initial values */
	if (!supply->priv->has_coldplug_values) {
		gchar *technology_native;

		/* when we add via sysfs power_supply class then we know this is true */
		obj->power_supply = TRUE;

		/* the ACPI spec is bad at defining battery type constants */
		technology_native = g_strstrip (sysfs_get_string (obj->native_path, "technology"));
		obj->technology = dkp_supply_convert_device_technology (technology_native);
		g_free (technology_native);

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

		/* assume true for laptops */
		obj->is_rechargeable = TRUE;
		obj->has_history = TRUE;
		obj->has_statistics = TRUE;

		/* these don't change at runtime */
		obj->energy_full =
			sysfs_get_double (obj->native_path, "energy_full") / 1000000.0;
		obj->energy_full_design =
			sysfs_get_double (obj->native_path, "energy_full_design") / 1000000.0;

		/* the last full should not be bigger than the design */
		if (obj->energy_full > obj->energy_full_design)
			egg_warning ("energy_full (%f) is greater than energy_full_design (%f)",
				     obj->energy_full, obj->energy_full_design);

		/* some systems don't have this */
		if (obj->energy_full < 0.01) {
			egg_warning ("correcting energy_full (%f) using energy_full_design (%f)",
				     obj->energy_full, obj->energy_full_design);
			obj->energy_full = obj->energy_full_design;
		}

		/* calculate how broken our battery is */
		if (obj->energy_full > 0) {
			obj->capacity = obj->energy_full_design / obj->energy_full * 100.0f;
			if (obj->capacity < 0)
				obj->capacity = 0;
			if (obj->capacity > 100.0)
				obj->capacity = 100.0;
		}

		/* we only coldplug once, as these values will never change */
		supply->priv->has_coldplug_values = TRUE;
	}

	status = g_strstrip (sysfs_get_string (obj->native_path, "status"));
	if (strcasecmp (status, "charging") == 0)
		state = DKP_DEVICE_STATE_CHARGING;
	else if (strcasecmp (status, "discharging") == 0)
		state = DKP_DEVICE_STATE_DISCHARGING;
	else if (strcasecmp (status, "full") == 0)
		state = DKP_DEVICE_STATE_FULLY_CHARGED;
	else if (strcasecmp (status, "empty") == 0)
		state = DKP_DEVICE_STATE_EMPTY;
	else {
		egg_warning ("unknown status string: %s", status);
		state = DKP_DEVICE_STATE_UNKNOWN;
	}

	/* get the currect charge */
	obj->energy =
		sysfs_get_double (obj->native_path, "energy_avg") / 1000000.0;
	if (obj->energy == 0)
		obj->energy =
			sysfs_get_double (obj->native_path, "energy_now") / 1000000.0;

	/* some batteries don't update last_full attribute */
	if (obj->energy > obj->energy_full)
		obj->energy_full = obj->energy;

	obj->energy_rate =
		fabs (sysfs_get_double (obj->native_path, "current_now") / 1000000.0);
	obj->voltage =
		sysfs_get_double (obj->native_path, "voltage_now") / 1000000.0;

	/* ACPI gives out the special 'Ones' value for rate when it's unable
	 * to calculate the true rate. We should set the rate zero, and wait
	 * for the BIOS to stabilise. */
	if (obj->energy_rate == 0xffff)
		obj->energy_rate = 0;

	/* sanity check to less than 100W */
	if (obj->energy_rate > 100*1000)
		obj->energy_rate = 0;

	/* the hardware reporting failed -- try to calculate this */
	if (obj->energy_rate < 0) {
		dkp_supply_calculate_rate (supply);
	}

	/* get a precise percentage */
	obj->percentage = 100.0 * obj->energy / obj->energy_full;
	if (obj->percentage < 0)
		obj->percentage = 0;
	if (obj->percentage > 100.0)
		obj->percentage = 100.0;

	/* calculate a quick and dirty time remaining value */
	obj->time_to_empty = 0;
	obj->time_to_full = 0;
	if (obj->energy_rate > 0) {
		if (state == DKP_DEVICE_STATE_DISCHARGING) {
			obj->time_to_empty = 3600 * (obj->energy / obj->energy_rate);
		} else if (state == DKP_DEVICE_STATE_CHARGING) {
			obj->time_to_full = 3600 * ((obj->energy_full - obj->energy) / obj->energy_rate);
		}
	}
	/* check the remaining time is under a set limit, to deal with broken
	   primary batteries rate */
	if (obj->time_to_empty > (100 * 60 * 60))
		obj->time_to_empty = 0;
	if (obj->time_to_full > (100 * 60 * 60))
		obj->time_to_full = 0;

	/* set the old status */
	supply->priv->energy_old = obj->energy;
	g_get_current_time (&supply->priv->energy_old_timespec);

	/* we changed state */
	if (obj->state != state) {
		supply->priv->energy_old = 0;
		obj->state = state;
	}

out:
	g_free (status);
	return ret;
}

/**
 * dkp_supply_poll_battery:
 **/
static gboolean
dkp_supply_poll_battery (DkpSupply *supply)
{
	gboolean ret;
	DkpDevice *device = DKP_DEVICE (supply);
	DkpObject *obj = dkp_device_get_obj (device);

	egg_debug ("No updates on supply %s for 30 seconds; forcing update", obj->native_path);
	supply->priv->poll_timer_id = 0;
	ret = dkp_supply_refresh (device);
	if (ret)
		dkp_device_emit_changed (device);
	return FALSE;
}

/**
 * dkp_supply_coldplug:
 **/
static gboolean
dkp_supply_coldplug (DkpDevice *device)
{
	DkpSupply *supply = DKP_SUPPLY (device);
	DevkitDevice *d;
	const gchar *native_path;
	DkpObject *obj = dkp_device_get_obj (device);

	dkp_supply_reset_values (supply);

	/* detect what kind of device we are */
	d = dkp_device_get_d (device);
	if (d == NULL)
		egg_error ("could not get device");

	native_path = devkit_device_get_native_path (d);
	if (native_path == NULL)
		egg_error ("could not get native path");

	if (sysfs_file_exists (native_path, "online")) {
		obj->type = DKP_DEVICE_TYPE_LINE_POWER;
	} else {
		/* this is correct, UPS and CSR are not in the kernel */
		obj->type = DKP_DEVICE_TYPE_BATTERY;
	}

	/* coldplug values */
	dkp_supply_refresh (device);

	return TRUE;
}

/**
 * dkp_supply_refresh:
 **/
static gboolean
dkp_supply_refresh (DkpDevice *device)
{
	gboolean ret;
	GTimeVal time;
	DkpSupply *supply = DKP_SUPPLY (device);
	DkpObject *obj = dkp_device_get_obj (device);

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

	g_get_current_time (&time);
	obj->update_time = time.tv_sec;

	switch (obj->type) {
	case DKP_DEVICE_TYPE_LINE_POWER:
		ret = dkp_supply_refresh_line_power (supply);
		break;
	case DKP_DEVICE_TYPE_BATTERY:
		ret = dkp_supply_refresh_battery (supply);
		/* Seems that we don't get change uevents from the
		 * kernel on some BIOS types; set up a timer to poll
		 * if we are charging or discharging */
		if (obj->state == DKP_DEVICE_STATE_CHARGING ||
		    obj->state == DKP_DEVICE_STATE_DISCHARGING)
			supply->priv->poll_timer_id =
				g_timeout_add_seconds (DKP_SUPPLY_REFRESH_TIMEOUT,
						       (GSourceFunc) dkp_supply_poll_battery, supply);
		break;
	default:
		g_assert_not_reached ();
		break;
	}
	return ret;
}

/**
 * dkp_supply_init:
 **/
static void
dkp_supply_init (DkpSupply *supply)
{
	supply->priv = DKP_SUPPLY_GET_PRIVATE (supply);
}

/**
 * dkp_supply_finalize:
 **/
static void
dkp_supply_finalize (GObject *object)
{
	DkpSupply *supply;

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

	supply = DKP_SUPPLY (object);
	g_return_if_fail (supply->priv != NULL);

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

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

/**
 * dkp_supply_class_init:
 **/
static void
dkp_supply_class_init (DkpSupplyClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
	DkpDeviceClass *device_class = DKP_DEVICE_CLASS (klass);

	object_class->finalize = dkp_supply_finalize;
	device_class->get_on_battery = dkp_supply_get_on_battery;
	device_class->get_low_battery = dkp_supply_get_low_battery;
	device_class->coldplug = dkp_supply_coldplug;
	device_class->refresh = dkp_supply_refresh;

	g_type_class_add_private (klass, sizeof (DkpSupplyPrivate));
}

/**
 * dkp_supply_new:
 **/
DkpSupply *
dkp_supply_new (void)
{
	return g_object_new (DKP_TYPE_SUPPLY, NULL);
}