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 <dongjiuzhu1@xiaomi.com>
This commit is contained in:
dongjiuzhu1 2025-03-15 13:46:07 +08:00 committed by Alan C. Assis
parent 28302ae7d7
commit ac881e5108
3 changed files with 94 additions and 25 deletions

View file

@ -27,6 +27,8 @@
* Included Files
****************************************************************************/
#include <sys/types.h>
/****************************************************************************
* 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

View file

@ -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");

View file

@ -28,6 +28,8 @@
#include <stdlib.h>
#include <stdio.h>
#include <sys/socket.h>
#include <unistd.h>
#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 <interface> &\n"
"ptpd status <pid>\n"
"ptpd stop <pid>\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);
}