Ejemplo n.º 1
0
jerry_value_t fs_do_read_or_write(const jerry_value_t jfunc,
                                  const jerry_value_t jthis,
                                  const jerry_value_t jargv[],
                                  const jerry_length_t jargc,
                                  const iotjs_fs_op_t fs_op) {
  DJS_CHECK_THIS();
  DJS_CHECK_ARGS(5, number, object, number, number, number);
  DJS_CHECK_ARG_IF_EXIST(5, function);

  const iotjs_environment_t* env = iotjs_environment_get();

  int fd = JS_GET_ARG(0, number);
  const jerry_value_t jbuffer = JS_GET_ARG(1, object);
  size_t offset = JS_GET_ARG(2, number);
  size_t length = JS_GET_ARG(3, number);
  int position = JS_GET_ARG(4, number);
  const jerry_value_t jcallback = JS_GET_ARG_IF_EXIST(5, function);

  iotjs_bufferwrap_t* buffer_wrap = iotjs_bufferwrap_from_jbuffer(jbuffer);
  char* data = buffer_wrap->buffer;
  size_t data_length = iotjs_bufferwrap_length(buffer_wrap);
  JS_CHECK(data != NULL && data_length > 0);

  if (!IsWithinBounds(offset, length, data_length)) {
    return JS_CREATE_ERROR(RANGE, "length out of bound");
  }

  uv_buf_t uvbuf = uv_buf_init(data + offset, length);

  jerry_value_t ret_value;
  if (fs_op == IOTJS_FS_READ) {
    if (!jerry_value_is_null(jcallback)) {
      FS_ASYNC(env, read, jcallback, fd, &uvbuf, 1, position);
    } else {
      FS_SYNC(env, read, fd, &uvbuf, 1, position);
    }
  } else {
    if (!jerry_value_is_null(jcallback)) {
      FS_ASYNC(env, write, jcallback, fd, &uvbuf, 1, position);
    } else {
      FS_SYNC(env, write, fd, &uvbuf, 1, position);
    }
  }
  return ret_value;
}
Ejemplo n.º 2
0
static jerry_value_t gpio_set_configuration(iotjs_gpio_t* gpio,
                                            jerry_value_t jconfigurable) {
  jerry_value_t jpin =
      iotjs_jval_get_property(jconfigurable, IOTJS_MAGIC_STRING_PIN);
  gpio->pin = iotjs_jval_as_number(jpin);
  jerry_release_value(jpin);

  // Direction
  jerry_value_t jdirection =
      iotjs_jval_get_property(jconfigurable, IOTJS_MAGIC_STRING_DIRECTION);

  if (jerry_value_is_undefined(jdirection)) {
    gpio->direction = kGpioDirectionOut;
  } else {
    if (jerry_value_is_number(jdirection)) {
      gpio->direction = (GpioDirection)iotjs_jval_as_number(jdirection);
    } else {
      gpio->direction = __kGpioDirectionMax;
    }
    if (gpio->direction >= __kGpioDirectionMax) {
      return JS_CREATE_ERROR(
          TYPE, "Bad arguments - gpio.direction should be DIRECTION.IN or OUT");
    }
  }
  jerry_release_value(jdirection);

  // Mode
  jerry_value_t jmode =
      iotjs_jval_get_property(jconfigurable, IOTJS_MAGIC_STRING_MODE);

  if (jerry_value_is_undefined(jmode)) {
    gpio->mode = kGpioModeNone;
  } else {
    if (jerry_value_is_number(jmode)) {
      gpio->mode = (GpioMode)iotjs_jval_as_number(jmode);
    } else {
      gpio->mode = __kGpioModeMax;
    }
    if (gpio->mode >= __kGpioModeMax) {
      return JS_CREATE_ERROR(TYPE,
                             "Bad arguments - gpio.mode should be MODE.NONE, "
                             "PULLUP, PULLDOWN, FLOAT, PUSHPULL or OPENDRAIN");

    } else if (gpio->direction == kGpioDirectionIn &&
               gpio->mode != kGpioModeNone && gpio->mode != kGpioModePullup &&
               gpio->mode != kGpioModePulldown) {
      return JS_CREATE_ERROR(TYPE,
                             "Bad arguments - DIRECTION.IN only supports "
                             "MODE.NONE, PULLUP and PULLDOWN");

    } else if (gpio->direction == kGpioDirectionOut &&
               gpio->mode != kGpioModeNone && gpio->mode != kGpioModeFloat &&
               gpio->mode != kGpioModePushpull &&
               gpio->mode != kGpioModeOpendrain) {
      return JS_CREATE_ERROR(TYPE,
                             "Bad arguments - DIRECTION.OUT only supports "
                             "MODE.NONE, FLOAT, PUSHPULL and OPENDRAIN");
    }
  }
  jerry_release_value(jmode);

  // Edge
  jerry_value_t jedge =
      iotjs_jval_get_property(jconfigurable, IOTJS_MAGIC_STRING_EDGE);

  if (jerry_value_is_undefined(jedge)) {
    gpio->edge = kGpioEdgeNone;
  } else {
    if (jerry_value_is_number(jedge)) {
      gpio->edge = (GpioEdge)iotjs_jval_as_number(jedge);
    } else {
      gpio->edge = __kGpioEdgeMax;
    }
    if (gpio->edge >= __kGpioEdgeMax) {
      return JS_CREATE_ERROR(TYPE,
                             "Bad arguments - gpio.edge should be EDGE.NONE, "
                             "RISING, FALLING or BOTH");
    }
  }
  jerry_release_value(jedge);

  return jerry_create_undefined();
}
Ejemplo n.º 3
0
/* Default configuration:
 *{
 *  mode: spi.MODE[0],
 *  chipSelect: spi.CHIPSELECT.NONE,
 *  maxSpeed: 500000,
 *  bitsPerWord: 8,
 *  bitOrder: spi.BITORDER.MSB,
 *  loopback: false
 * }
 */
static jerry_value_t spi_set_configuration(iotjs_spi_t* spi,
                                           jerry_value_t joptions) {
  jerry_value_t jmode =
      iotjs_jval_get_property(joptions, IOTJS_MAGIC_STRING_MODE);
  if (jerry_value_is_undefined(jmode)) {
    spi->mode = kSpiMode_0;
  } else {
    if (jerry_value_is_number(jmode)) {
      spi->mode = (SpiMode)iotjs_jval_as_number(jmode);
    } else {
      spi->mode = __kSpiModeMax;
    }

    if (spi->mode >= __kSpiModeMax) {
      return JS_CREATE_ERROR(
          TYPE, "Bad arguments - mode should be MODE[0], [1], [2] or [3]");
    }
  }
  jerry_release_value(jmode);

  jerry_value_t jchip_select =
      iotjs_jval_get_property(joptions, IOTJS_MAGIC_STRING_CHIPSELECT);
  if (jerry_value_is_undefined(jchip_select)) {
    spi->chip_select = kSpiCsNone;
  } else {
    if (jerry_value_is_number(jchip_select)) {
      spi->chip_select = (SpiChipSelect)iotjs_jval_as_number(jchip_select);
    } else {
      spi->chip_select = __kSpiCsMax;
    }

    if (spi->chip_select >= __kSpiCsMax) {
      return JS_CREATE_ERROR(
          TYPE, "Bad arguments - chipSelect should be CHIPSELECT.NONE or HIGH");
    }
  }
  jerry_release_value(jchip_select);

  jerry_value_t jmax_speed =
      iotjs_jval_get_property(joptions, IOTJS_MAGIC_STRING_MAXSPEED);
  if (jerry_value_is_undefined(jmax_speed)) {
    spi->max_speed = 500000;
  } else {
    if (!jerry_value_is_number(jmax_speed)) {
      return JS_CREATE_ERROR(TYPE, "Bad arguments - maxSpeed should be Number");
    }
    spi->max_speed = iotjs_jval_as_number(jmax_speed);
  }
  jerry_release_value(jmax_speed);

  jerry_value_t jbits_per_word =
      iotjs_jval_get_property(joptions, IOTJS_MAGIC_STRING_BITSPERWORD);
  if (jerry_value_is_undefined(jbits_per_word)) {
    spi->bits_per_word = 8;
  } else {
    if (jerry_value_is_number(jbits_per_word)) {
      spi->bits_per_word = iotjs_jval_as_number(jbits_per_word);
    } else {
      spi->bits_per_word = 0;
    }

    if (spi->bits_per_word != 8 && spi->bits_per_word != 9) {
      return JS_CREATE_ERROR(TYPE,
                             "Bad arguments - bitsPerWord should be 8 or 9");
    }
  }
  jerry_release_value(jbits_per_word);

  jerry_value_t jbit_order =
      iotjs_jval_get_property(joptions, IOTJS_MAGIC_STRING_BITORDER);
  if (jerry_value_is_undefined(jbit_order)) {
    spi->bit_order = kSpiOrderMsb;
  } else {
    if (jerry_value_is_number(jbit_order)) {
      spi->bit_order = (SpiOrder)iotjs_jval_as_number(jbit_order);
    } else {
      spi->bit_order = __kSpiOrderMax;
    }

    if (spi->bit_order >= __kSpiOrderMax) {
      return JS_CREATE_ERROR(
          TYPE, "Bad arguments - bitOrder should be BITORDER.MSB or LSB");
    }
  }
  jerry_release_value(jbit_order);

  jerry_value_t jloopback =
      iotjs_jval_get_property(joptions, IOTJS_MAGIC_STRING_LOOPBACK);
  if (jerry_value_is_undefined(jloopback)) {
    spi->loopback = false;
  } else {
    if (!jerry_value_is_boolean(jloopback)) {
      return JS_CREATE_ERROR(TYPE,
                             "Bad arguments - loopback should be Boolean");
    }
    spi->loopback = iotjs_jval_as_boolean(jloopback);
  }
  jerry_release_value(jloopback);

  return jerry_create_undefined();
}