grovescam: 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:26:07 -06:00 committed by Mihai Tudor Panu
parent 5ab01e5a65
commit f567f282ae

View File

@ -25,6 +25,8 @@
*/ */
#include <iostream> #include <iostream>
#include <string>
#include <stdexcept>
#include <errno.h> #include <errno.h>
#include "grovescam.h" #include "grovescam.h"
@ -45,7 +47,8 @@ GROVESCAM::GROVESCAM(int uart, uint8_t camAddr)
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;
} }
@ -54,15 +57,18 @@ GROVESCAM::GROVESCAM(int uart, uint8_t camAddr)
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;
} }
} }
@ -131,7 +137,9 @@ int GROVESCAM::writeData(uint8_t *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;
} }
@ -161,7 +169,9 @@ bool GROVESCAM::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;
} }
@ -187,7 +197,8 @@ bool GROVESCAM::init()
{ {
if (retries++ > maxRetries) if (retries++ > maxRetries)
{ {
cerr << __FUNCTION__ << ": retries exceeded" << endl; throw std::runtime_error(std::string(__FUNCTION__) +
": maximum retries exceeded");
return false; return false;
} }
@ -238,7 +249,8 @@ bool GROVESCAM::preCapture(PIC_FORMATS_T fmt)
{ {
if (retries++ > maxRetries) if (retries++ > maxRetries)
{ {
cerr << __FUNCTION__ << ": retries exceeded" << endl; throw std::runtime_error(std::string(__FUNCTION__) +
": maximum retries exceeded");
return false; return false;
} }
@ -276,7 +288,8 @@ bool GROVESCAM::doCapture()
{ {
if (retries++ > maxRetries) if (retries++ > maxRetries)
{ {
cerr << __FUNCTION__ << ": retries exceeded" << endl; throw std::runtime_error(std::string(__FUNCTION__) +
": maximum retries exceeded");
return false; return false;
} }
@ -309,7 +322,8 @@ bool GROVESCAM::doCapture()
{ {
if (retries++ > maxRetries) if (retries++ > maxRetries)
{ {
cerr << __FUNCTION__ << ": retries exceeded" << endl; throw std::runtime_error(std::string(__FUNCTION__) +
": maximum retries exceeded");
return false; return false;
} }
@ -334,7 +348,8 @@ bool GROVESCAM::doCapture()
{ {
if (retries++ > maxRetries) if (retries++ > maxRetries)
{ {
cerr << __FUNCTION__ << ": retries exceeded" << endl; throw std::runtime_error(std::string(__FUNCTION__) +
": maximum retries exceeded");
return false; return false;
} }
@ -373,15 +388,15 @@ bool GROVESCAM::storeImage(const char *fname)
{ {
if (!fname) if (!fname)
{ {
cerr << __FUNCTION__ << "@" << __LINE__ throw std::invalid_argument(std::string(__FUNCTION__) +
<< ": fname is NULL" << endl; ": filename is NULL");
return false; return false;
} }
if (!m_picTotalLen) if (!m_picTotalLen)
{ {
cerr << __FUNCTION__ << "@" << __LINE__ throw std::runtime_error(std::string(__FUNCTION__) +
<< ": Picture length is zero, you need to capture first." << endl; ": Picture length is zero, you need to capture first.");
return false; return false;
} }
@ -390,9 +405,9 @@ bool GROVESCAM::storeImage(const char *fname)
if (!file) if (!file)
{ {
cerr << __FUNCTION__ << "@" << __LINE__ throw std::runtime_error(std::string(__FUNCTION__) +
<< ": fopen failed: " << strerror(errno) << endl; ": fopen() failed: " +
string(strerror(errno)));
return false; return false;
} }
@ -405,7 +420,6 @@ bool GROVESCAM::storeImage(const char *fname)
uint8_t cmd[pktLen] = { 0xaa, 0x0e | m_camAddr, 0x00, 0x00, 0x00, 0x00 }; uint8_t cmd[pktLen] = { 0xaa, 0x0e | m_camAddr, 0x00, 0x00, 0x00, 0x00 };
uint8_t pkt[MAX_PKT_LEN]; uint8_t pkt[MAX_PKT_LEN];
int retries = 0; int retries = 0;
bool failed = false;
for (unsigned int i = 0; i < pktCnt; i++) for (unsigned int i = 0; i < pktCnt; i++)
{ {
@ -425,10 +439,9 @@ bool GROVESCAM::storeImage(const char *fname)
{ {
if (retries++ > maxRetries) if (retries++ > maxRetries)
{ {
cerr << __FUNCTION__ << ": timeout, max retries exhausted." throw std::runtime_error(std::string(__FUNCTION__) +
<< endl; ": timeout, maximum retries exceeded");
failed = true; return false;
break;
} }
goto retry; goto retry;
} }
@ -446,10 +459,9 @@ bool GROVESCAM::storeImage(const char *fname)
goto retry; goto retry;
else else
{ {
cerr << __FUNCTION__ << ": cksum error, max retries exhausted." throw std::runtime_error(std::string(__FUNCTION__) +
<< endl; ": cksum error, maximum retries exceeded");
failed = true; return false;
break;
} }
} }
@ -465,11 +477,5 @@ bool GROVESCAM::storeImage(const char *fname)
// reset the pic length to 0 for another run. // reset the pic length to 0 for another run.
m_picTotalLen = 0; m_picTotalLen = 0;
if (failed)
{
cerr << "Failed to download and store image." << endl;
return false;
}
return true; return true;
} }