timer: Add two more helper functions

This commit is contained in:
Daan De Meyer
2023-10-19 16:42:38 +02:00
parent f57cc32fa1
commit f8a990a0a1
3 changed files with 41 additions and 17 deletions

View File

@@ -30,26 +30,16 @@ static int property_get_monotonic_timers(
return r;
LIST_FOREACH(value, v, t->values) {
_cleanup_free_ char *buf = NULL;
const char *s;
size_t l;
_cleanup_free_ char *usec = NULL;
if (v->base == TIMER_CALENDAR)
continue;
s = timer_base_to_string(v->base);
assert(endswith(s, "Sec"));
/* s/Sec/USec/ */
l = strlen(s);
buf = new(char, l+2);
if (!buf)
usec = timer_base_to_usec_string(v->base);
if (!usec)
return -ENOMEM;
memcpy(buf, s, l-3);
memcpy(buf+l-3, "USec", 5);
r = sd_bus_message_append(reply, "(stt)", buf, v->value, v->next_elapse);
r = sd_bus_message_append(reply, "(stt)", usec, v->value, v->next_elapse);
if (r < 0)
return r;
}
@@ -108,9 +98,7 @@ static int property_get_next_elapse_monotonic(
assert(bus);
assert(reply);
return sd_bus_message_append(reply, "t",
(uint64_t) usec_shift_clock(t->next_elapse_monotonic_or_boottime,
TIMER_MONOTONIC_CLOCK(t), CLOCK_MONOTONIC));
return sd_bus_message_append(reply, "t", timer_next_elapse_monotonic(t));
}
const sd_bus_vtable bus_timer_vtable[] = {

View File

@@ -1001,6 +1001,13 @@ static int activation_details_timer_append_pair(ActivationDetails *details, char
return 2; /* Return the number of pairs added to the env block */
}
uint64_t timer_next_elapse_monotonic(const Timer *t) {
assert(t);
return (uint64_t) usec_shift_clock(t->next_elapse_monotonic_or_boottime,
TIMER_MONOTONIC_CLOCK(t), CLOCK_MONOTONIC);
}
static const char* const timer_base_table[_TIMER_BASE_MAX] = {
[TIMER_ACTIVE] = "OnActiveSec",
[TIMER_BOOT] = "OnBootSec",
@@ -1012,6 +1019,31 @@ static const char* const timer_base_table[_TIMER_BASE_MAX] = {
DEFINE_STRING_TABLE_LOOKUP(timer_base, TimerBase);
char* timer_base_to_usec_string(TimerBase i) {
_cleanup_free_ char *buf = NULL;
const char *s;
size_t l;
s = timer_base_to_string(i);
if (endswith(s, "Sec")) {
/* s/Sec/USec/ */
l = strlen(s);
buf = new(char, l+2);
if (!buf)
return NULL;
memcpy(buf, s, l-3);
memcpy(buf+l-3, "USec", 5);
} else {
buf = strdup(s);
if (!buf)
return NULL;
}
return TAKE_PTR(buf);
}
static const char* const timer_result_table[_TIMER_RESULT_MAX] = {
[TIMER_SUCCESS] = "success",
[TIMER_FAILURE_RESOURCES] = "resources",

View File

@@ -72,6 +72,8 @@ struct ActivationDetailsTimer {
#define TIMER_MONOTONIC_CLOCK(t) ((t)->wake_system ? CLOCK_BOOTTIME_ALARM : CLOCK_MONOTONIC)
uint64_t timer_next_elapse_monotonic(const Timer *t);
void timer_free_values(Timer *t);
extern const UnitVTable timer_vtable;
@@ -80,6 +82,8 @@ extern const ActivationDetailsVTable activation_details_timer_vtable;
const char *timer_base_to_string(TimerBase i) _const_;
TimerBase timer_base_from_string(const char *s) _pure_;
char* timer_base_to_usec_string(TimerBase i);
const char* timer_result_to_string(TimerResult i) _const_;
TimerResult timer_result_from_string(const char *s) _pure_;