posix: set the timeout when writing to the serial port
[deb_libcec.git] / src / lib / platform / linux / serialport.cpp
index 2e4560d56c683766bbdf876f6c360b51a2e180d8..f0408574dad1670238a571ab0dcadfb8acedcff3 100644 (file)
@@ -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)