~ubuntu-branches/debian/stretch/sensors-applet/stretch

« back to all changes in this revision

Viewing changes to plugins/hddtemp/hddtemp-plugin.c

  • Committer: Package Import Robot
  • Author(s): Jordi Mallach
  • Date: 2011-11-05 14:53:12 UTC
  • mfrom: (1.2.7)
  • Revision ID: package-import@ubuntu.com-20111105145312-pce58z1ov0rc9moq
Tags: 3.0.0-0.1
* Non-maintainer upload.
* New upstream release, with support for GNOME3 Panel (closes: #638099).
* Drop link-to-libdl and libnotify_0.7_api_change patches, applied
  upstream.
* Drop dh_autoreconf and all bootstrap friends from Build-Depends and
  Build-Conflicts, not needed anymore.
* Update Build-Depends for the GTK+3 port, as per configure.ac.
* Drop .a and .la files (closes: #633257).
* Build-Depend on libsensors4 for kfreebsd again, as the package is now
* available on that architecture.
* Avoid hardcoding architecture names for libatamart-dev, and drop it
  entirely for libsensors4, as all architectures should be supported,
  even if the Hurd isn't available yet (closes: #634515).
* As libsensors-applet-plugin hasn't bumped its sonames but has at least
  got an ABI break (due to the switch to GTK+3), and given there are no
  external rdepends, avoid a library rename by bumping its shlibs and
  adding Breaks: sensor-applets (<< 3.0.0).
* Change install path for the applet binary, and remove obsolete dirs.
* Tighten sensors-applet dependency on libsensors-applet-plugin to
  (= ${binary:Version}), just in case.

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
#include <arpa/inet.h>
37
37
#endif
38
38
 
 
39
#ifdef HAVE_STRING_H
 
40
#include <string.h>
 
41
#endif
 
42
 
 
43
#include <unistd.h>
 
44
#include <string.h>
39
45
#include "hddtemp-plugin.h"
40
46
 
41
47
const gchar *plugin_name = "hddtemp";
49
55
        HDDTEMP_SOCKET_CONNECT_ERROR,
50
56
        HDDTEMP_GIOCHANNEL_ERROR,
51
57
        HDDTEMP_GIOCHANNEL_READ_ERROR
52
 
        
 
58
 
53
59
};
54
60
 
 
61
static gchar buffer[HDDTEMP_OUTPUT_BUFFER_LENGTH];
 
62
 
55
63
static const gchar *hddtemp_plugin_query_hddtemp_daemon(GError **error) {
56
64
        int sockfd;
57
65
        ssize_t n = 1;
58
 
        gboolean first_run = FALSE;
59
 
        gint output_length = 0;
 
66
        guint output_length = 0;
 
67
        static gboolean first_run = TRUE;
60
68
        gchar *pc;
61
69
 
62
70
        struct sockaddr_in address;
63
 
        static char* buffer = NULL;
64
71
        static GTimeVal previous_query_time;
65
72
        GTimeVal current_query_time;
66
73
 
67
 
        if (NULL == buffer) {
68
 
                // initialise buffer and current time
69
 
                buffer = g_new0(char, HDDTEMP_OUTPUT_BUFFER_LENGTH);
 
74
        if (first_run) {
 
75
                // initialise previous time
70
76
                g_get_current_time(&previous_query_time);
71
 
                first_run = TRUE;
72
77
        }
73
78
        g_get_current_time(&current_query_time);
74
79
 
83
88
                        g_set_error(error, SENSORS_APPLET_PLUGIN_ERROR, HDDTEMP_SOCKET_OPEN_ERROR, "Error opening socket for hddtemp");
84
89
                        return NULL;
85
90
                }
86
 
                
 
91
 
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);
90
95
 
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);
 
99
                        close(sockfd);
93
100
                        return NULL;
94
101
                }
95
 
 
 
102
                memset(buffer, 0, sizeof(buffer));
96
103
                pc = 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;
99
 
                        pc = &pc[n];
 
107
                        pc += n;
100
108
                }
101
 
                /* terminate with pipe if not already terminated */
102
 
                if (buffer[n - 1] != '|') {
103
 
                        buffer[n++] = '|';
104
 
                } 
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';
108
111
                close(sockfd);
 
112
                first_run = FALSE;
109
113
        }
110
114
 
111
115
        return buffer;
112
116
}
113
 
        
 
117
 
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;
118
 
        
 
122
 
119
123
        hddtemp_output = hddtemp_plugin_query_hddtemp_daemon(&error);
120
 
        
 
124
 
121
125
        if (error) {
122
126
                g_error_free(error);
123
127
                return;
128
132
                return;
129
133
        }
130
134
 
131
 
        /* for each sensor the output will contain four strings ie 
 
135
        /* for each sensor the output will contain four strings ie
132
136
           |/dev/hda|WDC WD800JB-00ETA0|32|C||/dev/hdb|???|ERR|*|
133
137
           note the repetition -----^ */
134
138
 
135
139
        /*
136
 
          
 
140
 
137
141
          pv[0 + 5*n] empty
138
142
          pv[1 + 5*n] device name
139
143
          pv[2 + 5*n] disk label
144
148
        */
145
149
 
146
150
        pv = output_vector = g_strsplit(hddtemp_output, "|", -1);
147
 
        
 
151
 
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
156
165
                                                        HDD_ICON,
157
166
                                                        DEFAULT_GRAPH_COLOR);
158
167
                }
159
 
                pv += 5; 
 
168
                pv += 5;
160
169
        }
161
170
        g_strfreev(output_vector);
162
 
        
163
171
}
164
172
 
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);
169
177
        return sensors;
170
178
}
171
179
 
172
180
 
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, 
176
 
                                                  const gchar *id, 
 
183
static gdouble hddtemp_plugin_get_sensor_value(const gchar *path,
 
184
                                                  const gchar *id,
177
185
                                                  SensorType type,
178
186
                                                  GError **error) {
179
187
 
193
201
                return sensor_value;
194
202
        }
195
203
 
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 -----^
199
207
 
200
208
          pv[0 + 5*n] empty
201
209
          pv[1 + 5*n] device name
207
215
        */
208
216
 
209
217
        pv = output_vector = g_strsplit(hddtemp_output, "|", -1);
210
 
        
 
218
 
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));
214
 
        
 
222
 
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;
218
226
                        }
219
227
                        break;
220
228
                }
221
 
                pv += 5; 
 
229
                pv += 5;
222
230
        }
223
231
        g_strfreev(output_vector);
224
 
        
 
232
 
225
233
        return (gdouble)sensor_value;
226
234
}
227
235
 
228
 
const gchar *sensors_applet_plugin_name(void) 
 
236
const gchar *sensors_applet_plugin_name(void)
229
237
{
230
238
        return plugin_name;
231
239
}
232
240
 
233
 
GList *sensors_applet_plugin_init(void) 
 
241
GList *sensors_applet_plugin_init(void)
234
242
{
235
243
        return hddtemp_plugin_init();
236
244
}
237
245
 
238
 
gdouble sensors_applet_plugin_get_sensor_value(const gchar *path, 
239
 
                                                const gchar *id, 
 
246
gdouble sensors_applet_plugin_get_sensor_value(const gchar *path,
 
247
                                                const gchar *id,
240
248
                                                SensorType type,
241
249
                                                GError **error) {
242
250
        return hddtemp_plugin_get_sensor_value(path, id, type, error);