You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2021/02/20 09:31:03 UTC

[incubator-nuttx-apps] 01/02: ntpclient: Add ntpcstatus() api to query ntpc status programatically

This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx-apps.git

commit 8ae1267054b0e4f81db185d5fb133f720abf86dc
Author: YAMAMOTO Takashi <ya...@midokura.com>
AuthorDate: Fri Feb 19 13:25:05 2021 +0900

    ntpclient: Add ntpcstatus() api to query ntpc status programatically
    
    For now, just provide the latest samples.
---
 include/netutils/ntpclient.h   | 30 ++++++++++++++++++++++++++++++
 netutils/ntpclient/ntpclient.c | 31 +++++++++++++++++++++++++++++++
 2 files changed, 61 insertions(+)

diff --git a/include/netutils/ntpclient.h b/include/netutils/ntpclient.h
index d68c553..789d136 100644
--- a/include/netutils/ntpclient.h
+++ b/include/netutils/ntpclient.h
@@ -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
 }
diff --git a/netutils/ntpclient/ntpclient.c b/netutils/ntpclient/ntpclient.c
index db87b4d..cdaf1b1 100644
--- a/netutils/ntpclient/ntpclient.c
+++ b/netutils/ntpclient/ntpclient.c
@@ -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;
+}