summaryrefslogtreecommitdiff
path: root/tools/dkp-tool.c
blob: cf1960cd6868ecbdd43100c052f444d0deef466e (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2007 David Zeuthen <david@fubar.dk>
 * 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 <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pwd.h>
#include <grp.h>

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

#include <dbus/dbus-glib.h>
#include <dbus/dbus-glib-lowlevel.h>

#include <polkit-dbus/polkit-dbus.h>

#include "dkp-daemon-glue.h"
#include "dkp-marshal.h"
#include "dkp-debug.h"
#include "dkp-object.h"

static DBusGConnection *bus = NULL;
static DBusGProxy *power_proxy = NULL;
static GMainLoop *loop;

static gboolean opt_enumerate = FALSE;
static gboolean opt_monitor = FALSE;
static gboolean opt_monitor_detail = FALSE;
static gchar *opt_show_info = FALSE;

static gboolean dkp_tool_do_monitor (void);
static void dkp_tool_show_device_info (const gchar *object_path);

/**
 * dkp_tool_device_added_cb:
 **/
static void
dkp_tool_device_added_cb (DBusGProxy *proxy, const gchar *object_path, gpointer user_data)
{
	g_print ("added:     %s\n", object_path);
	if (opt_monitor_detail) {
		dkp_tool_show_device_info (object_path);
		g_print ("\n");
	}
}

/**
 * dkp_tool_device_changed_cb:
 **/
static void
dkp_tool_device_changed_cb (DBusGProxy *proxy, const gchar *object_path, gpointer user_data)
{
	g_print ("changed:     %s\n", object_path);
	if (opt_monitor_detail) {
		/* TODO: would be nice to just show the diff */
		dkp_tool_show_device_info (object_path);
		g_print ("\n");
	}
}

/**
 * dkp_tool_device_removed_cb:
 **/
static void
dkp_tool_device_removed_cb (DBusGProxy *proxy, const gchar *object_path, gpointer user_data)
{
	g_print ("removed:   %s\n", object_path);
}

/**
 * dkp_tool_get_device_properties:
 **/
static GHashTable *
dkp_tool_get_device_properties (DBusGConnection *bus, const char *object_path)
{
	GError *error;
	GHashTable *hash_table = NULL;
	DBusGProxy *proxy;
	const char *ifname = "org.freedesktop.DeviceKit.Power.Device";

	proxy = dbus_g_proxy_new_for_name (bus, "org.freedesktop.DeviceKit.Power",
						object_path, "org.freedesktop.DBus.Properties");
	error = NULL;
	if (!dbus_g_proxy_call (proxy, "GetAll", &error,
				G_TYPE_STRING, ifname,
				G_TYPE_INVALID,
				dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE),
				&hash_table,
				G_TYPE_INVALID)) {
		dkp_debug ("Couldn't call GetAll() to get properties for %s: %s", object_path, error->message);
		g_error_free (error);
		goto out;
	}

out:
	g_object_unref (proxy);
	return hash_table;
}

/**
 * dkp_tool_do_monitor:
 **/
static gboolean
dkp_tool_do_monitor (void)
{
	g_print ("Monitoring activity from the power daemon. Press Ctrl+C to cancel.\n");

	dbus_g_proxy_connect_signal (power_proxy, "DeviceAdded",
				     G_CALLBACK (dkp_tool_device_added_cb), NULL, NULL);
	dbus_g_proxy_connect_signal (power_proxy, "DeviceRemoved",
				     G_CALLBACK (dkp_tool_device_removed_cb), NULL, NULL);
	dbus_g_proxy_connect_signal (power_proxy, "DeviceChanged",
				     G_CALLBACK (dkp_tool_device_changed_cb), NULL, NULL);
	g_main_loop_run (loop);

	return FALSE;
}

/**
 * dkp_tool_get_device_stats:
 **/
static gboolean
dkp_tool_get_device_stats (DBusGConnection *bus, const char *object_path, const gchar *type, guint timespec)
{
	GError *error = NULL;
	DBusGProxy *proxy;
	GType g_type_gvalue_array;
	GPtrArray *gvalue_ptr_array = NULL;
	GValueArray *gva;
	GValue *gv;
	guint i;

	proxy = dbus_g_proxy_new_for_name (bus, "org.freedesktop.DeviceKit.Power",
						object_path, "org.freedesktop.DeviceKit.Power.Source");

	g_type_gvalue_array = dbus_g_type_get_collection ("GPtrArray",
					dbus_g_type_get_struct("GValueArray",
						G_TYPE_UINT,
						G_TYPE_DOUBLE,
						G_TYPE_STRING,
						G_TYPE_INVALID));

	error = NULL;
	if (!dbus_g_proxy_call (proxy, "GetStatistics", &error,
				G_TYPE_STRING, type,
				G_TYPE_UINT, timespec,
				G_TYPE_INVALID,
				g_type_gvalue_array, &gvalue_ptr_array,
				G_TYPE_INVALID)) {
		dkp_debug ("GetStatistics(%s,%i) on %s failed: %s", type, timespec, object_path, error->message);
		g_error_free (error);
		goto out;
	}

	/* no data */
	if (gvalue_ptr_array->len == 0)
		goto out;

	guint timeval;
	gdouble value;
	const gchar *state;

	g_print ("  statistics (%s)\n", type);
	for (i=0; i< gvalue_ptr_array->len; i++) {
		gva = (GValueArray *) g_ptr_array_index (gvalue_ptr_array, i);
		/* 0 */
		gv = g_value_array_get_nth (gva, 0);
		timeval = g_value_get_uint (gv);
		g_value_unset (gv);
		/* 1 */
		gv = g_value_array_get_nth (gva, 1);
		value = g_value_get_double (gv);
		g_value_unset (gv);
		/* 2 */
		gv = g_value_array_get_nth (gva, 2);
		state = g_value_get_string (gv);
		g_print ("    %lu seconds\t%.2lf (%s)\n", time (NULL) - timeval, value, state);
		g_value_unset (gv);
		g_value_array_free (gva);
	}

out:
	if (gvalue_ptr_array != NULL)
		g_ptr_array_free (gvalue_ptr_array, TRUE);
	g_object_unref (proxy);
	return TRUE;
}

/**
 * dkp_tool_show_device_info:
 **/
static void
dkp_tool_show_device_info (const gchar *object_path)
{
	GHashTable *hash;
	DkpObject *obj;

	/* get all the properties */
	hash = dkp_tool_get_device_properties (bus, object_path);
	if (hash == NULL) {
		g_print ("Cannot get device properties for %s\n", object_path);
		return;
	}

	/* create an object and copy properties */
	obj = dkp_object_new ();
	dkp_object_set_from_map (obj, hash);

	/* print to screen */
	dkp_object_print (obj);

	/* if we can, get stats */
	dkp_tool_get_device_stats (bus, object_path, "charge", 120);
	dkp_tool_get_device_stats (bus, object_path, "rate", 120);

	/* tidy up */
	dkp_object_free (obj);
	g_hash_table_unref (hash);
}

/**
 * main:
 **/
int
main (int argc, char **argv)
{
	int ret = 1;
	GOptionContext *context;
	GError *error = NULL;
	gboolean verbose = FALSE;
	gboolean opt_dump = FALSE;
	unsigned int n;

	const GOptionEntry entries[] = {
		{ "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, _("Show extra debugging information"), NULL },
		{ "enumerate", 0, 0, G_OPTION_ARG_NONE, &opt_enumerate, _("Enumerate objects paths for devices"), NULL },
		{ "dump", 0, 0, G_OPTION_ARG_NONE, &opt_dump, _("Dump all parameters for all objects"), NULL },
		{ "monitor", 0, 0, G_OPTION_ARG_NONE, &opt_monitor, _("Monitor activity from the power daemon"), NULL },
		{ "monitor-detail", 0, 0, G_OPTION_ARG_NONE, &opt_monitor_detail, _("Monitor with detail"), NULL },
		{ "show-info", 0, 0, G_OPTION_ARG_STRING, &opt_show_info, _("Show information about object path"), NULL },
		{ NULL }
	};

	g_type_init ();

	context = g_option_context_new ("DeviceKit-power tool");
	g_option_context_add_main_entries (context, entries, NULL);
	g_option_context_parse (context, &argc, &argv, NULL);
	g_option_context_free (context);
	dkp_debug_init (verbose);

	loop = g_main_loop_new (NULL, FALSE);

	bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
	if (bus == NULL) {
		dkp_warning ("Couldn't connect to system bus: %s", error->message);
		g_error_free (error);
		goto out;
	}

	power_proxy = dbus_g_proxy_new_for_name (bus, "org.freedesktop.DeviceKit.Power",
						 "/", "org.freedesktop.DeviceKit.Power");
	dbus_g_proxy_add_signal (power_proxy, "DeviceAdded", G_TYPE_STRING, G_TYPE_INVALID);
	dbus_g_proxy_add_signal (power_proxy, "DeviceRemoved", G_TYPE_STRING, G_TYPE_INVALID);
	dbus_g_proxy_add_signal (power_proxy, "DeviceChanged", G_TYPE_STRING, G_TYPE_INVALID);

	if (opt_dump) {
		dkp_warning ("dump not supported");
	} else if (opt_enumerate) {
		GPtrArray *devices;
		if (!org_freedesktop_DeviceKit_Power_enumerate_devices (power_proxy, &devices, &error)) {
			dkp_warning ("Couldn't enumerate devices: %s", error->message);
			g_error_free (error);
			goto out;
		}
		for (n = 0; n < devices->len; n++) {
			gchar *object_path = devices->pdata[n];
			g_print ("%s\n", object_path);
		}
		g_ptr_array_foreach (devices, (GFunc) g_free, NULL);
		g_ptr_array_free (devices, TRUE);
	} else if (opt_monitor || opt_monitor_detail) {
		if (!dkp_tool_do_monitor ())
			goto out;
	} else if (opt_show_info != NULL) {
		dkp_tool_show_device_info (opt_show_info);
	}

	ret = 0;

out:
	if (power_proxy != NULL)
		g_object_unref (power_proxy);
	if (bus != NULL)
		dbus_g_connection_unref (bus);

	return ret;
}