46 lines
965 B
C
46 lines
965 B
C
/**
|
|
* @file sntp.c
|
|
* @brief Implementation of SNTP client module.
|
|
*/
|
|
|
|
#include "sntp.h"
|
|
|
|
#include "esp_sntp.h"
|
|
#include "esp_log.h"
|
|
|
|
#include <time.h>
|
|
#include <sys/time.h>
|
|
|
|
static const char *TAG = "sntp";
|
|
|
|
static volatile eSntpState sntpState = SYNC_NOT_STARTED;
|
|
|
|
static void time_sync_notification_cb(struct timeval *tv);
|
|
|
|
esp_err_t initSntp(void)
|
|
{
|
|
esp_sntp_setoperatingmode(SNTP_OPMODE_POLL);
|
|
esp_sntp_setservername(0, CONFIG_SNTP_SERVER_IP_ADDR);
|
|
|
|
sntp_set_time_sync_notification_cb(time_sync_notification_cb);
|
|
esp_sntp_init();
|
|
|
|
ESP_LOGI(TAG, "Initialized successfully, server: %s", CONFIG_SNTP_SERVER_IP_ADDR);
|
|
return ESP_OK;
|
|
}
|
|
|
|
eSntpState getSntpState(void)
|
|
{
|
|
return sntpState;
|
|
}
|
|
|
|
/**
|
|
* @brief SNTP time sync callback.
|
|
* @param tv Synchronized time value.
|
|
*/
|
|
static void time_sync_notification_cb(struct timeval *tv)
|
|
{
|
|
ESP_LOGI(TAG, "Time synchronized! Unix Time: %lld", tv->tv_sec);
|
|
sntpState = SYNC_SUCCESSFUL;
|
|
}
|