2012 copyright
[psensor.git] / src / lib / hdd_atasmart.c
1 /*
2  * Copyright (C) 2010-2012 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
26 #include <atasmart.h>
27
28 #include "pio.h"
29 #include "hdd.h"
30 #include "log.h"
31
32 static int filter_sd(const char *p)
33 {
34         return strlen(p) == 8 && !strncmp(p, "/dev/sd", 7);
35 }
36
37 static struct psensor *
38 create_sensor(char *id, char *name, SkDisk *disk, int values_max_length)
39 {
40         struct psensor *s;
41         s = psensor_create(id,
42                            strdup(name),
43                            SENSOR_TYPE_HDD_TEMP_ATASMART,
44                            values_max_length);
45
46         s->disk = disk;
47
48         return s;
49 }
50
51 struct psensor **hdd_psensor_list_add(struct psensor **sensors,
52                                       int values_max_length)
53 {
54         char **paths, **tmp, *id;
55         SkDisk *disk;
56         struct psensor *sensor, **tmp_sensors, **result;
57
58         log_debug("hdd_psensor_list_add");
59
60         paths = dir_list("/dev", filter_sd);
61
62         result = sensors;
63         tmp = paths;
64         while (*tmp) {
65                 log_debug("hdd_psensor_list_add open %s", *tmp);
66
67                 if (!sk_disk_open(*tmp, &disk)) {
68                         id = malloc(strlen("hdd at") + strlen(*tmp) + 1);
69                         strcpy(id, "hdd at");
70                         strcat(id, *tmp);
71
72                         sensor = create_sensor(id,
73                                                *tmp,
74                                                disk,
75                                                values_max_length);
76
77                         tmp_sensors = psensor_list_add(result, sensor);
78
79                         if (result != sensors)
80                                 free(result);
81
82                         result = tmp_sensors;
83                 } else {
84                         log_err("Failed to open %s", *tmp);
85                 }
86
87                 tmp++;
88         }
89
90         paths_free(paths);
91
92         return result;
93 }
94
95 void hdd_psensor_list_update(struct psensor **sensors)
96 {
97         struct psensor **cur, *s;
98         uint64_t kelvin;
99         int ret;
100         double c;
101
102         cur = sensors;
103         while (*cur) {
104                 s = *cur;
105                 if (s->type == SENSOR_TYPE_HDD_TEMP_ATASMART) {
106                         ret = sk_disk_smart_read_data(s->disk);
107
108                         if (!ret) {
109                                 ret = sk_disk_smart_get_temperature(s->disk,
110                                                                     &kelvin);
111
112                                 if (!ret) {
113                                         c = (kelvin - 273150) / 1000;
114                                         psensor_set_current_value(s, c);
115                                         log_debug("hdd atasmart: %s %.2f",
116                                                   s->id,
117                                                   c);
118                                 }
119                         }
120                 }
121
122                 cur++;
123         }
124 }