1 /* Copyright 2016-present Samsung Electronics Co., Ltd. and other contributors
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
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef IOTJS_MODULE_UART_LINUX_GENERAL_INL_H
18 #define IOTJS_MODULE_UART_LINUX_GENERAL_INL_H
24 #include "module/iotjs_module_uart.h"
27 static int baud_to_constant(int baudRate) {
72 static int databits_to_constant(int dataBits) {
87 void iotjs_uart_open_worker(uv_work_t* work_req) {
89 IOTJS_VALIDATED_STRUCT_METHOD(iotjs_uart_t, uart);
91 int fd = open(iotjs_string_data(&_this->device_path),
92 O_RDWR | O_NOCTTY | O_NDELAY);
94 req_data->result = false;
98 struct termios options;
99 tcgetattr(fd, &options);
100 options.c_cflag = CLOCAL | CREAD;
101 options.c_cflag |= baud_to_constant(_this->baud_rate);
102 options.c_cflag |= databits_to_constant(_this->data_bits);
103 options.c_iflag = IGNPAR;
106 tcflush(fd, TCIFLUSH);
107 tcsetattr(fd, TCSANOW, &options);
109 _this->device_fd = fd;
110 uv_poll_t* poll_handle = &_this->poll_handle;
112 uv_loop_t* loop = iotjs_environment_loop(iotjs_environment_get());
113 uv_poll_init(loop, poll_handle, fd);
114 poll_handle->data = uart;
115 uv_poll_start(poll_handle, UV_READABLE, iotjs_uart_read_cb);
117 req_data->result = true;
121 bool iotjs_uart_write(iotjs_uart_t* uart) {
122 IOTJS_VALIDATED_STRUCT_METHOD(iotjs_uart_t, uart);
123 int bytesWritten = 0;
125 int fd = _this->device_fd;
126 const char* buf_data = iotjs_string_data(&_this->buf_data);
128 DDDLOG("%s - data: %s", __func__, buf_data);
132 bytesWritten = write(fd, buf_data + offset, _this->buf_len - offset);
135 DDDLOG("%s - size: %d", __func__, _this->buf_len - offset);
137 if (bytesWritten != -1) {
138 offset += bytesWritten;
142 if (errno == EINTR) {
148 } while (_this->buf_len > offset);
154 #endif /* IOTJS_MODULE_UART_LINUX_GENERAL_INL_H */