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