Browse Source

Merge pull request #6693 from miri64/sntp/enh/real-time-types

sntp: extend API for common real time types
pr/rotary
Martine Lenders 6 years ago committed by GitHub
parent
commit
ad38750587
  1. 6
      sys/include/net/ntp_packet.h
  2. 15
      sys/include/net/sntp.h
  3. 11
      sys/net/application_layer/sntp/sntp.c
  4. 22
      sys/shell/commands/sc_sntp.c
  5. 23
      tests/sntp/Makefile
  6. 6
      tests/sntp/README.md
  7. 31
      tests/sntp/main.c

6
sys/include/net/ntp_packet.h

@ -46,6 +46,12 @@ extern "C" {
#define NTP_VERSION (4U) /**< NTP version */
#define NTP_PORT (123U) /**< NTP port number */
/**
* @brief Offset in seconds of NTP timestamp (seconds from 1990-01-01 00:00:00 UTC)
* to UNIX timestamp (seconds from 1970-01-01 00:00:00 UTC).
*/
#define NTP_UNIX_OFFSET (2208988800)
/**
* @brief NTP modes
*/

15
sys/include/net/sntp.h

@ -1,5 +1,6 @@
/*
* Copyright (C) 2016 Luminița Lăzărescu <cluminita.lazarescu@gmail.com>
* Copyright (C) 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
@ -23,7 +24,11 @@
#define SNTP_H
#include <stdint.h>
#include <time.h>
#include "net/ntp_packet.h"
#include "net/sock/udp.h"
#include "xtimer.h"
#ifdef __cplusplus
extern "C" {
@ -47,6 +52,16 @@ int sntp_sync(sock_udp_ep_t *server, uint32_t timeout);
*/
int64_t sntp_get_offset(void);
/**
* @brief Get time in microseconds from 1970-01-01 00:00:00 UTC.
*
* @return Time in microseconds from 1970-01-01 00:00:00 UTC
*/
static inline uint64_t sntp_get_unix_usec(void)
{
return (uint64_t)(sntp_get_offset() - (NTP_UNIX_OFFSET * US_PER_SEC) + xtimer_now_usec64());
}
#ifdef __cplusplus
}
#endif

11
sys/net/application_layer/sntp/sntp.c

@ -38,13 +38,11 @@ int sntp_sync(sock_udp_ep_t *server, uint32_t timeout)
{
int result;
mutex_lock(&_sntp_mutex);
if ((result = sock_udp_create(&_sntp_sock,
NULL,
server,
0)) < 0) {
DEBUG("Error creating UDP sock\n");
mutex_unlock(&_sntp_mutex);
return result;
}
memset(&_sntp_packet, 0, sizeof(_sntp_packet));
@ -57,7 +55,6 @@ int sntp_sync(sock_udp_ep_t *server, uint32_t timeout)
NULL)) < 0) {
DEBUG("Error sending message\n");
sock_udp_close(&_sntp_sock);
mutex_unlock(&_sntp_mutex);
return result;
}
if ((result = (int)sock_udp_recv(&_sntp_sock,
@ -67,13 +64,13 @@ int sntp_sync(sock_udp_ep_t *server, uint32_t timeout)
NULL)) < 0) {
DEBUG("Error receiving message\n");
sock_udp_close(&_sntp_sock);
mutex_unlock(&_sntp_mutex);
return result;
}
sock_udp_close(&_sntp_sock);
_sntp_offset = (byteorder_ntohl(_sntp_packet.transmit.seconds) * US_PER_SEC) +
((byteorder_ntohl(_sntp_packet.transmit.fraction) * 232)
/ 1000000) - xtimer_now64();
mutex_lock(&_sntp_mutex);
_sntp_offset = (((int64_t)byteorder_ntohl(_sntp_packet.transmit.seconds)) * US_PER_SEC) +
((((int64_t)byteorder_ntohl(_sntp_packet.transmit.fraction)) * 232)
/ 1000000) - xtimer_now_usec64();
mutex_unlock(&_sntp_mutex);
return 0;
}

22
sys/shell/commands/sc_sntp.c

@ -1,5 +1,6 @@
/*
* 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
@ -14,13 +15,17 @@
* @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);
@ -47,6 +52,21 @@ int _ntpdate(int argc, char **argv)
puts("Error in synchronization");
return 1;
}
printf("Offset: %i\n", (int)sntp_get_offset());
#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;
}

23
tests/sntp/Makefile

@ -0,0 +1,23 @@
# name of your application
APPLICATION = sntp
include ../Makefile.tests_common
BOARD_INSUFFICIENT_MEMORY := chronos msb-430 msb-430h nucleo32-f031 \
nucleo32-f042 nucleo32-l031 nucleo-f030 \
nucleo-f334 nucleo-l053 stm32f0discovery \
telosb weio z1
USEMODULE += sntp
USEMODULE += gnrc_sock_udp
USEMODULE += gnrc_ipv6_default
USEMODULE += auto_init_gnrc_netif
USEMODULE += gnrc_netdev_default
USEMODULE += shell
USEMODULE += shell_commands
# Comment this out to disable code in RIOT that does safety checking
# which is not needed in a production environment but helps in the
# development process:
CFLAGS += -DDEVELHELP
include $(RIOTBASE)/Makefile.include

6
tests/sntp/README.md

@ -0,0 +1,6 @@
About
=====
This test application allows you to type `ntpdate <server>` and get the current
date + the offset in microseconds of the NTP timestamp (seconds from 1900-01-01)
from the current `xtimer_now_usec64()` back utilizing SNTP. Make sure that
`<server>` is reachable and has an NTP daemon installed.

31
tests/sntp/main.c

@ -0,0 +1,31 @@
/*
* Copyright (C) 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 tests
* @{
*
* @file
* @brief Tests sntp module.
*
* @author Martine Lenders <m.lenders@fu-berlin.de>
*
* @}
*/
#include <stdio.h>
#include "shell.h"
static char line_buf[SHELL_DEFAULT_BUFSIZE];
int main(void)
{
shell_run(NULL, line_buf, SHELL_DEFAULT_BUFSIZE);
return 0;
}
Loading…
Cancel
Save