From ac881e510889fddd9a36ba09203f0d460d9f0dee Mon Sep 17 00:00:00 2001 From: dongjiuzhu1 Date: Sat, 15 Mar 2025 13:46:07 +0800 Subject: [PATCH] system/ptpd: provide more parameter by struct ptpd_config_s when starting ptpd origin command change to new command: ptpd start interface & -> ptpd -i eth0 & ptpd stop pid -> ptpd -d pid ptpd status pid -> ptpd -t status Signed-off-by: dongjiuzhu1 --- include/netutils/ptpd.h | 16 +++++-- netutils/ptpd/ptpd.c | 8 +++- system/ptpd/ptpd_main.c | 95 ++++++++++++++++++++++++++++++++--------- 3 files changed, 94 insertions(+), 25 deletions(-) diff --git a/include/netutils/ptpd.h b/include/netutils/ptpd.h index 8e5a3c508..73a615be0 100644 --- a/include/netutils/ptpd.h +++ b/include/netutils/ptpd.h @@ -27,6 +27,8 @@ * Included Files ****************************************************************************/ +#include + /**************************************************************************** * Pre-processor Definitions ****************************************************************************/ @@ -35,6 +37,14 @@ * Public Types ****************************************************************************/ +struct ptpd_config_s +{ + FAR const char *interface; + FAR const char *clock; + bool hardware_ts; + sa_family_t af; +}; + /* PTPD status information structure */ struct ptpd_status_s @@ -115,10 +125,10 @@ extern "C" * Name: ptpd_start * * Description: - * Start the PTP daemon and bind it to specified interface. + * Start the PTP daemon and bind it to specified config. * * Input Parameters: - * interface - Name of the network interface to bind to, e.g. "eth0" + * config - The configs of PTP daemon, includes interface, af and clock... * * Returned Value: * On success, the non-negative task ID of the PTP daemon is returned; @@ -126,7 +136,7 @@ extern "C" * ****************************************************************************/ -int ptpd_start(FAR const char *interface); +int ptpd_start(FAR const struct ptpd_config_s *config); /**************************************************************************** * Name: ptpd_status diff --git a/netutils/ptpd/ptpd.c b/netutils/ptpd/ptpd.c index 411c9f4eb..492f762b3 100644 --- a/netutils/ptpd/ptpd.c +++ b/netutils/ptpd/ptpd.c @@ -1395,7 +1395,7 @@ static void ptp_process_statusreq(FAR struct ptp_state_s *state) * ****************************************************************************/ -int ptpd_start(FAR const char *interface) +int ptpd_start(FAR const struct ptpd_config_s *config) { FAR struct ptp_state_s *state; struct pollfd pollfds[2]; @@ -1407,8 +1407,12 @@ int ptpd_start(FAR const char *interface) memset(&rxiov, 0, sizeof(rxiov)); state = calloc(1, sizeof(struct ptp_state_s)); + if (state == NULL) + { + return -ENOMEM; + } - if (ptp_initialize_state(state, interface) != OK) + if (ptp_initialize_state(state, config->interface) != OK) { ptperr("Failed to initialize PTP state, exiting\n"); diff --git a/system/ptpd/ptpd_main.c b/system/ptpd/ptpd_main.c index 36b8b67c6..75cca2900 100644 --- a/system/ptpd/ptpd_main.c +++ b/system/ptpd/ptpd_main.c @@ -28,6 +28,8 @@ #include #include +#include +#include #include "netutils/ptpd.h" @@ -35,11 +37,11 @@ * Private Functions ****************************************************************************/ -static int do_ptpd_start(FAR const char *interface) +static int do_ptpd_start(FAR const struct ptpd_config_s *config) { int ret; - ret = ptpd_start(interface); + ret = ptpd_start(config); /* Should never happen */ @@ -140,6 +142,24 @@ int do_ptpd_stop(int pid) } } +static void usage(FAR const char *progname) +{ + fprintf(stderr, "Usage: %s [options]: run this daemon in background.\n" + " Network Transport:\n" + " -2 IEEE 802.3\n" + " -4 UDP IPV4 (default)\n" + " -6 UDP IPV6\n" + " Time Stamping:\n" + " -H HARDWARE (default) depends on NET_TIMESTAMP\n" + " -S SOFTWARE\n" + " -r synchronize system (realtime) clock\n" + " -i [dev] interface device to use, for example 'eth0'\n" + " -p [dev] clock device to use\n" + " -t [pid] look the status of ptp daemon\n" + " -s [pid] stop ptp daemon\n", + progname); +} + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -150,24 +170,59 @@ int do_ptpd_stop(int pid) int main(int argc, FAR char *argv[]) { - if (argc == 3 && strcmp(argv[1], "start") == 0) + struct ptpd_config_s config; + int option; + + /* Default config for ptp daemon */ + + config.interface = "eth0"; + config.clock = "realtime"; +#ifdef CONFIG_NET_TIMESTAMP + config.hardware_ts = true; +#else + config.hardware_ts = false; +#endif + config.af = AF_INET; + + while ((option = getopt(argc, argv, "p:i:t:s:r246HS")) != ERROR) { - return do_ptpd_start(argv[2]); - } - else if (argc == 3 && strcmp(argv[1], "status") == 0) - { - return do_ptpd_status(atoi(argv[2])); - } - else if (argc == 3 && strcmp(argv[1], "stop") == 0) - { - return do_ptpd_stop(atoi(argv[2])); - } - else - { - fprintf(stderr, "Usage: \n" - "ptpd start &\n" - "ptpd status \n" - "ptpd stop \n"); - return EXIT_FAILURE; + switch (option) + { + case 't': + return do_ptpd_status(atoi(optarg)); + case 's': + return do_ptpd_stop(atoi(optarg)); + case '2': + config.af = AF_PACKET; + break; + case '4': + config.af = AF_INET; + break; + case '6': + config.af = AF_INET6; + break; +#ifdef CONFIG_NET_TIMESTAMP + case 'H': + config.hardware_ts = true; + break; +#endif + case 'S': + config.hardware_ts = false; + break; + case 'i': + config.interface = optarg; + break; + case 'p': + config.clock = optarg; + break; + case 'r': + config.clock = "realtime"; + break; + default: + usage(argv[0]); + return 0; + } } + + return do_ptpd_start(&config); }