worked on smartmon support
[psensor.git] / src / lib / hdd_hddtemp.c
1 /*
2  * Copyright (C) 2010-2011 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 hddtemp-plugin.c
25   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                 fprintf(stderr, _("ERROR: hdd fetch, failed to open socket\n"));
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                 fprintf(stderr,
73                         _("ERROR: hdd fetch, failed to open connection\n"));
74         } else {
75                 buffer = malloc(HDDTEMP_OUTPUT_BUFFER_LENGTH);
76
77                 pc = buffer;
78                 while ((n = read(sockfd,
79                                  pc,
80                                  HDDTEMP_OUTPUT_BUFFER_LENGTH -
81                                  output_length)) > 0) {
82
83                         output_length += n;
84                         pc = &pc[n];
85                 }
86
87                 buffer[output_length] = '\0';
88         }
89
90         close(sockfd);
91
92         return buffer;
93 }
94
95 static int str_index(char *str, char d)
96 {
97         char *c;
98         int i;
99
100         if (!str || *str == '\0')
101                 return -1;
102
103         c = str;
104
105         i = 0;
106         while (*c) {
107                 if (*c == d)
108                         return i;
109                 i++;
110                 c++;
111         }
112
113         return -1;
114 }
115
116 static struct psensor *
117 create_sensor(char *id, char *name, int values_max_length)
118 {
119         return psensor_create(id, name, SENSOR_TYPE_HDD_TEMP_HDDTEMP,
120                               values_max_length);
121 }
122
123 static char *next_hdd_info(char *string, struct hdd_info *info)
124 {
125         char *c;
126         int idx_name_n, i, temp;
127
128         if (!string || strlen(string) <= 5      /* at least 5 pipes */
129             || string[0] != '|')
130                 return NULL;
131
132         /* skip first pipe */
133         c = string + 1;
134
135         /* name */
136         idx_name_n = str_index(c, '|');
137
138         if (idx_name_n == -1)
139                 return NULL;
140         c = c + idx_name_n + 1;
141
142         /* skip label */
143         i = str_index(c, '|');
144         if (i == -1)
145                 return NULL;
146         c = c + i + 1;
147
148         /* temp */
149         i = str_index(c, '|');
150         if (i == -1)
151                 return NULL;
152         temp = atoi(c);
153         c = c + i + 1;
154
155         /* skip unit  */
156         i = str_index(c, '|');
157         if (i == -1)
158                 return NULL;
159         c = c + i + 1;
160
161         info->name = malloc(idx_name_n + 1);
162         strncpy(info->name, string + 1, idx_name_n);
163         info->name[idx_name_n] = '\0';
164
165         info->temp = temp;
166
167         return c;
168 }
169
170 struct psensor **hdd_psensor_list_add(struct psensor **sensors,
171                                       int values_max_length)
172 {
173         char *hddtemp_output = fetch();
174         char *c;
175         struct hdd_info info;
176         struct psensor **result;
177
178         if (!hddtemp_output)
179                 return sensors;
180
181         if (hddtemp_output[0] != '|') {
182                 fprintf(stderr,
183                         _("ERROR: wrong hdd string: %s"), hddtemp_output);
184
185                 free(hddtemp_output);
186
187                 return sensors;
188         }
189
190         c = hddtemp_output;
191
192         result = sensors;
193
194         while (c && (c = next_hdd_info(c, &info))) {
195                 struct psensor *sensor;
196                 struct psensor **tmp_sensors;
197
198                 char *id = malloc(strlen("hdd ") + strlen(info.name) + 1);
199                 strcpy(id, "hdd ");
200                 strcat(id, info.name);
201
202                 sensor = create_sensor(id, info.name, values_max_length);
203
204                 tmp_sensors = psensor_list_add(result, sensor);
205
206                 if (result != sensors)
207                         free(result);
208
209                 result = tmp_sensors;
210         }
211
212         free(hddtemp_output);
213
214         return result;
215 }
216
217 static void update(struct psensor **sensors, struct hdd_info *info)
218 {
219         struct psensor **sensor_cur = sensors;
220
221         while (*sensor_cur) {
222                 if ((*sensor_cur)->type == SENSOR_TYPE_HDD_TEMP_HDDTEMP
223                     && !strcmp((*sensor_cur)->id + 4, info->name))
224                         psensor_set_current_value(*sensor_cur,
225                                                   (float)info->temp);
226
227                 sensor_cur++;
228         }
229 }
230
231 void hdd_psensor_list_update(struct psensor **sensors)
232 {
233         char *hddtemp_output = fetch();
234
235         if (!hddtemp_output)
236                 return;
237
238         if (hddtemp_output[0] == '|') {
239
240                 char *c = hddtemp_output;
241                 struct hdd_info info;
242                 info.name = NULL;
243                 info.temp = 0;
244
245                 while (c && (c = next_hdd_info(c, &info))) {
246
247                         update(sensors, &info);
248
249                         free(info.name);
250                 }
251         } else {
252                 fprintf(stderr,
253                         _("ERROR: wrong hdd string: %s\n"), hddtemp_output);
254         }
255
256         free(hddtemp_output);
257 }