]> Git Repo - serial.git/commitdiff
Updating the error handling.
authorJohn Harrison <[email protected]>
Mon, 23 Jan 2012 19:09:14 +0000 (13:09 -0600)
committerJohn Harrison <[email protected]>
Mon, 23 Jan 2012 19:09:14 +0000 (13:09 -0600)
include/serial/serial.h
src/impl/unix.cc
src/serial.cc

index fa21c72d608126df3885d666fca50c1a3740a74c..e924abc251fee0ebbfb2657872ab527c525bce71 100644 (file)
@@ -83,39 +83,53 @@ typedef enum {
   FLOWCONTROL_HARDWARE
 } flowcontrol_t;
 
-class SerialExecption : public std::exception {
-  const char * e_what;
+class SerialExecption : public std::exception
+{
+  const char* e_what_;
 public:
-  SerialExecption(const char *description) {e_what=description;};
-  virtual const char* what() const throw() {
-        std::stringstream ss;
-        ss << "SerialException " << this->e_what << " failed.";
-        return ss.str().c_str();
+  SerialExecption (const char *description) : e_what_ (description) {}
+
+  virtual const char* what () const throw ()
+  {
+    std::stringstream ss;
+    ss << "SerialException " << e_what_ << " failed.";
+    return ss.str ().c_str ();
   }
 };
 
-class IOException : public std::exception {
-    const char * e_what;
+class IOException : public std::exception
+{
+  const char* e_what_;
+  int errno_;
 public:
-    IOException(const char * description) {e_what = description;}
+  explicit IOException (int errnum) : e_what_ (strerror (errnum)), errno_(errnum) {}
+  explicit IOException (const char * description) : e_what_ (description), errno_(0) {}
+
+  int getErrorNumber () { return errno_; }
 
-    virtual const char* what() const throw() {
-        std::stringstream ss;
-        ss << "IO Exception " << this->e_what << " failed.";
-        return ss.str().c_str();
-    }
+  virtual const char* what () const throw ()
+  {
+    std::stringstream ss;
+    if (errno_ == 0)
+      ss << "IO Exception " << e_what_ << " failed.";
+    else
+      ss << "IO Exception " << errno_ << ":" << e_what_ << " failed.";
+    return ss.str ().c_str ();
+  }
 };
 
-class PortNotOpenedException : public std::exception {
-  const char * e_what;
+class PortNotOpenedException : public std::exception
+{
+  const char * e_what_;
 public:
-    PortNotOpenedException(const char * description) {e_what = description;}
+  PortNotOpenedException (const char * description) : e_what_ (description) {}
 
-    virtual const char* what() const throw() {
+  virtual const char* what () const throw ()
+  {
     std::stringstream ss;
-    ss << e_what << " called before port was opened.";
-        return ss.str().c_str();
-    }
+    ss << e_what_ << " called before port was opened.";
+    return ss.str ().c_str ();
+  }
 };
 
 
index b598024bdad55897932d78e582df80e4893da0bb..24ad257736f86a012c8f1507052df45e2da77cfa 100644 (file)
@@ -64,7 +64,19 @@ Serial::SerialImpl::open ()
 
   if (fd_ == -1)
   {
-    throw IOException ("invalid file descriptor");
+    switch (errno)
+    {
+      case EINTR:
+        // Recurse because this is a recoverable error.
+        open ();
+        return;
+      case ENFILE:
+      case EMFILE:
+        throw IOException ("to many file handles open");
+        break;
+      default:
+        throw IOException (errno);
+    }
   }
 
   reconfigurePort();
@@ -215,7 +227,7 @@ Serial::SerialImpl::available ()
   }
   else
   {
-    throw IOException ("ioctl");
+    throw IOException (errno);
   }
 }
 
@@ -244,8 +256,7 @@ Serial::SerialImpl::read (char* buf, size_t size)
 
       if (r == -1)
       {
-        perror("select()");
-        exit(EXIT_FAILURE);
+        throw IOException (errno);
       }
     }
 
@@ -276,12 +287,27 @@ Serial::SerialImpl::read (char* buf, size_t size)
 size_t
 Serial::SerialImpl::write (const string &data)
 {
-  if (isOpen_ == false) {
+  if (isOpen_ == false)
+  {
     throw PortNotOpenedException ("Serial::write");
   }
+
   ssize_t n = ::write (fd_, data.c_str (), data.length ());
-  if (n == -1) {
-    throw IOException ("Write");
+
+  if (n != static_cast<ssize_t> (data.length ()))
+  {
+    throw IOException ("Write did not complete");
+  }
+  else if (n == -1)
+  {
+    if (errno == EINTR)
+    {
+      return write (data);
+    }
+    else
+    {
+      throw IOException (errno);
+    }
   }
   return static_cast<size_t> (n);
 }
index a0245383fc7fc4ce8a022bb839b62d6c49facbce..c8ac752e4c4af6c31b6e791c388c6ad712e54ca3 100644 (file)
@@ -167,7 +167,7 @@ Serial::readlines(string eol)
 {
   if (pimpl_->getTimeout () < 0)
   {
-    throw "Error, must be set for readlines";
+    throw invalid_argument ("Error, must be set for readlines");
   }
   size_t leneol = eol.length ();
   vector<string> lines;
@@ -339,3 +339,4 @@ bool Serial::getCD ()
 {
   return pimpl_->getCD ();
 }
+
This page took 0.032532 seconds and 4 git commands to generate.