Send connman mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        https://lists.01.org/mailman/listinfo/connman
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of connman digest..."


Today's Topics:

   1. Re: [PATCH] ntp: Use adjtimex to tune the kernel clock
      (Naveen Singh)


----------------------------------------------------------------------

Message: 1
Date: Mon, 18 Jul 2016 18:44:39 -0700
From: Naveen Singh <[email protected]>
To: Justin Maggard <[email protected]>
Cc: [email protected], Justin Maggard <[email protected]>
Subject: Re: [PATCH] ntp: Use adjtimex to tune the kernel clock
Message-ID:
        <cafk1zra47jjnj-72smaces7iv+l5tyxzapkisu9ouac013y...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

On Mon, Jul 18, 2016 at 11:14 AM, Justin Maggard <[email protected]> wrote:
> Replace adjtime()/gettimeofday() calls with the RFC 5905 clock
> adjustment algorithm implemented by adjtimex().  This also allows us to
> support leap seconds sent from an NTP server.
>
> This code is more-or-less ripped straight from systemd timesyncd.
>
> Signed-off-by: Justin Maggard <[email protected]>
> ---
>  src/ntp.c | 62 ++++++++++++++++++++++++++++++++++++--------------------------
>  1 file changed, 36 insertions(+), 26 deletions(-)
>
> diff --git a/src/ntp.c b/src/ntp.c
> index dd246eb..9b1bb4f 100644
> --- a/src/ntp.c
> +++ b/src/ntp.c
> @@ -30,6 +30,7 @@
>  #include <stdlib.h>
>  #include <string.h>
>  #include <sys/time.h>
> +#include <sys/timex.h>
>  #include <sys/socket.h>
>  #include <netinet/in.h>
>  #include <netinet/ip.h>
> @@ -66,9 +67,13 @@ struct ntp_msg {
>
>  #define OFFSET_1900_1970  2208988800UL /* 1970 - 1900 in seconds */
>
> -#define STEPTIME_MIN_OFFSET  0.128
> +#define STEPTIME_MIN_OFFSET  0.4
>
>  #define LOGTOD(a)  ((a) < 0 ? 1. / (1L << -(a)) : 1L << (int)(a))
> +#define NSEC_PER_SEC  ((uint64_t)1000000000ULL)
> +#ifndef ADJ_SETOFFSET
> +#define ADJ_SETOFFSET          0x0100  /* add 'time' to current time */
> +#endif
>
>  #define NTP_SEND_TIMEOUT       2
>  #define NTP_SEND_RETRIES       3
> @@ -247,6 +252,7 @@ static void decode_msg(void *base, size_t len, struct 
> timeval *tv,
>         double m_delta, org, rec, xmt, dst;
>         double delay, offset;
>         static guint transmit_delay;
> +       struct timex tmx = {};
>
>         if (len < sizeof(*msg)) {
>                 connman_error("Invalid response from time server");
> @@ -336,39 +342,43 @@ static void decode_msg(void *base, size_t len, struct 
> timeval *tv,
>
>         poll_id = g_timeout_add_seconds(transmit_delay, next_poll, NULL);
>
> -       connman_info("ntp: time slew %+.6f s", offset);
> -
>         if (offset < STEPTIME_MIN_OFFSET && offset > -STEPTIME_MIN_OFFSET) {
> -               struct timeval adj;
> -
> -               adj.tv_sec = (long) offset;
> -               adj.tv_usec = (offset - adj.tv_sec) * 1000000;
> +               tmx.modes = ADJ_STATUS | ADJ_NANO | ADJ_OFFSET | 
> ADJ_TIMECONST | ADJ_MAXERROR | ADJ_ESTERROR;
> +               tmx.status = STA_PLL;
> +               tmx.offset = offset * NSEC_PER_SEC;
> +               tmx.constant = msg->poll - 4;
> +               tmx.maxerror = 0;
> +               tmx.esterror = 0;
> +
> +               connman_info("ntp: adjust (slew): %+.6f sec", offset);
> +       } else {
> +               tmx.modes = ADJ_STATUS | ADJ_NANO | ADJ_SETOFFSET;
>
> -               DBG("adjusting time %ld seconds, %ld msecs", adj.tv_sec, 
> adj.tv_usec);
> +               /* ADJ_NANO uses nanoseconds in the microseconds field */
> +               tmx.time.tv_sec = (long)offset;
> +               tmx.time.tv_usec = (offset - tmx.time.tv_sec) * NSEC_PER_SEC;
>
> -               if (adjtime(&adj, &adj) < 0) {
> -                       connman_error("Failed to adjust time");
> -                       return;
> +               /* the kernel expects -0.3s as {-1, 7000.000.000} */
> +               if (tmx.time.tv_usec < 0) {
> +                       tmx.time.tv_sec  -= 1;
> +                       tmx.time.tv_usec += NSEC_PER_SEC;
>                 }
>
> -               DBG("remaining adjustment %ld seconds, %ld msecs", 
> adj.tv_sec, adj.tv_usec);
> -       } else {
> -               struct timeval cur;
> -               double dtime;
> -
> -               gettimeofday(&cur, NULL);
> -               dtime = offset + cur.tv_sec + 1.0e-6 * cur.tv_usec;
> -               cur.tv_sec = (long) dtime;
> -               cur.tv_usec = (dtime - cur.tv_sec) * 1000000;
> -
> -               DBG("setting time: %ld seconds, %ld msecs", cur.tv_sec, 
> cur.tv_usec);
> +               connman_info("ntp: adjust (jump): %+.6f sec", offset);
> +       }
>
> -               if (settimeofday(&cur, NULL) < 0) {
> -                       connman_error("Failed to set time");
> -                       return;
> -               }
> +       if (NTP_FLAGS_LI_DECODE(msg->flags) & NTP_FLAG_LI_ADDSECOND)
> +               tmx.status |= STA_INS;
> +       else if (NTP_FLAGS_LI_DECODE(msg->flags) & NTP_FLAG_LI_DELSECOND)
> +               tmx.status |= STA_DEL;
>
> +       if (adjtimex(&tmx) < 0) {
> +               connman_error("Failed to adjust time");
> +               return;
>         }
> +
> +       DBG("interval/delta/delay/drift %fs/%+.3fs/%.3fs/%+ldppm",
> +               LOGTOD(msg->poll), offset, delay, tmx.freq / 65536);
>  }
>
>  static gboolean received_data(GIOChannel *channel, GIOCondition condition,
> --
> 2.9.1
>
I am glad you did it because adjtime is not supported in bionic and if
we get this patch in, we have one less item to take care of for making
connman work on Android.
> _______________________________________________
> connman mailing list
> [email protected]
> https://lists.01.org/mailman/listinfo/connman


------------------------------

Subject: Digest Footer

_______________________________________________
connman mailing list
[email protected]
https://lists.01.org/mailman/listinfo/connman


------------------------------

End of connman Digest, Vol 9, Issue 11
**************************************

Reply via email to