1013c59712bf8ee00a962f81884be15056c5d099
[psensor.git] / src / lib / hdd_hddtemp.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 /*
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         return psensor_create(id, name, SENSOR_TYPE_HDD_TEMP_HDDTEMP,
119                               values_max_length);
120 }
121
122 static char *next_hdd_info(char *string, struct hdd_info *info)
123 {
124         char *c;
125         int idx_name_n, i, temp;
126
127         if (!string || strlen(string) <= 5      /* at least 5 pipes */
128             || string[0] != '|')
129                 return NULL;
130
131         /* skip first pipe */
132         c = string + 1;
133
134         /* name */
135         idx_name_n = str_index(c, '|');
136
137         if (idx_name_n == -1)
138                 return NULL;
139         c = c + idx_name_n + 1;
140
141         /* skip label */
142         i = str_index(c, '|');
143         if (i == -1)
144                 return NULL;
145         c = c + i + 1;
146
147         /* temp */
148         i = str_index(c, '|');
149         if (i == -1)
150                 return NULL;
151         temp = atoi(c);
152         c = c + i + 1;
153
154         /* skip unit  */
155         i = str_index(c, '|');
156         if (i == -1)
157                 return NULL;
158         c = c + i + 1;
159
160         info->name = malloc(idx_name_n + 1);
161         strncpy(info->name, string + 1, idx_name_n);
162         info->name[idx_name_n] = '\0';
163
164         info->temp = temp;
165
166         return c;
167 }
168
169 struct psensor **hddtemp_psensor_list_add(struct psensor **sensors,
170                                           int values_max_length)
171 {
172         char *hddtemp_output = fetch();
173         char *c;
174         struct hdd_info info;
175         struct psensor **result;
176
177         if (!hddtemp_output)
178                 return sensors;
179
180         if (hddtemp_output[0] != '|') {
181                 log_err(_("hddtemp: wrong string: %s."), hddtemp_output);
182
183                 free(hddtemp_output);
184
185                 return sensors;
186         }
187
188         c = hddtemp_output;
189
190         result = sensors;
191
192         while (c && (c = next_hdd_info(c, &info))) {
193                 struct psensor *sensor;
194                 struct psensor **tmp_sensors;
195
196                 char *id = malloc(strlen("hdd ") + strlen(info.name) + 1);
197                 strcpy(id, "hdd ");
198                 strcat(id, info.name);
199
200                 sensor = create_sensor(id, info.name, values_max_length);
201
202                 tmp_sensors = psensor_list_add(result, sensor);
203
204                 if (result != sensors)
205                         free(result);
206
207                 result = tmp_sensors;
208         }
209
210         free(hddtemp_output);
211
212         return result;
213 }
214
215 static void update(struct psensor **sensors, struct hdd_info *info)
216 {
217         struct psensor **sensor_cur = sensors;
218
219         while (*sensor_cur) {
220                 if ((*sensor_cur)->type == SENSOR_TYPE_HDD_TEMP_HDDTEMP
221                     && !strcmp((*sensor_cur)->id + 4, info->name))
222                         psensor_set_current_value(*sensor_cur,
223                                                   (float)info->temp);
224
225                 sensor_cur++;
226         }
227 }
228
229 void hddtemp_psensor_list_update(struct psensor **sensors)
230 {
231         char *hddtemp_output = fetch();
232
233         if (!hddtemp_output)
234                 return;
235
236         if (hddtemp_output[0] == '|') {
237
238                 char *c = hddtemp_output;
239                 struct hdd_info info;
240                 info.name = NULL;
241                 info.temp = 0;
242
243                 while (c && (c = next_hdd_info(c, &info))) {
244
245                         update(sensors, &info);
246
247                         free(info.name);
248                 }
249         } else {
250                 log_err(_("hddtemp: wrong string: %s."), hddtemp_output);
251         }
252
253         free(hddtemp_output);
254 }