2 * This file is part of the libCEC(R) library.
4 * libCEC(R) is Copyright (C) 2011 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/
35 #include "../serialport.h"
36 #include "../baudrate.h"
37 #include "../timeutils.h"
39 #if defined(__APPLE__)
53 CSerialPort::CSerialPort()
59 CSerialPort::~CSerialPort()
64 int8_t CSerialPort::Write(CCECAdapterMessage
*data
)
68 CLockObject
lock(&m_mutex
);
71 m_error
= "port closed";
75 int32_t byteswritten
= 0;
77 while (byteswritten
< (int32_t) data
->size())
81 int returnv
= select(m_fd
+ 1, NULL
, &port
, NULL
, NULL
);
84 m_error
= strerror(errno
);
88 returnv
= write(m_fd
, data
->packet
.data
+ byteswritten
, data
->size() - byteswritten
);
91 m_error
= strerror(errno
);
94 byteswritten
+= returnv
;
97 //print what's written to stdout for debugging
100 printf("%s write:", m_name
.c_str());
101 for (int i
= 0; i
< byteswritten
; i
++)
102 printf(" %02x", data
->at(i
));
110 int32_t CSerialPort::Read(uint8_t* data
, uint32_t len
, uint64_t iTimeoutMs
/*= 0*/)
113 struct timeval timeout
, *tv
;
114 int64_t now(0), target(0);
115 int32_t bytesread
= 0;
117 CLockObject
lock(&m_mutex
);
120 m_error
= "port closed";
127 target
= now
+ (int64_t) iTimeoutMs
;
130 while (bytesread
< (int32_t) len
&& (iTimeoutMs
== 0 || target
> now
))
138 timeout
.tv_sec
= ((long int)target
- (long int)now
) / (long int)1000.;
139 timeout
.tv_usec
= ((long int)target
- (long int)now
) % (long int)1000.;
145 int32_t returnv
= select(m_fd
+ 1, &port
, NULL
, NULL
, tv
);
149 m_error
= strerror(errno
);
152 else if (returnv
== 0)
154 break; //nothing to read
157 returnv
= read(m_fd
, data
+ bytesread
, len
- bytesread
);
160 m_error
= strerror(errno
);
164 bytesread
+= returnv
;
170 //print what's read to stdout for debugging
171 if (m_tostdout
&& bytesread
> 0)
173 printf("%s read:", m_name
.c_str());
174 for (int i
= 0; i
< bytesread
; i
++)
175 printf(" %02x", data
[i
]);
183 //setting all this stuff up is a pain in the ass
184 bool CSerialPort::Open(string name
, uint32_t baudrate
, uint8_t databits
/* = 8 */, uint8_t stopbits
/* = 1 */, uint8_t parity
/* = PAR_NONE */)
187 m_error
= strerror(errno
);
188 CLockObject
lock(&m_mutex
);
190 if (databits
< 5 || databits
> 8)
192 m_error
= "Databits has to be between 5 and 8";
196 if (stopbits
!= 1 && stopbits
!= 2)
198 m_error
= "Stopbits has to be 1 or 2";
202 if (parity
!= PAR_NONE
&& parity
!= PAR_EVEN
&& parity
!= PAR_ODD
)
204 m_error
= "Parity has to be none, even or odd";
208 m_fd
= open(name
.c_str(), O_RDWR
| O_NOCTTY
| O_NDELAY
);
212 m_error
= strerror(errno
);
216 fcntl(m_fd
, F_SETFL
, 0);
218 if (!SetBaudRate(baudrate
))
223 m_options
.c_cflag
|= (CLOCAL
| CREAD
);
224 m_options
.c_cflag
&= ~HUPCL
;
226 m_options
.c_cflag
&= ~CSIZE
;
227 if (databits
== 5) m_options
.c_cflag
|= CS5
;
228 if (databits
== 6) m_options
.c_cflag
|= CS6
;
229 if (databits
== 7) m_options
.c_cflag
|= CS7
;
230 if (databits
== 8) m_options
.c_cflag
|= CS8
;
232 m_options
.c_cflag
&= ~PARENB
;
233 if (parity
== PAR_EVEN
|| parity
== PAR_ODD
)
234 m_options
.c_cflag
|= PARENB
;
235 if (parity
== PAR_ODD
)
236 m_options
.c_cflag
|= PARODD
;
239 m_options
.c_cflag
&= ~CRTSCTS
;
240 #elif defined(CNEW_RTSCTS)
241 m_options
.c_cflag
&= ~CNEW_RTSCTS
;
244 if (stopbits
== 1) m_options
.c_cflag
&= ~CSTOPB
;
245 else m_options
.c_cflag
|= CSTOPB
;
247 //I guessed a little here
248 m_options
.c_lflag
&= ~(ICANON
| ECHO
| ECHOE
| ISIG
| XCASE
| ECHOK
| ECHONL
| ECHOCTL
| ECHOPRT
| ECHOKE
| TOSTOP
);
250 if (parity
== PAR_NONE
)
252 m_options
.c_iflag
&= ~INPCK
;
256 m_options
.c_iflag
|= INPCK
| ISTRIP
;
259 m_options
.c_iflag
&= ~(IXON
| IXOFF
| IXANY
| BRKINT
| INLCR
| IGNCR
| ICRNL
| IUCLC
| IMAXBEL
);
260 m_options
.c_oflag
&= ~(OPOST
| ONLCR
| OCRNL
);
262 if (tcsetattr(m_fd
, TCSANOW
, &m_options
) != 0)
264 m_error
= strerror(errno
);
269 fcntl(m_fd
, F_SETFL
, FNDELAY
);
274 void CSerialPort::Close()
276 CLockObject
lock(&m_mutex
);
286 bool CSerialPort::SetBaudRate(uint32_t baudrate
)
288 int rate
= IntToBaudrate(baudrate
);
292 sprintf(buff
, "%i is not a valid baudrate", baudrate
);
297 //get the current port attributes
298 if (tcgetattr(m_fd
, &m_options
) != 0)
300 m_error
= strerror(errno
);
304 if (cfsetispeed(&m_options
, rate
) != 0)
306 m_error
= strerror(errno
);
310 if (cfsetospeed(&m_options
, rate
) != 0)
312 m_error
= strerror(errno
);
319 bool CSerialPort::IsOpen()
321 CLockObject
lock(&m_mutex
);