Exemple #1
0
void OnRead(uv_stream_t* handle, ssize_t nread, const uv_buf_t* buf) {
  iotjs_tcpwrap_t* tcp_wrap = iotjs_tcpwrap_from_handle((uv_tcp_t*)handle);

  // tcp handle
  jerry_value_t jtcp = iotjs_tcpwrap_jobject(tcp_wrap);

  // socket object
  jerry_value_t jsocket =
      iotjs_jval_get_property(jtcp, IOTJS_MAGIC_STRING_OWNER);
  IOTJS_ASSERT(jerry_value_is_object(jsocket));

  // onread callback
  jerry_value_t jonread =
      iotjs_jval_get_property(jtcp, IOTJS_MAGIC_STRING_ONREAD);
  IOTJS_ASSERT(jerry_value_is_function(jonread));

  iotjs_jargs_t jargs = iotjs_jargs_create(4);
  iotjs_jargs_append_jval(&jargs, jsocket);
  iotjs_jargs_append_number(&jargs, nread);
  iotjs_jargs_append_bool(&jargs, false);

  if (nread <= 0) {
    if (buf->base != NULL) {
      iotjs_buffer_release(buf->base);
    }
    if (nread < 0) {
      if (nread == UV__EOF) {
        iotjs_jargs_replace(&jargs, 2, jerry_create_boolean(true));
      }

      iotjs_make_callback(jonread, jerry_create_undefined(), &jargs);
    }
  } else {
    jerry_value_t jbuffer = iotjs_bufferwrap_create_buffer((size_t)nread);
    iotjs_bufferwrap_t* buffer_wrap = iotjs_bufferwrap_from_jbuffer(jbuffer);

    iotjs_bufferwrap_copy(buffer_wrap, buf->base, (size_t)nread);

    iotjs_jargs_append_jval(&jargs, jbuffer);
    iotjs_make_callback(jonread, jerry_create_undefined(), &jargs);

    jerry_release_value(jbuffer);
    iotjs_buffer_release(buf->base);
  }

  iotjs_jargs_destroy(&jargs);
  jerry_release_value(jonread);
  jerry_release_value(jsocket);
}
Exemple #2
0
// A client socket wants to connect to this server.
// Parameters:
//   * uv_stream_t* handle - server handle
//   * int status - status code
static void OnConnection(uv_stream_t* handle, int status) {
  // Server tcp wrapper.
  iotjs_tcpwrap_t* tcp_wrap = iotjs_tcpwrap_from_handle((uv_tcp_t*)handle);

  // Tcp object
  jerry_value_t jtcp = iotjs_tcpwrap_jobject(tcp_wrap);

  // `onconnection` callback.
  jerry_value_t jonconnection =
      iotjs_jval_get_property(jtcp, IOTJS_MAGIC_STRING_ONCONNECTION);
  IOTJS_ASSERT(jerry_value_is_function(jonconnection));

  // The callback takes two parameter
  // [0] status
  // [1] client tcp object
  iotjs_jargs_t args = iotjs_jargs_create(2);
  iotjs_jargs_append_number(&args, status);

  if (status == 0) {
    // Create client socket handle wrapper.
    jerry_value_t jcreate_tcp =
        iotjs_jval_get_property(jtcp, IOTJS_MAGIC_STRING_CREATETCP);
    IOTJS_ASSERT(jerry_value_is_function(jcreate_tcp));

    jerry_value_t jclient_tcp =
        iotjs_jhelper_call_ok(jcreate_tcp, jerry_create_undefined(),
                              iotjs_jargs_get_empty());
    IOTJS_ASSERT(jerry_value_is_object(jclient_tcp));

    iotjs_tcpwrap_t* tcp_wrap_client =
        (iotjs_tcpwrap_t*)(iotjs_jval_get_object_native_handle(jclient_tcp));

    uv_stream_t* client_handle =
        (uv_stream_t*)(iotjs_tcpwrap_tcp_handle(tcp_wrap_client));

    int err = uv_accept(handle, client_handle);
    if (err) {
      iotjs_jargs_destroy(&args);
      return;
    }

    iotjs_jargs_append_jval(&args, jclient_tcp);
    jerry_release_value(jcreate_tcp);
    jerry_release_value(jclient_tcp);
  }

  iotjs_make_callback(jonconnection, jtcp, &args);

  jerry_release_value(jonconnection);
  iotjs_jargs_destroy(&args);
}
Exemple #3
0
static void iotjs_adc_after_work(uv_work_t* work_req, int status) {
  iotjs_adc_reqwrap_t* req_wrap = iotjs_adc_reqwrap_from_request(work_req);
  iotjs_adc_reqdata_t* req_data = iotjs_adc_reqwrap_data(req_wrap);
  iotjs_jargs_t jargs = iotjs_jargs_create(2);
  bool result = req_data->result;

  if (status) {
    iotjs_jval_t error = iotjs_jval_create_error("System error");
    iotjs_jargs_append_jval(&jargs, &error);
    iotjs_jval_destroy(&error);
  } else {
    switch (req_data->op) {
      case kAdcOpOpen:
        if (!result) {
          iotjs_jargs_append_error(&jargs, "Failed to open ADC device");
        } else {
          iotjs_jargs_append_null(&jargs);
        }
        break;
      case kAdcOpRead:
        if (!result) {
          iotjs_jargs_append_error(&jargs, "Cannot read from ADC device");
        } else {
          iotjs_jargs_append_null(&jargs);
          iotjs_jargs_append_number(&jargs, req_data->value);
        }
        break;
      case kAdcOpClose:
        if (!result) {
          iotjs_jargs_append_error(&jargs, "Cannot close ADC device");
        } else {
          iotjs_jargs_append_null(&jargs);
        }
        break;
      default: {
        IOTJS_ASSERT(!"Unreachable");
        break;
      }
    }
  }

  const iotjs_jval_t* jcallback = iotjs_adc_reqwrap_jcallback(req_wrap);
  iotjs_make_callback(jcallback, iotjs_jval_get_undefined(), &jargs);

  iotjs_jargs_destroy(&jargs);

  iotjs_adc_reqwrap_dispatched(req_wrap);
}
void iotjs_uncaught_exception(const iotjs_jval_t* jexception) {
  const iotjs_jval_t* process = iotjs_module_get(MODULE_PROCESS);

  iotjs_jval_t jonuncaughtexception =
      iotjs_jval_get_property(process, IOTJS_MAGIC_STRING__ONUNCAUGHTEXCEPTION);
  IOTJS_ASSERT(iotjs_jval_is_function(&jonuncaughtexception));

  iotjs_jargs_t args = iotjs_jargs_create(1);
  iotjs_jargs_append_jval(&args, jexception);

  bool throws;
  iotjs_jval_t jres =
      iotjs_jhelper_call(&jonuncaughtexception, process, &args, &throws);

  iotjs_jargs_destroy(&args);
  iotjs_jval_destroy(&jres);
  iotjs_jval_destroy(&jonuncaughtexception);

  if (throws) {
    exit(2);
  }
}
// Write Callback is actually to read data from incoming response
size_t iotjs_https_curl_write_callback(void* contents, size_t size,
                                       size_t nmemb, void* userp) {
  iotjs_https_t* https_data = (iotjs_https_t*)userp;
  IOTJS_VALIDATED_STRUCT_METHOD(iotjs_https_t, https_data);
  size_t real_size = size * nmemb;
  if (iotjs_jval_is_null(&_this->jthis_native))
    return real_size - 1;
  iotjs_jargs_t jarg = iotjs_jargs_create(1);
  iotjs_jval_t jresult_arr = iotjs_jval_create_byte_array(real_size, contents);
  iotjs_jargs_append_jval(&jarg, &jresult_arr);

  bool result =
      iotjs_https_jcallback(https_data, IOTJS_MAGIC_STRING_ONDATA, &jarg, true);

  iotjs_jval_destroy(&jresult_arr);
  iotjs_jargs_destroy(&jarg);

  if (!result) {
    return real_size - 1;
  }

  return real_size;
}
static void iotjs_spi_after_work(uv_work_t* work_req, int status) {
  iotjs_spi_reqwrap_t* req_wrap = iotjs_spi_reqwrap_from_request(work_req);
  iotjs_spi_reqdata_t* req_data = iotjs_spi_reqwrap_data(req_wrap);
  iotjs_spi_t* spi = iotjs_spi_instance_from_reqwrap(req_wrap);

  iotjs_jargs_t jargs = iotjs_jargs_create(2);

  bool result = req_data->result;

  if (status) {
    iotjs_jargs_append_error(&jargs, "System error");
  } else {
    switch (req_data->op) {
      case kSpiOpOpen:
        if (!result) {
          iotjs_jargs_append_error(&jargs, "Failed to export SPI device");
        } else {
          iotjs_jargs_append_null(&jargs);
        }
        break;
      case kSpiOpTransferArray:
      case kSpiOpTransferBuffer:
        if (!result) {
          iotjs_jargs_append_error(&jargs, "Cannot transfer from SPI device");
        } else {
          iotjs_jargs_append_null(&jargs);

          IOTJS_VALIDATED_STRUCT_METHOD(iotjs_spi_t, spi);

          // Append read data
          iotjs_jval_t result_data =
              iotjs_jval_create_byte_array(_this->buf_len, _this->rx_buf_data);
          iotjs_jargs_append_jval(&jargs, &result_data);
          iotjs_jval_destroy(&result_data);
        }

        if (req_data->op == kSpiOpTransferArray)
          iotjs_spi_release_buffer(spi);

        break;
      case kSpiOpClose:
        if (!result) {
          iotjs_jargs_append_error(&jargs, "Failed to unexport SPI device");
        } else {
          iotjs_jargs_append_null(&jargs);
        }
        break;
      default: {
        IOTJS_ASSERT(!"Unreachable");
        break;
      }
    }
  }

  const iotjs_jval_t* jcallback = iotjs_spi_reqwrap_jcallback(req_wrap);
  iotjs_make_callback(jcallback, iotjs_jval_get_undefined(), &jargs);

  iotjs_jargs_destroy(&jargs);

  iotjs_spi_reqwrap_dispatched(req_wrap);
}
static void iotjs_pwm_after_worker(uv_work_t* work_req, int status) {
  iotjs_pwm_reqwrap_t* req_wrap = iotjs_pwm_reqwrap_from_request(work_req);
  iotjs_pwm_reqdata_t* req_data = iotjs_pwm_reqwrap_data(req_wrap);
  iotjs_jargs_t jargs = iotjs_jargs_create(1);
  bool result = req_data->result;

  if (status) {
    iotjs_jval_t error = iotjs_jval_create_error("System error");
    iotjs_jargs_append_jval(&jargs, &error);
    iotjs_jval_destroy(&error);
  } else {
    switch (req_data->op) {
      case kPwmOpOpen:
        if (!result) {
          iotjs_jargs_append_error(&jargs, "Failed to open PWM device");
        } else {
          iotjs_jargs_append_null(&jargs);
        }
        break;
      case kPwmOpSetDutyCycle:
        if (!result) {
          iotjs_jargs_append_error(&jargs, "Failed to set duty-cycle");
        } else {
          iotjs_jargs_append_null(&jargs);
        }
        break;
      case kPwmOpSetPeriod:
        if (!result) {
          iotjs_jargs_append_error(&jargs, "Failed to set period");
        } else {
          iotjs_jargs_append_null(&jargs);
        }
        break;
      case kPwmOpSetEnable:
        if (!result) {
          iotjs_jargs_append_error(&jargs, "Failed to set enable");
        } else {
          iotjs_jargs_append_null(&jargs);
        }
        break;
      case kPwmOpClose:
        if (!result) {
          iotjs_jargs_append_error(&jargs, "Cannot close PWM device");
        } else {
          iotjs_jargs_append_null(&jargs);
        }
        break;
      default: {
        IOTJS_ASSERT(!"Unreachable");
        break;
      }
    }
  }

  const iotjs_jval_t* jcallback = iotjs_pwm_reqwrap_jcallback(req_wrap);
  iotjs_make_callback(jcallback, iotjs_jval_get_undefined(), &jargs);

  iotjs_jargs_destroy(&jargs);

  iotjs_pwm_reqwrap_dispatched(req_wrap);
}
Exemple #8
0
void iotjs_jargs_append_string_raw(iotjs_jargs_t* jargs, const char* x) {
  IOTJS_VALIDATED_STRUCT_METHOD(iotjs_jargs_t, jargs);
  iotjs_jval_t jval = iotjs_jval_create_string_raw(x);
  iotjs_jargs_append_jval(jargs, &jval);
  iotjs_jval_destroy(&jval);
}
Exemple #9
0
void iotjs_jargs_append_number(iotjs_jargs_t* jargs, double x) {
  IOTJS_VALIDATED_STRUCT_METHOD(iotjs_jargs_t, jargs);
  iotjs_jval_t jval = iotjs_jval_create_number(x);
  iotjs_jargs_append_jval(jargs, &jval);
  iotjs_jval_destroy(&jval);
}
Exemple #10
0
void iotjs_jargs_append_bool(iotjs_jargs_t* jargs, bool x) {
  IOTJS_VALIDATED_STRUCT_METHOD(iotjs_jargs_t, jargs);
  iotjs_jargs_append_jval(jargs, iotjs_jval_get_boolean(x));
}
Exemple #11
0
void iotjs_jargs_append_null(iotjs_jargs_t* jargs) {
  IOTJS_VALIDATED_STRUCT_METHOD(iotjs_jargs_t, jargs);
  iotjs_jargs_append_jval(jargs, iotjs_jval_get_null());
}
Exemple #12
0
void iotjs_jargs_append_undefined(iotjs_jargs_t* jargs) {
  IOTJS_VALIDATED_STRUCT_METHOD(iotjs_jargs_t, jargs);
  iotjs_jargs_append_jval(jargs, iotjs_jval_get_undefined());
}
Exemple #13
0
static void AfterAsync(uv_fs_t* req) {
  iotjs_fs_reqwrap_t* req_wrap = (iotjs_fs_reqwrap_t*)(req->data);
  IOTJS_ASSERT(req_wrap != NULL);
  IOTJS_ASSERT(&req_wrap->req == req);

  const jerry_value_t cb = iotjs_reqwrap_jcallback(&req_wrap->reqwrap);
  IOTJS_ASSERT(jerry_value_is_function(cb));

  iotjs_jargs_t jarg = iotjs_jargs_create(2);
  if (req->result < 0) {
    jerry_value_t jerror = iotjs_create_uv_exception(req->result, "open");
    iotjs_jargs_append_jval(&jarg, jerror);
    jerry_release_value(jerror);
  } else {
    iotjs_jargs_append_null(&jarg);
    switch (req->fs_type) {
      case UV_FS_CLOSE: {
        break;
      }
      case UV_FS_OPEN:
      case UV_FS_READ:
      case UV_FS_WRITE: {
        iotjs_jargs_append_number(&jarg, (double)req->result);
        break;
      }
      case UV_FS_SCANDIR: {
        int r;
        uv_dirent_t ent;
        uint32_t idx = 0;
        jerry_value_t ret = jerry_create_array(0);
        while ((r = uv_fs_scandir_next(req, &ent)) != UV_EOF) {
          jerry_value_t name =
              jerry_create_string((const jerry_char_t*)ent.name);
          iotjs_jval_set_property_by_index(ret, idx, name);
          jerry_release_value(name);
          idx++;
        }
        iotjs_jargs_append_jval(&jarg, ret);
        jerry_release_value(ret);
        break;
      }
      case UV_FS_FSTAT:
      case UV_FS_STAT: {
        uv_stat_t s = (req->statbuf);
        jerry_value_t ret = MakeStatObject(&s);
        iotjs_jargs_append_jval(&jarg, ret);
        jerry_release_value(ret);
        break;
      }
      default: {
        iotjs_jargs_append_null(&jarg);
        break;
      }
    }
  }

  iotjs_make_callback(cb, jerry_create_undefined(), &jarg);

  iotjs_jargs_destroy(&jarg);
  iotjs_fs_reqwrap_destroy(req_wrap);
}