#include <zephyr.h>
#include <dfu/mcuboot.h>
#include <zephyr/sys/reboot.h>

#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(appcore);


void main(void)
{
    int retval = 0;
    LOG_INF("Starting nRF5340 DFU via extfalsh...[%s]", log_strdup(CONFIG_BOARD));
    LOG_INF("%s %s", __DATE__, __TIME__);

    if(boot_is_img_confirmed()){
        LOG_INF("Running image is confirmed.");
    } else {
        LOG_INF("Running image is NOT confirmed, start confirming...");
        retval = boot_write_img_confirmed_multi(0);

        if(retval){
            LOG_ERR("NOT confirmed, retval: %d.", retval);
            sys_reboot(SYS_REBOOT_COLD);
        }
        LOG_INF("Confirmed.");
    }

    LOG_INF("Boot new image after 10s.");
    for(int i = 10; i != 0; i--){
        LOG_INF("[%d].", i);
        k_sleep(K_SECONDS(1));
    }

    boot_request_upgrade_multi(0, BOOT_UPGRADE_TEST);
    // sys_reboot(SYS_REBOOT_COLD);
}
