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 #4
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;
}
//-------------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;
}