diff --git a/src/c/gentlewake.c b/src/c/gentlewake.c new file mode 100644 index 0000000..7396190 --- /dev/null +++ b/src/c/gentlewake.c @@ -0,0 +1,30 @@ +// lol the code in this file is mostly stolen from https://github.com/SeaPea/GentleWake + +#include + +// A slightly more robust wakeup scheduler that retries on certain errors and +// can retry for a different wakeup time offset by retry_diff up to retry_max times +WakeupId wakeup_schedule_simple_robust(time_t wakeup_time, int8_t retry_diff, bool is_fallback) { + WakeupId result = 0; + uint8_t try_count = 0; + + result = wakeup_schedule(wakeup_time, is_fallback, true); + + if (result < 0) { + // If result is negative, something went wrong, so make sure all wakeups for this app are cancelled and try again + wakeup_cancel_all(); + + result = wakeup_schedule(wakeup_time, is_fallback, true); + + if (result == E_RANGE) { + // E_RANGE means some other app has the same wakeup time +/- 1 minute, so try to set + // the wakeup for a time up to (retry_diff * 5) minutes before/after. + while (result == E_RANGE && try_count++ < 5) { + wakeup_time += retry_diff; + result = wakeup_schedule(wakeup_time, is_fallback, true); + } + } + } + + return result; +} diff --git a/src/c/gentlewake.h b/src/c/gentlewake.h new file mode 100644 index 0000000..3a780a6 --- /dev/null +++ b/src/c/gentlewake.h @@ -0,0 +1,4 @@ +#pragma once +#include + +WakeupId wakeup_schedule_simple_robust(time_t wakeup_time, int8_t retry_diff, bool is_fallback);