eadef4a9289aa0e96b57ffd6addf76bff536832a
[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 UART_BAUDRATE_SIZE      19
22
23 static const int peripheral_uart_br[UART_BAUDRATE_SIZE] = {
24         B0,                     B50,            B75,            B110,           B134,
25         B150,           B200,           B300,           B600,           B1200,
26         B1800,          B2400,          B4800,          B9600,          B19200,
27         B38400,         B57600,         B115200,        B230400
28 };
29
30 static const int byteinfo[4] = {CS5, CS6, CS7, CS8};
31
32 void peripheral_interface_uart_close(peripheral_uart_h uart)
33 {
34         peripheral_interface_uart_flush(uart);
35         close(uart->fd);
36 }
37
38 int peripheral_interface_uart_flush(peripheral_uart_h uart)
39 {
40         int ret = tcflush(uart->fd, TCIOFLUSH);
41         CHECK_ERROR(ret != 0);
42
43         return PERIPHERAL_ERROR_NONE;
44 }
45
46 int peripheral_interface_uart_set_baud_rate(peripheral_uart_h uart, peripheral_uart_baud_rate_e baud)
47 {
48         int ret;
49         struct termios tio;
50
51         ret = tcgetattr(uart->fd, &tio);
52         CHECK_ERROR(ret != 0);
53
54         tio.c_cflag = peripheral_uart_br[baud];
55         tio.c_iflag = IGNPAR;
56         tio.c_oflag = 0;
57         tio.c_lflag = 0;
58         tio.c_cc[VMIN] = 1;
59         tio.c_cc[VTIME] = 0;
60
61         peripheral_interface_uart_flush(uart);
62         ret = tcsetattr(uart->fd, TCSANOW, &tio);
63         CHECK_ERROR(ret != 0);
64
65         return PERIPHERAL_ERROR_NONE;
66 }
67
68 int peripheral_interface_uart_set_byte_size(peripheral_uart_h uart, peripheral_uart_byte_size_e byte_size)
69 {
70         int ret;
71         struct termios tio;
72
73         ret = tcgetattr(uart->fd, &tio);
74         CHECK_ERROR(ret != 0);
75
76         /* set byte size */
77         tio.c_cflag &= ~CSIZE;
78         tio.c_cflag |= byteinfo[byte_size];
79         tio.c_cflag |= (CLOCAL | CREAD);
80
81         peripheral_interface_uart_flush(uart);
82         ret = tcsetattr(uart->fd, TCSANOW, &tio);
83         CHECK_ERROR(ret != 0);
84
85         return PERIPHERAL_ERROR_NONE;
86 }
87
88 int peripheral_interface_uart_set_parity(peripheral_uart_h uart, peripheral_uart_parity_e parity)
89 {
90         int ret;
91         struct termios tio;
92
93         ret = tcgetattr(uart->fd, &tio);
94         CHECK_ERROR(ret != 0);
95
96         /* set parity info */
97         switch (parity) {
98         case PERIPHERAL_UART_PARITY_EVEN:
99                 tio.c_cflag |= PARENB;
100                 tio.c_cflag &= ~PARODD;
101                 break;
102         case PERIPHERAL_UART_PARITY_ODD:
103                 tio.c_cflag |= PARENB;
104                 tio.c_cflag |= PARODD;
105                 break;
106         case PERIPHERAL_UART_PARITY_NONE:
107         default:
108                 tio.c_cflag &= ~PARENB;
109                 tio.c_cflag &= ~PARODD;
110                 break;
111         }
112
113         peripheral_interface_uart_flush(uart);
114         ret = tcsetattr(uart->fd, TCSANOW, &tio);
115         CHECK_ERROR(ret != 0);
116
117         return PERIPHERAL_ERROR_NONE;
118 }
119
120 int peripheral_interface_uart_set_stop_bits(peripheral_uart_h uart, peripheral_uart_stop_bits_e stop_bits)
121 {
122         int ret;
123         struct termios tio;
124
125         ret = tcgetattr(uart->fd, &tio);
126         CHECK_ERROR(ret != 0);
127
128         /* set stop bit */
129         switch (stop_bits) {
130         case PERIPHERAL_UART_STOP_BITS_1BIT:
131                 tio.c_cflag &= ~CSTOPB;
132                 break;
133         case PERIPHERAL_UART_STOP_BITS_2BIT:
134                 tio.c_cflag |= CSTOPB;
135                 break;
136         default:
137                 _E("Invalid parameter stop_bits");
138                 return PERIPHERAL_ERROR_INVALID_PARAMETER;
139         }
140
141         peripheral_interface_uart_flush(uart);
142         ret = tcsetattr(uart->fd, TCSANOW, &tio);
143         CHECK_ERROR(ret != 0);
144
145         return PERIPHERAL_ERROR_NONE;
146 }
147
148 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)
149 {
150         int ret;
151         struct termios tio;
152
153         ret = tcgetattr(uart->fd, &tio);
154         CHECK_ERROR(ret != 0);
155
156         if (rtscts == PERIPHERAL_UART_HARDWARE_FLOW_CONTROL_AUTO_RTSCTS)
157                 tio.c_cflag |= CRTSCTS;
158         else if (rtscts == PERIPHERAL_UART_HARDWARE_FLOW_CONTROL_NONE)
159                 tio.c_cflag &= ~CRTSCTS;
160         else
161                 return PERIPHERAL_ERROR_INVALID_PARAMETER;
162
163         if (xonxoff == PERIPHERAL_UART_SOFTWARE_FLOW_CONTROL_XONXOFF)
164                 tio.c_iflag |= (IXON | IXOFF | IXANY);
165         else if (xonxoff == PERIPHERAL_UART_SOFTWARE_FLOW_CONTROL_NONE)
166                 tio.c_iflag &= ~(IXON | IXOFF | IXANY);
167         else
168                 return PERIPHERAL_ERROR_INVALID_PARAMETER;
169
170         ret = tcsetattr(uart->fd, TCSANOW, &tio);
171         CHECK_ERROR(ret != 0);
172
173         return PERIPHERAL_ERROR_NONE;
174 }
175
176 int peripheral_interface_uart_read(peripheral_uart_h uart, uint8_t *buf, uint32_t length)
177 {
178         int ret = read(uart->fd, (void *)buf, length);
179         CHECK_ERROR(ret != length);
180
181         return PERIPHERAL_ERROR_NONE;
182 }
183
184 int peripheral_interface_uart_write(peripheral_uart_h uart, uint8_t *buf, uint32_t length)
185 {
186         int ret = write(uart->fd, buf, length);
187         CHECK_ERROR(ret != length);
188
189         return PERIPHERAL_ERROR_NONE;
190 }