static void
boot_test_util_init_flash(void)
{
    const struct nffs_area_desc *area_desc;
    int rc;

    rc = hal_flash_init();
    TEST_ASSERT(rc == 0);

    for (area_desc = boot_test_area_descs;
         area_desc->nad_length != 0;
         area_desc++) {

        rc = hal_flash_erase(area_desc->nad_flash_id, area_desc->nad_offset,
                             area_desc->nad_length);
        TEST_ASSERT(rc == 0);
    }

    rc = nffs_init();
    TEST_ASSERT(rc == 0);

    rc = nffs_format(boot_test_format_descs);
    TEST_ASSERT(rc == 0);

    rc = fs_mkdir("/boot");
    TEST_ASSERT(rc == 0);
}
int
tr_init(void)
{
    int rc;

    if (tr_config.tc_base_path != NULL) {
        if (tr_config.tc_area_descs != NULL) {
            rc = hal_flash_init();
            if (rc != 0) {
                return -1;
            }

            rc = nffs_init();
            if (rc != 0) {
                return -1;
            }

            rc = nffs_detect(tr_config.tc_area_descs);
            if (rc == FS_ECORRUPT) {
                rc = nffs_format(tr_config.tc_area_descs);
            }
            if (rc != 0) {
                return -1;
            }
        }

        rc = tr_results_read_status();
        if (rc != 0) {
            tr_results_rmdir_results();
        }

        rc = tr_results_mkdir_results();
        if (rc != 0) {
            return -1;
        }

        rc = tr_results_mkdir_meta();
        if (rc != 0) {
            return -1;
        }
    }

    tu_config.tc_case_init_cb = tr_case_init;
    tu_config.tc_case_fail_cb = tr_case_fail;
    tu_config.tc_case_pass_cb = tr_case_pass;
    tu_config.tc_suite_init_cb = tr_suite_init;
    tu_config.tc_restart_cb = tr_restart;

    return 0;
}
/**
 * main
 *  
 * The main function for the project. This function initializes the os, calls 
 * init_tasks to initialize tasks (and possibly other objects), then starts the 
 * OS. We should not return from os start. 
 *  
 * @return int NOTE: this function should never return!
 */
int
main(void)
{
    int i;
    int rc;
    int cnt;
    uint32_t seed;
    struct nffs_area_desc descs[NFFS_AREA_MAX];

    /* Initialize OS */
    os_init();

    /* Set cputime to count at 1 usec increments */
    rc = cputime_init(1000000);
    assert(rc == 0);

    rc = os_mempool_init(&g_mbuf_mempool, MBUF_NUM_MBUFS, 
            MBUF_MEMBLOCK_SIZE, &g_mbuf_buffer[0], "mbuf_pool");
    assert(rc == 0);

    rc = os_mbuf_pool_init(&g_mbuf_pool, &g_mbuf_mempool, MBUF_MEMBLOCK_SIZE, 
                           MBUF_NUM_MBUFS);
    assert(rc == 0);

    rc = os_msys_register(&g_mbuf_pool);
    assert(rc == 0);

    /* Dummy device address */
#if BLETEST_CFG_ROLE == BLETEST_ROLE_ADVERTISER
    g_dev_addr[0] = 0x00;
    g_dev_addr[1] = 0x00;
    g_dev_addr[2] = 0x00;
    g_dev_addr[3] = 0x88;
    g_dev_addr[4] = 0x88;
    g_dev_addr[5] = 0x08;

    g_bletest_cur_peer_addr[0] = 0x00;
    g_bletest_cur_peer_addr[1] = 0x00;
    g_bletest_cur_peer_addr[2] = 0x00;
    g_bletest_cur_peer_addr[3] = 0x99;
    g_bletest_cur_peer_addr[4] = 0x99;
    g_bletest_cur_peer_addr[5] = 0x09;
#else
    g_dev_addr[0] = 0x00;
    g_dev_addr[1] = 0x00;
    g_dev_addr[2] = 0x00;
    g_dev_addr[3] = 0x99;
    g_dev_addr[4] = 0x99;
    g_dev_addr[5] = 0x09;

    g_bletest_cur_peer_addr[0] = 0x00;
    g_bletest_cur_peer_addr[1] = 0x00;
    g_bletest_cur_peer_addr[2] = 0x00;
    g_bletest_cur_peer_addr[3] = 0x88;
    g_bletest_cur_peer_addr[4] = 0x88;
    g_bletest_cur_peer_addr[5] = 0x08;
#endif

    /* 
     * Seed random number generator with least significant bytes of device
     * address.
     */ 
    seed = 0;
    for (i = 0; i < 4; ++i) {
        seed |= g_dev_addr[i];
        seed <<= 8;
    }
    srand(seed);

    /* Set the led pin as an output */
    g_led_pin = LED_BLINK_PIN;
    gpio_init_out(g_led_pin, 1);

    /* Init the console */
    rc = console_init(shell_console_rx_cb);
    assert(rc == 0);

    rc = hal_flash_init();
    assert(rc == 0);

    nffs_config.nc_num_inodes = 32;
    nffs_config.nc_num_blocks = 64;
    nffs_config.nc_num_files = 2;
    nffs_config.nc_num_dirs = 2;
    rc = nffs_init();
    assert(rc == 0);

    cnt = NFFS_AREA_MAX;
    rc = flash_area_to_nffs_desc(FLASH_AREA_NFFS, &cnt, descs);
    assert(rc == 0);
    if (nffs_detect(descs) == FS_ECORRUPT) {
        rc = nffs_format(descs);
        assert(rc == 0);
    }

    shell_task_init(SHELL_TASK_PRIO, shell_stack, SHELL_TASK_STACK_SIZE,
                         SHELL_MAX_INPUT_LEN);

    nmgr_task_init(NEWTMGR_TASK_PRIO, newtmgr_stack, NEWTMGR_TASK_STACK_SIZE);
    imgmgr_module_init();

    /* Init statistics module */
    stats_module_init();

    /* Init tasks */
    init_tasks();

    /* Start the OS */
    os_start();

    /* os start should never return. If it does, this should be an error */
    assert(0);

    return rc;
}