From 6db7dfd3d9a4b4f20eef51bafbe7a7c53d8ffa3d Mon Sep 17 00:00:00 2001 From: Jon Trulson Date: Thu, 10 Sep 2015 13:57:15 -0600 Subject: [PATCH] m24lr64e: throw exception(s) on fatal errors Signed-off-by: Jon Trulson Signed-off-by: Mihai Tudor Panu --- src/m24lr64e/m24lr64e.cxx | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/src/m24lr64e/m24lr64e.cxx b/src/m24lr64e/m24lr64e.cxx index d684aaaf..633d5e74 100644 --- a/src/m24lr64e/m24lr64e.cxx +++ b/src/m24lr64e/m24lr64e.cxx @@ -44,8 +44,8 @@ M24LR64E::M24LR64E(int bus, AccessMode mode): mraa::Result rv; if ( (rv = m_i2c.address(m_addr)) != mraa::SUCCESS) { - cerr << "M24LR64E: Could not initialize i2c address. " << endl; - printError(rv); + throw std::runtime_error(std::string(__FUNCTION__) + + ": I2c.address() failed"); return; } } @@ -78,7 +78,8 @@ bool M24LR64E::submitPasswd(uint32_t passwd) if (m_i2c.write(buf, pktLen)) { - cerr << __FUNCTION__ << "@" << __LINE__ << ": write failed" << endl; + throw std::runtime_error(std::string(__FUNCTION__) + + ": I2c.write() failed"); return false; } @@ -108,7 +109,8 @@ bool M24LR64E::writePasswd(uint32_t passwd) if (m_i2c.write(buf, pktLen)) { - cerr << __FUNCTION__ << "@" << __LINE__ << ": write failed" << endl; + throw std::runtime_error(std::string(__FUNCTION__) + + ": I2c.write() failed"); return false; } @@ -218,7 +220,8 @@ mraa::Result M24LR64E::EEPROM_Write_Byte(unsigned int address, uint8_t data) buf[2] = data; if ((rv = m_i2c.write(buf, pktLen))) - cerr << __FUNCTION__ << "@" << __LINE__ << ": write failed" << endl; + throw std::runtime_error(std::string(__FUNCTION__) + + ": I2c.write() failed"); usleep(I2C_WRITE_TIME * 1000); return rv; @@ -238,7 +241,8 @@ mraa::Result M24LR64E::EEPROM_Write_Bytes(unsigned int address, uint8_t* data, buf[2+i] = data[i]; if ((rv = m_i2c.write(buf, pktLen))) - cerr << __FUNCTION__ << "@" << __LINE__ << ": write failed" << endl; + throw std::runtime_error(std::string(__FUNCTION__) + + ": I2c.write() failed"); usleep(I2C_WRITE_TIME * 1000); @@ -255,7 +259,8 @@ uint8_t M24LR64E::EEPROM_Read_Byte(unsigned int address) if (m_i2c.write(abuf, apktLen)) { - cerr << __FUNCTION__ << "@" << __LINE__ << ": write failed" << endl; + throw std::runtime_error(std::string(__FUNCTION__) + + ": I2c.write() failed"); return 0x00; } @@ -266,7 +271,8 @@ uint8_t M24LR64E::EEPROM_Read_Byte(unsigned int address) if (m_i2c.read(buf, pktLen) != pktLen) { - cerr << __FUNCTION__ << "@" << __LINE__ << ": read failed" << endl; + throw std::runtime_error(std::string(__FUNCTION__) + + ": I2c.write() failed"); return 0x00; } @@ -284,14 +290,16 @@ int M24LR64E::EEPROM_Read_Bytes(unsigned int address, if (m_i2c.write(abuf, apktLen)) { - cerr << __FUNCTION__ << "@" << __LINE__ << ": write failed" << endl; + throw std::runtime_error(std::string(__FUNCTION__) + + ": I2c.write() failed"); return false; } int rv = m_i2c.read(buffer, len); if (rv != len) { - cerr << __FUNCTION__ << "@" << __LINE__ << ": read failed" << endl; + throw std::runtime_error(std::string(__FUNCTION__) + + ": I2c.read() failed"); } return rv;