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