49
55
HDDTEMP_SOCKET_CONNECT_ERROR,
50
56
HDDTEMP_GIOCHANNEL_ERROR,
51
57
HDDTEMP_GIOCHANNEL_READ_ERROR
61
static gchar buffer[HDDTEMP_OUTPUT_BUFFER_LENGTH];
55
63
static const gchar *hddtemp_plugin_query_hddtemp_daemon(GError **error) {
58
gboolean first_run = FALSE;
59
gint output_length = 0;
66
guint output_length = 0;
67
static gboolean first_run = TRUE;
62
70
struct sockaddr_in address;
63
static char* buffer = NULL;
64
71
static GTimeVal previous_query_time;
65
72
GTimeVal current_query_time;
68
// initialise buffer and current time
69
buffer = g_new0(char, HDDTEMP_OUTPUT_BUFFER_LENGTH);
75
// initialise previous time
70
76
g_get_current_time(&previous_query_time);
73
78
g_get_current_time(¤t_query_time);
83
88
g_set_error(error, SENSORS_APPLET_PLUGIN_ERROR, HDDTEMP_SOCKET_OPEN_ERROR, "Error opening socket for hddtemp");
87
92
address.sin_family = AF_INET;
88
93
address.sin_addr.s_addr = inet_addr(HDDTEMP_SERVER_IP_ADDRESS);
89
94
address.sin_port = htons(HDDTEMP_PORT_NUMBER);
91
if (connect(sockfd, (struct sockaddr *)&address, (socklen_t)sizeof(address)) == -1) {
96
if (connect(sockfd, (struct sockaddr *)&address,
97
(socklen_t)sizeof(address)) == -1) {
92
98
g_set_error(error, SENSORS_APPLET_PLUGIN_ERROR, HDDTEMP_SOCKET_CONNECT_ERROR, "Error connecting to hddtemp daemon on port %i on %s", htons(HDDTEMP_PORT_NUMBER), HDDTEMP_SERVER_IP_ADDRESS);
102
memset(buffer, 0, sizeof(buffer));
97
while ((n = read(sockfd, pc, HDDTEMP_OUTPUT_BUFFER_LENGTH - output_length)) > 0) {
104
while ((n = read(sockfd, pc,
105
sizeof(buffer) - output_length)) > 0) {
98
106
output_length += n;
101
/* terminate with pipe if not already terminated */
102
if (buffer[n - 1] != '|') {
105
/* always null terminate the end of the buffer
106
* regardless of how much stuff is in it already */
107
buffer[output_length] = '\0';
109
/* always null terminate the end of the buffer */
110
buffer[MIN(output_length, sizeof(buffer) - 1)] = '\0';
114
118
static void hddtemp_plugin_get_sensors(GList **sensors) {
115
119
GError *error = NULL;
116
120
const gchar *hddtemp_output;
117
121
gchar **output_vector = NULL, **pv;
119
123
hddtemp_output = hddtemp_plugin_query_hddtemp_daemon(&error);
122
126
g_error_free(error);
146
150
pv = output_vector = g_strsplit(hddtemp_output, "|", -1);
148
152
while(pv[1] != NULL) {
149
if (pv[2] != "" && pv [3] != "" && pv[4] != "" && (!(g_ascii_strcasecmp(pv[2], "???") == 0 || g_ascii_strcasecmp(pv[3], "ERR") == 0 || g_ascii_strcasecmp(pv[4], "*") == 0))) {
153
if (strcmp(pv[2], "") != 0 &&
154
strcmp(pv[3], "") != 0 &&
155
strcmp(pv[4], "") != 0 &&
156
(!(g_ascii_strcasecmp(pv[2], "???") == 0 ||
157
g_ascii_strcasecmp(pv[3], "ERR") == 0 ||
158
g_ascii_strcasecmp(pv[4], "*") == 0))) {
150
159
sensors_applet_plugin_add_sensor(sensors,
151
160
pv[1], // must be dynamically allocated
152
161
pv[1], // must be dynamically allocated
157
166
DEFAULT_GRAPH_COLOR);
161
170
g_strfreev(output_vector);
165
173
/* to be called to setup for hddtemp sensors */
166
174
static GList *hddtemp_plugin_init(void) {
167
175
GList *sensors = NULL;
168
hddtemp_plugin_get_sensors(&sensors);
176
hddtemp_plugin_get_sensors(&sensors);
173
181
/* returns the value of the sensor_list at the given iter, or if an
174
182
error occurs, instatiates error with an error message */
175
static gdouble hddtemp_plugin_get_sensor_value(const gchar *path,
183
static gdouble hddtemp_plugin_get_sensor_value(const gchar *path,
178
186
GError **error) {
193
201
return sensor_value;
196
/* for each sensor the output will contain four strings ie
204
/* for each sensor the output will contain four strings ie
197
205
|/dev/hda|WDC WD800JB-00ETA0|32|C||/dev/hdb|???|ERR|*|
198
note the repetition -----^
206
note the repetition -----^
200
208
pv[0 + 5*n] empty
201
209
pv[1 + 5*n] device name
209
217
pv = output_vector = g_strsplit(hddtemp_output, "|", -1);
211
219
while(pv[1] != NULL) {
212
220
if (g_ascii_strcasecmp(pv[1], path) == 0) {
213
221
sensor_value = (gfloat)(g_ascii_strtod(pv[3], NULL));
215
223
/* always return sensor values in celsius */
216
224
if (pv[4][0] == 'F') {
217
225
sensor_value = (sensor_value - 32.0) * 5.0 / 9.0;
223
231
g_strfreev(output_vector);
225
233
return (gdouble)sensor_value;
228
const gchar *sensors_applet_plugin_name(void)
236
const gchar *sensors_applet_plugin_name(void)
230
238
return plugin_name;
233
GList *sensors_applet_plugin_init(void)
241
GList *sensors_applet_plugin_init(void)
235
243
return hddtemp_plugin_init();
238
gdouble sensors_applet_plugin_get_sensor_value(const gchar *path,
246
gdouble sensors_applet_plugin_get_sensor_value(const gchar *path,
241
249
GError **error) {
242
250
return hddtemp_plugin_get_sensor_value(path, id, type, error);