Skip to content

Commit

Permalink
ntp:fix parameter is negative numbers.
Browse files Browse the repository at this point in the history
Signed-off-by: yangguangcai <[email protected]>
  • Loading branch information
yangguangcai1 authored and xiaoxiang781216 committed Sep 28, 2024
1 parent fb37ef3 commit df4cdba
Showing 1 changed file with 28 additions and 11 deletions.
39 changes: 28 additions & 11 deletions netutils/ntpclient/ntpclient.c
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ static inline void ntpc_setuint64(FAR uint8_t *ptr, uint64_t value)
* Name: ntp_secpart
****************************************************************************/

static uint32_t ntp_secpart(uint64_t time)
static int32_t ntp_secpart(int64_t time)
{
/* NTP timestamps are represented as a 64-bit fixed-point number, in
* seconds relative to 0000 UT on 1 January 1900. The integer part is
Expand All @@ -434,11 +434,11 @@ static uint32_t ntp_secpart(uint64_t time)
* Name: ntp_nsecpart
****************************************************************************/

static uint32_t ntp_nsecpart(uint64_t time)
static int32_t ntp_nsecpart(int64_t time)
{
/* Get fraction part converted to nanoseconds. */

return ((time & 0xffffffffu) * NSEC_PER_SEC) >> 32;
return (((int64_t)((uint64_t)time << 32) >> 32) * NSEC_PER_SEC) >> 32;
}

/****************************************************************************
Expand Down Expand Up @@ -527,6 +527,30 @@ static void ntpc_calculate_offset(FAR int64_t *offset, FAR int64_t *delay,
(remote_xmittime - remote_recvtime);
}

/****************************************************************************
* Name: ntpc_apply_offset
****************************************************************************/

static void ntpc_apply_offset(FAR struct timespec *tp,
FAR struct timespec *src,
int64_t offset)
{
tp->tv_sec = src->tv_sec + ntp_secpart(offset);
tp->tv_nsec = src->tv_nsec + ntp_nsecpart(offset);

while (tp->tv_nsec < 0)
{
tp->tv_nsec += NSEC_PER_SEC;
tp->tv_sec--;
}

while (tp->tv_nsec >= NSEC_PER_SEC)
{
tp->tv_nsec -= NSEC_PER_SEC;
tp->tv_sec++;
}
}

/****************************************************************************
* Name: ntpc_settime
*
Expand Down Expand Up @@ -580,14 +604,7 @@ static void ntpc_settime(int64_t offset, FAR struct timespec *start_realtime,

/* Apply offset */

tp = curr_realtime;
tp.tv_sec += ntp_secpart(offset);
tp.tv_nsec += ntp_nsecpart(offset);
while (tp.tv_nsec >= NSEC_PER_SEC)
{
tp.tv_nsec -= NSEC_PER_SEC;
tp.tv_sec++;
}
ntpc_apply_offset(&tp, &curr_realtime, offset);

/* Set the system time */

Expand Down

0 comments on commit df4cdba

Please sign in to comment.