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