2016-04-03 17:19:19 +02:00
|
|
|
/****************************************************************************
|
2021-05-29 21:59:38 +02:00
|
|
|
* apps/examples/alarm/alarm_main.c
|
2016-04-03 17:19:19 +02:00
|
|
|
*
|
2021-06-15 09:09:58 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2016-04-03 17:19:19 +02:00
|
|
|
*
|
2021-06-15 09:09:58 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2016-04-03 17:19:19 +02:00
|
|
|
*
|
2021-06-15 09:09:58 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2016-04-03 17:19:19 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sched.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <nuttx/timers/rtc.h>
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static bool g_alarm_daemon_started;
|
2017-10-17 18:43:55 +02:00
|
|
|
static pid_t g_alarm_daemon_pid;
|
2016-04-03 17:19:19 +02:00
|
|
|
static bool g_alarm_received[CONFIG_RTC_NALARMS];
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: alarm_handler
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void alarm_handler(int signo, FAR siginfo_t *info, FAR void *ucontext)
|
|
|
|
{
|
|
|
|
int almndx = info->si_value.sival_int;
|
|
|
|
if (almndx >= 0 && almndx < CONFIG_RTC_NALARMS)
|
|
|
|
{
|
|
|
|
g_alarm_received[almndx] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: alarm_daemon
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int alarm_daemon(int argc, FAR char *argv[])
|
|
|
|
{
|
|
|
|
struct sigaction act;
|
|
|
|
sigset_t set;
|
|
|
|
int ret;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Indicate that we are running */
|
|
|
|
|
|
|
|
g_alarm_daemon_started = true;
|
|
|
|
printf("alarm_daemon: Running\n");
|
|
|
|
|
|
|
|
/* Make sure that the alarm signal is unmasked */
|
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
sigemptyset(&set);
|
|
|
|
sigaddset(&set, CONFIG_EXAMPLES_ALARM_SIGNO);
|
2016-04-03 17:19:19 +02:00
|
|
|
ret = sigprocmask(SIG_UNBLOCK, &set, NULL);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
|
|
|
int errcode = errno;
|
|
|
|
fprintf(stderr, "ERROR: sigprocmask failed: %d\n",
|
|
|
|
errcode);
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Register alarm signal handler */
|
|
|
|
|
|
|
|
act.sa_sigaction = alarm_handler;
|
|
|
|
act.sa_flags = SA_SIGINFO;
|
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
sigfillset(&act.sa_mask);
|
|
|
|
sigdelset(&act.sa_mask, CONFIG_EXAMPLES_ALARM_SIGNO);
|
2016-04-03 17:19:19 +02:00
|
|
|
|
|
|
|
ret = sigaction(CONFIG_EXAMPLES_ALARM_SIGNO, &act, NULL);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
int errcode = errno;
|
|
|
|
fprintf(stderr, "ERROR: sigaction failed: %d\n",
|
|
|
|
errcode);
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now loop forever, waiting for alarm signals */
|
|
|
|
|
|
|
|
for (; ; )
|
|
|
|
{
|
|
|
|
/* Check if any alarms fired.
|
|
|
|
*
|
|
|
|
* NOTE that there are race conditions here... if we missing an alarm,
|
|
|
|
* we will just report it a half second late.
|
|
|
|
*/
|
|
|
|
|
|
|
|
for (i = 0; i < CONFIG_RTC_NALARMS; i++)
|
|
|
|
{
|
|
|
|
if (g_alarm_received[i])
|
|
|
|
{
|
2021-05-29 21:59:38 +02:00
|
|
|
printf("alarm_daemon: alarm %d received\n", i) ;
|
2016-04-03 17:19:19 +02:00
|
|
|
g_alarm_received[i] = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now wait a little while and poll again. If a signal is received
|
2020-02-23 05:51:44 +01:00
|
|
|
* this should cause us to awken earlier.
|
2016-04-03 17:19:19 +02:00
|
|
|
*/
|
|
|
|
|
2021-05-29 21:59:38 +02:00
|
|
|
usleep(500 * 1000L);
|
2016-04-03 17:19:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
errout:
|
|
|
|
g_alarm_daemon_started = false;
|
|
|
|
|
|
|
|
printf("alarm_daemon: Terminating\n");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: start_daemon
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int start_daemon(void)
|
|
|
|
{
|
|
|
|
if (!g_alarm_daemon_started)
|
|
|
|
{
|
2017-10-17 18:43:55 +02:00
|
|
|
g_alarm_daemon_pid =
|
2016-04-03 17:19:19 +02:00
|
|
|
task_create("alarm_daemon", CONFIG_EXAMPLES_ALARM_PRIORITY,
|
|
|
|
CONFIG_EXAMPLES_ALARM_STACKSIZE, alarm_daemon,
|
2017-05-09 18:59:24 +02:00
|
|
|
NULL);
|
2017-10-17 18:43:55 +02:00
|
|
|
if (g_alarm_daemon_pid < 0)
|
2016-04-03 17:19:19 +02:00
|
|
|
{
|
|
|
|
int errcode = errno;
|
|
|
|
fprintf(stderr, "ERROR: Failed to start alarm_daemon: %d\n",
|
|
|
|
errcode);
|
|
|
|
return -errcode;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("alarm_daemon started\n");
|
2021-05-29 21:59:38 +02:00
|
|
|
usleep(500 * 1000L);
|
2016-04-03 17:19:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: show_usage
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void show_usage(FAR const char *progname)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "USAGE:\n");
|
2017-10-17 18:43:55 +02:00
|
|
|
fprintf(stderr, "\t%s [-a <alarmid>] [-cr] [<seconds>]\n", progname);
|
2016-04-03 17:19:19 +02:00
|
|
|
fprintf(stderr, "Where:\n");
|
|
|
|
fprintf(stderr, "\t-a <alarmid>\n");
|
2017-10-17 18:43:55 +02:00
|
|
|
fprintf(stderr, "\t\t<alarmid> selects the alarm: 0..%d (default: 0)\n",
|
2016-04-03 17:19:19 +02:00
|
|
|
CONFIG_RTC_NALARMS - 1);
|
2017-10-17 18:43:55 +02:00
|
|
|
fprintf(stderr, "\t-c\tCancel previously set alarm\n");
|
|
|
|
fprintf(stderr, "\t-r\tRead previously set alarm\n");
|
2016-04-03 17:19:19 +02:00
|
|
|
fprintf(stderr, "\t<seconds>\n");
|
|
|
|
fprintf(stderr, "\t\tThe number of seconds until the alarm expires.\n");
|
2017-10-17 18:43:55 +02:00
|
|
|
fprintf(stderr, "\t\t(only if no -c or -r option given.)\n");
|
2016-04-03 17:19:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* alarm_main
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int main(int argc, FAR char *argv[])
|
|
|
|
{
|
2017-10-17 18:43:55 +02:00
|
|
|
unsigned long seconds = 0;
|
|
|
|
bool badarg = false;
|
|
|
|
bool readmode = false;
|
|
|
|
bool cancelmode = false;
|
|
|
|
bool setmode;
|
|
|
|
int opt;
|
2016-04-03 17:19:19 +02:00
|
|
|
int alarmid = 0;
|
|
|
|
int fd;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Make sure that the alarm daemon is running */
|
|
|
|
|
|
|
|
ret = start_daemon();
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2021-05-29 21:59:38 +02:00
|
|
|
/* Parse commandline parameters. NOTE: getopt() is not thread safe nor
|
|
|
|
* re-entrant.
|
2017-10-17 18:43:55 +02:00
|
|
|
* To keep its state proper for the next usage, it is necessary to parse to
|
|
|
|
* the end of the line even if an error occurs. If an error occurs, this
|
|
|
|
* logic just sets 'badarg' and continues.
|
|
|
|
*/
|
2016-04-03 17:19:19 +02:00
|
|
|
|
2017-10-17 18:43:55 +02:00
|
|
|
while ((opt = getopt(argc, argv, ":a:cr")) != ERROR)
|
2016-04-03 17:19:19 +02:00
|
|
|
{
|
2017-10-17 18:43:55 +02:00
|
|
|
switch (opt)
|
2016-04-03 17:19:19 +02:00
|
|
|
{
|
2017-10-17 18:43:55 +02:00
|
|
|
case 'a': /* -a: Select alarm id */
|
|
|
|
alarmid = strtol(optarg, NULL, 0);
|
|
|
|
if (alarmid < 0 || alarmid >= CONFIG_RTC_NALARMS)
|
|
|
|
{
|
|
|
|
badarg = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
cancelmode = true;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
readmode = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "<unknown parameter '-%c'>\n\n", opt);
|
2021-05-29 21:59:38 +02:00
|
|
|
|
2017-10-17 18:43:55 +02:00
|
|
|
/* fall through */
|
2021-05-29 21:59:38 +02:00
|
|
|
|
2017-10-17 18:43:55 +02:00
|
|
|
case '?':
|
|
|
|
case ':':
|
|
|
|
badarg = true;
|
2016-04-03 17:19:19 +02:00
|
|
|
}
|
2017-10-17 18:43:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Both -r and -c can be given at the same time, setting is exclusive. */
|
2016-04-03 17:19:19 +02:00
|
|
|
|
2017-10-17 18:43:55 +02:00
|
|
|
setmode = !readmode && !cancelmode;
|
|
|
|
|
|
|
|
if (setmode)
|
|
|
|
{
|
|
|
|
if (optind >= argc)
|
|
|
|
{
|
|
|
|
badarg = true;
|
|
|
|
}
|
2016-04-03 17:19:19 +02:00
|
|
|
}
|
2017-10-17 18:43:55 +02:00
|
|
|
else if (optind < argc)
|
2016-04-03 17:19:19 +02:00
|
|
|
{
|
2017-10-17 18:43:55 +02:00
|
|
|
badarg = true;
|
2016-04-03 17:19:19 +02:00
|
|
|
}
|
2017-10-17 18:43:55 +02:00
|
|
|
|
|
|
|
if (badarg)
|
2016-04-03 17:19:19 +02:00
|
|
|
{
|
|
|
|
show_usage(argv[0]);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2017-10-17 18:43:55 +02:00
|
|
|
if (setmode)
|
2016-04-03 17:19:19 +02:00
|
|
|
{
|
2017-10-17 18:43:55 +02:00
|
|
|
/* Get the number of seconds until the alarm expiration */
|
|
|
|
|
|
|
|
seconds = strtoul(argv[optind], NULL, 10);
|
|
|
|
if (seconds < 1)
|
|
|
|
{
|
2021-05-29 21:59:38 +02:00
|
|
|
fprintf(stderr, "ERROR: Invalid number of seconds: %lu\n",
|
|
|
|
seconds);
|
2017-10-17 18:43:55 +02:00
|
|
|
show_usage(argv[0]);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2016-04-03 17:19:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Open the RTC driver */
|
|
|
|
|
|
|
|
printf("Opening %s\n", CONFIG_EXAMPLES_ALARM_DEVPATH);
|
2016-06-02 14:58:45 +02:00
|
|
|
|
2016-04-03 17:19:19 +02:00
|
|
|
fd = open(CONFIG_EXAMPLES_ALARM_DEVPATH, O_WRONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
{
|
|
|
|
int errcode = errno;
|
|
|
|
fprintf(stderr, "ERROR: Failed to open %s: %d\n",
|
|
|
|
CONFIG_EXAMPLES_ALARM_DEVPATH, errcode);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2017-10-17 18:43:55 +02:00
|
|
|
if (readmode)
|
|
|
|
{
|
2021-05-29 21:59:38 +02:00
|
|
|
struct rtc_rdalarm_s rd =
|
|
|
|
{
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
2017-10-17 18:43:55 +02:00
|
|
|
long timeleft;
|
|
|
|
time_t now;
|
2016-04-03 17:19:19 +02:00
|
|
|
|
2017-10-17 18:43:55 +02:00
|
|
|
rd.id = alarmid;
|
|
|
|
time(&now);
|
2016-04-03 17:19:19 +02:00
|
|
|
|
2017-10-17 18:43:55 +02:00
|
|
|
ret = ioctl(fd, RTC_RD_ALARM, (unsigned long)((uintptr_t)&rd));
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
int errcode = errno;
|
2016-04-03 17:19:19 +02:00
|
|
|
|
2017-10-17 18:43:55 +02:00
|
|
|
fprintf(stderr, "ERROR: RTC_RD_ALARM ioctl failed: %d\n",
|
|
|
|
errcode);
|
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
close(fd);
|
2017-10-17 18:43:55 +02:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Some of the NuttX RTC implementations do not support alarms
|
|
|
|
* longer than one month. There RTC_RD_ALARM can return partial
|
|
|
|
* calendar values without month and year fields.
|
|
|
|
* TODO: fix this in lower layers?
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (rd.time.tm_year > 0)
|
|
|
|
{
|
|
|
|
/* Normal sane case, assuming we did not actually request an
|
|
|
|
* alarm expiring in year 1900.
|
|
|
|
*/
|
|
|
|
|
|
|
|
timeleft = mktime((struct tm *)&rd.time) - now;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
struct tm now_tm;
|
|
|
|
|
|
|
|
/* Periodic extend "partial" alarms by "unfolding" months,
|
|
|
|
* until we get alarm that is in future. Note that mktime()
|
|
|
|
* normalizes fields that are out of their valid values,
|
|
|
|
* so we don't have to handle carry to tm_year by ourselves.
|
|
|
|
*/
|
|
|
|
|
|
|
|
gmtime_r(&now, &now_tm);
|
|
|
|
rd.time.tm_mon = now_tm.tm_mon;
|
|
|
|
rd.time.tm_year = now_tm.tm_year;
|
|
|
|
|
2021-05-29 21:59:38 +02:00
|
|
|
do
|
|
|
|
{
|
2017-10-17 18:43:55 +02:00
|
|
|
timeleft = mktime((struct tm *)&rd.time) - now;
|
|
|
|
if (timeleft < 0)
|
|
|
|
{
|
|
|
|
rd.time.tm_mon++;
|
|
|
|
}
|
2021-05-29 21:59:38 +02:00
|
|
|
}
|
|
|
|
while (timeleft < 0);
|
2017-10-17 18:43:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
printf("Alarm %d is %s with %ld seconds to expiration\n", alarmid,
|
|
|
|
rd.active ? "active" : "inactive", timeleft);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cancelmode)
|
2016-04-03 17:19:19 +02:00
|
|
|
{
|
2017-10-17 18:43:55 +02:00
|
|
|
ret = ioctl(fd, RTC_CANCEL_ALARM, (unsigned long)alarmid);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
int errcode = errno;
|
2016-06-02 14:58:45 +02:00
|
|
|
|
2017-10-17 18:43:55 +02:00
|
|
|
fprintf(stderr, "ERROR: RTC_CANCEL_ALARM ioctl failed: %d\n",
|
|
|
|
errcode);
|
2016-06-02 14:58:45 +02:00
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
close(fd);
|
2017-10-17 18:43:55 +02:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Alarm %d has been canceled\n", alarmid);
|
2016-04-03 17:19:19 +02:00
|
|
|
}
|
|
|
|
|
2017-10-17 18:43:55 +02:00
|
|
|
if (setmode)
|
|
|
|
{
|
|
|
|
struct rtc_setrelative_s setrel;
|
|
|
|
|
|
|
|
/* Set the alarm */
|
|
|
|
|
|
|
|
setrel.id = alarmid;
|
|
|
|
setrel.pid = g_alarm_daemon_pid;
|
|
|
|
setrel.reltime = (time_t)seconds;
|
|
|
|
|
2019-01-27 16:48:29 +01:00
|
|
|
setrel.event.sigev_notify = SIGEV_SIGNAL;
|
|
|
|
setrel.event.sigev_signo = CONFIG_EXAMPLES_ALARM_SIGNO;
|
|
|
|
setrel.event.sigev_value.sival_int = alarmid;
|
2017-10-17 18:43:55 +02:00
|
|
|
|
|
|
|
ret = ioctl(fd, RTC_SET_RELATIVE, (unsigned long)((uintptr_t)&setrel));
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
int errcode = errno;
|
|
|
|
|
|
|
|
fprintf(stderr, "ERROR: RTC_SET_RELATIVE ioctl failed: %d\n",
|
|
|
|
errcode);
|
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
close(fd);
|
2017-10-17 18:43:55 +02:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Alarm %d set in %lu seconds\n", alarmid, seconds);
|
|
|
|
}
|
2016-06-02 14:58:45 +02:00
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
close(fd);
|
2016-04-03 17:19:19 +02:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|