7d70b4a4d57b82be1f137ecde908a3bb83205c79
[psensor.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()
52 {
53         int sockfd;
54         ssize_t n = 1;
55         int output_length = 0;
56         char *pc;
57         char *buffer;
58         struct sockaddr_in address;
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;
181         struct hdd_info info;
182
183         hddtemp_output = fetch();
184
185         if (!hddtemp_output)
186                 return;
187
188         if (hddtemp_output[0] != '|') {
189                 log_err(_("%s: wrong string: %s."),
190                         PROVIDER_NAME,
191                         hddtemp_output);
192
193                 free(hddtemp_output);
194
195                 return;
196         }
197
198         c = hddtemp_output;
199
200         while (c && (c = next_hdd_info(c, &info))) {
201                 struct psensor *sensor;
202                 char *id;
203
204                 id = malloc(strlen(PROVIDER_NAME) + 1 + strlen(info.name) + 1);
205                 sprintf(id, "%s %s", PROVIDER_NAME, info.name);
206
207                 sensor = create_sensor(id, info.name, values_max_length);
208
209                 psensor_list_append(sensors, sensor);
210         }
211
212         free(hddtemp_output);
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_REMOTE)
221                     && (*sensor_cur)->type & SENSOR_TYPE_HDDTEMP
222                     && !strcmp((*sensor_cur)->id + 8, info->name))
223                         psensor_set_current_value(*sensor_cur,
224                                                   (double)info->temp);
225
226                 sensor_cur++;
227         }
228 }
229
230 void hddtemp_psensor_list_update(struct psensor **sensors)
231 {
232         char *hddtemp_output;
233
234         hddtemp_output = fetch();
235
236         if (!hddtemp_output)
237                 return;
238
239         if (hddtemp_output[0] == '|') {
240                 char *c = hddtemp_output;
241                 struct hdd_info info;
242
243                 info.name = NULL;
244                 info.temp = 0;
245
246                 while (c && (c = next_hdd_info(c, &info))) {
247
248                         update(sensors, &info);
249
250                         free(info.name);
251                 }
252         } else {
253                 log_err(_("%s: wrong string: %s."),
254                         PROVIDER_NAME,
255                         hddtemp_output);
256         }
257
258         free(hddtemp_output);
259 }