nuttx/sched/clock/clock_settime.c

177 lines
4.9 KiB
C
Raw Normal View History

/****************************************************************************
* sched/clock/clock_settime.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 <time.h>
#include <assert.h>
#include <errno.h>
#include <nuttx/debug.h>
sched/clock: support using CLOCKFD to call clock_gettime/settime This patch implements POSIX-compliant CLOCKFD support, enabling user applications to access dynamic PTP clocks through file descriptors combined with clock_gettime() and clock_settime() system calls. Key changes include: 1. CLOCKFD macro support: - Added CLOCKFD() macro in include/nuttx/clock.h - Allows encoding file descriptor into clockid_t: CLOCKFD(fd) - Enables accessing PTP clocks via: clock_gettime(CLOCKFD(fd), &ts) 2. clock_gettime() enhancements: - Modified sched/clock/clock_gettime.c to detect CLOCKFD clockids - Extracts file descriptor from clockid using CLOCKFD_TO_FD() - Validates file descriptor and calls file_ioctl() with PTP_CLOCK_GETTIME - Falls back to standard clock handling for non-CLOCKFD clockids 3. clock_settime() enhancements: - Modified sched/clock/clock_settime.c to support CLOCKFD clockids - Extracts file descriptor and validates accessibility - Issues PTP_CLOCK_SETTIME ioctl to underlying PTP clock device - Maintains backward compatibility with standard POSIX clocks 4. File descriptor validation: - Checks file descriptor validity using fs_getfilep() - Ensures proper reference counting with fs_putfilep() - Returns appropriate error codes (EINVAL, EBADF) on failures 5. Integration with PTP clock framework: - Seamlessly integrates with PTP clock driver's ioctl interface - Enables standard POSIX clock API usage for dynamic PTP clocks - No changes required to existing applications using standard clocks Usage example: int fd = open("/dev/ptp0", O_RDONLY); struct timespec ts; clock_gettime(CLOCKFD(fd), &ts); /* Get PTP clock time */ clock_settime(CLOCKFD(fd), &ts); /* Set PTP clock time */ close(fd); This implementation follows the Linux kernel's PTP clock model, providing a familiar interface for developers working with PTP hardware. Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2025-03-20 18:20:26 +08:00
#include <sys/time.h>
#include <nuttx/arch.h>
sched/clock: support using CLOCKFD to call clock_gettime/settime This patch implements POSIX-compliant CLOCKFD support, enabling user applications to access dynamic PTP clocks through file descriptors combined with clock_gettime() and clock_settime() system calls. Key changes include: 1. CLOCKFD macro support: - Added CLOCKFD() macro in include/nuttx/clock.h - Allows encoding file descriptor into clockid_t: CLOCKFD(fd) - Enables accessing PTP clocks via: clock_gettime(CLOCKFD(fd), &ts) 2. clock_gettime() enhancements: - Modified sched/clock/clock_gettime.c to detect CLOCKFD clockids - Extracts file descriptor from clockid using CLOCKFD_TO_FD() - Validates file descriptor and calls file_ioctl() with PTP_CLOCK_GETTIME - Falls back to standard clock handling for non-CLOCKFD clockids 3. clock_settime() enhancements: - Modified sched/clock/clock_settime.c to support CLOCKFD clockids - Extracts file descriptor and validates accessibility - Issues PTP_CLOCK_SETTIME ioctl to underlying PTP clock device - Maintains backward compatibility with standard POSIX clocks 4. File descriptor validation: - Checks file descriptor validity using fs_getfilep() - Ensures proper reference counting with fs_putfilep() - Returns appropriate error codes (EINVAL, EBADF) on failures 5. Integration with PTP clock framework: - Seamlessly integrates with PTP clock driver's ioctl interface - Enables standard POSIX clock API usage for dynamic PTP clocks - No changes required to existing applications using standard clocks Usage example: int fd = open("/dev/ptp0", O_RDONLY); struct timespec ts; clock_gettime(CLOCKFD(fd), &ts); /* Get PTP clock time */ clock_settime(CLOCKFD(fd), &ts); /* Set PTP clock time */ close(fd); This implementation follows the Linux kernel's PTP clock model, providing a familiar interface for developers working with PTP hardware. Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2025-03-20 18:20:26 +08:00
#include <nuttx/fs/fs.h>
fs/timerfd: implement TFD_TIMER_CANCEL_ON_SET to detect clock changes Implement Linux-compatible TFD_TIMER_CANCEL_ON_SET flag for timerfd to allow applications to detect discontinuous changes to CLOCK_REALTIME. Background: According to Linux timerfd_create(2) man page, when a timerfd is created with CLOCK_REALTIME and TFD_TIMER_CANCEL_ON_SET flag is specified, the read() operation should fail with ECANCELED if the real-time clock undergoes a discontinuous change. This allows applications to detect and respond to system time changes. Implementation: 1. Add clock notifier infrastructure (clock_notifier.h/c) to notify interested parties when system time changes 2. Implement TFD_TIMER_CANCEL_ON_SET flag support in timerfd 3. Register timerfd with clock notifier when flag is set 4. Cancel timer and return ECANCELED when clock change is detected 5. Notify clock changes in: - clock_settime() - clock_initialize() - clock_timekeeping_set_wall_time() - rpmsg_rtc time synchronization Changes include: - New files: include/nuttx/clock_notifier.h, sched/clock/clock_notifier.c - Modified: fs/vfs/fs_timerfd.c to handle TFD_TIMER_CANCEL_ON_SET - Modified: clock subsystem to call notifier chain on time changes - Modified: rpmsg_rtc to notify time changes during sync Use case example: Applications using timerfd with absolute time can now detect when system time is adjusted (e.g., NTP sync, manual time change) and take appropriate action such as recalculating timeouts or updating scheduled events. Reference: https://man7.org/linux/man-pages/man2/timerfd_create.2.html Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2024-12-21 20:05:05 +08:00
#include <nuttx/clock_notifier.h>
#include <nuttx/irq.h>
#include <nuttx/spinlock.h>
sched/clock: support using CLOCKFD to call clock_gettime/settime This patch implements POSIX-compliant CLOCKFD support, enabling user applications to access dynamic PTP clocks through file descriptors combined with clock_gettime() and clock_settime() system calls. Key changes include: 1. CLOCKFD macro support: - Added CLOCKFD() macro in include/nuttx/clock.h - Allows encoding file descriptor into clockid_t: CLOCKFD(fd) - Enables accessing PTP clocks via: clock_gettime(CLOCKFD(fd), &ts) 2. clock_gettime() enhancements: - Modified sched/clock/clock_gettime.c to detect CLOCKFD clockids - Extracts file descriptor from clockid using CLOCKFD_TO_FD() - Validates file descriptor and calls file_ioctl() with PTP_CLOCK_GETTIME - Falls back to standard clock handling for non-CLOCKFD clockids 3. clock_settime() enhancements: - Modified sched/clock/clock_settime.c to support CLOCKFD clockids - Extracts file descriptor and validates accessibility - Issues PTP_CLOCK_SETTIME ioctl to underlying PTP clock device - Maintains backward compatibility with standard POSIX clocks 4. File descriptor validation: - Checks file descriptor validity using fs_getfilep() - Ensures proper reference counting with fs_putfilep() - Returns appropriate error codes (EINVAL, EBADF) on failures 5. Integration with PTP clock framework: - Seamlessly integrates with PTP clock driver's ioctl interface - Enables standard POSIX clock API usage for dynamic PTP clocks - No changes required to existing applications using standard clocks Usage example: int fd = open("/dev/ptp0", O_RDONLY); struct timespec ts; clock_gettime(CLOCKFD(fd), &ts); /* Get PTP clock time */ clock_settime(CLOCKFD(fd), &ts); /* Set PTP clock time */ close(fd); This implementation follows the Linux kernel's PTP clock model, providing a familiar interface for developers working with PTP hardware. Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2025-03-20 18:20:26 +08:00
#include <nuttx/timers/ptp_clock.h>
#include "clock/clock.h"
#ifdef CONFIG_CLOCK_TIMEKEEPING
2016-07-10 16:14:25 -06:00
# include "clock/clock_timekeeping.h"
#endif
#if defined(CONFIG_RTC) && defined(CONFIG_SCHED_LPWORK)
static struct work_s g_rtc_work;
static struct timespec g_rtc_to_set;
#endif
/****************************************************************************
sched/clock: support using CLOCKFD to call clock_gettime/settime This patch implements POSIX-compliant CLOCKFD support, enabling user applications to access dynamic PTP clocks through file descriptors combined with clock_gettime() and clock_settime() system calls. Key changes include: 1. CLOCKFD macro support: - Added CLOCKFD() macro in include/nuttx/clock.h - Allows encoding file descriptor into clockid_t: CLOCKFD(fd) - Enables accessing PTP clocks via: clock_gettime(CLOCKFD(fd), &ts) 2. clock_gettime() enhancements: - Modified sched/clock/clock_gettime.c to detect CLOCKFD clockids - Extracts file descriptor from clockid using CLOCKFD_TO_FD() - Validates file descriptor and calls file_ioctl() with PTP_CLOCK_GETTIME - Falls back to standard clock handling for non-CLOCKFD clockids 3. clock_settime() enhancements: - Modified sched/clock/clock_settime.c to support CLOCKFD clockids - Extracts file descriptor and validates accessibility - Issues PTP_CLOCK_SETTIME ioctl to underlying PTP clock device - Maintains backward compatibility with standard POSIX clocks 4. File descriptor validation: - Checks file descriptor validity using fs_getfilep() - Ensures proper reference counting with fs_putfilep() - Returns appropriate error codes (EINVAL, EBADF) on failures 5. Integration with PTP clock framework: - Seamlessly integrates with PTP clock driver's ioctl interface - Enables standard POSIX clock API usage for dynamic PTP clocks - No changes required to existing applications using standard clocks Usage example: int fd = open("/dev/ptp0", O_RDONLY); struct timespec ts; clock_gettime(CLOCKFD(fd), &ts); /* Get PTP clock time */ clock_settime(CLOCKFD(fd), &ts); /* Set PTP clock time */ close(fd); This implementation follows the Linux kernel's PTP clock model, providing a familiar interface for developers working with PTP hardware. Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2025-03-20 18:20:26 +08:00
* Private Functions
****************************************************************************/
#if defined(CONFIG_RTC) && defined(CONFIG_SCHED_LPWORK)
static void rtc_worker(FAR void *arg)
{
up_rtc_settime(arg);
}
#endif
sched/clock: support using CLOCKFD to call clock_gettime/settime This patch implements POSIX-compliant CLOCKFD support, enabling user applications to access dynamic PTP clocks through file descriptors combined with clock_gettime() and clock_settime() system calls. Key changes include: 1. CLOCKFD macro support: - Added CLOCKFD() macro in include/nuttx/clock.h - Allows encoding file descriptor into clockid_t: CLOCKFD(fd) - Enables accessing PTP clocks via: clock_gettime(CLOCKFD(fd), &ts) 2. clock_gettime() enhancements: - Modified sched/clock/clock_gettime.c to detect CLOCKFD clockids - Extracts file descriptor from clockid using CLOCKFD_TO_FD() - Validates file descriptor and calls file_ioctl() with PTP_CLOCK_GETTIME - Falls back to standard clock handling for non-CLOCKFD clockids 3. clock_settime() enhancements: - Modified sched/clock/clock_settime.c to support CLOCKFD clockids - Extracts file descriptor and validates accessibility - Issues PTP_CLOCK_SETTIME ioctl to underlying PTP clock device - Maintains backward compatibility with standard POSIX clocks 4. File descriptor validation: - Checks file descriptor validity using fs_getfilep() - Ensures proper reference counting with fs_putfilep() - Returns appropriate error codes (EINVAL, EBADF) on failures 5. Integration with PTP clock framework: - Seamlessly integrates with PTP clock driver's ioctl interface - Enables standard POSIX clock API usage for dynamic PTP clocks - No changes required to existing applications using standard clocks Usage example: int fd = open("/dev/ptp0", O_RDONLY); struct timespec ts; clock_gettime(CLOCKFD(fd), &ts); /* Get PTP clock time */ clock_settime(CLOCKFD(fd), &ts); /* Set PTP clock time */ close(fd); This implementation follows the Linux kernel's PTP clock model, providing a familiar interface for developers working with PTP hardware. Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2025-03-20 18:20:26 +08:00
static void nxclock_set_realtime(FAR const struct timespec *tp)
{
#ifndef CONFIG_CLOCK_TIMEKEEPING
struct timespec bias;
irqstate_t flags;
# ifdef CONFIG_CLOCK_ADJTIME
const struct timeval zerodelta = {
0, 0
};
# endif
/* Interrupts are disabled here so that the in-memory time
* representation and the RTC setting will be as close as
* possible.
*/
/* Get the elapsed time since power up (in milliseconds). This is a
* bias value that we need to use to correct the base time.
*/
clock_systime_timespec(&bias);
flags = spin_lock_irqsave(&g_basetime_lock);
clock_timespec_subtract(tp, &bias, &g_basetime);
fs/timerfd: implement TFD_TIMER_CANCEL_ON_SET to detect clock changes Implement Linux-compatible TFD_TIMER_CANCEL_ON_SET flag for timerfd to allow applications to detect discontinuous changes to CLOCK_REALTIME. Background: According to Linux timerfd_create(2) man page, when a timerfd is created with CLOCK_REALTIME and TFD_TIMER_CANCEL_ON_SET flag is specified, the read() operation should fail with ECANCELED if the real-time clock undergoes a discontinuous change. This allows applications to detect and respond to system time changes. Implementation: 1. Add clock notifier infrastructure (clock_notifier.h/c) to notify interested parties when system time changes 2. Implement TFD_TIMER_CANCEL_ON_SET flag support in timerfd 3. Register timerfd with clock notifier when flag is set 4. Cancel timer and return ECANCELED when clock change is detected 5. Notify clock changes in: - clock_settime() - clock_initialize() - clock_timekeeping_set_wall_time() - rpmsg_rtc time synchronization Changes include: - New files: include/nuttx/clock_notifier.h, sched/clock/clock_notifier.c - Modified: fs/vfs/fs_timerfd.c to handle TFD_TIMER_CANCEL_ON_SET - Modified: clock subsystem to call notifier chain on time changes - Modified: rpmsg_rtc to notify time changes during sync Use case example: Applications using timerfd with absolute time can now detect when system time is adjusted (e.g., NTP sync, manual time change) and take appropriate action such as recalculating timeouts or updating scheduled events. Reference: https://man7.org/linux/man-pages/man2/timerfd_create.2.html Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2024-12-21 20:05:05 +08:00
clock_notifier_call_chain(CLOCK_REALTIME, tp);
spin_unlock_irqrestore(&g_basetime_lock, flags);
/* Setup the RTC (lo- or high-res) */
# ifdef CONFIG_RTC
if (g_rtc_enabled)
{
# ifdef CONFIG_SCHED_LPWORK
/* Setting the current time in RTC may be a blocking operation (driver
* needs to wait for oscillator stabilization after reset and so on).
* This may cause the unwanted effect of clock_settime blocking the
* code execution for a considerable amount of time.
*
* The solution is to plan a low priority work that takes care
* of setting the time in RTC and let clock_settime continue. We don't
* have to check if the work is available, just cancel it if there
* is a new time set request.
*/
g_rtc_to_set = *tp;
work_queue(LPWORK, &g_rtc_work, rtc_worker,
(FAR void *)&g_rtc_to_set, 0);
# else
up_rtc_settime(tp);
# endif
}
# endif
# ifdef CONFIG_CLOCK_ADJTIME
/* Cancel any ongoing adjustment */
adjtime(&zerodelta, NULL);
# endif
#else
clock_timekeeping_set_wall_time(tp);
#endif
}
sched/clock: support using CLOCKFD to call clock_gettime/settime This patch implements POSIX-compliant CLOCKFD support, enabling user applications to access dynamic PTP clocks through file descriptors combined with clock_gettime() and clock_settime() system calls. Key changes include: 1. CLOCKFD macro support: - Added CLOCKFD() macro in include/nuttx/clock.h - Allows encoding file descriptor into clockid_t: CLOCKFD(fd) - Enables accessing PTP clocks via: clock_gettime(CLOCKFD(fd), &ts) 2. clock_gettime() enhancements: - Modified sched/clock/clock_gettime.c to detect CLOCKFD clockids - Extracts file descriptor from clockid using CLOCKFD_TO_FD() - Validates file descriptor and calls file_ioctl() with PTP_CLOCK_GETTIME - Falls back to standard clock handling for non-CLOCKFD clockids 3. clock_settime() enhancements: - Modified sched/clock/clock_settime.c to support CLOCKFD clockids - Extracts file descriptor and validates accessibility - Issues PTP_CLOCK_SETTIME ioctl to underlying PTP clock device - Maintains backward compatibility with standard POSIX clocks 4. File descriptor validation: - Checks file descriptor validity using fs_getfilep() - Ensures proper reference counting with fs_putfilep() - Returns appropriate error codes (EINVAL, EBADF) on failures 5. Integration with PTP clock framework: - Seamlessly integrates with PTP clock driver's ioctl interface - Enables standard POSIX clock API usage for dynamic PTP clocks - No changes required to existing applications using standard clocks Usage example: int fd = open("/dev/ptp0", O_RDONLY); struct timespec ts; clock_gettime(CLOCKFD(fd), &ts); /* Get PTP clock time */ clock_settime(CLOCKFD(fd), &ts); /* Set PTP clock time */ close(fd); This implementation follows the Linux kernel's PTP clock model, providing a familiar interface for developers working with PTP hardware. Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2025-03-20 18:20:26 +08:00
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: clock_settime
sched/clock: support using CLOCKFD to call clock_gettime/settime This patch implements POSIX-compliant CLOCKFD support, enabling user applications to access dynamic PTP clocks through file descriptors combined with clock_gettime() and clock_settime() system calls. Key changes include: 1. CLOCKFD macro support: - Added CLOCKFD() macro in include/nuttx/clock.h - Allows encoding file descriptor into clockid_t: CLOCKFD(fd) - Enables accessing PTP clocks via: clock_gettime(CLOCKFD(fd), &ts) 2. clock_gettime() enhancements: - Modified sched/clock/clock_gettime.c to detect CLOCKFD clockids - Extracts file descriptor from clockid using CLOCKFD_TO_FD() - Validates file descriptor and calls file_ioctl() with PTP_CLOCK_GETTIME - Falls back to standard clock handling for non-CLOCKFD clockids 3. clock_settime() enhancements: - Modified sched/clock/clock_settime.c to support CLOCKFD clockids - Extracts file descriptor and validates accessibility - Issues PTP_CLOCK_SETTIME ioctl to underlying PTP clock device - Maintains backward compatibility with standard POSIX clocks 4. File descriptor validation: - Checks file descriptor validity using fs_getfilep() - Ensures proper reference counting with fs_putfilep() - Returns appropriate error codes (EINVAL, EBADF) on failures 5. Integration with PTP clock framework: - Seamlessly integrates with PTP clock driver's ioctl interface - Enables standard POSIX clock API usage for dynamic PTP clocks - No changes required to existing applications using standard clocks Usage example: int fd = open("/dev/ptp0", O_RDONLY); struct timespec ts; clock_gettime(CLOCKFD(fd), &ts); /* Get PTP clock time */ clock_settime(CLOCKFD(fd), &ts); /* Set PTP clock time */ close(fd); This implementation follows the Linux kernel's PTP clock model, providing a familiar interface for developers working with PTP hardware. Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2025-03-20 18:20:26 +08:00
*
* Description:
* Clock Functions based on POSIX APIs
*
* CLOCK_REALTIME - POSIX demands this to be present. This is the wall
* time clock.
*
****************************************************************************/
int clock_settime(clockid_t clock_id, FAR const struct timespec *tp)
sched/clock: support using CLOCKFD to call clock_gettime/settime This patch implements POSIX-compliant CLOCKFD support, enabling user applications to access dynamic PTP clocks through file descriptors combined with clock_gettime() and clock_settime() system calls. Key changes include: 1. CLOCKFD macro support: - Added CLOCKFD() macro in include/nuttx/clock.h - Allows encoding file descriptor into clockid_t: CLOCKFD(fd) - Enables accessing PTP clocks via: clock_gettime(CLOCKFD(fd), &ts) 2. clock_gettime() enhancements: - Modified sched/clock/clock_gettime.c to detect CLOCKFD clockids - Extracts file descriptor from clockid using CLOCKFD_TO_FD() - Validates file descriptor and calls file_ioctl() with PTP_CLOCK_GETTIME - Falls back to standard clock handling for non-CLOCKFD clockids 3. clock_settime() enhancements: - Modified sched/clock/clock_settime.c to support CLOCKFD clockids - Extracts file descriptor and validates accessibility - Issues PTP_CLOCK_SETTIME ioctl to underlying PTP clock device - Maintains backward compatibility with standard POSIX clocks 4. File descriptor validation: - Checks file descriptor validity using fs_getfilep() - Ensures proper reference counting with fs_putfilep() - Returns appropriate error codes (EINVAL, EBADF) on failures 5. Integration with PTP clock framework: - Seamlessly integrates with PTP clock driver's ioctl interface - Enables standard POSIX clock API usage for dynamic PTP clocks - No changes required to existing applications using standard clocks Usage example: int fd = open("/dev/ptp0", O_RDONLY); struct timespec ts; clock_gettime(CLOCKFD(fd), &ts); /* Get PTP clock time */ clock_settime(CLOCKFD(fd), &ts); /* Set PTP clock time */ close(fd); This implementation follows the Linux kernel's PTP clock model, providing a familiar interface for developers working with PTP hardware. Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2025-03-20 18:20:26 +08:00
{
int ret = -EINVAL;
if (tp != NULL && tp->tv_nsec >= 0 && tp->tv_nsec < 1000000000)
sched/clock: support using CLOCKFD to call clock_gettime/settime This patch implements POSIX-compliant CLOCKFD support, enabling user applications to access dynamic PTP clocks through file descriptors combined with clock_gettime() and clock_settime() system calls. Key changes include: 1. CLOCKFD macro support: - Added CLOCKFD() macro in include/nuttx/clock.h - Allows encoding file descriptor into clockid_t: CLOCKFD(fd) - Enables accessing PTP clocks via: clock_gettime(CLOCKFD(fd), &ts) 2. clock_gettime() enhancements: - Modified sched/clock/clock_gettime.c to detect CLOCKFD clockids - Extracts file descriptor from clockid using CLOCKFD_TO_FD() - Validates file descriptor and calls file_ioctl() with PTP_CLOCK_GETTIME - Falls back to standard clock handling for non-CLOCKFD clockids 3. clock_settime() enhancements: - Modified sched/clock/clock_settime.c to support CLOCKFD clockids - Extracts file descriptor and validates accessibility - Issues PTP_CLOCK_SETTIME ioctl to underlying PTP clock device - Maintains backward compatibility with standard POSIX clocks 4. File descriptor validation: - Checks file descriptor validity using fs_getfilep() - Ensures proper reference counting with fs_putfilep() - Returns appropriate error codes (EINVAL, EBADF) on failures 5. Integration with PTP clock framework: - Seamlessly integrates with PTP clock driver's ioctl interface - Enables standard POSIX clock API usage for dynamic PTP clocks - No changes required to existing applications using standard clocks Usage example: int fd = open("/dev/ptp0", O_RDONLY); struct timespec ts; clock_gettime(CLOCKFD(fd), &ts); /* Get PTP clock time */ clock_settime(CLOCKFD(fd), &ts); /* Set PTP clock time */ close(fd); This implementation follows the Linux kernel's PTP clock model, providing a familiar interface for developers working with PTP hardware. Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2025-03-20 18:20:26 +08:00
{
if (clock_id == CLOCK_REALTIME)
{
nxclock_set_realtime(tp);
ret = 0;
}
sched/clock: support using CLOCKFD to call clock_gettime/settime This patch implements POSIX-compliant CLOCKFD support, enabling user applications to access dynamic PTP clocks through file descriptors combined with clock_gettime() and clock_settime() system calls. Key changes include: 1. CLOCKFD macro support: - Added CLOCKFD() macro in include/nuttx/clock.h - Allows encoding file descriptor into clockid_t: CLOCKFD(fd) - Enables accessing PTP clocks via: clock_gettime(CLOCKFD(fd), &ts) 2. clock_gettime() enhancements: - Modified sched/clock/clock_gettime.c to detect CLOCKFD clockids - Extracts file descriptor from clockid using CLOCKFD_TO_FD() - Validates file descriptor and calls file_ioctl() with PTP_CLOCK_GETTIME - Falls back to standard clock handling for non-CLOCKFD clockids 3. clock_settime() enhancements: - Modified sched/clock/clock_settime.c to support CLOCKFD clockids - Extracts file descriptor and validates accessibility - Issues PTP_CLOCK_SETTIME ioctl to underlying PTP clock device - Maintains backward compatibility with standard POSIX clocks 4. File descriptor validation: - Checks file descriptor validity using fs_getfilep() - Ensures proper reference counting with fs_putfilep() - Returns appropriate error codes (EINVAL, EBADF) on failures 5. Integration with PTP clock framework: - Seamlessly integrates with PTP clock driver's ioctl interface - Enables standard POSIX clock API usage for dynamic PTP clocks - No changes required to existing applications using standard clocks Usage example: int fd = open("/dev/ptp0", O_RDONLY); struct timespec ts; clock_gettime(CLOCKFD(fd), &ts); /* Get PTP clock time */ clock_settime(CLOCKFD(fd), &ts); /* Set PTP clock time */ close(fd); This implementation follows the Linux kernel's PTP clock model, providing a familiar interface for developers working with PTP hardware. Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2025-03-20 18:20:26 +08:00
#ifdef CONFIG_PTP_CLOCK
else if ((clock_id & CLOCK_MASK) == CLOCK_FD)
sched/clock: support using CLOCKFD to call clock_gettime/settime This patch implements POSIX-compliant CLOCKFD support, enabling user applications to access dynamic PTP clocks through file descriptors combined with clock_gettime() and clock_settime() system calls. Key changes include: 1. CLOCKFD macro support: - Added CLOCKFD() macro in include/nuttx/clock.h - Allows encoding file descriptor into clockid_t: CLOCKFD(fd) - Enables accessing PTP clocks via: clock_gettime(CLOCKFD(fd), &ts) 2. clock_gettime() enhancements: - Modified sched/clock/clock_gettime.c to detect CLOCKFD clockids - Extracts file descriptor from clockid using CLOCKFD_TO_FD() - Validates file descriptor and calls file_ioctl() with PTP_CLOCK_GETTIME - Falls back to standard clock handling for non-CLOCKFD clockids 3. clock_settime() enhancements: - Modified sched/clock/clock_settime.c to support CLOCKFD clockids - Extracts file descriptor and validates accessibility - Issues PTP_CLOCK_SETTIME ioctl to underlying PTP clock device - Maintains backward compatibility with standard POSIX clocks 4. File descriptor validation: - Checks file descriptor validity using fs_getfilep() - Ensures proper reference counting with fs_putfilep() - Returns appropriate error codes (EINVAL, EBADF) on failures 5. Integration with PTP clock framework: - Seamlessly integrates with PTP clock driver's ioctl interface - Enables standard POSIX clock API usage for dynamic PTP clocks - No changes required to existing applications using standard clocks Usage example: int fd = open("/dev/ptp0", O_RDONLY); struct timespec ts; clock_gettime(CLOCKFD(fd), &ts); /* Get PTP clock time */ clock_settime(CLOCKFD(fd), &ts); /* Set PTP clock time */ close(fd); This implementation follows the Linux kernel's PTP clock model, providing a familiar interface for developers working with PTP hardware. Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2025-03-20 18:20:26 +08:00
{
FAR struct file *filep;
ret = ptp_clockid_to_filep(clock_id, &filep);
if (ret >= 0)
{
ret = file_ioctl(filep, PTP_CLOCK_SETTIME, tp);
file_put(filep);
}
sched/clock: support using CLOCKFD to call clock_gettime/settime This patch implements POSIX-compliant CLOCKFD support, enabling user applications to access dynamic PTP clocks through file descriptors combined with clock_gettime() and clock_settime() system calls. Key changes include: 1. CLOCKFD macro support: - Added CLOCKFD() macro in include/nuttx/clock.h - Allows encoding file descriptor into clockid_t: CLOCKFD(fd) - Enables accessing PTP clocks via: clock_gettime(CLOCKFD(fd), &ts) 2. clock_gettime() enhancements: - Modified sched/clock/clock_gettime.c to detect CLOCKFD clockids - Extracts file descriptor from clockid using CLOCKFD_TO_FD() - Validates file descriptor and calls file_ioctl() with PTP_CLOCK_GETTIME - Falls back to standard clock handling for non-CLOCKFD clockids 3. clock_settime() enhancements: - Modified sched/clock/clock_settime.c to support CLOCKFD clockids - Extracts file descriptor and validates accessibility - Issues PTP_CLOCK_SETTIME ioctl to underlying PTP clock device - Maintains backward compatibility with standard POSIX clocks 4. File descriptor validation: - Checks file descriptor validity using fs_getfilep() - Ensures proper reference counting with fs_putfilep() - Returns appropriate error codes (EINVAL, EBADF) on failures 5. Integration with PTP clock framework: - Seamlessly integrates with PTP clock driver's ioctl interface - Enables standard POSIX clock API usage for dynamic PTP clocks - No changes required to existing applications using standard clocks Usage example: int fd = open("/dev/ptp0", O_RDONLY); struct timespec ts; clock_gettime(CLOCKFD(fd), &ts); /* Get PTP clock time */ clock_settime(CLOCKFD(fd), &ts); /* Set PTP clock time */ close(fd); This implementation follows the Linux kernel's PTP clock model, providing a familiar interface for developers working with PTP hardware. Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2025-03-20 18:20:26 +08:00
}
#endif
}
sched/clock: support using CLOCKFD to call clock_gettime/settime This patch implements POSIX-compliant CLOCKFD support, enabling user applications to access dynamic PTP clocks through file descriptors combined with clock_gettime() and clock_settime() system calls. Key changes include: 1. CLOCKFD macro support: - Added CLOCKFD() macro in include/nuttx/clock.h - Allows encoding file descriptor into clockid_t: CLOCKFD(fd) - Enables accessing PTP clocks via: clock_gettime(CLOCKFD(fd), &ts) 2. clock_gettime() enhancements: - Modified sched/clock/clock_gettime.c to detect CLOCKFD clockids - Extracts file descriptor from clockid using CLOCKFD_TO_FD() - Validates file descriptor and calls file_ioctl() with PTP_CLOCK_GETTIME - Falls back to standard clock handling for non-CLOCKFD clockids 3. clock_settime() enhancements: - Modified sched/clock/clock_settime.c to support CLOCKFD clockids - Extracts file descriptor and validates accessibility - Issues PTP_CLOCK_SETTIME ioctl to underlying PTP clock device - Maintains backward compatibility with standard POSIX clocks 4. File descriptor validation: - Checks file descriptor validity using fs_getfilep() - Ensures proper reference counting with fs_putfilep() - Returns appropriate error codes (EINVAL, EBADF) on failures 5. Integration with PTP clock framework: - Seamlessly integrates with PTP clock driver's ioctl interface - Enables standard POSIX clock API usage for dynamic PTP clocks - No changes required to existing applications using standard clocks Usage example: int fd = open("/dev/ptp0", O_RDONLY); struct timespec ts; clock_gettime(CLOCKFD(fd), &ts); /* Get PTP clock time */ clock_settime(CLOCKFD(fd), &ts); /* Set PTP clock time */ close(fd); This implementation follows the Linux kernel's PTP clock model, providing a familiar interface for developers working with PTP hardware. Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2025-03-20 18:20:26 +08:00
if (ret < 0)
{
sched/clock: support using CLOCKFD to call clock_gettime/settime This patch implements POSIX-compliant CLOCKFD support, enabling user applications to access dynamic PTP clocks through file descriptors combined with clock_gettime() and clock_settime() system calls. Key changes include: 1. CLOCKFD macro support: - Added CLOCKFD() macro in include/nuttx/clock.h - Allows encoding file descriptor into clockid_t: CLOCKFD(fd) - Enables accessing PTP clocks via: clock_gettime(CLOCKFD(fd), &ts) 2. clock_gettime() enhancements: - Modified sched/clock/clock_gettime.c to detect CLOCKFD clockids - Extracts file descriptor from clockid using CLOCKFD_TO_FD() - Validates file descriptor and calls file_ioctl() with PTP_CLOCK_GETTIME - Falls back to standard clock handling for non-CLOCKFD clockids 3. clock_settime() enhancements: - Modified sched/clock/clock_settime.c to support CLOCKFD clockids - Extracts file descriptor and validates accessibility - Issues PTP_CLOCK_SETTIME ioctl to underlying PTP clock device - Maintains backward compatibility with standard POSIX clocks 4. File descriptor validation: - Checks file descriptor validity using fs_getfilep() - Ensures proper reference counting with fs_putfilep() - Returns appropriate error codes (EINVAL, EBADF) on failures 5. Integration with PTP clock framework: - Seamlessly integrates with PTP clock driver's ioctl interface - Enables standard POSIX clock API usage for dynamic PTP clocks - No changes required to existing applications using standard clocks Usage example: int fd = open("/dev/ptp0", O_RDONLY); struct timespec ts; clock_gettime(CLOCKFD(fd), &ts); /* Get PTP clock time */ clock_settime(CLOCKFD(fd), &ts); /* Set PTP clock time */ close(fd); This implementation follows the Linux kernel's PTP clock model, providing a familiar interface for developers working with PTP hardware. Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2025-03-20 18:20:26 +08:00
set_errno(-ret);
return ERROR;
}
return OK;
}