167 lines
4.6 KiB
C++
167 lines
4.6 KiB
C++
//
|
|
// Created by erki on 25/01/24.
|
|
//
|
|
|
|
#include "clock_core.hpp"
|
|
|
|
#include "esp_expected.hpp"
|
|
|
|
#include <cstring>
|
|
|
|
#include <esp_event.h>
|
|
#include <esp_log.h>
|
|
#include <esp_netif.h>
|
|
#include <esp_netif_sntp.h>
|
|
#include <esp_wifi.h>
|
|
|
|
namespace
|
|
{
|
|
|
|
const char* TAG = "clock_core";
|
|
|
|
constexpr EventBits_t WIFI_FAIL_BIT = BIT0;
|
|
constexpr EventBits_t WIFI_CONNECTED_BIT = BIT1;
|
|
constexpr EventBits_t SNTP_SYNCED = BIT2;
|
|
EventGroupHandle_t clock_core_event_group_;
|
|
|
|
void wifiEventHandler(void* arg, esp_event_base_t event_base,
|
|
int32_t event_id, void* event_data)
|
|
{
|
|
static int retry_num = 0;
|
|
|
|
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START)
|
|
{
|
|
esp_wifi_connect();
|
|
}
|
|
else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED)
|
|
{
|
|
if (retry_num < 10)
|
|
{
|
|
esp_wifi_connect();
|
|
retry_num++;
|
|
ESP_LOGI(TAG, "Retrying to connect to AP.");
|
|
}
|
|
else
|
|
{
|
|
xEventGroupSetBits(clock_core_event_group_, WIFI_FAIL_BIT);
|
|
}
|
|
ESP_LOGW(TAG, "Connection to AP failed.");
|
|
}
|
|
else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP)
|
|
{
|
|
auto* event = static_cast<ip_event_got_ip_t*>(event_data);
|
|
ESP_LOGI(TAG, "Connected to AP. Got IP: " IPSTR, IP2STR(&event->ip_info.ip));
|
|
retry_num = 0;
|
|
|
|
xEventGroupSetBits(clock_core_event_group_, WIFI_CONNECTED_BIT);
|
|
}
|
|
}
|
|
|
|
void sntpEventHandler(struct timeval*)
|
|
{
|
|
xEventGroupSetBits(clock_core_event_group_, SNTP_SYNCED);
|
|
}
|
|
|
|
esp_err_t sntpInitialize()
|
|
{
|
|
esp_sntp_config_t config = ESP_NETIF_SNTP_DEFAULT_CONFIG("pool.ntp.org");
|
|
config.smooth_sync = true;
|
|
config.server_from_dhcp = true;
|
|
config.renew_servers_after_new_IP = true;
|
|
config.sync_cb = sntpEventHandler;
|
|
config.start = false;
|
|
config.ip_event_to_renew = IP_EVENT_STA_GOT_IP;
|
|
|
|
return esp_netif_sntp_init(&config);
|
|
}
|
|
|
|
std::expected<void, esp_err_t> wifiInitialize(const char* wifi_ssid, const char* wifi_password)
|
|
{
|
|
TRY_ESP(esp_netif_init());
|
|
TRY_ESP(esp_event_loop_create_default());
|
|
esp_netif_create_default_wifi_sta();
|
|
|
|
wifi_init_config_t config = WIFI_INIT_CONFIG_DEFAULT();
|
|
TRY_ESP(esp_wifi_init(&config));
|
|
|
|
TRY_ESP(sntpInitialize());
|
|
|
|
esp_event_handler_instance_t instance_any_id;
|
|
esp_event_handler_instance_t instance_got_ip;
|
|
TRY_ESP(esp_event_handler_instance_register(WIFI_EVENT,
|
|
ESP_EVENT_ANY_ID,
|
|
&wifiEventHandler,
|
|
nullptr,
|
|
&instance_any_id));
|
|
TRY_ESP(esp_event_handler_instance_register(IP_EVENT,
|
|
IP_EVENT_STA_GOT_IP,
|
|
&wifiEventHandler,
|
|
nullptr,
|
|
&instance_got_ip));
|
|
|
|
wifi_sta_config_t sta_config;
|
|
std::memset(&sta_config, 0, sizeof(sta_config));
|
|
std::strncpy((char*) sta_config.ssid, wifi_ssid, 32);
|
|
std::strncpy((char*) sta_config.password, wifi_password, 64);
|
|
sta_config.threshold.authmode = WIFI_AUTH_WPA2_PSK;
|
|
|
|
wifi_config_t wifi_config = { .sta = sta_config };
|
|
|
|
TRY_ESP(esp_wifi_set_mode(WIFI_MODE_STA));
|
|
TRY_ESP(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
|
|
TRY_ESP(esp_wifi_start());
|
|
|
|
return {};
|
|
}
|
|
|
|
}
|
|
|
|
namespace clock_core
|
|
{
|
|
|
|
void run(const char* wifi_ssid, const char* wifi_password)
|
|
{
|
|
clock_core_event_group_ = xEventGroupCreate();
|
|
|
|
if (const auto res = wifiInitialize(wifi_ssid, wifi_password);
|
|
!res.has_value())
|
|
{
|
|
ESP_LOGI(TAG, "WiFi setup errored in clock_core::run. Error: %s", esp_err_to_name(res.error()));
|
|
abort();
|
|
}
|
|
|
|
while (true)
|
|
{
|
|
const EventBits_t bits = xEventGroupWaitBits(clock_core_event_group_,
|
|
WIFI_CONNECTED_BIT | WIFI_FAIL_BIT | SNTP_SYNCED,
|
|
pdTRUE,
|
|
pdFALSE,
|
|
portMAX_DELAY);
|
|
|
|
if (bits & WIFI_CONNECTED_BIT)
|
|
{
|
|
ESP_LOGI(TAG, "WiFi setup in clock_core::run successful.");
|
|
if (const auto res = esp_netif_sntp_start();
|
|
res != ESP_OK)
|
|
{
|
|
ESP_LOGE(TAG, "SNTP setup failed. Error: %s", esp_err_to_name(res));
|
|
abort();
|
|
}
|
|
}
|
|
else if (bits & WIFI_FAIL_BIT)
|
|
{
|
|
ESP_LOGE(TAG, "WiFi setup in clock_core::run failed.");
|
|
}
|
|
else if (bits & SNTP_SYNCED)
|
|
{
|
|
ESP_LOGI(TAG, "SNTP sync successful.");
|
|
}
|
|
else
|
|
{
|
|
ESP_LOGE(TAG, "WiFi setup encountered an unexpected set of bits. %lu", bits);
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|