diff options
author | Daniel Lezcano <daniel.lezcano@linaro.org> | 2021-05-12 08:49:50 +0200 |
---|---|---|
committer | Daniel Lezcano <daniel.lezcano@linaro.org> | 2021-05-12 08:49:50 +0200 |
commit | 58d5914c873accb4606b60be08dd29140a0dcb4d (patch) | |
tree | ea5079d08ea06279304ac273396f3e610ad9372f | |
parent | f20bd5f3b0064874fcd2c78e710eda6a9e62b54a (diff) |
Fix some warnings
Signed-off-by: Daniel Lezcano <daniel.lezcano@linaro.org>
-rw-r--r-- | src/dtpm/dtpm.c | 151 | ||||
-rw-r--r-- | src/libthermal/commands.c | 4 |
2 files changed, 100 insertions, 55 deletions
diff --git a/src/dtpm/dtpm.c b/src/dtpm/dtpm.c index 749f0f0..f756678 100644 --- a/src/dtpm/dtpm.c +++ b/src/dtpm/dtpm.c @@ -28,6 +28,7 @@ typedef unsigned long long u64; struct dtpm_field { + FILE *file; const char *filename; const char *fmt; union { @@ -64,32 +65,15 @@ struct cb_param { int line; }; +struct open_param { + struct dtpm_field *field; + const char *mode; +}; + typedef int (*filter_t)(const char *); typedef int (*dtpm_file_cb_t)(const char *, void *); typedef int (*dtpm_node_cb_t)(struct dtpm_node *, void *); -static int dtpm_file_rw(const char *filename, int rw, const char *fmt, ...) -{ - const char *mode = rw == O_RDONLY ? "r" : "w"; - FILE *f; - va_list args; - int ret; - - f = fopen(filename, mode); - if (!f) { - ERROR("Failed to open %s:%m\n", filename); - return -1; - } - - va_start (args, fmt); - ret = rw == O_RDONLY ? vfscanf(f, fmt, args) : vfprintf(f, fmt, args); - va_end(args); - - fclose(f); - - return ret == EOF; -} - int dtpm_node_add_child(struct dtpm_node *node, struct dtpm_node *child) { int i; @@ -193,30 +177,81 @@ int for_each_dtpm_node(struct dtpm_node *node, dtpm_node_cb_t cb, void *data) return ret; } -static int dtpm_read_field(struct dtpm_node *node, struct dtpm_field *field) +static int dtpm_read_field(struct dtpm_field *field) +{ + lseek(fileno(field->file), 0L, SEEK_SET); + + return fscanf(field->file, field->fmt, field->ptr) == EOF ? -1 : 0; +} + +int dtpm_read(struct dtpm_node *node) +{ + int i, ret; + + struct open_param dtpm_fields[] = { + { &node->max_power_range_uw, "r" }, + { &node->power_uw, "r" }, + { &node->name, "r" }, + { &node->constraints[0].name, "r" }, + { &node->constraints[0].power_limit_uw, "rw" }, + { &node->constraints[0].max_power_uw, "r" }, + }; + + for (i = 0; i < sizeof(dtpm_fields) / sizeof(dtpm_fields[0]); i++) { + ret |= dtpm_read_field(dtpm_fields[i].field); + } + + return ret < 0 ? -1 : 0; +} + +int dtpm_open_field(struct dtpm_node *node, struct dtpm_field *field, + const char *mode) { char path[PATH_MAX]; sprintf(path, "%s/%s", node->path, field->filename); - - return dtpm_file_rw(path, O_RDONLY, field->fmt, field->ptr); + + field->file = fopen(path, mode); + + return field->file ? 0 : -1; } -int dtpm_read(struct dtpm_node *node) +void dtpm_close(struct dtpm_node *node) +{ + int i; + + struct open_param dtpm_fields[] = { + { &node->max_power_range_uw, "r" }, + { &node->power_uw, "r" }, + { &node->name, "r" }, + { &node->constraints[0].name, "r" }, + { &node->constraints[0].power_limit_uw, "rw" }, + { &node->constraints[0].max_power_uw, "r" }, + }; + + for (i = 0; i < sizeof(dtpm_fields) / sizeof(dtpm_fields[0]); i++) + fclose(dtpm_fields[i].field->file); +} + +int dtpm_open(struct dtpm_node *node) { int i; int ret = 0; - - ret += dtpm_read_field(node, &node->max_power_range_uw); - ret += dtpm_read_field(node, &node->power_uw); - ret += dtpm_read_field(node, &node->name); - for (i = 0; i < DTPM_CONSTRAINT_MAX; i++) { - ret += dtpm_read_field(node, &node->constraints[i].name); - ret += dtpm_read_field(node, &node->constraints[i].power_limit_uw); - ret += dtpm_read_field(node, &node->constraints[i].max_power_uw); - } + struct open_param dtpm_fields[] = { + { &node->max_power_range_uw, "r" }, + { &node->power_uw, "r" }, + { &node->name, "r" }, + { &node->constraints[0].name, "r" }, + { &node->constraints[0].power_limit_uw, "rw" }, + { &node->constraints[0].max_power_uw, "r" }, + }; + for (i = 0; i < sizeof(dtpm_fields) / sizeof(dtpm_fields[0]); i++) { + ret += dtpm_open_field(node, dtpm_fields[i].field, + dtpm_fields[i].mode); + } + return ret; } @@ -228,11 +263,16 @@ int dtpm_init(const char *dirname, struct dtpm_node *node) return -1; } + if (dtpm_open(node) < 0) { + fprintf(stderr, "Failed to open node '%s'\n", dirname); + return -1; + } + if (dtpm_read(node) < 0) { fprintf(stderr, "Failed to read node information '%s'\n", dirname); return -1; } - + return 0; } @@ -331,29 +371,31 @@ static int tz_enable(int tz_id, __maybe_unused void *arg) return 0; } -static int trip_high(int tz_id, int trip_id, int temp, __maybe_unused void *arg) +static int trip_high(int tz_id, int trip_id, int temp, + __maybe_unused void *arg) { - struct cb_param *cb_param = arg; - struct thermal_zone *tz = cb_param->tz; + /* struct cb_param *cb_param = arg; */ + /* struct thermal_zone *tz = cb_param->tz; */ -/* printf("Thermal zone %d: trip point %d (%d mC°) crossed way up with %d °C\n", - tz_id, trip_id, tz[tz_id].trip[trip_id].temp, temp); */ + /* printf("Thermal zone %d: trip point %d (%d mC°) crossed way up with %d °C\n", */ + /* tz_id, trip_id, tz[tz_id].trip[trip_id].temp, temp); */ return 0; } static int trip_low(int tz_id, int trip_id, int temp, __maybe_unused void *arg) { - struct cb_param *cb_param = arg; - struct thermal_zone *tz = cb_param->tz; + /* struct cb_param *cb_param = arg; */ + /* struct thermal_zone *tz = cb_param->tz; */ - /* printf("Thermal zone %d: trip point %d (%d mC°) crossed way down with %d °C\n", - tz_id, trip_id, tz[tz_id].trip[trip_id].temp, temp); */ + /* printf("Thermal zone %d: trip point %d (%d mC°) crossed way down with %d °C\n", */ + /* tz_id, trip_id, tz[tz_id].trip[trip_id].temp, temp); */ return 0; } -static int trip_add(int tz_id, int trip_id, int type, int temp, int hyst, __maybe_unused void *arg) +static int trip_add(int tz_id, int trip_id, int type, int temp, int hyst, + __maybe_unused void *arg) { printf("Trip point added %d: id=%d, type=%d, temp=%d, hyst=%d\n", tz_id, trip_id, type, temp, hyst); @@ -368,7 +410,8 @@ static int trip_delete(int tz_id, int trip_id, __maybe_unused void *arg) return 0; } -static int trip_change(int tz_id, int trip_id, int type, int temp, int hyst, __maybe_unused void *arg) +static int trip_change(int tz_id, int trip_id, int type, int temp, int hyst, + __maybe_unused void *arg) { printf("Trip point changed %d: id=%d, type=%d, temp=%d, hyst=%d\n", tz_id, trip_id, type, temp, hyst); @@ -376,7 +419,8 @@ static int trip_change(int tz_id, int trip_id, int type, int temp, int hyst, __m return 0; } -static int cdev_add(const char *name, int cdev_id, int max_state, __maybe_unused void *arg) +static int cdev_add(const char *name, int cdev_id, int max_state, + __maybe_unused void *arg) { printf("Cooling device '%s'/%d (max state=%d) added", name, cdev_id, max_state); @@ -393,7 +437,6 @@ static int cdev_delete(int cdev_id, __maybe_unused void *arg) static int cdev_update(int cdev_id, int cur_state, __maybe_unused void *arg) { - struct cb_param *cb_param = arg; char buffer[4096]; snprintf(buffer, sizeof(buffer) - 1, "cdev:%d state:%d", @@ -438,16 +481,18 @@ int show_power_uw(struct dtpm_node *node, void *arg) char buffer[4096]; size_t len; - dtpm_read_field(node, &node->power_uw); + dtpm_read_field(&node->power_uw); - len = snprintf(buffer, sizeof(buffer) - 1, "%*s%s: power=%llu uW", node->level, "", - node->name.name, node->power_uw.u64val); + len = snprintf(buffer, sizeof(buffer) - 1, "%*s%s: power=%llu uW", + node->level, "", node->name.name, + node->power_uw.u64val); if (node->tz) { if (thermal_cmd_get_temp(th, node->tz) < 0) return -1; - snprintf(buffer + len, sizeof(buffer) + len - 1, " / temp=%d mC°", node->tz->temp); + snprintf(buffer + len, sizeof(buffer) + len - 1, + " / temp=%d mC°", node->tz->temp); } display_print_line(0, cb_param->line++, buffer, 0, arg); diff --git a/src/libthermal/commands.c b/src/libthermal/commands.c index bd7b244..cf26028 100644 --- a/src/libthermal/commands.c +++ b/src/libthermal/commands.c @@ -162,7 +162,7 @@ int parse_tz_get_trip(struct genl_info *info, struct thermal_zone *tz) int parse_tz_get_temp(struct genl_info *info, struct thermal_zone *tz) { - int id; + int id = -1; if (info->attrs[THERMAL_GENL_ATTR_TZ_ID]) id = nla_get_u32(info->attrs[THERMAL_GENL_ATTR_TZ_ID]); @@ -181,7 +181,7 @@ int parse_tz_get_temp(struct genl_info *info, struct thermal_zone *tz) int parse_tz_get_gov(struct genl_info *info, struct thermal_zone *tz) { - int id; + int id = -1; if (info->attrs[THERMAL_GENL_ATTR_TZ_ID]) id = nla_get_u32(info->attrs[THERMAL_GENL_ATTR_TZ_ID]); |