jerry_value_t InitProcess() {
  jerry_value_t process = jerry_create_object();

  iotjs_jval_set_method(process, IOTJS_MAGIC_STRING_COMPILE, Compile);
  iotjs_jval_set_method(process, IOTJS_MAGIC_STRING_COMPILEMODULE,
                        CompileModule);
  iotjs_jval_set_method(process, IOTJS_MAGIC_STRING_READSOURCE, ReadSource);
  iotjs_jval_set_method(process, IOTJS_MAGIC_STRING_CWD, Cwd);
  iotjs_jval_set_method(process, IOTJS_MAGIC_STRING_CHDIR, Chdir);
  iotjs_jval_set_method(process, IOTJS_MAGIC_STRING_DEBUGGERGETSOURCE,
                        DebuggerGetSource);
  iotjs_jval_set_method(process, IOTJS_MAGIC_STRING_DOEXIT, DoExit);
  SetProcessEnv(process);

  // process.builtin_modules
  jerry_value_t builtin_modules = jerry_create_object();
  SetBuiltinModules(builtin_modules);
  iotjs_jval_set_property_jval(process, IOTJS_MAGIC_STRING_BUILTIN_MODULES,
                               builtin_modules);
  jerry_release_value(builtin_modules);

  // process.platform
  iotjs_jval_set_property_string_raw(process, IOTJS_MAGIC_STRING_PLATFORM,
                                     TARGET_OS);

  // process.arch
  iotjs_jval_set_property_string_raw(process, IOTJS_MAGIC_STRING_ARCH,
                                     TARGET_ARCH);

  // process.version
  iotjs_jval_set_property_string_raw(process, IOTJS_MAGIC_STRING_VERSION,
                                     IOTJS_VERSION);

  // Set iotjs
  SetProcessIotjs(process);
  bool wait_source;
  if (iotjs_environment_config(iotjs_environment_get())->debugger != NULL) {
    wait_source = iotjs_environment_config(iotjs_environment_get())
                      ->debugger->wait_source;
  } else {
    wait_source = false;
  }

  if (!wait_source) {
    SetProcessArgv(process);
  }

  jerry_value_t wait_source_val = jerry_create_boolean(wait_source);
  iotjs_jval_set_property_jval(process, IOTJS_MAGIC_STRING_DEBUGGERWAITSOURCE,
                               wait_source_val);
  jerry_release_value(wait_source_val);

  return process;
}
int iotjs_tizen_bridge_native(const char* fn_name, unsigned fn_name_size,
                              const char* message, unsigned message_size,
                              user_callback_t cb) {
  iotjs_environment_t* env = iotjs_environment_get();

  if (env->state != kRunningMain && env->state != kRunningLoop) {
    return IOTJS_ERROR_RESULT_FAILED;
  }

  iotjs_call_jfunc_t* handle = IOTJS_ALLOC(iotjs_call_jfunc_t);

  if (handle == NULL) {
    return IOTJS_ERROR_OUT_OF_MEMORY;
  }

  handle->async.data = (void*)handle;
  handle->fn_name = create_string_buffer(fn_name, fn_name_size);
  handle->message = create_string_buffer(message, message_size);
  handle->module = create_string_buffer(IOTJS_MAGIC_STRING_TIZEN,
                                        sizeof(IOTJS_MAGIC_STRING_TIZEN));
  handle->cb = cb;

  uv_loop_t* loop = iotjs_environment_loop(env);
  uv_async_init(loop, &handle->async, bridge_native_async_handler);
  uv_async_send(&handle->async);

  return IOTJS_ERROR_NONE;
}
static bool adc_read_data(uint32_t pin, struct adc_msg_s* msg) {
  int32_t adc_number = ADC_GET_NUMBER(pin);
  char path[ADC_DEVICE_PATH_BUFFER_SIZE] = { 0 };
  adc_get_path(path, adc_number);

  const iotjs_environment_t* env = iotjs_environment_get();
  uv_loop_t* loop = iotjs_environment_loop(env);
  int result, close_result;

  // Open file
  uv_fs_t open_req;
  result = uv_fs_open(loop, &open_req, path, O_RDONLY, 0666, NULL);
  uv_fs_req_cleanup(&open_req);
  if (result < 0) {
    return false;
  }

  // Read value
  uv_fs_t read_req;
  uv_buf_t uvbuf = uv_buf_init((char*)msg, sizeof(*msg));
  result = uv_fs_read(loop, &read_req, open_req.result, &uvbuf, 1, 0, NULL);
  uv_fs_req_cleanup(&read_req);

  // Close file
  uv_fs_t close_req;
  close_result = uv_fs_close(loop, &close_req, open_req.result, NULL);
  uv_fs_req_cleanup(&close_req);
  if (result < 0 || close_result < 0) {
    return false;
  }

  DDDLOG("ADC Read - path: %s, value: %d", path, msg->am_data);

  return true;
}
void iotjs_uart_open_worker(uv_work_t* work_req) {
  UART_WORKER_INIT;
  IOTJS_VALIDATED_STRUCT_METHOD(iotjs_uart_t, uart);

  int fd = open(iotjs_string_data(&_this->device_path),
                O_RDWR | O_NOCTTY | O_NDELAY);
  if (fd < 0) {
    req_data->result = false;
    return;
  }

  struct termios options;
  tcgetattr(fd, &options);
  options.c_cflag = CLOCAL | CREAD;
  options.c_cflag |= baud_to_constant(_this->baud_rate);
  options.c_cflag |= databits_to_constant(_this->data_bits);
  options.c_iflag = IGNPAR;
  options.c_oflag = 0;
  options.c_lflag = 0;
  tcflush(fd, TCIFLUSH);
  tcsetattr(fd, TCSANOW, &options);

  _this->device_fd = fd;
  uv_poll_t* poll_handle = &_this->poll_handle;

  uv_loop_t* loop = iotjs_environment_loop(iotjs_environment_get());
  uv_poll_init(loop, poll_handle, fd);
  poll_handle->data = uart;
  uv_poll_start(poll_handle, UV_READABLE, iotjs_uart_read_cb);

  req_data->result = true;
}
Exemple #5
0
iotjs_tcpwrap_t* iotjs_tcpwrap_create(jerry_value_t jtcp) {
  iotjs_tcpwrap_t* tcpwrap = IOTJS_ALLOC(iotjs_tcpwrap_t);
  IOTJS_VALIDATED_STRUCT_CONSTRUCTOR(iotjs_tcpwrap_t, tcpwrap);

  iotjs_handlewrap_initialize(&_this->handlewrap, jtcp,
                              (uv_handle_t*)(&_this->handle),
                              &this_module_native_info);

  const iotjs_environment_t* env = iotjs_environment_get();
  uv_tcp_init(iotjs_environment_loop(env), &_this->handle);

  return tcpwrap;
}
void iotjs_tizen_app_control_cb(app_control_h app_control, void* user_data) {
  DDDLOG("%s", __func__);

  iotjs_environment_t* env = iotjs_environment_get();

  if (env->state != kRunningMain && env->state != kRunningLoop) {
    return;
  }

  const char* event_emitter_name = IOTJS_MAGIC_STRING_TIZEN;
  const char* event_name = IOTJS_MAGIC_STRING_APP_CONTROL;

  jerry_value_t tizen = iotjs_module_get(event_emitter_name);
  jerry_value_t fn = iotjs_jval_get_property(tizen, IOTJS_MAGIC_STRING_EMIT);

  if (jerry_value_is_function(fn) == false) {
    DDDLOG("tizen module is not loaded");
    goto exit;
  }

  // parse app control
  char* json = NULL;
  bundle* b = NULL;

  app_control_export_as_bundle(app_control, &b);

  if (BUNDLE_ERROR_NONE != bundle_to_json(b, &json)) {
    DDLOG("bundle_to_json failed");
    bundle_free(b);
    return;
  }
  DDDLOG("JSON: %s", json);

  // call emit
  jerry_value_t jargv[2] = { jerry_create_string(
                                 (const jerry_char_t*)event_name),
                             jerry_create_string((const jerry_char_t*)json) };

  iotjs_invoke_callback(fn, tizen, jargv, 2);
  jerry_release_value(jargv[0]);
  jerry_release_value(jargv[1]);

  free(json);
  bundle_free(b);

exit:
  jerry_release_value(fn);
}
static void SetProcessArgv(jerry_value_t process) {
  const iotjs_environment_t* env = iotjs_environment_get();
  uint32_t argc = iotjs_environment_argc(env);

  jerry_value_t argv = jerry_create_array(argc);

  for (uint32_t i = 0; i < argc; ++i) {
    const char* argvi = iotjs_environment_argv(env, i);
    jerry_value_t arg = jerry_create_string((const jerry_char_t*)argvi);
    iotjs_jval_set_property_by_index(argv, i, arg);
    jerry_release_value(arg);
  }
  iotjs_jval_set_property_jval(process, IOTJS_MAGIC_STRING_ARGV, argv);

  jerry_release_value(argv);
}
Exemple #8
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;
}
//-------------Constructor------------
iotjs_https_t* iotjs_https_create(const char* URL, const char* method,
                                  const char* ca, const char* cert,
                                  const char* key,
                                  const bool reject_unauthorized,
                                  const iotjs_jval_t* jthis) {
  iotjs_https_t* https_data = IOTJS_ALLOC(iotjs_https_t);
  IOTJS_VALIDATED_STRUCT_CONSTRUCTOR(iotjs_https_t, https_data);

  // Original Request Details
  _this->URL = URL;
  _this->header_list = NULL;
  if (strcmp(method, STRING_GET) == 0)
    _this->method = HTTPS_GET;
  else if (strcmp(method, STRING_POST) == 0)
    _this->method = HTTPS_POST;
  else if (strcmp(method, STRING_PUT) == 0)
    _this->method = HTTPS_PUT;
  else if (strcmp(method, STRING_DELETE) == 0)
    _this->method = HTTPS_DELETE;
  else if (strcmp(method, STRING_HEAD) == 0)
    _this->method = HTTPS_HEAD;
  else if (strcmp(method, STRING_CONNECT) == 0)
    _this->method = HTTPS_CONNECT;
  else if (strcmp(method, STRING_OPTIONS) == 0)
    _this->method = HTTPS_OPTIONS;
  else if (strcmp(method, STRING_TRACE) == 0)
    _this->method = HTTPS_TRACE;
  else {
    IOTJS_ASSERT(0);
  }

  // TLS certs stuff
  _this->ca = ca;
  _this->cert = cert;
  _this->key = key;
  _this->reject_unauthorized = reject_unauthorized;
  // Content Length stuff
  _this->content_length = -1;

  // Handles
  _this->loop = iotjs_environment_loop(iotjs_environment_get());
  _this->jthis_native = iotjs_jval_create_copied(jthis);
  iotjs_jval_set_object_native_handle(&(_this->jthis_native),
                                      (uintptr_t)https_data,
                                      &https_native_info);
  _this->curl_multi_handle = curl_multi_init();
  _this->curl_easy_handle = curl_easy_init();
  _this->timeout.data = (void*)https_data;
  uv_timer_init(_this->loop, &(_this->timeout));
  _this->request_done = false;
  _this->closing_handles = 3;
  _this->poll_data = NULL;

  // Timeout stuff
  _this->timeout_ms = -1;
  _this->last_bytes_num = -1;
  _this->last_bytes_time = 0;
  _this->socket_timeout.data = (void*)https_data;
  uv_timer_init(_this->loop, &(_this->socket_timeout));

  // ReadData stuff
  _this->cur_read_index = 0;
  _this->is_stream_writable = false;
  _this->stream_ended = false;
  _this->data_to_read = false;
  _this->to_destroy_read_onwrite = false;
  _this->async_read_onwrite.data = (void*)https_data;
  uv_timer_init(_this->loop, &(_this->async_read_onwrite));
  // No Need to read data for following types of requests
  if (_this->method == HTTPS_GET || _this->method == HTTPS_DELETE ||
      _this->method == HTTPS_HEAD || _this->method == HTTPS_OPTIONS ||
      _this->method == HTTPS_TRACE)
    _this->stream_ended = true;

  return https_data;
}