ntpclient: Add ntpcstatus() api to query ntpc status programatically

For now, just provide the latest samples.
This commit is contained in:
YAMAMOTO Takashi 2021-02-19 13:25:05 +09:00 committed by Xiang Xiao
parent 771d19b94a
commit 8ae1267054
2 changed files with 61 additions and 0 deletions

View File

@ -42,6 +42,8 @@
#include <nuttx/config.h>
#include <sys/socket.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
@ -144,6 +146,34 @@ int ntpc_start(void);
int ntpc_stop(void);
/****************************************************************************
* Name: ntpc_status
*
* Description:
* Get a status of the NTP daemon
*
* Returned Value:
* Zero on success; a negated errno value on failure.
*
****************************************************************************/
struct ntpc_status_s
{
/* the latest samples */
unsigned int nsamples;
struct
{
int64_t offset;
int64_t delay;
FAR const struct sockaddr *srv_addr;
struct sockaddr_storage _srv_addr_store;
}
samples[CONFIG_NETUTILS_NTPCLIENT_NUM_SAMPLES];
};
int ntpc_status(struct ntpc_status_s *statusp);
#undef EXTERN
#ifdef __cplusplus
}

View File

@ -160,6 +160,7 @@ union ntp_addr_u
#ifdef CONFIG_NET_IPv6
struct sockaddr_in6 in6;
#endif
struct sockaddr_storage ss;
};
/* NTP offset. */
@ -211,6 +212,10 @@ static struct ntpc_daemon_s g_ntpc_daemon =
AF_UNSPEC, /* Default is both IPv4 and IPv6 */
};
static struct ntp_sample_s g_last_samples
[CONFIG_NETUTILS_NTPCLIENT_NUM_SAMPLES];
unsigned int g_last_nsamples = 0;
/****************************************************************************
* Private Functions
****************************************************************************/
@ -1369,6 +1374,13 @@ static int ntpc_daemon(int argc, FAR char **argv)
ntpc_settime(offset, &start_realtime, &start_monotonic);
/* Save samples for ntpc_status() */
sem_wait(&g_ntpc_daemon.lock);
g_last_nsamples = nsamples;
memcpy(&g_last_samples, samples, nsamples * sizeof(*samples));
sem_post(&g_ntpc_daemon.lock);
#ifndef CONFIG_NETUTILS_NTPCLIENT_STAY_ON
/* Configured to exit at success. */
@ -1571,3 +1583,22 @@ int ntpc_stop(void)
sem_post(&g_ntpc_daemon.lock);
return OK;
}
int ntpc_status(struct ntpc_status_s *statusp)
{
unsigned int i;
sem_wait(&g_ntpc_daemon.lock);
statusp->nsamples = g_last_nsamples;
for (i = 0; i < g_last_nsamples; i++)
{
statusp->samples[i].offset = g_last_samples[i].offset;
statusp->samples[i].delay = g_last_samples[i].delay;
statusp->samples[i]._srv_addr_store = g_last_samples[i].srv_addr.ss;
statusp->samples[i].srv_addr = (FAR const struct sockaddr *)
&statusp->samples[i]._srv_addr_store;
}
sem_post(&g_ntpc_daemon.lock);
return OK;
}