added udisks2 support
[psensor.git] / src / lib / pudisks2.c
1 /*
2  * Copyright (C) 2010-2014 jeanfi@gmail.com
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301 USA
18  */
19 #include <locale.h>
20 #include <libintl.h>
21 #define _(str) gettext(str)
22
23 #include <string.h>
24
25 #include <udisks/udisks.h>
26
27 #include <pudisks2.h>
28 #include <temperature.h>
29
30 const char *PROVIDER_NAME = "udisks2";
31
32 static GDBusObjectManager *manager;
33
34 static void udisks2_err(const char *msg)
35 {
36         log_err("%s: %s", PROVIDER_NAME, msg);
37 }
38
39 void udisks2_psensor_list_update(struct psensor **sensors)
40 {
41         struct psensor *s;
42         GDBusObject *o;
43         UDisksDriveAta *drive_ata;
44         double v;
45
46         while (*sensors) {
47                 s = *sensors;
48
49                 if (s->type & SENSOR_TYPE_UDISKS2) {
50                         o = g_dbus_object_manager_get_object(manager,
51                                                              s->udisks2_path);
52
53                         if (!o)
54                                 continue;
55
56                         g_object_get(o, "drive-ata", &drive_ata, NULL);
57
58                         v = udisks_drive_ata_get_smart_temperature(drive_ata);
59
60                         psensor_set_current_value(s, kelvin_to_celsius(v));
61
62                         g_object_unref(G_OBJECT(o));
63                 }
64
65                 sensors++;
66         }
67 }
68
69 void udisks2_psensor_list_add(struct psensor ***sensors, int values_length)
70 {
71         UDisksClient *client;
72         GList *objects, *cur;
73         UDisksDrive *drive;
74         UDisksDriveAta *drive_ata;
75         int i, type;
76         char *id, *name, *chip;
77         const char *path, *drive_id, *drive_model;
78         struct psensor *s;
79
80         log_fct_enter();
81
82         client = udisks_client_new_sync(NULL, NULL);
83
84         if (!client) {
85                 udisks2_err(_("Cannot get the udisks2 client"));
86                 log_fct_exit();
87                 return;
88         }
89
90         manager = udisks_client_get_object_manager(client);
91
92         objects = g_dbus_object_manager_get_objects(manager);
93
94         i = 0;
95         for (cur = objects; cur; cur = cur->next) {
96                 path = g_dbus_object_get_object_path(cur->data);
97
98                 g_object_get(cur->data,
99                              "drive", &drive,
100                              "drive-ata", &drive_ata,
101                              NULL);
102
103                 if (!drive) {
104                         log_fct("Not a drive: %s", path);
105                         continue;
106                 }
107
108                 if (!drive_ata) {
109                         log_fct("Not an ATA drive: %s", path);
110                         continue;
111                 }
112
113                 if (!udisks_drive_ata_get_smart_enabled(drive_ata)) {
114                         log_fct("SMART not enabled: %s", path);
115                         continue;
116                 }
117
118                 if (!udisks_drive_ata_get_smart_temperature(drive_ata)) {
119                         log_fct("No temperature available: %s", path);
120                         continue;
121                 }
122
123                 drive_id = udisks_drive_get_id(drive);
124                 if (drive_id) {
125                         id = g_strdup_printf("%s %s", PROVIDER_NAME, drive_id);
126                 } else {
127                         id = g_strdup_printf("%s %d", PROVIDER_NAME, i);
128                         i++;
129                 }
130
131                 drive_model = udisks_drive_get_model(drive);
132                 if (drive_model) {
133                         name = strdup(drive_model);
134                         chip = strdup(drive_model);
135                 } else {
136                         name = strdup(_("Disk"));
137                         chip = strdup(_("Disk"));
138                 }
139
140                 type = SENSOR_TYPE_TEMP | SENSOR_TYPE_UDISKS2 | SENSOR_TYPE_HDD;
141
142                 s = psensor_create(id, name, chip, type, values_length);
143                 s->udisks2_path = strdup(path);
144
145                 psensor_list_append(sensors, s);
146
147                 g_object_unref(G_OBJECT(cur->data));
148         }
149
150         g_list_free(objects);
151
152         log_fct_exit();
153 }