82 lines
2.6 KiB
C++

#include "nvs_flash.h"
#include "nvs.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "sdkconfig.h"
#include <cinttypes>
#include <cstdio>
#include <expected>
#include "esp_expected.hpp"
#include "wifi_provisioner.hpp"
extern "C" void app_main(void)
{
esp_err_t err = nvs_flash_init();
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND)
{
// NVS partition was truncated and needs to be erased
// Retry nvs_flash_init
ESP_ERROR_CHECK(nvs_flash_erase());
err = nvs_flash_init();
}
ESP_ERROR_CHECK(err);
auto* provisioner = new WifiProvisioner("wifi_settings", [task_id = xTaskGetCurrentTaskHandle()](const auto& params)
{
printf("Settings successfully done.");
xTaskNotify(task_id, 0, eNoAction);
});
if (!provisioner->addParameter("SSID", "ssid", WifiProvisioner::Parameter::Type::STRING).has_value())
{
printf("Error adding parameter.");
return;
}
if (!provisioner->addParameter("Password", "password", WifiProvisioner::Parameter::Type::STRING).has_value())
{
printf("Error adding parameter.");
return;
}
if (!provisioner->parametersAreConfigured())
{
auto result = provisioner->startProvisioning();
if (!result.has_value())
{
ESP_ERROR_CHECK(result.error());
}
printf("Provisioning started...\n");
const auto notification_received = xTaskNotifyWait(pdFALSE, ULONG_MAX, nullptr, portMAX_DELAY);
if (notification_received == pdPASS)
{
printf("Notification happened.");
}
else
{
abortWithError("Main task notification timed out.");
}
}
const auto password = unwrapOrAbort(provisioner->getParameter("password")
.and_then([] (const auto& value)
{
return value.template getValue<etl::string<100>>();
}));
const auto ssid = unwrapOrAbort(provisioner->getParameter("ssid")
.and_then([] (const auto& value)
{
return value.template getValue<etl::string<100>>();
}));
// Pack up the provisioner.
delete provisioner;
printf("We now have a wifi with SSID: %s, password: %s\n", ssid.c_str(), password.c_str());
}