Skip to content

Commit

Permalink
Merge pull request #48 from umts/sntp-changes
Browse files Browse the repository at this point in the history
Fix SNTP request errors
  • Loading branch information
bandogora authored Jan 4, 2025
2 parents 6ebc0c4 + 64e45f1 commit 2e70227
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 39 deletions.
2 changes: 1 addition & 1 deletion app/VERSION
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
VERSION_MAJOR = 0
VERSION_MINOR = 9
PATCHLEVEL = 0
PATCHLEVEL = 1
VERSION_TWEAK = 0
EXTRAVERSION = stable
76 changes: 76 additions & 0 deletions app/src/net/ntp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include "ntp.h"

#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>

LOG_MODULE_REGISTER(ntp);

#define SNTP_PORT "123"

struct sntp_time time_stamp;

static int ntp_request(char *url) {
int err;
struct sntp_ctx ctx;

struct zsock_addrinfo *addr_inf;
static struct zsock_addrinfo hints = {.ai_socktype = SOCK_DGRAM, .ai_flags = AI_NUMERICSERV};

err = zsock_getaddrinfo(url, SNTP_PORT, &hints, &addr_inf);
if (err) {
LOG_ERR("getaddrinfo() failed, %s", strerror(errno));
return errno;
}

if (addr_inf->ai_family == AF_INET) {
err = sntp_init(&ctx, addr_inf->ai_addr, sizeof(struct sockaddr_in));
} else {
err = sntp_init(&ctx, addr_inf->ai_addr, sizeof(struct sockaddr_in6));
}

if (err < 0) {
LOG_ERR("Failed to init SNTP ctx: %d", err);
goto end;
}

err = sntp_query(&ctx, CONFIG_NTP_REQUEST_TIMEOUT_MS, &time_stamp);
if (err) {
LOG_ERR("SNTP request failed: %d", err);
}

end:
sntp_close(&ctx);
return err;
}

int get_ntp_time(void) {
int err;

/* Get sntp time */
for (int rc = 0; rc < (CONFIG_NTP_FETCH_RETRY_COUNT * 2); rc++) {
if (rc < CONFIG_NTP_FETCH_RETRY_COUNT) {
err = ntp_request(CONFIG_PRIMARY_NTP_SERVER);
} else {
err = ntp_request(CONFIG_FALLBACK_NTP_SERVER);
}

if (err && (rc == (CONFIG_NTP_FETCH_RETRY_COUNT * 2) - 1)) {
LOG_ERR(
"Failed to get time from all NTP pools! Err: %i\n Check your network "
"connection.",
err
);
} else if (err && (rc == CONFIG_NTP_FETCH_RETRY_COUNT - 1)) {
LOG_WRN(
"Unable to get time after %d tries from NTP "
"pool " CONFIG_PRIMARY_NTP_SERVER " . Err: %i\n Attempting to use fallback NTP pool...",
CONFIG_NTP_FETCH_RETRY_COUNT, err
);
} else if (err) {
LOG_WRN("Failed to get time using SNTP, Err: %i. Retrying...", err);
} else {
break;
}
}
return err;
}
13 changes: 13 additions & 0 deletions app/src/net/ntp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef NTP_H
#define NTP_H

#include <zephyr/net/sntp.h>

extern struct sntp_time time_stamp;

/** @fn int get_ntp_time(void)
* @brief Get NTP time usiing Zephyr's sntp client.
*/
int get_ntp_time(void);

#endif // NTP_H
50 changes: 12 additions & 38 deletions app/src/real_time_counter.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
#include <zephyr/drivers/counter.h>
#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>
#include <zephyr/net/sntp.h>
#include <zephyr/sys/util.h>

#include "net/ntp.h"

#define RTC DEVICE_DT_GET(DT_ALIAS(rtc))

LOG_MODULE_REGISTER(real_time_counter);
Expand All @@ -15,41 +16,8 @@ K_MUTEX_DEFINE(rtc_mutex);

const struct device *const rtc = RTC;

static struct sntp_time ts;

K_SEM_DEFINE(rtc_sync_sem, 0, 1);

static void get_ntp_time(void) {
int err;

/* Get sntp time */
for (int rc = 0; rc < (CONFIG_NTP_FETCH_RETRY_COUNT * 2); rc++) {
if (rc < CONFIG_NTP_FETCH_RETRY_COUNT) {
err = sntp_simple(CONFIG_PRIMARY_NTP_SERVER, CONFIG_NTP_REQUEST_TIMEOUT_MS, &ts);
} else {
err = sntp_simple(CONFIG_FALLBACK_NTP_SERVER, CONFIG_NTP_REQUEST_TIMEOUT_MS, &ts);
}

if (err && (rc == (CONFIG_NTP_FETCH_RETRY_COUNT * 2) - 1)) {
LOG_ERR(
"Failed to get time from all NTP pools! Err: %i\n Check your network "
"connection.",
err
);
} else if (err && (rc == CONFIG_NTP_FETCH_RETRY_COUNT - 1)) {
LOG_WRN(
"Unable to get time after %d tries from NTP "
"pool " CONFIG_PRIMARY_NTP_SERVER " . Err: %i\n Attempting to use fallback NTP pool...",
CONFIG_NTP_FETCH_RETRY_COUNT, err
);
} else if (err) {
LOG_WRN("Failed to get time using SNTP, Err: %i. Retrying...", err);
} else {
break;
}
}
}

static void counter_top_callback(
const struct device *counter_dev, void *user_data
) {
Expand Down Expand Up @@ -92,10 +60,16 @@ int set_rtc_time(void) {
LOG_ERR("Failed to set RTC top value, ERR: %d", err);
}

(void)get_ntp_time();
err = get_ntp_time();
if (err) {
LOG_ERR("Failed to get NTP time, ERR: %d", err);
k_msleep(CONFIG_NTP_REQUEST_TIMEOUT_MS);
goto clean_up;
}

LOG_INF(
"time since Epoch: high word: %u, low word: %u",
(uint32_t)(ts.seconds >> 32), (uint32_t)ts.seconds
"time since Epoch: high word: %u, low word: %u", (uint32_t)(time_stamp.seconds >> 32),
(uint32_t)time_stamp.seconds
);

err = counter_start(rtc);
Expand Down Expand Up @@ -125,5 +99,5 @@ unsigned int get_rtc_time(void) {

k_mutex_unlock(&rtc_mutex);

return ((uint32_t)ts.seconds + (ticks / counter_get_frequency(rtc)));
return ((uint32_t)time_stamp.seconds + (ticks / counter_get_frequency(rtc)));
}

0 comments on commit 2e70227

Please sign in to comment.