improved some debug messages
[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 <errno.h>
24 #include <fcntl.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28
29
30 #include <atasmart.h>
31
32 #include "pio.h"
33 #include "hdd.h"
34 #include "log.h"
35
36 static int filter_sd(const char *p)
37 {
38         return strlen(p) == 8 && !strncmp(p, "/dev/sd", 7);
39 }
40
41 static struct psensor *
42 create_sensor(char *id, char *name, SkDisk *disk, int values_max_length)
43 {
44         struct psensor *s;
45         s = psensor_create(id,
46                            strdup(name),
47                            SENSOR_TYPE_HDD_TEMP_ATASMART,
48                            values_max_length);
49
50         s->disk = disk;
51
52         return s;
53 }
54
55 static void analyze_disk(const char *dname)
56 {
57         int f;
58
59         log_debug("analyze_disk(hdd_atasmart): %s", dname);
60
61         f = open(dname, O_RDONLY|O_NOCTTY|O_NONBLOCK|O_CLOEXEC);
62
63         if (f != -1)
64                 close(f);
65         else
66                 log_debug("Could not open file %s: %s", dname, strerror(errno));
67 }
68
69
70 struct psensor **hdd_psensor_list_add(struct psensor **sensors,
71                                       int values_max_length)
72 {
73         char **paths, **tmp, *id;
74         SkDisk *disk;
75         struct psensor *sensor, **tmp_sensors, **result;
76
77         log_debug("hdd_psensor_list_add(hdd_atasmart)");
78
79         paths = dir_list("/dev", filter_sd);
80
81         result = sensors;
82         tmp = paths;
83         while (*tmp) {
84                 log_debug("hdd_psensor_list_add(hdd_atasmart) open %s", *tmp);
85
86                 if (!sk_disk_open(*tmp, &disk)) {
87                         id = malloc(strlen("hdd at") + strlen(*tmp) + 1);
88                         strcpy(id, "hdd at");
89                         strcat(id, *tmp);
90
91                         sensor = create_sensor(id,
92                                                *tmp,
93                                                disk,
94                                                values_max_length);
95
96                         tmp_sensors = psensor_list_add(result, sensor);
97
98                         if (result != sensors)
99                                 free(result);
100
101                         result = tmp_sensors;
102                 } else {
103                         log_err("sk_disk_open %s failure", *tmp);
104                         analyze_disk(*tmp);
105                 }
106
107                 tmp++;
108         }
109
110         paths_free(paths);
111
112         return result;
113 }
114
115 void hdd_psensor_list_update(struct psensor **sensors)
116 {
117         struct psensor **cur, *s;
118         uint64_t kelvin;
119         int ret;
120         double c;
121
122         cur = sensors;
123         while (*cur) {
124                 s = *cur;
125                 if (s->type == SENSOR_TYPE_HDD_TEMP_ATASMART) {
126                         ret = sk_disk_smart_read_data(s->disk);
127
128                         if (!ret) {
129                                 ret = sk_disk_smart_get_temperature(s->disk,
130                                                                     &kelvin);
131
132                                 if (!ret) {
133                                         c = (kelvin - 273150) / 1000;
134                                         psensor_set_current_value(s, c);
135                                         log_debug("hdd_psensor_list_update(hdd_atasmart): %s %.2f",
136                                                   s->id,
137                                                   c);
138                                 }
139                         }
140                 }
141
142                 cur++;
143         }
144 }