Skip to content

Commit f65727a

Browse files
pfalconjukkar
authored andcommitted
net: sntp: Add sntp_query() function with fractional precision
Existing sntp_request() function has a coarse integer seconds precision, discarding fractional part as returned by SNTP. Deprecate it, and instead introduce sntp_query() function which returns both integer and fractional seconds as a newly introduced structure sntp_tstamp. Fixes: #15596 Signed-off-by: Paul Sokolovsky <[email protected]>
1 parent 901d85b commit f65727a

File tree

2 files changed

+40
-9
lines changed

2 files changed

+40
-9
lines changed

include/net/sntp.h

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ struct sntp_ctx {
3232
u32_t expected_orig_ts;
3333
};
3434

35+
/** Time as returned by SNTP API, fractional seconds since 1 Jan 1970 */
36+
struct sntp_time {
37+
u64_t seconds;
38+
u32_t fraction;
39+
};
40+
3541
/**
3642
* @brief Initialize SNTP context
3743
*
@@ -45,15 +51,29 @@ int sntp_init(struct sntp_ctx *ctx, struct sockaddr *addr,
4551
socklen_t addr_len);
4652

4753
/**
48-
* @brief Send SNTP request
54+
* @brief SNTP query with seconds precision (deprecated)
4955
*
5056
* @param ctx Address of sntp context.
5157
* @param timeout Timeout of waiting for sntp response (in milliseconds).
5258
* @param epoch_time Seconds since 1 January 1970 (output).
5359
*
5460
* @return 0 if ok, <0 if error (-ETIMEDOUT if timeout).
5561
*/
56-
int sntp_request(struct sntp_ctx *ctx, u32_t timeout, u64_t *epoch_time);
62+
__deprecated int sntp_request(struct sntp_ctx *ctx, u32_t timeout,
63+
u64_t *epoch_time);
64+
65+
/**
66+
* @brief Perform SNTP query
67+
*
68+
* @param ctx Address of sntp context.
69+
* @param timeout Timeout of waiting for sntp response (in milliseconds).
70+
* @param time Timestamp including integer and fractional seconds since
71+
* 1 Jan 1970 (output).
72+
*
73+
* @return 0 if ok, <0 if error (-ETIMEDOUT if timeout).
74+
*/
75+
int sntp_query(struct sntp_ctx *ctx, u32_t timeout,
76+
struct sntp_time *time);
5777

5878
/**
5979
* @brief Release SNTP context

subsys/net/lib/sntp/sntp.c

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ static void sntp_pkt_dump(struct sntp_pkt *pkt)
4444
}
4545

4646
static s32_t parse_response(u8_t *data, u16_t len, u32_t orig_ts,
47-
u64_t *epoch_time)
47+
struct sntp_time *time)
4848
{
4949
struct sntp_pkt *pkt = (struct sntp_pkt *)data;
5050
u32_t ts;
@@ -76,6 +76,7 @@ static s32_t parse_response(u8_t *data, u16_t len, u32_t orig_ts,
7676
return -EINVAL;
7777
}
7878

79+
time->fraction = ntohl(pkt->tx_tm_f);
7980
ts = ntohl(pkt->tx_tm_s);
8081

8182
/* Check if most significant bit is set */
@@ -84,22 +85,22 @@ static s32_t parse_response(u8_t *data, u16_t len, u32_t orig_ts,
8485
* on 1 January 1900.
8586
*/
8687
if (ts >= OFFSET_1970_JAN_1) {
87-
*epoch_time = ts - OFFSET_1970_JAN_1;
88+
time->seconds = ts - OFFSET_1970_JAN_1;
8889
} else {
8990
return -EINVAL;
9091
}
9192
} else {
9293
/* UTC time is reckoned from 6h 28m 16s UTC
9394
* on 7 February 2036.
9495
*/
95-
*epoch_time = ts + 0x100000000 - OFFSET_1970_JAN_1;
96+
time->seconds = ts + 0x100000000ULL - OFFSET_1970_JAN_1;
9697
}
9798

9899
return 0;
99100
}
100101

101102
static int sntp_recv_response(struct sntp_ctx *sntp, u32_t timeout,
102-
u64_t *epoch_time)
103+
struct sntp_time *time)
103104
{
104105
struct sntp_pkt buf = { 0 };
105106
int status;
@@ -126,7 +127,7 @@ static int sntp_recv_response(struct sntp_ctx *sntp, u32_t timeout,
126127

127128
status = parse_response((u8_t *)&buf, sizeof(buf),
128129
sntp->expected_orig_ts,
129-
epoch_time);
130+
time);
130131
return status;
131132
}
132133

@@ -169,11 +170,21 @@ int sntp_init(struct sntp_ctx *ctx, struct sockaddr *addr, socklen_t addr_len)
169170
}
170171

171172
int sntp_request(struct sntp_ctx *ctx, u32_t timeout, u64_t *epoch_time)
173+
{
174+
struct sntp_time time;
175+
int res = sntp_query(ctx, timeout, &time);
176+
177+
*epoch_time = time.seconds;
178+
179+
return res;
180+
}
181+
182+
int sntp_query(struct sntp_ctx *ctx, u32_t timeout, struct sntp_time *time)
172183
{
173184
struct sntp_pkt tx_pkt = { 0 };
174185
int ret = 0;
175186

176-
if (!ctx || !epoch_time) {
187+
if (!ctx || !time) {
177188
return -EFAULT;
178189
}
179190

@@ -190,7 +201,7 @@ int sntp_request(struct sntp_ctx *ctx, u32_t timeout, u64_t *epoch_time)
190201
return ret;
191202
}
192203

193-
return sntp_recv_response(ctx, timeout, epoch_time);
204+
return sntp_recv_response(ctx, timeout, time);
194205
}
195206

196207
void sntp_close(struct sntp_ctx *ctx)

0 commit comments

Comments
 (0)