Initial FreeBSD support
[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 *
9b53a148 4 * libCEC(R) is Copyright (C) 2011-2012 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
ba65909d 33#include "../os.h"
abbca718 34#include <stdio.h>
abbca718 35#include <fcntl.h>
ba65909d
LOK
36#include "../sockets/serialport.h"
37#include "../util/baudrate.h"
99666519 38#include "../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
50#endif
abbca718 51using namespace std;
f00ff009 52using namespace PLATFORM;
abbca718 53
99666519 54void CSerialSocket::Close(void)
abbca718 55{
99666519
LOK
56 SocketClose(m_socket);
57}
58
59void CSerialSocket::Shutdown(void)
60{
61 SocketClose(m_socket);
62}
63
64ssize_t CSerialSocket::Write(void* data, size_t len)
65{
66 return SocketWrite(m_socket, &m_iError, data, len);
67}
68
69ssize_t CSerialSocket::Read(void* data, size_t len, uint64_t iTimeoutMs /* = 0 */)
70{
71 return SocketRead(m_socket, &m_iError, data, len, iTimeoutMs);
abbca718
LOK
72}
73
abbca718 74//setting all this stuff up is a pain in the ass
99666519 75bool CSerialSocket::Open(uint64_t iTimeoutMs /* = 0 */)
abbca718 76{
99666519 77 iTimeoutMs = 0;
f00ff009 78 CLockObject lock(m_mutex);
b1f50952 79
99666519
LOK
80 if (m_iDatabits != SERIAL_DATA_BITS_FIVE && m_iDatabits != SERIAL_DATA_BITS_SIX &&
81 m_iDatabits != SERIAL_DATA_BITS_SEVEN && m_iDatabits != SERIAL_DATA_BITS_EIGHT)
abbca718 82 {
ba65909d 83 m_strError = "Databits has to be between 5 and 8";
abbca718
LOK
84 return false;
85 }
86
99666519 87 if (m_iStopbits != SERIAL_STOP_BITS_ONE && m_iStopbits != SERIAL_STOP_BITS_TWO)
abbca718 88 {
ba65909d 89 m_strError = "Stopbits has to be 1 or 2";
abbca718
LOK
90 return false;
91 }
92
99666519 93 if (m_iParity != SERIAL_PARITY_NONE && m_iParity != SERIAL_PARITY_EVEN && m_iParity != SERIAL_PARITY_ODD)
abbca718 94 {
ba65909d 95 m_strError = "Parity has to be none, even or odd";
abbca718
LOK
96 return false;
97 }
98
99666519 99 m_socket = open(m_strName.c_str(), O_RDWR | O_NOCTTY | O_NDELAY);
abbca718 100
99666519 101 if (m_socket == INVALID_SERIAL_SOCKET_VALUE)
abbca718 102 {
ba65909d 103 m_strError = strerror(errno);
abbca718
LOK
104 return false;
105 }
106
ba65909d 107 SocketSetBlocking(m_socket, false);
abbca718 108
99666519 109 if (!SetBaudRate(m_iBaudrate))
abbca718 110 return false;
abbca718
LOK
111
112 m_options.c_cflag |= (CLOCAL | CREAD);
113 m_options.c_cflag &= ~HUPCL;
114
115 m_options.c_cflag &= ~CSIZE;
99666519
LOK
116 if (m_iDatabits == SERIAL_DATA_BITS_FIVE) m_options.c_cflag |= CS5;
117 if (m_iDatabits == SERIAL_DATA_BITS_SIX) m_options.c_cflag |= CS6;
118 if (m_iDatabits == SERIAL_DATA_BITS_SEVEN) m_options.c_cflag |= CS7;
119 if (m_iDatabits == SERIAL_DATA_BITS_EIGHT) m_options.c_cflag |= CS8;
abbca718
LOK
120
121 m_options.c_cflag &= ~PARENB;
99666519 122 if (m_iParity == SERIAL_PARITY_EVEN || m_iParity == SERIAL_PARITY_ODD)
abbca718 123 m_options.c_cflag |= PARENB;
99666519 124 if (m_iParity == SERIAL_PARITY_ODD)
abbca718
LOK
125 m_options.c_cflag |= PARODD;
126
127#ifdef CRTSCTS
128 m_options.c_cflag &= ~CRTSCTS;
129#elif defined(CNEW_RTSCTS)
130 m_options.c_cflag &= ~CNEW_RTSCTS;
131#endif
132
99666519 133 if (m_iStopbits == SERIAL_STOP_BITS_ONE) m_options.c_cflag &= ~CSTOPB;
abbca718
LOK
134 else m_options.c_cflag |= CSTOPB;
135
136 //I guessed a little here
137 m_options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG | XCASE | ECHOK | ECHONL | ECHOCTL | ECHOPRT | ECHOKE | TOSTOP);
138
99666519 139 if (m_iParity == SERIAL_PARITY_NONE)
abbca718 140 m_options.c_iflag &= ~INPCK;
abbca718 141 else
abbca718 142 m_options.c_iflag |= INPCK | ISTRIP;
abbca718
LOK
143
144 m_options.c_iflag &= ~(IXON | IXOFF | IXANY | BRKINT | INLCR | IGNCR | ICRNL | IUCLC | IMAXBEL);
145 m_options.c_oflag &= ~(OPOST | ONLCR | OCRNL);
146
ba65909d 147 if (tcsetattr(m_socket, TCSANOW, &m_options) != 0)
abbca718 148 {
ba65909d 149 m_strError = strerror(errno);
abbca718
LOK
150 return false;
151 }
152
ba65909d 153 SocketSetBlocking(m_socket, true);
99666519 154 m_bIsOpen = true;
abbca718
LOK
155
156 return true;
157}
158
99666519 159bool CSerialSocket::SetBaudRate(uint32_t baudrate)
abbca718 160{
b9187cc6 161 int rate = IntToBaudrate(baudrate);
abbca718
LOK
162 if (rate == -1)
163 {
164 char buff[255];
165 sprintf(buff, "%i is not a valid baudrate", baudrate);
ba65909d 166 m_strError = buff;
abbca718
LOK
167 return false;
168 }
99666519 169
abbca718 170 //get the current port attributes
ba65909d 171 if (tcgetattr(m_socket, &m_options) != 0)
abbca718 172 {
ba65909d 173 m_strError = strerror(errno);
abbca718
LOK
174 return false;
175 }
176
177 if (cfsetispeed(&m_options, rate) != 0)
178 {
ba65909d 179 m_strError = strerror(errno);
abbca718
LOK
180 return false;
181 }
99666519 182
abbca718
LOK
183 if (cfsetospeed(&m_options, rate) != 0)
184 {
ba65909d 185 m_strError = strerror(errno);
abbca718
LOK
186 return false;
187 }
188
189 return true;
190}