static void iotjs_spi_set_buffer(iotjs_spi_t* spi, const iotjs_jval_t* jtx_buf, const iotjs_jval_t* jrx_buf) { IOTJS_VALIDATED_STRUCT_METHOD(iotjs_spi_t, spi); iotjs_bufferwrap_t* tx_buf = iotjs_bufferwrap_from_jbuffer(jtx_buf); iotjs_bufferwrap_t* rx_buf = iotjs_bufferwrap_from_jbuffer(jrx_buf); _this->tx_buf_data = iotjs_bufferwrap_buffer(tx_buf); uint8_t tx_buf_len = iotjs_bufferwrap_length(tx_buf); _this->rx_buf_data = iotjs_bufferwrap_buffer(rx_buf); uint8_t rx_buf_len = iotjs_bufferwrap_length(rx_buf); IOTJS_ASSERT(_this->tx_buf_data != NULL && _this->rx_buf_data != NULL); IOTJS_ASSERT(tx_buf_len > 0 && rx_buf_len > 0 && tx_buf_len == rx_buf_len); _this->buf_len = tx_buf_len; }
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; }
static uint8_t spi_transfer_helper(jerry_value_t jtx_buf, iotjs_spi_t* spi) { uint8_t result = kSpiOpTransferBuffer; if (jerry_value_is_array(jtx_buf)) { spi->buf_len = spi_get_array_data(&spi->tx_buf_data, jtx_buf); result = kSpiOpTransferArray; } else if (jerry_value_is_object(jtx_buf)) { iotjs_bufferwrap_t* tx_buf = iotjs_bufferwrap_from_jbuffer(jtx_buf); spi->tx_buf_data = tx_buf->buffer; spi->buf_len = iotjs_bufferwrap_length(tx_buf); } IOTJS_ASSERT(spi->buf_len > 0); spi->rx_buf_data = iotjs_buffer_allocate(spi->buf_len); IOTJS_ASSERT(spi->tx_buf_data != NULL && spi->rx_buf_data != NULL); return result; }