Blame view

RIOT/sys/shell/commands/sc_sntp.c 1.86 KB
a752c7ab   elopes   add first test an...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
  /*
   * Copyright (C) 2016 Luminița Lăzărescu <cluminita.lazarescu@gmail.com>
   *               2017 Freie Universität Berlin
   *
   * This file is subject to the terms and conditions of the GNU Lesser
   * General Public License v2.1. See the file LICENSE in the top level
   * directory for more details.
   */
  
  /**
   * @ingroup     sys_shell_commands
   * @{
   *
   * @file
   * @brief       Prints the real time offset from the system time
   *
   * @author      Luminița Lăzărescu <cluminita.lazarescu@gmail.com>
   * @author      Martine Lenders <m.lenders@fu-berlin.de>
   */
  
  #include <stdio.h>
  #include <time.h>
  
  #include "net/sntp.h"
  #include "net/ntp_packet.h"
  #include "net/af.h"
  #include "net/ipv6/addr.h"
  #include "timex.h"
  
  #define _DEFAULT_TIMEOUT (5000U);
  
  static void _usage(char *cmd)
  {
      printf("Usage: %s <server addr> [<timeout>]\n", cmd);
      puts("default: timeout = 5000");
  }
  
  int _ntpdate(int argc, char **argv)
  {
      uint32_t timeout = _DEFAULT_TIMEOUT;
  
      if (argc < 2) {
          _usage(argv[0]);
          return 1;
      }
      sock_udp_ep_t server = { .port = NTP_PORT, .family = AF_INET6 };
      ipv6_addr_from_str((ipv6_addr_t *)&server.addr, argv[1]);
      if (argc > 2) {
          timeout = atoi(argv[2]);
      }
      if (sntp_sync(&server, timeout) < 0) {
          puts("Error in synchronization");
          return 1;
      }
  #ifdef MODULE_NEWLIB
      struct tm *tm;
      time_t time = (time_t)(sntp_get_unix_usec() / US_PER_SEC);
  
      tm = gmtime(&time);
      printf("%04i-%02i-%02i %02i:%02i:%02i UTC (%i us)\n",
             tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
             tm->tm_hour, tm->tm_min, tm->tm_sec,
             (int)sntp_get_offset());
  #else
      uint64_t time = sntp_get_unix_usec();
      printf("%" PRIu32 ".%" PRIu32 " (%i us)\n",
             (uint32_t)(time / US_PER_SEC),
             (uint32_t)(time / US_PER_SEC),
             (int)sntp_get_offset());
  #endif
      return 0;
  }