nuttx-apps/examples/oneshot/oneshot_main.c
Xiang Xiao f9f59bd0f8 !apps: drop redundant casts on tv_sec/tv_nsec and fix printf formats
Now that time_t is unconditionally 64-bit (signed int64_t) and the
struct timespec fields tv_sec / tv_nsec are wide enough on their own,
the explicit (uint64_t)/(int64_t)/(int) casts that used to guard the
multiplications and subtractions in *_us / *_ms / *_ns helpers are no
longer needed.  Drop them to keep the timekeeping math readable.

In the same spirit, this commit also normalises the printf-style format
specifiers and casts used to print tv_sec / tv_nsec / tv_usec values.
The prior code was a mix of "%d"/"%u"/"%ld"/"%lu"/"%lld" with matching
(int)/(unsigned long)/(long long) casts; some formats truncated time_t
on 32-bit hosts, others mismatched signedness or width.  Replace all
such cases with the portable POSIX-recommended forms:

  - tv_sec  (time_t,       signed, impl-defined width) -> %jd  + (intmax_t)
  - tv_nsec (long,         signed)                     -> %ld  (no cast)
  - tv_usec (suseconds_t / long)                       -> %ld  (no cast)

Also drop two stale `(FAR const time_t *)&ts.tv_sec` casts that are
unnecessary now that ts.tv_sec is plain time_t.

Arithmetic-cleanup files (existing scope):

  - benchmarks/cyclictest/cyclictest.c:        timediff_us()
  - benchmarks/sd_bench/sd_bench_main.c:       get_time_delta_us()
  - examples/oneshot/oneshot_main.c:           maxus computation
  - examples/watchdog/watchdog_main.c:         current_time_ms (x2)
  - industry/nxmodbus/nxmb_internal.h:         nxmb_util_clock_ms()
  - netutils/ntpclient/ntpclient.c:            timespec2ntp()
  - netutils/ptpd/ptpd.c:                      ptp_adjtime()
  - system/dd/dd_main.c:                       elapsed accounting
  - testing/drivers/drivertest/drivertest_posix_timer.c:
                                               get_timestamp()
  - testing/drivers/sd_stress/sd_stress_main.c:get_time_delta()
  - testing/sched/getprime/getprime_main.c:    elapsed accounting
  - testing/sched/pthread_mutex_perf/pthread_mutex_perf.c:
                                               timespec_avg()

Printf-format-fix files (new in this revision):

  - examples/adjtime/adjtime_main.c
  - examples/charger/charger_main.c
  - examples/netpkt/netpkt_ethercat.c
  - fsutils/mkfatfs/mkfatfs.c
  - graphics/tiff/tiff_initialize.c
  - netutils/ptpd/ptpd.c
  - nshlib/nsh_timcmds.c
  - system/coredump/coredump.c
  - system/ptpd/ptpd_main.c
  - testing/drivers/drivertest/drivertest_oneshot.c
  - testing/mm/kasantest/kasantest.c
  - testing/ostest/semtimed.c
  - testing/sched/pthread_mutex_perf/pthread_mutex_perf.c
  - testing/sched/timerjitter/timerjitter.c
  - testing/testsuites/kernel/time/cases/clock_test_clock01.c
  - testing/testsuites/kernel/time/cases/clock_test_smoke.c

No behavioural change.

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2026-05-22 13:38:25 +08:00

261 lines
7.3 KiB
C

/****************************************************************************
* apps/examples/oneshot/oneshot_main.c
*
* SPDX-License-Identifier: Apache-2.0
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <fcntl.h>
#include <time.h>
#include <errno.h>
#include <unistd.h>
#include <nuttx/clock.h>
#include <nuttx/timers/oneshot.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#ifndef CONFIG_EXAMPLES_ONESHOT_DEVNAME
# define CONFIG_EXAMPLES_ONESHOT_DEVNAME "/dev/oneshot"
#endif
#ifndef CONFIG_EXAMPLES_ONESHOT_DELAY
# define CONFIG_EXAMPLES_ONESHOT_DELAY 100000
#endif
#ifndef CONFIG_EXAMPLES_ONESHOT_SIGNO
# define CONFIG_EXAMPLES_ONESHOT_SIGNO 32
#endif
/* For long delays that have to be broken into segments, some loss of
* precision is expected due to interrupt and context switching overhead.
* This inaccuracy will cause somewhat longer times than the time requested
* due to this overhead. It might be possible to reduce this inaccuracy
* some by (1) making this task very high priority so that it runs in a more
* deterministic way, and (2) by subtracting a "fudge factor" to account for
* time lost due to the overhead.
*/
#define FUDGE_FACTOR 10
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: oneshot_main
****************************************************************************/
static void show_usage(FAR const char *progname)
{
fprintf(stderr, "USAGE: %s [-d <usecs>] [<devname>]\n", progname);
fprintf(stderr, "Where:\n");
fprintf(stderr, "\t-d <usecs>:\n");
fprintf(stderr, "\tSpecifies the oneshot delay in microseconds."
" Default %ld\n", (unsigned long)CONFIG_EXAMPLES_ONESHOT_DELAY);
fprintf(stderr, "\t<devname>:\n");
fprintf(stderr, "\tSpecifies the path to the oneshot driver. Default %s\n",
CONFIG_EXAMPLES_ONESHOT_DEVNAME);
exit(EXIT_FAILURE);
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: oneshot_main
****************************************************************************/
int main(int argc, FAR char *argv[])
{
FAR const char *devname = CONFIG_EXAMPLES_ONESHOT_DEVNAME;
unsigned long usecs = CONFIG_EXAMPLES_ONESHOT_DELAY;
unsigned long secs;
struct oneshot_start_s start;
struct timespec ts;
uint64_t maxus;
sigset_t set;
int ret;
int fd;
/* USAGE: nsh> oneshot [-d <usecs>] [<devname>] */
if (argc > 1)
{
if (argc == 2)
{
/* FORM: nsh> oneshot [<devname>] */
devname = argv[1];
}
else if (argc < 5)
{
/* FORM: nsh> oneshot [-d <usecs>] */
if (strcmp(argv[1], "-d") != 0)
{
fprintf(stderr, "ERROR: Unrecognized option: %s\n", argv[1]);
show_usage(argv[0]);
}
usecs = strtoul(argv[2], NULL, 10);
if (argc == 4)
{
/* FORM: nsh> oneshot [-d <usecs>] [<devname>] */
devname = argv[3];
}
}
else
{
fprintf(stderr, "ERROR: Unsupported number of arguments: %d\n",
argc);
show_usage(argv[0]);
}
}
/* Open the oneshot device */
printf("Opening %s\n", devname);
fd = open(devname, O_RDONLY);
if (fd < 0)
{
fprintf(stderr, "ERROR: Failed to open %s: %d\n",
devname, errno);
return EXIT_FAILURE;
}
/* Get the maximum delay */
ret = ioctl(fd, OSIOC_MAXDELAY, (unsigned long)((uintptr_t)&ts));
if (ret < 0)
{
fprintf(stderr, "ERROR: Failed to get the maximum delayl: %d\n",
errno);
close(fd);
return EXIT_FAILURE;
}
maxus = ts.tv_sec * USEC_PER_SEC +
ts.tv_nsec / NSEC_PER_USEC;
printf("Maximum delay is %" PRIu64 "\n", maxus);
/* Ignore the default signal action */
signal(CONFIG_EXAMPLES_ONESHOT_SIGNO, SIG_IGN);
/* Loop waiting until the full delay expires */
while (usecs > 0)
{
/* Start the oneshot timer */
sigemptyset(&set);
sigaddset(&set, CONFIG_EXAMPLES_ONESHOT_SIGNO);
if (usecs < maxus)
{
/* Wait for the remaining time */
printf("Starting oneshot timer with delay %lu microseconds\n",
usecs);
start.pid = 0;
secs = usecs / USEC_PER_SEC;
usecs -= USEC_PER_SEC * secs;
start.ts.tv_sec = secs;
start.ts.tv_nsec = usecs * NSEC_PER_USEC;
start.event.sigev_notify = SIGEV_SIGNAL;
start.event.sigev_signo = CONFIG_EXAMPLES_ONESHOT_SIGNO;
start.event.sigev_value.sival_ptr = NULL;
/* Zero usecs to terminate the loop */
usecs = 0;
}
else
{
/* Wait for the maximum */
printf("Starting oneshot timer with delay %" PRId64
" microseconds\n", maxus);
start.ts.tv_sec = ts.tv_sec;
start.ts.tv_nsec = ts.tv_nsec;
usecs -= maxus;
#if FUDGE_FACTOR > 0
if (usecs > FUDGE_FACTOR)
{
usecs -= FUDGE_FACTOR;
}
else
{
usecs = 0;
}
#endif
}
ret = ioctl(fd, OSIOC_START, (unsigned long)((uintptr_t)&start));
if (ret < 0)
{
fprintf(stderr, "ERROR: Failed to start the oneshot: %d\n",
errno);
close(fd);
return EXIT_FAILURE;
}
/* Wait for the oneshot to fire */
printf("Waiting...\n");
ret = sigwaitinfo(&set, NULL);
if (ret < 0)
{
fprintf(stderr, "ERROR: sigwaitinfo failed: %d\n",
errno);
close(fd);
return EXIT_FAILURE;
}
}
/* Close the oneshot driver */
printf("Finished\n");
close(fd);
return EXIT_SUCCESS;
}