123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- #include "libbb.h"
- #include "rtc_.h"
- #define SYS_RTC_PATH "/sys/class/rtc/%s/device/power/wakeup"
- #define SYS_POWER_PATH "/sys/power/state"
- static NOINLINE bool may_wakeup(const char *rtcname)
- {
- ssize_t ret;
- char buf[128];
-
- rtcname = skip_dev_pfx(rtcname);
- snprintf(buf, sizeof(buf), SYS_RTC_PATH, rtcname);
- ret = open_read_close(buf, buf, sizeof(buf));
- if (ret < 0)
- return false;
-
- return is_prefixed_with(buf, "enabled\n") != NULL;
- }
- static NOINLINE void setup_alarm(int fd, time_t *wakeup, time_t rtc_time)
- {
- struct tm *ptm;
- struct linux_rtc_wkalrm wake;
-
- ptm = localtime(wakeup);
- wake.time.tm_sec = ptm->tm_sec;
- wake.time.tm_min = ptm->tm_min;
- wake.time.tm_hour = ptm->tm_hour;
- wake.time.tm_mday = ptm->tm_mday;
- wake.time.tm_mon = ptm->tm_mon;
- wake.time.tm_year = ptm->tm_year;
-
- wake.time.tm_wday = -1;
- wake.time.tm_yday = -1;
- wake.time.tm_isdst = -1;
-
- if ((rtc_time + (24 * 60 * 60)) > *wakeup) {
- xioctl(fd, RTC_ALM_SET, &wake.time);
- xioctl(fd, RTC_AIE_ON, 0);
- } else {
-
- wake.enabled = 1;
- xioctl(fd, RTC_WKALM_SET, &wake);
- }
- }
- #define RTCWAKE_OPT_AUTO 0x01
- #define RTCWAKE_OPT_LOCAL 0x02
- #define RTCWAKE_OPT_UTC 0x04
- #define RTCWAKE_OPT_DEVICE 0x08
- #define RTCWAKE_OPT_SUSPEND_MODE 0x10
- #define RTCWAKE_OPT_SECONDS 0x20
- #define RTCWAKE_OPT_TIME 0x40
- int rtcwake_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
- int rtcwake_main(int argc UNUSED_PARAM, char **argv)
- {
- unsigned opt;
- const char *rtcname = NULL;
- const char *suspend = "standby";
- const char *opt_seconds;
- const char *opt_time;
- time_t rtc_time;
- time_t sys_time;
- time_t alarm_time = alarm_time;
- unsigned seconds = seconds;
- int utc = -1;
- int fd;
- #if ENABLE_LONG_OPTS
- static const char rtcwake_longopts[] ALIGN1 =
- "auto\0" No_argument "a"
- "local\0" No_argument "l"
- "utc\0" No_argument "u"
- "device\0" Required_argument "d"
- "mode\0" Required_argument "m"
- "seconds\0" Required_argument "s"
- "time\0" Required_argument "t"
- ;
- #endif
- opt = getopt32long(argv,
-
- "^alud:m:s:t:" "\0" "s:t:s--t:t--s", rtcwake_longopts,
- &rtcname, &suspend, &opt_seconds, &opt_time);
-
- if (opt & (RTCWAKE_OPT_UTC | RTCWAKE_OPT_LOCAL))
- utc = opt & RTCWAKE_OPT_UTC;
- if (opt & RTCWAKE_OPT_SECONDS) {
-
- seconds = xatou(opt_seconds);
- } else {
-
-
- if (sizeof(alarm_time) <= sizeof(long))
- alarm_time = xatol(opt_time);
- else
- alarm_time = xatoll(opt_time);
- }
- if (utc == -1)
- utc = rtc_adjtime_is_utc();
-
- xchdir("/dev");
-
- fd = rtc_xopen(&rtcname, O_RDONLY);
- if (strcmp(suspend, "on") != 0)
- if (!may_wakeup(rtcname))
- bb_error_msg_and_die("%s not enabled for wakeup events", rtcname);
-
- sys_time = time(NULL);
- {
- struct tm tm_time;
- rtc_read_tm(&tm_time, fd);
- rtc_time = rtc_tm2time(&tm_time, utc);
- }
- if (opt & RTCWAKE_OPT_TIME) {
-
- alarm_time += rtc_time - sys_time;
- if (alarm_time < rtc_time)
-
- bb_error_msg_and_die("time doesn't go backward to %s", ctime(&alarm_time));
- } else
- alarm_time = rtc_time + seconds + 1;
- setup_alarm(fd, &alarm_time, rtc_time);
- sync();
- #if 0
- printf("sys_time: %s", ctime(&sys_time));
- printf("rtc_time: %s", ctime(&rtc_time));
- #endif
- printf("wakeup from \"%s\" at %s", suspend, ctime(&alarm_time));
- fflush_all();
- usleep(10 * 1000);
- if (strcmp(suspend, "on") != 0)
- xopen_xwrite_close(SYS_POWER_PATH, suspend);
- else {
-
- unsigned long data;
- do {
- ssize_t ret = safe_read(fd, &data, sizeof(data));
- if (ret < 0) {
- bb_simple_perror_msg("rtc read");
- break;
- }
- } while (!(data & RTC_AF));
- }
- xioctl(fd, RTC_AIE_OFF, 0);
- if (ENABLE_FEATURE_CLEAN_UP)
- close(fd);
- return EXIT_SUCCESS;
- }
|