2 * Copyright (C) 2010-2014 jeanfi@gmail.com
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.
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.
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
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/
26 #define _(str) gettext(str)
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
40 static const char *PROVIDER_NAME = "hddtemp";
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;
53 int sockfd, output_length;
56 struct sockaddr_in address;
60 sockfd = socket(AF_INET, SOCK_STREAM, 0);
62 log_err(_("%s: failed to open socket."), PROVIDER_NAME);
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);
73 (struct sockaddr *)&address,
74 (socklen_t) sizeof(address)) == -1) {
75 log_err(_("%s: failed to open connection."), PROVIDER_NAME);
77 buffer = malloc(HDDTEMP_OUTPUT_BUFFER_LENGTH);
80 while ((n = read(sockfd,
82 HDDTEMP_OUTPUT_BUFFER_LENGTH -
83 output_length)) > 0) {
89 buffer[output_length] = '\0';
97 static int str_index(char *str, char d)
102 if (!str || *str == '\0')
118 static struct psensor *
119 create_sensor(char *id, char *name, int values_max_length)
123 t = SENSOR_TYPE_HDD | SENSOR_TYPE_HDDTEMP | SENSOR_TYPE_TEMP;
125 return psensor_create(id, name, strdup(_("Disk")),
130 static char *next_hdd_info(char *string, struct hdd_info *info)
133 int idx_name_n, i, temp;
135 if (!string || strlen(string) <= 5 /* at least 5 pipes */
139 /* skip first pipe */
143 idx_name_n = str_index(c, '|');
145 if (idx_name_n == -1)
147 c = c + idx_name_n + 1;
150 i = str_index(c, '|');
156 i = str_index(c, '|');
163 i = str_index(c, '|');
168 info->name = malloc(idx_name_n + 1);
169 strncpy(info->name, string + 1, idx_name_n);
170 info->name[idx_name_n] = '\0';
178 hddtemp_psensor_list_append(struct psensor ***sensors, int values_max_length)
180 char *hddtemp_output, *c, *id;
181 struct hdd_info info;
182 struct psensor *sensor;
184 hddtemp_output = fetch();
189 if (hddtemp_output[0] != '|') {
190 log_err(_("%s: wrong string: %s."),
194 free(hddtemp_output);
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);
205 sensor = create_sensor(id, info.name, values_max_length);
207 psensor_list_append(sensors, sensor);
210 free(hddtemp_output);
213 static void update(struct psensor **sensors, struct hdd_info *info)
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,
226 static bool contains_hddtemp_sensor(struct psensor **sensors)
235 if (!(s->type & SENSOR_TYPE_REMOTE)
236 && (s->type & SENSOR_TYPE_HDDTEMP))
244 void hddtemp_psensor_list_update(struct psensor **sensors)
246 char *hddtemp_output;
248 if (!contains_hddtemp_sensor(sensors))
251 hddtemp_output = fetch();
256 if (hddtemp_output[0] == '|') {
257 char *c = hddtemp_output;
258 struct hdd_info info;
263 while (c && (c = next_hdd_info(c, &info))) {
265 update(sensors, &info);
270 log_err(_("%s: wrong string: %s."),
275 free(hddtemp_output);