Exemple #1
0
int sj_app_init(struct v7 *v7) {

  { /* Print a message using a value from config. */
    printf("Hello, %s!\n", get_cfg()->hello.who);
  }

  { /* Turn on LED. */
    sj_gpio_set_mode(GPIO, GPIO_MODE_OUTPUT, GPIO_PULL_FLOAT);
    sj_gpio_write(GPIO, GPIO_LEVEL_HIGH);
  }

  { /* Read a file. */
    FILE *fp = fopen("README.txt", "r");
    if (fp != NULL) {
      char buf[100];
      int n = fread(buf, 1, sizeof(buf), fp);
      if (n > 0) {
        fwrite(buf, 1, n, stdout);
      }
      fclose(fp);
    }
  }

  (void) v7;
  return MG_APP_INIT_SUCCESS;
}
Exemple #2
0
static int do_wifi(const struct sys_config *cfg) {
  int result = 1;
  int gpio = cfg->wifi.ap.trigger_on_gpio;
  int trigger_ap = 0;

  wifi_set_opmode_current(STATION_MODE);
  wifi_station_set_auto_connect(0);
  wifi_station_disconnect();

  if (gpio >= 0) {
    sj_gpio_set_mode(gpio, GPIO_MODE_INPUT, GPIO_PULL_PULLUP);
    trigger_ap = sj_gpio_read(gpio) == GPIO_LEVEL_HIGH;
  }

  if (!trigger_ap && cfg->wifi.ap.mode == 2 && cfg->wifi.sta.enable) {
    wifi_set_opmode_current(STATIONAP_MODE);
    result =
        sj_wifi_setup_ap(&cfg->wifi.ap) ? sj_wifi_setup_sta(&cfg->wifi.sta) : 0;
  } else if (!trigger_ap && cfg->wifi.sta.enable) {
    wifi_set_opmode_current(STATION_MODE);
    result = sj_wifi_setup_sta(&cfg->wifi.sta);
  } else if (trigger_ap || cfg->wifi.ap.mode > 0) {
    wifi_set_opmode_current(SOFTAP_MODE);
    result = sj_wifi_setup_ap(&cfg->wifi.ap);
  } else {
    LOG(LL_WARN, ("No wifi mode specified"));
  }

  return result;
}
Exemple #3
0
int sj_app_init(struct v7 *v7) {
  (void) v7;
  printf("Hello, world!\n");
  sj_gpio_set_mode(GPIO, GPIO_MODE_OUTPUT, GPIO_PULL_FLOAT);
  sj_gpio_write(GPIO, GPIO_LEVEL_HIGH);

  return 1;
}
Exemple #4
0
static v7_val_t GPIO_setmode(struct v7 *v7, v7_val_t this_obj, v7_val_t args) {
  v7_val_t pinv = v7_array_get(v7, args, 0);
  v7_val_t modev = v7_array_get(v7, args, 1);
  v7_val_t pullv = v7_array_get(v7, args, 2);
  int pin, mode, pull;

  if (!v7_is_number(pinv) || !v7_is_number(modev) || !v7_is_number(pullv)) {
    printf("Invalid arguments");
    return v7_create_undefined();
  }

  pin = v7_to_number(pinv);
  mode = v7_to_number(modev);
  pull = v7_to_number(pullv);

  return v7_create_boolean(sj_gpio_set_mode(pin, mode, pull) == 0);
}
Exemple #5
0
SJ_PRIVATE enum v7_err GPIO_setmode(struct v7 *v7, v7_val_t *res) {
  v7_val_t pinv = v7_arg(v7, 0);
  v7_val_t modev = v7_arg(v7, 1);
  v7_val_t pullv = v7_arg(v7, 2);
  int pin, mode, pull;

  if (!v7_is_number(pinv) || !v7_is_number(modev) || !v7_is_number(pullv)) {
    printf("Invalid arguments");
    *res = v7_mk_undefined();
  } else {
    pin = v7_to_number(pinv);
    mode = v7_to_number(modev);
    pull = v7_to_number(pullv);
    *res = v7_mk_boolean(sj_gpio_set_mode(pin, mode, pull) == 0);
  }

  return V7_OK;
}
Exemple #6
0
SJ_PRIVATE enum v7_err GPIO_setMode(struct v7 *v7, v7_val_t *res) {
  v7_val_t pinv = v7_arg(v7, 0);
  v7_val_t modev = v7_arg(v7, 1);
  v7_val_t pullv = v7_arg(v7, 2);
  int pin, mode, pull;

  if (!v7_is_number(pinv) || !v7_is_number(modev) || !v7_is_number(pullv)) {
    printf("Invalid arguments\n");
    *res = V7_UNDEFINED;
  } else {
    pin = v7_get_double(v7, pinv);
    mode = v7_get_double(v7, modev);
    pull = v7_get_double(v7, pullv);
    *res = v7_mk_boolean(v7, sj_gpio_set_mode(pin, (enum gpio_mode) mode,
                                              (enum gpio_pull_type) pull) == 0);
  }

  return V7_OK;
}
int init_device(struct v7 *v7) {
  int result = 1;
  uint8_t mac[6] = "";

  /* Load system defaults - mandatory */
  memset(&s_cfg, 0, sizeof(s_cfg));
  if (!load_config_defaults(&s_cfg)) {
    LOG(LL_ERROR, ("Failed to load config defaults"));
    return 0;
  }

#ifndef SJ_DISABLE_GPIO
  /*
   * Check factory reset GPIO. We intentionally do it before loading CONF_FILE
   * so that it cannot be overridden by the end user.
   */
  if (s_cfg.debug.factory_reset_gpio >= 0) {
    int gpio = s_cfg.debug.factory_reset_gpio;
    sj_gpio_set_mode(gpio, GPIO_MODE_INPUT, GPIO_PULL_PULLUP);
    if (sj_gpio_read(gpio) == GPIO_LEVEL_LOW) {
      LOG(LL_WARN, ("Factory reset requested via GPIO%d", gpio));
      if (remove(CONF_FILE) == 0) {
        LOG(LL_WARN, ("Removed %s", CONF_FILE));
      }
      /* Continue as if nothing happened, no reboot necessary. */
    }
  }
#endif

  /* Successfully loaded system config. Try overrides - they are optional. */
  load_config_file(CONF_FILE, s_cfg.conf_acl, 0, &s_cfg);

  REGISTER_RO_VAR(fw_id, &build_id);
  REGISTER_RO_VAR(fw_timestamp, &build_timestamp);
  REGISTER_RO_VAR(fw_version, &build_version);
  REGISTER_RO_VAR(arch, &s_architecture);

  /* Init mac address readonly var - users may use it as device ID */
  device_get_mac_address(mac);
  snprintf(s_mac_address, sizeof(s_mac_address), "%02X%02X%02X%02X%02X%02X",
           mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  REGISTER_RO_VAR(mac_address, &mac_address_ptr);
  LOG(LL_INFO, ("MAC: %s", s_mac_address));

  if (get_cfg()->wifi.ap.ssid != NULL) {
    expand_mac_address_placeholders((char *) get_cfg()->wifi.ap.ssid);
  }

  result = device_init_platform(v7, get_cfg());
  if (result != 0) {
    if (get_cfg()->http.enable) {
      result = init_web_server(get_cfg());
    }
  } else {
    LOG(LL_ERROR, ("Platform init failed"));
  }

#ifndef CS_DISABLE_JS
  /* NOTE(lsm): must be done last */
  export_read_only_vars_to_v7(v7);
#endif

  return result;
}