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); }
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); }
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); }