zfm20: 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-11 13:10:30 -06:00 committed by Mihai Tudor Panu
parent 64777bd0ef
commit 3c36aa1624

View File

@ -23,6 +23,8 @@
*/ */
#include <iostream> #include <iostream>
#include <string>
#include <stdexcept>
#include "zfm20.h" #include "zfm20.h"
@ -37,7 +39,8 @@ ZFM20::ZFM20(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 @@ ZFM20::ZFM20(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;
} }
@ -108,7 +114,12 @@ int ZFM20::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;
} }
@ -125,7 +136,16 @@ int ZFM20::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;
}
if (rv == 0)
{
throw std::runtime_error(std::string(__FUNCTION__) +
": write() failed, no bytes written");
return rv; return rv;
} }
@ -155,7 +175,9 @@ bool ZFM20::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;
} }
@ -234,15 +256,16 @@ bool ZFM20::verifyPacket(uint8_t *pkt, int len)
// verify packet header // verify packet header
if (pkt[0] != ZFM20_START1 || pkt[1] != ZFM20_START2) if (pkt[0] != ZFM20_START1 || pkt[1] != ZFM20_START2)
{ {
cerr << __FUNCTION__ << ": Invalid packet header." << endl; throw std::runtime_error(std::string(__FUNCTION__) +
": Invalid packet header");
return false; return false;
} }
// check the ack byte // check the ack byte
if (pkt[6] != PKT_ACK) if (pkt[6] != PKT_ACK)
{ {
cerr << __FUNCTION__ << ": Invalid ACK code: " throw std::runtime_error(std::string(__FUNCTION__) +
<< int(pkt[6]) << ", expected: " << int(PKT_ACK) << endl; ": Invalid ACK code");
return false; return false;
} }
@ -268,16 +291,18 @@ bool ZFM20::getResponse(uint8_t *pkt, int len)
timer += getMillis(); timer += getMillis();
if (timer > ZFM20_TIMEOUT) if (timer > ZFM20_TIMEOUT)
{ {
cerr << __FUNCTION__ << ": Timed out waiting for packet" << endl; throw std::runtime_error(std::string(__FUNCTION__) +
": Timed out waiting for packet");
return false; return false;
} }
continue; continue;
} }
if ((rv = readData(buf, ZFM20_MAX_PKT_LEN)) <= 0) if ((rv = readData(buf, ZFM20_MAX_PKT_LEN)) == 0)
{ {
cerr << __FUNCTION__ << ": Read failed" << endl; throw std::runtime_error(std::string(__FUNCTION__) +
": readData() failed, no data returned");
return false; return false;
} }
@ -303,21 +328,14 @@ bool ZFM20::verifyPassword()
(m_password >> 8) & 0xff, (m_password >> 8) & 0xff,
m_password & 0xff }; m_password & 0xff };
if (!writeCmdPacket(pkt, pktLen)) writeCmdPacket(pkt, pktLen);
{
cerr << __FUNCTION__ << ": writePacket() failed" << endl;
return false;
}
// now read a response // now read a response
const int rPktLen = 12; const int rPktLen = 12;
uint8_t rPkt[rPktLen]; uint8_t rPkt[rPktLen];
if (!getResponse(rPkt, rPktLen)) getResponse(rPkt, rPktLen);
{
cerr << __FUNCTION__ << ": getResponse() failed" << endl;
return false;
}
return true; return true;
} }
@ -327,27 +345,19 @@ int ZFM20::getNumTemplates()
const int pktLen = 1; const int pktLen = 1;
uint8_t pkt[pktLen] = {CMD_GET_TMPL_COUNT}; uint8_t pkt[pktLen] = {CMD_GET_TMPL_COUNT};
if (!writeCmdPacket(pkt, pktLen)) writeCmdPacket(pkt, pktLen);
{
cerr << __FUNCTION__ << ": writePacket() failed" << endl;
return 0;
}
// now read a response // now read a response
const int rPktLen = 14; const int rPktLen = 14;
uint8_t rPkt[rPktLen]; uint8_t rPkt[rPktLen];
if (!getResponse(rPkt, rPktLen)) getResponse(rPkt, rPktLen);
{
cerr << __FUNCTION__ << ": getResponse() failed" << endl;
return 0;
}
// check confirmation code // check confirmation code
if (rPkt[9] != 0x00) if (rPkt[9] != 0x00)
{ {
cerr << __FUNCTION__ << ": Invalid confirmation code:" << int(rPkt[9]) throw std::runtime_error(std::string(__FUNCTION__) +
<< endl; ": Invalid confirmation code");
return 0; return 0;
} }
@ -363,27 +373,19 @@ bool ZFM20::setNewPassword(uint32_t pwd)
(pwd >> 8) & 0xff, (pwd >> 8) & 0xff,
pwd & 0xff }; pwd & 0xff };
if (!writeCmdPacket(pkt, pktLen)) writeCmdPacket(pkt, pktLen);
{
cerr << __FUNCTION__ << ": writePacket() failed" << endl;
return false;
}
// now read a response // now read a response
const int rPktLen = 12; const int rPktLen = 12;
uint8_t rPkt[rPktLen]; uint8_t rPkt[rPktLen];
if (!getResponse(rPkt, rPktLen)) getResponse(rPkt, rPktLen);
{
cerr << __FUNCTION__ << ": getResponse() failed" << endl;
return false;
}
// check confirmation code // check confirmation code
if (rPkt[9] != 0x00) if (rPkt[9] != 0x00)
{ {
cerr << __FUNCTION__ << ": Invalid confirmation code:" << int(rPkt[9]) throw std::runtime_error(std::string(__FUNCTION__) +
<< endl; ": Invalid confirmation code");
return false; return false;
} }
@ -401,27 +403,19 @@ bool ZFM20::setNewAddress(uint32_t addr)
(addr >> 8) & 0xff, (addr >> 8) & 0xff,
addr & 0xff }; addr & 0xff };
if (!writeCmdPacket(pkt, pktLen)) writeCmdPacket(pkt, pktLen);
{
cerr << __FUNCTION__ << ": writePacket() failed" << endl;
return false;
}
// now read a response // now read a response
const int rPktLen = 12; const int rPktLen = 12;
uint8_t rPkt[rPktLen]; uint8_t rPkt[rPktLen];
if (!getResponse(rPkt, rPktLen)) getResponse(rPkt, rPktLen);
{
cerr << __FUNCTION__ << ": getResponse() failed" << endl;
return false;
}
// check confirmation code // check confirmation code
if (rPkt[9] != 0x00) if (rPkt[9] != 0x00)
{ {
cerr << __FUNCTION__ << ": Invalid confirmation code:" << int(rPkt[9]) throw std::runtime_error(std::string(__FUNCTION__) +
<< endl; ": Invalid confirmation code");
return false; return false;
} }
@ -435,21 +429,13 @@ uint8_t ZFM20::generateImage()
const int pktLen = 1; const int pktLen = 1;
uint8_t pkt[pktLen] = {CMD_GEN_IMAGE}; uint8_t pkt[pktLen] = {CMD_GEN_IMAGE};
if (!writeCmdPacket(pkt, pktLen)) writeCmdPacket(pkt, pktLen);
{
cerr << __FUNCTION__ << ": writePacket() failed" << endl;
return ERR_INTERNAL_ERR;
}
// now read a response // now read a response
const int rPktLen = 12; const int rPktLen = 12;
uint8_t rPkt[rPktLen]; uint8_t rPkt[rPktLen];
if (!getResponse(rPkt, rPktLen)) getResponse(rPkt, rPktLen);
{
cerr << __FUNCTION__ << ": getResponse() failed" << endl;
return ERR_INTERNAL_ERR;
}
return rPkt[9]; return rPkt[9];
} }
@ -458,7 +444,8 @@ uint8_t ZFM20::image2Tz(int slot)
{ {
if (slot != 1 && slot != 2) if (slot != 1 && slot != 2)
{ {
cerr << __FUNCTION__ << ": slot must be 1 or 2" << endl; throw std::out_of_range(std::string(__FUNCTION__) +
": slot must be 1 or 2");
return ERR_INTERNAL_ERR; return ERR_INTERNAL_ERR;
} }
@ -466,21 +453,13 @@ uint8_t ZFM20::image2Tz(int slot)
uint8_t pkt[pktLen] = {CMD_IMG2TZ, uint8_t pkt[pktLen] = {CMD_IMG2TZ,
(slot & 0xff)}; (slot & 0xff)};
if (!writeCmdPacket(pkt, pktLen)) writeCmdPacket(pkt, pktLen);
{
cerr << __FUNCTION__ << ": writePacket() failed" << endl;
return ERR_INTERNAL_ERR;
}
// now read a response // now read a response
const int rPktLen = 12; const int rPktLen = 12;
uint8_t rPkt[rPktLen]; uint8_t rPkt[rPktLen];
if (!getResponse(rPkt, rPktLen)) getResponse(rPkt, rPktLen);
{
cerr << __FUNCTION__ << ": getResponse() failed" << endl;
return ERR_INTERNAL_ERR;
}
return rPkt[9]; return rPkt[9];
} }
@ -490,21 +469,13 @@ uint8_t ZFM20::createModel()
const int pktLen = 1; const int pktLen = 1;
uint8_t pkt[pktLen] = {CMD_REGMODEL}; uint8_t pkt[pktLen] = {CMD_REGMODEL};
if (!writeCmdPacket(pkt, pktLen)) writeCmdPacket(pkt, pktLen);
{
cerr << __FUNCTION__ << ": writePacket() failed" << endl;
return ERR_INTERNAL_ERR;
}
// now read a response // now read a response
const int rPktLen = 12; const int rPktLen = 12;
uint8_t rPkt[rPktLen]; uint8_t rPkt[rPktLen];
if (!getResponse(rPkt, rPktLen)) getResponse(rPkt, rPktLen);
{
cerr << __FUNCTION__ << ": getResponse() failed" << endl;
return ERR_INTERNAL_ERR;
}
return rPkt[9]; return rPkt[9];
} }
@ -513,7 +484,8 @@ uint8_t ZFM20::storeModel(int slot, uint16_t id)
{ {
if (slot != 1 && slot != 2) if (slot != 1 && slot != 2)
{ {
cerr << __FUNCTION__ << ": slot must be 1 or 2" << endl; throw std::out_of_range(std::string(__FUNCTION__) +
": slot must be 1 or 2");
return ERR_INTERNAL_ERR; return ERR_INTERNAL_ERR;
} }
@ -523,21 +495,13 @@ uint8_t ZFM20::storeModel(int slot, uint16_t id)
(id >> 8) & 0xff, (id >> 8) & 0xff,
id & 0xff}; id & 0xff};
if (!writeCmdPacket(pkt, pktLen)) writeCmdPacket(pkt, pktLen);
{
cerr << __FUNCTION__ << ": writePacket() failed" << endl;
return ERR_INTERNAL_ERR;
}
// now read a response // now read a response
const int rPktLen = 12; const int rPktLen = 12;
uint8_t rPkt[rPktLen]; uint8_t rPkt[rPktLen];
if (!getResponse(rPkt, rPktLen)) getResponse(rPkt, rPktLen);
{
cerr << __FUNCTION__ << ": getResponse() failed" << endl;
return ERR_INTERNAL_ERR;
}
return rPkt[9]; return rPkt[9];
} }
@ -551,21 +515,13 @@ uint8_t ZFM20::deleteModel(uint16_t id)
0x00, 0x00,
0x01}; 0x01};
if (!writeCmdPacket(pkt, pktLen)) writeCmdPacket(pkt, pktLen);
{
cerr << __FUNCTION__ << ": writePacket() failed" << endl;
return ERR_INTERNAL_ERR;
}
// now read a response // now read a response
const int rPktLen = 12; const int rPktLen = 12;
uint8_t rPkt[rPktLen]; uint8_t rPkt[rPktLen];
if (!getResponse(rPkt, rPktLen)) getResponse(rPkt, rPktLen);
{
cerr << __FUNCTION__ << ": getResponse() failed" << endl;
return ERR_INTERNAL_ERR;
}
return rPkt[9]; return rPkt[9];
} }
@ -575,21 +531,13 @@ uint8_t ZFM20::deleteDB()
const int pktLen = 1; const int pktLen = 1;
uint8_t pkt[pktLen] = {CMD_EMPTYDB}; uint8_t pkt[pktLen] = {CMD_EMPTYDB};
if (!writeCmdPacket(pkt, pktLen)) writeCmdPacket(pkt, pktLen);
{
cerr << __FUNCTION__ << ": writePacket() failed" << endl;
return ERR_INTERNAL_ERR;
}
// now read a response // now read a response
const int rPktLen = 12; const int rPktLen = 12;
uint8_t rPkt[rPktLen]; uint8_t rPkt[rPktLen];
if (!getResponse(rPkt, rPktLen)) getResponse(rPkt, rPktLen);
{
cerr << __FUNCTION__ << ": getResponse() failed" << endl;
return ERR_INTERNAL_ERR;
}
return rPkt[9]; return rPkt[9];
} }
@ -601,7 +549,8 @@ uint8_t ZFM20::search(int slot, uint16_t *id, uint16_t *score)
if (slot != 1 && slot != 2) if (slot != 1 && slot != 2)
{ {
cerr << __FUNCTION__ << ": slot must be 1 or 2" << endl; throw std::out_of_range(std::string(__FUNCTION__) +
": slot must be 1 or 2");
return ERR_INTERNAL_ERR; return ERR_INTERNAL_ERR;
} }
@ -614,21 +563,13 @@ uint8_t ZFM20::search(int slot, uint16_t *id, uint16_t *score)
0x00, 0x00,
0xa3}; 0xa3};
if (!writeCmdPacket(pkt, pktLen)) writeCmdPacket(pkt, pktLen);
{
cerr << __FUNCTION__ << ": writePacket() failed" << endl;
return ERR_INTERNAL_ERR;
}
// now read a response // now read a response
const int rPktLen = 16; const int rPktLen = 16;
uint8_t rPkt[rPktLen]; uint8_t rPkt[rPktLen];
if (!getResponse(rPkt, rPktLen)) getResponse(rPkt, rPktLen);
{
cerr << __FUNCTION__ << ": getResponse() failed" << endl;
return ERR_INTERNAL_ERR;
}
// if it was found, extract the location and the score // if it was found, extract the location and the score
if (rPkt[9] == ERR_OK) if (rPkt[9] == ERR_OK)
@ -647,21 +588,13 @@ uint8_t ZFM20::match(uint16_t *score)
const int pktLen = 1; const int pktLen = 1;
uint8_t pkt[pktLen] = {CMD_MATCH}; uint8_t pkt[pktLen] = {CMD_MATCH};
if (!writeCmdPacket(pkt, pktLen)) writeCmdPacket(pkt, pktLen);
{
cerr << __FUNCTION__ << ": writePacket() failed" << endl;
return ERR_INTERNAL_ERR;
}
// now read a response // now read a response
const int rPktLen = 14; const int rPktLen = 14;
uint8_t rPkt[rPktLen]; uint8_t rPkt[rPktLen];
if (!getResponse(rPkt, rPktLen)) getResponse(rPkt, rPktLen);
{
cerr << __FUNCTION__ << ": getResponse() failed" << endl;
return ERR_INTERNAL_ERR;
}
*score = (rPkt[10] << 8) & 0xff | rPkt[11] & 0xff; *score = (rPkt[10] << 8) & 0xff | rPkt[11] & 0xff;