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" );
void configureSerialInterface()
Set options of serial interface for USB relais.
void switchInternal(const bool lightSwitch)
Internal routine to switch relais on or off.
static void switchOff() noexcept
Switch off red alarm flash light.
Control USB relais for red alarm flash light.
boost::mutex _serialLineMutex
Mutex to protect device access.
const std::string _deviceNode
Path to the device file.
static const unsigned int deviceBaudRate
Constant for baud rate.
static void switchOn() noexcept
Switch on red alarm flash light.
void closeSerialInterface()
Close the device handle.
std::vector< uint8_t > deviceCommand
Type for storing commands to the device.
Namespace for Alarm Notifications application.
void writeSerialInteface(const deviceCommand command)
Write command sequence to the USB relais.
bool _fdOpen
File descriptor use flag.
#define noexcept
Allow using the noexcept keyword with GCC < 4.6.
void openSerialInterface()
Open serial interface.
static deviceCommand createCommand(const bool lightSwitch)
Assemble command sequence.
void ExceptionHandler(std::exception &e, std::string location, const bool quit=false) noexcept
Generic exception handler for known exceptions.
static FlashLight & instance() noexcept
Get singleton instance.
Generic functions for exception handling.
Singleton to read and change the configuration of this application.
Controller for USB relais of red alarm flash light.
Configuration of the AlarmNotifications application.