3764a1567f9673a26aedfaa0ba1192cfe4884145
[deb_libcec.git] / src / lib / platform / posix / serialport.cpp
1 /*
2 * This file is part of the libCEC(R) library.
3 *
4 * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved.
5 * libCEC(R) is an original work, containing original code.
6 *
7 * libCEC(R) is a trademark of Pulse-Eight Limited.
8 *
9 * This program is dual-licensed; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 *
23 *
24 * Alternatively, you can license this library under a commercial license,
25 * please contact Pulse-Eight Licensing for more information.
26 *
27 * For more information contact:
28 * Pulse-Eight Licensing <license@pulse-eight.com>
29 * http://www.pulse-eight.com/
30 * http://www.pulse-eight.net/
31 */
32
33 #include "../os.h"
34 #include <stdio.h>
35 #include <fcntl.h>
36 #include "../sockets/serialport.h"
37 #include "../util/baudrate.h"
38 #include "../posix/os-socket.h"
39
40 #if defined(__APPLE__) || defined(__FreeBSD__)
41 #ifndef XCASE
42 #define XCASE 0
43 #endif
44 #ifndef OLCUC
45 #define OLCUC 0
46 #endif
47 #ifndef IUCLC
48 #define IUCLC 0
49 #endif
50 #else
51 #include <lockdev.h>
52 #endif
53
54 using namespace std;
55 using namespace PLATFORM;
56
57 inline bool RemoveLock(const char *strDeviceName)
58 {
59 #if !defined(__APPLE__) && !defined(__FreeBSD__)
60 return dev_unlock(strDeviceName, 0) == 0;
61 #endif
62 }
63
64 void CSerialSocket::Close(void)
65 {
66 if (IsOpen())
67 {
68 SocketClose(m_socket);
69 RemoveLock(m_strName.c_str());
70 }
71 }
72
73 void CSerialSocket::Shutdown(void)
74 {
75 if (IsOpen())
76 {
77 SocketClose(m_socket);
78 RemoveLock(m_strName.c_str());
79 }
80 }
81
82 ssize_t CSerialSocket::Write(void* data, size_t len)
83 {
84 return IsOpen() ? SocketWrite(m_socket, &m_iError, data, len) : -1;
85 }
86
87 ssize_t CSerialSocket::Read(void* data, size_t len, uint64_t iTimeoutMs /* = 0 */)
88 {
89 return IsOpen() ? SocketRead(m_socket, &m_iError, data, len, iTimeoutMs) : -1;
90 }
91
92 //setting all this stuff up is a pain in the ass
93 bool CSerialSocket::Open(uint64_t iTimeoutMs /* = 0 */)
94 {
95 iTimeoutMs = 0;
96 if (IsOpen())
97 {
98 m_iError = EINVAL;
99 return false;
100 }
101
102 if (m_iDatabits != SERIAL_DATA_BITS_FIVE && m_iDatabits != SERIAL_DATA_BITS_SIX &&
103 m_iDatabits != SERIAL_DATA_BITS_SEVEN && m_iDatabits != SERIAL_DATA_BITS_EIGHT)
104 {
105 m_strError = "Databits has to be between 5 and 8";
106 m_iError = EINVAL;
107 return false;
108 }
109
110 if (m_iStopbits != SERIAL_STOP_BITS_ONE && m_iStopbits != SERIAL_STOP_BITS_TWO)
111 {
112 m_strError = "Stopbits has to be 1 or 2";
113 m_iError = EINVAL;
114 return false;
115 }
116
117 if (m_iParity != SERIAL_PARITY_NONE && m_iParity != SERIAL_PARITY_EVEN && m_iParity != SERIAL_PARITY_ODD)
118 {
119 m_strError = "Parity has to be none, even or odd";
120 m_iError = EINVAL;
121 return false;
122 }
123
124 #if !defined(__APPLE__) && !defined(__FreeBSD__)
125 if (dev_lock(m_strName.c_str()) != 0)
126 {
127 m_strError = "Couldn't lock the serial port";
128 m_iError = EBUSY;
129 return false;
130 }
131 #endif
132
133 m_socket = open(m_strName.c_str(), O_RDWR | O_NOCTTY | O_NDELAY);
134
135 if (m_socket == INVALID_SERIAL_SOCKET_VALUE)
136 {
137 m_strError = strerror(errno);
138 RemoveLock(m_strName.c_str());
139 return false;
140 }
141
142 SocketSetBlocking(m_socket, false);
143
144 if (!SetBaudRate(m_iBaudrate))
145 return false;
146
147 m_options.c_cflag |= (CLOCAL | CREAD);
148 m_options.c_cflag &= ~HUPCL;
149
150 m_options.c_cflag &= ~CSIZE;
151 if (m_iDatabits == SERIAL_DATA_BITS_FIVE) m_options.c_cflag |= CS5;
152 if (m_iDatabits == SERIAL_DATA_BITS_SIX) m_options.c_cflag |= CS6;
153 if (m_iDatabits == SERIAL_DATA_BITS_SEVEN) m_options.c_cflag |= CS7;
154 if (m_iDatabits == SERIAL_DATA_BITS_EIGHT) m_options.c_cflag |= CS8;
155
156 m_options.c_cflag &= ~PARENB;
157 if (m_iParity == SERIAL_PARITY_EVEN || m_iParity == SERIAL_PARITY_ODD)
158 m_options.c_cflag |= PARENB;
159 if (m_iParity == SERIAL_PARITY_ODD)
160 m_options.c_cflag |= PARODD;
161
162 #ifdef CRTSCTS
163 m_options.c_cflag &= ~CRTSCTS;
164 #elif defined(CNEW_RTSCTS)
165 m_options.c_cflag &= ~CNEW_RTSCTS;
166 #endif
167
168 if (m_iStopbits == SERIAL_STOP_BITS_ONE) m_options.c_cflag &= ~CSTOPB;
169 else m_options.c_cflag |= CSTOPB;
170
171 //I guessed a little here
172 m_options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG | XCASE | ECHOK | ECHONL | ECHOCTL | ECHOPRT | ECHOKE | TOSTOP);
173
174 if (m_iParity == SERIAL_PARITY_NONE)
175 m_options.c_iflag &= ~INPCK;
176 else
177 m_options.c_iflag |= INPCK | ISTRIP;
178
179 m_options.c_iflag &= ~(IXON | IXOFF | IXANY | BRKINT | INLCR | IGNCR | ICRNL | IUCLC | IMAXBEL);
180 m_options.c_oflag &= ~(OPOST | ONLCR | OCRNL);
181
182 if (tcsetattr(m_socket, TCSANOW, &m_options) != 0)
183 {
184 m_strError = strerror(errno);
185 RemoveLock(m_strName.c_str());
186 return false;
187 }
188
189 SocketSetBlocking(m_socket, true);
190 m_bIsOpen = true;
191
192 return true;
193 }
194
195 bool CSerialSocket::SetBaudRate(uint32_t baudrate)
196 {
197 int rate = IntToBaudrate(baudrate);
198 if (rate == -1)
199 {
200 char buff[255];
201 sprintf(buff, "%i is not a valid baudrate", baudrate);
202 m_strError = buff;
203 return false;
204 }
205
206 //get the current port attributes
207 if (tcgetattr(m_socket, &m_options) != 0)
208 {
209 m_strError = strerror(errno);
210 return false;
211 }
212
213 if (cfsetispeed(&m_options, rate) != 0)
214 {
215 m_strError = strerror(errno);
216 return false;
217 }
218
219 if (cfsetospeed(&m_options, rate) != 0)
220 {
221 m_strError = strerror(errno);
222 return false;
223 }
224
225 return true;
226 }