diff --git a/src/hm11/hm11.cxx b/src/hm11/hm11.cxx index 81e33be1..0c6cebcf 100644 --- a/src/hm11/hm11.cxx +++ b/src/hm11/hm11.cxx @@ -23,6 +23,8 @@ */ #include +#include +#include #include "hm11.h" @@ -37,7 +39,8 @@ HM11::HM11(int uart) if ( !(m_uart = mraa_uart_init(uart)) ) { - cerr << __FUNCTION__ << ": mraa_uart_init() failed" << endl; + throw std::invalid_argument(std::string(__FUNCTION__) + + ": mraa_uart_init() failed"); return; } @@ -46,15 +49,18 @@ HM11::HM11(int uart) if (!devPath) { - cerr << __FUNCTION__ << ": mraa_uart_get_dev_path() failed" << endl; + throw std::runtime_error(std::string(__FUNCTION__) + + ": mraa_uart_get_dev_path() failed"); return; } // now open the tty if ( (m_ttyFd = open(devPath, O_RDWR)) == -1) { - cerr << __FUNCTION__ << ": open of " << devPath << " failed: " - << strerror(errno) << endl; + throw std::runtime_error(std::string(__FUNCTION__) + + ": open of " + + string(devPath) + " failed:" + + string(strerror(errno))); return; } } @@ -97,7 +103,12 @@ int HM11::readData(char *buffer, int len) int rv = read(m_ttyFd, buffer, len); if (rv < 0) - cerr << __FUNCTION__ << ": read failed: " << strerror(errno) << endl; + { + throw std::runtime_error(std::string(__FUNCTION__) + + ": read() failed: " + + string(strerror(errno))); + return rv; + } return rv; } @@ -115,7 +126,9 @@ int HM11::writeData(char *buffer, int len) if (rv < 0) { - cerr << __FUNCTION__ << ": write failed: " << strerror(errno) << endl; + throw std::runtime_error(std::string(__FUNCTION__) + + ": write() failed: " + + string(strerror(errno))); return rv; } @@ -145,7 +158,9 @@ bool HM11::setupTty(speed_t baud) // make it so if (tcsetattr(m_ttyFd, TCSAFLUSH, &termio) < 0) { - cerr << __FUNCTION__ << ": tcsetattr failed: " << strerror(errno) << endl; + throw std::runtime_error(std::string(__FUNCTION__) + + ": tcsetattr() failed: " + + string(strerror(errno))); return false; }