From: Walter Fetter Lages Date: Thu, 31 May 2018 22:25:26 +0000 (-0300) Subject: Add messages on exceptions. X-Git-Url: http://git.ece.ufrgs.br/?a=commitdiff_plain;h=6057de5d2c6eede36f95d619576067383d388485;p=st25_i2c.git Add messages on exceptions. --- diff --git a/include/st25_i2c.hpp b/include/st25_i2c.hpp index 880670e..4597664 100644 --- a/include/st25_i2c.hpp +++ b/include/st25_i2c.hpp @@ -29,6 +29,9 @@ #include #include +#include + +#include class St25I2c { @@ -56,12 +59,14 @@ class St25I2c FTM_MAILBOX= 0x2008 }; - class Error + class Error:public std::exception { int e_; public: Error(const int e):e_(e) { }; + virtual int code(void) const {return e_;}; + virtual const char *what() const _GLIBCXX_USE_NOEXCEPT {return strerror(e_);}; }; class OpenError: public Error { diff --git a/lib/st25_i2c.cpp b/lib/st25_i2c.cpp index d48761a..a9828ff 100644 --- a/lib/st25_i2c.cpp +++ b/lib/st25_i2c.cpp @@ -27,6 +27,7 @@ #include #include +#include #include #include diff --git a/st25_i2c_read/st25_i2c_read.cpp b/st25_i2c_read/st25_i2c_read.cpp index 39d7706..6b61d7c 100644 --- a/st25_i2c_read/st25_i2c_read.cpp +++ b/st25_i2c_read/st25_i2c_read.cpp @@ -38,22 +38,30 @@ int main(int argc,char *argv[]) std::cout << "\tUsage: " << argv[0] << " address count" << std::endl; return -1; } - - St25I2c st25I2c; - + uint16_t addr=strtol(argv[1],NULL,0); size_t count=strtol(argv[2],NULL,0); unsigned char *data=new unsigned char[count]; - - st25I2c.read(addr,data,count); + + try + { + St25I2c st25I2c; + st25I2c.read(addr,data,count); + } + catch (std::exception &e) + { + std::cerr << e.what() << std::endl; + const St25I2c::Error *err=dynamic_cast(&e); + return (err==NULL)? -1:err->code(); + } for(size_t i=0;i < count;i++) std::cout << "0x" << std::hex << std::setw(2) << std::setfill('0') << static_cast(data[i]) << " "; delete[] data; - + std::cout << std::endl; return 0;