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:04 UTC

[incubator-nuttx-apps] 02/02: system/ntpc: Add ntpcstatus command

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 a63bdcbacb6d8d3731ec3d10c7d864fd7734b0dc
Author: YAMAMOTO Takashi <ya...@midokura.com>
AuthorDate: Tue Feb 16 15:52:43 2021 +0900

    system/ntpc: Add ntpcstatus command
    
    An example output:
    
        nsh> ntpcstatus
        The number of last samples: 5
        [0] srv 178.16.23.50 offset -0.014006560 delay 0.349967444
        [1] srv 5.9.57.158 offset 0.001792161 delay 0.269991633
        [2] srv 206.75.147.25 offset 0.009916600 delay 0.129989672
        [3] srv 162.159.200.1 offset 0.011508908 delay 0.019917401
        [4] srv 185.19.184.35 offset 0.021468135 delay 0.239915030
        nsh>
---
 system/ntpc/Makefile          |   4 +-
 system/ntpc/ntpcstatus_main.c | 125 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 127 insertions(+), 2 deletions(-)

diff --git a/system/ntpc/Makefile b/system/ntpc/Makefile
index 4660204..a44c876 100644
--- a/system/ntpc/Makefile
+++ b/system/ntpc/Makefile
@@ -37,13 +37,13 @@ include $(APPDIR)/Make.defs
 
 # NTPC address renewal built-in application info
 
-PROGNAME = ntpcstart ntpcstop
+PROGNAME = ntpcstart ntpcstop ntpcstatus
 PRIORITY = $(CONFIG_SYSTEM_NTPC_PRIORITY)
 STACKSIZE = $(CONFIG_SYSTEM_NTPC_STACKSIZE)
 MODULE = $(CONFIG_SYSTEM_NTPC)
 
 # NTPC address renewal
 
-MAINSRC = ntpcstart_main.c ntpcstop_main.c
+MAINSRC = ntpcstart_main.c ntpcstop_main.c ntpcstatus_main.c
 
 include $(APPDIR)/Application.mk
diff --git a/system/ntpc/ntpcstatus_main.c b/system/ntpc/ntpcstatus_main.c
new file mode 100644
index 0000000..1695701
--- /dev/null
+++ b/system/ntpc/ntpcstatus_main.c
@@ -0,0 +1,125 @@
+/****************************************************************************
+ * system/ntpc/ntpc_status.c
+ *
+ * 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 <sys/types.h>
+#include <sys/socket.h>
+
+#include <inttypes.h>
+#ifdef CONFIG_LIBC_NETDB
+#include <netdb.h>
+#endif
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "netutils/ntpclient.h"
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/* "-", uint64_t, ".", 9 digits fraction part, NUL */
+
+#define	NTP_TIME_STR_MAX_LEN (1 + 21 + 1 + 9 + 1)
+
+static void
+format_ntptimestamp(int64_t ts, FAR char *buf)
+{
+  FAR const char *sign;
+  uint64_t absts;
+
+  if (ts < 0)
+    {
+      sign = "-";
+      absts = -ts;
+    }
+  else
+    {
+      sign = "";
+      absts = ts;
+    }
+
+  sprintf(buf, "%s%" PRIu64 ".%09" PRIu64,
+          sign, absts >> 32,
+          ((absts & 0xffffffff) * NSEC_PER_SEC) >> 32);
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * ntpcstatus_main
+ ****************************************************************************/
+
+int main(int argc, FAR char *argv[])
+{
+  struct ntpc_status_s status;
+  unsigned int i;
+  int ret;
+
+  ret = ntpc_status(&status);
+  if (ret < 0)
+    {
+      fprintf(stderr, "ERROR: ntpc_status() failed\n");
+      return EXIT_FAILURE;
+    }
+
+  printf("The number of last samples: %u\n", status.nsamples);
+  for (i = 0; i < status.nsamples; i++)
+    {
+      FAR const struct sockaddr *sa;
+      socklen_t salen;
+      FAR const char *name = "<noname>";
+      char offset_buf[NTP_TIME_STR_MAX_LEN];
+      char delay_buf[NTP_TIME_STR_MAX_LEN];
+
+      sa = status.samples[i].srv_addr;
+      salen = (sa->sa_family == AF_INET) ? sizeof(struct sockaddr_in)
+                                         : sizeof(struct sockaddr_in6);
+#ifdef CONFIG_LIBC_NETDB
+      char hbuf[NI_MAXHOST];
+
+      ret = getnameinfo(sa, salen,
+                        hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST);
+      if (ret == 0)
+        {
+          name = hbuf;
+        }
+      else
+        {
+          fprintf(stderr, "WARNING: getnameinfo failed: %s\n",
+                  gai_strerror(ret));
+        }
+#endif
+
+      format_ntptimestamp(status.samples[i].offset, offset_buf);
+      format_ntptimestamp(status.samples[i].delay, delay_buf);
+      printf("[%u] srv %s offset %s delay %s\n",
+             i, name, offset_buf, delay_buf);
+    }
+
+  return EXIT_SUCCESS;
+}