From 10ca03aab35f1203122e0b39f587e287451aed6e Mon Sep 17 00:00:00 2001 From: Martine Lenders Date: Sat, 4 Mar 2017 22:04:59 +0100 Subject: [PATCH] sntp: extend API for UNIX timestamp --- sys/include/net/ntp_packet.h | 6 ++++++ sys/include/net/sntp.h | 15 +++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/sys/include/net/ntp_packet.h b/sys/include/net/ntp_packet.h index 3484d54f64..51cd659744 100644 --- a/sys/include/net/ntp_packet.h +++ b/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 */ diff --git a/sys/include/net/sntp.h b/sys/include/net/sntp.h index 23594a75a7..8e8d80a8ac 100644 --- a/sys/include/net/sntp.h +++ b/sys/include/net/sntp.h @@ -1,5 +1,6 @@ /* * Copyright (C) 2016 Luminița Lăzărescu + * 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 +#include + +#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