2 * This file is part of the libCEC(R) library.
4 * libCEC(R) is Copyright (C) 2011-2013 Pulse-Eight Limited. All rights reserved.
5 * libCEC(R) is an original work, containing original code.
7 * libCEC(R) is a trademark of Pulse-Eight Limited.
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.
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.
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.
24 * Alternatively, you can license this library under a commercial license,
25 * please contact Pulse-Eight Licensing for more information.
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/
36 #include "lib/platform/sockets/serialport.h"
37 #include "lib/platform/util/baudrate.h"
38 #include "lib/platform/posix/os-socket.h"
40 #if defined(__APPLE__) || defined(__FreeBSD__)
57 using namespace PLATFORM
;
59 inline bool RemoveLock(const char *strDeviceName
)
61 #if !defined(__APPLE__) && !defined(__FreeBSD__) && defined(HAVE_LOCKDEV)
62 return dev_unlock(strDeviceName
, 0) == 0;
64 (void)strDeviceName
; // silence unused warning
69 void CSerialSocket::Close(void)
73 SocketClose(m_socket
);
74 RemoveLock(m_strName
.c_str());
78 void CSerialSocket::Shutdown(void)
82 SocketClose(m_socket
);
83 RemoveLock(m_strName
.c_str());
87 ssize_t
CSerialSocket::Write(void* data
, size_t len
)
89 return IsOpen() ? SocketWrite(m_socket
, &m_iError
, data
, len
) : -1;
92 ssize_t
CSerialSocket::Read(void* data
, size_t len
, uint64_t iTimeoutMs
/* = 0 */)
94 return IsOpen() ? SocketRead(m_socket
, &m_iError
, data
, len
, iTimeoutMs
) : -1;
97 //setting all this stuff up is a pain in the ass
98 bool CSerialSocket::Open(uint64_t iTimeoutMs
/* = 0 */)
100 iTimeoutMs
= 0; if (!iTimeoutMs
){} // silence unused warning
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
)
110 m_strError
= "Databits has to be between 5 and 8";
115 if (m_iStopbits
!= SERIAL_STOP_BITS_ONE
&& m_iStopbits
!= SERIAL_STOP_BITS_TWO
)
117 m_strError
= "Stopbits has to be 1 or 2";
122 if (m_iParity
!= SERIAL_PARITY_NONE
&& m_iParity
!= SERIAL_PARITY_EVEN
&& m_iParity
!= SERIAL_PARITY_ODD
)
124 m_strError
= "Parity has to be none, even or odd";
129 #if !defined(__APPLE__) && !defined(__FreeBSD__) && defined(HAVE_LOCKDEV)
130 if (dev_lock(m_strName
.c_str()) != 0)
132 m_strError
= "Couldn't lock the serial port";
138 m_socket
= open(m_strName
.c_str(), O_RDWR
| O_NOCTTY
| O_NDELAY
);
140 if (m_socket
== INVALID_SERIAL_SOCKET_VALUE
)
142 m_strError
= strerror(errno
);
143 RemoveLock(m_strName
.c_str());
147 SocketSetBlocking(m_socket
, false);
149 if (!SetBaudRate(m_iBaudrate
))
152 m_options
.c_cflag
|= (CLOCAL
| CREAD
);
153 m_options
.c_cflag
&= ~HUPCL
;
155 m_options
.c_cflag
&= ~CSIZE
;
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
;
161 m_options
.c_cflag
&= ~PARENB
;
162 if (m_iParity
== SERIAL_PARITY_EVEN
|| m_iParity
== SERIAL_PARITY_ODD
)
163 m_options
.c_cflag
|= PARENB
;
164 if (m_iParity
== SERIAL_PARITY_ODD
)
165 m_options
.c_cflag
|= PARODD
;
168 m_options
.c_cflag
&= ~CRTSCTS
;
169 #elif defined(CNEW_RTSCTS)
170 m_options
.c_cflag
&= ~CNEW_RTSCTS
;
173 if (m_iStopbits
== SERIAL_STOP_BITS_ONE
) m_options
.c_cflag
&= ~CSTOPB
;
174 else m_options
.c_cflag
|= CSTOPB
;
176 //I guessed a little here
177 m_options
.c_lflag
&= ~(ICANON
| ECHO
| ECHOE
| ISIG
| XCASE
| ECHOK
| ECHONL
| ECHOCTL
| ECHOPRT
| ECHOKE
| TOSTOP
);
179 if (m_iParity
== SERIAL_PARITY_NONE
)
180 m_options
.c_iflag
&= ~INPCK
;
182 m_options
.c_iflag
|= INPCK
| ISTRIP
;
184 m_options
.c_iflag
&= ~(IXON
| IXOFF
| IXANY
| BRKINT
| INLCR
| IGNCR
| ICRNL
| IUCLC
| IMAXBEL
);
185 m_options
.c_oflag
&= ~(OPOST
| ONLCR
| OCRNL
);
187 if (tcsetattr(m_socket
, TCSANOW
, &m_options
) != 0)
189 m_strError
= strerror(errno
);
190 RemoveLock(m_strName
.c_str());
194 SocketSetBlocking(m_socket
, true);
200 bool CSerialSocket::SetBaudRate(uint32_t baudrate
)
202 int rate
= IntToBaudrate(baudrate
);
206 sprintf(buff
, "%i is not a valid baudrate", baudrate
);
211 //get the current port attributes
212 if (tcgetattr(m_socket
, &m_options
) != 0)
214 m_strError
= strerror(errno
);
218 if (cfsetispeed(&m_options
, rate
) != 0)
220 m_strError
= strerror(errno
);
224 if (cfsetospeed(&m_options
, rate
) != 0)
226 m_strError
= strerror(errno
);