Update Iot.js
[platform/upstream/iotjs.git] / src / platform / iotjs_module_uart-linux-general.inl.h
1 /* Copyright 2016-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
17 #ifndef IOTJS_MODULE_UART_LINUX_GENERAL_INL_H
18 #define IOTJS_MODULE_UART_LINUX_GENERAL_INL_H
19
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <termios.h>
23 #include <unistd.h>
24 #include "module/iotjs_module_uart.h"
25
26
27 static int baud_to_constant(int baudRate) {
28   switch (baudRate) {
29     case 0:
30       return B0;
31     case 50:
32       return B50;
33     case 75:
34       return B75;
35     case 110:
36       return B110;
37     case 134:
38       return B134;
39     case 150:
40       return B150;
41     case 200:
42       return B200;
43     case 300:
44       return B300;
45     case 600:
46       return B600;
47     case 1200:
48       return B1200;
49     case 1800:
50       return B1800;
51     case 2400:
52       return B2400;
53     case 4800:
54       return B4800;
55     case 9600:
56       return B9600;
57     case 19200:
58       return B19200;
59     case 38400:
60       return B38400;
61     case 57600:
62       return B57600;
63     case 115200:
64       return B115200;
65     case 230400:
66       return B230400;
67   }
68   return -1;
69 }
70
71
72 static int databits_to_constant(int dataBits) {
73   switch (dataBits) {
74     case 8:
75       return CS8;
76     case 7:
77       return CS7;
78     case 6:
79       return CS6;
80     case 5:
81       return CS5;
82   }
83   return -1;
84 }
85
86
87 void iotjs_uart_open_worker(uv_work_t* work_req) {
88   UART_WORKER_INIT;
89   IOTJS_VALIDATED_STRUCT_METHOD(iotjs_uart_t, uart);
90
91   int fd = open(iotjs_string_data(&_this->device_path),
92                 O_RDWR | O_NOCTTY | O_NDELAY);
93   if (fd < 0) {
94     req_data->result = false;
95     return;
96   }
97
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;
104   options.c_oflag = 0;
105   options.c_lflag = 0;
106   tcflush(fd, TCIFLUSH);
107   tcsetattr(fd, TCSANOW, &options);
108
109   _this->device_fd = fd;
110   uv_poll_t* poll_handle = &_this->poll_handle;
111
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);
116
117   req_data->result = true;
118 }
119
120
121 bool iotjs_uart_write(iotjs_uart_t* uart) {
122   IOTJS_VALIDATED_STRUCT_METHOD(iotjs_uart_t, uart);
123   int bytesWritten = 0;
124   unsigned offset = 0;
125   int fd = _this->device_fd;
126   const char* buf_data = iotjs_string_data(&_this->buf_data);
127
128   DDDLOG("%s - data: %s", __func__, buf_data);
129
130   do {
131     errno = 0;
132     bytesWritten = write(fd, buf_data + offset, _this->buf_len - offset);
133     tcdrain(fd);
134
135     DDDLOG("%s - size: %d", __func__, _this->buf_len - offset);
136
137     if (bytesWritten != -1) {
138       offset += bytesWritten;
139       continue;
140     }
141
142     if (errno == EINTR) {
143       continue;
144     }
145
146     return false;
147
148   } while (_this->buf_len > offset);
149
150   return true;
151 }
152
153
154 #endif /* IOTJS_MODULE_UART_LINUX_GENERAL_INL_H */