sysfs objects: convert to generic object payload
authorPhil Sutter <phil@nwl.cc>
Sun, 4 Oct 2009 16:22:08 +0000 (18:22 +0200)
committerPhil Sutter <phil@nwl.cc>
Tue, 3 Nov 2009 22:23:22 +0000 (23:23 +0100)
src/core.c
src/linux.c
src/linux.h
src/text_object.h

index 6628a2d..495cfb5 100644 (file)
@@ -1371,7 +1371,7 @@ void free_text_objects(struct text_object *root, int internal)
                        case OBJ_i2c:
                        case OBJ_platform:
                        case OBJ_hwmon:
-                               close(data.sysfs.fd);
+                               free_sysfs_sensor(obj);
                                break;
 #endif /* __linux__ */
                        case OBJ_read_tcp:
index e1deceb..a0b21ef 100644 (file)
 #include <iwlib.h>
 #endif
 
+struct sysfs {
+       int fd;
+       int arg;
+       char devtype[256];
+       char type[64];
+       float factor, offset;
+};
+
 #define SHORTSTAT_TEMPL "%*s %llu %llu %llu"
 #define LONGSTAT_TEMPL "%*s %llu %llu %llu "
 
@@ -1033,6 +1041,7 @@ static void parse_sysfs_sensor(struct text_object *obj, const char *arg, const c
        char buf1[64], buf2[64];
        float factor, offset;
        int n, found = 0;
+       struct sysfs *sf;
 
        if (sscanf(arg, "%63s %d %f %f", buf2, &n, &factor, &offset) == 4) found = 1; else HWMON_RESET();
        if (!found && sscanf(arg, "%63s %63s %d %f %f", buf1, buf2, &n, &factor, &offset) == 5) found = 1; else if (!found) HWMON_RESET();
@@ -1045,11 +1054,14 @@ static void parse_sysfs_sensor(struct text_object *obj, const char *arg, const c
                return;
        }
        DBGP("parsed %s args: '%s' '%s' %d %f %f\n", type, buf1, buf2, n, factor, offset);
-       obj->data.sysfs.fd = open_sysfs_sensor(path, (*buf1) ? buf1 : 0, buf2, n,
-                       &obj->data.sysfs.arg, obj->data.sysfs.devtype);
-       strncpy(obj->data.sysfs.type, buf2, 63);
-       obj->data.sysfs.factor = factor;
-       obj->data.sysfs.offset = offset;
+       sf = malloc(sizeof(struct sysfs));
+       memset(sf, 0, sizeof(struct sysfs));
+       sf->fd = open_sysfs_sensor(path, (*buf1) ? buf1 : 0, buf2, n,
+                       &sf->arg, sf->devtype);
+       strncpy(sf->type, buf2, 63);
+       sf->factor = factor;
+       sf->offset = offset;
+       obj->data.opaque = sf;
 }
 
 #define PARSER_GENERATOR(name, path)                                \
@@ -1065,13 +1077,17 @@ PARSER_GENERATOR(platform, "/sys/bus/platform/devices/")
 void print_sysfs_sensor(struct text_object *obj, char *p, int p_max_size)
 {
        double r;
+       struct sysfs *sf = obj->data.opaque;
+
+       if (!sf)
+               return;
 
-       r = get_sysfs_info(&obj->data.sysfs.fd, obj->data.sysfs.arg,
-                       obj->data.sysfs.devtype, obj->data.sysfs.type);
+       r = get_sysfs_info(&sf->fd, sf->arg,
+                       sf->devtype, sf->type);
 
-       r = r * obj->data.sysfs.factor + obj->data.sysfs.offset;
+       r = r * sf->factor + sf->offset;
 
-       if (!strncmp(obj->data.sysfs.type, "temp", 4)) {
+       if (!strncmp(sf->type, "temp", 4)) {
                temp_print(p, p_max_size, r, TEMP_CELSIUS);
        } else if (r >= 100.0 || r == 0) {
                snprintf(p, p_max_size, "%d", (int) r);
@@ -1080,6 +1096,18 @@ void print_sysfs_sensor(struct text_object *obj, char *p, int p_max_size)
        }
 }
 
+void free_sysfs_sensor(struct text_object *obj)
+{
+       struct sysfs *sf = obj->data.opaque;
+
+       if (!sf)
+               return;
+
+       close(sf->fd);
+       free(obj->data.opaque);
+       obj->data.opaque = NULL;
+}
+
 /* Prior to kernel version 2.6.12, the CPU fan speed was available in
  * ADT746X_FAN_OLD, whereas later kernel versions provide this information in
  * ADT746X_FAN. */
index 93e2a4b..323528a 100644 (file)
@@ -38,5 +38,6 @@ void parse_i2c_sensor(struct text_object *, const char *);
 void parse_hwmon_sensor(struct text_object *, const char *);
 void parse_platform_sensor(struct text_object *, const char *);
 void print_sysfs_sensor(struct text_object *, char *, int );
+void free_sysfs_sensor(struct text_object *);
 
 #endif /* _LINUX_H */
index e1f411d..a97b355 100644 (file)
@@ -462,14 +462,6 @@ struct text_object {
 #endif
 
                struct {
-                       int fd;
-                       int arg;
-                       char devtype[256];
-                       char type[64];
-                       float factor, offset;
-               } sysfs;
-
-               struct {
                        struct text_object *next;
                        char *s;
                        int i;