hm11: throw exception(s) on fatal errors

Signed-off-by: Jon Trulson <jtrulson@ics.com>
Signed-off-by: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
This commit is contained in:
Jon Trulson 2015-09-10 11:50:53 -06:00 committed by Mihai Tudor Panu
parent 56ddb441d4
commit 3cd61b56f8

View File

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