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