

#include <stdio.h>
#include <stddef.h>
#include <limits.h>
#include "os/os_fault.h"
#include "syscfg/syscfg.h"
#include "sysinit/sysinit.h"

static void
sysinit_dflt_panic_cb(const char *file, int line, const char *func,
                      const char *expr, const char *msg)
{
#if MYNEWT_VAL(SYSINIT_PANIC_MESSAGE)
    if (msg != NULL) {
        fprintf(stderr, "sysinit failure: %s\n", msg);
    }
#endif

    __assert_func(file, line, func, expr);
}

sysinit_panic_fn *sysinit_panic_cb = sysinit_dflt_panic_cb;

uint8_t sysinit_active;

/**
 * Sets the sysinit panic function; i.e., the function which executes when
 * initialization fails.  By default, a panic triggers a failed assertion.
 */
void
sysinit_panic_set(sysinit_panic_fn *panic_cb)
{
    sysinit_panic_cb = panic_cb;
}

void
sysinit_start(void)
{
    sysinit_active = 1;
}

void
sysinit_end(void)
{
    sysinit_active = 0;
}
