42 using namespace AlarmNotifications;
47 return global_instance;
69 catch ( std::exception& e )
84 catch ( std::exception& e )
109 command.push_back (
'\xff' );
111 command.push_back (
'\x01' );
114 command.push_back (
'\x01' );
116 command.push_back (
'\x00' );
122 _fd = open (
_deviceNode.c_str(), O_RDWR | O_NOCTTY | O_NONBLOCK );
124 throw std::runtime_error (
"Cannot open serial interface for flashlight." );
131 throw std::logic_error (
"configureSerialInterface() called on closed interface." );
132 termios serialConfig;
133 const int getattr_result = tcgetattr (
_fd, &serialConfig );
134 if ( getattr_result < 0 )
135 throw std::runtime_error (
"Error while reading configuration of serial interface." );
138 serialConfig.c_cc[VMIN] = 0;
139 serialConfig.c_cc[VTIME] = 0;
142 serialConfig.c_cflag |= CREAD | CLOCAL;
145 serialConfig.c_iflag &= ~IXON & ~IXOFF;
148 serialConfig.c_cflag &= ~CSIZE;
149 serialConfig.c_cflag |= CS8;
152 serialConfig.c_cflag &= ~PARENB & ~PARODD;
153 serialConfig.c_iflag |= IGNPAR;
156 serialConfig.c_cflag &= ~CSTOPB;
159 serialConfig.c_cflag &= ~CRTSCTS;
162 const int ispeed_result = cfsetispeed ( &serialConfig,
deviceBaudRate );
163 if ( ispeed_result < 0 )
164 throw std::runtime_error (
"Cannot set input baud rate." );
165 const int ospeed_result = cfsetospeed ( &serialConfig,
deviceBaudRate );
166 if ( ospeed_result < 0 )
167 throw std::runtime_error (
"Cannot set input baud rate." );
170 const int setattr_result = tcsetattr (
_fd, TCSANOW, &serialConfig );
171 if ( setattr_result < 0 )
172 throw std::runtime_error (
"Error while writing configuration of serial interface." );
178 throw std::logic_error (
"writeSerialInterface() called on closed interface." );
179 const long unsigned int numBytes = command.size();
180 uint8_t* buffer =
nullptr;
184 buffer =
new uint8_t[numBytes+1];
185 if ( buffer ==
nullptr )
186 throw std::bad_alloc();
188 memset ( buffer, 0, numBytes+1 );
190 std::copy ( command.begin(), command.end(), buffer );
192 long int bytesWritten = -1;
195 bytesWritten = write (
_fd, buffer, numBytes );
197 while ( ( bytesWritten < 0 && EAGAIN == errno ) );
199 if ( bytesWritten < 0 )
200 throw std::runtime_error (
"An error occured while writing to the serial interface" );
201 if ( (
long unsigned int ) ( bytesWritten ) < numBytes )
202 throw std::runtime_error (
"Could not write all bytes to serial interface" );