Imported Upstream version 1.2.0
[psensor-pkg-debian.git] / src / lib / hdd_hddtemp.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
20 /* Part of the code in this file is based on GNOME sensors applet code
21  * hddtemp-plugin.c see http://sensors-applet.sourceforge.net/
22  */
23
24 #include <locale.h>
25 #include <libintl.h>
26 #define _(str) gettext(str)
27
28 #include <stdlib.h>
29 #include <string.h>
30 #include <stdio.h>
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
35 #include <unistd.h>
36
37 #include <hdd.h>
38 #include <psensor.h>
39
40 static const char *PROVIDER_NAME = "hddtemp";
41
42 static const char *HDDTEMP_SERVER_IP_ADDRESS = "127.0.0.1";
43 static const int HDDTEMP_PORT_NUMBER = 7634;
44 static const int HDDTEMP_OUTPUT_BUFFER_LENGTH = 4048;
45
46 struct hdd_info {
47         char *name;
48         int temp;
49 };
50
51 static char *fetch(void)
52 {
53         int sockfd, output_length;
54         ssize_t n = 1;
55         char *pc, *buffer;
56         struct sockaddr_in address;
57
58         output_length = 0;
59
60         sockfd = socket(AF_INET, SOCK_STREAM, 0);
61         if (sockfd == -1) {
62                 log_err(_("%s: failed to open socket."), PROVIDER_NAME);
63                 return NULL;
64         }
65
66         address.sin_family = AF_INET;
67         address.sin_addr.s_addr = inet_addr(HDDTEMP_SERVER_IP_ADDRESS);
68         address.sin_port = htons(HDDTEMP_PORT_NUMBER);
69
70         buffer = NULL;
71
72         if (connect(sockfd,
73                     (struct sockaddr *)&address,
74                     (socklen_t) sizeof(address)) == -1) {
75                 log_err(_("%s: failed to open connection."), PROVIDER_NAME);
76         } else {
77                 buffer = malloc(HDDTEMP_OUTPUT_BUFFER_LENGTH);
78
79                 pc = buffer;
80                 while ((n = read(sockfd,
81                                  pc,
82                                  HDDTEMP_OUTPUT_BUFFER_LENGTH -
83                                  output_length)) > 0) {
84
85                         output_length += n;
86                         pc = &pc[n];
87                 }
88
89                 buffer[output_length] = '\0';
90         }
91
92         close(sockfd);
93
94         return buffer;
95 }
96
97 static int str_index(char *str, char d)
98 {
99         char *c;
100         int i;
101
102         if (!str || *str == '\0')
103                 return -1;
104
105         c = str;
106
107         i = 0;
108         while (*c) {
109                 if (*c == d)
110                         return i;
111                 i++;
112                 c++;
113         }
114
115         return -1;
116 }
117
118 static struct psensor *
119 create_sensor(char *id, char *name, int values_max_length)
120 {
121         int t;
122
123         t = SENSOR_TYPE_HDD | SENSOR_TYPE_HDDTEMP | SENSOR_TYPE_TEMP;
124
125         return psensor_create(id, name, strdup(_("Disk")),
126                               t,
127                               values_max_length);
128 }
129
130 static char *next_hdd_info(char *string, struct hdd_info *info)
131 {
132         char *c;
133         int idx_name_n, i, temp;
134
135         if (!string || strlen(string) <= 5      /* at least 5 pipes */
136             || string[0] != '|')
137                 return NULL;
138
139         /* skip first pipe */
140         c = string + 1;
141
142         /* name */
143         idx_name_n = str_index(c, '|');
144
145         if (idx_name_n == -1)
146                 return NULL;
147         c = c + idx_name_n + 1;
148
149         /* skip label */
150         i = str_index(c, '|');
151         if (i == -1)
152                 return NULL;
153         c = c + i + 1;
154
155         /* temp */
156         i = str_index(c, '|');
157         if (i == -1)
158                 return NULL;
159         temp = atoi(c);
160         c = c + i + 1;
161
162         /* skip unit  */
163         i = str_index(c, '|');
164         if (i == -1)
165                 return NULL;
166         c = c + i + 1;
167
168         info->name = malloc(idx_name_n + 1);
169         strncpy(info->name, string + 1, idx_name_n);
170         info->name[idx_name_n] = '\0';
171
172         info->temp = temp;
173
174         return c;
175 }
176
177 void
178 hddtemp_psensor_list_append(struct psensor ***sensors, int values_max_length)
179 {
180         char *hddtemp_output, *c, *id;
181         struct hdd_info info;
182         struct psensor *sensor;
183
184         hddtemp_output = fetch();
185
186         if (!hddtemp_output)
187                 return;
188
189         if (hddtemp_output[0] != '|') {
190                 log_err(_("%s: wrong string: %s."),
191                         PROVIDER_NAME,
192                         hddtemp_output);
193
194                 free(hddtemp_output);
195
196                 return;
197         }
198
199         c = hddtemp_output;
200
201         while (c && (c = next_hdd_info(c, &info))) {
202                 id = malloc(strlen(PROVIDER_NAME) + 1 + strlen(info.name) + 1);
203                 sprintf(id, "%s %s", PROVIDER_NAME, info.name);
204
205                 sensor = create_sensor(id, info.name, values_max_length);
206
207                 psensor_list_append(sensors, sensor);
208         }
209
210         free(hddtemp_output);
211 }
212
213 static void update(struct psensor **sensors, struct hdd_info *info)
214 {
215         while (*sensors) {
216                 if (!((*sensors)->type & SENSOR_TYPE_REMOTE)
217                     && (*sensors)->type & SENSOR_TYPE_HDDTEMP
218                     && !strcmp((*sensors)->id + 8, info->name))
219                         psensor_set_current_value(*sensors,
220                                                   (double)info->temp);
221
222                 sensors++;
223         }
224 }
225
226 static bool contains_hddtemp_sensor(struct psensor **sensors)
227 {
228         struct psensor *s;
229
230         if (!sensors)
231                 return false;
232
233         while (*sensors) {
234                 s = *sensors;
235                 if (!(s->type & SENSOR_TYPE_REMOTE)
236                      && (s->type & SENSOR_TYPE_HDDTEMP))
237                         return true;
238                 sensors++;
239         }
240
241         return false;
242 }
243
244 void hddtemp_psensor_list_update(struct psensor **sensors)
245 {
246         char *hddtemp_output;
247
248         if (!contains_hddtemp_sensor(sensors))
249                 return;
250
251         hddtemp_output = fetch();
252
253         if (!hddtemp_output)
254                 return;
255
256         if (hddtemp_output[0] == '|') {
257                 char *c = hddtemp_output;
258                 struct hdd_info info;
259
260                 info.name = NULL;
261                 info.temp = 0;
262
263                 while (c && (c = next_hdd_info(c, &info))) {
264
265                         update(sensors, &info);
266
267                         free(info.name);
268                 }
269         } else {
270                 log_err(_("%s: wrong string: %s."),
271                         PROVIDER_NAME,
272                         hddtemp_output);
273         }
274
275         free(hddtemp_output);
276 }