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;
55 int output_length = 0;
58 struct sockaddr_in address;
60 sockfd = socket(AF_INET, SOCK_STREAM, 0);
62 log_err(_("hddtemp: failed to open socket."));
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(_("hddtemp: failed to open connection."));
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';
177 struct psensor **hddtemp_psensor_list_add(struct psensor **sensors,
178 int values_max_length)
180 char *hddtemp_output, *c;
181 struct hdd_info info;
182 struct psensor **result;
184 hddtemp_output = fetch();
189 if (hddtemp_output[0] != '|') {
190 log_err(_("hddtemp: wrong string: %s."), hddtemp_output);
192 free(hddtemp_output);
201 while (c && (c = next_hdd_info(c, &info))) {
202 struct psensor *sensor;
203 struct psensor **tmp_sensors;
206 id = malloc(strlen(PROVIDER_NAME) + 1 + strlen(info.name) + 1);
207 sprintf(id, "%s %s", PROVIDER_NAME, info.name);
209 sensor = create_sensor(id, info.name, values_max_length);
211 tmp_sensors = psensor_list_add(result, sensor);
213 if (result != sensors)
216 result = tmp_sensors;
219 free(hddtemp_output);
224 static void update(struct psensor **sensors, struct hdd_info *info)
226 struct psensor **sensor_cur = sensors;
228 while (*sensor_cur) {
229 if (!((*sensor_cur)->type & SENSOR_TYPE_REMOTE)
230 && (*sensor_cur)->type & SENSOR_TYPE_HDDTEMP
231 && !strcmp((*sensor_cur)->id + 8, info->name))
232 psensor_set_current_value(*sensor_cur,
239 void hddtemp_psensor_list_update(struct psensor **sensors)
241 char *hddtemp_output;
243 hddtemp_output = fetch();
248 if (hddtemp_output[0] == '|') {
249 char *c = hddtemp_output;
250 struct hdd_info info;
255 while (c && (c = next_hdd_info(c, &info))) {
257 update(sensors, &info);
262 log_err(_("hddtemp: wrong string: %s."), hddtemp_output);
265 free(hddtemp_output);