/**
 * @file main.c
 * @author LokLiang
 * @brief 根据实际平台启动并初始化，用户 APP 入口为 void work_app_main(void *arg)
 * @version 0.1
 * @date 2023-11-24
 *
 * @copyright Copyright (c) 2023
 *
 */

#include "os_entry.h"
#include "app_main.h"

#include "ota.h"
#include "nvs_flash.h"
#include "esp_err.h"

static void _init_prev_nvs(void)
{
    esp_err_t err = nvs_flash_init();
    if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND)
    {
        // OTA app partition table has a smaller NVS partition size than the
        // non-OTA partition table. This size mismatch may cause NVS
        // initialization to fail. If this happens, we erase NVS partition and
        // initialize NVS again.
        ESP_ERROR_CHECK(nvs_flash_erase());
        err = nvs_flash_init();
    }
    ESP_ERROR_CHECK(err);
}

int app_main(void)
{
    /* 初始化平台相关 */
    _init_prev_nvs();

    /* 初始化 os 抽象层 */
    extern void work_app_main(void *arg);
    return os_entry(work_app_main);
}
