Add messages on exceptions.
authorWalter Fetter Lages <w.fetter@ieee.org>
Thu, 31 May 2018 22:25:26 +0000 (19:25 -0300)
committerWalter Fetter Lages <w.fetter@ieee.org>
Thu, 31 May 2018 22:25:26 +0000 (19:25 -0300)
include/st25_i2c.hpp
lib/st25_i2c.cpp
st25_i2c_read/st25_i2c_read.cpp

index 880670e..4597664 100644 (file)
@@ -29,6 +29,9 @@
 
 #include <errno.h>
 #include <stdint.h>
+#include <string.h>
+
+#include <exception>
 
 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
     {
index d48761a..a9828ff 100644 (file)
@@ -27,6 +27,7 @@
 
 #include <errno.h>
 #include <fcntl.h>
+#include <string.h>
 #include <unistd.h>
 
 #include <arpa/inet.h>
index 39d7706..6b61d7c 100644 (file)
@@ -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<St25I2c::Error *>(&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<unsigned>(data[i]) << " ";
         
         delete[] data;
-            
+
         std::cout << std::endl;
 
         return 0;