force smart update (if disk is not in standby)
[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 void udisks2_psensor_list_update(struct psensor **sensors)
35 {
36         struct psensor *s;
37         GDBusObject *o;
38         UDisksDriveAta *drive_ata;
39         double v;
40         GVariant *variant;
41
42         for (; *sensors; sensors++) {
43                 s = *sensors;
44
45                 if (s->type & SENSOR_TYPE_UDISKS2) {
46                         o = g_dbus_object_manager_get_object(manager,
47                                                              s->udisks2_path);
48
49                         if (!o)
50                                 continue;
51
52                         g_object_get(o, "drive-ata", &drive_ata, NULL);
53
54                         variant = g_variant_new_parsed
55                                 ("{'nowakeup': %v}",
56                                  g_variant_new_boolean(TRUE));
57
58                         udisks_drive_ata_call_smart_update_sync(drive_ata,
59                                                                 variant,
60                                                                 NULL,
61                                                                 NULL);
62
63                         v = udisks_drive_ata_get_smart_temperature(drive_ata);
64
65                         psensor_set_current_value(s, kelvin_to_celsius(v));
66
67                         g_object_unref(G_OBJECT(o));
68                 }
69         }
70 }
71
72 void udisks2_psensor_list_add(struct psensor ***sensors, int values_length)
73 {
74         UDisksClient *client;
75         GList *objects, *cur;
76         UDisksDrive *drive;
77         UDisksDriveAta *drive_ata;
78         int i, type;
79         char *id, *name, *chip;
80         const char *path, *drive_id, *drive_model;
81         struct psensor *s;
82
83         log_fct_enter();
84
85         client = udisks_client_new_sync(NULL, NULL);
86
87         if (!client) {
88                 log_err(_("%s: cannot get the udisks2 client"), PROVIDER_NAME);
89                 log_fct_exit();
90                 return;
91         }
92
93         manager = udisks_client_get_object_manager(client);
94
95         objects = g_dbus_object_manager_get_objects(manager);
96
97         i = 0;
98         for (cur = objects; cur; cur = cur->next) {
99                 path = g_dbus_object_get_object_path(cur->data);
100
101                 g_object_get(cur->data,
102                              "drive", &drive,
103                              "drive-ata", &drive_ata,
104                              NULL);
105
106                 if (!drive) {
107                         log_fct("Not a drive: %s", path);
108                         continue;
109                 }
110
111                 if (!drive_ata) {
112                         log_fct("Not an ATA drive: %s", path);
113                         continue;
114                 }
115
116                 if (!udisks_drive_ata_get_smart_enabled(drive_ata)) {
117                         log_fct("SMART not enabled: %s", path);
118                         continue;
119                 }
120
121                 if (!udisks_drive_ata_get_smart_temperature(drive_ata)) {
122                         log_fct("No temperature available: %s", path);
123                         continue;
124                 }
125
126                 drive_id = udisks_drive_get_id(drive);
127                 if (drive_id) {
128                         id = g_strdup_printf("%s %s", PROVIDER_NAME, drive_id);
129                 } else {
130                         id = g_strdup_printf("%s %d", PROVIDER_NAME, i);
131                         i++;
132                 }
133
134                 drive_model = udisks_drive_get_model(drive);
135                 if (drive_model) {
136                         name = strdup(drive_model);
137                         chip = strdup(drive_model);
138                 } else {
139                         name = strdup(_("Disk"));
140                         chip = strdup(_("Disk"));
141                 }
142
143                 type = SENSOR_TYPE_TEMP | SENSOR_TYPE_UDISKS2 | SENSOR_TYPE_HDD;
144
145                 s = psensor_create(id, name, chip, type, values_length);
146                 s->udisks2_path = strdup(path);
147
148                 psensor_list_append(sensors, s);
149
150                 g_object_unref(G_OBJECT(cur->data));
151         }
152
153         g_list_free(objects);
154
155         log_fct_exit();
156 }