/ src / libserver / lowlatency.cpp
lowlatency.cpp
 1  /*
 2      EIBD eib bus access and management daemon
 3      Copyright (C) 2005-2011 Martin Koegler <mkoegler@auto.tuwien.ac.at>
 4  
 5      This program is free software; you can redistribute it and/or modify
 6      it under the terms of the GNU General Public License as published by
 7      the Free Software Foundation; either version 2 of the License, or
 8      (at your option) any later version.
 9  
10      This program is distributed in the hope that it will be useful,
11      but WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13      GNU General Public License for more details.
14  
15      You should have received a copy of the GNU General Public License
16      along with this program; if not, write to the Free Software
17      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19  
20  #include "lowlatency.h"
21  
22  #include <cerrno>
23  #include <cstring> // memcpy
24  #include <sys/ioctl.h>
25  
26  bool
27  set_low_latency (int fd, low_latency_save * save, const bool really)
28  {
29    struct termios opts;
30  
31  #ifdef HAVE_LINUX_LOWLATENCY
32    if (really)
33      {
34        struct serial_struct snew;
35        ioctl (fd, TIOCGSERIAL, &save->ser);
36        memcpy(&snew, &save->ser, sizeof(snew));
37        snew.flags |= ASYNC_LOW_LATENCY;
38        // not all serial drivers support this call, so don't bail out on failure with ENOTTY
39        if(ioctl (fd, TIOCSSERIAL, &snew) < 0)
40          {
41            if (errno != ENOTTY && errno != EOPNOTSUPP)
42              return false;
43          }
44      }
45  #endif
46  
47    tcgetattr(fd, &save->term);
48    memcpy(&opts, &save->term, sizeof(opts));
49    opts.c_cc[VTIME] = 1;
50    opts.c_cc[VMIN] = 1;
51    if (tcsetattr(fd, TCSANOW, &opts) < 0)
52      return false;
53  
54    return true;
55  }
56  
57  void
58  restore_low_latency (int fd, low_latency_save * save, const bool really)
59  {
60  #ifdef HAVE_LINUX_LOWLATENCY
61    if (really)
62      ioctl (fd, TIOCSSERIAL, &save->ser);
63  #endif
64    ioctl (fd, TCSANOW, &save->term);
65  }
66