diff options
author | Daniel Lezcano <daniel.lezcano@linaro.org> | 2021-05-02 22:46:43 +0200 |
---|---|---|
committer | Daniel Lezcano <daniel.lezcano@linaro.org> | 2021-05-02 22:46:43 +0200 |
commit | 207c514356c151cceccbbd1ef62b08409b2b6cf8 (patch) | |
tree | d5b9ea725db6a933a759b1e92cdcf14436621175 | |
parent | b9b3813eff450a27b811bba35693f89d70e2ab4d (diff) |
Bind the thermal zone to a dtpm node
Signed-off-by: Daniel Lezcano <daniel.lezcano@linaro.org>
-rw-r--r-- | src/dtpm/dtpm.c | 109 |
1 files changed, 78 insertions, 31 deletions
diff --git a/src/dtpm/dtpm.c b/src/dtpm/dtpm.c index 00429e1..749f0f0 100644 --- a/src/dtpm/dtpm.c +++ b/src/dtpm/dtpm.c @@ -53,11 +53,13 @@ struct dtpm_node { struct dtpm_field max_power_range_uw; struct dtpm_field power_uw; struct dtpm_field name; + struct thermal_zone *tz; }; struct cb_param { struct thermal_handler *th; struct thermal_zone *tz; + struct dtpm_config *cfg; struct dtpm_node *root; int line; }; @@ -334,8 +336,8 @@ 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; - 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; } @@ -345,8 +347,8 @@ 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; - 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; } @@ -391,7 +393,17 @@ 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) { - printf("cdev:%d state:%d\n", cdev_id, cur_state); + struct cb_param *cb_param = arg; + char buffer[4096]; + + snprintf(buffer, sizeof(buffer) - 1, "cdev:%d state:%d", + cdev_id, cur_state); + + display_reset_cursor(15 + cdev_id); + + display_print_line(0, 0, buffer, 0, arg); + + display_refresh_pad(0); return 0; } @@ -422,12 +434,21 @@ static struct thermal_ops ops = { int show_power_uw(struct dtpm_node *node, void *arg) { struct cb_param *cb_param = arg; + struct thermal_handler *th = cb_param->th; char buffer[4096]; + size_t len; dtpm_read_field(node, &node->power_uw); - snprintf(buffer, sizeof(buffer) - 1, "%*s%s: %llu mW", 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); + } display_print_line(0, cb_param->line++, buffer, 0, arg); @@ -438,16 +459,8 @@ int tz_get_temp(struct thermal_zone *tz, void *arg) { struct cb_param *cb_param = arg; struct thermal_handler *th = cb_param->th; - char buffer[128]; - - if (thermal_cmd_get_temp(th, tz) < 0) - return -1; - - sprintf(buffer, "%s: %d mC°", tz->name, tz->temp); - - display_print_line(0, cb_param->line++, buffer, 0, arg); - return 0; + return thermal_cmd_get_temp(th, tz); } static int event_callback(__maybe_unused int fd, void *arg) @@ -461,16 +474,16 @@ static int event_callback(__maybe_unused int fd, void *arg) static int timer_callback(int fd, void *arg) { struct cb_param *cb_param = arg; - struct thermal_zone *tz = cb_param->tz; struct dtpm_node *root = cb_param->root; + struct thermal_zone *tz = cb_param->tz; char buf[8]; cb_param->line = 3; display_reset_cursor(0); - - for_each_thermal_zone(tz, tz_get_temp, arg); + for_each_thermal_zone(tz, tz_get_temp, arg); + if (for_each_dtpm_node(root, show_power_uw, arg)) return -1; @@ -481,6 +494,38 @@ static int timer_callback(int fd, void *arg) return 0; } +static int dtpm_bind_thermal_zone(struct dtpm_node *node, void *arg) +{ + struct cb_param *cbp = arg; + struct thermal_zone *tz; + const char *name; + + name = dtpm_get_thermal_zone(cbp->cfg, node->name.ptr); + if (!name) + return 0; + + tz = thermal_zone_find_by_name(cbp->tz, name); + if (!tz) + return 0; + + node->tz = tz; + + DEBUG("'%s' belongs to the thermal zone '%s'\n", + (char *)node->name.ptr, name); + + return 0; +} + +static struct timespec sec_to_timespec(float delay) +{ + struct timespec tv = { + .tv_sec = (time_t)delay, + .tv_nsec = (delay - tv.tv_sec) * 1000000000 + }; + + return tv; +} + int main(void) { struct cb_param cb_param; @@ -488,8 +533,9 @@ int main(void) struct thermal_zone *tz; struct thermal_handler *th; struct itimerspec timer_it = { 0 }; - struct dtpm_config *dtpm_cfg; + struct dtpm_config *cfg; char *config_path = NULL; + float delay = 0.5; int timerfd; /* @@ -499,8 +545,8 @@ int main(void) } */ - dtpm_cfg = dtpm_config(config_path); - if (!dtpm_cfg) + cfg = dtpm_config(config_path); + if (!cfg) WARN("No configuration file\n"); root = dtpm_build_tree(dtpm_path(), NULL); @@ -515,6 +561,14 @@ int main(void) if (!tz) return -1; + cb_param.cfg = cfg; + cb_param.root = root; + cb_param.th = th; + cb_param.tz = tz; + + if (for_each_dtpm_node(root, dtpm_bind_thermal_zone, &cb_param)) + return -1; + if (mainloop_init()) return -1; @@ -522,18 +576,11 @@ int main(void) if (timerfd < 0) return -1; - timer_it.it_interval.tv_sec = 1; - timer_it.it_interval.tv_nsec = 0; - timer_it.it_value.tv_sec = 1; - timer_it.it_value.tv_nsec = 0; + timer_it.it_interval = timer_it.it_value = sec_to_timespec(delay); if (timerfd_settime(timerfd, 0, &timer_it, NULL) < 0) return -1; - cb_param.th = th; - cb_param.tz = tz; - cb_param.root = root; - if (mainloop_add(timerfd, timer_callback, &cb_param)) return -1; @@ -543,5 +590,5 @@ int main(void) if (display_init()) return -1; - return mainloop(1000); + return mainloop(-1); } |