avoid to update SMART more than each 30s (costly operation for the udisks2 daemon)
[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 <stdlib.h>
24 #include <string.h>
25 #include <sys/time.h>
26
27 #include <udisks/udisks.h>
28
29 #include <pudisks2.h>
30 #include <temperature.h>
31
32 const char *PROVIDER_NAME = "udisks2";
33
34 static GDBusObjectManager *manager;
35
36 const time_t SMART_UPDATE_INTERVAL = 30;
37
38 struct udisks_data {
39         char *path;
40         struct timeval last_smart_update;
41 };
42
43 void udisks_data_free(void *data)
44 {
45         struct udisks_data *u;
46
47         u = (struct udisks_data *)data;
48         free(u->path);
49         free(u);
50 }
51
52 static void smart_update(struct psensor *s, UDisksDriveAta *ata)
53 {
54         GVariant *variant;
55         gboolean ret;
56         struct timeval t;
57         struct udisks_data *data;
58
59         data = s->provider_data;
60
61         if (gettimeofday(&t, NULL) != 0) {
62                 log_err("%s: %s", PROVIDER_NAME, _("gettimeofday failed."));
63                 return;
64         }
65
66         if (data->last_smart_update.tv_sec
67             &&
68             (t.tv_sec - data->last_smart_update.tv_sec < SMART_UPDATE_INTERVAL))
69                 return;
70
71         log_fct("%s: update SMART data for %s", PROVIDER_NAME, data->path);
72
73         variant = g_variant_new_parsed("{'nowakeup': %v}",
74                                        g_variant_new_boolean(TRUE));
75
76         ret = udisks_drive_ata_call_smart_update_sync(ata,
77                                                       variant,
78                                                       NULL,
79                                                       NULL);
80
81         if (!ret)
82                 log_fct("%s: SMART update failed for %s",
83                         PROVIDER_NAME,
84                         data->path);
85
86                 data->last_smart_update = t;
87 }
88
89 void udisks2_psensor_list_update(struct psensor **sensors)
90 {
91         struct psensor *s;
92         GDBusObject *o;
93         UDisksDriveAta *drive_ata;
94         double v;
95         struct udisks_data *data;
96
97         for (; *sensors; sensors++) {
98                 s = *sensors;
99
100                 if (s->type & SENSOR_TYPE_UDISKS2) {
101                         data = (struct udisks_data *)s->provider_data;
102
103                         o = g_dbus_object_manager_get_object(manager,
104                                                              data->path);
105
106                         if (!o)
107                                 continue;
108
109                         g_object_get(o, "drive-ata", &drive_ata, NULL);
110
111                         smart_update(s, drive_ata);
112
113                         v = udisks_drive_ata_get_smart_temperature(drive_ata);
114
115                         psensor_set_current_value(s, kelvin_to_celsius(v));
116
117                         g_object_unref(G_OBJECT(o));
118                 }
119         }
120 }
121
122 void udisks2_psensor_list_add(struct psensor ***sensors, int values_length)
123 {
124         UDisksClient *client;
125         GList *objects, *cur;
126         UDisksDrive *drive;
127         UDisksDriveAta *drive_ata;
128         int i, type;
129         char *id, *name, *chip;
130         const char *path, *drive_id, *drive_model;
131         struct psensor *s;
132         struct udisks_data *data;
133
134         log_fct_enter();
135
136         client = udisks_client_new_sync(NULL, NULL);
137
138         if (!client) {
139                 log_err(_("%s: cannot get the udisks2 client"), PROVIDER_NAME);
140                 log_fct_exit();
141                 return;
142         }
143
144         manager = udisks_client_get_object_manager(client);
145
146         objects = g_dbus_object_manager_get_objects(manager);
147
148         i = 0;
149         for (cur = objects; cur; cur = cur->next) {
150                 path = g_dbus_object_get_object_path(cur->data);
151
152                 g_object_get(cur->data,
153                              "drive", &drive,
154                              "drive-ata", &drive_ata,
155                              NULL);
156
157                 if (!drive) {
158                         log_fct("Not a drive: %s", path);
159                         continue;
160                 }
161
162                 if (!drive_ata) {
163                         log_fct("Not an ATA drive: %s", path);
164                         continue;
165                 }
166
167                 if (!udisks_drive_ata_get_smart_enabled(drive_ata)) {
168                         log_fct("SMART not enabled: %s", path);
169                         continue;
170                 }
171
172                 if (!udisks_drive_ata_get_smart_temperature(drive_ata)) {
173                         log_fct("No temperature available: %s", path);
174                         continue;
175                 }
176
177                 drive_id = udisks_drive_get_id(drive);
178                 if (drive_id) {
179                         id = g_strdup_printf("%s %s", PROVIDER_NAME, drive_id);
180                 } else {
181                         id = g_strdup_printf("%s %d", PROVIDER_NAME, i);
182                         i++;
183                 }
184
185                 drive_model = udisks_drive_get_model(drive);
186                 if (drive_model) {
187                         name = strdup(drive_model);
188                         chip = strdup(drive_model);
189                 } else {
190                         name = strdup(_("Disk"));
191                         chip = strdup(_("Disk"));
192                 }
193
194                 type = SENSOR_TYPE_TEMP | SENSOR_TYPE_UDISKS2 | SENSOR_TYPE_HDD;
195
196                 s = psensor_create(id, name, chip, type, values_length);
197
198                 data = malloc(sizeof(struct udisks_data));
199                 data->path = strdup(path);
200                 memset(&data->last_smart_update, 0, sizeof(struct timeval));
201
202                 s->provider_data = data;
203                 s->provider_data_free_fct = &udisks_data_free;
204
205                 psensor_list_append(sensors, s);
206
207                 g_object_unref(G_OBJECT(cur->data));
208         }
209
210         g_list_free(objects);
211
212         log_fct_exit();
213 }