From: Lars Op den Kamp Date: Sun, 4 Dec 2011 09:11:13 +0000 (+0100) Subject: posix: set the timeout when writing to the serial port X-Git-Tag: upstream/2.2.0~1^2~44^2~48 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=2c56dd5189b47478350c84917c420670598ca06b;hp=5606e90bb3c5a85ab8e29e639e8248581247699e;p=deb_libcec.git posix: set the timeout when writing to the serial port --- diff --git a/src/lib/platform/linux/serialport.cpp b/src/lib/platform/linux/serialport.cpp index 2e4560d..f040857 100644 --- a/src/lib/platform/linux/serialport.cpp +++ b/src/lib/platform/linux/serialport.cpp @@ -74,16 +74,33 @@ int8_t CSerialPort::Write(CCECAdapterMessage *data) int32_t byteswritten = 0; + struct timeval timeout, *tv; + if (data->transmit_timeout <= 0) + { + tv = NULL; + } + else + { + timeout.tv_sec = (long int)data->transmit_timeout / (long int)1000.; + timeout.tv_usec = (long int)data->transmit_timeout % (long int)1000.; + tv = &timeout; + } + while (byteswritten < (int32_t) data->size()) { FD_ZERO(&port); FD_SET(m_fd, &port); - int returnv = select(m_fd + 1, NULL, &port, NULL, NULL); - if (returnv == -1) + int returnv = select(m_fd + 1, NULL, &port, NULL, tv); + if (returnv < 0) { m_error = strerror(errno); return -1; } + else if (returnv == 0) + { + m_error = "timeout"; + return -1; + } returnv = write(m_fd, data->packet.data + byteswritten, data->size() - byteswritten); if (returnv == -1)