bool iotjs_uart_write(iotjs_uart_t* uart) { IOTJS_VALIDATED_STRUCT_METHOD(iotjs_uart_t, uart); int bytesWritten = 0; unsigned offset = 0; int fd = _this->device_fd; const char* buf_data = iotjs_string_data(&_this->buf_data); DDDLOG("%s - data: %s", __func__, buf_data); do { errno = 0; bytesWritten = write(fd, buf_data + offset, _this->buf_len - offset); tcdrain(fd); DDDLOG("%s - size: %d", __func__, _this->buf_len - offset); if (bytesWritten != -1) { offset += bytesWritten; continue; } if (errno == EINTR) { continue; } return false; } while (_this->buf_len > offset); return true; }
bool iotjs_uart_write(iotjs_uart_t* uart) { int bytesWritten = 0; unsigned offset = 0; int fd = uart->device_fd; const char* buf_data = iotjs_string_data(&uart->buf_data); DDDLOG("%s - data: %s", __func__, buf_data); do { errno = 0; bytesWritten = write(fd, buf_data + offset, uart->buf_len - offset); DDDLOG("%s - size: %d", __func__, uart->buf_len - offset); if (bytesWritten != -1) { offset += (unsigned)bytesWritten; continue; } if (errno == EINTR) { continue; } return false; } while (uart->buf_len > offset); return true; }
static iotjs_error_t tizen_send_launch_request(const char* json, void* hbridge) { DDDLOG("%s", __func__); bundle* b; int ret; ret = bundle_from_json(json, &b); if (ret != BUNDLE_ERROR_NONE) { DDLOG("bundle_from_json failed"); return IOTJS_ERROR_INVALID_PARAMETER; } app_control_h app_control = NULL; app_control_create(&app_control); app_control_import_from_bundle(app_control, b); ret = app_control_send_launch_request(app_control, NULL, NULL); if (ret != APP_CONTROL_ERROR_NONE) { DDDLOG("app_control_send_launch_request failed"); switch (ret) { case APP_CONTROL_ERROR_INVALID_PARAMETER: iotjs_bridge_set_err(hbridge, "APP_CONTROL_ERROR_INVALID_PARAMETER"); break; case APP_CONTROL_ERROR_OUT_OF_MEMORY: iotjs_bridge_set_err(hbridge, "APP_CONTROL_ERROR_OUT_OF_MEMORY"); break; case APP_CONTROL_ERROR_APP_NOT_FOUND: iotjs_bridge_set_err(hbridge, "APP_CONTROL_ERROR_APP_NOT_FOUND"); break; case APP_CONTROL_ERROR_LAUNCH_REJECTED: iotjs_bridge_set_err(hbridge, "APP_CONTROL_ERROR_LAUNCH_REJECTED"); break; case APP_CONTROL_ERROR_LAUNCH_FAILED: iotjs_bridge_set_err(hbridge, "APP_CONTROL_ERROR_LAUNCH_FAILED"); break; case APP_CONTROL_ERROR_TIMED_OUT: iotjs_bridge_set_err(hbridge, "APP_CONTROL_ERROR_TIMED_OUT"); break; case APP_CONTROL_ERROR_PERMISSION_DENIED: iotjs_bridge_set_err(hbridge, "APP_CONTROL_ERROR_PERMISSION_DENIED"); break; default: iotjs_bridge_set_err(hbridge, "APP_CONTROL_ERROR_UNKNOWN"); break; } return IOTJS_ERROR_RESULT_FAILED; } bundle_free(b); app_control_destroy(app_control); return IOTJS_ERROR_NONE; }
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); }
// # tizen bridge void iotjs_tizen_func(const char* command, const char* message, void* handle) { DDDLOG("%s, cmd: %s, msg: %s", __func__, command, message); if (strncmp(command, "getResPath", strlen("getResPath")) == 0) { char* app_res_path = app_get_resource_path(); iotjs_bridge_set_msg(handle, app_res_path); if (app_res_path != NULL) { free(app_res_path); } } else if (strncmp(command, "getDataPath", strlen("getDataPath")) == 0) { char* app_data_path = app_get_data_path(); iotjs_bridge_set_msg(handle, app_data_path); if (app_data_path != NULL) { free(app_data_path); } } else if (strncmp(command, "launchAppControl", strlen("launchAppControl")) == 0) { iotjs_error_t err = tizen_send_launch_request(message, handle); if (err == IOTJS_ERROR_NONE) { iotjs_bridge_set_msg(handle, "OK"); } } else { iotjs_bridge_set_err(handle, "Can't find command"); } }
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; }
bool iotjs_pwm_open(iotjs_pwm_t* pwm) { int timer = SYSIO_GET_TIMER(pwm->pin); char path[PWM_DEVICE_PATH_BUFFER_SIZE] = { 0 }; if (snprintf(path, PWM_DEVICE_PATH_BUFFER_SIZE, PWM_DEVICE_PATH_FORMAT, timer) < 0) { return false; } struct pwm_lowerhalf_s* pwm_lowerhalf = iotjs_pwm_config_nuttx(timer, pwm->pin); DDDLOG("%s - path: %s, timer: %d\n", __func__, path, timer); if (pwm_register(path, pwm_lowerhalf) != 0) { return false; } // File open iotjs_pwm_platform_data_t* platform_data = pwm->platform_data; platform_data->device_fd = open(path, O_RDONLY); if (platform_data->device_fd < 0) { DLOG("%s - file open failed", __func__); return false; } if (!pwm_set_options(pwm)) { return false; } return true; }
static bool pwm_set_options(iotjs_pwm_t* pwm) { iotjs_pwm_platform_data_t* platform_data = pwm->platform_data; int fd = platform_data->device_fd; if (fd < 0) { DLOG("%s - file open failed", __func__); return false; } struct pwm_info_s info; // Clamp so that the value inverted fits into uint32 if (pwm->period < 2.33E-10) pwm->period = 2.33E-10; info.frequency = (uint32_t)(1.0 / pwm->period); double duty_value = pwm->duty_cycle * (1 << 16); // 16 bit timer if (duty_value > 0xffff) duty_value = 0xffff; else if (duty_value < 1) duty_value = 1; info.duty = (ub16_t)duty_value; DDDLOG("%s - frequency: %d, duty: %d", __func__, info.frequency, info.duty); // Set Pwm info if (ioctl(fd, PWMIOC_SETCHARACTERISTICS, (unsigned long)((uintptr_t)&info)) < 0) { return false; } return true; }
bool iotjs_pwm_close(iotjs_pwm_t* pwm) { iotjs_pwm_platform_data_t* platform_data = pwm->platform_data; int fd = platform_data->device_fd; if (fd < 0) { DLOG("%s - file not opened", __func__); return false; } DDDLOG("%s", __func__); // Close file close(fd); platform_data->device_fd = -1; uint32_t timer = SYSIO_GET_TIMER(pwm->pin); char path[PWM_DEVICE_PATH_BUFFER_SIZE] = { 0 }; if (snprintf(path, PWM_DEVICE_PATH_BUFFER_SIZE - 1, PWM_DEVICE_PATH_FORMAT, timer) < 0) { return false; } // Release driver unregister_driver(path); iotjs_gpio_unconfig_nuttx(pwm->pin); return true; }
bool iotjs_pwm_set_enable(iotjs_pwm_t* pwm) { iotjs_pwm_platform_data_t* platform_data = pwm->platform_data; int fd = platform_data->device_fd; if (fd < 0) { DLOG("%s - file open failed", __func__); return false; } DDDLOG("%s - enable: %d", __func__, pwm->enable); int ret; if (pwm->enable) { ret = ioctl(fd, PWMIOC_START, 0); } else { ret = ioctl(fd, PWMIOC_STOP, 0); } if (ret < 0) { DLOG("%s - setEnable failed", __func__); return false; } return true; }
bool iotjs_gpio_write(iotjs_gpio_t* gpio) { IOTJS_VALIDATED_STRUCT_METHOD(iotjs_gpio_t, gpio); char value_path[GPIO_PATH_BUFFER_SIZE]; snprintf(value_path, GPIO_PATH_BUFFER_SIZE, GPIO_PIN_FORMAT_VALUE, _this->pin); const char* buffer = _this->value ? "1" : "0"; DDDLOG("%s - pin: %d, value: %d", __func__, _this->pin, _this->value); return iotjs_systemio_open_write_close(value_path, buffer); }
static bool gpio_set_direction(uint32_t pin, GpioDirection direction) { IOTJS_ASSERT(direction == kGpioDirectionIn || direction == kGpioDirectionOut); char direction_path[GPIO_PATH_BUFFER_SIZE]; snprintf(direction_path, GPIO_PATH_BUFFER_SIZE, GPIO_PIN_FORMAT_DIRECTION, pin); const char* buffer = (direction == kGpioDirectionIn) ? "in" : "out"; DDDLOG("%s - path: %s, dir: %s", __func__, direction_path, buffer); return iotjs_systemio_open_write_close(direction_path, buffer); }
bool iotjs_adc_open(iotjs_adc_t* adc) { iotjs_adc_platform_data_t* platform_data = adc->platform_data; uint32_t pin = platform_data->pin; int32_t adc_number = ADC_GET_NUMBER(pin); int32_t timer = SYSIO_GET_TIMER(pin); struct adc_dev_s* adc_dev = iotjs_adc_config_nuttx(adc_number, timer, pin); char path[ADC_DEVICE_PATH_BUFFER_SIZE] = { 0 }; adc_get_path(path, adc_number); if (adc_register(path, adc_dev) != 0) { return false; } DDDLOG("%s - path: %s, number: %d, timer: %d", __func__, path, adc_number, timer); return true; }
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); }
bool iotjs_gpio_open(iotjs_gpio_t* gpio) { IOTJS_VALIDATED_STRUCT_METHOD(iotjs_gpio_t, gpio); DDDLOG("%s - pin: %d, dir: %d, mode: %d", __func__, _this->pin, _this->direction, _this->mode); // Open GPIO pin. char exported_path[GPIO_PATH_BUFFER_SIZE]; snprintf(exported_path, GPIO_PATH_BUFFER_SIZE, GPIO_PIN_FORMAT, _this->pin); const char* created_files[] = { GPIO_DIRECTION, GPIO_EDGE, GPIO_VALUE }; int created_files_length = sizeof(created_files) / sizeof(created_files[0]); if (!iotjs_systemio_device_open(GPIO_PIN_FORMAT_EXPORT, _this->pin, exported_path, created_files, created_files_length)) { return false; } // Set direction. if (!gpio_set_direction(_this->pin, _this->direction)) { return false; } // Set mode. if (!gpio_set_mode(_this->pin, _this->mode)) { return false; } // Set edge. if (!gpio_set_edge(gpio)) { return false; } return true; }