summaryrefslogtreecommitdiff
path: root/plugins/udisks/udisks-plugin.c
blob: 97630154a47230a02d20a5b7734b1bac7577c3fa (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 * vim: sts=0 sw=8 ts=8 tw=78 noexpandtab
 *
 * Copyright (C) 2009 Pramod Dematagoda <pmd.lotr.gandalf@gmail.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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */

#include <stdio.h>
#include <atasmart.h>
#include <glib.h>
#include <dbus/dbus-glib.h>
#include "udisks-plugin.h"

#define UDISKS_BUS_NAME              "org.freedesktop.UDisks"
#define UDISKS_DEVICE_INTERFACE_NAME "org.freedesktop.UDisks.Device"
#define UDISKS_INTERFACE_NAME        "org.freedesktop.UDisks"
#define UDISKS_PROPERTIES_INTERFACE  "org.freedesktop.DBus.Properties"
#define UDISKS_OBJECT_PATH           "/org/freedesktop/UDisks"


/*
 * Info about a single sensor
 */
typedef struct _DevInfo{
	gchar *path;
	gboolean changed;
	gdouble temp;
	DBusGProxy *sensor_proxy;
} DevInfo;

const gchar *plugin_name = "udisks";

GHashTable *devices = NULL;

/* This is a global variable for convenience */
DBusGConnection *connection;


/* This is the handler for the Changed() signal emitted by UDisks. */
static void udisks_changed_signal_cb(DBusGProxy *sensor_proxy) {
	const gchar *path = dbus_g_proxy_get_path(sensor_proxy);
	DevInfo *info;

	info = g_hash_table_lookup(devices, path);
	if (info)
	{
		info->changed = TRUE;
	}
}

static void udisks_plugin_get_sensors(GList **sensors) {
	DBusGProxy *proxy, *sensor_proxy;
	GError *error = NULL;
	GPtrArray *paths;
	guint i;
	DevInfo *info;

	g_type_init();

	/* This connection will be used for everything, including the obtaining
	 * of sensor data
	 */
	connection = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
	if (connection == NULL)
	{
		g_debug("Failed to open connection to DBUS: %s",
							error->message);
		g_error_free(error);
		return;
	}

	/* This is the proxy which is only used once during the enumeration of
	 * the device object paths
	 */
	proxy = dbus_g_proxy_new_for_name(connection,
					  UDISKS_BUS_NAME,
					  UDISKS_OBJECT_PATH,
					  UDISKS_INTERFACE_NAME);

	/* The object paths of the disks are enumerated and placed in an array
	 * of object paths
	 */
	if (!dbus_g_proxy_call(proxy, "EnumerateDevices", &error,
			       G_TYPE_INVALID,
			       dbus_g_type_get_collection("GPtrArray",
							  DBUS_TYPE_G_OBJECT_PATH),
			       &paths,
			       G_TYPE_INVALID))
	{
		g_debug("Failed to enumerate disk devices: %s",
			   error->message);
		g_error_free(error);
		g_object_unref(proxy);
		dbus_g_connection_unref (connection);
		return;
	}

	for (i = 0; i < paths->len; i++) {
		/* This proxy is used to get the required data in order to build
		 * up the list of sensors
		 */
		GValue smart_available = G_VALUE_INIT;
		gchar *path = (gchar *)g_ptr_array_index(paths, i);

		sensor_proxy = dbus_g_proxy_new_for_name(connection,
							 UDISKS_BUS_NAME,
							 path,
							 UDISKS_PROPERTIES_INTERFACE);

		if (dbus_g_proxy_call(sensor_proxy, "Get", &error,
				      G_TYPE_STRING,
				      UDISKS_BUS_NAME,
				      G_TYPE_STRING,
				      "DriveAtaSmartIsAvailable",
				      G_TYPE_INVALID,
				      G_TYPE_VALUE, &smart_available, G_TYPE_INVALID)) {
			if (!g_value_get_boolean(&smart_available)) {
				g_debug("Drive at path '%s' does not support "
                                        "Smart monitoring... ignoring",
					path);
				g_object_unref(sensor_proxy);
				g_free (path);
				continue;
			}

			GValue model_v = G_VALUE_INIT;
			dbus_g_proxy_call(sensor_proxy, "Get", NULL,
					  G_TYPE_STRING, UDISKS_BUS_NAME,
					  G_TYPE_STRING, "DriveModel",
					  G_TYPE_INVALID,
					  G_TYPE_VALUE, &model_v,
					  G_TYPE_INVALID);

			GValue dev_v = G_VALUE_INIT;
			dbus_g_proxy_call(sensor_proxy, "Get", NULL,
					  G_TYPE_STRING, UDISKS_BUS_NAME,
					  G_TYPE_STRING, "DeviceFile",
					  G_TYPE_INVALID,
					  G_TYPE_VALUE, &dev_v,
					  G_TYPE_INVALID);

			GValue ids_v = G_VALUE_INIT;
			dbus_g_proxy_call(sensor_proxy, "Get", NULL,
					  G_TYPE_STRING, UDISKS_BUS_NAME,
					  G_TYPE_STRING, "DeviceFileById",
					  G_TYPE_INVALID,
					  G_TYPE_VALUE, &ids_v,
					  G_TYPE_INVALID);

			g_object_unref(sensor_proxy);

			sensor_proxy = dbus_g_proxy_new_for_name(connection,
								 UDISKS_BUS_NAME,
								 path,
								 UDISKS_DEVICE_INTERFACE_NAME);

			/* Use the Changed() signal emitted from UDisks to
			 * get the temperature without always polling
			 */
			dbus_g_proxy_add_signal(sensor_proxy, "Changed",
						G_TYPE_INVALID);

			dbus_g_proxy_connect_signal(sensor_proxy, "Changed",
						    G_CALLBACK(udisks_changed_signal_cb),
						    path, NULL);

			info = g_malloc(sizeof(DevInfo));
			if (devices == NULL)
			{
				devices = g_hash_table_new(g_str_hash,
							   g_str_equal);
			}
			info->path = g_strdup(path);
			info->sensor_proxy = sensor_proxy;
			/* Set the device status changed as TRUE because we need
			 * to get the initial temperature reading
			 */
			info->changed = TRUE;
			g_hash_table_insert(devices, info->path, info);

			gchar *model = g_value_get_string(&model_v);
			gchar *dev = g_value_get_string(&dev_v);
			GStrv ids = g_value_get_boxed(&ids_v);

			gchar *id = ids != NULL && ids[0] != NULL ? ids[0] : dev;

			/* Write the sensor data */
			sensors_applet_plugin_add_sensor(sensors,
							 path,
							 id,
							 model,
							 TEMP_SENSOR,
							 FALSE,
							 HDD_ICON,
							 DEFAULT_GRAPH_COLOR);
			g_strfreev(ids);
			g_free(model);
			g_free(dev);
			g_debug("Added %s %s", path, id);
		} else {
			g_debug ("Cannot obtain data for device: %s\n"
				 "Error: %s\n",
				 path,
				 error->message);
			g_error_free (error);
			error = NULL;
			g_object_unref(sensor_proxy);
		}
		g_free(path);
	}
	g_ptr_array_free(paths, TRUE);
	g_object_unref(proxy);
	if (devices == NULL)
		dbus_g_connection_unref (connection);
}

static gdouble udisks_plugin_get_sensor_value(const gchar *path,
					      const gchar *id,
					      SensorType type,
					      GError **error) {
	GValue smart_blob_val = { 0, };
	GArray *smart_blob;
	gdouble temperature;
	guint64 temperature_placer;
	DBusGProxy *sensor_proxy;
	guint count;
	DevInfo *info;
	SkDisk *sk_disk;

	info = (DevInfo *)g_hash_table_lookup(devices, path);
	if (info == NULL)
	{
		g_set_error(error, SENSORS_APPLET_PLUGIN_ERROR, 0,
			    "Error finding disk with path %s", path);
		return 0.0;
	}

	/* If the device has changed, we find the new temperature and return
	 * it
	 */
	if (info->changed)
	{
		GValue smart_time = { 0, };
		sensor_proxy = dbus_g_proxy_new_for_name(connection,
							 UDISKS_BUS_NAME,
							 path,
							 UDISKS_PROPERTIES_INTERFACE);
		if (!dbus_g_proxy_call(sensor_proxy, "Get", error,
				       G_TYPE_STRING, UDISKS_BUS_NAME,
				       G_TYPE_STRING, "DriveAtaSmartTimeCollected", G_TYPE_INVALID,
				       G_TYPE_VALUE, &smart_time,
				       G_TYPE_INVALID) ||
		    !g_value_get_uint64(&smart_time))
		{
			g_debug("Smart data has not been collected yet... returning 0.0 temp for now to avoid waking drive up unnecessarily");
			g_object_unref(sensor_proxy);
			return 0.0;
		}

		if (dbus_g_proxy_call(sensor_proxy, "Get", error,
				      G_TYPE_STRING, UDISKS_BUS_NAME,
				      G_TYPE_STRING, "DriveAtaSmartBlob", G_TYPE_INVALID,
				      G_TYPE_VALUE, &smart_blob_val,
				      G_TYPE_INVALID))
		{
			smart_blob = g_value_get_boxed(&smart_blob_val);

			sk_disk_open(NULL, &sk_disk);
			sk_disk_set_blob (sk_disk, smart_blob->data, smart_blob->len);
			/* Note: A gdouble cannot be passed in through a cast as it is likely that the
			 * temperature is placed in it purely through memory functions, hence a guint64
			 * is passed and the number is then placed in a gdouble manually 
			 */
			sk_disk_smart_get_temperature (sk_disk, &temperature_placer);
			temperature = temperature_placer;

			/* Temperature is in mK, so convert it to K first */
			temperature /= 1000;
			info->temp = temperature - 273.15;
			info->changed = FALSE;

			g_free (sk_disk);
			g_array_free(smart_blob, TRUE);
		}
		g_object_unref(sensor_proxy);
	}
	return info->temp;
}

static GList *udisks_plugin_init(void) {
	GList *sensors = NULL;

	udisks_plugin_get_sensors(&sensors);

	return sensors;
}

const gchar *sensors_applet_plugin_name(void)
{
	return plugin_name;
}

GList *sensors_applet_plugin_init(void)
{
	return udisks_plugin_init();
}

gdouble sensors_applet_plugin_get_sensor_value(const gchar *path,
					       const gchar *id,
					       SensorType type,
					       GError **error) {
	return udisks_plugin_get_sensor_value(path, id, type, error);
}