forked from martinpitt/umockdev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
battery.c
107 lines (93 loc) · 3.42 KB
/
battery.c
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
/* umockdev example: use libumockdev in C to fake a battery
* Build with:
* gcc battery.c -Wall `pkg-config --cflags --libs umockdev-1.0 gio-2.0` -o /tmp/battery
* Run with:
* umockdev-wrapper /tmp/battery
*
* Copyright (C) 2013 Canonical Ltd.
* Author: Martin Pitt <[email protected]>
*
* umockdev is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* umockdev 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; If not, see <http://www.gnu.org/licenses/>.
*/
#include <unistd.h>
#include <stdio.h>
#include <sys/wait.h>
#include <glib.h>
#include <gio/gio.h>
#include "umockdev.h"
/* determine upowerd path */
static const char*
upowerd_path(void)
{
static char path[PATH_MAX];
gchar *contents, *exec;
g_assert (g_file_get_contents ("/usr/share/dbus-1/system-services/org.freedesktop.UPower.service",
&contents, NULL, NULL));
exec = strstr (contents, "Exec=");
g_assert (exec != NULL);
*strchr(exec, '\n') = '\0';
strcpy (path, exec + 5);
g_free (contents);
return path;
}
int main()
{
UMockdevTestbed *testbed;
const char* sys_bat;
GTestDBus* dbus;
GPid upowerd;
const char* upowerd_argv[] = {upowerd_path(), NULL};
/* create test bed */
testbed = umockdev_testbed_new ();
/* add a battery with good charge */
sys_bat = umockdev_testbed_add_device (testbed, "power_supply", "fakeBAT0", NULL,
/* attributes */
"type", "Battery",
"present", "1",
"status", "Discharging",
"energy_full", "60000000",
"energy_full_design", "80000000",
"energy_now", "48000000",
"voltage_now", "12000000",
NULL,
/* properties */
"POWER_SUPPLY_ONLINE", "1",
NULL);
/* start a fake system D-BUS */
dbus = g_test_dbus_new (G_TEST_DBUS_NONE);
g_test_dbus_up (dbus);
g_setenv ("DBUS_SYSTEM_BUS_ADDRESS", g_test_dbus_get_bus_address (dbus), TRUE);
puts("-- starting upower on test dbus under umockdev-wrapper");
g_assert (g_spawn_async_with_pipes (NULL, (gchar**) upowerd_argv, NULL,
G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL, NULL,
NULL, &upowerd, NULL, NULL, NULL, NULL));
/* give it some time to settle */
sleep(1);
puts("-- Initial upower --dump");
g_assert (g_spawn_command_line_sync ("upower --dump", NULL, NULL, NULL, NULL));
puts("-- Starting upower monitoring now");
g_assert (g_spawn_command_line_async ("upower --monitor-detail", NULL));
sleep(1);
puts("-- setting battery charge to 2.5\% now");
umockdev_testbed_set_attribute (testbed, sys_bat, "energy_now", "1500000");
/* send uevent to notify upowerd */
umockdev_testbed_uevent (testbed, sys_bat, "change");
sleep(1);
/* clean up */
puts("-- cleaning up");
kill (upowerd, SIGTERM);
waitpid (upowerd, NULL, 0);
g_test_dbus_down (dbus);
return 0;
}