1c1002731805c3ea33ab5956ded8cc619968bcd4
[platform/core/api/peripheral-io.git] / src / interface / peripheral_interface_uart.c
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <termios.h>
18
19 #include "peripheral_interface_uart.h"
20
21 #define PATH_BUF_MAX            64
22 #define UART_BUF_MAX            16
23
24 #define UART_BAUDRATE_SIZE      19
25
26 #ifndef ARRAY_SIZE
27 #define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0]))
28 #endif
29 #define MAX_ERR_LEN 128
30
31 char *sysfs_uart_path[] = {
32         "/dev/ttyS",
33         "/dev/ttyAMA",
34         "/dev/ttySAC",
35 };
36
37 static const int peripheral_uart_br[UART_BAUDRATE_SIZE] = {
38         B0,                     B50,            B75,            B110,           B134,
39         B150,           B200,           B300,           B600,           B1200,
40         B1800,          B2400,          B4800,          B9600,          B19200,
41         B38400,         B57600,         B115200,        B230400
42 };
43
44 static const int byteinfo[4] = {CS5, CS6, CS7, CS8};
45
46 int peripheral_interface_uart_close(peripheral_uart_h uart)
47 {
48         int ret;
49
50         peripheral_interface_uart_flush(uart);
51
52         ret = close(uart->fd);
53         CHECK_ERROR(ret != 0);
54
55         return 0;
56 }
57
58 int peripheral_interface_uart_flush(peripheral_uart_h uart)
59 {
60         int ret = tcflush(uart->fd, TCIOFLUSH);
61         CHECK_ERROR(ret != 0);
62
63         return 0;
64 }
65
66 int peripheral_interface_uart_set_baud_rate(peripheral_uart_h uart, peripheral_uart_baud_rate_e baud)
67 {
68         int ret;
69         struct termios tio;
70
71         ret = tcgetattr(uart->fd, &tio);
72         CHECK_ERROR(ret != 0);
73
74         tio.c_cflag = peripheral_uart_br[baud];
75         tio.c_iflag = IGNPAR;
76         tio.c_oflag = 0;
77         tio.c_lflag = 0;
78         tio.c_cc[VMIN] = 1;
79         tio.c_cc[VTIME] = 0;
80
81         peripheral_interface_uart_flush(uart);
82         ret = tcsetattr(uart->fd, TCSANOW, &tio);
83         CHECK_ERROR(ret != 0);
84
85         return 0;
86 }
87
88 int peripheral_interface_uart_set_byte_size(peripheral_uart_h uart, peripheral_uart_byte_size_e byte_size)
89 {
90         int ret;
91         struct termios tio;
92
93         ret = tcgetattr(uart->fd, &tio);
94         CHECK_ERROR(ret != 0);
95
96         /* set byte size */
97         tio.c_cflag &= ~CSIZE;
98         tio.c_cflag |= byteinfo[byte_size];
99         tio.c_cflag |= (CLOCAL | CREAD);
100
101         peripheral_interface_uart_flush(uart);
102         ret = tcsetattr(uart->fd, TCSANOW, &tio);
103         CHECK_ERROR(ret != 0);
104
105         return 0;
106 }
107
108 int peripheral_interface_uart_set_parity(peripheral_uart_h uart, peripheral_uart_parity_e parity)
109 {
110         int ret;
111         struct termios tio;
112
113         ret = tcgetattr(uart->fd, &tio);
114         CHECK_ERROR(ret != 0);
115
116         /* set parity info */
117         switch (parity) {
118         case PERIPHERAL_UART_PARITY_EVEN:
119                 tio.c_cflag |= PARENB;
120                 tio.c_cflag &= ~PARODD;
121                 break;
122         case PERIPHERAL_UART_PARITY_ODD:
123                 tio.c_cflag |= PARENB;
124                 tio.c_cflag |= PARODD;
125                 break;
126         case PERIPHERAL_UART_PARITY_NONE:
127         default:
128                 tio.c_cflag &= ~PARENB;
129                 tio.c_cflag &= ~PARODD;
130                 break;
131         }
132
133         peripheral_interface_uart_flush(uart);
134         ret = tcsetattr(uart->fd, TCSANOW, &tio);
135         CHECK_ERROR(ret != 0);
136
137         return 0;
138 }
139
140 int peripheral_interface_uart_set_stop_bits(peripheral_uart_h uart, peripheral_uart_stop_bits_e stop_bits)
141 {
142         int ret;
143         struct termios tio;
144
145         ret = tcgetattr(uart->fd, &tio);
146         CHECK_ERROR(ret != 0);
147
148         /* set stop bit */
149         switch (stop_bits) {
150         case PERIPHERAL_UART_STOP_BITS_1BIT:
151                 tio.c_cflag &= ~CSTOPB;
152                 break;
153         case PERIPHERAL_UART_STOP_BITS_2BIT:
154                 tio.c_cflag |= CSTOPB;
155                 break;
156         default:
157                 _E("Invalid parameter stop_bits");
158                 return -EINVAL;
159         }
160
161         peripheral_interface_uart_flush(uart);
162         ret = tcsetattr(uart->fd, TCSANOW, &tio);
163         CHECK_ERROR(ret != 0);
164
165         return 0;
166 }
167
168 int peripheral_interface_uart_set_flow_control(peripheral_uart_h uart, peripheral_uart_software_flow_control_e xonxoff, peripheral_uart_hardware_flow_control_e rtscts)
169 {
170         int ret;
171         struct termios tio;
172
173         ret = tcgetattr(uart->fd, &tio);
174         CHECK_ERROR(ret != 0);
175
176         if (rtscts == PERIPHERAL_UART_HARDWARE_FLOW_CONTROL_AUTO_RTSCTS)
177                 tio.c_cflag |= CRTSCTS;
178         else if (rtscts == PERIPHERAL_UART_HARDWARE_FLOW_CONTROL_NONE)
179                 tio.c_cflag &= ~CRTSCTS;
180         else
181                 return -EINVAL;
182
183         if (xonxoff == PERIPHERAL_UART_SOFTWARE_FLOW_CONTROL_XONXOFF)
184                 tio.c_iflag |= (IXON | IXOFF | IXANY);
185         else if (xonxoff == PERIPHERAL_UART_SOFTWARE_FLOW_CONTROL_NONE)
186                 tio.c_iflag &= ~(IXON | IXOFF | IXANY);
187         else
188                 return -EINVAL;
189
190         ret = tcsetattr(uart->fd, TCSANOW, &tio);
191         CHECK_ERROR(ret != 0);
192
193         return 0;
194 }
195
196 int peripheral_interface_uart_read(peripheral_uart_h uart, uint8_t *buf, uint32_t length)
197 {
198         int ret = read(uart->fd, (void *)buf, length);
199         CHECK_ERROR(ret != length);
200
201         return ret;
202 }
203
204 int peripheral_interface_uart_write(peripheral_uart_h uart, uint8_t *buf, uint32_t length)
205 {
206         int ret = write(uart->fd, buf, length);
207         CHECK_ERROR(ret != length);
208
209         return ret;
210 }