| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-json.h" |
| 4 |
|
| 5 |
#include "calendarspec.h" |
| 6 |
#include "json-util.h" |
| 7 |
#include "timer.h" |
| 8 |
#include "varlink-timer.h" |
| 9 |
|
| 10 |
static int timers_build_json(sd_json_variant **ret, const char *name, void *userdata) { |
| 11 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; |
| 12 |
TimerValue *values = userdata; |
| 13 |
int r; |
| 14 |
|
| 15 |
assert(ret); |
| 16 |
|
| 17 |
LIST_FOREACH(value, value, values) { |
| 18 |
_cleanup_free_ char *base_name = NULL; |
| 19 |
|
| 20 |
base_name = timer_base_to_usec_string(value->base); |
| 21 |
if (!base_name) |
| 22 |
return -ENOMEM; |
| 23 |
|
| 24 |
if (value->base == TIMER_CALENDAR) { |
| 25 |
_cleanup_free_ char *calendar = NULL; |
| 26 |
|
| 27 |
r = calendar_spec_to_string(value->calendar_spec, &calendar); |
| 28 |
if (r < 0) |
| 29 |
return log_debug_errno(r, "Failed to convert calendar spec into string: %m"); |
| 30 |
|
| 31 |
r = sd_json_variant_append_arraybo( |
| 32 |
&v, |
| 33 |
JSON_BUILD_PAIR_ENUM("base", base_name), |
| 34 |
SD_JSON_BUILD_PAIR_STRING("calendar", calendar)); |
| 35 |
} else |
| 36 |
r = sd_json_variant_append_arraybo( |
| 37 |
&v, |
| 38 |
JSON_BUILD_PAIR_ENUM("base", base_name), |
| 39 |
SD_JSON_BUILD_PAIR_UNSIGNED("usec", value->value)); |
| 40 |
if (r < 0) |
| 41 |
return r; |
| 42 |
} |
| 43 |
|
| 44 |
*ret = TAKE_PTR(v); |
| 45 |
return 0; |
| 46 |
} |
| 47 |
|
| 48 |
int timer_context_build_json(sd_json_variant **ret, const char *name, void *userdata) { |
| 49 |
Timer *t = ASSERT_PTR(TIMER(userdata)); |
| 50 |
Unit *trigger = UNIT_TRIGGER(UNIT(t)); |
| 51 |
|
| 52 |
return sd_json_buildo( |
| 53 |
ASSERT_PTR(ret), |
| 54 |
JSON_BUILD_PAIR_CALLBACK_NON_NULL("Timers", timers_build_json, t->values), |
| 55 |
JSON_BUILD_PAIR_STRING_NON_EMPTY("Unit", trigger ? trigger->id : NULL), |
| 56 |
SD_JSON_BUILD_PAIR_BOOLEAN("OnClockChange", t->on_clock_change), |
| 57 |
SD_JSON_BUILD_PAIR_BOOLEAN("OnTimezoneChange", t->on_timezone_change), |
| 58 |
JSON_BUILD_PAIR_FINITE_USEC("AccuracyUSec", t->accuracy_usec), |
| 59 |
JSON_BUILD_PAIR_FINITE_USEC_NON_ZERO("RandomizedDelayUSec", t->random_delay_usec), |
| 60 |
JSON_BUILD_PAIR_FINITE_USEC_NON_ZERO("RandomizedOffsetUSec", t->random_offset_usec), |
| 61 |
SD_JSON_BUILD_PAIR_BOOLEAN("FixedRandomDelay", t->fixed_random_delay), |
| 62 |
SD_JSON_BUILD_PAIR_BOOLEAN("Persistent", t->persistent), |
| 63 |
SD_JSON_BUILD_PAIR_BOOLEAN("WakeSystem", t->wake_system), |
| 64 |
SD_JSON_BUILD_PAIR_BOOLEAN("RemainAfterElapse", t->remain_after_elapse), |
| 65 |
SD_JSON_BUILD_PAIR_BOOLEAN("DeferReactivation", t->defer_reactivation)); |
| 66 |
} |
| 67 |
|
| 68 |
int timer_runtime_build_json(sd_json_variant **ret, const char *name, void *userdata) { |
| 69 |
Timer *t = ASSERT_PTR(TIMER(userdata)); |
| 70 |
|
| 71 |
return sd_json_buildo( |
| 72 |
ASSERT_PTR(ret), |
| 73 |
JSON_BUILD_PAIR_ENUM("Result", timer_result_to_string(t->result)), |
| 74 |
JSON_BUILD_PAIR_FINITE_USEC("NextElapseUSecRealtime", t->next_elapse_realtime), |
| 75 |
JSON_BUILD_PAIR_FINITE_USEC("NextElapseUSecMonotonic", timer_next_elapse_monotonic(t)), |
| 76 |
JSON_BUILD_PAIR_DUAL_TIMESTAMP_NON_NULL("LastTriggerUSec", &t->last_trigger)); |
| 77 |
} |