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 struct timeval timeout
, *tv
;
78 if (data
->transmit_timeout
<= 0)
84 timeout
.tv_sec
= (long int)data
->transmit_timeout
/ (long int)1000.;
85 timeout
.tv_usec
= (long int)data
->transmit_timeout
% (long int)1000.;
89 while (byteswritten
< (int32_t) data
->size())
93 int returnv
= select(m_fd
+ 1, NULL
, &port
, NULL
, tv
);
96 m_error
= strerror(errno
);
99 else if (returnv
== 0)
105 returnv
= write(m_fd
, data
->packet
.data
+ byteswritten
, data
->size() - byteswritten
);
108 m_error
= strerror(errno
);
111 byteswritten
+= returnv
;
114 //print what's written to stdout for debugging
117 printf("%s write:", m_name
.c_str());
118 for (int i
= 0; i
< byteswritten
; i
++)
119 printf(" %02x", data
->at(i
));
127 int32_t CSerialPort::Read(uint8_t* data
, uint32_t len
, uint64_t iTimeoutMs
/*= 0*/)
130 struct timeval timeout
, *tv
;
131 int64_t now(0), target(0);
132 int32_t bytesread
= 0;
134 CLockObject
lock(&m_mutex
);
137 m_error
= "port closed";
144 target
= now
+ (int64_t) iTimeoutMs
;
147 while (bytesread
< (int32_t) len
&& (iTimeoutMs
== 0 || target
> now
))
155 timeout
.tv_sec
= ((long int)target
- (long int)now
) / (long int)1000.;
156 timeout
.tv_usec
= ((long int)target
- (long int)now
) % (long int)1000.;
162 int32_t returnv
= select(m_fd
+ 1, &port
, NULL
, NULL
, tv
);
166 m_error
= strerror(errno
);
169 else if (returnv
== 0)
171 break; //nothing to read
174 returnv
= read(m_fd
, data
+ bytesread
, len
- bytesread
);
177 m_error
= strerror(errno
);
181 bytesread
+= returnv
;
187 //print what's read to stdout for debugging
188 if (m_tostdout
&& bytesread
> 0)
190 printf("%s read:", m_name
.c_str());
191 for (int i
= 0; i
< bytesread
; i
++)
192 printf(" %02x", data
[i
]);
200 //setting all this stuff up is a pain in the ass
201 bool CSerialPort::Open(string name
, uint32_t baudrate
, uint8_t databits
/* = 8 */, uint8_t stopbits
/* = 1 */, uint8_t parity
/* = PAR_NONE */)
204 m_error
= strerror(errno
);
205 CLockObject
lock(&m_mutex
);
207 if (databits
< 5 || databits
> 8)
209 m_error
= "Databits has to be between 5 and 8";
213 if (stopbits
!= 1 && stopbits
!= 2)
215 m_error
= "Stopbits has to be 1 or 2";
219 if (parity
!= PAR_NONE
&& parity
!= PAR_EVEN
&& parity
!= PAR_ODD
)
221 m_error
= "Parity has to be none, even or odd";
225 m_fd
= open(name
.c_str(), O_RDWR
| O_NOCTTY
| O_NDELAY
);
229 m_error
= strerror(errno
);
233 fcntl(m_fd
, F_SETFL
, 0);
235 if (!SetBaudRate(baudrate
))
240 m_options
.c_cflag
|= (CLOCAL
| CREAD
);
241 m_options
.c_cflag
&= ~HUPCL
;
243 m_options
.c_cflag
&= ~CSIZE
;
244 if (databits
== 5) m_options
.c_cflag
|= CS5
;
245 if (databits
== 6) m_options
.c_cflag
|= CS6
;
246 if (databits
== 7) m_options
.c_cflag
|= CS7
;
247 if (databits
== 8) m_options
.c_cflag
|= CS8
;
249 m_options
.c_cflag
&= ~PARENB
;
250 if (parity
== PAR_EVEN
|| parity
== PAR_ODD
)
251 m_options
.c_cflag
|= PARENB
;
252 if (parity
== PAR_ODD
)
253 m_options
.c_cflag
|= PARODD
;
256 m_options
.c_cflag
&= ~CRTSCTS
;
257 #elif defined(CNEW_RTSCTS)
258 m_options
.c_cflag
&= ~CNEW_RTSCTS
;
261 if (stopbits
== 1) m_options
.c_cflag
&= ~CSTOPB
;
262 else m_options
.c_cflag
|= CSTOPB
;
264 //I guessed a little here
265 m_options
.c_lflag
&= ~(ICANON
| ECHO
| ECHOE
| ISIG
| XCASE
| ECHOK
| ECHONL
| ECHOCTL
| ECHOPRT
| ECHOKE
| TOSTOP
);
267 if (parity
== PAR_NONE
)
269 m_options
.c_iflag
&= ~INPCK
;
273 m_options
.c_iflag
|= INPCK
| ISTRIP
;
276 m_options
.c_iflag
&= ~(IXON
| IXOFF
| IXANY
| BRKINT
| INLCR
| IGNCR
| ICRNL
| IUCLC
| IMAXBEL
);
277 m_options
.c_oflag
&= ~(OPOST
| ONLCR
| OCRNL
);
279 if (tcsetattr(m_fd
, TCSANOW
, &m_options
) != 0)
281 m_error
= strerror(errno
);
286 fcntl(m_fd
, F_SETFL
, FNDELAY
);
291 void CSerialPort::Close()
293 CLockObject
lock(&m_mutex
);
303 bool CSerialPort::SetBaudRate(uint32_t baudrate
)
305 int rate
= IntToBaudrate(baudrate
);
309 sprintf(buff
, "%i is not a valid baudrate", baudrate
);
314 //get the current port attributes
315 if (tcgetattr(m_fd
, &m_options
) != 0)
317 m_error
= strerror(errno
);
321 if (cfsetispeed(&m_options
, rate
) != 0)
323 m_error
= strerror(errno
);
327 if (cfsetospeed(&m_options
, rate
) != 0)
329 m_error
= strerror(errno
);
336 bool CSerialPort::IsOpen()
338 CLockObject
lock(&m_mutex
);