7eb695dc59429327c68268dc82c0148cf223a7ac
[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-2013 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 "env.h"
34 #include <stdio.h>
35 #include <fcntl.h>
36 #include "lib/platform/sockets/serialport.h"
37 #include "lib/platform/util/baudrate.h"
38 #include "lib/platform/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 #ifdef HAVE_LOCKDEV
52 #include <lockdev.h>
53 #endif
54 #endif
55
56 using namespace std;
57 using namespace PLATFORM;
58
59 inline bool RemoveLock(const char *strDeviceName)
60 {
61 #if !defined(__APPLE__) && !defined(__FreeBSD__) && defined(HAVE_LOCKDEV)
62 return dev_unlock(strDeviceName, 0) == 0;
63 #else
64 void *tmp = (void*)strDeviceName; // silence unused warning
65 tmp = NULL;
66 return true;
67 #endif
68 }
69
70 void CSerialSocket::Close(void)
71 {
72 if (IsOpen())
73 {
74 SocketClose(m_socket);
75 RemoveLock(m_strName.c_str());
76 }
77 }
78
79 void CSerialSocket::Shutdown(void)
80 {
81 if (IsOpen())
82 {
83 SocketClose(m_socket);
84 RemoveLock(m_strName.c_str());
85 }
86 }
87
88 ssize_t CSerialSocket::Write(void* data, size_t len)
89 {
90 return IsOpen() ? SocketWrite(m_socket, &m_iError, data, len) : -1;
91 }
92
93 ssize_t CSerialSocket::Read(void* data, size_t len, uint64_t iTimeoutMs /* = 0 */)
94 {
95 return IsOpen() ? SocketRead(m_socket, &m_iError, data, len, iTimeoutMs) : -1;
96 }
97
98 //setting all this stuff up is a pain in the ass
99 bool CSerialSocket::Open(uint64_t iTimeoutMs /* = 0 */)
100 {
101 iTimeoutMs = 0; if (!iTimeoutMs){} // silence unused warning
102 if (IsOpen())
103 {
104 m_iError = EINVAL;
105 return false;
106 }
107
108 if (m_iDatabits != SERIAL_DATA_BITS_FIVE && m_iDatabits != SERIAL_DATA_BITS_SIX &&
109 m_iDatabits != SERIAL_DATA_BITS_SEVEN && m_iDatabits != SERIAL_DATA_BITS_EIGHT)
110 {
111 m_strError = "Databits has to be between 5 and 8";
112 m_iError = EINVAL;
113 return false;
114 }
115
116 if (m_iStopbits != SERIAL_STOP_BITS_ONE && m_iStopbits != SERIAL_STOP_BITS_TWO)
117 {
118 m_strError = "Stopbits has to be 1 or 2";
119 m_iError = EINVAL;
120 return false;
121 }
122
123 if (m_iParity != SERIAL_PARITY_NONE && m_iParity != SERIAL_PARITY_EVEN && m_iParity != SERIAL_PARITY_ODD)
124 {
125 m_strError = "Parity has to be none, even or odd";
126 m_iError = EINVAL;
127 return false;
128 }
129
130 #if !defined(__APPLE__) && !defined(__FreeBSD__) && defined(HAVE_LOCKDEV)
131 if (dev_lock(m_strName.c_str()) != 0)
132 {
133 m_strError = "Couldn't lock the serial port";
134 m_iError = EBUSY;
135 return false;
136 }
137 #endif
138
139 m_socket = open(m_strName.c_str(), O_RDWR | O_NOCTTY | O_NDELAY);
140
141 if (m_socket == INVALID_SERIAL_SOCKET_VALUE)
142 {
143 m_strError = strerror(errno);
144 RemoveLock(m_strName.c_str());
145 return false;
146 }
147
148 SocketSetBlocking(m_socket, false);
149
150 if (!SetBaudRate(m_iBaudrate))
151 return false;
152
153 m_options.c_cflag |= (CLOCAL | CREAD);
154 m_options.c_cflag &= ~HUPCL;
155
156 m_options.c_cflag &= ~CSIZE;
157 if (m_iDatabits == SERIAL_DATA_BITS_FIVE) m_options.c_cflag |= CS5;
158 if (m_iDatabits == SERIAL_DATA_BITS_SIX) m_options.c_cflag |= CS6;
159 if (m_iDatabits == SERIAL_DATA_BITS_SEVEN) m_options.c_cflag |= CS7;
160 if (m_iDatabits == SERIAL_DATA_BITS_EIGHT) m_options.c_cflag |= CS8;
161
162 m_options.c_cflag &= ~PARENB;
163 if (m_iParity == SERIAL_PARITY_EVEN || m_iParity == SERIAL_PARITY_ODD)
164 m_options.c_cflag |= PARENB;
165 if (m_iParity == SERIAL_PARITY_ODD)
166 m_options.c_cflag |= PARODD;
167
168 #ifdef CRTSCTS
169 m_options.c_cflag &= ~CRTSCTS;
170 #elif defined(CNEW_RTSCTS)
171 m_options.c_cflag &= ~CNEW_RTSCTS;
172 #endif
173
174 if (m_iStopbits == SERIAL_STOP_BITS_ONE) m_options.c_cflag &= ~CSTOPB;
175 else m_options.c_cflag |= CSTOPB;
176
177 //I guessed a little here
178 m_options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG | XCASE | ECHOK | ECHONL | ECHOCTL | ECHOPRT | ECHOKE | TOSTOP);
179
180 if (m_iParity == SERIAL_PARITY_NONE)
181 m_options.c_iflag &= ~INPCK;
182 else
183 m_options.c_iflag |= INPCK | ISTRIP;
184
185 m_options.c_iflag &= ~(IXON | IXOFF | IXANY | BRKINT | INLCR | IGNCR | ICRNL | IUCLC | IMAXBEL);
186 m_options.c_oflag &= ~(OPOST | ONLCR | OCRNL);
187
188 if (tcsetattr(m_socket, TCSANOW, &m_options) != 0)
189 {
190 m_strError = strerror(errno);
191 RemoveLock(m_strName.c_str());
192 return false;
193 }
194
195 SocketSetBlocking(m_socket, true);
196 m_bIsOpen = true;
197
198 return true;
199 }
200
201 bool CSerialSocket::SetBaudRate(uint32_t baudrate)
202 {
203 int rate = IntToBaudrate(baudrate);
204 if (rate == -1)
205 {
206 char buff[255];
207 sprintf(buff, "%i is not a valid baudrate", baudrate);
208 m_strError = buff;
209 return false;
210 }
211
212 //get the current port attributes
213 if (tcgetattr(m_socket, &m_options) != 0)
214 {
215 m_strError = strerror(errno);
216 return false;
217 }
218
219 if (cfsetispeed(&m_options, rate) != 0)
220 {
221 m_strError = strerror(errno);
222 return false;
223 }
224
225 if (cfsetospeed(&m_options, rate) != 0)
226 {
227 m_strError = strerror(errno);
228 return false;
229 }
230
231 return true;
232 }