示例#1
0
static enum cc3200_init_result cc3200_init(void *arg) {
  mongoose_init();
  if (miot_uart_init(0, miot_uart_default_config(), NULL, NULL) == NULL) {
    return CC3200_INIT_UART_INIT_FAILED;
  }
  if (miot_uart_init(1, miot_uart_default_config(), NULL, NULL) == NULL) {
    return CC3200_INIT_UART_INIT_FAILED;
  }

  if (strcmp(MIOT_APP, "mongoose-iot") != 0) {
    LOG(LL_INFO, ("%s %s (%s)", MIOT_APP, build_version, build_id));
  }
  LOG(LL_INFO,
      ("Mongoose IoT Firmware %s (%s)", mg_build_version, mg_build_id));
  LOG(LL_INFO, ("RAM: %d total, %d free", miot_get_heap_size(),
                miot_get_free_heap_size()));

  int r = start_nwp();
  if (r < 0) {
    LOG(LL_ERROR, ("Failed to start NWP: %d", r));
    return CC3200_INIT_FAILED_TO_START_NWP;
  }

  g_boot_cfg_idx = get_active_boot_cfg_idx();
  if (g_boot_cfg_idx < 0 || read_boot_cfg(g_boot_cfg_idx, &g_boot_cfg) < 0) {
    return CC3200_INIT_FAILED_TO_READ_BOOT_CFG;
  }

  LOG(LL_INFO, ("Boot cfg %d: 0x%llx, 0x%u, %s @ 0x%08x, %s", g_boot_cfg_idx,
                g_boot_cfg.seq, g_boot_cfg.flags, g_boot_cfg.app_image_file,
                g_boot_cfg.app_load_addr, g_boot_cfg.fs_container_prefix));

  if (g_boot_cfg.flags & BOOT_F_FIRST_BOOT) {
    /* Tombstone the current config. If anything goes wrong between now and
     * commit, next boot will use the old one. */
    uint64_t saved_seq = g_boot_cfg.seq;
    g_boot_cfg.seq = BOOT_CFG_TOMBSTONE_SEQ;
    write_boot_cfg(&g_boot_cfg, g_boot_cfg_idx);
    g_boot_cfg.seq = saved_seq;
  }

  r = cc3200_fs_init(g_boot_cfg.fs_container_prefix);
  if (r < 0) {
    LOG(LL_ERROR, ("FS init error: %d", r));
    return CC3200_INIT_FS_INIT_FAILED;
  } else {
    /*
     * We aim to maintain at most 3 FS containers at all times.
     * Delete inactive FS container in the inactive boot configuration.
     */
    struct boot_cfg cfg;
    int inactive_idx = (g_boot_cfg_idx == 0 ? 1 : 0);
    if (read_boot_cfg(inactive_idx, &cfg) >= 0) {
      fs_delete_inactive_container(cfg.fs_container_prefix);
    }
  }

#if MIOT_ENABLE_UPDATER
  if (g_boot_cfg.flags & BOOT_F_FIRST_BOOT) {
    LOG(LL_INFO, ("Applying update"));
    r = miot_upd_apply_update();
    if (r < 0) {
      LOG(LL_ERROR, ("Failed to apply update: %d", r));
      return CC3200_INIT_UPDATE_FAILED;
    }
  }
#endif

  enum miot_init_result ir = mg_init();
  if (ir != MIOT_INIT_OK) {
    LOG(LL_ERROR, ("%s init error: %d", "MG", ir));
    return CC3200_INIT_MG_INIT_FAILED;
  }

#if MIOT_ENABLE_JS
  struct v7 *v7 = s_v7 = init_v7(&arg);

  ir = miot_init_js_all(v7);
  if (ir != MIOT_INIT_OK) {
    LOG(LL_ERROR, ("%s init error: %d", "JS", ir));
    return CC3200_INIT_MG_INIT_JS_FAILED;
  }
#endif

#if MIOT_ENABLE_JS
  miot_prompt_init(v7, get_cfg()->debug.stdout_uart);
#endif
  return CC3200_INIT_OK;
}
示例#2
0
static int sj_init() {
  struct v7 *v7 = s_v7;

  LOG(LL_INFO, ("Mongoose IoT Firmware %s", build_id));
  LOG(LL_INFO,
      ("RAM: %d total, %d free", sj_get_heap_size(), sj_get_free_heap_size()));

  int r = start_nwp();
  if (r < 0) {
    LOG(LL_ERROR, ("Failed to start NWP: %d", r));
    return 0;
  }

  int boot_cfg_idx = get_active_boot_cfg_idx();
  if (boot_cfg_idx < 0) return 0;
  struct boot_cfg boot_cfg;
  if (read_boot_cfg(boot_cfg_idx, &boot_cfg) < 0) return 0;

  LOG(LL_INFO, ("Boot cfg %d: 0x%llx, 0x%u, %s @ 0x%08x, %s", boot_cfg_idx,
                boot_cfg.seq, boot_cfg.flags, boot_cfg.app_image_file,
                boot_cfg.app_load_addr, boot_cfg.fs_container_prefix));

  uint64_t saved_seq = 0;
  if (boot_cfg.flags & BOOT_F_FIRST_BOOT) {
    /* Tombstone the current config. If anything goes wrong between now and
     * commit, next boot will use the old one. */
    saved_seq = boot_cfg.seq;
    boot_cfg.seq = BOOT_CFG_TOMBSTONE_SEQ;
    write_boot_cfg(&boot_cfg, boot_cfg_idx);
  }

  r = init_fs(boot_cfg.fs_container_prefix);
  if (r < 0) {
    LOG(LL_ERROR, ("FS init error: %d", r));
    if (boot_cfg.flags & BOOT_F_FIRST_BOOT) {
      revert_update(boot_cfg_idx, &boot_cfg);
    }
    return 0;
  }

  if (boot_cfg.flags & BOOT_F_FIRST_BOOT) {
    LOG(LL_INFO, ("Applying update"));
    if (apply_update(boot_cfg_idx, &boot_cfg) < 0) {
      revert_update(boot_cfg_idx, &boot_cfg);
    }
  }

  mongoose_init();

#ifndef CS_DISABLE_JS
  v7 = s_v7 = init_v7(&v7);

  /* Disable GC during JS API initialization. */
  v7_set_gc_enabled(v7, 0);
  sj_gpio_api_setup(v7);
  sj_i2c_api_setup(v7);
  sj_wifi_api_setup(v7);
  sj_timers_api_setup(v7);
#endif

  sj_v7_ext_api_setup(v7);
  sj_init_sys(v7);
  sj_wifi_init(v7);
#ifndef DISABLE_C_CLUBBY
  sj_clubby_init();
#endif

  sj_http_api_setup(v7);

#if !defined(DISABLE_C_CLUBBY) && !defined(CS_DISABLE_JS)
  sj_clubby_api_setup(v7);
#endif

  /* Common config infrastructure. Mongoose & v7 must be initialized. */
  init_device(v7);

  sj_updater_post_init(v7);
#ifndef DISABLE_C_CLUBBY
  init_updater_clubby(v7);
#endif

#ifndef CS_DISABLE_JS
  /* SJS initialized, enable GC back, and trigger it */
  v7_set_gc_enabled(v7, 1);
  v7_gc(v7, 1);

  v7_val_t res;
  if (v7_exec_file(v7, "sys_init.js", &res) != V7_OK) {
    fprintf(stderr, "Error: ");
    v7_fprint(stderr, v7, res);
  }
#endif

  LOG(LL_INFO, ("%s init done, RAM: %d free", "Sys", sj_get_free_heap_size()));

  if (!sj_app_init(v7)) {
    LOG(LL_ERROR, ("App init failed"));
    abort();
  }
  LOG(LL_INFO, ("%s init done, RAM: %d free", "App", sj_get_free_heap_size()));

  if (boot_cfg.flags & BOOT_F_FIRST_BOOT) {
    boot_cfg.seq = saved_seq;
    commit_update(boot_cfg_idx, &boot_cfg);
    clubby_updater_finish(0);
  } else {
    /*
     * If there is no update reply state, this will just be ignored.
     * But if there is, then update was rolled back and reply will be sent.
     */
    clubby_updater_finish(-1);
  }

#ifndef CS_DISABLE_JS
  sj_prompt_init(v7);
#endif
  return 1;
}