2 * This file is part of the libCEC(R) library.
4 * libCEC(R) is Copyright (C) 2011-2012 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 "../sockets/serialport.h"
37 #include "../util/baudrate.h"
38 #include "../posix/os-socket.h"
40 #if defined(__APPLE__)
52 using namespace PLATFORM
;
54 void CSerialSocket::Close(void)
56 SocketClose(m_socket
);
59 void CSerialSocket::Shutdown(void)
61 SocketClose(m_socket
);
64 ssize_t
CSerialSocket::Write(void* data
, size_t len
)
66 return SocketWrite(m_socket
, &m_iError
, data
, len
);
69 ssize_t
CSerialSocket::Read(void* data
, size_t len
, uint64_t iTimeoutMs
/* = 0 */)
71 return SocketRead(m_socket
, &m_iError
, data
, len
, iTimeoutMs
);
74 //setting all this stuff up is a pain in the ass
75 bool CSerialSocket::Open(uint64_t iTimeoutMs
/* = 0 */)
78 CLockObject
lock(m_mutex
);
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
)
83 m_strError
= "Databits has to be between 5 and 8";
87 if (m_iStopbits
!= SERIAL_STOP_BITS_ONE
&& m_iStopbits
!= SERIAL_STOP_BITS_TWO
)
89 m_strError
= "Stopbits has to be 1 or 2";
93 if (m_iParity
!= SERIAL_PARITY_NONE
&& m_iParity
!= SERIAL_PARITY_EVEN
&& m_iParity
!= SERIAL_PARITY_ODD
)
95 m_strError
= "Parity has to be none, even or odd";
99 m_socket
= open(m_strName
.c_str(), O_RDWR
| O_NOCTTY
| O_NDELAY
);
101 if (m_socket
== INVALID_SERIAL_SOCKET_VALUE
)
103 m_strError
= strerror(errno
);
107 SocketSetBlocking(m_socket
, false);
109 if (!SetBaudRate(m_iBaudrate
))
112 m_options
.c_cflag
|= (CLOCAL
| CREAD
);
113 m_options
.c_cflag
&= ~HUPCL
;
115 m_options
.c_cflag
&= ~CSIZE
;
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
;
121 m_options
.c_cflag
&= ~PARENB
;
122 if (m_iParity
== SERIAL_PARITY_EVEN
|| m_iParity
== SERIAL_PARITY_ODD
)
123 m_options
.c_cflag
|= PARENB
;
124 if (m_iParity
== SERIAL_PARITY_ODD
)
125 m_options
.c_cflag
|= PARODD
;
128 m_options
.c_cflag
&= ~CRTSCTS
;
129 #elif defined(CNEW_RTSCTS)
130 m_options
.c_cflag
&= ~CNEW_RTSCTS
;
133 if (m_iStopbits
== SERIAL_STOP_BITS_ONE
) m_options
.c_cflag
&= ~CSTOPB
;
134 else m_options
.c_cflag
|= CSTOPB
;
136 //I guessed a little here
137 m_options
.c_lflag
&= ~(ICANON
| ECHO
| ECHOE
| ISIG
| XCASE
| ECHOK
| ECHONL
| ECHOCTL
| ECHOPRT
| ECHOKE
| TOSTOP
);
139 if (m_iParity
== SERIAL_PARITY_NONE
)
140 m_options
.c_iflag
&= ~INPCK
;
142 m_options
.c_iflag
|= INPCK
| ISTRIP
;
144 m_options
.c_iflag
&= ~(IXON
| IXOFF
| IXANY
| BRKINT
| INLCR
| IGNCR
| ICRNL
| IUCLC
| IMAXBEL
);
145 m_options
.c_oflag
&= ~(OPOST
| ONLCR
| OCRNL
);
147 if (tcsetattr(m_socket
, TCSANOW
, &m_options
) != 0)
149 m_strError
= strerror(errno
);
153 SocketSetBlocking(m_socket
, true);
159 bool CSerialSocket::SetBaudRate(uint32_t baudrate
)
161 int rate
= IntToBaudrate(baudrate
);
165 sprintf(buff
, "%i is not a valid baudrate", baudrate
);
170 //get the current port attributes
171 if (tcgetattr(m_socket
, &m_options
) != 0)
173 m_strError
= strerror(errno
);
177 if (cfsetispeed(&m_options
, rate
) != 0)
179 m_strError
= strerror(errno
);
183 if (cfsetospeed(&m_options
, rate
) != 0)
185 m_strError
= strerror(errno
);