bool iotjs_uart_open(iotjs_uart_t* uart) {
  int fd = open(iotjs_string_data(&uart->platform_data->device_path),
                O_RDWR | O_NOCTTY | O_NDELAY);

  if (fd < 0) {
    return false;
  }

  uart->device_fd = fd;
  iotjs_uart_register_read_cb(uart);

  return true;
}
示例#2
0
//--------------CURL Callbacks------------------
// Read callback is actually to write data to outgoing request
size_t iotjs_https_curl_read_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);

  // If stream wasnt made writable yet, make it so.
  if (!_this->is_stream_writable) {
    _this->is_stream_writable = true;
    iotjs_https_jcallback(https_data, IOTJS_MAGIC_STRING_ONWRITABLE,
                          iotjs_jargs_get_empty(), false);
  }

  if (_this->data_to_read) {
    size_t real_size = size * nmemb;
    size_t chunk_size = iotjs_string_size(&(_this->read_chunk));
    size_t left_to_copy_size = chunk_size - _this->cur_read_index;

    if (real_size < 1)
      return 0;

    // send some data
    if (_this->cur_read_index < chunk_size) {
      size_t num_to_copy =
          (left_to_copy_size < real_size) ? left_to_copy_size : real_size;
      const char* buf = iotjs_string_data(&(_this->read_chunk));
      buf = &buf[_this->cur_read_index];
      strncpy((char*)contents, buf, num_to_copy);
      _this->cur_read_index = _this->cur_read_index + num_to_copy;
      return num_to_copy;
    }

    // Finished sending one chunk of data
    _this->cur_read_index = 0;
    _this->data_to_read = false;
    iotjs_https_call_read_onwrite_async(https_data);
  }

  // If the data is sent, and stream hasn't ended, wait for more data
  if (!_this->stream_ended) {
    return CURL_READFUNC_PAUSE;
  }

  // All done, end the transfer
  return 0;
}
示例#3
0
static void bridge_native_async_handler(uv_async_t* handle) {
  DDDLOG("%s\n", __func__);
  iotjs_call_jfunc_t* data = (iotjs_call_jfunc_t*)handle->data;

  bool result;
  iotjs_string_t output;

  result = bridge_native_call(IOTJS_MAGIC_STRING_TIZEN, data->fn_name,
                              data->message, &output);

  if (data->cb) {
    data->cb((int)!result, iotjs_string_data(&output));
  }

  iotjs_string_destroy(&output);

  // release
  uv_close((uv_handle_t*)&data->async, NULL);
  IOTJS_RELEASE(data->module);
  IOTJS_RELEASE(data->fn_name);
  IOTJS_RELEASE(data->message);
  IOTJS_RELEASE(data);
}
示例#4
0
// This function should be able to print utf8 encoded string
// as utf8 is internal string representation in Jerryscript
static jerry_value_t Print(const jerry_value_t* jargv,
                           const jerry_length_t jargc, FILE* out_fd) {
  JS_CHECK_ARGS(1, string);
  iotjs_string_t msg = JS_GET_ARG(0, string);
  const char* str = iotjs_string_data(&msg);
  unsigned str_len = iotjs_string_size(&msg);
  unsigned idx = 0;

  if (iotjs_console_out) {
    int level = (out_fd == stdout) ? DBGLEV_INFO : DBGLEV_ERR;
    iotjs_console_out(level, "%s", str);
  } else {
    for (idx = 0; idx < str_len; idx++) {
      if (str[idx] != 0) {
        fprintf(out_fd, "%c", str[idx]);
      } else {
        fprintf(out_fd, "\\u0000");
      }
    }
  }

  iotjs_string_destroy(&msg);
  return jerry_create_undefined();
}