Skip to content

Commit

Permalink
rg_network: Use esp-idf's sntp client instead of my custom solution
Browse files Browse the repository at this point in the history
  • Loading branch information
ducalex committed Jul 23, 2024
1 parent 2e5b552 commit e530c42
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 59 deletions.
67 changes: 9 additions & 58 deletions components/retro-go/rg_network.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@
#ifdef RG_ENABLE_NETWORKING
#include <esp_http_client.h>
#include <esp_system.h>
#include <esp_sntp.h>
#include <esp_wifi.h>
#include <esp_event.h>
#include <nvs_flash.h>
#include <lwip/err.h>
#include <lwip/sys.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netdb.h>

static rg_network_t network = {0};
static rg_wifi_config_t wifi_config = {0};
Expand Down Expand Up @@ -79,12 +77,13 @@ static void network_event_handler(void *arg, esp_event_base_t event_base, int32_
network.channel = wifidata.primary;
network.rssi = wifidata.rssi;
}
network.state = RG_NETWORK_CONNECTED;

RG_LOGI("Connected! IP: %s, RSSI: %d", network.ip_addr, network.rssi);
network.state = RG_NETWORK_CONNECTED;
if (rg_network_sync_time("pool.ntp.org", 0))
rg_system_save_time();
rg_system_event(RG_EVENT_NETWORK_CONNECTED, NULL);

sntp_stop();
sntp_init();
}
else if (event_id == IP_EVENT_AP_STAIPASSIGNED)
{
Expand Down Expand Up @@ -212,58 +211,6 @@ rg_network_t rg_network_get_info(void)
#endif
}

bool rg_network_sync_time(const char *host, int *out_delta)
{
#ifdef RG_ENABLE_NETWORKING
RG_ASSERT(host, "bad param");
int sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
struct hostent *server = gethostbyname(host);
struct sockaddr_in serv_addr = {};
struct timeval timeout = {2, 0};
struct timeval ntp_time = {0, 0};
struct timeval cur_time;

if (server == NULL)
{
RG_LOGE("Failed to resolve NTP server hostname");
return false;
}

size_t addr_length = RG_MIN(server->h_length, sizeof(serv_addr.sin_addr.s_addr));
memcpy(&serv_addr.sin_addr.s_addr, server->h_addr, addr_length);
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(123);

uint32_t ntp_packet[12] = {0x0000001B, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; // li, vn, mode.

setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
connect(sockfd, (void *)&serv_addr, sizeof(serv_addr));
send(sockfd, &ntp_packet, sizeof(ntp_packet), 0);

if (recv(sockfd, &ntp_packet, sizeof(ntp_packet), 0) >= 0)
{
ntp_time.tv_sec = ntohl(ntp_packet[10]) - 2208988800UL; // DIFF_SEC_1900_1970;
ntp_time.tv_usec = (((int64_t)ntohl(ntp_packet[11]) * 1000000) >> 32);

gettimeofday(&cur_time, NULL);
settimeofday(&ntp_time, NULL);

int64_t prev_millis = ((((int64_t)cur_time.tv_sec * 1000000) + cur_time.tv_usec) / 1000);
int64_t now_millis = ((int64_t)ntp_time.tv_sec * 1000000 + ntp_time.tv_usec) / 1000;
int ntp_time_delta = (now_millis - prev_millis);

RG_LOGI("Received Time: %.24s, we were %dms %s\n", ctime(&ntp_time.tv_sec), abs((int)ntp_time_delta),
ntp_time_delta < 0 ? "ahead" : "behind");

if (out_delta)
*out_delta = ntp_time_delta;
return true;
}
#endif
RG_LOGE("Failed to receive NTP time.\n");
return false;
}

void rg_network_deinit(void)
{
#ifdef RG_ENABLE_NETWORKING
Expand Down Expand Up @@ -291,6 +238,10 @@ bool rg_network_init(void)
esp_netif_create_default_wifi_sta();
esp_netif_create_default_wifi_ap();

sntp_setoperatingmode(SNTP_OPMODE_POLL);
sntp_setservername(0, "pool.ntp.org");
// sntp_init();

// Wifi may use nvs for calibration data
if (nvs_flash_init() != ESP_OK && nvs_flash_erase() == ESP_OK)
nvs_flash_init();
Expand Down
1 change: 0 additions & 1 deletion components/retro-go/rg_network.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ bool rg_network_wifi_set_config(const rg_wifi_config_t *config);
bool rg_network_wifi_load_config(int slot);
bool rg_network_wifi_start(void);
void rg_network_wifi_stop(void);
bool rg_network_sync_time(const char *host, int *out_delta);
rg_network_t rg_network_get_info(void);

typedef struct
Expand Down
8 changes: 8 additions & 0 deletions components/retro-go/rg_system.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ static void system_monitor_task(void *arg)
{
bool batteryLedState = false;
int64_t nextLoopTime = 0;
time_t prevTime = time(NULL);

rg_task_delay(2000);

Expand Down Expand Up @@ -237,6 +238,13 @@ static void system_monitor_task(void *arg)
}
}

if (abs(prevTime - rtcValue) > 60)
{
RG_LOGI("System time suddenly changed %d seconds.", (int)(rtcValue - prevTime));
rg_system_save_time(); // Not sure if this is thread safe...
}
prevTime = rtcValue;

if (nextLoopTime > rg_system_timer())
{
rg_task_delay((nextLoopTime - rg_system_timer()) / 1000 + 1);
Expand Down

0 comments on commit e530c42

Please sign in to comment.