|
| 1 | +/* Copyright 2017-present Samsung Electronics Co., Ltd. and other contributors |
| 2 | + * |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +#if defined(__TIZENRT__) |
| 17 | + |
| 18 | + |
| 19 | +#include "modules/iotjs_module_uart.h" |
| 20 | + |
| 21 | +void iotjs_uart_open_worker(uv_work_t* work_req) { |
| 22 | + UART_WORKER_INIT; |
| 23 | + IOTJS_VALIDATED_STRUCT_METHOD(iotjs_uart_t, uart); |
| 24 | + |
| 25 | + int fd = open(iotjs_string_data(&_this->device_path), |
| 26 | + O_RDWR | O_NOCTTY | O_NDELAY); |
| 27 | + |
| 28 | + if (fd < 0) { |
| 29 | + req_data->result = false; |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + _this->device_fd = fd; |
| 34 | + uv_poll_t* poll_handle = &_this->poll_handle; |
| 35 | + |
| 36 | + uv_loop_t* loop = iotjs_environment_loop(iotjs_environment_get()); |
| 37 | + uv_poll_init(loop, poll_handle, fd); |
| 38 | + poll_handle->data = uart; |
| 39 | + uv_poll_start(poll_handle, UV_READABLE, iotjs_uart_read_cb); |
| 40 | + |
| 41 | + req_data->result = true; |
| 42 | +} |
| 43 | + |
| 44 | + |
| 45 | +bool iotjs_uart_write(iotjs_uart_t* uart) { |
| 46 | + IOTJS_VALIDATED_STRUCT_METHOD(iotjs_uart_t, uart); |
| 47 | + int bytesWritten = 0; |
| 48 | + unsigned offset = 0; |
| 49 | + int fd = _this->device_fd; |
| 50 | + const char* buf_data = iotjs_string_data(&_this->buf_data); |
| 51 | + |
| 52 | + DDDLOG("%s - data: %s", __func__, buf_data); |
| 53 | + |
| 54 | + do { |
| 55 | + errno = 0; |
| 56 | + bytesWritten = write(fd, buf_data + offset, _this->buf_len - offset); |
| 57 | + |
| 58 | + DDDLOG("%s - size: %d", __func__, _this->buf_len - offset); |
| 59 | + |
| 60 | + if (bytesWritten != -1) { |
| 61 | + offset += (unsigned)bytesWritten; |
| 62 | + continue; |
| 63 | + } |
| 64 | + |
| 65 | + if (errno == EINTR) { |
| 66 | + continue; |
| 67 | + } |
| 68 | + |
| 69 | + return false; |
| 70 | + |
| 71 | + } while (_this->buf_len > offset); |
| 72 | + |
| 73 | + return true; |
| 74 | +} |
| 75 | + |
| 76 | + |
| 77 | +#endif // __TIZENRT__ |
0 commit comments