From 313b01985a02b41279767692b30bc875a60b83fb Mon Sep 17 00:00:00 2001 From: William Woodall Date: Thu, 5 Jan 2012 15:46:20 -0600 Subject: [PATCH 01/72] Adding files for serial_listener. --- examples/serial_listener_example.cc | 46 +++++ include/serial/serial_listener.h | 194 ++++++++++++++++++++ src/serial_listener.cc | 267 ++++++++++++++++++++++++++++ tests/serial_listener_tests.cc | 122 +++++++++++++ 4 files changed, 629 insertions(+) create mode 100644 examples/serial_listener_example.cc create mode 100644 include/serial/serial_listener.h create mode 100644 src/serial_listener.cc create mode 100644 tests/serial_listener_tests.cc diff --git a/examples/serial_listener_example.cc b/examples/serial_listener_example.cc new file mode 100644 index 0000000..1d52d7e --- /dev/null +++ b/examples/serial_listener_example.cc @@ -0,0 +1,46 @@ +#include + +#include +#include + +using namespace serial; + +void default_handler(std::string line) { + std::cout << "default_handler got a: " << line << std::endl; +} + +void callback(std::string line) { + std::cout << "callback got a: " << line << std::endl; +} + +bool comparator(std::string line) { + if (line.substr(0,2) == "V=") + return true; + return false; +} + +int main(void) { + Serial serial("/dev/tty.usbmodemfd1231", 115200); + + SerialListener listener; + // Set the time to live for messages to 1 second + listener.setTimeToLive(1000); + listener.startListening(serial); + + listener.listenFor(comparator, callback); + + serial.write("?$1E\r"); + if (!listener.listenForOnce("?$1E")) { + std::cerr << "Didn't get conformation of device version!" << std::endl; + return; + } + +} + +/* +TODO: + +listenForOnce -> listenForStringOnce +listenForOnce(ComparatorType comparator, std::string& result, size_t timeout) + +*/ \ No newline at end of file diff --git a/include/serial/serial_listener.h b/include/serial/serial_listener.h new file mode 100644 index 0000000..8aa16ba --- /dev/null +++ b/include/serial/serial_listener.h @@ -0,0 +1,194 @@ +/*! + * \file serial/serial_listener.h + * \author William Woodall + * \version 0.1 + * + * \section LICENSE + * + * The BSD License + * + * Copyright (c) 2011 William Woodall + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + * \section DESCRIPTION + * + * This provides a class that allows for asynchronous serial port reading. + * + */ + +// Serial +#include + +// Boost +#include +#include +#include +#include +#include + +#ifndef SERIAL_LISTENER_TEST +#define SERIAL_LISTENER_TEST false +#endif + +namespace serial { + +/*! + * This is a general function type that is used both as the callback prototype + * for asynchronous functions like the default handler callback and the + * listenFor callbacks. + * + * The function takes a std::string reference and returns nothing, it is + * simply passing the resulting line detected by the comparator to the user's + * callback for processing. + * + * \see SerialListener::listenFor, SerialListener::setDefaultHandler + */ +typedef boost::function SerialCallback; + +/*! + * This is a general function type that is used as the comparator callback + * prototpe for the listenFor* type functions. + * + * The function takes a std::string reference and returns true if the string + * matches what the comparator is looking for and false if it does not, unless + * otherwise specified. + * + * \see SerialListener::listenFor, SerialListener::listenForOnce + */ +typedef boost::function ComparatorType; + + +typedef boost::function InfoCallback; +typedef boost::function WarningCallback; +typedef boost::function DebugCallback; +typedef boost::function ExceptionCallback; + +typedef boost::uuids::uuid uuid_t; + +class SerialListenerException : public std::exception { + const char * e_what; +public: + SerialListenerException(const char * e_what) {this->e_what = e_what;} + + virtual const char* what() const throw() { + std::stringstream ss; + ss << "Error listening to serial port: " << this->e_what; + return ss.str().c_str(); + } +}; + +/*! + * Listens to a serial port, facilitates asynchronous reading + */ +class SerialListener +{ +public: + /*! + * Creates a new Serial Listener. + */ + SerialListener (); + + /*! + * Destructor. + */ + virtual ~SerialListener (); + + /*! + * Sets the time-to-live (ttl) for messages waiting to be processsed. + * + * \param ms Time in milliseconds until messages are purged from the buffer. + */ + void setTimeToLive (size_t ms); + + /*! + * Starts a thread to listen for messages and process them through filters. + * + * \param serial_port Pointer to a serial::Serial object that is used to + * retrieve new data. + */ + void startListening (serial::Serial * serial_port); + + /*! + * Stops the listening thread and blocks until it completely stops. + */ + void stopListening (); + + /*! + * Blocks until the given string is detected or until the timeout occurs. + * + * \param token std::string that should be watched for, this string must + * match the message exactly. + * + * \param timeout in milliseconds before timing out and returning false. + * Defaults to 1000 milliseconds or 1 second. + * + * \return bool If true then the token was detected before the token, false + * if the token was not heard and the timeout occured. + */ + bool listenForStringOnce (std::string token, size_t timeout = 1000); + + boost::uuids::uuid listenFor (ComparatorType, SerialCallback); + void stopListeningFor (boost::uuids::uuid filter_uuid); + + InfoCallback info; + WarningCallback warn; + DebugCallback debug; + ExceptionCallback handle_exc; + SerialCallback default_handler; + +private: + void listen(); + std::string listenOnce(std::string data); + size_t determineAmountToRead(); + bool listenForOnceComparator(std::string line); + + bool listening; + + serial::Serial * serial_port; + + boost::thread listen_thread; + boost::uuids::random_generator random_generator(); + + std::string buffer; + std::map lines; + std::map ttls; + boost::posix_time::time_duration ttl; + + // map + std::map filters; + // map + std::map comparators; + // map + std::map callbacks; + // map + std::map + condition_vars; + + // ptime time_start(microsec_clock::local_time()); + // //... execution goes here ... + // ptime time_end(microsec_clock::local_time()); + // time_duration duration(time_end - time_start); + + std::string current_listen_for_one_target; + + boost::mutex filter_mux; +}; + +} \ No newline at end of file diff --git a/src/serial_listener.cc b/src/serial_listener.cc new file mode 100644 index 0000000..5f06e19 --- /dev/null +++ b/src/serial_listener.cc @@ -0,0 +1,267 @@ +#include "mdc2250/serial_listener.h" + +/***** Inline Functions *****/ + +inline void defaultWarningCallback(const std::string& msg) { + std::cout << "SerialListener Warning: " << msg << std::endl; +} + +inline void defaultDebugCallback(const std::string& msg) { + std::cout << "SerialListener Debug: " << msg << std::endl; +} + +inline void defaultInfoCallback(const std::string& msg) { + std::cout << "SerialListener Info: " << msg << std::endl; +} + +inline void defaultExceptionCallback(const std::exception &error) { + std::cerr << "SerialListener Unhandled Exception: " << error.what(); + std::cerr << std::endl; + throw(error); +} + +using namespace serial; + +/***** Listener Class Functions *****/ + +SerialListener::SerialListener() : listening(false) { + // Set default callbacks + this->handle_exc = defaultExceptionCallback; + this->info = defaultInfoCallback; + this->debug = defaultDebugCallback; + this->warn = defaultWarningCallback; + this->default_handler = NULL; + + // Set default ttl + using namespace boost::posix_time; + this->ttl = time_duration(milliseconds(1000)); +} + +SerialListener::~SerialListener() { + +} + +void SerialListener::setTimeToLive(size_t ms) { + this->ttl = time_duration(boost::posix_time::milliseconds(ms)); +} + +void SerialListener::startListening(Serial * serial_port) { + if (this->listening) { + throw(SerialListenerException("Already listening.")); + return; + } + this->listening = true; + + this->serial_port = serial_port; + if (!this->serial_port->isOpen()) { + throw(SerialListenerException("Serial port not open.")); + return; + } + + listen_thread = boost::thread(boost::bind(&SerialListener::listen, this)); +} + +void SerialListener::stopListening() { + listening = false; + listen_thread.join(); + this->serial_port = NULL; +} + +void SerialListener::listen() { + // Make sure there is a serial port + if (this->serial_port == NULL) { + this->handle_exc(SerialListenerException("Invalid serial port.")); + } + // Make sure the serial port is open + if (!this->serial_port->isOpen()) { + this->handle_exc(SerialListenerException("Serial port not open.")); + } + try { + while (this->listening) { + // Determine how much to read in + size_t amount_to_read = determineAmountToRead(); + // Read some + std::string temp = this->serial_port->read(amount_to_read); + if (temp.length() == 0) { + // If nothing was read don't interate through the filters + continue; + } + this->buffer += temp; + if (this->buffer.find("\r") == std::string::npos) { + // If there is no return carrage in the buffer, then a command hasn't + // been completed. + continue; + } + // Listen once + buffer = this->listenOnce(buffer); + // Done parsing lines and buffer should now be set to the left overs + } // while (this->listening) + } catch (std::exception &e) { + this->handle_exc(SerialListenerException(e.what())); + } +} + +std::string SerialListener::listenOnce(std::string data) { + std::string left_overs; +// TODO: Make the delimeter settable + // Split the buffer by the delimeter + std::vector new_lines; + boost::split(new_lines, data, boost::is_any_of("\r")); // it only uses \r + // Iterate through new lines and add times to them + std::vector::iterator it_lines; + for(it_lines=new_lines.begin(); it_lines!=new_lines.end(); it_lines++) { + // The last line needs to be put back in the buffer always: + // In the case that the string ends with \r the last element will be + // empty (""). In the case that it does not the last element will be + // what is left over from the next message that hasn't sent + // everything. Ex.: "?$1E\r" -> ["?$1E", ""] and + // "?$1E\r$1E=Robo" -> ["?$1E","$1E=Robo"] + if (it_lines == new_lines.end()-1) { + left_overs = (*it_lines); + continue; + } + uuid_t uuid = random_generator(); + lines.insert(std::pair(uuid,(*it_lines))); + using namespace boost::posix_time; + ttls.insert(std::pair + (uuid,ptime(microsec_clock::local_time()))); + } + // Iterate through the lines checking for a match + for(it_lines=lines.begin(); it_lines!=lines.end(); it_lines++) { + std::string line = (*it_lines).second; + uuid_t uuid = (*it_lines).first + // If the line is empty, continue + if (line.length() == 0) { + continue; + } + bool matched = false; + bool erased = false; + // Get the filter lock + boost::mutex::scoped_lock l(filter_mux); + // Iterate through each filter + std::map::iterator it; + for(it=filters.begin(); it!=filters.end(); it++) { + if (comparators[(*it).first](line)) { // If comparator matches line + if ((*it).second == "non-blocking") { +// TODO: Put this callback execution into a queue + // If non-blocking run the callback + callbacks[(*it).first](line); + lines.erase(uuid); + ttls.erase(uuid); + erased = true; + } else if ((*it).second == "blocking") { + // If blocking then notify the waiting call to continue + condition_vars[(*it).first]->notify_all(); + lines.erase(uuid); + ttls.erase(uuid); + erased = true; + } + matched = true; + break; // It matched, continue to next line + } + } // for(it=filters.begin(); it!=filters.end(); it++) + // If the comparator doesn't match try another + if (!matched) { // Try to send to default handler + if (this->default_handler) { + this->default_handler(line); + lines.erase(uuid); + ttls.erase(uuid); + erased = true; + } + } + // If not already erased check how old it is, remove the too old + if (!erased) { + using namespace boost::posix_time; + if (ptime(microsec_clock::local_time())-ttls[uuid] > ttl) { + lines.erase(uuid); + ttls.erase(uuid); + } + } + } // for(it_lines=lines.begin(); it_lines!=lines.end(); it_lines++) + return left_overs; +} + +size_t SerialListener::determineAmountToRead() { + // TODO: Make a more intelligent method based on the length of the things + // filters are looking for. i.e.: if the filter is looking for 'V=XX\r' + // make the read amount at least 5. + return 5; +} + +bool SerialListener::listenForOnceComparator(std::string line) { + if (line == current_listen_for_one_target) + return true; + return false; +} + +bool SerialListener::listenForOnce(std::string token, size_t milliseconds) { + boost::condition_variable cond; + boost::mutex mut; + current_listen_for_one_target = token; + + // Create blocking filter + uuid_t uuid = random_generator(); + std::pair + filter_pair(uuid, "blocking"); + std::pair + comparator_pair(uuid, + boost::bind(&SerialListener::listenForOnceComparator, this, _1)); + std::pair + condition_pair(uuid, &cond); + { + boost::mutex::scoped_lock l(filter_mux); + filters.insert(filter_pair); + comparators.insert(comparator_pair); + condition_vars.insert(condition_pair); + } + + bool result = false; + + // Wait + boost::unique_lock lock(mut); + if (cond.timed_wait(lock, boost::posix_time::milliseconds(milliseconds))) + result = true; + + // Destroy the filter + { + boost::mutex::scoped_lock l(filter_mux); + filters.erase(uuid); + comparators.erase(uuid); + condition_vars.erase(uuid); + } + + return result; +} + +boost::uuids::uuid +SerialListener::listenFor(ComparatorType comparator, + SerialCallback callback) +{ + // Create Filter + uuid_t uuid = random_generator(); + std::pair + filter_pair(uuid, "non-blocking"); + std::pair + comparator_pair(uuid, comparator); + std::pair + callback_pair(uuid, callback); + + { + boost::mutex::scoped_lock l(filter_mux); + filters.insert(filter_pair); + comparators.insert(comparator_pair); + callbacks.insert(callback_pair); + } + + return uuid; +} + +void SerialListener::stopListeningFor(boost::uuids::uuid filter_uuid) { + // Delete filter + boost::mutex::scoped_lock l(filter_mux); + filters.erase(filter_uuid); + comparators.erase(filter_uuid); + callbacks.erase(filter_uuid); +} + + diff --git a/tests/serial_listener_tests.cc b/tests/serial_listener_tests.cc new file mode 100644 index 0000000..06a0a58 --- /dev/null +++ b/tests/serial_listener_tests.cc @@ -0,0 +1,122 @@ +#include "gtest/gtest.h" + +#include + +#define SERIAL_LISTENER_TEST true + +// OMG this is so nasty... +#define private public +#define protected public + +#include "mdc2250/serial_listener.h" +using namespace serial; + +static size_t global_count, global_listen_count; + +void default_handler(std::string line) { + global_count++; + // std::cout << "default_handler got: " << line << std::endl; +} + +namespace { + +class SerialListenerTests : public ::testing::Test { +protected: + virtual void SetUp() { + listener.default_handler = default_handler; + } + + void execute_lookForOnce() { + listener.listenForOnce("?$1E", 1000); + } + + SerialListener listener; + +}; + +TEST_F(SerialListenerTests, ignoresEmptyString) { + global_count = 0; + + listener.listenOnce(""); + + ASSERT_TRUE(global_count == 0); +} + +TEST_F(SerialListenerTests, ignoresPartialMessage) { + global_count = 0; + + listener.listenOnce("?$1E\r$1E=Robo"); + + ASSERT_EQ(global_count, 1); +} + +TEST_F(SerialListenerTests, listenForOnceWorks) { + global_count = 0; + + boost::thread t( + boost::bind(&SerialListenerTests::execute_lookForOnce, this)); + + boost::this_thread::sleep(boost::posix_time::milliseconds(100)); + + listener.listenOnce("\r+\r?$1E\r$1E=Robo"); + + ASSERT_TRUE(t.timed_join(boost::posix_time::milliseconds(1500))); + + // Make sure the filters are getting deleted + ASSERT_EQ(listener.filters.size(), 0); + + ASSERT_EQ(global_count, 1); +} + +// lookForOnce should not find it, but timeout after 1000ms, so it should +// still join. +TEST_F(SerialListenerTests, listenForOnceTimesout) { + global_count = 0; + + boost::thread t( + boost::bind(&SerialListenerTests::execute_lookForOnce, this)); + + boost::this_thread::sleep(boost::posix_time::milliseconds(100)); + + listener.listenOnce("\r+\r?$1ENOTRIGHT\r$1E=Robo"); + + ASSERT_TRUE(t.timed_join(boost::posix_time::milliseconds(1500))); + + ASSERT_EQ(global_count, 2); +} + +bool listenForComparator(std::string line) { + if (line.substr(0,2) == "V=") { + return true; + } + return false; +} + +void listenForCallback(std::string line) { + global_listen_count++; +} + +TEST_F(SerialListenerTests, listenForWorks) { + global_count = 0; + global_listen_count = 0; + + boost::uuids::uuid filt_uuid = + listener.listenFor(listenForComparator, listenForCallback); + + listener.listenOnce("\r+\rV=05:06\r?$1E\rV=06:05\r$1E=Robo"); + + ASSERT_EQ(global_count, 2); + ASSERT_EQ(global_listen_count, 2); + + listener.stopListeningFor(filt_uuid); + + ASSERT_EQ(listener.filters.size(), 0); + +} + +} // namespace + +int main(int argc, char **argv) { + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} From 318bce46bf30237bd192abc68f4885c5262a68c7 Mon Sep 17 00:00:00 2001 From: William Woodall Date: Sat, 7 Jan 2012 15:24:30 -0600 Subject: [PATCH 02/72] Still working on SerialListener addition. --- Findserial.cmake | 2 +- Makefile | 2 +- .../{serial_example.cpp => serial_example.cc} | 0 examples/serial_listener_example.cc | 6 +- include/serial/serial_listener.h | 346 +++++++++++++++--- serial.cmake | 35 +- serial.mk => serial.makefile | 2 +- src/{serial.cpp => serial.cc} | 0 src/serial_listener.cc | 165 ++++++--- tests/proof_of_concepts/tokenizer.cc.out | Bin 0 -> 203304 bytes tests/serial_listener_tests.cc | 22 +- 11 files changed, 457 insertions(+), 123 deletions(-) rename examples/{serial_example.cpp => serial_example.cc} (100%) rename serial.mk => serial.makefile (93%) rename src/{serial.cpp => serial.cc} (100%) create mode 100755 tests/proof_of_concepts/tokenizer.cc.out diff --git a/Findserial.cmake b/Findserial.cmake index b102e26..26877ab 100644 --- a/Findserial.cmake +++ b/Findserial.cmake @@ -1,4 +1,4 @@ -find_path(serial_INCLUDE_DIRS serial.h /usr/include "$ENV{NAMER_ROOT}") +find_path(serial_INCLUDE_DIRS serial.h serial_listener.h /usr/include/serial "$ENV{NAMER_ROOT}") find_library(serial_LIBRARIES serial /usr/lib "$ENV{NAMER_ROOT}") diff --git a/Makefile b/Makefile index 663e15c..5df6eab 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ ifdef ROS_ROOT include $(shell rospack find mk)/cmake.mk else -include serial.mk +include serial.makefile endif diff --git a/examples/serial_example.cpp b/examples/serial_example.cc similarity index 100% rename from examples/serial_example.cpp rename to examples/serial_example.cc diff --git a/examples/serial_listener_example.cc b/examples/serial_listener_example.cc index 1d52d7e..83ef3c1 100644 --- a/examples/serial_listener_example.cc +++ b/examples/serial_listener_example.cc @@ -25,14 +25,14 @@ int main(void) { SerialListener listener; // Set the time to live for messages to 1 second listener.setTimeToLive(1000); - listener.startListening(serial); + listener.startListening(&serial); listener.listenFor(comparator, callback); serial.write("?$1E\r"); - if (!listener.listenForOnce("?$1E")) { + if (!listener.listenForStringOnce("?$1E")) { std::cerr << "Didn't get conformation of device version!" << std::endl; - return; + return 1; } } diff --git a/include/serial/serial_listener.h b/include/serial/serial_listener.h index 8aa16ba..23ca918 100644 --- a/include/serial/serial_listener.h +++ b/include/serial/serial_listener.h @@ -33,6 +33,9 @@ * */ +#ifndef SERIAL_LISTENER_H +#define SERIAL_LISTENER_H + // Serial #include @@ -43,14 +46,10 @@ #include #include -#ifndef SERIAL_LISTENER_TEST -#define SERIAL_LISTENER_TEST false -#endif - namespace serial { /*! - * This is a general function type that is used both as the callback prototype + * This is a general function type that is used as the callback prototype * for asynchronous functions like the default handler callback and the * listenFor callbacks. * @@ -60,7 +59,7 @@ namespace serial { * * \see SerialListener::listenFor, SerialListener::setDefaultHandler */ -typedef boost::function SerialCallback; +typedef boost::function DataCallback; /*! * This is a general function type that is used as the comparator callback @@ -74,22 +73,101 @@ typedef boost::function SerialCallback; */ typedef boost::function ComparatorType; +/*! + * This function type describes the prototype for the logging callbacks. + * + * The function takes a std::string reference and returns nothing. It is + * called from the library when a logging message occurs. This + * allows the library user to hook into this and integrate it with their own + * logging system. It can be set with any of the setHandler + * functions. + * + * \see SerialListener::setInfoHandler, SerialListener::setDebugHandler, + * SerialListener::setWarningHandler + */ +typedef boost::function LoggingCallback; -typedef boost::function InfoCallback; -typedef boost::function WarningCallback; -typedef boost::function DebugCallback; +/*! + * This function type describes the prototype for the exception callback. + * + * The function takes a std::exception reference and returns nothing. It is + * called from the library when an exception occurs in a library thread. + * This exposes these exceptions to the user so they can to error handling. + * + * \see SerialListener::setExceptionHandler + */ typedef boost::function ExceptionCallback; -typedef boost::uuids::uuid uuid_t; +/*! + * This function type describes the prototype for the tokenizer callback. + * + * The function should take a std::string reference and tokenize it into a + * several std::string's and store them in the given + * std::vector reference. There are some default ones or the + * user can create their own. + * + * The last element in the std::vector of std::string's should always be + * either an empty string ("") or the last partial message. The last element + * in the std::vector will be put back into the data buffer so that if it is + * incomplete it can be completed when more data is read. + * + * Example: A delimeter tokenizer with a delimeter of "\r". The result would + * be: "msg1\rmsg2\r" -> ["msg1", "msg2", ""] for all complete messages, or: + * "msg1\rpartial_msg2" -> ["msg1","partial_msg2"] for partial messages. + * + * \see SerialListener::setTokenizer, serial::delimeter_tokenizer + */ +typedef boost::function&)> +TokenizerType; -class SerialListenerException : public std::exception { - const char * e_what; +/*! This is a convenience alias for boost::uuids::uuid. */ +typedef boost::uuids::uuid uuid_type; // uuid_t is already taken! =( + +void +_delimeter_tokenizer (std::string &data, std::vector &tokens, + std::string delimeter); + +/*! + * This returns a tokenizer that splits on a given delimeter. + * + * The delimeter is passed into the function and a TokenizerType is returned + * that can be passed to SerialListener::setTokenizer. + * + * Example: + *
+ *   my_listener.setTokenizer(delimeter_tokenizer("\r"));
+ * <\pre>
+ * 
+ * \see SerialListener::setTokenizer, serial::TokenizerType
+ */
+class delimeter_tokenizer
+{
 public:
-  SerialListenerException(const char * e_what) {this->e_what = e_what;}
+  delimeter_tokenizer ();
+  virtual ~delimeter_tokenizer ();
+
+private:
+  /* data */
+};
+TokenizerType
+delimeter_tokenizer (std::string delimeter);
+
+/*!
+ * This is a general exception generated by the SerialListener class.
+ * 
+ * Check the SerialListenerException::what function for the cause.
+ 
+ * \param e_what is a std::string that describes the cause of the error.
+ */
+class SerialListenerException : public std::exception {
+  const std::string e_what;
+public:
+  SerialListenerException(const std::string e_what) : e_what(e_what) {}
+  ~SerialListenerException() throw() {std::exception::~exception();}
 
   virtual const char* what() const throw() {
     std::stringstream ss;
-    ss << "Error listening to serial port: " << this->e_what;
+    ss << "SerialListenerException: " << this->e_what;
     return ss.str().c_str();
   }
 };
@@ -104,18 +182,42 @@ public:
    * Creates a new Serial Listener.
    */
   SerialListener ();
-  
+
   /*!
    * Destructor.
    */
   virtual ~SerialListener ();
 
+/***** Configurations ******/
+
   /*!
    * Sets the time-to-live (ttl) for messages waiting to be processsed.
    * 
+   * Messages are processed before checking for expiration, therefore they 
+   * will always be passed through filters once before being removed 
+   * due to ttl expiration. The default value for this is 10 ms.
+   * 
    * \param ms Time in milliseconds until messages are purged from the buffer.
    */
-  void setTimeToLive (size_t ms);
+  void setTimeToLive (size_t ms = 10);
+
+  /*!
+   * Sets the tokenizer to be used when tokenizing the data into tokens.
+   * 
+   * This function is given a std::string of data and is responsible for 
+   * tokenizing that data into a std::vector of data tokens.
+   * The default tokenizer splits the data by the ascii return carriage.
+   * The user can create their own tokenizer or use one of the default ones.
+   * 
+   * \param tokenizer Function for tokenizing the incoming data.
+   * 
+   * \see serial::TokenizerType, serial::delimeter_tokenizer
+   */
+  void setTokenizer (TokenizerType tokenizer) {
+    this->tokenize = tokenizer;
+  }
+
+/***** Start and Stop Listening ******/
 
   /*!
    * Starts a thread to listen for messages and process them through filters.
@@ -127,9 +229,14 @@ public:
 
   /*!
    * Stops the listening thread and blocks until it completely stops.
+   * 
+   * This function also clears all of the active filters from listenFor and 
+   * similar functions.
    */
   void stopListening ();
 
+/***** Filter Functions ******/
+
   /*!
    * Blocks until the given string is detected or until the timeout occurs.
    * 
@@ -144,51 +251,198 @@ public:
    */
   bool listenForStringOnce (std::string token, size_t timeout = 1000);
 
-  boost::uuids::uuid listenFor (ComparatorType, SerialCallback);
-  void stopListeningFor (boost::uuids::uuid filter_uuid);
+  /*!
+   * Setups up a filter that calls a callback when a comparator returns true.
+   * 
+   * The user provides a comparator and a callback, and every time a line is 
+   * received the comparator is called and the comparator has to evaluate the 
+   * line and return true if it matches and false if it doesn't.  If it does 
+   * match, the callback is called with the resulting line.
+   * 
+   * \param comparator This is a comparator for detecting if a line matches.
+   * The comparartor receives a std::string reference and must return a true 
+   * if it matches and false if it doesn't.
+   * 
+   * \param callback This is the handler for when a match occurs. It is given 
+   * a std::string reference of the line that matched your comparator.
+   * 
+   * \return boost::uuids::uuid a unique identifier used to remove the filter.
+   */
+  uuid_type listenFor (ComparatorType comparator, DataCallback callback);
 
-  InfoCallback info;
-  WarningCallback warn;
-  DebugCallback debug;
-  ExceptionCallback handle_exc;
-  SerialCallback default_handler;
+  /*!
+   * Removes a filter by a given uuid.
+   * 
+   * The uuid for a filter is returned by the listenFor function.
+   * 
+   * \param filter_uuid The uuid of the filter to be removed.
+   */
+  void stopListeningFor (uuid_type filter_uuid);
+
+  /*!
+   * Stops listening for anything, but doesn't stop reading the serial port.
+   */
+  void stopListeningForAll ();
+
+/***** Hooks and Handlers ******/
+
+  /*!
+   * Sets the handler to be called when a lines is not caught by a filter.
+   * 
+   * This allows you to set a catch all function that will get called 
+   * everytime a line is not matched by a filter and the ttl expires.
+   * 
+   * Setting the callbacks works just like SerialListener::setInfoHandler.
+   * 
+   * \param default_handler A function pointer to the callback to handle 
+   * unmatched and expired messages.
+   * 
+   * \see serial::DataCallback, SerialListener::setInfoHandler
+   */
+  void setDefaultHandler(DataCallback default_handler) {
+    this->default_handler = default_handler;
+  }
+
+  /*!
+   * Sets the function to be called when an info logging message occurs.
+   * 
+   * This allows you to hook into the message reporting of the library and use
+   * your own logging facilities.
+   * 
+   * The provided function must follow this prototype:
+   * 
+   *    void yourInfoCallback(const std::string &msg)
+   * 
+ * Here is an example: + *
+   *    void yourInfoCallback(const std::string &msg) {
+   *        std::cout << "SerialListener Info: " << msg << std::endl;
+   *    }
+   * 
+ * And the resulting call to make it the callback: + *
+   *    serial::SerialListener listener;
+   *    listener.setInfoCallback(yourInfoCallback);
+   * 
+ * Alternatively you can use a class method as a callback using boost::bind: + *
+   *    #include 
+   *    
+   *    #include "serial/serial_listener.h"
+   *    
+   *    class MyClass
+   *    {
+   *    public:
+   *     MyClass () {
+   *      listener.setInfoHandler(
+   *          boost::bind(&MyClass::handleInfo, this, _1));
+   *     }
+   *    
+   *     void handleInfo(const std::string &msg) {
+   *       std::cout << "MyClass Info: " << msg << std::endl;
+   *     }
+   *    
+   *    private:
+   *     serial::SerialListener listener;
+   *    };
+   * 
+ * + * \param info_handler A function pointer to the callback to handle new + * Info messages. + * + * \see serial::LoggingCallback + */ + void setInfoHandler(LoggingCallback info_handler) { + this->info = info_handler; + } + + /*! + * Sets the function to be called when a debug logging message occurs. + * + * This allows you to hook into the message reporting of the library and use + * your own logging facilities. + * + * This works just like SerialListener::setInfoHandler. + * + * \param debug_handler A function pointer to the callback to handle new + * Debug messages. + * + * \see serial::LoggingCallback, SerialListener::setInfoHandler + */ + void setDebugHandler(LoggingCallback debug_handler) { + this->debug = debug_handler; + } + + /*! + * Sets the function to be called when a warning logging message occurs. + * + * This allows you to hook into the message reporting of the library and use + * your own logging facilities. + * + * This works just like SerialListener::setInfoHandler. + * + * \param warning_handler A function pointer to the callback to handle new + * Warning messages. + * + * \see serial::LoggingCallback, SerialListener::setInfoHandler + */ + void setWarningHandler(LoggingCallback warning_handler) { + this->warn = warning_handler; + } private: - void listen(); - std::string listenOnce(std::string data); - size_t determineAmountToRead(); + // Function that loops while listening is true + void listen (); + // Called by listen iteratively + std::string listenOnce (std::string data); + // Determines how much to read on each loop of listen + size_t determineAmountToRead (); + // Used in the look for string once function bool listenForOnceComparator(std::string line); + // Tokenizer + TokenizerType tokenize; + + // Logging handlers + LoggingCallback warn; + LoggingCallback info; + LoggingCallback debug; + + // Exception handler + ExceptionCallback handle_exc; + + // Default handler + DataCallback default_handler; + + // Persistent listening variables bool listening; - serial::Serial * serial_port; - boost::thread listen_thread; - boost::uuids::random_generator random_generator(); - std::string buffer; - std::map lines; - std::map ttls; + std::map lines; + std::map ttls; + + // For generating random uuids + boost::uuids::random_generator random_generator; + + // Setting for ttl on messages boost::posix_time::time_duration ttl; // map - std::map filters; + std::map filters; // map - std::map comparators; + std::map comparators; // map - std::map callbacks; + std::map callbacks; // map - std::map - condition_vars; - - // ptime time_start(microsec_clock::local_time()); - // //... execution goes here ... - // ptime time_end(microsec_clock::local_time()); - // time_duration duration(time_end - time_start); - - std::string current_listen_for_one_target; - + std::map condition_vars; + // Mutex for locking use of filters boost::mutex filter_mux; + + // Used as temporary storage for listenForStringOnce + std::string current_listen_for_one_target; }; -} \ No newline at end of file +} + +#endif // SERIAL_LISTENER_H \ No newline at end of file diff --git a/serial.cmake b/serial.cmake index 7f2ea57..6baec63 100644 --- a/serial.cmake +++ b/serial.cmake @@ -10,6 +10,11 @@ project(Serial) ## Configurations +# Use clang if available +IF(EXISTS /usr/bin/clang) + set(CMAKE_CXX_COMPILER /usr/bin/clang++) +ENDIF(EXISTS /usr/bin/clang) + option(SERIAL_BUILD_TESTS "Build all of the Serial tests." OFF) option(SERIAL_BUILD_EXAMPLES "Build all of the Serial examples." OFF) @@ -33,9 +38,9 @@ ENDIF(NOT DEFINED(LIBRARY_OUTPUT_PATH)) include_directories(${PROJECT_SOURCE_DIR}/include) # Add default source files -set(SERIAL_SRCS src/serial.cpp) +set(SERIAL_SRCS src/serial.cc src/serial_listener.cc) # Add default header files -set(SERIAL_HEADERS include/serial/serial.h) +set(SERIAL_HEADERS include/serial/serial.h include/serial/serial_listener.h) # Find Boost, if it hasn't already been found IF(NOT Boost_FOUND OR NOT Boost_SYSTEM_FOUND OR NOT Boost_FILESYSTEM_FOUND OR NOT Boost_THREAD_FOUND) @@ -67,17 +72,33 @@ ENDIF(CMAKE_SYSTEM_NAME MATCHES Darwin) # If asked to IF(SERIAL_BUILD_EXAMPLES) - # Compile the Test program - add_executable(serial_example examples/serial_example.cpp) + # Compile the Serial Test program + add_executable(serial_example examples/serial_example.cc) # Link the Test program to the Serial library target_link_libraries(serial_example serial) + + # Compile the Serial Listener Test program + add_executable(serial_listener_example + examples/serial_listener_example.cc) + # Link the Test program to the Serial library + target_link_libraries(serial_listener_example serial) ENDIF(SERIAL_BUILD_EXAMPLES) ## Build tests # If asked to IF(SERIAL_BUILD_TESTS) - # none yet... + # Find Google Test + enable_testing() + find_package(GTest REQUIRED) + include_directories(${GTEST_INCLUDE_DIRS}) + + # Compile the Serial Listener Test program + add_executable(serial_listener_tests tests/serial_listener_tests.cc) + # Link the Test program to the serial library + target_link_libraries(serial_listener_tests ${GTEST_BOTH_LIBRARIES} + serial) + add_test(AllTestsIntest_serial serial_listener_tests) ENDIF(SERIAL_BUILD_TESTS) ## Setup install and uninstall @@ -95,7 +116,9 @@ IF(NOT SERIAL_DONT_CONFIGURE_INSTALL) ARCHIVE DESTINATION lib ) - INSTALL(FILES include/serial/serial.h DESTINATION include/serial) + INSTALL(FILES include/serial/serial.h + include/serial/serial_listener.h + DESTINATION include/serial) IF(NOT CMAKE_FIND_INSTALL_PATH) set(CMAKE_FIND_INSTALL_PATH ${CMAKE_ROOT}) diff --git a/serial.mk b/serial.makefile similarity index 93% rename from serial.mk rename to serial.makefile index 1cd189d..e1a0af8 100644 --- a/serial.mk +++ b/serial.makefile @@ -30,4 +30,4 @@ ifneq ($(MAKE),) else cd build && make endif - # cd bin && ./serial_tests \ No newline at end of file + cd bin && ./serial_listener_tests \ No newline at end of file diff --git a/src/serial.cpp b/src/serial.cc similarity index 100% rename from src/serial.cpp rename to src/serial.cc diff --git a/src/serial_listener.cc b/src/serial_listener.cc index 5f06e19..32994f9 100644 --- a/src/serial_listener.cc +++ b/src/serial_listener.cc @@ -1,4 +1,4 @@ -#include "mdc2250/serial_listener.h" +#include "serial/serial_listener.h" /***** Inline Functions *****/ @@ -22,6 +22,18 @@ inline void defaultExceptionCallback(const std::exception &error) { using namespace serial; +void +_delimeter_tokenizer (std::string &data, std::vector &tokens, + std::string delimeter) +{ + boost::split(tokens, data, boost::is_any_of(delimeter)); +} + +TokenizerType +delimeter_tokenizer (std::string delimeter) { + return boost::bind(_delimeter_tokenizer, _1, _2, delimeter); +} + /***** Listener Class Functions *****/ SerialListener::SerialListener() : listening(false) { @@ -32,20 +44,25 @@ SerialListener::SerialListener() : listening(false) { this->warn = defaultWarningCallback; this->default_handler = NULL; + // Set default tokenizer + this->setTokenizer(delimeter_tokenizer("\r")); + // Set default ttl - using namespace boost::posix_time; - this->ttl = time_duration(milliseconds(1000)); + this->setTimeToLive(); } SerialListener::~SerialListener() { } -void SerialListener::setTimeToLive(size_t ms) { - this->ttl = time_duration(boost::posix_time::milliseconds(ms)); +void +SerialListener::setTimeToLive(size_t ms) { + using namespace boost::posix_time; + this->ttl = time_duration(milliseconds(ms)); } -void SerialListener::startListening(Serial * serial_port) { +void +SerialListener::startListening(Serial * serial_port) { if (this->listening) { throw(SerialListenerException("Already listening.")); return; @@ -61,13 +78,31 @@ void SerialListener::startListening(Serial * serial_port) { listen_thread = boost::thread(boost::bind(&SerialListener::listen, this)); } -void SerialListener::stopListening() { +void +SerialListener::stopListening() { + // Stop listening and clear buffers listening = false; listen_thread.join(); + this->buffer = ""; + this->lines.clear(); + this->ttls.clear(); this->serial_port = NULL; + + // Delete all the filters + this->stopListeningForAll(); } -void SerialListener::listen() { +void +SerialListener::stopListeningForAll() { + boost::mutex::scoped_lock l(filter_mux); + filters.clear(); + comparators.clear(); + callbacks.clear(); + condition_vars.clear(); +} + +void +SerialListener::listen() { // Make sure there is a serial port if (this->serial_port == NULL) { this->handle_exc(SerialListenerException("Invalid serial port.")); @@ -82,17 +117,20 @@ void SerialListener::listen() { size_t amount_to_read = determineAmountToRead(); // Read some std::string temp = this->serial_port->read(amount_to_read); - if (temp.length() == 0) { - // If nothing was read don't interate through the filters + // If nothing was read and there is nothing in the lines, then we + // don't need to interate through the filters + if (temp.length() == 0 && lines.size() == 0) { continue; } + // Add the new data to the buffer this->buffer += temp; - if (this->buffer.find("\r") == std::string::npos) { - // If there is no return carrage in the buffer, then a command hasn't - // been completed. + // If there is no return carrage in the buffer, then a command hasn't + // been completed and if there is no data in the lines buffer, then + // continue. + if (this->buffer.find("\r") == std::string::npos && lines.size() == 0) { continue; } - // Listen once + // Listen once, this parses the buffer and filters the data in lines buffer = this->listenOnce(buffer); // Done parsing lines and buffer should now be set to the left overs } // while (this->listening) @@ -101,35 +139,40 @@ void SerialListener::listen() { } } -std::string SerialListener::listenOnce(std::string data) { +// TODO: as it is, each line is passed to filters repeatedly until they are +// too old... Change it to only send each line to each filter once and +// then send to new fitlers up until it is too old. +std::string +SerialListener::listenOnce(std::string data) { std::string left_overs; -// TODO: Make the delimeter settable - // Split the buffer by the delimeter + std::vector to_be_erased; + // Tokenize the new data std::vector new_lines; - boost::split(new_lines, data, boost::is_any_of("\r")); // it only uses \r + tokenize(data, new_lines); // Iterate through new lines and add times to them - std::vector::iterator it_lines; - for(it_lines=new_lines.begin(); it_lines!=new_lines.end(); it_lines++) { + std::vector::iterator it_new; + for(it_new=new_lines.begin(); it_new != new_lines.end(); it_new++) { // The last line needs to be put back in the buffer always: // In the case that the string ends with \r the last element will be // empty (""). In the case that it does not the last element will be // what is left over from the next message that hasn't sent // everything. Ex.: "?$1E\r" -> ["?$1E", ""] and // "?$1E\r$1E=Robo" -> ["?$1E","$1E=Robo"] - if (it_lines == new_lines.end()-1) { - left_overs = (*it_lines); + if (it_new == new_lines.end()-1) { + left_overs = (*it_new); continue; } - uuid_t uuid = random_generator(); - lines.insert(std::pair(uuid,(*it_lines))); + uuid_type uuid = random_generator(); + lines.insert(std::pair(uuid,(*it_new))); using namespace boost::posix_time; - ttls.insert(std::pair + ttls.insert(std::pair (uuid,ptime(microsec_clock::local_time()))); } // Iterate through the lines checking for a match + std::map::iterator it_lines; for(it_lines=lines.begin(); it_lines!=lines.end(); it_lines++) { std::string line = (*it_lines).second; - uuid_t uuid = (*it_lines).first + uuid_type uuid = (*it_lines).first; // If the line is empty, continue if (line.length() == 0) { continue; @@ -139,74 +182,80 @@ std::string SerialListener::listenOnce(std::string data) { // Get the filter lock boost::mutex::scoped_lock l(filter_mux); // Iterate through each filter - std::map::iterator it; + std::map::iterator it; for(it=filters.begin(); it!=filters.end(); it++) { if (comparators[(*it).first](line)) { // If comparator matches line if ((*it).second == "non-blocking") { -// TODO: Put this callback execution into a queue +// TODO: Put this callback execution into a queue. And if I do, make sure to +// keep the line instance around until the callback is done... // If non-blocking run the callback callbacks[(*it).first](line); - lines.erase(uuid); - ttls.erase(uuid); + to_be_erased.push_back(uuid); erased = true; } else if ((*it).second == "blocking") { // If blocking then notify the waiting call to continue condition_vars[(*it).first]->notify_all(); - lines.erase(uuid); - ttls.erase(uuid); + to_be_erased.push_back(uuid); erased = true; } matched = true; break; // It matched, continue to next line } } // for(it=filters.begin(); it!=filters.end(); it++) - // If the comparator doesn't match try another - if (!matched) { // Try to send to default handler - if (this->default_handler) { - this->default_handler(line); - lines.erase(uuid); - ttls.erase(uuid); - erased = true; - } - } // If not already erased check how old it is, remove the too old if (!erased) { using namespace boost::posix_time; if (ptime(microsec_clock::local_time())-ttls[uuid] > ttl) { - lines.erase(uuid); - ttls.erase(uuid); + // If there is a default handler pass it on + if (this->default_handler) { +// TODO: see above about callback execution queue + this->default_handler(line); + } + to_be_erased.push_back(uuid); } } } // for(it_lines=lines.begin(); it_lines!=lines.end(); it_lines++) + // Remove any lines that need to be erased + // (this must be done outside the iterator to prevent problems incrementing + // the iterator) + std::vector::iterator it; + for (it=to_be_erased.begin(); it != to_be_erased.end(); it++) { + lines.erase((*it)); + ttls.erase((*it)); + } + // Return the left_overs return left_overs; } -size_t SerialListener::determineAmountToRead() { +size_t +SerialListener::determineAmountToRead() { // TODO: Make a more intelligent method based on the length of the things // filters are looking for. i.e.: if the filter is looking for 'V=XX\r' // make the read amount at least 5. return 5; } -bool SerialListener::listenForOnceComparator(std::string line) { +bool +SerialListener::listenForOnceComparator(std::string line) { if (line == current_listen_for_one_target) return true; return false; } -bool SerialListener::listenForOnce(std::string token, size_t milliseconds) { +bool +SerialListener::listenForStringOnce(std::string token, size_t milliseconds) { boost::condition_variable cond; boost::mutex mut; current_listen_for_one_target = token; // Create blocking filter - uuid_t uuid = random_generator(); - std::pair + uuid_type uuid = random_generator(); + std::pair filter_pair(uuid, "blocking"); - std::pair + std::pair comparator_pair(uuid, boost::bind(&SerialListener::listenForOnceComparator, this, _1)); - std::pair + std::pair condition_pair(uuid, &cond); { boost::mutex::scoped_lock l(filter_mux); @@ -233,17 +282,16 @@ bool SerialListener::listenForOnce(std::string token, size_t milliseconds) { return result; } -boost::uuids::uuid -SerialListener::listenFor(ComparatorType comparator, - SerialCallback callback) +uuid_type +SerialListener::listenFor(ComparatorType comparator, DataCallback callback) { // Create Filter - uuid_t uuid = random_generator(); - std::pair + uuid_type uuid = random_generator(); + std::pair filter_pair(uuid, "non-blocking"); - std::pair + std::pair comparator_pair(uuid, comparator); - std::pair + std::pair callback_pair(uuid, callback); { @@ -256,7 +304,8 @@ SerialListener::listenFor(ComparatorType comparator, return uuid; } -void SerialListener::stopListeningFor(boost::uuids::uuid filter_uuid) { +void +SerialListener::stopListeningFor(uuid_type filter_uuid) { // Delete filter boost::mutex::scoped_lock l(filter_mux); filters.erase(filter_uuid); diff --git a/tests/proof_of_concepts/tokenizer.cc.out b/tests/proof_of_concepts/tokenizer.cc.out new file mode 100755 index 0000000000000000000000000000000000000000..1c5193737f66e7eb47d4aee41dbb363bf26f496c GIT binary patch literal 203304 zcmeGF4|r77wFZpO0BW?vM2i+J)o9UzMU9FImTFK&M3FIa>W5{e2MAkj>Q z(NsaHa&6jDrHYzb)Id>#QYN)jqnE3>mTFY0Cr+--wNz6}<$K??*FJM*G9fwf{+{3S zeNWGmv(DaU?|1FB*Is+=Kj+NJYhV1&mxlxb!M=e&d7nTaa5&BpS%E-h`3VRG0>|JC z<18*N8huIV+R*qbcsKP3rbaWy$fpqn6c{Z0texQNOBYxm(E&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--xiKL9~HOIy(4V?mC@lKGfnDt zz}y(|H?7TTm&;eA-Mi6G6Xe}sraCO%CEUz7tw~rXy$*X&9(4P=n6%|Q*>Ip<(#!e! zq~ksm!{p^CR=@0&zL2xUEB5)89M7M1Kql=j#ILP~!rdPRHM@ z`7%#RL{BeQ`AUu+&+NZ`q~u6%7Xw|GbYBA21ziWX)XdvQg?2BMFe#Bw8i`-Q&fjC$ zVC00gB5Ihb*v<@BCA{iU0V68;TG^~63aLTDwxbqr=pTH82e;t-`xmw?Scsx*i~6bX z`FSPKq6}bF@kYVGsm;UH0;u%!J5Ak*I*Yt2X(|4+)iR1S0@;xi-73sameF}Bwh$<$P@lJ(FY77z6p%5 zmv@+!LsZ8^8}7HEt6Myn4uhS36d5~ z#exhwR6Z42W*{mVNq=R&p)cFI_K -#define SERIAL_LISTENER_TEST true - // OMG this is so nasty... #define private public #define protected public -#include "mdc2250/serial_listener.h" +#include "serial/serial_listener.h" using namespace serial; static size_t global_count, global_listen_count; @@ -26,8 +24,8 @@ protected: listener.default_handler = default_handler; } - void execute_lookForOnce() { - listener.listenForOnce("?$1E", 1000); + void execute_listenForStringOnce() { + listener.listenForStringOnce("?$1E", 1000); } SerialListener listener; @@ -37,6 +35,8 @@ protected: TEST_F(SerialListenerTests, ignoresEmptyString) { global_count = 0; + listener.listenOnce(""); + boost::this_thread::sleep(boost::posix_time::milliseconds(11)); listener.listenOnce(""); ASSERT_TRUE(global_count == 0); @@ -46,6 +46,8 @@ TEST_F(SerialListenerTests, ignoresPartialMessage) { global_count = 0; listener.listenOnce("?$1E\r$1E=Robo"); + boost::this_thread::sleep(boost::posix_time::milliseconds(11)); + listener.listenOnce(""); ASSERT_EQ(global_count, 1); } @@ -54,11 +56,13 @@ TEST_F(SerialListenerTests, listenForOnceWorks) { global_count = 0; boost::thread t( - boost::bind(&SerialListenerTests::execute_lookForOnce, this)); + boost::bind(&SerialListenerTests::execute_listenForStringOnce, this)); boost::this_thread::sleep(boost::posix_time::milliseconds(100)); listener.listenOnce("\r+\r?$1E\r$1E=Robo"); + boost::this_thread::sleep(boost::posix_time::milliseconds(11)); + listener.listenOnce(""); ASSERT_TRUE(t.timed_join(boost::posix_time::milliseconds(1500))); @@ -74,11 +78,13 @@ TEST_F(SerialListenerTests, listenForOnceTimesout) { global_count = 0; boost::thread t( - boost::bind(&SerialListenerTests::execute_lookForOnce, this)); + boost::bind(&SerialListenerTests::execute_listenForStringOnce, this)); boost::this_thread::sleep(boost::posix_time::milliseconds(100)); listener.listenOnce("\r+\r?$1ENOTRIGHT\r$1E=Robo"); + boost::this_thread::sleep(boost::posix_time::milliseconds(11)); + listener.listenOnce(""); ASSERT_TRUE(t.timed_join(boost::posix_time::milliseconds(1500))); @@ -104,6 +110,8 @@ TEST_F(SerialListenerTests, listenForWorks) { listener.listenFor(listenForComparator, listenForCallback); listener.listenOnce("\r+\rV=05:06\r?$1E\rV=06:05\r$1E=Robo"); + boost::this_thread::sleep(boost::posix_time::milliseconds(11)); + listener.listenOnce(""); ASSERT_EQ(global_count, 2); ASSERT_EQ(global_listen_count, 2); From 9824eb1d4c2fe0dd0f699d7bae52fa1389bb7491 Mon Sep 17 00:00:00 2001 From: William Woodall Date: Sat, 7 Jan 2012 16:53:37 -0600 Subject: [PATCH 03/72] Cleaning up binary inclusion. --- .gitignore | 1 + include/serial/serial_listener.h | 9 --------- src/serial_listener.cc | 3 ++- tests/proof_of_concepts/tokenizer.cc.out | Bin 203304 -> 0 bytes 4 files changed, 3 insertions(+), 10 deletions(-) delete mode 100755 tests/proof_of_concepts/tokenizer.cc.out diff --git a/.gitignore b/.gitignore index f6cd356..a3ee3d4 100644 --- a/.gitignore +++ b/.gitignore @@ -12,6 +12,7 @@ */tmp/* *.hwm* *.cfg +*.out .svn build bin diff --git a/include/serial/serial_listener.h b/include/serial/serial_listener.h index 23ca918..43a3fff 100644 --- a/include/serial/serial_listener.h +++ b/include/serial/serial_listener.h @@ -140,15 +140,6 @@ _delimeter_tokenizer (std::string &data, std::vector &tokens, * * \see SerialListener::setTokenizer, serial::TokenizerType */ -class delimeter_tokenizer -{ -public: - delimeter_tokenizer (); - virtual ~delimeter_tokenizer (); - -private: - /* data */ -}; TokenizerType delimeter_tokenizer (std::string delimeter); diff --git a/src/serial_listener.cc b/src/serial_listener.cc index 32994f9..095db07 100644 --- a/src/serial_listener.cc +++ b/src/serial_listener.cc @@ -31,7 +31,8 @@ _delimeter_tokenizer (std::string &data, std::vector &tokens, TokenizerType delimeter_tokenizer (std::string delimeter) { - return boost::bind(_delimeter_tokenizer, _1, _2, delimeter); + TokenizerType temp = boost::bind(_delimeter_tokenizer, _1, _2, delimeter); + return temp; } /***** Listener Class Functions *****/ diff --git a/tests/proof_of_concepts/tokenizer.cc.out b/tests/proof_of_concepts/tokenizer.cc.out deleted file mode 100755 index 1c5193737f66e7eb47d4aee41dbb363bf26f496c..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 203304 zcmeGF4|r77wFZpO0BW?vM2i+J)o9UzMU9FImTFK&M3FIa>W5{e2MAkj>Q z(NsaHa&6jDrHYzb)Id>#QYN)jqnE3>mTFY0Cr+--wNz6}<$K??*FJM*G9fwf{+{3S zeNWGmv(DaU?|1FB*Is+=Kj+NJYhV1&mxlxb!M=e&d7nTaa5&BpS%E-h`3VRG0>|JC z<18*N8huIV+R*qbcsKP3rbaWy$fpqn6c{Z0texQNOBYxm(E&J zsu5h$`Q_)LGPoc8mC3_+zhrhs%DvIz;u#C(FDeC{Hjnqy`88}d{9aV?7(Xz-MWtoa z7r8~G^DFS5 zz%Rw_?0vl!7vH$R(=9sj8=~^nXnw}M^`yQ1huRRc6c^8*SG?$!o2M_BS6ouMuoJ&} z<)>y6U_71QQz`uBO*;_3M&;-9Hv%&5FqnUz{rA7i;KrzI_~n&m`klsE>b&gQV9^}G2|69K)91~de)`N?=FLQ$$2W0O zj`NNx92*mug^x{rNF6u3} zs87H_oBjLvw+8;Lfq!e@-x~P02L7#q|LH}b{Gmx9~g z?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!J5Ak*I*Yt2X(|4+)iR1S0@;xi-73sameF}Bwh$<$P@lJ(FY77z6p%5 zmv@+!LsZ8^8}7HEt6Myn4uhS36d5~ z#exhwR6Z42W*{mVNq=R&p)cFI_K Date: Sat, 7 Jan 2012 23:05:38 -0600 Subject: [PATCH 04/72] Fixing to remove once type functions and re-implement them --- include/serial/serial_listener.h | 160 ++++++++++-- serial.cmake | 1 + src/serial_listener.cc | 418 ++++++++++++++++++++----------- tests/serial_listener_tests.cc | 73 ++++-- 4 files changed, 453 insertions(+), 199 deletions(-) diff --git a/include/serial/serial_listener.h b/include/serial/serial_listener.h index 43a3fff..e6ee241 100644 --- a/include/serial/serial_listener.h +++ b/include/serial/serial_listener.h @@ -36,6 +36,9 @@ #ifndef SERIAL_LISTENER_H #define SERIAL_LISTENER_H +// STL +#include + // Serial #include @@ -123,26 +126,6 @@ TokenizerType; /*! This is a convenience alias for boost::uuids::uuid. */ typedef boost::uuids::uuid uuid_type; // uuid_t is already taken! =( -void -_delimeter_tokenizer (std::string &data, std::vector &tokens, - std::string delimeter); - -/*! - * This returns a tokenizer that splits on a given delimeter. - * - * The delimeter is passed into the function and a TokenizerType is returned - * that can be passed to SerialListener::setTokenizer. - * - * Example: - *
- *   my_listener.setTokenizer(delimeter_tokenizer("\r"));
- * <\pre>
- * 
- * \see SerialListener::setTokenizer, serial::TokenizerType
- */
-TokenizerType
-delimeter_tokenizer (std::string delimeter);
-
 /*!
  * This is a general exception generated by the SerialListener class.
  * 
@@ -163,6 +146,87 @@ public:
   }
 };
 
+// Based on: http://www.justsoftwaresolutions.co.uk/threading/implementing-a-thread-safe-queue-using-condition-variables.html
+template
+class ConcurrentQueue
+{
+private:
+  std::queue the_queue;
+  mutable boost::mutex the_mutex;
+  boost::condition_variable the_condition_variable;
+public:
+  void push(Data const& data) {
+    boost::mutex::scoped_lock lock(the_mutex);
+    the_queue.push(data);
+    lock.unlock();
+    the_condition_variable.notify_one();
+  }
+
+  bool empty() const {
+    boost::mutex::scoped_lock lock(the_mutex);
+    return the_queue.empty();
+  }
+
+  bool try_pop(Data& popped_value) {
+    boost::mutex::scoped_lock lock(the_mutex);
+    if(the_queue.empty()) {
+      return false;
+    }
+
+    popped_value=the_queue.front();
+    the_queue.pop();
+    return true;
+  }
+
+  bool timed_wait_and_pop(Data& popped_value, size_t timeout) {
+    using namespace boost::posix_time;
+    bool result;
+    boost::mutex::scoped_lock lock(the_mutex);
+    result = !the_queue.empty();
+    if (!result) {
+      result = the_condition_variable.timed_wait(lock, milliseconds(timeout));
+    }
+
+    if (result) {
+      popped_value=the_queue.front();
+      the_queue.pop();
+    }
+    return result;
+  }
+
+  void wait_and_pop(Data& popped_value) {
+    boost::mutex::scoped_lock lock(the_mutex);
+    while(the_queue.empty()) {
+      the_condition_variable.wait(lock);
+    }
+
+    popped_value=the_queue.front();
+    the_queue.pop();
+  }
+  
+  size_t size() const {
+    return the_queue.size();
+  }
+  
+  void cancel() {
+    the_condition_variable.notify_one();
+  }
+
+  void clear() {
+    boost::mutex::scoped_lock lock(the_mutex);
+    while (!the_queue.empty()) {
+      the_queue.pop();
+    }
+  }
+};
+
+
+namespace filter_type {
+typedef enum {
+  nonblocking, blocking
+} FilterType;
+}
+
 /*!
  * Listens to a serial port, facilitates asynchronous reading
  */
@@ -381,15 +445,52 @@ public:
     this->warn = warning_handler;
   }
 
+/***** Static Functions ******/
+
+  /*!
+   * This returns a tokenizer that splits on a given delimeter.
+   * 
+   * The delimeter is passed into the function and a TokenizerType is returned 
+   * that can be passed to SerialListener::setTokenizer.
+   * 
+   * Example:
+   * 
+   *   my_listener.setTokenizer(delimeter_tokenizer("\r"));
+   * <\pre>
+   * 
+   * \see SerialListener::setTokenizer, serial::TokenizerType
+   */
+  static TokenizerType
+  delimeter_tokenizer (std::string delimeter);
+
+  // tokenizer functions
+  static void
+  _delimeter_tokenizer (std::string &data, std::vector &tokens,
+                        std::string delimeter);
+
 private:
+  // Gets some data from the serial port
+  void readSomeData (std::string&, size_t);
+  // Takes newly tokenized data and processes them
+  void addNewTokens(std::vector &new_tokens,
+                    std::vector new_uuids,
+                    std::string &left_overs);
+  // Runs the new tokens through the filters
+  void filter (std::vector new_uuids);
   // Function that loops while listening is true
   void listen ();
-  // Called by listen iteratively
-  std::string listenOnce (std::string data);
+  // Target of callback thread
+  void callback ();
+  // Prune old tokens
+  void pruneTokens ();
+  // Erases one token
+  void eraseToken (uuid_type&);
+  // Erases several tokens
+  void eraseTokens (std::vector&);
   // Determines how much to read on each loop of listen
   size_t determineAmountToRead ();
   // Used in the look for string once function
-  bool listenForOnceComparator(std::string line);
+  bool listenForOnceComparator (std::string line);
 
   // Tokenizer
   TokenizerType tokenize;
@@ -409,10 +510,16 @@ private:
   bool listening;
   serial::Serial * serial_port;
   boost::thread listen_thread;
-  std::string buffer;
-  std::map lines;
+  std::string data_buffer;
+  boost::mutex token_mux;
+  std::map tokens;
   std::map ttls;
 
+  // Callback related variables
+  // uuid and true for default handler, false for normal callback
+  ConcurrentQueue > callback_queue;
+  boost::thread callback_thread;
+
   // For generating random uuids
   boost::uuids::random_generator random_generator;
 
@@ -420,7 +527,7 @@ private:
   boost::posix_time::time_duration ttl;
 
   // map
-  std::map filters;
+  std::map filters;
   // map
   std::map comparators;
   // map
@@ -429,6 +536,7 @@ private:
   std::map condition_vars;
   // Mutex for locking use of filters
   boost::mutex filter_mux;
+  boost::mutex callback_mux;
 
   // Used as temporary storage for listenForStringOnce
   std::string current_listen_for_one_target;
diff --git a/serial.cmake b/serial.cmake
index 6baec63..94a9fce 100644
--- a/serial.cmake
+++ b/serial.cmake
@@ -13,6 +13,7 @@ project(Serial)
 # Use clang if available
 IF(EXISTS /usr/bin/clang)
   set(CMAKE_CXX_COMPILER /usr/bin/clang++)
+  set(CMAKE_CXX_FLAGS -ferror-limit=5)
 ENDIF(EXISTS /usr/bin/clang)
 
 option(SERIAL_BUILD_TESTS "Build all of the Serial tests." OFF)
diff --git a/src/serial_listener.cc b/src/serial_listener.cc
index 095db07..c3becd0 100644
--- a/src/serial_listener.cc
+++ b/src/serial_listener.cc
@@ -22,19 +22,6 @@ inline void defaultExceptionCallback(const std::exception &error) {
 
 using namespace serial;
 
-void
-_delimeter_tokenizer (std::string &data, std::vector &tokens,
-                      std::string delimeter)
-{
-  boost::split(tokens, data, boost::is_any_of(delimeter));
-}
-
-TokenizerType
-delimeter_tokenizer (std::string delimeter) {
-  TokenizerType temp = boost::bind(_delimeter_tokenizer, _1, _2, delimeter);
-  return temp;
-}
-
 /***** Listener Class Functions *****/
 
 SerialListener::SerialListener() : listening(false) {
@@ -53,7 +40,47 @@ SerialListener::SerialListener() : listening(false) {
 }
 
 SerialListener::~SerialListener() {
-  
+  if (this->listening) {
+    this->stopListening();
+  }
+}
+
+void
+SerialListener::callback() {
+  try {
+    std::pair pair;
+    DataCallback _callback;
+    while (this->listening) {
+      if (this->callback_queue.timed_wait_and_pop(pair, 10)) {
+        if (this->listening) {
+          std::cout << "After pop (" << pair.second << "): ";
+          std::cout << this->tokens[pair.first] << std::endl;
+          try {
+            // If default handler
+            if (pair.second) {
+              if (this->default_handler)
+                this->default_handler(this->tokens[pair.first]);
+            // Else use provided callback
+            } else {
+              // Grab the callback as to not hold the mutex while executing
+              {
+                boost::mutex::scoped_lock l(callback_mux);
+                _callback = this->callbacks[pair.first];
+              }
+              // Execute callback
+              _callback(this->tokens[pair.first]);
+            }
+          } catch (std::exception &e) {
+            this->handle_exc(e);
+          }// try callback
+        } // if listening
+        // Erase the used and executed callback
+        this->eraseToken(pair.first);
+      } // if popped
+    } // while (this->listening)
+  } catch (std::exception &e) {
+    this->handle_exc(SerialListenerException(e.what()));
+  }
 }
 
 void
@@ -77,15 +104,23 @@ SerialListener::startListening(Serial * serial_port) {
   }
   
   listen_thread = boost::thread(boost::bind(&SerialListener::listen, this));
+  
+  // Start the callback thread
+  callback_thread =
+   boost::thread(boost::bind(&SerialListener::callback, this));
 }
 
 void
 SerialListener::stopListening() {
   // Stop listening and clear buffers
   listening = false;
+
   listen_thread.join();
-  this->buffer = "";
-  this->lines.clear();
+  callback_thread.join();
+
+  callback_queue.clear();
+  this->data_buffer = "";
+  this->tokens.clear();
   this->ttls.clear();
   this->serial_port = NULL;
 
@@ -96,136 +131,11 @@ SerialListener::stopListening() {
 void
 SerialListener::stopListeningForAll() {
   boost::mutex::scoped_lock l(filter_mux);
+  boost::mutex::scoped_lock l2(callback_mux);
   filters.clear();
   comparators.clear();
-  callbacks.clear();
   condition_vars.clear();
-}
-
-void
-SerialListener::listen() {
-  // Make sure there is a serial port
-  if (this->serial_port == NULL) {
-    this->handle_exc(SerialListenerException("Invalid serial port."));
-  }
-  // Make sure the serial port is open
-  if (!this->serial_port->isOpen()) {
-    this->handle_exc(SerialListenerException("Serial port not open."));
-  }
-  try {
-    while (this->listening) {
-      // Determine how much to read in
-      size_t amount_to_read = determineAmountToRead();
-      // Read some
-      std::string temp = this->serial_port->read(amount_to_read);
-      // If nothing was read and there is nothing in the lines, then we
-      //  don't need to interate through the filters
-      if (temp.length() == 0 && lines.size() == 0) {
-        continue;
-      }
-      // Add the new data to the buffer
-      this->buffer += temp;
-      // If there is no return carrage in the buffer, then a command hasn't 
-      //  been completed and if there is no data in the lines buffer, then 
-      //  continue.
-      if (this->buffer.find("\r") == std::string::npos && lines.size() == 0) {
-        continue;
-      }
-      // Listen once, this parses the buffer and filters the data in lines
-      buffer = this->listenOnce(buffer);
-      // Done parsing lines and buffer should now be set to the left overs
-    } // while (this->listening)
-  } catch (std::exception &e) {
-    this->handle_exc(SerialListenerException(e.what()));
-  }
-}
-
-// TODO: as it is, each line is passed to filters repeatedly until they are 
-//       too old...  Change it to only send each line to each filter once and 
-//       then send to new fitlers up until it is too old.
-std::string
-SerialListener::listenOnce(std::string data) {
-  std::string left_overs;
-  std::vector to_be_erased;
-  // Tokenize the new data
-  std::vector new_lines;
-  tokenize(data, new_lines);
-  // Iterate through new lines and add times to them
-  std::vector::iterator it_new;
-  for(it_new=new_lines.begin(); it_new != new_lines.end(); it_new++) {
-    // The last line needs to be put back in the buffer always:
-    //  In the case that the string ends with \r the last element will be 
-    //  empty ("").  In the case that it does not the last element will be 
-    //  what is left over from the next message that hasn't sent 
-    //  everything.  Ex.: "?$1E\r" -> ["?$1E", ""] and 
-    //  "?$1E\r$1E=Robo" -> ["?$1E","$1E=Robo"]
-    if (it_new == new_lines.end()-1) {
-      left_overs = (*it_new);
-      continue;
-    }
-    uuid_type uuid = random_generator();
-    lines.insert(std::pair(uuid,(*it_new)));
-    using namespace boost::posix_time;
-    ttls.insert(std::pair
-                  (uuid,ptime(microsec_clock::local_time())));
-  }
-  // Iterate through the lines checking for a match
-  std::map::iterator it_lines;
-  for(it_lines=lines.begin(); it_lines!=lines.end(); it_lines++) {
-    std::string line = (*it_lines).second;
-    uuid_type uuid = (*it_lines).first;
-    // If the line is empty, continue
-    if (line.length() == 0) {
-      continue;
-    }
-    bool matched = false;
-    bool erased = false;
-    // Get the filter lock
-    boost::mutex::scoped_lock l(filter_mux);
-    // Iterate through each filter
-    std::map::iterator it;
-    for(it=filters.begin(); it!=filters.end(); it++) {
-      if (comparators[(*it).first](line)) { // If comparator matches line
-        if ((*it).second == "non-blocking") {
-// TODO: Put this callback execution into a queue. And if I do, make sure to 
-//        keep the line instance around until the callback is done...
-          // If non-blocking run the callback
-          callbacks[(*it).first](line);
-          to_be_erased.push_back(uuid);
-          erased = true;
-        } else if ((*it).second == "blocking") {
-          // If blocking then notify the waiting call to continue
-          condition_vars[(*it).first]->notify_all();
-          to_be_erased.push_back(uuid);
-          erased = true;
-        }
-        matched = true;
-        break; // It matched, continue to next line
-      }
-    } // for(it=filters.begin(); it!=filters.end(); it++)
-    // If not already erased check how old it is, remove the too old
-    if (!erased) {
-      using namespace boost::posix_time;
-      if (ptime(microsec_clock::local_time())-ttls[uuid] > ttl) {
-        // If there is a default handler pass it on
-        if (this->default_handler) {
-// TODO: see above about callback execution queue
-          this->default_handler(line);
-        }
-        to_be_erased.push_back(uuid);
-      }
-    }
-  } // for(it_lines=lines.begin(); it_lines!=lines.end(); it_lines++)
-  // Remove any lines that need to be erased
-  //  (this must be done outside the iterator to prevent problems incrementing 
-  //   the iterator)
-  std::vector::iterator it;
-  for (it=to_be_erased.begin(); it != to_be_erased.end(); it++) {
-    lines.erase((*it));
-    ttls.erase((*it));
-  }
-  // Return the left_overs
-  return left_overs;
+  callbacks.clear();
 }
 
 size_t
@@ -236,9 +146,201 @@ SerialListener::determineAmountToRead() {
   return 5;
 }
 
+void
+SerialListener::readSomeData(std::string &temp, size_t this_many) {
+  // Make sure there is a serial port
+  if (this->serial_port == NULL) {
+    this->handle_exc(SerialListenerException("Invalid serial port."));
+  }
+  // Make sure the serial port is open
+  if (!this->serial_port->isOpen()) {
+    this->handle_exc(SerialListenerException("Serial port not open."));
+  }
+  temp = this->serial_port->read(this_many);
+}
+
+void
+SerialListener::addNewTokens(std::vector &new_tokens,
+                             std::vector new_uuids,
+                             std::string &left_overs)
+{
+  std::cout << "Inside SerialListener::addNewTokens:" << std::endl;
+  // Iterate through new tokens and add times to them
+  std::vector::iterator it_new;
+  for (it_new=new_tokens.begin(); it_new != new_tokens.end(); it_new++) {
+    std::cout << "  Token (" << (*it_new).length() << "): " << (*it_new) << std::endl;
+    // The last token needs to be put back in the buffer always:
+    //  (in the case that the delimeter is \r)...
+    //  In the case that the string ends with \r the last element will be 
+    //  empty ("").  In the case that it does not the last element will be 
+    //  what is left over from the next message that hasn't sent 
+    //  everything.  Ex.: "?$1E\r" -> ["?$1E", ""] and 
+    //  "?$1E\r$1E=Robo" -> ["?$1E","$1E=Robo"]
+    if (it_new == new_tokens.end()-1) {
+      left_overs = (*it_new);
+      continue;
+    }
+    // Create a new uuid
+    uuid_type uuid = random_generator();
+    // Put the new uuid in the list of new uuids
+    new_uuids.push_back(uuid);
+    // Create a uuid, token pair
+    std::pair token_pair(uuid,(*it_new));
+    // Create a uuid, ttl pair
+    using namespace boost::posix_time;
+    std::pair 
+     ttl_pair(uuid,ptime(microsec_clock::local_time()));
+    // Insert the new pairs
+    {
+      boost::mutex::scoped_lock l(token_mux);
+      this->tokens.insert(token_pair);
+      ttls.insert(ttl_pair);
+    }
+  } // for (it_new=new_tokens.begin(); it_new != new_tokens.end(); it_new++)
+}
+
+void
+SerialListener::eraseToken(uuid_type &uuid) {
+  boost::mutex::scoped_lock l(token_mux);
+  this->tokens.erase(uuid);
+  this->ttls.erase(uuid);
+}
+
+void
+SerialListener::eraseTokens(std::vector &uuids) {
+  std::vector::iterator it;
+  for (it=uuids.begin(); it != uuids.end(); it++) {
+    this->eraseToken((*it));
+  }
+}
+
+// TODO: Investigate possible race condition where the filter processing takes 
+//  longer than the ttl
+void
+SerialListener::filterNewTokens(std::vector new_uuids) {
+  // Iterate through the filters, checking each against new tokens
+  boost::mutex::scoped_lock l(filter_mux);
+  std::map::iterator it;
+  for (it=filters.begin(); it!=filters.end(); it++) {
+    this->filter((*it).first, new_uuids);
+  } // for (it=filters.begin(); it!=filters.end(); it++)
+  // Get the filter lock
+  boost::mutex::scoped_lock l(filter_mux);
+  std::vector to_be_erased;
+  // Iterate through the tokens checking for a match
+  std::vector::iterator it_uuids;
+  for (it_uuids=new_uuids.begin(); it_uuids!=new_uuids.end(); it_uuids++) {
+    bool matched = false;
+    uuid_type uuid = (*it_uuids);
+    // If the line is empty, continue
+    if (tokens[uuid].length() == 0) {
+      continue;
+    }
+    // Iterate through each filter
+    std::map::iterator it;
+    for (it=filters.begin(); it!=filters.end(); it++) {
+      // If comparator matches line
+      if (comparators[(*it).first](tokens[uuid])) {
+        // If non-blocking run the callback
+        if ((*it).second == filter_type::nonblocking) {
+          callback_queue.push(std::pair(uuid,false));
+        // If blocking then notify the waiting call to continue
+        } else if ((*it).second == filter_type::blocking) {
+          condition_vars[(*it).first]->notify_all();
+          to_be_erased.push_back(uuid);
+        }
+        matched = true;
+        break; // It matched, continue to next line
+      }
+    } // for(it=filters.begin(); it!=filters.end(); it++)
+  } // for(it_lines=lines.begin(); it_lines!=lines.end(); it_lines++)
+  // Remove any lines that need to be erased
+  //  (this must be done outside the iterator to prevent problems incrementing
+  //   the iterator)
+  this->eraseTokens(to_be_erased);
+}
+
+void
+filter(uuid_type filter_uuid, std::vector token_uuids) {
+  std::vector to_be_erased;
+  // Iterate through the token uuids and run each against the filter
+  std::vector::iterator it_uuids;
+  for (it_uuids=new_uuids.begin(); it_uuids!=new_uuids.end(); it_uuids++) {
+    
+  }
+  // Remove any lines that need to be erased
+  //  (this must be done outside the iterator to prevent problems incrementing
+  //   the iterator)
+  this->eraseTokens(to_be_erased);
+}
+
+void
+SerialListener::pruneTokens() {
+  // Iterate through the buffered tokens
+  std::vector to_be_erased;
+  std::map::iterator it;
+
+  {
+    boost::mutex::scoped_lock l(token_mux);
+    for (it = this->tokens.begin(); it != this->tokens.end(); it++) {
+      uuid_type uuid = (*it).first;
+      using namespace boost::posix_time;
+      // If the current time - the creation time is greater than the ttl, 
+      //  then prune it
+      if (ptime(microsec_clock::local_time())-this->ttls[uuid] > this->ttl) {
+        std::cout << "Pruning (" << this->tokens[uuid].length();
+        std::cout << "): " << this->tokens[uuid] << std::endl;
+        // If there is a default handler pass it on
+        if (this->default_handler) {
+          boost::mutex::scoped_lock l(callback_mux);
+          callback_queue.push(std::pair(uuid,true));
+        } else {
+          // Otherwise delete it
+          to_be_erased.push_back(uuid);
+        }
+      }
+    }
+  }
+  // Remove any lines that need to be erased
+  //  (this must be done outside the iterator to prevent problems incrementing
+  //   the iterator)
+  this->eraseTokens(to_be_erased);
+}
+
+void
+SerialListener::listen() {
+  try {
+    while (this->listening) {
+      // Read some data
+      std::string temp;
+      this->readSomeData(temp, determineAmountToRead());
+      // If nothing was read then we
+      //  don't need to iterate through the filters
+      if (temp.length() != 0) {
+        // Add the new data to the buffer
+        this->data_buffer += temp;
+        // Call the tokenizer on the updated buffer
+        std::vector new_tokens;
+        this->tokenize(this->data_buffer, new_tokens);
+        // Add the new tokens to the new token buffer, get a list of new uuids 
+        //  to filter once, and put left_overs in the data buffer.
+        std::vector new_uuids;
+        this->addNewTokens(new_tokens, new_uuids, this->data_buffer);
+        // Run the new tokens through existing filters
+        this->filterNewTokens(new_uuids);
+      }
+      // Look for old data and pass to the default handler or delete
+      this->pruneTokens();
+      // Done parsing lines and buffer should now be set to the left overs
+    } // while (this->listening)
+  } catch (std::exception &e) {
+    this->handle_exc(SerialListenerException(e.what()));
+  }
+}
+
 bool
-SerialListener::listenForOnceComparator(std::string line) {
-  if (line == current_listen_for_one_target)
+SerialListener::listenForOnceComparator(std::string token) {
+  if (token == current_listen_for_one_target)
     return true;
   return false;
 }
@@ -251,8 +353,8 @@ SerialListener::listenForStringOnce(std::string token, size_t milliseconds) {
 
   // Create blocking filter
   uuid_type uuid = random_generator();
-  std::pair
-   filter_pair(uuid, "blocking");
+  std::pair
+   filter_pair(uuid, filter_type::blocking);
   std::pair
    comparator_pair(uuid,
     boost::bind(&SerialListener::listenForOnceComparator, this, _1));
@@ -265,6 +367,8 @@ SerialListener::listenForStringOnce(std::string token, size_t milliseconds) {
     condition_vars.insert(condition_pair);
   }
 
+  this->processNewFilter(uuid);
+
   bool result = false;
 
   // Wait
@@ -288,8 +392,8 @@ SerialListener::listenFor(ComparatorType comparator, DataCallback callback)
 {
   // Create Filter
   uuid_type uuid = random_generator();
-  std::pair
-   filter_pair(uuid, "non-blocking");
+  std::pair
+   filter_pair(uuid, filter_type::nonblocking);
   std::pair
    comparator_pair(uuid, comparator);
   std::pair
@@ -299,6 +403,9 @@ SerialListener::listenFor(ComparatorType comparator, DataCallback callback)
     boost::mutex::scoped_lock l(filter_mux);
     filters.insert(filter_pair);
     comparators.insert(comparator_pair);
+  }
+  {
+    boost::mutex::scoped_lock l(callback_mux);
     callbacks.insert(callback_pair);
   }
 
@@ -314,4 +421,17 @@ SerialListener::stopListeningFor(uuid_type filter_uuid) {
   callbacks.erase(filter_uuid);
 }
 
+TokenizerType
+SerialListener::delimeter_tokenizer (std::string delimeter) {
+  return boost::bind(&SerialListener::_delimeter_tokenizer,
+                     _1, _2, delimeter);
+}
+
+void
+SerialListener::_delimeter_tokenizer (std::string &data,
+                                      std::vector &tokens,
+                                      std::string delimeter)
+{
+  boost::split(tokens, data, boost::is_any_of(delimeter));
+}
 
diff --git a/tests/serial_listener_tests.cc b/tests/serial_listener_tests.cc
index 4299a9b..f589b04 100644
--- a/tests/serial_listener_tests.cc
+++ b/tests/serial_listener_tests.cc
@@ -13,7 +13,7 @@ static size_t global_count, global_listen_count;
 
 void default_handler(std::string line) {
   global_count++;
-  // std::cout << "default_handler got: " << line << std::endl;
+  std::cout << "default_handler got: " << line << std::endl;
 }
 
 namespace {
@@ -21,33 +21,55 @@ namespace {
 class SerialListenerTests : public ::testing::Test {
 protected:
   virtual void SetUp() {
+    listener.listening = true;
+    listener.setTimeToLive(10);
     listener.default_handler = default_handler;
+    listener.callback_thread =
+     boost::thread(boost::bind(&SerialListener::callback, &listener));
+  }
+
+  virtual void TearDown() {
+    listener.listening = false;
+    listener.callback_thread.join();
+  }
+
+  void stopCallbackThread() {
+    while (true) {
+      boost::this_thread::sleep(boost::posix_time::milliseconds(1));
+      boost::mutex::scoped_lock lock(listener.callback_queue.the_mutex);
+      if (listener.callback_queue.the_queue.empty())
+        break;
+    }
+    listener.listening = false;
+    listener.callback_thread.join();
   }
 
   void execute_listenForStringOnce() {
     listener.listenForStringOnce("?$1E", 1000);
   }
 
+  void simulate_loop(std::string input_str) {
+    std::vector new_tokens;
+    listener.tokenize(input_str, new_tokens);
+    std::vector new_uuids;
+    listener.addNewTokens(new_tokens, new_uuids, listener.data_buffer);
+    listener.filterNewTokens(new_uuids);
+    boost::this_thread::sleep(boost::posix_time::milliseconds(11));
+    listener.pruneTokens();
+  }
+
   SerialListener listener;
 
 };
 
-TEST_F(SerialListenerTests, ignoresEmptyString) {
+TEST_F(SerialListenerTests, handlesPartialMessage) {
   global_count = 0;
+  std::string input_str = "?$1E\r$1E=Robo";
 
-  listener.listenOnce("");
-  boost::this_thread::sleep(boost::posix_time::milliseconds(11));
-  listener.listenOnce("");
+  simulate_loop(input_str);
 
-  ASSERT_TRUE(global_count == 0);
-}
-
-TEST_F(SerialListenerTests, ignoresPartialMessage) {
-  global_count = 0;
-
-  listener.listenOnce("?$1E\r$1E=Robo");
-  boost::this_thread::sleep(boost::posix_time::milliseconds(11));
-  listener.listenOnce("");
+  // give some time for the callback thread to finish
+  stopCallbackThread();
 
   ASSERT_EQ(global_count, 1);
 }
@@ -60,16 +82,17 @@ TEST_F(SerialListenerTests, listenForOnceWorks) {
 
   boost::this_thread::sleep(boost::posix_time::milliseconds(100));
 
-  listener.listenOnce("\r+\r?$1E\r$1E=Robo");
-  boost::this_thread::sleep(boost::posix_time::milliseconds(11));
-  listener.listenOnce("");
+  simulate_loop("\r+\r?$1E\r$1E=Robo");
 
   ASSERT_TRUE(t.timed_join(boost::posix_time::milliseconds(1500)));
 
   // Make sure the filters are getting deleted
   ASSERT_EQ(listener.filters.size(), 0);
 
-  ASSERT_EQ(global_count, 1);
+  // give some time for the callback thread to finish
+  stopCallbackThread();
+
+  ASSERT_EQ(global_count, 2);
 }
 
 // lookForOnce should not find it, but timeout after 1000ms, so it should 
@@ -82,12 +105,13 @@ TEST_F(SerialListenerTests, listenForOnceTimesout) {
 
   boost::this_thread::sleep(boost::posix_time::milliseconds(100));
 
-  listener.listenOnce("\r+\r?$1ENOTRIGHT\r$1E=Robo");
-  boost::this_thread::sleep(boost::posix_time::milliseconds(11));
-  listener.listenOnce("");
+  simulate_loop("\r+\r?$1ENOTRIGHT\r$1E=Robo");
 
   ASSERT_TRUE(t.timed_join(boost::posix_time::milliseconds(1500)));
 
+  // give some time for the callback thread to finish
+  stopCallbackThread();
+
   ASSERT_EQ(global_count, 2);
 }
 
@@ -109,9 +133,10 @@ TEST_F(SerialListenerTests, listenForWorks) {
   boost::uuids::uuid filt_uuid = 
     listener.listenFor(listenForComparator, listenForCallback);
 
-  listener.listenOnce("\r+\rV=05:06\r?$1E\rV=06:05\r$1E=Robo");
-  boost::this_thread::sleep(boost::posix_time::milliseconds(11));
-  listener.listenOnce("");
+  simulate_loop("\r+\rV=05:06\r?$1E\rV=06:05\r$1E=Robo");
+
+  // give some time for the callback thread to finish
+  stopCallbackThread();
 
   ASSERT_EQ(global_count, 2);
   ASSERT_EQ(global_listen_count, 2);

From 709fa5e1745a75547a779965c86b9149e2818b7f Mon Sep 17 00:00:00 2001
From: William Woodall 
Date: Mon, 9 Jan 2012 22:25:34 -0600
Subject: [PATCH 05/72] Finished serial_listener.  But serial is seriously
 inefficient, I need to investigate this before moving on.

---
 examples/serial_listener_example.cc |  45 ++++--
 include/serial/serial_listener.h    | 186 +++++++++++++++++++++---
 src/serial.cc                       |   6 +-
 src/serial_listener.cc              | 218 ++++++++++++++++------------
 tests/serial_listener_tests.cc      |  16 +-
 5 files changed, 337 insertions(+), 134 deletions(-)

diff --git a/examples/serial_listener_example.cc b/examples/serial_listener_example.cc
index 83ef3c1..deb0646 100644
--- a/examples/serial_listener_example.cc
+++ b/examples/serial_listener_example.cc
@@ -13,21 +13,16 @@ void callback(std::string line) {
   std::cout << "callback got a: " << line << std::endl;
 }
 
-bool comparator(std::string line) {
-  if (line.substr(0,2) == "V=")
-    return true;
-  return false;
-}
-
+#if 0
 int main(void) {
   Serial serial("/dev/tty.usbmodemfd1231", 115200);
 
   SerialListener listener;
-  // Set the time to live for messages to 1 second
-  listener.setTimeToLive(1000);
-  listener.startListening(&serial);
+  // Set the time to live for messages to 10 milliseconds
+  listener.setTimeToLive(10);
+  listener.startListening(serial);
 
-  listener.listenFor(comparator, callback);
+  listener.listenFor(SerialListener::startsWith("V="), callback);
 
   serial.write("?$1E\r");
   if (!listener.listenForStringOnce("?$1E")) {
@@ -35,12 +30,32 @@ int main(void) {
     return 1;
   }
 
+  serial.write("?V\r");
+  serial.write("# 1\r");
+
+  while (true) {
+    // Sleep 100 ms
+    SerialListener::sleep(100);
+  }
+
 }
+#endif
 
-/*
-TODO:
+int main(void) {
+  Serial serial("/dev/tty.usbmodemfd1231", 115200);
 
-listenForOnce -> listenForStringOnce
-listenForOnce(ComparatorType comparator, std::string& result, size_t timeout)
+  serial.write("?$1E\r");
+  SerialListener::sleep(10);
+  // if ("?$1E\r" != serial.read(5)) {
+  //   std::cerr << "Didn't get conformation of device version!" << std::endl;
+  //   return 1;
+  // }
 
-*/
\ No newline at end of file
+  serial.write("?V\r");
+  serial.write("# 1\r");
+
+  while (true) {
+    std::cout << serial.read(5) << std::endl;
+  }
+
+}
diff --git a/include/serial/serial_listener.h b/include/serial/serial_listener.h
index e6ee241..2204849 100644
--- a/include/serial/serial_listener.h
+++ b/include/serial/serial_listener.h
@@ -280,7 +280,7 @@ public:
    * \param serial_port Pointer to a serial::Serial object that is used to 
    * retrieve new data.
    */
-  void startListening (serial::Serial * serial_port);
+  void startListening (serial::Serial &serial_port);
 
   /*!
    * Stops the listening thread and blocks until it completely stops.
@@ -306,6 +306,20 @@ public:
    */
   bool listenForStringOnce (std::string token, size_t timeout = 1000);
 
+  /*!
+   * Blocks until the comparator returns true or until the timeout occurs.
+   * 
+   * \param comparator ComparatorType function that should return true if the
+   * given std::string matches otherwise false.
+   * 
+   * \param timeout in milliseconds before timing out and returning false.
+   * Defaults to 1000 milliseconds or 1 second.
+   * 
+   * \return bool If true then the token was detected before the token, false 
+   * if the token was not heard and the timeout occured.
+   */
+  bool listenForOnce (ComparatorType comparator, size_t timeout = 1000);
+
   /*!
    * Setups up a filter that calls a callback when a comparator returns true.
    * 
@@ -447,6 +461,16 @@ public:
 
 /***** Static Functions ******/
 
+  /*!
+   * Sleeps for a given number of milliseconds.
+   * 
+   * \param ms number of milliseconds to sleep.
+   */
+  static void
+  sleep (size_t ms) {
+    boost::this_thread::sleep(boost::posix_time::milliseconds(ms));
+  }
+
   /*!
    * This returns a tokenizer that splits on a given delimeter.
    * 
@@ -455,28 +479,155 @@ public:
    * 
    * Example:
    * 
-   *   my_listener.setTokenizer(delimeter_tokenizer("\r"));
+   *   my_listener.setTokenizer(SerialListener::delimeter_tokenizer("\r"));
    * <\pre>
    * 
+   * \param delimeter A std::string that is used as a delimeter when 
+   * tokenizing data.
+   * 
+   * \return TokenizerType A tokenizer function type that can be passed to 
+   * SerialListener::setTokenizer.
+   * 
    * \see SerialListener::setTokenizer, serial::TokenizerType
    */
   static TokenizerType
   delimeter_tokenizer (std::string delimeter);
 
-  // tokenizer functions
+  // delimeter tokenizer function
   static void
   _delimeter_tokenizer (std::string &data, std::vector &tokens,
                         std::string delimeter);
 
+  /*!
+   * This returns a comparator that matches only the exact string given.
+   * 
+   * This can be used with listenFor or listenForOnce:
+   * 
+   * Example:
+   * 
+   *   my_listener.listenFor(SerialListener::exactly("my_string"),
+   *                         my_callback);
+   * <\pre>
+   * 
+   * \param exact_str A std::string that is used as the exact string to match 
+   * when comparing tokens for matching.
+   * 
+   * \return ComparatorType A comparator function type that can be passed to 
+   * SerialListener::listenFor or SerialListener::listenForOnce.
+   *
+   * \see SerialListener::listenFor, SerialListener::listenForOnce, 
+   * serial::ComparatorType
+   */
+  static ComparatorType
+  exactly (std::string exact_str);
+
+  // exact comparator function
+  static bool
+  _exactly (const std::string&, std::string);
+
+  /*!
+   * This returns a comparator that looks for a given prefix.
+   * 
+   * This can be used with listenFor or listenForOnce:
+   * 
+   * Example:
+   * 
+   *   my_listener.listenFor(SerialListener::startsWith("V="), my_callback);
+   * <\pre>
+   * 
+   * \param prefix A std::string that is used as the prefix string to match 
+   * when comparing tokens for matching.
+   * 
+   * \return ComparatorType A comparator function type that can be passed to 
+   * SerialListener::listenFor or SerialListener::listenForOnce.
+   *
+   * \see SerialListener::listenFor, SerialListener::listenForOnce, 
+   * serial::ComparatorType
+   */
+  static ComparatorType
+  startsWith (std::string prefix) {
+    return boost::bind(&SerialListener::_startsWith, _1, prefix);
+  }
+
+  // exact comparator function
+  static bool
+  _startsWith (const std::string& token, std::string prefix) {
+    return token.substr(0,prefix.length()) == prefix;
+  }
+
+  /*!
+   * This returns a comparator that looks for a given postfix.
+   * 
+   * This can be used with listenFor or listenForOnce:
+   * 
+   * Example:
+   * 
+   *   my_listener.listenFor(SerialListener::endsWith(";"), my_callback);
+   * <\pre>
+   * 
+   * \param postfix A std::string that is used as the postfix string to match 
+   * when comparing tokens for matching.
+   * 
+   * \return ComparatorType A comparator function type that can be passed to 
+   * SerialListener::listenFor or SerialListener::listenForOnce.
+   *
+   * \see SerialListener::listenFor, SerialListener::listenForOnce, 
+   * serial::ComparatorType
+   */
+  static ComparatorType
+  endsWith (std::string postfix) {
+    return boost::bind(&SerialListener::_endsWith, _1, postfix);
+  }
+
+  // endswith comparator function
+  static bool
+  _endsWith (const std::string& token, std::string postfix) {
+    return token.substr(token.length()-postfix.length()) == postfix;
+  }
+
+  /*!
+   * This returns a comparator that looks for a given substring in the token.
+   * 
+   * This can be used with listenFor or listenForOnce:
+   * 
+   * Example:
+   * 
+   *   my_listener.listenFor(SerialListener::contains("some string"),
+   *                         my_callback);
+   * <\pre>
+   * 
+   * \param substr A std::string that is used as the search substring to match 
+   * when comparing tokens for matching.
+   * 
+   * \return ComparatorType A comparator function type that can be passed to 
+   * SerialListener::listenFor or SerialListener::listenForOnce.
+   *
+   * \see SerialListener::listenFor, SerialListener::listenForOnce, 
+   * serial::ComparatorType
+   */
+  static ComparatorType
+  contains (std::string substr) {
+    return boost::bind(&SerialListener::_contains, _1, substr);
+  }
+
+  // contains comparator function
+  static bool
+  _contains (const std::string& token, std::string substr) {
+    return token.find(substr) != std::string::npos;
+  }
+
 private:
   // Gets some data from the serial port
   void readSomeData (std::string&, size_t);
   // Takes newly tokenized data and processes them
   void addNewTokens(std::vector &new_tokens,
-                    std::vector new_uuids,
+                    std::vector &new_uuids,
                     std::string &left_overs);
   // Runs the new tokens through the filters
-  void filter (std::vector new_uuids);
+  void filterNewTokens (std::vector new_uuids);
+  // Runs a list of tokens through one filter
+  std::vector >
+  filter(uuid_type filter_uuid, std::vector &token_uuids);
   // Function that loops while listening is true
   void listen ();
   // Target of callback thread
@@ -489,8 +640,9 @@ private:
   void eraseTokens (std::vector&);
   // Determines how much to read on each loop of listen
   size_t determineAmountToRead ();
-  // Used in the look for string once function
-  bool listenForOnceComparator (std::string line);
+  // Hanlder for listen for once
+  typedef boost::shared_ptr shared_cond_var_ptr_t;
+  void notifyListenForOnce (shared_cond_var_ptr_t cond_ptr);
 
   // Tokenizer
   TokenizerType tokenize;
@@ -512,34 +664,30 @@ private:
   boost::thread listen_thread;
   std::string data_buffer;
   boost::mutex token_mux;
-  std::map tokens;
-  std::map ttls;
+  std::map tokens;
+  std::map ttls;
 
   // Callback related variables
-  // uuid and true for default handler, false for normal callback
-  ConcurrentQueue > callback_queue;
+  // filter uuid, token uuid
+  ConcurrentQueue > callback_queue;
   boost::thread callback_thread;
 
   // For generating random uuids
   boost::uuids::random_generator random_generator;
+  boost::uuids::nil_generator nil_generator;
 
   // Setting for ttl on messages
   boost::posix_time::time_duration ttl;
 
   // map
-  std::map filters;
+  std::vector filters;
   // map
-  std::map comparators;
+  std::map comparators;
   // map
-  std::map callbacks;
-  // map
-  std::map condition_vars;
+  std::map callbacks;
   // Mutex for locking use of filters
   boost::mutex filter_mux;
   boost::mutex callback_mux;
-
-  // Used as temporary storage for listenForStringOnce
-  std::string current_listen_for_one_target;
 };
 
 }
diff --git a/src/serial.cc b/src/serial.cc
index ae37ff1..c304ee3 100644
--- a/src/serial.cc
+++ b/src/serial.cc
@@ -170,7 +170,8 @@ void Serial::open() {
     
     // Try to open the serial port
     try {
-        this->serial_port.reset(new boost::asio::serial_port(this->io_service, this->port));
+        this->serial_port.reset(
+              new boost::asio::serial_port(this->io_service, this->port));
         
         this->serial_port->set_option(this->baudrate);
         this->serial_port->set_option(this->flowcontrol);
@@ -199,7 +200,8 @@ void Serial::close() {
     }
 }
 
-static const boost::posix_time::time_duration timeout_zero_comparison(boost::posix_time::milliseconds(0));
+static const boost::posix_time::time_duration 
+timeout_zero_comparison(boost::posix_time::milliseconds(0));
 
 int Serial::read(char* buffer, int size) {
     this->reading = true;
diff --git a/src/serial_listener.cc b/src/serial_listener.cc
index c3becd0..77993f0 100644
--- a/src/serial_listener.cc
+++ b/src/serial_listener.cc
@@ -48,34 +48,37 @@ SerialListener::~SerialListener() {
 void
 SerialListener::callback() {
   try {
-    std::pair pair;
+    // 
+    std::pair pair;
     DataCallback _callback;
     while (this->listening) {
       if (this->callback_queue.timed_wait_and_pop(pair, 10)) {
         if (this->listening) {
-          std::cout << "After pop (" << pair.second << "): ";
-          std::cout << this->tokens[pair.first] << std::endl;
           try {
             // If default handler
-            if (pair.second) {
+            if (pair.first.is_nil()) {
               if (this->default_handler)
-                this->default_handler(this->tokens[pair.first]);
+                this->default_handler(this->tokens[pair.second]);
             // Else use provided callback
             } else {
+              bool go = false;
               // Grab the callback as to not hold the mutex while executing
               {
                 boost::mutex::scoped_lock l(callback_mux);
-                _callback = this->callbacks[pair.first];
+                // Make sure the filter hasn't been removed
+                if ((go = (this->callbacks.count(pair.first) > 0)))
+                  _callback = this->callbacks[pair.first];
               }
               // Execute callback
-              _callback(this->tokens[pair.first]);
+              if (go)
+                _callback(this->tokens[pair.second]);
             }
           } catch (std::exception &e) {
             this->handle_exc(e);
           }// try callback
         } // if listening
         // Erase the used and executed callback
-        this->eraseToken(pair.first);
+        this->eraseToken(pair.second);
       } // if popped
     } // while (this->listening)
   } catch (std::exception &e) {
@@ -90,14 +93,14 @@ SerialListener::setTimeToLive(size_t ms) {
 }
 
 void
-SerialListener::startListening(Serial * serial_port) {
+SerialListener::startListening(Serial &serial_port) {
   if (this->listening) {
     throw(SerialListenerException("Already listening."));
     return;
   }
   this->listening = true;
   
-  this->serial_port = serial_port;
+  this->serial_port = &serial_port;
   if (!this->serial_port->isOpen()) {
     throw(SerialListenerException("Serial port not open."));
     return;
@@ -131,11 +134,11 @@ SerialListener::stopListening() {
 void
 SerialListener::stopListeningForAll() {
   boost::mutex::scoped_lock l(filter_mux);
-  boost::mutex::scoped_lock l2(callback_mux);
   filters.clear();
   comparators.clear();
-  condition_vars.clear();
+  boost::mutex::scoped_lock l2(callback_mux);
   callbacks.clear();
+  callback_queue.clear();
 }
 
 size_t
@@ -143,7 +146,7 @@ SerialListener::determineAmountToRead() {
   // TODO: Make a more intelligent method based on the length of the things 
   //  filters are looking for.  i.e.: if the filter is looking for 'V=XX\r' 
   //  make the read amount at least 5.
-  return 5;
+  return 1024;
 }
 
 void
@@ -161,14 +164,11 @@ SerialListener::readSomeData(std::string &temp, size_t this_many) {
 
 void
 SerialListener::addNewTokens(std::vector &new_tokens,
-                             std::vector new_uuids,
+                             std::vector &new_uuids,
                              std::string &left_overs)
 {
-  std::cout << "Inside SerialListener::addNewTokens:" << std::endl;
-  // Iterate through new tokens and add times to them
   std::vector::iterator it_new;
   for (it_new=new_tokens.begin(); it_new != new_tokens.end(); it_new++) {
-    std::cout << "  Token (" << (*it_new).length() << "): " << (*it_new) << std::endl;
     // The last token needs to be put back in the buffer always:
     //  (in the case that the delimeter is \r)...
     //  In the case that the string ends with \r the last element will be 
@@ -180,6 +180,9 @@ SerialListener::addNewTokens(std::vector &new_tokens,
       left_overs = (*it_new);
       continue;
     }
+    // If the token is empty ignore it
+    if ((*it_new).length() == 0)
+      continue;
     // Create a new uuid
     uuid_type uuid = random_generator();
     // Put the new uuid in the list of new uuids
@@ -219,59 +222,40 @@ SerialListener::eraseTokens(std::vector &uuids) {
 void
 SerialListener::filterNewTokens(std::vector new_uuids) {
   // Iterate through the filters, checking each against new tokens
+  std::vector > tbd;
   boost::mutex::scoped_lock l(filter_mux);
-  std::map::iterator it;
+  std::vector::iterator it;
   for (it=filters.begin(); it!=filters.end(); it++) {
-    this->filter((*it).first, new_uuids);
+    std::vector > temp =
+      this->filter((*it), new_uuids);
+    if (temp.size() > 0)
+      tbd.insert(tbd.end(), temp.begin(), temp.end());
   } // for (it=filters.begin(); it!=filters.end(); it++)
-  // Get the filter lock
-  boost::mutex::scoped_lock l(filter_mux);
-  std::vector to_be_erased;
-  // Iterate through the tokens checking for a match
-  std::vector::iterator it_uuids;
-  for (it_uuids=new_uuids.begin(); it_uuids!=new_uuids.end(); it_uuids++) {
-    bool matched = false;
-    uuid_type uuid = (*it_uuids);
-    // If the line is empty, continue
-    if (tokens[uuid].length() == 0) {
-      continue;
-    }
-    // Iterate through each filter
-    std::map::iterator it;
-    for (it=filters.begin(); it!=filters.end(); it++) {
-      // If comparator matches line
-      if (comparators[(*it).first](tokens[uuid])) {
-        // If non-blocking run the callback
-        if ((*it).second == filter_type::nonblocking) {
-          callback_queue.push(std::pair(uuid,false));
-        // If blocking then notify the waiting call to continue
-        } else if ((*it).second == filter_type::blocking) {
-          condition_vars[(*it).first]->notify_all();
-          to_be_erased.push_back(uuid);
-        }
-        matched = true;
-        break; // It matched, continue to next line
-      }
-    } // for(it=filters.begin(); it!=filters.end(); it++)
-  } // for(it_lines=lines.begin(); it_lines!=lines.end(); it_lines++)
-  // Remove any lines that need to be erased
-  //  (this must be done outside the iterator to prevent problems incrementing
-  //   the iterator)
-  this->eraseTokens(to_be_erased);
+  // Dispatch
+  std::vector >::iterator it_tbd;
+  for (it_tbd = tbd.begin(); it_tbd != tbd.end(); it_tbd++) {
+    callback_queue.push((*it_tbd));
+  }
 }
 
-void
-filter(uuid_type filter_uuid, std::vector token_uuids) {
+// 
+std::vector >
+SerialListener::filter(uuid_type filter_uuid,
+                       std::vector &token_uuids)
+{
   std::vector to_be_erased;
+  std::vector > to_be_dispatched;
   // Iterate through the token uuids and run each against the filter
-  std::vector::iterator it_uuids;
-  for (it_uuids=new_uuids.begin(); it_uuids!=new_uuids.end(); it_uuids++) {
-    
+  std::vector::iterator it;
+  for (it=token_uuids.begin(); it!=token_uuids.end(); it++) {
+    bool matched = false;
+    uuid_type token_uuid = (*it);
+    if (this->comparators[filter_uuid](this->tokens[token_uuid])) {
+      matched = true;
+      to_be_dispatched.push_back(std::make_pair(filter_uuid,token_uuid));
+    }
   }
-  // Remove any lines that need to be erased
-  //  (this must be done outside the iterator to prevent problems incrementing
-  //   the iterator)
-  this->eraseTokens(to_be_erased);
+  return to_be_dispatched;
 }
 
 void
@@ -288,12 +272,10 @@ SerialListener::pruneTokens() {
       // If the current time - the creation time is greater than the ttl, 
       //  then prune it
       if (ptime(microsec_clock::local_time())-this->ttls[uuid] > this->ttl) {
-        std::cout << "Pruning (" << this->tokens[uuid].length();
-        std::cout << "): " << this->tokens[uuid] << std::endl;
         // If there is a default handler pass it on
         if (this->default_handler) {
           boost::mutex::scoped_lock l(callback_mux);
-          callback_queue.push(std::pair(uuid,true));
+          callback_queue.push(std::make_pair(nil_generator(),uuid));
         } else {
           // Otherwise delete it
           to_be_erased.push_back(uuid);
@@ -339,49 +321,80 @@ SerialListener::listen() {
 }
 
 bool
-SerialListener::listenForOnceComparator(std::string token) {
-  if (token == current_listen_for_one_target)
-    return true;
-  return false;
+SerialListener::listenForStringOnce(std::string token, size_t milliseconds) {
+  return this->listenForOnce(exactly(token), milliseconds);
+}
+
+void
+SerialListener::notifyListenForOnce(shared_cond_var_ptr_t cond_ptr) {
+  cond_ptr->notify_all();
 }
 
 bool
-SerialListener::listenForStringOnce(std::string token, size_t milliseconds) {
-  boost::condition_variable cond;
+SerialListener::listenForOnce(ComparatorType comparator, size_t ms)
+{
+  shared_cond_var_ptr_t cond_ptr(new boost::condition_variable());
   boost::mutex mut;
-  current_listen_for_one_target = token;
 
   // Create blocking filter
-  uuid_type uuid = random_generator();
-  std::pair
-   filter_pair(uuid, filter_type::blocking);
-  std::pair
-   comparator_pair(uuid,
-    boost::bind(&SerialListener::listenForOnceComparator, this, _1));
-  std::pair
-   condition_pair(uuid, &cond);
+  const uuid_type uuid = random_generator();
   {
     boost::mutex::scoped_lock l(filter_mux);
-    filters.insert(filter_pair);
-    comparators.insert(comparator_pair);
-    condition_vars.insert(condition_pair);
+    filters.push_back(uuid);
+    comparators.insert(std::make_pair(uuid,comparator));
+  }
+  {
+    boost::mutex::scoped_lock l(callback_mux);
+    callbacks.insert(std::make_pair(uuid,
+      boost::bind(&SerialListener::notifyListenForOnce, this, cond_ptr)));
   }
 
-  this->processNewFilter(uuid);
+  // Run this filter through all tokens onces
+  std::vector token_uuids;
+  std::map::iterator it;
+  for (it = tokens.begin(); it != tokens.end(); it++)
+    token_uuids.push_back((*it).first);
+  std::vector > pairs =
+    this->filter(uuid, token_uuids);
+
+  // If there is at least one
+  if (pairs.size() > 0) {
+    // If there is more than one find the oldest
+    size_t index = 0;
+    if (pairs.size() > 1) {
+      using namespace boost::posix_time;
+      ptime oldest_time = ttls[pairs[index].second];
+      size_t i = 0;
+      std::vector >::iterator it;
+      for (it = pairs.begin(); it != pairs.end(); it++) {
+        if (ttls[(*it).second] < oldest_time) {
+          oldest_time = ttls[(*it).second];
+          index = i;
+        }
+        i++;
+      }
+    }
+    // Either way put the final index into the callback queue
+    callback_queue.push(pairs[index]);
+  }
 
   bool result = false;
 
   // Wait
   boost::unique_lock lock(mut);
-  if (cond.timed_wait(lock, boost::posix_time::milliseconds(milliseconds)))
+  using namespace boost::posix_time;
+  if (cond_ptr->timed_wait(lock, milliseconds(ms)))
     result = true;
 
   // Destroy the filter
   {
     boost::mutex::scoped_lock l(filter_mux);
-    filters.erase(uuid);
+    filters.erase(std::find(filters.begin(),filters.end(),uuid));
     comparators.erase(uuid);
-    condition_vars.erase(uuid);
+  }
+  {
+    boost::mutex::scoped_lock l(callback_mux);
+    callbacks.erase(uuid);
   }
 
   return result;
@@ -392,8 +405,6 @@ SerialListener::listenFor(ComparatorType comparator, DataCallback callback)
 {
   // Create Filter
   uuid_type uuid = random_generator();
-  std::pair
-   filter_pair(uuid, filter_type::nonblocking);
   std::pair
    comparator_pair(uuid, comparator);
   std::pair
@@ -401,7 +412,7 @@ SerialListener::listenFor(ComparatorType comparator, DataCallback callback)
 
   {
     boost::mutex::scoped_lock l(filter_mux);
-    filters.insert(filter_pair);
+    filters.push_back(uuid);
     comparators.insert(comparator_pair);
   }
   {
@@ -409,6 +420,20 @@ SerialListener::listenFor(ComparatorType comparator, DataCallback callback)
     callbacks.insert(callback_pair);
   }
 
+  // Run this filter through all tokens onces
+  std::vector token_uuids;
+  std::map::iterator it;
+  for (it = tokens.begin(); it != tokens.end(); it++)
+    token_uuids.push_back((*it).first);
+  std::vector > pairs =
+    this->filter(uuid, token_uuids);
+
+  // Dispatch
+  std::vector >::iterator it_cb;
+  for (it_cb = pairs.begin(); it_cb != pairs.end(); it_cb++) {
+    callback_queue.push((*it_cb));
+  }
+
   return uuid;
 }
 
@@ -416,8 +441,9 @@ void
 SerialListener::stopListeningFor(uuid_type filter_uuid) {
   // Delete filter
   boost::mutex::scoped_lock l(filter_mux);
-  filters.erase(filter_uuid);
+  filters.erase(std::find(filters.begin(),filters.end(),filter_uuid));
   comparators.erase(filter_uuid);
+  boost::mutex::scoped_lock l2(callback_mux);
   callbacks.erase(filter_uuid);
 }
 
@@ -435,3 +461,13 @@ SerialListener::_delimeter_tokenizer (std::string &data,
   boost::split(tokens, data, boost::is_any_of(delimeter));
 }
 
+ComparatorType
+SerialListener::exactly(std::string exact_str) {
+  return boost::bind(&SerialListener::_exactly, _1, exact_str);
+}
+
+bool
+SerialListener::_exactly(const std::string &token, std::string exact_str) {
+  return token == exact_str;
+}
+
diff --git a/tests/serial_listener_tests.cc b/tests/serial_listener_tests.cc
index f589b04..81e4aef 100644
--- a/tests/serial_listener_tests.cc
+++ b/tests/serial_listener_tests.cc
@@ -13,7 +13,7 @@ static size_t global_count, global_listen_count;
 
 void default_handler(std::string line) {
   global_count++;
-  std::cout << "default_handler got: " << line << std::endl;
+  // std::cout << "default_handler got: " << line << std::endl;
 }
 
 namespace {
@@ -45,7 +45,7 @@ protected:
   }
 
   void execute_listenForStringOnce() {
-    listener.listenForStringOnce("?$1E", 1000);
+    listener.listenForStringOnce("?$1E", 50);
   }
 
   void simulate_loop(std::string input_str) {
@@ -80,11 +80,11 @@ TEST_F(SerialListenerTests, listenForOnceWorks) {
   boost::thread t(
     boost::bind(&SerialListenerTests::execute_listenForStringOnce, this));
 
-  boost::this_thread::sleep(boost::posix_time::milliseconds(100));
+  boost::this_thread::sleep(boost::posix_time::milliseconds(5));
 
   simulate_loop("\r+\r?$1E\r$1E=Robo");
 
-  ASSERT_TRUE(t.timed_join(boost::posix_time::milliseconds(1500)));
+  ASSERT_TRUE(t.timed_join(boost::posix_time::milliseconds(60)));
 
   // Make sure the filters are getting deleted
   ASSERT_EQ(listener.filters.size(), 0);
@@ -92,7 +92,7 @@ TEST_F(SerialListenerTests, listenForOnceWorks) {
   // give some time for the callback thread to finish
   stopCallbackThread();
 
-  ASSERT_EQ(global_count, 2);
+  ASSERT_EQ(global_count, 1);
 }
 
 // lookForOnce should not find it, but timeout after 1000ms, so it should 
@@ -103,11 +103,11 @@ TEST_F(SerialListenerTests, listenForOnceTimesout) {
   boost::thread t(
     boost::bind(&SerialListenerTests::execute_listenForStringOnce, this));
 
-  boost::this_thread::sleep(boost::posix_time::milliseconds(100));
+  boost::this_thread::sleep(boost::posix_time::milliseconds(55));
 
   simulate_loop("\r+\r?$1ENOTRIGHT\r$1E=Robo");
 
-  ASSERT_TRUE(t.timed_join(boost::posix_time::milliseconds(1500)));
+  ASSERT_TRUE(t.timed_join(boost::posix_time::milliseconds(60)));
 
   // give some time for the callback thread to finish
   stopCallbackThread();
@@ -116,6 +116,7 @@ TEST_F(SerialListenerTests, listenForOnceTimesout) {
 }
 
 bool listenForComparator(std::string line) {
+  // std::cout << "In listenForComparator(" << line << ")" << std::endl;
   if (line.substr(0,2) == "V=") {
     return true;
   }
@@ -123,6 +124,7 @@ bool listenForComparator(std::string line) {
 }
 
 void listenForCallback(std::string line) {
+  // std::cout << "In listenForCallback(" << line << ")" << std::endl;
   global_listen_count++;
 }
 

From 18284ae76478a9491bb0a16d0dab30ad0bd12f1c Mon Sep 17 00:00:00 2001
From: William Woodall 
Date: Tue, 10 Jan 2012 14:19:56 -0600
Subject: [PATCH 06/72] working on new boostless serial with a pimpl setup

---
 examples/serial_listener_example.cc |   4 +-
 include/serial/impl/unix.h          |  90 ++++
 include/serial/serial.h             | 755 +++++++++++++---------------
 include/serial/serial_old.h         | 448 +++++++++++++++++
 src/impl/unix.cc                    | 132 +++++
 src/serial_listener.cc              |   2 +-
 src/{serial.cc => serial_old.cc}    |   0
 7 files changed, 1034 insertions(+), 397 deletions(-)
 create mode 100644 include/serial/impl/unix.h
 create mode 100644 include/serial/serial_old.h
 create mode 100644 src/impl/unix.cc
 rename src/{serial.cc => serial_old.cc} (100%)

diff --git a/examples/serial_listener_example.cc b/examples/serial_listener_example.cc
index deb0646..5c0555b 100644
--- a/examples/serial_listener_example.cc
+++ b/examples/serial_listener_example.cc
@@ -13,7 +13,7 @@ void callback(std::string line) {
   std::cout << "callback got a: " << line << std::endl;
 }
 
-#if 0
+#if 1
 int main(void) {
   Serial serial("/dev/tty.usbmodemfd1231", 115200);
 
@@ -41,6 +41,7 @@ int main(void) {
 }
 #endif
 
+#if 0
 int main(void) {
   Serial serial("/dev/tty.usbmodemfd1231", 115200);
 
@@ -59,3 +60,4 @@ int main(void) {
   }
 
 }
+#endif
diff --git a/include/serial/impl/unix.h b/include/serial/impl/unix.h
new file mode 100644
index 0000000..8dfefb5
--- /dev/null
+++ b/include/serial/impl/unix.h
@@ -0,0 +1,90 @@
+/*!
+ * \file serial/impl/unix.h
+ * \author  William Woodall 
+ * \version 0.1
+ *
+ * \section LICENSE
+ *
+ * The MIT License
+ *
+ * Copyright (c) 2011 William Woodall
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a 
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
+ * DEALINGS IN THE SOFTWARE.
+ *
+ * \section DESCRIPTION
+ *
+ * This provides a unix based pimpl for the Serial class.
+ */
+
+#ifndef SERIAL_IMPL_UNIX_H
+#define SERIAL_IMPL_UNIX_H
+
+#include "serial/serial.h"
+
+namespace {
+
+class Serial::Serial_pimpl {
+public:
+  Serial_pimpl (const std::string &port,
+                int baudrate,
+                long timeout,
+                bytesize_t bytesize,
+                parity_t parity,
+                stopbits_t stopbits,
+                flowcontrol_t flowcontrol);
+
+  virtual ~Serial_pimpl ();
+
+  void open ();
+  void close ();
+  bool isOpen ();
+
+  size_t read (unsigned char* buffer, size_t size = 1);
+  std::string read (size_t size = 1);
+  size_t read (std::string &buffer, size_t size = 1);
+
+  size_t write (unsigned char* data, size_t length);
+  size_t write (const std::string &data);
+
+  void setPort (const std::string &port);
+  std::string getPort () const;
+
+  void setTimeout (long timeout);
+  long getTimeout () const;
+
+  void setBaudrate (int baudrate);
+  int getBaudrate () const;
+
+  void setBytesize (bytesize_t bytesize);
+  bytesize_t getBytesize () const;
+
+  void setParity (parity_t parity);
+  parity_t getParity () const;
+
+  void setStopbits (stopbits_t stopbits);
+  stopbits_t getStopbits () const;
+
+  void setFlowcontrol (flowcontrol_t flowcontrol);
+  flowcontrol_t getFlowcontrol () const;
+
+};
+
+}
+
+#endif // SERIAL_IMPL_UNIX_H
\ No newline at end of file
diff --git a/include/serial/serial.h b/include/serial/serial.h
index ed8215a..7286fe0 100644
--- a/include/serial/serial.h
+++ b/include/serial/serial.h
@@ -1,21 +1,21 @@
-/**
- * @file serial.h
- * @author  William Woodall 
- * @author  John Harrison   
- * @version 0.1
+/*!
+ * \file serial/serial.h
+ * \author  William Woodall 
+ * \author  John Harrison   
+ * \version 0.1
  *
- * @section LICENSE
+ * \section LICENSE
  *
  * The MIT License
  *
  * Copyright (c) 2011 William Woodall
  *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
+ * Permission is hereby granted, free of charge, to any person obtaining a 
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
  *
  * The above copyright notice and this permission notice shall be included in
  * all copies or substantial portions of the Software.
@@ -24,11 +24,11 @@
  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
+ * DEALINGS IN THE SOFTWARE.
  *
- * @section DESCRIPTION
+ * \section DESCRIPTION
  *
  * This provides a cross platform interface for interacting with Serial Ports.
  */
@@ -37,356 +37,345 @@
 #ifndef SERIAL_H
 #define SERIAL_H
 
-#include 
-#include 
-#include 
-
-#include 
-#include 
-#include 
-#include 
-#include 
-
-// A macro to disallow the copy constructor and operator= functions
-// This should be used in the private: declarations for a class
-#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
-  TypeName(const TypeName&);               \
-  void operator=(const TypeName&)
-
-// If on Windows undefine the PARITY_* defines that are in winbase.h
-#ifdef _WIN32
-    #undef PARITY_NONE
-    #undef PARITY_ODD
-    #undef PARITY_EVEN
-#endif
-
-// DEFINES
-#ifndef DEFAULT_BAUDRATE
-#define DEFAULT_BAUDRATE 9600
-#endif
-#ifndef DEFAULT_TIMEOUT
-#define DEFAULT_TIMEOUT 0
-#endif
-#ifndef DEFAULT_BYTESIZE
-#define DEFAULT_BYTESIZE EIGHTBITS
-#endif
-#ifndef DEFAULT_PARITY
-#define DEFAULT_PARITY PARITY_NONE
-#endif
-#ifndef DEFAULT_STOPBITS
-#define DEFAULT_STOPBITS STOPBITS_ONE
-#endif 
-#ifndef DEFAULT_FLOWCONTROL
-#define DEFAULT_FLOWCONTROL FLOWCONTROL_NONE
-#endif
-
 namespace serial {
 
-// Serial Port settings CONSTANTS
-enum bytesize_t { FIVEBITS = 5, SIXBITS = 6, SEVENBITS = 7, EIGHTBITS = 8 };
-enum parity_t { PARITY_NONE, PARITY_ODD, PARITY_EVEN };
-enum stopbits_t { STOPBITS_ONE, STOPBITS_ONE_POINT_FIVE, STOPBITS_TWO };
-enum flowcontrol_t { FLOWCONTROL_NONE, FLOWCONTROL_SOFTWARE, FLOWCONTROL_HARDWARE };
+/*!
+ * Enumeration defines the possible bytesizes for the serial port.
+ */
+typedef enum {
+  FIVEBITS = 5,
+  SIXBITS = 6,
+  SEVENBITS = 7,
+  EIGHTBITS = 8
+} bytesize_t;
 
+/*!
+ * Enumeration defines the possible parity types for the serial port.
+ */
+typedef enum {
+  PARITY_NONE = 0,
+  PARITY_ODD = 1,
+  PARITY_EVEN = 2
+} parity_t;
+
+/*!
+ * Enumeration defines the possible stopbit types for the serial port.
+ */
+typedef enum {
+  STOPBITS_ONE = 1,
+  STOPBITS_ONE_POINT_FIVE,
+  STOPBITS_TWO = 2
+} stopbits_t;
+
+/*!
+ * Enumeration defines the possible flowcontrol types for the serial port.
+ */
+typedef enum {
+  FLOWCONTROL_NONE = 0,
+  FLOWCONTROL_SOFTWARE,
+  FLOWCONTROL_HARDWARE
+} flowcontrol_t;
+
+/*!
+ * Class that provides a portable serial port interface.
+ */
 class Serial {
 public:
-    /** Constructor, Creates a Serial object but doesn't open the serial port. */
-    Serial();
-    
-    /**
-    * Constructor, creates a SerialPortBoost object and opens the port.
-    * 
-    * @param port A std::string containing the address of the serial port,
-    *        which would be something like 'COM1' on Windows and '/dev/ttyS0'
-    *        on Linux.
-    * 
-    * @param baudrate An integer that represents the buadrate
-    * 
-    * @param timeout A long that represents the time (in milliseconds) until a 
-    *        timeout on reads occur.  Setting this to zero (0) will cause reading
-    *        to be non-blocking, i.e. the available data will be returned immediately,
-    *        but it will not block to wait for more.  Setting this to a number less than
-    *        zero (-1) will result in infinite blocking behaviour, i.e. the serial port will
-    *        block until either size bytes have been read or an exception has occured.
-    * 
-    * @param bytesize Size of each byte in the serial transmission of data, 
-    *        default is EIGHTBITS, possible values are: FIVEBITS, 
-    *        SIXBITS, SEVENBITS, EIGHTBITS
-    * 
-    * @param parity Method of parity, default is PARITY_NONE, possible values
-    *        are: PARITY_NONE, PARITY_ODD, PARITY_EVEN
-    * 
-    * @param stopbits Number of stop bits used, default is STOPBITS_ONE, possible 
-    *        values are: STOPBITS_ONE, STOPBITS_ONE_POINT_FIVE, STOPBITS_TWO
-    * 
-    * @param flowcontrol Type of flowcontrol used, default is FLOWCONTROL_NONE, possible
-    *        values are: FLOWCONTROL_NONE, FLOWCONTROL_SOFTWARE, FLOWCONTROL_HARDWARE
-    * 
-    * @throw SerialPortAlreadyOpenException
-    * @throw SerialPortFailedToOpenException
-    */
-    Serial(std::string port,
-           int baudrate = DEFAULT_BAUDRATE,
-           long timeout = DEFAULT_TIMEOUT,
-           bytesize_t bytesize = DEFAULT_BYTESIZE,
-           parity_t parity = DEFAULT_PARITY,
-           stopbits_t stopbits = DEFAULT_STOPBITS,
-           flowcontrol_t flowcontrol = DEFAULT_FLOWCONTROL);
-    
-    /** Destructor */
-    ~Serial();
-    
-    /** 
-    * Opens the serial port as long as the portname is set and the port isn't alreay open.
-    * 
-    * @throw SerialPortAlreadyOpenException
-    * @throw SerialPortFailedToOpenException
-    */
-    void open();
-    
-    /** Gets the status of the serial port.
-    * 
-    * @return A boolean value that represents whether or not the serial port is open.
-    */
-    bool isOpen();
-    
-    /** Closes the serial port and terminates threads. */
-    void close();
-    
-    /** Read size bytes from the serial port.
-    * If a timeout is set it may return less characters than requested. With no timeout
-    * it will block until the requested number of bytes have been read.
-    * 
-    * @param buffer A char[] of length >= the size parameter to hold incoming data.
-    * 
-    * @param size An integer defining how many bytes to be read.
-    * 
-    * @return An integer representing the number of bytes read.
-    */
-    int read(char* buffer, int size = 1);
-    
-    /** Read size bytes from the serial port.
-    * If a timeout is set it may return less characters than requested. With no timeout
-    * it will block until the requested number of bytes have been read.
-    * 
-    * @param size An integer defining how many bytes to be read.
-    * 
-    * @return A std::string containing the data read.
-    */
-    std::string read(int size = 1);
-    
-    std::string read_until(char delim, size_t size = -1);
-    std::string read_until(std::string delim, size_t size = -1);
-    
-    /** Write length bytes from buffer to the serial port.
-    * 
-    * @param data A char[] with data to be written to the serial port.
-    * 
-    * @param length An integer representing the number of bytes to be written.
-    * 
-    * @return An integer representing the number of bytes written.
-    */
-    int write(char data[], int length);
-    
-    /** Write a string to the serial port.
-    * 
-    * @param data A std::string to be written to the serial port. (must be null terminated)
-    * 
-    * @return An integer representing the number of bytes written to the serial port.
-    */
-    int write(std::string data);
-    
-    /** Sets the logic level of the RTS line.
-    * 
-    * @param level The logic level to set the RTS to. Defaults to true.
-    */
-    void setRTS(bool level = true);
-    
-    /** Sets the logic level of the DTR line.
-    * 
-    * @param level The logic level to set the DTR to. Defaults to true.
-    */
-    void setDTR(bool level = true);
-    
-    /** Gets the status of the CTS line.
-    * 
-    * @return A boolean value that represents the current logic level of the CTS line.
-    */
-    bool getCTS() const;
-    
-    /** Gets the status of the DSR line.
-    * 
-    * @return A boolean value that represents the current logic level of the DSR line.
-    */
-    bool getDSR() const;
-    
-    /** Sets the serial port identifier.
-    * 
-    * @param port A std::string containing the address of the serial port,
-    *        which would be something like 'COM1' on Windows and '/dev/ttyS0'
-    *        on Linux.
-    */
-    void setPort(std::string port);
-    
-    /** Gets the serial port identifier.
-    * 
-    * @return A std::string containing the address of the serial port,
-    *         which would be something like 'COM1' on Windows and '/dev/ttyS0'
-    *         on Linux.
-    */
-    std::string getPort() const;
-    
-    /** Sets the timeout for reads in seconds.
-    * 
-    * @param timeout A long that represents the time (in milliseconds) until a 
-    *        timeout on reads occur.  Setting this to zero (0) will cause reading
-    *        to be non-blocking, i.e. the available data will be returned immediately,
-    *        but it will not block to wait for more.  Setting this to a number less than
-    *        zero (-1) will result in infinite blocking behaviour, i.e. the serial port will
-    *        block until either size bytes have been read or an exception has occured.
-    */
-    void setTimeoutMilliseconds(long timeout);
-    
-    /** Gets the timeout for reads in seconds.
-    * 
-    * @param timeout A long that represents the time (in milliseconds) until a 
-    *        timeout on reads occur.  Setting this to zero (0) will cause reading
-    *        to be non-blocking, i.e. the available data will be returned immediately,
-    *        but it will not block to wait for more.  Setting this to a number less than
-    *        zero (-1) will result in infinite blocking behaviour, i.e. the serial port will
-    *        block until either size bytes have been read or an exception has occured.
-    */
-    long getTimeoutMilliseconds() const;
-    
-    /** Sets the baudrate for the serial port.
-    * 
-    * @param baudrate An integer that sets the baud rate for the serial port.
-    */
-    void setBaudrate(int baudrate);
-    
-    /** Gets the baudrate for the serial port.
-    * 
-    * @return An integer that sets the baud rate for the serial port.
-    */
-    int getBaudrate() const;
-    
-    /** Sets the bytesize for the serial port.
-    * 
-    * @param bytesize Size of each byte in the serial transmission of data, 
-    *        default is EIGHTBITS, possible values are: FIVEBITS, 
-    *        SIXBITS, SEVENBITS, EIGHTBITS
-    * 
-    * @throw InvalidBytesizeException
-    */
-    void setBytesize(bytesize_t bytesize);
-    
-    /** Gets the bytesize for the serial port.
-    * 
-    * @return Size of each byte in the serial transmission of data, 
-    *         default is EIGHTBITS, possible values are: FIVEBITS, 
-    *         SIXBITS, SEVENBITS, EIGHTBITS
-    * 
-    * @throw InvalidBytesizeException
-    */
-    bytesize_t getBytesize() const;
-    
-    /** Sets the parity for the serial port.
-    * 
-    * @param parity Method of parity, default is PARITY_NONE, possible values
-    *        are: PARITY_NONE, PARITY_ODD, PARITY_EVEN
-    * 
-    * @throw InvalidParityException
-    */
-    void setParity(parity_t parity);
-    
-    /** Gets the parity for the serial port.
-    * 
-    * @return Method of parity, default is PARITY_NONE, possible values
-    *         are: PARITY_NONE, PARITY_ODD, PARITY_EVEN
-    * 
-    * @throw InvalidParityException
-    */
-    parity_t getParity() const;
-    
-    /** Sets the stopbits for the serial port.
-    * 
-    * @param stopbits Number of stop bits used, default is STOPBITS_ONE, possible 
-    *        values are: STOPBITS_ONE, STOPBITS_ONE_POINT_FIVE, STOPBITS_TWO
-    * 
-    * @throw InvalidStopbitsException
-    */
-    void setStopbits(stopbits_t stopbits);
-    
-    /** Gets the stopbits for the serial port.
-    * 
-    * @return Number of stop bits used, default is STOPBITS_ONE, possible 
-    *         values are: STOPBITS_ONE, STOPBITS_ONE_POINT_FIVE, STOPBITS_TWO
-    * 
-    * @throw InvalidStopbitsException
-    */
-    stopbits_t getStopbits() const;
-    
-    /** Sets the flow control for the serial port.
-    * 
-    * @param flowcontrol Type of flowcontrol used, default is FLOWCONTROL_NONE, possible
-    *        values are: FLOWCONTROL_NONE, FLOWCONTROL_SOFTWARE, FLOWCONTROL_HARDWARE
-    * 
-    * @throw InvalidFlowcontrolException
-    */
-    void setFlowcontrol(flowcontrol_t flowcontrol);
-    
-    /** Gets the flow control for the serial port.
-    * 
-    * @return Type of flowcontrol used, default is FLOWCONTROL_NONE, possible
-    *         values are: FLOWCONTROL_NONE, FLOWCONTROL_SOFTWARE, FLOWCONTROL_HARDWARE
-    * 
-    * @throw InvalidFlowcontrolException
-    */
-    flowcontrol_t getFlowcontrol() const;
+  /*!
+  * Constructor, creates a SerialPortBoost object and opens the port.
+  * 
+  * \param port A std::string containing the address of the serial port,
+  *        which would be something like 'COM1' on Windows and '/dev/ttyS0'
+  *        on Linux.
+  * 
+  * \param baudrate An integer that represents the buadrate
+  * 
+  * \param timeout A long that represents the time (in milliseconds) until a 
+  * timeout on reads occur. Setting this to zero (0) will cause reading to
+  * be non-blocking, i.e. the available data will be returned immediately,
+  * but it will not block to wait for more. Setting this to a number less
+  * than zero (-1) will result in infinite blocking behaviour, i.e. the
+  * serial port will block until either size bytes have been read or an
+  * exception has occured.
+  * 
+  * \param bytesize Size of each byte in the serial transmission of data, 
+  * default is EIGHTBITS, possible values are: FIVEBITS, SIXBITS, SEVENBITS, 
+  * EIGHTBITS
+  * 
+  * \param parity Method of parity, default is PARITY_NONE, possible values
+  * are: PARITY_NONE, PARITY_ODD, PARITY_EVEN
+  * 
+  * \param stopbits Number of stop bits used, default is STOPBITS_ONE, 
+  * possible values are: STOPBITS_ONE, STOPBITS_ONE_POINT_FIVE, STOPBITS_TWO
+  * 
+  * \param flowcontrol Type of flowcontrol used, default is  
+  * FLOWCONTROL_NONE, possible values are: FLOWCONTROL_NONE, 
+  * FLOWCONTROL_SOFTWARE, FLOWCONTROL_HARDWARE
+  * 
+  * \throw PortNotOpenedException
+  */
+  Serial (const std::string &port = "",
+          int baudrate = 9600,
+          long timeout = 0,
+          bytesize_t bytesize = EIGHTBITS,
+          parity_t parity = PARITY_NONE,
+          stopbits_t stopbits = STOPBITS_ONE,
+          flowcontrol_t flowcontrol = FLOWCONTROL_NONE);
+
+  /*! Destructor */
+  virtual ~Serial ();
+
+  /*!
+  * Opens the serial port as long as the portname is set and the port isn't
+  * alreay open.
+  * 
+  * If the port is provided to the constructor then an explicit call to open 
+  * is not needed.
+  * 
+  * \see Serial::Serial
+  * 
+  * \throw PortNotOpenedException
+  */
+  void
+  open ();
+
+  /*! Gets the open status of the serial port.
+  * 
+  * \return Returns true if the port is open, false otherwise.
+  */
+  bool
+  isOpen ();
+
+  /*! Closes the serial port. */
+  void
+  close ();
+
+  /*! Read a given amount of bytes from the serial port.
+  * 
+  * If a timeout is set it may return less characters than requested. With
+  * no timeout it will block until the requested number of bytes have been
+  * read or until an exception occurs.
+  * 
+  * \param buffer An unsigned char array large enough to hold incoming data 
+  * up to the requested size.
+  * 
+  * \param size A size_t defining how many bytes to be read.
+  * 
+  * \return A size_t representing the number of bytes actually read.
+  */
+  size_t
+  read (unsigned char* buffer, size_t size = 1);
+
+  /*! Read a given amount of bytes from the serial port.
+  * 
+  * If a timeout is set it may return less characters than requested. With
+  * no timeout it will block until the requested number of bytes have been
+  * read or until an exception occurs.
+  * 
+  * \param size A size_t defining how many bytes to be read.
+  * 
+  * \return A std::string containing the data read.
+  */
+  std::string
+  read (size_t size = 1);
+
+  /*! Read a given amount of bytes from the serial port.
+  * 
+  * Reads into a std::string by reference rather than returning it.
+  * 
+  * \param buffer A std::string reference for reading to.
+  * \param size A size_t defining how many bytes to be read.
+  * 
+  * \return A size_t that represents how many bytes were read.
+  * 
+  * \see Serial::read(size_t)
+  */
+  size_t
+  read (const std::string &buffer, size_t size = 1);
+
+  /*! Write bytes from the data to the serial port by given length.
+  * 
+  * \param data An unsigned char array containing data to be written to the
+  * serial port.
+  * 
+  * \param length A size_t representing the number of bytes to be written.
+  * 
+  * \return A size_t representing the number of bytes actually written.
+  */
+  size_t
+  write (unsigned char* data, size_t length);
+
+  /*! Write a string to the serial port.
+  * 
+  * \param data A const std::string reference containg the data to be written 
+  * to the serial port.
+  * 
+  * \return A size_t representing the number of bytes actually written to
+  * the serial port.
+  */
+  size_t
+  write (const std::string &data);
+
+  /*! Sets the serial port identifier.
+  * 
+  * \param port A const std::string reference containing the address of the 
+  * serial port, which would be something like 'COM1' on Windows and 
+  * '/dev/ttyS0' on Linux.
+  * 
+  * \throw InvalidConfigurationException
+  */
+  void
+  setPort (const std::string &port);
+
+  /*! Gets the serial port identifier.
+  * 
+  * \see Serial::setPort
+  * 
+  * \throw InvalidConfigurationException
+  */
+  std::string
+  getPort () const;
+
+  /*! Sets the timeout for reads in milliseconds.
+  * 
+  * \param timeout A long that represents the time (in milliseconds) until a
+  * timeout on reads occur.  Setting this to zero (0) will cause reading to be
+  * non-blocking, i.e. the available data will be returned immediately, but it
+  * will not block to wait for more.  Setting this to a number less than
+  * zero (-1) will result in infinite blocking behaviour, i.e. the serial port 
+  * will block until either size bytes have been read or an exception has 
+  * occured.
+  */
+  void
+  setTimeout (long timeout);
+
+  /*! Gets the timeout for reads in seconds.
+  * 
+  * \see Serial::setTimeout
+  */
+  long
+  getTimeout () const;
+
+  /*! Sets the baudrate for the serial port.
+  * 
+  * Possible baudrates depends on the system but some safe baudrates include:
+  * 110, 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 56000,
+  * 57600, 115200
+  * Some other baudrates that are supported by some comports:
+  * 128000, 153600, 230400, 256000, 460800, 921600
+  * 
+  * \param baudrate An integer that sets the baud rate for the serial port.
+  * 
+  * \throw InvalidConfigurationException
+  */
+  void
+  setBaudrate (int baudrate);
+
+  /*! Gets the baudrate for the serial port.
+  * 
+  * \return An integer that sets the baud rate for the serial port.
+  * 
+  * \see Serial::setBaudrate
+  * 
+  * \throw InvalidConfigurationException
+  */
+  int
+  getBaudrate () const;
+
+  /*! Sets the bytesize for the serial port.
+  * 
+  * \param bytesize Size of each byte in the serial transmission of data, 
+  * default is EIGHTBITS, possible values are: FIVEBITS, SIXBITS, SEVENBITS, 
+  * EIGHTBITS
+  * 
+  * \throw InvalidConfigurationException
+  */
+  void
+  setBytesize (bytesize_t bytesize);
+
+  /*! Gets the bytesize for the serial port.
+  * 
+  * \see Serial::setBytesize
+  * 
+  * \throw InvalidConfigurationException
+  */
+  bytesize_t
+  getBytesize () const;
+
+  /*! Sets the parity for the serial port.
+  * 
+  * \param parity Method of parity, default is PARITY_NONE, possible values 
+  * are: PARITY_NONE, PARITY_ODD, PARITY_EVEN
+  * 
+  * \throw InvalidConfigurationException
+  */
+  void
+  setParity (parity_t parity);
+
+  /*! Gets the parity for the serial port.
+  * 
+  * \see Serial::setParity
+  * 
+  * \throw InvalidConfigurationException
+  */
+  parity_t
+  getParity () const;
+
+  /*! Sets the stopbits for the serial port.
+  * 
+  * \param stopbits Number of stop bits used, default is STOPBITS_ONE, 
+  * possible values are: STOPBITS_ONE, STOPBITS_ONE_POINT_FIVE, STOPBITS_TWO
+  * 
+  * \throw InvalidConfigurationException
+  */
+  void
+  setStopbits (stopbits_t stopbits);
+
+  /*! Gets the stopbits for the serial port.
+  * 
+  * \see Serial::setStopbits
+  * 
+  * \throw InvalidConfigurationException
+  */
+  stopbits_t
+  getStopbits () const;
+
+  /*! Sets the flow control for the serial port.
+  * 
+  * \param flowcontrol Type of flowcontrol used, default is FLOWCONTROL_NONE, 
+  * possible values are: FLOWCONTROL_NONE, FLOWCONTROL_SOFTWARE, 
+  * FLOWCONTROL_HARDWARE
+  * 
+  * \throw InvalidConfigurationException
+  */
+  void
+  setFlowcontrol (flowcontrol_t flowcontrol);
+
+  /*! Gets the flow control for the serial port.
+  * 
+  * \see Serial::setFlowcontrol
+  * 
+  * \throw InvalidConfigurationException
+  */
+  flowcontrol_t
+  getFlowcontrol () const;
+
 private:
-    DISALLOW_COPY_AND_ASSIGN(Serial);
-    void init();
-    void read_complete(const boost::system::error_code& error, std::size_t bytes_transferred);
-    void timeout_callback(const boost::system::error_code& error);
-    
-    boost::asio::io_service io_service;
-    
-    boost::asio::io_service::work work;
-    
-    boost::scoped_ptr serial_port;
-    
-    boost::asio::deadline_timer timeout_timer;
-    
-    std::string port;
-    boost::asio::serial_port_base::baud_rate baudrate;
-    boost::posix_time::time_duration timeout;
-    boost::asio::serial_port_base::character_size bytesize;
-    boost::asio::serial_port_base::parity parity;
-    boost::asio::serial_port_base::stop_bits stopbits;
-    boost::asio::serial_port_base::flow_control flowcontrol;
-    
-    int bytes_read;
-    int bytes_to_read;
-    bool reading;
-    bool nonblocking;
+  // Disable copy constructors
+  Serial(const Serial&);
+  void operator=(const Serial&);
+  const Serial& operator=(Serial);
+
+  // Pimpl idiom, d_pointer
+  class Serial_pimpl;
+  std::shared_ptr pimpl;
+
 };
 
-class SerialPortAlreadyOpenException : public std::exception {
-    const char * port;
-public:
-    SerialPortAlreadyOpenException(const char * port) {this->port = port;}
-    
-    virtual const char* what() const throw() {
-        std::stringstream ss;
-        ss << "Serial Port already open: " << this->port;
-        return ss.str().c_str();
-    }
-};
-
-class SerialPortFailedToOpenException : public std::exception {
+class IOException : public std::exception {
     const char * e_what;
 public:
-    SerialPortFailedToOpenException(const char * e_what) {this->e_what = e_what;}
+    IOException(const char * e_what) {this->e_what = e_what;}
     
     virtual const char* what() const throw() {
         std::stringstream ss;
@@ -395,50 +384,26 @@ public:
     }
 };
 
-class InvalidBytesizeException : public std::exception {
+class PortNotOpenedException : public std::exception {
+    const char * e_what;
+public:
+    PortNotOpenedException(const char * e_what) {this->e_what = e_what;}
+    
+    virtual const char* what() const throw() {
+        std::stringstream ss;
+        ss << "Serial Port failed to open: " << this->e_what;
+        return ss.str().c_str();
+    }
+};
+
+class InvalidConfigurationException : public std::exception {
     int bytesize;
 public:
-    InvalidBytesizeException(int bytesize) {this->bytesize = bytesize;}
+    InvalidConfigurationException(int bytesize) {this->bytesize = bytesize;}
     
     virtual const char* what() const throw() {
         std::stringstream ss;
-        ss << "Invalid bytesize provided: " << this->bytesize;
-        return ss.str().c_str();
-    }
-};
-
-class InvalidParityException : public std::exception {
-    int parity;
-public:
-    InvalidParityException(int parity) {this->parity = parity;}
-    
-    virtual const char* what() const throw() {
-        std::stringstream ss;
-        ss << "Invalid parity provided: " << this->parity;
-        return ss.str().c_str();
-    }
-};
-
-class InvalidStopbitsException : public std::exception {
-    int stopbits;
-public:
-    InvalidStopbitsException(int stopbits) {this->stopbits = stopbits;}
-    
-    virtual const char* what() const throw() {
-        std::stringstream ss;
-        ss << "Invalid stopbits provided: " << this->stopbits;
-        return ss.str().c_str();
-    }
-};
-
-class InvalidFlowcontrolException : public std::exception {
-    int flowcontrol;
-public:
-    InvalidFlowcontrolException(int flowcontrol) {this->flowcontrol = flowcontrol;}
-    
-    virtual const char* what() const throw() {
-        std::stringstream ss;
-        ss << "Invalid flowcontrol provided: " << this->flowcontrol;
+        ss << "Invalid configuration provided: " << this->bytesize;
         return ss.str().c_str();
     }
 };
diff --git a/include/serial/serial_old.h b/include/serial/serial_old.h
new file mode 100644
index 0000000..ed8215a
--- /dev/null
+++ b/include/serial/serial_old.h
@@ -0,0 +1,448 @@
+/**
+ * @file serial.h
+ * @author  William Woodall 
+ * @author  John Harrison   
+ * @version 0.1
+ *
+ * @section LICENSE
+ *
+ * The MIT License
+ *
+ * Copyright (c) 2011 William Woodall
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ * @section DESCRIPTION
+ *
+ * This provides a cross platform interface for interacting with Serial Ports.
+ */
+
+
+#ifndef SERIAL_H
+#define SERIAL_H
+
+#include 
+#include 
+#include 
+
+#include 
+#include 
+#include 
+#include 
+#include 
+
+// A macro to disallow the copy constructor and operator= functions
+// This should be used in the private: declarations for a class
+#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
+  TypeName(const TypeName&);               \
+  void operator=(const TypeName&)
+
+// If on Windows undefine the PARITY_* defines that are in winbase.h
+#ifdef _WIN32
+    #undef PARITY_NONE
+    #undef PARITY_ODD
+    #undef PARITY_EVEN
+#endif
+
+// DEFINES
+#ifndef DEFAULT_BAUDRATE
+#define DEFAULT_BAUDRATE 9600
+#endif
+#ifndef DEFAULT_TIMEOUT
+#define DEFAULT_TIMEOUT 0
+#endif
+#ifndef DEFAULT_BYTESIZE
+#define DEFAULT_BYTESIZE EIGHTBITS
+#endif
+#ifndef DEFAULT_PARITY
+#define DEFAULT_PARITY PARITY_NONE
+#endif
+#ifndef DEFAULT_STOPBITS
+#define DEFAULT_STOPBITS STOPBITS_ONE
+#endif 
+#ifndef DEFAULT_FLOWCONTROL
+#define DEFAULT_FLOWCONTROL FLOWCONTROL_NONE
+#endif
+
+namespace serial {
+
+// Serial Port settings CONSTANTS
+enum bytesize_t { FIVEBITS = 5, SIXBITS = 6, SEVENBITS = 7, EIGHTBITS = 8 };
+enum parity_t { PARITY_NONE, PARITY_ODD, PARITY_EVEN };
+enum stopbits_t { STOPBITS_ONE, STOPBITS_ONE_POINT_FIVE, STOPBITS_TWO };
+enum flowcontrol_t { FLOWCONTROL_NONE, FLOWCONTROL_SOFTWARE, FLOWCONTROL_HARDWARE };
+
+class Serial {
+public:
+    /** Constructor, Creates a Serial object but doesn't open the serial port. */
+    Serial();
+    
+    /**
+    * Constructor, creates a SerialPortBoost object and opens the port.
+    * 
+    * @param port A std::string containing the address of the serial port,
+    *        which would be something like 'COM1' on Windows and '/dev/ttyS0'
+    *        on Linux.
+    * 
+    * @param baudrate An integer that represents the buadrate
+    * 
+    * @param timeout A long that represents the time (in milliseconds) until a 
+    *        timeout on reads occur.  Setting this to zero (0) will cause reading
+    *        to be non-blocking, i.e. the available data will be returned immediately,
+    *        but it will not block to wait for more.  Setting this to a number less than
+    *        zero (-1) will result in infinite blocking behaviour, i.e. the serial port will
+    *        block until either size bytes have been read or an exception has occured.
+    * 
+    * @param bytesize Size of each byte in the serial transmission of data, 
+    *        default is EIGHTBITS, possible values are: FIVEBITS, 
+    *        SIXBITS, SEVENBITS, EIGHTBITS
+    * 
+    * @param parity Method of parity, default is PARITY_NONE, possible values
+    *        are: PARITY_NONE, PARITY_ODD, PARITY_EVEN
+    * 
+    * @param stopbits Number of stop bits used, default is STOPBITS_ONE, possible 
+    *        values are: STOPBITS_ONE, STOPBITS_ONE_POINT_FIVE, STOPBITS_TWO
+    * 
+    * @param flowcontrol Type of flowcontrol used, default is FLOWCONTROL_NONE, possible
+    *        values are: FLOWCONTROL_NONE, FLOWCONTROL_SOFTWARE, FLOWCONTROL_HARDWARE
+    * 
+    * @throw SerialPortAlreadyOpenException
+    * @throw SerialPortFailedToOpenException
+    */
+    Serial(std::string port,
+           int baudrate = DEFAULT_BAUDRATE,
+           long timeout = DEFAULT_TIMEOUT,
+           bytesize_t bytesize = DEFAULT_BYTESIZE,
+           parity_t parity = DEFAULT_PARITY,
+           stopbits_t stopbits = DEFAULT_STOPBITS,
+           flowcontrol_t flowcontrol = DEFAULT_FLOWCONTROL);
+    
+    /** Destructor */
+    ~Serial();
+    
+    /** 
+    * Opens the serial port as long as the portname is set and the port isn't alreay open.
+    * 
+    * @throw SerialPortAlreadyOpenException
+    * @throw SerialPortFailedToOpenException
+    */
+    void open();
+    
+    /** Gets the status of the serial port.
+    * 
+    * @return A boolean value that represents whether or not the serial port is open.
+    */
+    bool isOpen();
+    
+    /** Closes the serial port and terminates threads. */
+    void close();
+    
+    /** Read size bytes from the serial port.
+    * If a timeout is set it may return less characters than requested. With no timeout
+    * it will block until the requested number of bytes have been read.
+    * 
+    * @param buffer A char[] of length >= the size parameter to hold incoming data.
+    * 
+    * @param size An integer defining how many bytes to be read.
+    * 
+    * @return An integer representing the number of bytes read.
+    */
+    int read(char* buffer, int size = 1);
+    
+    /** Read size bytes from the serial port.
+    * If a timeout is set it may return less characters than requested. With no timeout
+    * it will block until the requested number of bytes have been read.
+    * 
+    * @param size An integer defining how many bytes to be read.
+    * 
+    * @return A std::string containing the data read.
+    */
+    std::string read(int size = 1);
+    
+    std::string read_until(char delim, size_t size = -1);
+    std::string read_until(std::string delim, size_t size = -1);
+    
+    /** Write length bytes from buffer to the serial port.
+    * 
+    * @param data A char[] with data to be written to the serial port.
+    * 
+    * @param length An integer representing the number of bytes to be written.
+    * 
+    * @return An integer representing the number of bytes written.
+    */
+    int write(char data[], int length);
+    
+    /** Write a string to the serial port.
+    * 
+    * @param data A std::string to be written to the serial port. (must be null terminated)
+    * 
+    * @return An integer representing the number of bytes written to the serial port.
+    */
+    int write(std::string data);
+    
+    /** Sets the logic level of the RTS line.
+    * 
+    * @param level The logic level to set the RTS to. Defaults to true.
+    */
+    void setRTS(bool level = true);
+    
+    /** Sets the logic level of the DTR line.
+    * 
+    * @param level The logic level to set the DTR to. Defaults to true.
+    */
+    void setDTR(bool level = true);
+    
+    /** Gets the status of the CTS line.
+    * 
+    * @return A boolean value that represents the current logic level of the CTS line.
+    */
+    bool getCTS() const;
+    
+    /** Gets the status of the DSR line.
+    * 
+    * @return A boolean value that represents the current logic level of the DSR line.
+    */
+    bool getDSR() const;
+    
+    /** Sets the serial port identifier.
+    * 
+    * @param port A std::string containing the address of the serial port,
+    *        which would be something like 'COM1' on Windows and '/dev/ttyS0'
+    *        on Linux.
+    */
+    void setPort(std::string port);
+    
+    /** Gets the serial port identifier.
+    * 
+    * @return A std::string containing the address of the serial port,
+    *         which would be something like 'COM1' on Windows and '/dev/ttyS0'
+    *         on Linux.
+    */
+    std::string getPort() const;
+    
+    /** Sets the timeout for reads in seconds.
+    * 
+    * @param timeout A long that represents the time (in milliseconds) until a 
+    *        timeout on reads occur.  Setting this to zero (0) will cause reading
+    *        to be non-blocking, i.e. the available data will be returned immediately,
+    *        but it will not block to wait for more.  Setting this to a number less than
+    *        zero (-1) will result in infinite blocking behaviour, i.e. the serial port will
+    *        block until either size bytes have been read or an exception has occured.
+    */
+    void setTimeoutMilliseconds(long timeout);
+    
+    /** Gets the timeout for reads in seconds.
+    * 
+    * @param timeout A long that represents the time (in milliseconds) until a 
+    *        timeout on reads occur.  Setting this to zero (0) will cause reading
+    *        to be non-blocking, i.e. the available data will be returned immediately,
+    *        but it will not block to wait for more.  Setting this to a number less than
+    *        zero (-1) will result in infinite blocking behaviour, i.e. the serial port will
+    *        block until either size bytes have been read or an exception has occured.
+    */
+    long getTimeoutMilliseconds() const;
+    
+    /** Sets the baudrate for the serial port.
+    * 
+    * @param baudrate An integer that sets the baud rate for the serial port.
+    */
+    void setBaudrate(int baudrate);
+    
+    /** Gets the baudrate for the serial port.
+    * 
+    * @return An integer that sets the baud rate for the serial port.
+    */
+    int getBaudrate() const;
+    
+    /** Sets the bytesize for the serial port.
+    * 
+    * @param bytesize Size of each byte in the serial transmission of data, 
+    *        default is EIGHTBITS, possible values are: FIVEBITS, 
+    *        SIXBITS, SEVENBITS, EIGHTBITS
+    * 
+    * @throw InvalidBytesizeException
+    */
+    void setBytesize(bytesize_t bytesize);
+    
+    /** Gets the bytesize for the serial port.
+    * 
+    * @return Size of each byte in the serial transmission of data, 
+    *         default is EIGHTBITS, possible values are: FIVEBITS, 
+    *         SIXBITS, SEVENBITS, EIGHTBITS
+    * 
+    * @throw InvalidBytesizeException
+    */
+    bytesize_t getBytesize() const;
+    
+    /** Sets the parity for the serial port.
+    * 
+    * @param parity Method of parity, default is PARITY_NONE, possible values
+    *        are: PARITY_NONE, PARITY_ODD, PARITY_EVEN
+    * 
+    * @throw InvalidParityException
+    */
+    void setParity(parity_t parity);
+    
+    /** Gets the parity for the serial port.
+    * 
+    * @return Method of parity, default is PARITY_NONE, possible values
+    *         are: PARITY_NONE, PARITY_ODD, PARITY_EVEN
+    * 
+    * @throw InvalidParityException
+    */
+    parity_t getParity() const;
+    
+    /** Sets the stopbits for the serial port.
+    * 
+    * @param stopbits Number of stop bits used, default is STOPBITS_ONE, possible 
+    *        values are: STOPBITS_ONE, STOPBITS_ONE_POINT_FIVE, STOPBITS_TWO
+    * 
+    * @throw InvalidStopbitsException
+    */
+    void setStopbits(stopbits_t stopbits);
+    
+    /** Gets the stopbits for the serial port.
+    * 
+    * @return Number of stop bits used, default is STOPBITS_ONE, possible 
+    *         values are: STOPBITS_ONE, STOPBITS_ONE_POINT_FIVE, STOPBITS_TWO
+    * 
+    * @throw InvalidStopbitsException
+    */
+    stopbits_t getStopbits() const;
+    
+    /** Sets the flow control for the serial port.
+    * 
+    * @param flowcontrol Type of flowcontrol used, default is FLOWCONTROL_NONE, possible
+    *        values are: FLOWCONTROL_NONE, FLOWCONTROL_SOFTWARE, FLOWCONTROL_HARDWARE
+    * 
+    * @throw InvalidFlowcontrolException
+    */
+    void setFlowcontrol(flowcontrol_t flowcontrol);
+    
+    /** Gets the flow control for the serial port.
+    * 
+    * @return Type of flowcontrol used, default is FLOWCONTROL_NONE, possible
+    *         values are: FLOWCONTROL_NONE, FLOWCONTROL_SOFTWARE, FLOWCONTROL_HARDWARE
+    * 
+    * @throw InvalidFlowcontrolException
+    */
+    flowcontrol_t getFlowcontrol() const;
+private:
+    DISALLOW_COPY_AND_ASSIGN(Serial);
+    void init();
+    void read_complete(const boost::system::error_code& error, std::size_t bytes_transferred);
+    void timeout_callback(const boost::system::error_code& error);
+    
+    boost::asio::io_service io_service;
+    
+    boost::asio::io_service::work work;
+    
+    boost::scoped_ptr serial_port;
+    
+    boost::asio::deadline_timer timeout_timer;
+    
+    std::string port;
+    boost::asio::serial_port_base::baud_rate baudrate;
+    boost::posix_time::time_duration timeout;
+    boost::asio::serial_port_base::character_size bytesize;
+    boost::asio::serial_port_base::parity parity;
+    boost::asio::serial_port_base::stop_bits stopbits;
+    boost::asio::serial_port_base::flow_control flowcontrol;
+    
+    int bytes_read;
+    int bytes_to_read;
+    bool reading;
+    bool nonblocking;
+};
+
+class SerialPortAlreadyOpenException : public std::exception {
+    const char * port;
+public:
+    SerialPortAlreadyOpenException(const char * port) {this->port = port;}
+    
+    virtual const char* what() const throw() {
+        std::stringstream ss;
+        ss << "Serial Port already open: " << this->port;
+        return ss.str().c_str();
+    }
+};
+
+class SerialPortFailedToOpenException : public std::exception {
+    const char * e_what;
+public:
+    SerialPortFailedToOpenException(const char * e_what) {this->e_what = e_what;}
+    
+    virtual const char* what() const throw() {
+        std::stringstream ss;
+        ss << "Serial Port failed to open: " << this->e_what;
+        return ss.str().c_str();
+    }
+};
+
+class InvalidBytesizeException : public std::exception {
+    int bytesize;
+public:
+    InvalidBytesizeException(int bytesize) {this->bytesize = bytesize;}
+    
+    virtual const char* what() const throw() {
+        std::stringstream ss;
+        ss << "Invalid bytesize provided: " << this->bytesize;
+        return ss.str().c_str();
+    }
+};
+
+class InvalidParityException : public std::exception {
+    int parity;
+public:
+    InvalidParityException(int parity) {this->parity = parity;}
+    
+    virtual const char* what() const throw() {
+        std::stringstream ss;
+        ss << "Invalid parity provided: " << this->parity;
+        return ss.str().c_str();
+    }
+};
+
+class InvalidStopbitsException : public std::exception {
+    int stopbits;
+public:
+    InvalidStopbitsException(int stopbits) {this->stopbits = stopbits;}
+    
+    virtual const char* what() const throw() {
+        std::stringstream ss;
+        ss << "Invalid stopbits provided: " << this->stopbits;
+        return ss.str().c_str();
+    }
+};
+
+class InvalidFlowcontrolException : public std::exception {
+    int flowcontrol;
+public:
+    InvalidFlowcontrolException(int flowcontrol) {this->flowcontrol = flowcontrol;}
+    
+    virtual const char* what() const throw() {
+        std::stringstream ss;
+        ss << "Invalid flowcontrol provided: " << this->flowcontrol;
+        return ss.str().c_str();
+    }
+};
+
+} // namespace serial
+
+#endif
\ No newline at end of file
diff --git a/src/impl/unix.cc b/src/impl/unix.cc
new file mode 100644
index 0000000..e4db489
--- /dev/null
+++ b/src/impl/unix.cc
@@ -0,0 +1,132 @@
+#include "serial/impl/unix.h"
+
+using namespace serial;
+
+Serial_pimpl::Serial_pimpl (const std::string &port, int baudrate,
+                            long timeout, bytesize_t bytesize,
+                            parity_t parity, stopbits_t stopbits,
+                            flowcontrol_t flowcontrol)
+: port(port), baudrate(baudrate), timeout(timeout), bytesize(bytesize),
+  parity(parity), stopbits(stopbits), flowcontrol(flowcontrol)
+{
+  
+}
+
+Serial_pimpl::~Serial_pimpl () {
+  
+}
+
+void
+Serial_pimpl::open () {
+  
+}
+
+void
+Serial_pimpl::close () {
+  
+}
+bool
+Serial_pimpl::isOpen () {
+  
+}
+
+size_t
+Serial_pimpl::read (unsigned char* buffer, size_t size = 1) {
+  
+}
+
+std::string
+Serial_pimpl::read (size_t size = 1) {
+  
+}
+
+size_t
+Serial_pimpl::read (std::string &buffer, size_t size = 1) {
+  
+}
+
+size_t
+Serial_pimpl::write (unsigned char* data, size_t length) {
+  
+}
+
+size_t
+Serial_pimpl::write (const std::string &data) {
+  
+}
+
+void
+Serial_pimpl::setPort (const std::string &port) {
+  
+}
+
+std::string
+Serial_pimpl::getPort () const {
+  
+}
+
+void
+Serial_pimpl::setTimeout (long timeout) {
+  
+}
+
+long
+Serial_pimpl::getTimeout () const {
+  
+}
+
+void
+Serial_pimpl::setBaudrate (int baudrate) {
+  
+}
+
+int
+Serial_pimpl::getBaudrate () const {
+  
+}
+
+void
+Serial_pimpl::setBytesize (bytesize_t bytesize) {
+  
+}
+
+bytesize_t
+Serial_pimpl::getBytesize () const {
+  
+}
+
+void
+Serial_pimpl::setParity (parity_t parity) {
+  
+}
+
+parity_t
+Serial_pimpl::getParity () const {
+  
+}
+
+void
+Serial_pimpl::setStopbits (stopbits_t stopbits) {
+  
+}
+
+stopbits_t
+Serial_pimpl::getStopbits () const {
+  
+}
+
+void
+Serial_pimpl::setFlowcontrol (flowcontrol_t flowcontrol) {
+  
+}
+
+flowcontrol_t
+Serial_pimpl::getFlowcontrol () const {
+  
+}
+
+
+
+
+
+
diff --git a/src/serial_listener.cc b/src/serial_listener.cc
index 77993f0..ce4efa2 100644
--- a/src/serial_listener.cc
+++ b/src/serial_listener.cc
@@ -146,7 +146,7 @@ SerialListener::determineAmountToRead() {
   // TODO: Make a more intelligent method based on the length of the things 
   //  filters are looking for.  i.e.: if the filter is looking for 'V=XX\r' 
   //  make the read amount at least 5.
-  return 1024;
+  return 5;
 }
 
 void
diff --git a/src/serial.cc b/src/serial_old.cc
similarity index 100%
rename from src/serial.cc
rename to src/serial_old.cc

From c8e78412230a51aa4fdf045b4ee471e24e992c25 Mon Sep 17 00:00:00 2001
From: William Woodall 
Date: Tue, 10 Jan 2012 15:08:57 -0600
Subject: [PATCH 07/72] Working on pimpl implementation of serial.

---
 .gitignore                           |   1 -
 include/serial/impl/unix.h           |   3 +
 src/serial.cc                        | 134 +++++++++++++++++++++++++++
 tests/proof_of_concepts/tokenizer.cc |  31 +++++++
 4 files changed, 168 insertions(+), 1 deletion(-)
 create mode 100644 src/serial.cc
 create mode 100644 tests/proof_of_concepts/tokenizer.cc

diff --git a/.gitignore b/.gitignore
index a3ee3d4..00f5a05 100644
--- a/.gitignore
+++ b/.gitignore
@@ -7,7 +7,6 @@
 *.pyc
 *.pyo
 *.zip
-*.cc
 */files/*
 */tmp/*
 *.hwm*
diff --git a/include/serial/impl/unix.h b/include/serial/impl/unix.h
index 8dfefb5..08a94de 100644
--- a/include/serial/impl/unix.h
+++ b/include/serial/impl/unix.h
@@ -83,6 +83,9 @@ public:
   void setFlowcontrol (flowcontrol_t flowcontrol);
   flowcontrol_t getFlowcontrol () const;
 
+private:
+  int fd;
+
 };
 
 }
diff --git a/src/serial.cc b/src/serial.cc
new file mode 100644
index 0000000..4637e72
--- /dev/null
+++ b/src/serial.cc
@@ -0,0 +1,134 @@
+#include "serial/serial.h"
+
+#ifdef _WIN32
+#include "serial/impl/win.h"
+#else
+#include "serial/impl/unix.h"
+#endif
+
+Serial::Serial (const std::string &port, int baudrate, 
+                            long timeout, bytesize_t bytesize,
+                            parity_t parity, stopbits_t stopbits,
+                            flowcontrol_t flowcontrol)
+: impl(new Serial_pimpl(port,baudrate,timeout,bytesize,parity,stopbits,
+                        flowcontrol))
+{
+  
+}
+
+Serial::~Serial () {
+  delete impl;
+}
+
+void
+Serial::open () {
+  this->impl->open ();
+}
+
+void
+Serial::close () {
+  this->impl->close ();
+}
+bool
+Serial::isOpen () {
+  return this->impl->isOpen ();
+}
+
+size_t
+Serial::read (unsigned char* buffer, size_t size = 1) {
+  return this->impl->read (buffer, size);
+}
+
+std::string
+Serial::read (size_t size = 1) {
+  return this->impl->read (size);
+}
+
+size_t
+Serial::read (std::string &buffer, size_t size = 1) {
+  return this->impl->read (buffer, size);
+}
+
+size_t
+Serial::write (unsigned char* data, size_t length) {
+  return this->impl->write (data, length);
+}
+
+size_t
+Serial::write (const std::string &data) {
+  return this->impl->write (data);
+}
+
+void
+Serial::setPort (const std::string &port) {
+  this->impl->setPort (port);
+}
+
+std::string
+Serial::getPort () const {
+  return this->impl->getPort ();
+}
+
+void
+Serial::setTimeout (long timeout) {
+  this->impl->setTimeout (timeout);
+}
+
+long
+Serial::getTimeout () const {
+  return this->impl->getTimeout ();
+}
+
+void
+Serial::setBaudrate (int baudrate) {
+  this->impl->setBaudrate (baudrate);
+}
+
+int
+Serial::getBaudrate () const {
+  return this->impl->getBaudrate ();
+}
+
+void
+Serial::setBytesize (bytesize_t bytesize) {
+  this->impl->setBytesize (bytesize);
+}
+
+bytesize_t
+Serial::getBytesize () const {
+  return this->impl->getBytesize ();
+}
+
+void
+Serial::setParity (parity_t parity) {
+  this->impl->setParity (parity);
+}
+
+parity_t
+Serial::getParity () const {
+  return this->impl->getParity ();
+}
+
+void
+Serial::setStopbits (stopbits_t stopbits) {
+  this->impl->setStopbits (stopbits);
+}
+
+stopbits_t
+Serial::getStopbits () const {
+  return this->impl->getStopbits ();
+}
+
+void
+Serial::setFlowcontrol (flowcontrol_t flowcontrol) {
+  this->impl->setFlowcontrol (flowcontrol);
+}
+
+flowcontrol_t
+Serial::getFlowcontrol () const {
+  return this->impl->getFlowcontrol ();
+}
+
+
+
+
diff --git a/tests/proof_of_concepts/tokenizer.cc b/tests/proof_of_concepts/tokenizer.cc
new file mode 100644
index 0000000..da15a09
--- /dev/null
+++ b/tests/proof_of_concepts/tokenizer.cc
@@ -0,0 +1,31 @@
+#include 
+#include 
+#include 
+
+#include 
+#include 
+#include 
+#include 
+
+void
+_delimeter_tokenizer (std::string &data, std::vector &tokens,
+                      std::string delimeter)
+{
+  boost::split(tokens, data, boost::is_any_of(delimeter));
+}
+
+typedef boost::function&)> TokenizerType;
+
+int main(void) {
+  std::string data = "a\rb\rc\r";
+  std::vector tokens;
+  std::string delimeter = "\r";
+  
+  TokenizerType f = boost::bind(_delimeter_tokenizer, _1, _2, delimeter);
+  f(data, tokens);
+  
+  BOOST_FOREACH(std::string token, tokens)
+    std::cout << token << std::endl;
+  
+  return 0;
+}
\ No newline at end of file

From 0a6aabe719da1f8399eb82b8c13908120386d766 Mon Sep 17 00:00:00 2001
From: John Harrison 
Date: Wed, 11 Jan 2012 17:48:31 -0600
Subject: [PATCH 08/72] Adding in my unix implementation. reconfigurePort and
 read should probably work, but I haven't tested them yet. I am going to work
 on write later.

---
 include/serial/impl/unix.h |  67 +++++---
 include/serial/serial.h    |  17 +-
 serial.cmake               |   4 +-
 src/impl/unix.cc           | 324 ++++++++++++++++++++++++++++++-------
 src/serial.cc              |  77 +++++----
 5 files changed, 368 insertions(+), 121 deletions(-)

diff --git a/include/serial/impl/unix.h b/include/serial/impl/unix.h
index 08a94de..5d99ebc 100644
--- a/include/serial/impl/unix.h
+++ b/include/serial/impl/unix.h
@@ -1,6 +1,7 @@
 /*!
  * \file serial/impl/unix.h
  * \author  William Woodall 
+ * \author  John Harrison 
  * \version 0.1
  *
  * \section LICENSE
@@ -39,31 +40,43 @@
 
 namespace {
 
-class Serial::Serial_pimpl {
-public:
-  Serial_pimpl (const std::string &port,
-                int baudrate,
-                long timeout,
-                bytesize_t bytesize,
-                parity_t parity,
-                stopbits_t stopbits,
-                flowcontrol_t flowcontrol);
+using std::string;
 
-  virtual ~Serial_pimpl ();
+class serial::Serial::SerialImpl {
+public:
+  SerialImpl (const string &port,
+             int baudrate,
+             long timeout,
+             bytesize_t bytesize,
+             parity_t parity,
+             stopbits_t stopbits,
+             flowcontrol_t flowcontrol);
+
+  virtual ~SerialImpl ();
 
   void open ();
   void close ();
   bool isOpen ();
 
-  size_t read (unsigned char* buffer, size_t size = 1);
-  std::string read (size_t size = 1);
-  size_t read (std::string &buffer, size_t size = 1);
+	size_t available ();
+  string read (size_t size = 1);
+  size_t write (const string &data);
+	
+	void flush ();
+	void flushInput ();
+	void flushOutput ();
 
-  size_t write (unsigned char* data, size_t length);
-  size_t write (const std::string &data);
+	void sendBreak();
+	void setBreak();
+	void setRTS();
+	void setDTR();
+	void getCTS();
+	void getDSR();
+	void getRI();
+	void getCD();
 
-  void setPort (const std::string &port);
-  std::string getPort () const;
+  void setPort (const string &port);
+  string getPort () const;
 
   void setTimeout (long timeout);
   long getTimeout () const;
@@ -83,9 +96,25 @@ public:
   void setFlowcontrol (flowcontrol_t flowcontrol);
   flowcontrol_t getFlowcontrol () const;
 
-private:
-  int fd;
+protected:
+	void reconfigurePort ();
 
+private:
+  int fd_; // The current file descriptor.
+
+	bool isOpen_;
+	
+	int interCharTimeout_;
+	int xonxoff_;
+	int rtscts_;
+
+	string port_;               // Path to the file descriptor
+	int baudrate_;              // Baudrate
+	long timeout_;              // Timeout for read operations
+	bytesize_t bytesize_;       // Size of the bytes
+	parity_t parity_;           // Parity
+	stopbits_t stopbits_;       // Stop Bits
+	flowcontrol_t flowcontrol_; // Flow Control
 };
 
 }
diff --git a/include/serial/serial.h b/include/serial/serial.h
index 7286fe0..60733c4 100644
--- a/include/serial/serial.h
+++ b/include/serial/serial.h
@@ -37,6 +37,15 @@
 #ifndef SERIAL_H
 #define SERIAL_H
 
+#include 
+#include 
+
+#ifdef CXX_11
+#include 
+#else
+// #include 
+#endif
+
 namespace serial {
 
 /*!
@@ -191,7 +200,7 @@ public:
   * \see Serial::read(size_t)
   */
   size_t
-  read (const std::string &buffer, size_t size = 1);
+  read (std::string &buffer, size_t size = 1);
 
   /*! Write bytes from the data to the serial port by given length.
   * 
@@ -367,9 +376,9 @@ private:
   const Serial& operator=(Serial);
 
   // Pimpl idiom, d_pointer
-  class Serial_pimpl;
-  std::shared_ptr pimpl;
-
+  class SerialImpl;
+  // std::shared_ptr pimpl;
+  SerialImpl *pimpl;
 };
 
 class IOException : public std::exception {
diff --git a/serial.cmake b/serial.cmake
index 94a9fce..1362894 100644
--- a/serial.cmake
+++ b/serial.cmake
@@ -13,7 +13,7 @@ project(Serial)
 # Use clang if available
 IF(EXISTS /usr/bin/clang)
   set(CMAKE_CXX_COMPILER /usr/bin/clang++)
-  set(CMAKE_CXX_FLAGS -ferror-limit=5)
+  set(CMAKE_CXX_FLAGS "-ferror-limit=5 -std=c++0x -stdlib=libc++")
 ENDIF(EXISTS /usr/bin/clang)
 
 option(SERIAL_BUILD_TESTS "Build all of the Serial tests." OFF)
@@ -39,7 +39,7 @@ ENDIF(NOT DEFINED(LIBRARY_OUTPUT_PATH))
 include_directories(${PROJECT_SOURCE_DIR}/include)
 
 # Add default source files
-set(SERIAL_SRCS src/serial.cc src/serial_listener.cc)
+set(SERIAL_SRCS src/serial.cc src/impl/unix.cc) # src/serial_listener.cc)
 # Add default header files
 set(SERIAL_HEADERS include/serial/serial.h include/serial/serial_listener.h)
 
diff --git a/src/impl/unix.cc b/src/impl/unix.cc
index e4db489..261824c 100644
--- a/src/impl/unix.cc
+++ b/src/impl/unix.cc
@@ -1,127 +1,329 @@
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
 #include "serial/impl/unix.h"
 
-using namespace serial;
+#ifndef TIOCINQ
+#ifdef FIONREAD
+#define TIOCINQ FIONREAD
+#else
+#define TIOCINQ 0x541B
+#endif
+#endif
 
-Serial_pimpl::Serial_pimpl (const std::string &port, int baudrate,
-                            long timeout, bytesize_t bytesize,
-                            parity_t parity, stopbits_t stopbits,
-                            flowcontrol_t flowcontrol)
-: port(port), baudrate(baudrate), timeout(timeout), bytesize(bytesize),
-  parity(parity), stopbits(stopbits), flowcontrol(flowcontrol)
+using ::serial::Serial;
+using std::string;
+
+Serial::SerialImpl::SerialImpl (const string &port, int baudrate,
+                                    long timeout, bytesize_t bytesize,
+                                    parity_t parity, stopbits_t stopbits,
+																		flowcontrol_t flowcontrol)
+: fd_(-1), isOpen_(false), interCharTimeout_(-1), port_(port), baudrate_(baudrate),
+  timeout_(timeout), bytesize_(bytesize), parity_(parity), stopbits_(stopbits),
+	flowcontrol_(flowcontrol)
 {
-  
+	if (port_.empty() == false) {
+		this->open();
+	}
 }
 
-Serial_pimpl::~Serial_pimpl () {
-  
+Serial::SerialImpl::~SerialImpl () {
+  this->close();
 }
 
 void
-Serial_pimpl::open () {
-  
+Serial::SerialImpl::open () {
+	if (port_.empty() == false) {
+		throw "error";
+	}
+	if (isOpen_ == false) {
+		throw "error";
+	}
+	
+	fd_ = ::open (port_.c_str(), O_RDWR | O_NOCTTY | O_NONBLOCK);
+	
+	if (fd_ == -1) {
+//		printf("Error opening serial port %s - %s(%d).\n",
+//		       port.c_str(), strerror(errno), errno);
+		throw "Error"; // Error
+	}
+	
+	reconfigurePort();
+	isOpen_ = true;
 }
 
 void
-Serial_pimpl::close () {
-  
+Serial::SerialImpl::reconfigurePort () {
+	if (fd_ == -1) {
+		throw "Error"; // Can only operate on a valid file descriptor
+	}
+	
+	struct termios options; // The current options for the file descriptor
+	struct termios originalTTYAttrs; // The orignal file descriptor options
+	
+  uint8_t vmin = 0, vtime = 0;                // timeout is done via select
+  if (interCharTimeout_ == -1) {
+		vmin = 1;
+  	vtime = int(interCharTimeout_ * 10);
+	}
+	
+	if (tcgetattr(fd_, &originalTTYAttrs) == -1) {
+		throw "Error";
+	}
+	
+	options = originalTTYAttrs;
+	
+  // set up raw mode / no echo / binary
+  options.c_cflag |=  (CLOCAL|CREAD);
+  options.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL|
+                     ISIG|IEXTEN); //|ECHOPRT
+
+  options.c_oflag &= ~(OPOST);
+  options.c_iflag &= ~(INLCR|IGNCR|ICRNL|IGNBRK);
+#ifdef IUCLC
+  options.c_iflag &= ~IUCLC;
+#endif
+#ifdef PARMRK
+	options.c_iflag &= ~PARMRK;
+#endif
+
+  // setup baud rate
+	// TODO(ash_git): validate baud rate
+	cfsetspeed(&options, baudrate_);
+
+  // setup char len
+  options.c_cflag &= ~CSIZE;
+  if (bytesize_ == EIGHTBITS)
+      options.c_cflag |= CS8;
+  else if (bytesize_ == SEVENBITS)
+      options.c_cflag |= CS7;
+  else if (bytesize_ == SIXBITS)
+      options.c_cflag |= CS6;
+  else if (bytesize_ == FIVEBITS)
+      options.c_cflag |= CS5;
+  else
+      throw "ValueError(Invalid char len: %%r)";
+  // setup stopbits
+  if (stopbits_ == STOPBITS_ONE)
+      options.c_cflag &= ~(CSTOPB);
+  else if (stopbits_ == STOPBITS_ONE_POINT_FIVE)
+      options.c_cflag |=  (CSTOPB);  // XXX same as TWO.. there is no POSIX support for 1.5
+  else if (stopbits_ == STOPBITS_TWO)
+      options.c_cflag |=  (CSTOPB);
+	else 
+      throw "ValueError(Invalid stop bit specification:)";
+  // setup parity
+  options.c_iflag &= ~(INPCK|ISTRIP);
+  if (parity_ == PARITY_NONE) {
+    options.c_cflag &= ~(PARENB|PARODD);
+  }
+	else if (parity_ == PARITY_EVEN) {
+    options.c_cflag &= ~(PARODD);
+    options.c_cflag |=  (PARENB);
+  } 
+	else if (parity_ == PARITY_ODD) {
+    options.c_cflag |=  (PARENB|PARODD);
+  }
+	else {
+    throw "ValueError(Invalid parity:";
+	}
+  // setup flow control
+  // xonxoff
+#ifdef IXANY
+  if (xonxoff_)
+    options.c_iflag |=  (IXON|IXOFF); //|IXANY)
+  else
+    options.c_iflag &= ~(IXON|IXOFF|IXANY);
+#else
+  if (xonxoff_)
+    options.c_iflag |=  (IXON|IXOFF);
+  else
+    options.c_iflag &= ~(IXON|IXOFF);
+#endif
+  // rtscts
+#ifdef CRTSCTS
+  if (rtscts_)
+    options.c_cflag |=  (CRTSCTS);
+  else
+    options.c_cflag &= ~(CRTSCTS);
+#elif defined CNEW_RTSCTS
+  if (rtscts_)
+	  options.c_cflag |=  (CNEW_RTSCTS);
+  else
+    options.c_cflag &= ~(CNEW_RTSCTS);
+#else
+#error "OS Support seems wrong."
+#endif
+
+  // buffer
+  // vmin "minimal number of characters to be read. = for non blocking"
+  options.c_cc[VMIN] = vmin;
+  // vtime
+  options.c_cc[VTIME] = vtime;
+
+  // activate settings
+	::tcsetattr(fd_, TCSANOW, &options);
 }
+
+void
+Serial::SerialImpl::close () {
+	if (isOpen_ == true) {
+		if (fd_ != -1) {
+			::close(fd_);
+			fd_ = -1;
+		}
+		isOpen_ = false;
+	}
+}
+
 bool
-Serial_pimpl::isOpen () {
-  
+Serial::SerialImpl::isOpen () {
+  return isOpen_;
 }
 
 size_t
-Serial_pimpl::read (unsigned char* buffer, size_t size = 1) {
-  
+Serial::SerialImpl::available () {
+	if (!isOpen_) {
+		return 0;
+	}
+	int count = 0;
+	int result = ioctl(fd_, TIOCINQ, &count);
+	if (result == 0) {
+		return count;
+	}
+	else {
+		throw "Error";
+	}
 }
 
-std::string
-Serial_pimpl::read (size_t size = 1) {
-  
+string
+Serial::SerialImpl::read (size_t size) {
+  if (!isOpen_) {
+  	throw "PortNotOpenError()"; //
+  }
+	string message = "";
+	char buf[1024];
+	fd_set readfds;
+  while (message.length() < size) {
+		FD_ZERO(&readfds);
+		FD_SET(fd_, &readfds);
+		struct timeval timeout;
+		timeout.tv_sec = timeout_ / 1000000;
+		timeout.tv_usec = timeout_ % 1000000;
+		int r = select(1, &readfds, NULL, NULL, &timeout);
+	
+		if (r == -1 && errno == EINTR)
+			continue;
+
+		if (r == -1) {
+      perror("select()");
+      exit(EXIT_FAILURE);
+    }
+
+    if (FD_ISSET(fd_, &readfds)) {
+			memset(buf, 0, 1024);
+			size_t bytes_read = ::read(fd_, buf, 1024);
+	    // read should always return some data as select reported it was
+	    // ready to read when we get to this point.
+	    if (bytes_read < 1) {
+	        // Disconnected devices, at least on Linux, show the
+	        // behavior that they are always ready to read immediately
+	        // but reading returns nothing.
+	        throw "SerialException('device reports readiness to read but returned no data (device disconnected?)')";
+			}
+	    message.append(buf, bytes_read);
+		}
+		else {
+			break; // Timeout
+		}
+	}
+  return message;
 }
 
 size_t
-Serial_pimpl::read (std::string &buffer, size_t size = 1) {
-  
-}
-
-size_t
-Serial_pimpl::write (unsigned char* data, size_t length) {
-  
-}
-
-size_t
-Serial_pimpl::write (const std::string &data) {
+Serial::SerialImpl::write (const string &data) {
   
 }
 
 void
-Serial_pimpl::setPort (const std::string &port) {
-  
+Serial::SerialImpl::setPort (const string &port) {
+  port_ = port;
 }
 
-std::string
-Serial_pimpl::getPort () const {
-  
+string
+Serial::SerialImpl::getPort () const {
+  return port_;
 }
 
 void
-Serial_pimpl::setTimeout (long timeout) {
+Serial::SerialImpl::setTimeout (long timeout) {
   
 }
 
 long
-Serial_pimpl::getTimeout () const {
+Serial::SerialImpl::getTimeout () const {
   
 }
 
 void
-Serial_pimpl::setBaudrate (int baudrate) {
-  
+Serial::SerialImpl::setBaudrate (int baudrate) {
+	baudrate_ = baudrate;
+	reconfigurePort();
 }
 
 int
-Serial_pimpl::getBaudrate () const {
+Serial::SerialImpl::getBaudrate () const {
+  return baudrate_;
+}
+
+void
+Serial::SerialImpl::setBytesize (serial::bytesize_t bytesize) {
+  
+}
+
+serial::bytesize_t
+Serial::SerialImpl::getBytesize () const {
   
 }
 
 void
-Serial_pimpl::setBytesize (bytesize_t bytesize) {
+Serial::SerialImpl::setParity (serial::parity_t parity) {
   
 }
 
-bytesize_t
-Serial_pimpl::getBytesize () const {
+serial::parity_t
+Serial::SerialImpl::getParity () const {
   
 }
 
 void
-Serial_pimpl::setParity (parity_t parity) {
+Serial::SerialImpl::setStopbits (serial::stopbits_t stopbits) {
   
 }
 
-parity_t
-Serial_pimpl::getParity () const {
+serial::stopbits_t
+Serial::SerialImpl::getStopbits () const {
   
 }
 
 void
-Serial_pimpl::setStopbits (stopbits_t stopbits) {
+Serial::SerialImpl::setFlowcontrol (serial::flowcontrol_t flowcontrol) {
   
 }
 
-stopbits_t
-Serial_pimpl::getStopbits () const {
-  
-}
-
-void
-Serial_pimpl::setFlowcontrol (flowcontrol_t flowcontrol) {
-  
-}
-
-flowcontrol_t
-Serial_pimpl::getFlowcontrol () const {
+serial::flowcontrol_t
+Serial::SerialImpl::getFlowcontrol () const {
   
 }
 
diff --git a/src/serial.cc b/src/serial.cc
index 4637e72..4c1bfba 100644
--- a/src/serial.cc
+++ b/src/serial.cc
@@ -6,127 +6,134 @@
 #include "serial/impl/unix.h"
 #endif
 
-Serial::Serial (const std::string &port, int baudrate, 
+using serial::Serial;
+using serial::bytesize_t;
+using serial::parity_t;
+using serial::stopbits_t;
+using serial::flowcontrol_t;
+using std::string;
+
+Serial::Serial (const string &port, int baudrate, 
                             long timeout, bytesize_t bytesize,
                             parity_t parity, stopbits_t stopbits,
                             flowcontrol_t flowcontrol)
-: impl(new Serial_pimpl(port,baudrate,timeout,bytesize,parity,stopbits,
+: pimpl(new SerialImpl(port,baudrate,timeout,bytesize,parity,stopbits,
                         flowcontrol))
 {
   
 }
 
 Serial::~Serial () {
-  delete impl;
+  delete pimpl;
 }
 
 void
 Serial::open () {
-  this->impl->open ();
+  this->pimpl->open ();
 }
 
 void
 Serial::close () {
-  this->impl->close ();
+  this->pimpl->close ();
 }
 bool
 Serial::isOpen () {
-  return this->impl->isOpen ();
+  return this->pimpl->isOpen ();
 }
 
 size_t
-Serial::read (unsigned char* buffer, size_t size = 1) {
-  return this->impl->read (buffer, size);
+Serial::read (unsigned char* buffer, size_t size) {
+ // return this->pimpl->read (buffer, size);
 }
 
-std::string
-Serial::read (size_t size = 1) {
-  return this->impl->read (size);
+string
+Serial::read (size_t size) {
+  return this->pimpl->read (size);
 }
 
 size_t
-Serial::read (std::string &buffer, size_t size = 1) {
-  return this->impl->read (buffer, size);
+Serial::read (string &buffer, size_t size) {
+//  return this->pimpl->read (buffer, size);
 }
 
-size_t
-Serial::write (unsigned char* data, size_t length) {
-  return this->impl->write (data, length);
-}
+//size_t
+//Serial::write (unsigned char* data, size_t length) {
+//  return this->pimpl->write (data, length);
+//}
 
 size_t
-Serial::write (const std::string &data) {
-  return this->impl->write (data);
+Serial::write (const string &data) {
+  return this->pimpl->write (data);
 }
 
 void
-Serial::setPort (const std::string &port) {
-  this->impl->setPort (port);
+Serial::setPort (const string &port) {
+  this->pimpl->setPort (port);
 }
 
-std::string
+string
 Serial::getPort () const {
-  return this->impl->getPort ();
+  return this->pimpl->getPort ();
 }
 
 void
 Serial::setTimeout (long timeout) {
-  this->impl->setTimeout (timeout);
+  this->pimpl->setTimeout (timeout);
 }
 
 long
 Serial::getTimeout () const {
-  return this->impl->getTimeout ();
+  return this->pimpl->getTimeout ();
 }
 
 void
 Serial::setBaudrate (int baudrate) {
-  this->impl->setBaudrate (baudrate);
+  this->pimpl->setBaudrate (baudrate);
 }
 
 int
 Serial::getBaudrate () const {
-  return this->impl->getBaudrate ();
+  return this->pimpl->getBaudrate ();
 }
 
 void
 Serial::setBytesize (bytesize_t bytesize) {
-  this->impl->setBytesize (bytesize);
+  this->pimpl->setBytesize (bytesize);
 }
 
 bytesize_t
 Serial::getBytesize () const {
-  return this->impl->getBytesize ();
+  return this->pimpl->getBytesize ();
 }
 
 void
 Serial::setParity (parity_t parity) {
-  this->impl->setParity (parity);
+  this->pimpl->setParity (parity);
 }
 
 parity_t
 Serial::getParity () const {
-  return this->impl->getParity ();
+  return this->pimpl->getParity ();
 }
 
 void
 Serial::setStopbits (stopbits_t stopbits) {
-  this->impl->setStopbits (stopbits);
+  this->pimpl->setStopbits (stopbits);
 }
 
 stopbits_t
 Serial::getStopbits () const {
-  return this->impl->getStopbits ();
+  return this->pimpl->getStopbits ();
 }
 
 void
 Serial::setFlowcontrol (flowcontrol_t flowcontrol) {
-  this->impl->setFlowcontrol (flowcontrol);
+  this->pimpl->setFlowcontrol (flowcontrol);
 }
 
 flowcontrol_t
 Serial::getFlowcontrol () const {
-  return this->impl->getFlowcontrol ();
+  return this->pimpl->getFlowcontrol ();
 }
 
 

From f14ac390bf1b9ca20936800ee540ec8c4b990ed6 Mon Sep 17 00:00:00 2001
From: John Harrison 
Date: Wed, 11 Jan 2012 21:53:26 -0600
Subject: [PATCH 09/72] Implemented write, readline, readlines and available

---
 include/serial/impl/unix.h |  1 +
 include/serial/serial.h    | 28 ++++++++++--------
 src/impl/unix.cc           | 34 +++++++++++++---------
 src/serial.cc              | 58 ++++++++++++++++++++++++++++++++++----
 4 files changed, 91 insertions(+), 30 deletions(-)

diff --git a/include/serial/impl/unix.h b/include/serial/impl/unix.h
index 5d99ebc..9fe739a 100644
--- a/include/serial/impl/unix.h
+++ b/include/serial/impl/unix.h
@@ -105,6 +105,7 @@ private:
 	bool isOpen_;
 	
 	int interCharTimeout_;
+	int writeTimeout_;
 	int xonxoff_;
 	int rtscts_;
 
diff --git a/include/serial/serial.h b/include/serial/serial.h
index 60733c4..b4bca19 100644
--- a/include/serial/serial.h
+++ b/include/serial/serial.h
@@ -39,12 +39,8 @@
 
 #include 
 #include 
-
-#ifdef CXX_11
-#include 
-#else
-// #include 
-#endif
+#include 
+#include 
 
 namespace serial {
 
@@ -159,6 +155,11 @@ public:
   void
   close ();
 
+  /* Return the number of characters in the buffer.
+  */
+  size_t
+  available();
+
   /*! Read a given amount of bytes from the serial port.
   * 
   * If a timeout is set it may return less characters than requested. With
@@ -172,8 +173,8 @@ public:
   * 
   * \return A size_t representing the number of bytes actually read.
   */
-  size_t
-  read (unsigned char* buffer, size_t size = 1);
+  //size_t
+  //read (unsigned char* buffer, size_t size = 1);
 
   /*! Read a given amount of bytes from the serial port.
   * 
@@ -188,6 +189,9 @@ public:
   std::string
   read (size_t size = 1);
 
+  std::string readline(size_t size = std::numeric_limits::max(), std::string eol = "\n");
+  std::vector readlines(std::string eol = "\n");
+
   /*! Read a given amount of bytes from the serial port.
   * 
   * Reads into a std::string by reference rather than returning it.
@@ -199,8 +203,8 @@ public:
   * 
   * \see Serial::read(size_t)
   */
-  size_t
-  read (std::string &buffer, size_t size = 1);
+  //size_t
+  //read (std::string &buffer, size_t size = 1);
 
   /*! Write bytes from the data to the serial port by given length.
   * 
@@ -211,8 +215,8 @@ public:
   * 
   * \return A size_t representing the number of bytes actually written.
   */
-  size_t
-  write (unsigned char* data, size_t length);
+  //size_t
+  //write (unsigned char* data, size_t length);
 
   /*! Write a string to the serial port.
   * 
diff --git a/src/impl/unix.cc b/src/impl/unix.cc
index 261824c..766aba4 100644
--- a/src/impl/unix.cc
+++ b/src/impl/unix.cc
@@ -219,8 +219,8 @@ Serial::SerialImpl::read (size_t size) {
 		FD_ZERO(&readfds);
 		FD_SET(fd_, &readfds);
 		struct timeval timeout;
-		timeout.tv_sec = timeout_ / 1000000;
-		timeout.tv_usec = timeout_ % 1000000;
+		timeout.tv_sec = timeout_ / 1000;
+		timeout.tv_usec = timeout_ % 1000;
 		int r = select(1, &readfds, NULL, NULL, &timeout);
 	
 		if (r == -1 && errno == EINTR)
@@ -253,7 +253,15 @@ Serial::SerialImpl::read (size_t size) {
 
 size_t
 Serial::SerialImpl::write (const string &data) {
-  
+  if (isOpen_ == false) {
+		throw "portNotOpenError";
+	}
+  size_t t = data.length();
+	size_t n = ::write(fd_, data.c_str(), data.length());
+	if (n == -1) {
+		throw "Write error";
+	}
+  return n;
 }
 
 void
@@ -268,12 +276,12 @@ Serial::SerialImpl::getPort () const {
 
 void
 Serial::SerialImpl::setTimeout (long timeout) {
-  
+  timeout_ = timeout;
 }
 
 long
 Serial::SerialImpl::getTimeout () const {
-  
+  return timeout_;
 }
 
 void
@@ -289,42 +297,42 @@ Serial::SerialImpl::getBaudrate () const {
 
 void
 Serial::SerialImpl::setBytesize (serial::bytesize_t bytesize) {
-  
+	bytesize_ = bytesize;
 }
 
 serial::bytesize_t
 Serial::SerialImpl::getBytesize () const {
-  
+  return bytesize_;
 }
 
 void
 Serial::SerialImpl::setParity (serial::parity_t parity) {
-  
+  parity_ = parity;
 }
 
 serial::parity_t
 Serial::SerialImpl::getParity () const {
-  
+  return parity_;
 }
 
 void
 Serial::SerialImpl::setStopbits (serial::stopbits_t stopbits) {
-  
+  stopbits_ = stopbits;
 }
 
 serial::stopbits_t
 Serial::SerialImpl::getStopbits () const {
-  
+  return stopbits_;
 }
 
 void
 Serial::SerialImpl::setFlowcontrol (serial::flowcontrol_t flowcontrol) {
-  
+  flowcontrol_ = flowcontrol;
 }
 
 serial::flowcontrol_t
 Serial::SerialImpl::getFlowcontrol () const {
-  
+  return flowcontrol_;
 }
 
 
diff --git a/src/serial.cc b/src/serial.cc
index 4c1bfba..804c7f7 100644
--- a/src/serial.cc
+++ b/src/serial.cc
@@ -12,6 +12,7 @@ using serial::parity_t;
 using serial::stopbits_t;
 using serial::flowcontrol_t;
 using std::string;
+using std::vector;
 
 Serial::Serial (const string &port, int baudrate, 
                             long timeout, bytesize_t bytesize,
@@ -42,18 +43,65 @@ Serial::isOpen () {
 }
 
 size_t
-Serial::read (unsigned char* buffer, size_t size) {
- // return this->pimpl->read (buffer, size);
+Serial::available () {
+  return this->pimpl->available();
 }
 
+//size_t
+//Serial::read (unsigned char* buffer, size_t size) {
+ // return this->pimpl->read (buffer, size);
+//}
+
 string
 Serial::read (size_t size) {
   return this->pimpl->read (size);
 }
 
-size_t
-Serial::read (string &buffer, size_t size) {
-//  return this->pimpl->read (buffer, size);
+string
+Serial::readline(size_t size, string eol) {
+  size_t leneol = eol.length();
+  string line;
+  while (true) {
+    string c = read(1);
+    if (c.empty()) {
+      line += c;
+      if (line.substr(line.length() - leneol, leneol) == eol) {
+        break;
+      }
+      if (line.length() >= size) {
+        break;
+      }
+    }
+    else {
+      // Timeout
+      break;
+    }
+  }
+
+  return line;
+}
+
+vector
+Serial::readlines(string eol) {
+  if (this->pimpl->getTimeout() < 0) {
+    throw "Error, must be set for readlines";
+  }
+  size_t leneol = eol.length();
+  vector lines;
+  while (true) {
+    string line = readline(std::numeric_limits::max(), eol);
+    if (!line.empty()) {
+      lines.push_back(line);
+      if (line.substr(line.length() - leneol, leneol) == eol)
+        break;
+    }
+    else {
+      // Timeout
+      break;
+    }
+  }
+
+  return lines;
 }
 
 //size_t

From 99bf74faae80fefa4c70cf6c53206fff423b0567 Mon Sep 17 00:00:00 2001
From: John Harrison 
Date: Wed, 11 Jan 2012 22:03:32 -0600
Subject: [PATCH 10/72] Removing some unenessacary repetition

---
 src/serial.cc | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/src/serial.cc b/src/serial.cc
index 804c7f7..3462019 100644
--- a/src/serial.cc
+++ b/src/serial.cc
@@ -13,6 +13,8 @@ using serial::stopbits_t;
 using serial::flowcontrol_t;
 using std::string;
 using std::vector;
+using std::numeric_limits;
+using std::size_t;
 
 Serial::Serial (const string &port, int baudrate, 
                             long timeout, bytesize_t bytesize,
@@ -89,7 +91,7 @@ Serial::readlines(string eol) {
   size_t leneol = eol.length();
   vector lines;
   while (true) {
-    string line = readline(std::numeric_limits::max(), eol);
+    string line = readline(numeric_limits::max(), eol);
     if (!line.empty()) {
       lines.push_back(line);
       if (line.substr(line.length() - leneol, leneol) == eol)

From 7c0c976033b8e20867e2c111d24bd7eeb0ea86ae Mon Sep 17 00:00:00 2001
From: William Woodall 
Date: Wed, 11 Jan 2012 23:07:58 -0600
Subject: [PATCH 11/72] Still working on Serial listener refactor, not working,
 fixing to make big changes.

---
 examples/serial_listener_example.cc | 101 +++---
 include/serial/impl/unix.h          |  12 +-
 include/serial/serial.h             |   9 +-
 include/serial/serial_listener.h    | 522 ++++++++++++++++++++--------
 serial.cmake                        |  19 +-
 src/impl/unix.cc                    | 117 +++----
 src/serial.cc                       |  59 ++--
 src/serial_listener.cc              | 403 +++++----------------
 tests/serial_listener_tests.cc      |  13 +-
 9 files changed, 646 insertions(+), 609 deletions(-)

diff --git a/examples/serial_listener_example.cc b/examples/serial_listener_example.cc
index 5c0555b..2cd5b20 100644
--- a/examples/serial_listener_example.cc
+++ b/examples/serial_listener_example.cc
@@ -5,59 +5,80 @@
 
 using namespace serial;
 
-void default_handler(std::string line) {
-  std::cout << "default_handler got a: " << line << std::endl;
+void default_handler(std::string token) {
+  std::cout << "default_handler got a: " << token << std::endl;
 }
 
-void callback(std::string line) {
-  std::cout << "callback got a: " << line << std::endl;
+void callback(std::string token) {
+  std::cout << "callback got a: " << token << std::endl;
 }
 
-#if 1
 int main(void) {
+  // Assuming this device prints the string 'pre-substr-post\r' at 100Hz
   Serial serial("/dev/tty.usbmodemfd1231", 115200);
 
   SerialListener listener;
-  // Set the time to live for messages to 10 milliseconds
-  listener.setTimeToLive(10);
   listener.startListening(serial);
 
-  listener.listenFor(SerialListener::startsWith("V="), callback);
+  // Set the tokenizer
+  //  This is the same as the default delimeter, so an explicit call to
+  //  setTokenizer is not necessary if your data is \r delimited.
+  //  You can create your own Tokenizer as well.
+  listener.setTokenizer(SerialListener::delimeter_tokenizer("\r"));
 
-  serial.write("?$1E\r");
-  if (!listener.listenForStringOnce("?$1E")) {
-    std::cerr << "Didn't get conformation of device version!" << std::endl;
-    return 1;
+  // Method #1:
+  //  comparator, callback - async
+  FilterPtr f1 =
+    listener.createFilter(SerialListener::startsWith("pre"), callback);
+  SerialListener::sleep(15); // Sleep 15ms, to let the data come in
+  listener.removeFilter(f1); // Not scoped, must be removed explicity
+
+  // Method #2:
+  //  comparator - blocking
+  {
+    BlockingFilter f2 =
+      listener.createBlockingFilter(SerialListener::endsWith("post"));
+    for (size_t i = 0; i < 3; i++) {
+      std::string token = f2.wait(100); // Wait for 100 ms or a matched token
+      if (token == "")
+        std::cout << "Found something ending with 'post'" << std::endl;
+      else
+        std::cout << "Did not find something ending with 'post'" << std::endl;
+    }
   }
+  // BlockingFilter is scoped and will remove itself, so no removeFilter
+  // required, but a call like `listener.removeFilter(BlockingFilter) will
+  // remove it from the filter list so wait will always timeout.
 
-  serial.write("?V\r");
-  serial.write("# 1\r");
-
-  while (true) {
-    // Sleep 100 ms
-    SerialListener::sleep(100);
+  // Method #3:
+  //  comparator, token buffer size - blocking
+  {
+    // Give it a comparator, then a buffer size of 10
+    BufferedFilter f3 =
+      listener.createBufferedFilter(SerialListener::contains("substr"), 10);
+    SerialListener::sleep(75); // Sleep 75ms, should have about 7
+    std::cout << "Caught " << f3.count();
+    std::cout << " tokens containing 'substr'" << std::endl;
+    for(size_t i = 0; i < 20; ++i) {
+      std::string token = f3.wait(5); // Pull message from the buffer
+      if (token == "") // If an empty string is returned, a timeout occured
+        break;
+    }
+    f3.clear(); // Empties the buffer
+    if (f3.wait(0) == "") // Non-blocking wait
+      std::cout << "We won the race condition!" << std::endl;
+    else
+      std::cout << "We lost the race condition..." << std::endl;
+    // The buffer is circular, so the oldest matches will be dropped first
   }
+  // BufferedFilter is scoped and will remove itself just like BlockingFilter.
+
+  // Method #4:
+  //  callback - async
+  // Gets called if a token doesn't match a filter
+  listener.setDefaultHandler(default_handler);
+  SerialListener::sleep(25); // Sleep 25 ms, so some default callbacks occur
+
+  return 0;
 
 }
-#endif
-
-#if 0
-int main(void) {
-  Serial serial("/dev/tty.usbmodemfd1231", 115200);
-
-  serial.write("?$1E\r");
-  SerialListener::sleep(10);
-  // if ("?$1E\r" != serial.read(5)) {
-  //   std::cerr << "Didn't get conformation of device version!" << std::endl;
-  //   return 1;
-  // }
-
-  serial.write("?V\r");
-  serial.write("# 1\r");
-
-  while (true) {
-    std::cout << serial.read(5) << std::endl;
-  }
-
-}
-#endif
diff --git a/include/serial/impl/unix.h b/include/serial/impl/unix.h
index 08a94de..316634d 100644
--- a/include/serial/impl/unix.h
+++ b/include/serial/impl/unix.h
@@ -37,7 +37,7 @@
 
 #include "serial/serial.h"
 
-namespace {
+namespace serial {
 
 class Serial::Serial_pimpl {
 public:
@@ -84,7 +84,17 @@ public:
   flowcontrol_t getFlowcontrol () const;
 
 private:
+  // Serial handle
   int fd;
+  
+  // Parameters
+  std::string port;
+  int baudrate;
+  long timeout;
+  bytesize_t bytesize;
+  parity_t parity;
+  stopbits_t stopbits;
+  flowcontrol_t flowcontrol;
 
 };
 
diff --git a/include/serial/serial.h b/include/serial/serial.h
index 7286fe0..376f461 100644
--- a/include/serial/serial.h
+++ b/include/serial/serial.h
@@ -33,10 +33,13 @@
  * This provides a cross platform interface for interacting with Serial Ports.
  */
 
-
 #ifndef SERIAL_H
 #define SERIAL_H
 
+#include 
+#include  // std::shared_ptr
+#include 
+
 namespace serial {
 
 /*!
@@ -191,7 +194,7 @@ public:
   * \see Serial::read(size_t)
   */
   size_t
-  read (const std::string &buffer, size_t size = 1);
+  read (std::string &buffer, size_t size = 1);
 
   /*! Write bytes from the data to the serial port by given length.
   * 
@@ -368,7 +371,7 @@ private:
 
   // Pimpl idiom, d_pointer
   class Serial_pimpl;
-  std::shared_ptr pimpl;
+  Serial_pimpl * pimpl;
 
 };
 
diff --git a/include/serial/serial_listener.h b/include/serial/serial_listener.h
index 2204849..1c8e5de 100644
--- a/include/serial/serial_listener.h
+++ b/include/serial/serial_listener.h
@@ -38,6 +38,7 @@
 
 // STL
 #include 
+#include 
 
 // Serial
 #include 
@@ -45,19 +46,32 @@
 // Boost
 #include 
 #include 
-#include 
-#include 
 #include 
+#include 
 
 namespace serial {
 
 /*!
- * This is a general function type that is used as the callback prototype 
- * for asynchronous functions like the default handler callback and the 
+ * This is an alias to boost::shared_ptr used for tokens.
+ * 
+ * This is the type used internally and is the type returned in a vector by
+ * the tokenizer.  The shared_ptr allows for the token to be stored and kept
+ * around long enough to be used by the comparators and callbacks, but no
+ * longer.  This internal storage is passed as a const std::string reference
+ * to callbacks, like the DataCallback function type, to prevent implicit
+ * copying.
+ * 
+ * \see serial::TokenizerType, serial::SerialListener::setTokenizer
+ */
+typedef boost::shared_ptr TokenPtr;
+
+/*!
+ * This is a general function type that is used as the callback prototype
+ * for asynchronous functions like the default handler callback and the
  * listenFor callbacks.
  * 
- * The function takes a std::string reference and returns nothing, it is 
- * simply passing the resulting line detected by the comparator to the user's 
+ * The function takes a std::string reference and returns nothing, it is
+ * simply passing the resulting line detected by the comparator to the user's
  * callback for processing.
  * 
  * \see SerialListener::listenFor, SerialListener::setDefaultHandler
@@ -65,11 +79,11 @@ namespace serial {
 typedef boost::function DataCallback;
 
 /*!
- * This is a general function type that is used as the comparator callback 
+ * This is a general function type that is used as the comparator callback
  * prototpe for the listenFor* type functions.
  * 
- * The function takes a std::string reference and returns true if the string 
- * matches what the comparator is looking for and false if it does not, unless 
+ * The function takes a std::string reference and returns true if the string
+ * matches what the comparator is looking for and false if it does not, unless
  * otherwise specified.
  * 
  * \see SerialListener::listenFor, SerialListener::listenForOnce
@@ -79,10 +93,10 @@ typedef boost::function ComparatorType;
 /*!
  * This function type describes the prototype for the logging callbacks.
  * 
- * The function takes a std::string reference and returns nothing.  It is 
- * called from the library when a logging message occurs.  This 
- * allows the library user to hook into this and integrate it with their own 
- * logging system.  It can be set with any of the setHandler 
+ * The function takes a std::string reference and returns nothing.  It is
+ * called from the library when a logging message occurs.  This
+ * allows the library user to hook into this and integrate it with their own
+ * logging system.  It can be set with any of the setHandler
  * functions.
  * 
  * \see SerialListener::setInfoHandler, SerialListener::setDebugHandler, 
@@ -104,27 +118,214 @@ typedef boost::function ExceptionCallback;
 /*!
  * This function type describes the prototype for the tokenizer callback.
  * 
- * The function should take a std::string reference and tokenize it into a 
- * several std::string's and store them in the given 
- * std::vector reference.  There are some default ones or the 
- * user can create their own.
+ * The function should take a std::string reference and tokenize it into a
+ * several TokenPtr's and store them in the given std::vector 
+ * reference.  There are some default ones or the user can create their own.
  * 
- * The last element in the std::vector of std::string's should always be 
- * either an empty string ("") or the last partial message.  The last element 
- * in the std::vector will be put back into the data buffer so that if it is 
+ * The last element in the std::vector of TokenPtr's should always be
+ * either an empty string ("") or the last partial message.  The last element
+ * in the std::vector will be put back into the data buffer so that if it is
  * incomplete it can be completed when more data is read.
  * 
- * Example: A delimeter tokenizer with a delimeter of "\r".  The result would 
- * be: "msg1\rmsg2\r" -> ["msg1", "msg2", ""] for all complete messages, or: 
- * "msg1\rpartial_msg2" -> ["msg1","partial_msg2"] for partial messages.
+ * Example: A delimeter tokenizer with a delimeter of "\r".  The result would
+ * be: "msg1\rmsg2\r" -> ["msg1", "msg2", ""] for two complete messages, or:
+ * "msg1\rpartial_msg2" -> ["msg1","partial_msg2"] for one complete message 
+ * and one partial message.
  * 
- * \see SerialListener::setTokenizer, serial::delimeter_tokenizer
+ * \see SerialListener::setTokenizer, serial::delimeter_tokenizer,
+ * serial::TokenPtr
  */
-typedef boost::function&)>
+typedef boost::function&)>
 TokenizerType;
 
-/*! This is a convenience alias for boost::uuids::uuid. */
-typedef boost::uuids::uuid uuid_type; // uuid_t is already taken! =(
+/*!
+ * Represents a filter which new data is passed through.
+ * 
+ * The filter consists of a comparator and a callback.  The comparator takes a 
+ * token and returns true if it matches, false if it doesn't.  If a match 
+ * occurs the serial listener will dispatch a call of the callback with the 
+ * matched data in a another thread.  The comparator should be as short as 
+ * possible, but the callback can be longer since it is executed in a thread 
+ * or thread pool.
+ * 
+ * \param comparator A ComparatorType that matches incoming data, returns true 
+ * for a match, false othewise.
+ * 
+ * \param callback A DataCallback that gets called when a match occurs.
+ * 
+ * \see serial::ComparatorType, serial::DataCallback, serial::FilterPtr
+ */
+class Filter
+{
+public:
+  Filter (ComparatorType comparator, DataCallback callback)
+  : comparator(comparator), callback(callback) {}
+  virtual ~Filter () {}
+
+  ComparatorType comparator;
+  DataCallback callback;
+};
+
+/*!
+ * This is an alias to boost::shared_ptr used for tokens.
+ * 
+ * This is used internally and is returned from SerialListener::listenFor like
+ * functions so that users can later remove those filters by passing the
+ * FilterPtr.
+ * 
+ * \see serial::Filter, serial::SerialListener::listenFor,
+ * serial::SerialListener::listenForOnce
+ */
+typedef boost::shared_ptr FilterPtr;
+
+/*!
+ * This is the a filter that provides a wait function for blocking until a 
+ * match is found.
+ * 
+ * This should probably not be created manually, but instead should be 
+ * constructed using SerialListener::createBlockingFilter(ComparatorType)
+ * function which returns a BlockingFilter instance.
+ * 
+ * \see serial::SerialListener::ComparatorType,
+ * serial::SerialListener::createBlockingFilter
+ */
+class BlockingFilter
+{
+public:
+  BlockingFilter (ComparatorType comparator,
+                  boost::shared_ptr listener)
+  : listener(listener)
+  {
+    DataCallback cb = boost::bind(&BlockingFilter::callback, this, _1);
+    this->filter_ptr = listener.createFilter(comparator, cb);
+  }
+
+  virtual ~BlockingFilter () {
+    this->listener.removeFilter(filter_ptr);
+    this->result = "";
+    this->cond.notify_all();
+  }
+
+  /*!
+   * Waits a given number of milliseconds or until a token is matched.  If a
+   * token is matched it is returned, otherwise an empty string is returned.
+   * 
+   * \param ms Time in milliseconds to wait on a new token.
+   * 
+   * \return std::string token that was matched or "" if none were matched.
+   */
+  std::string wait(size_t ms) {
+    this->result = "";
+    boost::unique_lock lock(this->mutex);
+    this->cond.timed_wait(lock, boost::posix_time::milliseconds(ms));
+    return this->result;
+  }
+
+private:
+  void callback(const std::string& token) {
+    this->cond.notify_all();
+    this->result = token;
+  }
+
+  FilterPtr filter_ptr;
+  boost::shared_ptr listener;
+  boost::condition_variable cond;
+  boost::mutex mutex;
+  std::string result;
+
+};
+
+/*!
+ * This is the a filter that provides a wait function for blocking until a
+ * match is found.  It will also buffer up to a given buffer size of tokens so
+ * that they can be counted or accessed after they are matched by the filter.
+ * 
+ * This should probably not be created manually, but instead should be 
+ * constructed using SerialListener::createBufferedFilter(ComparatorType)
+ * function which returns a BufferedFilter instance.
+ * 
+ * The internal buffer is a circular queue buffer, so when the buffer is full,
+ * the oldest token is dropped and the new one is added.  Additionally, when
+ * wait is a called the oldest available token is returned.
+ * 
+ * \see serial::SerialListener::ComparatorType,
+ * serial::SerialListener::createBufferedFilter
+ */
+class BufferedFilter
+{
+public:
+  BufferedFilter (ComparatorType comparator, size_t buffer_size,
+                  boost::shared_ptr listener)
+  : listener(listener), buffer_size(buffer_size)
+  {
+    DataCallback cb = boost::bind(&BlockingFilter::callback, this, _1);
+    this->filter_ptr = listener.createFilter(comparator, cb);
+  }
+
+  virtual ~BufferedFilter () {
+    this->listener.removeFilter(filter_ptr);
+    this->queue.clear();
+    this->result = "";
+    this->cond.notify_all();
+  }
+
+  /*!
+   * Waits a given number of milliseconds or until a matched token is 
+   * available in the buffer.  If a token is matched it is returned, otherwise
+   * an empty string is returned.
+   * 
+   * \param ms Time in milliseconds to wait on a new token.  If ms is set to 0 
+   * then it will try to get a new token if one is available but will not 
+   * block.
+   * 
+   * \return std::string token that was matched or "" if none were matched.
+   */
+  std::string wait(size_t ms) {
+    if (ms == 0)
+      if (!this->queue.try_pop(this->result))
+        this->result = "";
+    else
+      if (!this->queue.timed_wait_and_pop(this->result, ms))
+        this->result = "";
+    return result;
+  }
+
+  /*!
+   * Clears the buffer of any tokens.
+   */
+  void clear() {
+    queue.clear();
+  }
+
+  /*!
+   * Returns the number of tokens waiting in the buffer.
+   */
+  size_t count() {
+    return queue.size();
+  }
+
+  /*!
+   * Returns the capacity of the buffer.
+   */
+  size_t capacity() {
+    return buffer_size;
+  }
+
+private:
+  void callback(const std::string &token) {
+    std::string throw_away;
+    if (this->queue.size() == buffer_size)
+      this->queue.wait_and_pop(throw_away);
+    this->queue.push(token);
+  }
+
+  FilterPtr filter_ptr;
+  size_t buffer_size;
+  boost::shared_ptr listener;
+  ConcurrentQueue queue;
+  std::string result;
+
+};
 
 /*!
  * This is a general exception generated by the SerialListener class.
@@ -220,13 +421,6 @@ public:
   }
 };
 
-
-namespace filter_type {
-typedef enum {
-  nonblocking, blocking
-} FilterType;
-}
-
 /*!
  * Listens to a serial port, facilitates asynchronous reading
  */
@@ -245,22 +439,11 @@ public:
 
 /***** Configurations ******/
 
-  /*!
-   * Sets the time-to-live (ttl) for messages waiting to be processsed.
-   * 
-   * Messages are processed before checking for expiration, therefore they 
-   * will always be passed through filters once before being removed 
-   * due to ttl expiration. The default value for this is 10 ms.
-   * 
-   * \param ms Time in milliseconds until messages are purged from the buffer.
-   */
-  void setTimeToLive (size_t ms = 10);
-
   /*!
    * Sets the tokenizer to be used when tokenizing the data into tokens.
    * 
    * This function is given a std::string of data and is responsible for 
-   * tokenizing that data into a std::vector of data tokens.
+   * tokenizing that data into a std::vector of data tokens.
    * The default tokenizer splits the data by the ascii return carriage.
    * The user can create their own tokenizer or use one of the default ones.
    * 
@@ -268,10 +451,21 @@ public:
    * 
    * \see serial::TokenizerType, serial::delimeter_tokenizer
    */
-  void setTokenizer (TokenizerType tokenizer) {
+  void
+  setTokenizer (TokenizerType tokenizer) {
     this->tokenize = tokenizer;
   }
 
+  /*!
+   * Sets the number of bytes to be read at a time by the listener.
+   * 
+   * \param chunk_size Number of bytes to be read at a time.
+   */
+  void
+  setChunkSize (size_t chunk_size) {
+    this->chunk_size = chunk_size;
+  }
+
 /***** Start and Stop Listening ******/
 
   /*!
@@ -280,7 +474,8 @@ public:
    * \param serial_port Pointer to a serial::Serial object that is used to 
    * retrieve new data.
    */
-  void startListening (serial::Serial &serial_port);
+  void
+  startListening (serial::Serial &serial_port);
 
   /*!
    * Stops the listening thread and blocks until it completely stops.
@@ -288,38 +483,11 @@ public:
    * This function also clears all of the active filters from listenFor and 
    * similar functions.
    */
-  void stopListening ();
+  void
+  stopListening ();
 
 /***** Filter Functions ******/
 
-  /*!
-   * Blocks until the given string is detected or until the timeout occurs.
-   * 
-   * \param token std::string that should be watched for, this string must 
-   * match the message exactly.
-   * 
-   * \param timeout in milliseconds before timing out and returning false.
-   * Defaults to 1000 milliseconds or 1 second.
-   * 
-   * \return bool If true then the token was detected before the token, false 
-   * if the token was not heard and the timeout occured.
-   */
-  bool listenForStringOnce (std::string token, size_t timeout = 1000);
-
-  /*!
-   * Blocks until the comparator returns true or until the timeout occurs.
-   * 
-   * \param comparator ComparatorType function that should return true if the
-   * given std::string matches otherwise false.
-   * 
-   * \param timeout in milliseconds before timing out and returning false.
-   * Defaults to 1000 milliseconds or 1 second.
-   * 
-   * \return bool If true then the token was detected before the token, false 
-   * if the token was not heard and the timeout occured.
-   */
-  bool listenForOnce (ComparatorType comparator, size_t timeout = 1000);
-
   /*!
    * Setups up a filter that calls a callback when a comparator returns true.
    * 
@@ -335,23 +503,84 @@ public:
    * \param callback This is the handler for when a match occurs. It is given 
    * a std::string reference of the line that matched your comparator.
    * 
-   * \return boost::uuids::uuid a unique identifier used to remove the filter.
+   * \return boost::shared_ptr so you can remove it later.
+   * 
+   * \see SerialListener::stopListeningFor
    */
-  uuid_type listenFor (ComparatorType comparator, DataCallback callback);
+  FilterPtr
+  listenFor (ComparatorType comparator, DataCallback callback);
+
+  /*!
+   * Blocks until the comparator returns true or until the timeout occurs.
+   * 
+   * \param comparator ComparatorType function that should return true if the
+   * given std::string matches otherwise false.
+   * 
+   * \param timeout in milliseconds before timing out and returning false.
+   * Defaults to 1000 milliseconds or 1 second.
+   * 
+   * \return std::string the token that was matched, returns an empty string 
+   * if the timeout occurs first.
+   * i.e. if (listenForOnce(...) != "") // Got match
+   */
+  std::string
+  listenForOnce (ComparatorType comparator, size_t timeout = 1000);
+
+  /*!
+   * Writes to the seiral port then blocks until the comparator returns true 
+   * or until the timeout occurs.
+   * 
+   * This function creates a filter, writes the data, then waits for it to 
+   * match atleast once.
+   * 
+   * \param to_be_written const std::string reference of data to be written to
+   * the serial port.
+   * 
+   * \param comparator ComparatorType function that should return true if the
+   * given std::string matches otherwise false.
+   * 
+   * \param timeout in milliseconds before timing out and returning false.
+   * Defaults to 1000 milliseconds or 1 second.
+   * 
+   * \return std::string the token that was matched, returns an empty string 
+   * if the timeout occurs first.
+   * i.e. if (listenForOnce(...) != "") // Got match
+   */
+  std::string
+  listenForOnce (ComparatorType comparator, size_t timeout = 1000);
+
+  /*!
+   * Blocks until the given string is detected or until the timeout occurs.
+   * 
+   * \param token std::string that should be watched for, this string must 
+   * match the message exactly.
+   * 
+   * \param timeout in milliseconds before timing out and returning false.
+   * Defaults to 1000 milliseconds or 1 second.
+   * 
+   * \return bool If true then the token was detected before the token, false 
+   * if the token was not heard and the timeout occured.
+   */
+  bool
+  listenForStringOnce (std::string token, size_t timeout = 1000);
 
   /*!
    * Removes a filter by a given uuid.
    * 
    * The uuid for a filter is returned by the listenFor function.
    * 
-   * \param filter_uuid The uuid of the filter to be removed.
+   * \param filter_ptr A shared pointer to the filter.
+   * 
+   * \see SerialListener::listenFor
    */
-  void stopListeningFor (uuid_type filter_uuid);
+   void
+   stopListeningFor (FilterPtr filter_ptr);
 
   /*!
    * Stops listening for anything, but doesn't stop reading the serial port.
    */
-  void stopListeningForAll ();
+  void
+  stopListeningForAll ();
 
 /***** Hooks and Handlers ******/
 
@@ -368,8 +597,9 @@ public:
    * 
    * \see serial::DataCallback, SerialListener::setInfoHandler
    */
-  void setDefaultHandler(DataCallback default_handler) {
-    this->default_handler = default_handler;
+  void
+  setDefaultHandler (DataCallback default_handler) {
+    this->_default_handler = default_handler;
   }
 
   /*!
@@ -421,7 +651,8 @@ public:
    * 
    * \see serial::LoggingCallback
    */
-  void setInfoHandler(LoggingCallback info_handler) {
+  void
+  setInfoHandler (LoggingCallback info_handler) {
     this->info = info_handler;
   }
   
@@ -438,7 +669,8 @@ public:
    * 
    * \see serial::LoggingCallback, SerialListener::setInfoHandler
    */
-  void setDebugHandler(LoggingCallback debug_handler) {
+  void
+  setDebugHandler (LoggingCallback debug_handler) {
     this->debug = debug_handler;
   }
   
@@ -455,7 +687,8 @@ public:
    * 
    * \see serial::LoggingCallback, SerialListener::setInfoHandler
    */
-  void setWarningHandler(LoggingCallback warning_handler) {
+  void
+  setWarningHandler (LoggingCallback warning_handler) {
     this->warn = warning_handler;
   }
 
@@ -491,12 +724,10 @@ public:
    * \see SerialListener::setTokenizer, serial::TokenizerType
    */
   static TokenizerType
-  delimeter_tokenizer (std::string delimeter);
-
-  // delimeter tokenizer function
-  static void
-  _delimeter_tokenizer (std::string &data, std::vector &tokens,
-                        std::string delimeter);
+  delimeter_tokenizer (std::string delimeter) {
+    return boost::bind(&SerialListener::_delimeter_tokenizer,
+                       _1, _2, delimeter);
+  }
 
   /*!
    * This returns a comparator that matches only the exact string given.
@@ -519,11 +750,9 @@ public:
    * serial::ComparatorType
    */
   static ComparatorType
-  exactly (std::string exact_str);
-
-  // exact comparator function
-  static bool
-  _exactly (const std::string&, std::string);
+  exactly (std::string exact_str) {
+    return boost::bind(&SerialListener::_exactly, _1, exact_str);
+  }
 
   /*!
    * This returns a comparator that looks for a given prefix.
@@ -549,12 +778,6 @@ public:
     return boost::bind(&SerialListener::_startsWith, _1, prefix);
   }
 
-  // exact comparator function
-  static bool
-  _startsWith (const std::string& token, std::string prefix) {
-    return token.substr(0,prefix.length()) == prefix;
-  }
-
   /*!
    * This returns a comparator that looks for a given postfix.
    * 
@@ -579,12 +802,6 @@ public:
     return boost::bind(&SerialListener::_endsWith, _1, postfix);
   }
 
-  // endswith comparator function
-  static bool
-  _endsWith (const std::string& token, std::string postfix) {
-    return token.substr(token.length()-postfix.length()) == postfix;
-  }
-
   /*!
    * This returns a comparator that looks for a given substring in the token.
    * 
@@ -607,42 +824,55 @@ public:
    */
   static ComparatorType
   contains (std::string substr) {
-    return boost::bind(&SerialListener::_contains, _1, substr);
+    return boost::bind(_contains, _1, substr);
   }
 
+private:
+  // delimeter tokenizer function
+  static void
+  _delimeter_tokenizer (const std::string &data,
+                        std::vector &tokens,
+                        std::string delimeter)
+  {
+    typedef std::vector find_vector_type;
+    find_vector_type t;
+    boost::split(t, data, boost::is_any_of(delimeter));
+    for (find_vector_type::iterator it = t.begin(); it != t.end(); it++)
+      tokens.push_back(TokenPtr( new std::string(*it) ));
+  }
+  // exact comparator function
+  static bool
+  _exactly (const std::string& token, std::string exact_str) {
+    return token == exact_str;
+  }
+  // startswith comparator function
+  static bool
+  _startsWith (const std::string& token, std::string prefix) {
+    return token.substr(0,prefix.length()) == prefix;
+  }
+  // endswith comparator function
+  static bool
+  _endsWith (const std::string& token, std::string postfix) {
+    return token.substr(token.length()-postfix.length()) == postfix;
+  }
   // contains comparator function
   static bool
   _contains (const std::string& token, std::string substr) {
     return token.find(substr) != std::string::npos;
   }
-
-private:
+  
   // Gets some data from the serial port
   void readSomeData (std::string&, size_t);
-  // Takes newly tokenized data and processes them
-  void addNewTokens(std::vector &new_tokens,
-                    std::vector &new_uuids,
-                    std::string &left_overs);
   // Runs the new tokens through the filters
-  void filterNewTokens (std::vector new_uuids);
-  // Runs a list of tokens through one filter
-  std::vector >
-  filter(uuid_type filter_uuid, std::vector &token_uuids);
+  void filterNewTokens (std::vector new_tokens);
+  // Given a filter_id and a list of tokens, return list of matched tokens
+  void filter (FilterPtr filter, std::vector &tokens);
   // Function that loops while listening is true
   void listen ();
   // Target of callback thread
   void callback ();
-  // Prune old tokens
-  void pruneTokens ();
-  // Erases one token
-  void eraseToken (uuid_type&);
-  // Erases several tokens
-  void eraseTokens (std::vector&);
   // Determines how much to read on each loop of listen
   size_t determineAmountToRead ();
-  // Hanlder for listen for once
-  typedef boost::shared_ptr shared_cond_var_ptr_t;
-  void notifyListenForOnce (shared_cond_var_ptr_t cond_ptr);
 
   // Tokenizer
   TokenizerType tokenize;
@@ -656,38 +886,30 @@ private:
   ExceptionCallback handle_exc;
 
   // Default handler
-  DataCallback default_handler;
+  FilterPtr default_filter;
+  DataCallback _default_handler;
+  ComparatorType default_comparator;
+  void default_handler(const std::string &token);
 
   // Persistent listening variables
   bool listening;
   serial::Serial * serial_port;
   boost::thread listen_thread;
   std::string data_buffer;
-  boost::mutex token_mux;
-  std::map tokens;
-  std::map ttls;
+  size_t chunk_size;
 
   // Callback related variables
-  // filter uuid, token uuid
-  ConcurrentQueue > callback_queue;
+  // filter id, token
+  // filter id == 0 is going to be default handled
+  ConcurrentQueue >
+  callback_queue;
   boost::thread callback_thread;
 
-  // For generating random uuids
-  boost::uuids::random_generator random_generator;
-  boost::uuids::nil_generator nil_generator;
-
-  // Setting for ttl on messages
-  boost::posix_time::time_duration ttl;
-
-  // map
-  std::vector filters;
-  // map
-  std::map comparators;
-  // map
-  std::map callbacks;
   // Mutex for locking use of filters
   boost::mutex filter_mux;
-  boost::mutex callback_mux;
+  // vector of filter ids
+  std::vector filters;
+
 };
 
 }
diff --git a/serial.cmake b/serial.cmake
index 94a9fce..e32fd3f 100644
--- a/serial.cmake
+++ b/serial.cmake
@@ -13,7 +13,9 @@ project(Serial)
 # Use clang if available
 IF(EXISTS /usr/bin/clang)
   set(CMAKE_CXX_COMPILER /usr/bin/clang++)
-  set(CMAKE_CXX_FLAGS -ferror-limit=5)
+  set(CMAKE_OSX_DEPLOYMENT_TARGET "")
+  # set(CMAKE_CXX_FLAGS "-ferror-limit=5 -std=c++0x -stdlib=libc++")
+  set(CMAKE_CXX_FLAGS "-ferror-limit=5")
 ENDIF(EXISTS /usr/bin/clang)
 
 option(SERIAL_BUILD_TESTS "Build all of the Serial tests." OFF)
@@ -43,6 +45,13 @@ set(SERIAL_SRCS src/serial.cc src/serial_listener.cc)
 # Add default header files
 set(SERIAL_HEADERS include/serial/serial.h include/serial/serial_listener.h)
 
+IF(UNIX)
+  list(APPEND SERIAL_SRCS src/impl/unix.cc)
+  list(APPEND SERIAL_HEADERS include/serial/impl/unix.h)
+ELSE(UNIX)
+  
+ENDIF(UNIX)
+
 # Find Boost, if it hasn't already been found
 IF(NOT Boost_FOUND OR NOT Boost_SYSTEM_FOUND OR NOT Boost_FILESYSTEM_FOUND OR NOT Boost_THREAD_FOUND)
     find_package(Boost COMPONENTS system filesystem thread REQUIRED)
@@ -61,7 +70,7 @@ set(SERIAL_LINK_LIBS ${Boost_SYSTEM_LIBRARY}
 add_library(serial ${SERIAL_SRCS} ${SERIAL_HEADERS})
 target_link_libraries(serial ${SERIAL_LINK_LIBS})
 IF( WIN32 )
-	target_link_libraries(serial wsock32)
+  target_link_libraries(serial wsock32)
 ENDIF( )
 
 # Check for OS X and if so disable kqueue support in asio
@@ -97,8 +106,10 @@ IF(SERIAL_BUILD_TESTS)
     # Compile the Serial Listener Test program
     add_executable(serial_listener_tests tests/serial_listener_tests.cc)
     # Link the Test program to the serial library
-    target_link_libraries(serial_listener_tests ${GTEST_BOTH_LIBRARIES} 
-                                                serial)
+    target_link_libraries(serial_listener_tests ${GTEST_BOTH_LIBRARIES}
+                          serial)
+    # # See: http://code.google.com/p/googlemock/issues/detail?id=146
+    # add_definitions(-DGTEST_USE_OWN_TR1_TUPLE=1)
     add_test(AllTestsIntest_serial serial_listener_tests)
 ENDIF(SERIAL_BUILD_TESTS)
 
diff --git a/src/impl/unix.cc b/src/impl/unix.cc
index e4db489..549aaf5 100644
--- a/src/impl/unix.cc
+++ b/src/impl/unix.cc
@@ -2,127 +2,128 @@
 
 using namespace serial;
 
-Serial_pimpl::Serial_pimpl (const std::string &port, int baudrate,
+Serial::Serial_pimpl::Serial_pimpl (const std::string &port, int baudrate,
                             long timeout, bytesize_t bytesize,
                             parity_t parity, stopbits_t stopbits,
                             flowcontrol_t flowcontrol)
 : port(port), baudrate(baudrate), timeout(timeout), bytesize(bytesize),
   parity(parity), stopbits(stopbits), flowcontrol(flowcontrol)
 {
-  
+  this->fd = -1;
 }
 
-Serial_pimpl::~Serial_pimpl () {
+Serial::Serial_pimpl::~Serial_pimpl () {
+  if (this->isOpen())
+    this->close();
+}
+
+void
+Serial::Serial_pimpl::open () {
   
 }
 
 void
-Serial_pimpl::open () {
-  
-}
-
-void
-Serial_pimpl::close () {
-  
+Serial::Serial_pimpl::close () {
+  this->fd = -1;
 }
 bool
-Serial_pimpl::isOpen () {
-  
+Serial::Serial_pimpl::isOpen () {
+  return false;
 }
 
 size_t
-Serial_pimpl::read (unsigned char* buffer, size_t size = 1) {
+Serial::Serial_pimpl::read (unsigned char* buffer, size_t size) {
+  return 0;
+}
+
+std::string
+Serial::Serial_pimpl::read (size_t size) {
+  return "";
+}
+
+size_t
+Serial::Serial_pimpl::read (std::string &buffer, size_t size) {
+  return 0;
+}
+
+size_t
+Serial::Serial_pimpl::write (unsigned char* data, size_t length) {
+  return 0;
+}
+
+size_t
+Serial::Serial_pimpl::write (const std::string &data) {
+  return 0;
+}
+
+void
+Serial::Serial_pimpl::setPort (const std::string &port) {
   
 }
 
 std::string
-Serial_pimpl::read (size_t size = 1) {
-  
-}
-
-size_t
-Serial_pimpl::read (std::string &buffer, size_t size = 1) {
-  
-}
-
-size_t
-Serial_pimpl::write (unsigned char* data, size_t length) {
-  
-}
-
-size_t
-Serial_pimpl::write (const std::string &data) {
-  
+Serial::Serial_pimpl::getPort () const {
+  return this->port;
 }
 
 void
-Serial_pimpl::setPort (const std::string &port) {
-  
-}
-
-std::string
-Serial_pimpl::getPort () const {
-  
-}
-
-void
-Serial_pimpl::setTimeout (long timeout) {
+Serial::Serial_pimpl::setTimeout (long timeout) {
   
 }
 
 long
-Serial_pimpl::getTimeout () const {
-  
+Serial::Serial_pimpl::getTimeout () const {
+  return this->timeout;
 }
 
 void
-Serial_pimpl::setBaudrate (int baudrate) {
+Serial::Serial_pimpl::setBaudrate (int baudrate) {
   
 }
 
 int
-Serial_pimpl::getBaudrate () const {
-  
+Serial::Serial_pimpl::getBaudrate () const {
+  return this->baudrate;
 }
 
 void
-Serial_pimpl::setBytesize (bytesize_t bytesize) {
+Serial::Serial_pimpl::setBytesize (bytesize_t bytesize) {
   
 }
 
 bytesize_t
-Serial_pimpl::getBytesize () const {
-  
+Serial::Serial_pimpl::getBytesize () const {
+  return this->bytesize;
 }
 
 void
-Serial_pimpl::setParity (parity_t parity) {
+Serial::Serial_pimpl::setParity (parity_t parity) {
   
 }
 
 parity_t
-Serial_pimpl::getParity () const {
-  
+Serial::Serial_pimpl::getParity () const {
+  return this->parity;
 }
 
 void
-Serial_pimpl::setStopbits (stopbits_t stopbits) {
+Serial::Serial_pimpl::setStopbits (stopbits_t stopbits) {
   
 }
 
 stopbits_t
-Serial_pimpl::getStopbits () const {
-  
+Serial::Serial_pimpl::getStopbits () const {
+  return this->stopbits;
 }
 
 void
-Serial_pimpl::setFlowcontrol (flowcontrol_t flowcontrol) {
+Serial::Serial_pimpl::setFlowcontrol (flowcontrol_t flowcontrol) {
   
 }
 
 flowcontrol_t
-Serial_pimpl::getFlowcontrol () const {
-  
+Serial::Serial_pimpl::getFlowcontrol () const {
+  return this->flowcontrol;
 }
 
 
diff --git a/src/serial.cc b/src/serial.cc
index 4637e72..eb6c5c0 100644
--- a/src/serial.cc
+++ b/src/serial.cc
@@ -6,127 +6,128 @@
 #include "serial/impl/unix.h"
 #endif
 
+using namespace serial;
+
 Serial::Serial (const std::string &port, int baudrate, 
                             long timeout, bytesize_t bytesize,
                             parity_t parity, stopbits_t stopbits,
                             flowcontrol_t flowcontrol)
-: impl(new Serial_pimpl(port,baudrate,timeout,bytesize,parity,stopbits,
-                        flowcontrol))
 {
-  
+  pimpl = new Serial_pimpl(port,baudrate,timeout,bytesize,parity,stopbits,
+                           flowcontrol);
 }
 
 Serial::~Serial () {
-  delete impl;
+  delete pimpl;
 }
 
 void
 Serial::open () {
-  this->impl->open ();
+  this->pimpl->open ();
 }
 
 void
 Serial::close () {
-  this->impl->close ();
+  this->pimpl->close ();
 }
 bool
 Serial::isOpen () {
-  return this->impl->isOpen ();
+  return this->pimpl->isOpen ();
 }
 
 size_t
-Serial::read (unsigned char* buffer, size_t size = 1) {
-  return this->impl->read (buffer, size);
+Serial::read (unsigned char* buffer, size_t size) {
+  return this->pimpl->read (buffer, size);
 }
 
 std::string
-Serial::read (size_t size = 1) {
-  return this->impl->read (size);
+Serial::read (size_t size) {
+  return this->pimpl->read (size);
 }
 
 size_t
-Serial::read (std::string &buffer, size_t size = 1) {
-  return this->impl->read (buffer, size);
+Serial::read (std::string &buffer, size_t size) {
+  return this->pimpl->read (buffer, size);
 }
 
 size_t
 Serial::write (unsigned char* data, size_t length) {
-  return this->impl->write (data, length);
+  return this->pimpl->write (data, length);
 }
 
 size_t
 Serial::write (const std::string &data) {
-  return this->impl->write (data);
+  return this->pimpl->write (data);
 }
 
 void
 Serial::setPort (const std::string &port) {
-  this->impl->setPort (port);
+  this->pimpl->setPort (port);
 }
 
 std::string
 Serial::getPort () const {
-  return this->impl->getPort ();
+  return this->pimpl->getPort ();
 }
 
 void
 Serial::setTimeout (long timeout) {
-  this->impl->setTimeout (timeout);
+  this->pimpl->setTimeout (timeout);
 }
 
 long
 Serial::getTimeout () const {
-  return this->impl->getTimeout ();
+  return this->pimpl->getTimeout ();
 }
 
 void
 Serial::setBaudrate (int baudrate) {
-  this->impl->setBaudrate (baudrate);
+  this->pimpl->setBaudrate (baudrate);
 }
 
 int
 Serial::getBaudrate () const {
-  return this->impl->getBaudrate ();
+  return this->pimpl->getBaudrate ();
 }
 
 void
 Serial::setBytesize (bytesize_t bytesize) {
-  this->impl->setBytesize (bytesize);
+  this->pimpl->setBytesize (bytesize);
 }
 
 bytesize_t
 Serial::getBytesize () const {
-  return this->impl->getBytesize ();
+  return this->pimpl->getBytesize ();
 }
 
 void
 Serial::setParity (parity_t parity) {
-  this->impl->setParity (parity);
+  this->pimpl->setParity (parity);
 }
 
 parity_t
 Serial::getParity () const {
-  return this->impl->getParity ();
+  return this->pimpl->getParity ();
 }
 
 void
 Serial::setStopbits (stopbits_t stopbits) {
-  this->impl->setStopbits (stopbits);
+  this->pimpl->setStopbits (stopbits);
 }
 
 stopbits_t
 Serial::getStopbits () const {
-  return this->impl->getStopbits ();
+  return this->pimpl->getStopbits ();
 }
 
 void
 Serial::setFlowcontrol (flowcontrol_t flowcontrol) {
-  this->impl->setFlowcontrol (flowcontrol);
+  this->pimpl->setFlowcontrol (flowcontrol);
 }
 
 flowcontrol_t
 Serial::getFlowcontrol () const {
-  return this->impl->getFlowcontrol ();
+  return this->pimpl->getFlowcontrol ();
 }
 
 
diff --git a/src/serial_listener.cc b/src/serial_listener.cc
index ce4efa2..8306f0c 100644
--- a/src/serial_listener.cc
+++ b/src/serial_listener.cc
@@ -20,23 +20,35 @@ inline void defaultExceptionCallback(const std::exception &error) {
   throw(error);
 }
 
+inline bool defaultComparator(const std::string &token) {
+  return true;
+}
+
 using namespace serial;
 
 /***** Listener Class Functions *****/
 
-SerialListener::SerialListener() : listening(false) {
+void
+SerialListener::default_handler(const std::string &token) {
+  if (this->_default_handler)
+    this->_default_handler(token);
+}
+
+SerialListener::SerialListener() : listening(false), chunk_size(5) {
   // Set default callbacks
   this->handle_exc = defaultExceptionCallback;
   this->info = defaultInfoCallback;
   this->debug = defaultDebugCallback;
   this->warn = defaultWarningCallback;
-  this->default_handler = NULL;
-  
+
+  // Default handler stuff
+  this->_default_handler = NULL;
+  this->default_comparator = defaultComparator;
+  DataCallback tmp = boost::bind(&SerialListener::default_handler, this, _1);
+  this->default_filter = FilterPtr(new Filter(default_comparator, tmp));
+
   // Set default tokenizer
   this->setTokenizer(delimeter_tokenizer("\r"));
-  
-  // Set default ttl
-  this->setTimeToLive();
 }
 
 SerialListener::~SerialListener() {
@@ -48,37 +60,17 @@ SerialListener::~SerialListener() {
 void
 SerialListener::callback() {
   try {
-    // 
-    std::pair pair;
-    DataCallback _callback;
+    // 
+    std::pair pair;
     while (this->listening) {
       if (this->callback_queue.timed_wait_and_pop(pair, 10)) {
         if (this->listening) {
           try {
-            // If default handler
-            if (pair.first.is_nil()) {
-              if (this->default_handler)
-                this->default_handler(this->tokens[pair.second]);
-            // Else use provided callback
-            } else {
-              bool go = false;
-              // Grab the callback as to not hold the mutex while executing
-              {
-                boost::mutex::scoped_lock l(callback_mux);
-                // Make sure the filter hasn't been removed
-                if ((go = (this->callbacks.count(pair.first) > 0)))
-                  _callback = this->callbacks[pair.first];
-              }
-              // Execute callback
-              if (go)
-                _callback(this->tokens[pair.second]);
-            }
+            pair.first->callback((*pair.second));
           } catch (std::exception &e) {
             this->handle_exc(e);
           }// try callback
         } // if listening
-        // Erase the used and executed callback
-        this->eraseToken(pair.second);
       } // if popped
     } // while (this->listening)
   } catch (std::exception &e) {
@@ -86,12 +78,6 @@ SerialListener::callback() {
   }
 }
 
-void
-SerialListener::setTimeToLive(size_t ms) {
-  using namespace boost::posix_time;
-  this->ttl = time_duration(milliseconds(ms));
-}
-
 void
 SerialListener::startListening(Serial &serial_port) {
   if (this->listening) {
@@ -121,32 +107,19 @@ SerialListener::stopListening() {
   listen_thread.join();
   callback_thread.join();
 
-  callback_queue.clear();
   this->data_buffer = "";
-  this->tokens.clear();
-  this->ttls.clear();
   this->serial_port = NULL;
 
   // Delete all the filters
   this->stopListeningForAll();
 }
 
-void
-SerialListener::stopListeningForAll() {
-  boost::mutex::scoped_lock l(filter_mux);
-  filters.clear();
-  comparators.clear();
-  boost::mutex::scoped_lock l2(callback_mux);
-  callbacks.clear();
-  callback_queue.clear();
-}
-
 size_t
 SerialListener::determineAmountToRead() {
   // TODO: Make a more intelligent method based on the length of the things 
-  //  filters are looking for.  i.e.: if the filter is looking for 'V=XX\r' 
+  //  filters are looking for.  e.g.: if the filter is looking for 'V=XX\r' 
   //  make the read amount at least 5.
-  return 5;
+  return this->chunk_size;
 }
 
 void
@@ -163,130 +136,26 @@ SerialListener::readSomeData(std::string &temp, size_t this_many) {
 }
 
 void
-SerialListener::addNewTokens(std::vector &new_tokens,
-                             std::vector &new_uuids,
-                             std::string &left_overs)
-{
-  std::vector::iterator it_new;
-  for (it_new=new_tokens.begin(); it_new != new_tokens.end(); it_new++) {
-    // The last token needs to be put back in the buffer always:
-    //  (in the case that the delimeter is \r)...
-    //  In the case that the string ends with \r the last element will be 
-    //  empty ("").  In the case that it does not the last element will be 
-    //  what is left over from the next message that hasn't sent 
-    //  everything.  Ex.: "?$1E\r" -> ["?$1E", ""] and 
-    //  "?$1E\r$1E=Robo" -> ["?$1E","$1E=Robo"]
-    if (it_new == new_tokens.end()-1) {
-      left_overs = (*it_new);
-      continue;
-    }
-    // If the token is empty ignore it
-    if ((*it_new).length() == 0)
-      continue;
-    // Create a new uuid
-    uuid_type uuid = random_generator();
-    // Put the new uuid in the list of new uuids
-    new_uuids.push_back(uuid);
-    // Create a uuid, token pair
-    std::pair token_pair(uuid,(*it_new));
-    // Create a uuid, ttl pair
-    using namespace boost::posix_time;
-    std::pair 
-     ttl_pair(uuid,ptime(microsec_clock::local_time()));
-    // Insert the new pairs
-    {
-      boost::mutex::scoped_lock l(token_mux);
-      this->tokens.insert(token_pair);
-      ttls.insert(ttl_pair);
-    }
-  } // for (it_new=new_tokens.begin(); it_new != new_tokens.end(); it_new++)
-}
-
-void
-SerialListener::eraseToken(uuid_type &uuid) {
-  boost::mutex::scoped_lock l(token_mux);
-  this->tokens.erase(uuid);
-  this->ttls.erase(uuid);
-}
-
-void
-SerialListener::eraseTokens(std::vector &uuids) {
-  std::vector::iterator it;
-  for (it=uuids.begin(); it != uuids.end(); it++) {
-    this->eraseToken((*it));
-  }
-}
-
-// TODO: Investigate possible race condition where the filter processing takes 
-//  longer than the ttl
-void
-SerialListener::filterNewTokens(std::vector new_uuids) {
+SerialListener::filterNewTokens (std::vector new_tokens) {
   // Iterate through the filters, checking each against new tokens
-  std::vector > tbd;
-  boost::mutex::scoped_lock l(filter_mux);
-  std::vector::iterator it;
+  boost::mutex::scoped_lock lock(filter_mux);
+  std::vector::iterator it;
   for (it=filters.begin(); it!=filters.end(); it++) {
-    std::vector > temp =
-      this->filter((*it), new_uuids);
-    if (temp.size() > 0)
-      tbd.insert(tbd.end(), temp.begin(), temp.end());
+    this->filter((*it), new_tokens);
   } // for (it=filters.begin(); it!=filters.end(); it++)
-  // Dispatch
-  std::vector >::iterator it_tbd;
-  for (it_tbd = tbd.begin(); it_tbd != tbd.end(); it_tbd++) {
-    callback_queue.push((*it_tbd));
-  }
-}
-
-// 
-std::vector >
-SerialListener::filter(uuid_type filter_uuid,
-                       std::vector &token_uuids)
-{
-  std::vector to_be_erased;
-  std::vector > to_be_dispatched;
-  // Iterate through the token uuids and run each against the filter
-  std::vector::iterator it;
-  for (it=token_uuids.begin(); it!=token_uuids.end(); it++) {
-    bool matched = false;
-    uuid_type token_uuid = (*it);
-    if (this->comparators[filter_uuid](this->tokens[token_uuid])) {
-      matched = true;
-      to_be_dispatched.push_back(std::make_pair(filter_uuid,token_uuid));
-    }
-  }
-  return to_be_dispatched;
 }
 
+// 
 void
-SerialListener::pruneTokens() {
-  // Iterate through the buffered tokens
-  std::vector to_be_erased;
-  std::map::iterator it;
-
-  {
-    boost::mutex::scoped_lock l(token_mux);
-    for (it = this->tokens.begin(); it != this->tokens.end(); it++) {
-      uuid_type uuid = (*it).first;
-      using namespace boost::posix_time;
-      // If the current time - the creation time is greater than the ttl, 
-      //  then prune it
-      if (ptime(microsec_clock::local_time())-this->ttls[uuid] > this->ttl) {
-        // If there is a default handler pass it on
-        if (this->default_handler) {
-          boost::mutex::scoped_lock l(callback_mux);
-          callback_queue.push(std::make_pair(nil_generator(),uuid));
-        } else {
-          // Otherwise delete it
-          to_be_erased.push_back(uuid);
-        }
-      }
-    }
+SerialListener::filter (FilterPtr filter, std::vector &tokens)
+{
+  // Iterate through the token uuids and run each against the filter
+  std::vector::iterator it;
+  for (it=tokens.begin(); it!=tokens.end(); it++) {
+    TokenPtr token = (*it);
+    if (filter->comparator((*token)))
+      callback_queue.push(std::make_pair(filter,token));
   }
-  // Remove any lines that need to be erased
-  //  (this must be done outside the iterator to prevent problems incrementing
-  //   the iterator)
-  this->eraseTokens(to_be_erased);
 }
 
 void
@@ -302,17 +171,11 @@ SerialListener::listen() {
         // Add the new data to the buffer
         this->data_buffer += temp;
         // Call the tokenizer on the updated buffer
-        std::vector new_tokens;
+        std::vector new_tokens;
         this->tokenize(this->data_buffer, new_tokens);
-        // Add the new tokens to the new token buffer, get a list of new uuids 
-        //  to filter once, and put left_overs in the data buffer.
-        std::vector new_uuids;
-        this->addNewTokens(new_tokens, new_uuids, this->data_buffer);
         // Run the new tokens through existing filters
-        this->filterNewTokens(new_uuids);
+        this->filterNewTokens(new_tokens);
       }
-      // Look for old data and pass to the default handler or delete
-      this->pruneTokens();
       // Done parsing lines and buffer should now be set to the left overs
     } // while (this->listening)
   } catch (std::exception &e) {
@@ -320,154 +183,64 @@ SerialListener::listen() {
   }
 }
 
+/***** Filter Functions *****/
+
+FilterPtr
+SerialListener::listenFor(ComparatorType comparator, DataCallback callback) {
+  FilterPtr filter_ptr(new Filter(comparator, callback));
+
+  boost::mutex::scoped_lock l(filter_mux);
+  this->filters.push_back(filter_ptr);
+
+  return filter_ptr;
+}
+
+typedef boost::shared_ptr shared_cond_var_ptr_t;
+
+inline void
+listenForOnceCallback(const std::string &token,
+                      shared_cond_var_ptr_t cond,
+                      boost::shared_ptr result)
+{
+  (*result) = token;
+  cond->notify_all();
+}
+
+std::string
+SerialListener::listenForOnce(ComparatorType comparator, size_t ms) {
+  boost::shared_ptr result(new std::string(""));
+
+  shared_cond_var_ptr_t cond(new boost::condition_variable());
+  boost::mutex mutex;
+
+  DataCallback callback = boost::bind(listenForOnceCallback,_1,cond,result);
+  FilterPtr filter_id = this->listenFor(comparator, callback);
+
+  boost::unique_lock lock(mutex);
+  cond->timed_wait(lock, boost::posix_time::milliseconds(ms)));
+
+  this->stopListeningFor(filter_id);
+
+  // If the callback never got called then result will be "" because tokens
+  // can never be ""
+  return (*result);
+}
+
 bool
 SerialListener::listenForStringOnce(std::string token, size_t milliseconds) {
-  return this->listenForOnce(exactly(token), milliseconds);
+  return this->listenForOnce(exactly(token), milliseconds) == token;
 }
 
 void
-SerialListener::notifyListenForOnce(shared_cond_var_ptr_t cond_ptr) {
-  cond_ptr->notify_all();
-}
-
-bool
-SerialListener::listenForOnce(ComparatorType comparator, size_t ms)
-{
-  shared_cond_var_ptr_t cond_ptr(new boost::condition_variable());
-  boost::mutex mut;
-
-  // Create blocking filter
-  const uuid_type uuid = random_generator();
-  {
-    boost::mutex::scoped_lock l(filter_mux);
-    filters.push_back(uuid);
-    comparators.insert(std::make_pair(uuid,comparator));
-  }
-  {
-    boost::mutex::scoped_lock l(callback_mux);
-    callbacks.insert(std::make_pair(uuid,
-      boost::bind(&SerialListener::notifyListenForOnce, this, cond_ptr)));
-  }
-
-  // Run this filter through all tokens onces
-  std::vector token_uuids;
-  std::map::iterator it;
-  for (it = tokens.begin(); it != tokens.end(); it++)
-    token_uuids.push_back((*it).first);
-  std::vector > pairs =
-    this->filter(uuid, token_uuids);
-
-  // If there is at least one
-  if (pairs.size() > 0) {
-    // If there is more than one find the oldest
-    size_t index = 0;
-    if (pairs.size() > 1) {
-      using namespace boost::posix_time;
-      ptime oldest_time = ttls[pairs[index].second];
-      size_t i = 0;
-      std::vector >::iterator it;
-      for (it = pairs.begin(); it != pairs.end(); it++) {
-        if (ttls[(*it).second] < oldest_time) {
-          oldest_time = ttls[(*it).second];
-          index = i;
-        }
-        i++;
-      }
-    }
-    // Either way put the final index into the callback queue
-    callback_queue.push(pairs[index]);
-  }
-
-  bool result = false;
-
-  // Wait
-  boost::unique_lock lock(mut);
-  using namespace boost::posix_time;
-  if (cond_ptr->timed_wait(lock, milliseconds(ms)))
-    result = true;
-
-  // Destroy the filter
-  {
-    boost::mutex::scoped_lock l(filter_mux);
-    filters.erase(std::find(filters.begin(),filters.end(),uuid));
-    comparators.erase(uuid);
-  }
-  {
-    boost::mutex::scoped_lock l(callback_mux);
-    callbacks.erase(uuid);
-  }
-
-  return result;
-}
-
-uuid_type
-SerialListener::listenFor(ComparatorType comparator, DataCallback callback)
-{
-  // Create Filter
-  uuid_type uuid = random_generator();
-  std::pair
-   comparator_pair(uuid, comparator);
-  std::pair
-   callback_pair(uuid, callback);
-
-  {
-    boost::mutex::scoped_lock l(filter_mux);
-    filters.push_back(uuid);
-    comparators.insert(comparator_pair);
-  }
-  {
-    boost::mutex::scoped_lock l(callback_mux);
-    callbacks.insert(callback_pair);
-  }
-
-  // Run this filter through all tokens onces
-  std::vector token_uuids;
-  std::map::iterator it;
-  for (it = tokens.begin(); it != tokens.end(); it++)
-    token_uuids.push_back((*it).first);
-  std::vector > pairs =
-    this->filter(uuid, token_uuids);
-
-  // Dispatch
-  std::vector >::iterator it_cb;
-  for (it_cb = pairs.begin(); it_cb != pairs.end(); it_cb++) {
-    callback_queue.push((*it_cb));
-  }
-
-  return uuid;
-}
-
-void
-SerialListener::stopListeningFor(uuid_type filter_uuid) {
-  // Delete filter
+SerialListener::stopListeningFor(FilterPtr filter_ptr) {
   boost::mutex::scoped_lock l(filter_mux);
-  filters.erase(std::find(filters.begin(),filters.end(),filter_uuid));
-  comparators.erase(filter_uuid);
-  boost::mutex::scoped_lock l2(callback_mux);
-  callbacks.erase(filter_uuid);
-}
-
-TokenizerType
-SerialListener::delimeter_tokenizer (std::string delimeter) {
-  return boost::bind(&SerialListener::_delimeter_tokenizer,
-                     _1, _2, delimeter);
+  filters.erase(std::find(filters.begin(),filters.end(),filter_ptr));
 }
 
 void
-SerialListener::_delimeter_tokenizer (std::string &data,
-                                      std::vector &tokens,
-                                      std::string delimeter)
-{
-  boost::split(tokens, data, boost::is_any_of(delimeter));
-}
-
-ComparatorType
-SerialListener::exactly(std::string exact_str) {
-  return boost::bind(&SerialListener::_exactly, _1, exact_str);
-}
-
-bool
-SerialListener::_exactly(const std::string &token, std::string exact_str) {
-  return token == exact_str;
+SerialListener::stopListeningForAll() {
+  boost::mutex::scoped_lock l(filter_mux);
+  filters.clear();
+  callback_queue.clear();
 }
 
diff --git a/tests/serial_listener_tests.cc b/tests/serial_listener_tests.cc
index 81e4aef..7c5e3df 100644
--- a/tests/serial_listener_tests.cc
+++ b/tests/serial_listener_tests.cc
@@ -22,8 +22,7 @@ class SerialListenerTests : public ::testing::Test {
 protected:
   virtual void SetUp() {
     listener.listening = true;
-    listener.setTimeToLive(10);
-    listener.default_handler = default_handler;
+    listener.setDefaultHandler(default_handler);
     listener.callback_thread =
      boost::thread(boost::bind(&SerialListener::callback, &listener));
   }
@@ -49,13 +48,9 @@ protected:
   }
 
   void simulate_loop(std::string input_str) {
-    std::vector new_tokens;
+    std::vector new_tokens;
     listener.tokenize(input_str, new_tokens);
-    std::vector new_uuids;
-    listener.addNewTokens(new_tokens, new_uuids, listener.data_buffer);
-    listener.filterNewTokens(new_uuids);
-    boost::this_thread::sleep(boost::posix_time::milliseconds(11));
-    listener.pruneTokens();
+    listener.filterNewTokens(new_tokens);
   }
 
   SerialListener listener;
@@ -132,7 +127,7 @@ TEST_F(SerialListenerTests, listenForWorks) {
   global_count = 0;
   global_listen_count = 0;
 
-  boost::uuids::uuid filt_uuid = 
+  FilterPtr filt_uuid =
     listener.listenFor(listenForComparator, listenForCallback);
 
   simulate_loop("\r+\rV=05:06\r?$1E\rV=06:05\r$1E=Robo");

From dfd1837cfc7075c83403205264d004fd3f215d6d Mon Sep 17 00:00:00 2001
From: William Woodall 
Date: Wed, 11 Jan 2012 23:42:42 -0600
Subject: [PATCH 12/72] Serial Listener changes compile against the example
 reference, time to merge with John.

---
 examples/serial_listener_example.cc |  14 +-
 include/serial/serial_listener.h    | 446 +++++++++++++++-------------
 src/serial_listener.cc              |  63 ++--
 3 files changed, 276 insertions(+), 247 deletions(-)

diff --git a/examples/serial_listener_example.cc b/examples/serial_listener_example.cc
index 2cd5b20..5fd23e4 100644
--- a/examples/serial_listener_example.cc
+++ b/examples/serial_listener_example.cc
@@ -36,10 +36,10 @@ int main(void) {
   // Method #2:
   //  comparator - blocking
   {
-    BlockingFilter f2 =
+    BlockingFilterPtr f2 =
       listener.createBlockingFilter(SerialListener::endsWith("post"));
     for (size_t i = 0; i < 3; i++) {
-      std::string token = f2.wait(100); // Wait for 100 ms or a matched token
+      std::string token = f2->wait(100); // Wait for 100 ms or a matched token
       if (token == "")
         std::cout << "Found something ending with 'post'" << std::endl;
       else
@@ -54,18 +54,18 @@ int main(void) {
   //  comparator, token buffer size - blocking
   {
     // Give it a comparator, then a buffer size of 10
-    BufferedFilter f3 =
+    BufferedFilterPtr f3 =
       listener.createBufferedFilter(SerialListener::contains("substr"), 10);
     SerialListener::sleep(75); // Sleep 75ms, should have about 7
-    std::cout << "Caught " << f3.count();
+    std::cout << "Caught " << f3->count();
     std::cout << " tokens containing 'substr'" << std::endl;
     for(size_t i = 0; i < 20; ++i) {
-      std::string token = f3.wait(5); // Pull message from the buffer
+      std::string token = f3->wait(5); // Pull message from the buffer
       if (token == "") // If an empty string is returned, a timeout occured
         break;
     }
-    f3.clear(); // Empties the buffer
-    if (f3.wait(0) == "") // Non-blocking wait
+    f3->clear(); // Empties the buffer
+    if (f3->wait(0) == "") // Non-blocking wait
       std::cout << "We won the race condition!" << std::endl;
     else
       std::cout << "We lost the race condition..." << std::endl;
diff --git a/include/serial/serial_listener.h b/include/serial/serial_listener.h
index 1c8e5de..737e146 100644
--- a/include/serial/serial_listener.h
+++ b/include/serial/serial_listener.h
@@ -178,160 +178,31 @@ public:
  */
 typedef boost::shared_ptr FilterPtr;
 
-/*!
- * This is the a filter that provides a wait function for blocking until a 
- * match is found.
- * 
- * This should probably not be created manually, but instead should be 
- * constructed using SerialListener::createBlockingFilter(ComparatorType)
- * function which returns a BlockingFilter instance.
- * 
- * \see serial::SerialListener::ComparatorType,
- * serial::SerialListener::createBlockingFilter
- */
-class BlockingFilter
-{
-public:
-  BlockingFilter (ComparatorType comparator,
-                  boost::shared_ptr listener)
-  : listener(listener)
-  {
-    DataCallback cb = boost::bind(&BlockingFilter::callback, this, _1);
-    this->filter_ptr = listener.createFilter(comparator, cb);
-  }
-
-  virtual ~BlockingFilter () {
-    this->listener.removeFilter(filter_ptr);
-    this->result = "";
-    this->cond.notify_all();
-  }
-
-  /*!
-   * Waits a given number of milliseconds or until a token is matched.  If a
-   * token is matched it is returned, otherwise an empty string is returned.
-   * 
-   * \param ms Time in milliseconds to wait on a new token.
-   * 
-   * \return std::string token that was matched or "" if none were matched.
-   */
-  std::string wait(size_t ms) {
-    this->result = "";
-    boost::unique_lock lock(this->mutex);
-    this->cond.timed_wait(lock, boost::posix_time::milliseconds(ms));
-    return this->result;
-  }
-
-private:
-  void callback(const std::string& token) {
-    this->cond.notify_all();
-    this->result = token;
-  }
-
-  FilterPtr filter_ptr;
-  boost::shared_ptr listener;
-  boost::condition_variable cond;
-  boost::mutex mutex;
-  std::string result;
-
-};
+class BlockingFilter;
 
 /*!
- * This is the a filter that provides a wait function for blocking until a
- * match is found.  It will also buffer up to a given buffer size of tokens so
- * that they can be counted or accessed after they are matched by the filter.
+ * Shared Pointer of BlockingFilter, returned by
+ * SerialListener::createBlockingFilter.
  * 
- * This should probably not be created manually, but instead should be 
- * constructed using SerialListener::createBufferedFilter(ComparatorType)
- * function which returns a BufferedFilter instance.
- * 
- * The internal buffer is a circular queue buffer, so when the buffer is full,
- * the oldest token is dropped and the new one is added.  Additionally, when
- * wait is a called the oldest available token is returned.
- * 
- * \see serial::SerialListener::ComparatorType,
- * serial::SerialListener::createBufferedFilter
+ * \see serial::BlockingFilter, SerialListener::createBlockingFilter
  */
-class BufferedFilter
-{
-public:
-  BufferedFilter (ComparatorType comparator, size_t buffer_size,
-                  boost::shared_ptr listener)
-  : listener(listener), buffer_size(buffer_size)
-  {
-    DataCallback cb = boost::bind(&BlockingFilter::callback, this, _1);
-    this->filter_ptr = listener.createFilter(comparator, cb);
-  }
+typedef boost::shared_ptr BlockingFilterPtr;
 
-  virtual ~BufferedFilter () {
-    this->listener.removeFilter(filter_ptr);
-    this->queue.clear();
-    this->result = "";
-    this->cond.notify_all();
-  }
+class BufferedFilter;
 
-  /*!
-   * Waits a given number of milliseconds or until a matched token is 
-   * available in the buffer.  If a token is matched it is returned, otherwise
-   * an empty string is returned.
-   * 
-   * \param ms Time in milliseconds to wait on a new token.  If ms is set to 0 
-   * then it will try to get a new token if one is available but will not 
-   * block.
-   * 
-   * \return std::string token that was matched or "" if none were matched.
-   */
-  std::string wait(size_t ms) {
-    if (ms == 0)
-      if (!this->queue.try_pop(this->result))
-        this->result = "";
-    else
-      if (!this->queue.timed_wait_and_pop(this->result, ms))
-        this->result = "";
-    return result;
-  }
-
-  /*!
-   * Clears the buffer of any tokens.
-   */
-  void clear() {
-    queue.clear();
-  }
-
-  /*!
-   * Returns the number of tokens waiting in the buffer.
-   */
-  size_t count() {
-    return queue.size();
-  }
-
-  /*!
-   * Returns the capacity of the buffer.
-   */
-  size_t capacity() {
-    return buffer_size;
-  }
-
-private:
-  void callback(const std::string &token) {
-    std::string throw_away;
-    if (this->queue.size() == buffer_size)
-      this->queue.wait_and_pop(throw_away);
-    this->queue.push(token);
-  }
-
-  FilterPtr filter_ptr;
-  size_t buffer_size;
-  boost::shared_ptr listener;
-  ConcurrentQueue queue;
-  std::string result;
-
-};
+/*!
+ * Shared Pointer of BufferedFilter, returned by
+ * SerialListener::createBufferedFilter.
+ * 
+ * \see serial::BufferedFilter, SerialListener::createBufferedFilter
+ */
+typedef boost::shared_ptr BufferedFilterPtr;
 
 /*!
  * This is a general exception generated by the SerialListener class.
  * 
  * Check the SerialListenerException::what function for the cause.
- 
+ * 
  * \param e_what is a std::string that describes the cause of the error.
  */
 class SerialListenerException : public std::exception {
@@ -489,98 +360,115 @@ public:
 /***** Filter Functions ******/
 
   /*!
-   * Setups up a filter that calls a callback when a comparator returns true.
+   * Creates a filter that calls a callback when the comparator returns true.
    * 
-   * The user provides a comparator and a callback, and every time a line is 
-   * received the comparator is called and the comparator has to evaluate the 
-   * line and return true if it matches and false if it doesn't.  If it does 
+   * The user provides a comparator and a callback, and every time a line is
+   * received the comparator is called and the comparator has to evaluate the
+   * line and return true if it matches and false if it doesn't.  If it does
    * match, the callback is called with the resulting line.
    * 
    * \param comparator This is a comparator for detecting if a line matches.
-   * The comparartor receives a std::string reference and must return a true 
+   * The comparartor receives a std::string reference and must return a true
    * if it matches and false if it doesn't.
    * 
-   * \param callback This is the handler for when a match occurs. It is given 
+   * \param callback This is the handler for when a match occurs. It is given
    * a std::string reference of the line that matched your comparator.
    * 
    * \return boost::shared_ptr so you can remove it later.
    * 
-   * \see SerialListener::stopListeningFor
+   * \see SerialListener::removeFilter
    */
   FilterPtr
-  listenFor (ComparatorType comparator, DataCallback callback);
+  createFilter (ComparatorType comparator, DataCallback callback);
 
   /*!
-   * Blocks until the comparator returns true or until the timeout occurs.
+   * Creates a BlockingFilter which blocks until the comparator returns true.
    * 
-   * \param comparator ComparatorType function that should return true if the
-   * given std::string matches otherwise false.
+   * The user provides a comparator, and every time a line is
+   * received the comparator is called and the comparator has to evaluate the
+   * line and return true if it matches and false if it doesn't.  If it does
+   * match, any threads that have called BlockingFilter::wait will be
+   * notified.  The BlockingFilter will remove itself when its destructor is
+   * called, i.e. when it leaves the scope, so in those cases an explicit call 
+   * to SerialListener::removeFilter is not needed.
    * 
-   * \param timeout in milliseconds before timing out and returning false.
-   * Defaults to 1000 milliseconds or 1 second.
+   * \param comparator This is a comparator for detecting if a line matches.
+   * The comparartor receives a std::string reference and must return a true
+   * if it matches and false if it doesn't.
    * 
-   * \return std::string the token that was matched, returns an empty string 
-   * if the timeout occurs first.
-   * i.e. if (listenForOnce(...) != "") // Got match
+   * \return BlockingFilterPtr So you can call BlockingFilter::wait on it.
+   * 
+   * \see SerialListener::removeFilter, serial::BlockingFilter,
+   * serial::BlockingFilterPtr
    */
-  std::string
-  listenForOnce (ComparatorType comparator, size_t timeout = 1000);
+  BlockingFilterPtr
+  createBlockingFilter (ComparatorType comparator);
 
   /*!
-   * Writes to the seiral port then blocks until the comparator returns true 
-   * or until the timeout occurs.
+   * Creates a BlockingFilter blocks until the comparator returns true.
    * 
-   * This function creates a filter, writes the data, then waits for it to 
-   * match atleast once.
+   * The user provides a comparator, and every time a line is
+   * received the comparator is called and the comparator has to evaluate the
+   * line and return true if it matches and false if it doesn't.  If it does
+   * match, any threads that have called BlockingFilter::wait will be
+   * notified.  The BlockingFilter will remove itself when its destructor is
+   * called, i.e. when it leaves the scope, so in those cases an explicit call 
+   * to SerialListener::removeFilter is not needed.
    * 
-   * \param to_be_written const std::string reference of data to be written to
-   * the serial port.
+   * \param comparator This is a comparator for detecting if a line matches.
+   * The comparartor receives a std::string reference and must return a true
+   * if it matches and false if it doesn't.
    * 
-   * \param comparator ComparatorType function that should return true if the
-   * given std::string matches otherwise false.
+   * \param buffer_size This is the number of tokens to be buffered by the
+   * BufferedFilter, defaults to 1024.
    * 
-   * \param timeout in milliseconds before timing out and returning false.
-   * Defaults to 1000 milliseconds or 1 second.
+   * \return BlockingFilter So you can call BlockingFilter::wait on it.
    * 
-   * \return std::string the token that was matched, returns an empty string 
-   * if the timeout occurs first.
-   * i.e. if (listenForOnce(...) != "") // Got match
+   * \see SerialListener::removeFilter, serial::BufferedFilter,
+   * serial::BufferedFilterPtr
    */
-  std::string
-  listenForOnce (ComparatorType comparator, size_t timeout = 1000);
+  BufferedFilterPtr
+  createBufferedFilter (ComparatorType comparator, size_t buffer_size = 1024);
 
   /*!
-   * Blocks until the given string is detected or until the timeout occurs.
+   * Removes a filter by a given FilterPtr.
    * 
-   * \param token std::string that should be watched for, this string must 
-   * match the message exactly.
+   * \param filter_ptr A shared pointer to the filter to be removed.
    * 
-   * \param timeout in milliseconds before timing out and returning false.
-   * Defaults to 1000 milliseconds or 1 second.
-   * 
-   * \return bool If true then the token was detected before the token, false 
-   * if the token was not heard and the timeout occured.
-   */
-  bool
-  listenForStringOnce (std::string token, size_t timeout = 1000);
-
-  /*!
-   * Removes a filter by a given uuid.
-   * 
-   * The uuid for a filter is returned by the listenFor function.
-   * 
-   * \param filter_ptr A shared pointer to the filter.
-   * 
-   * \see SerialListener::listenFor
-   */
-   void
-   stopListeningFor (FilterPtr filter_ptr);
-
-  /*!
-   * Stops listening for anything, but doesn't stop reading the serial port.
+   * \see SerialListener::createFilter
    */
   void
-  stopListeningForAll ();
+  removeFilter (FilterPtr filter_ptr);
+
+  /*!
+   * Removes a BlockingFilter.
+   * 
+   * The BlockingFilter will remove itself if the destructor is called.
+   * 
+   * \param blocking_filter A BlockingFilter to be removed.
+   * 
+   * \see SerialListener::createBlockingFilter
+   */
+  void
+  removeFilter (BlockingFilterPtr blocking_filter);
+
+  /*!
+   * Removes a BufferedFilter.
+   * 
+   * The BufferedFilter will remove itself if the destructor is called.
+   * 
+   * \param buffered_filter A BufferedFilter to be removed.
+   * 
+   * \see SerialListener::createBufferedFilter
+   */
+  void
+  removeFilter (BufferedFilterPtr buffered_filter);
+
+  /*!
+   * Removes all filters.
+   */
+  void
+  removeAllFilters ();
 
 /***** Hooks and Handlers ******/
 
@@ -912,6 +800,156 @@ private:
 
 };
 
-}
+/*!
+ * This is the a filter that provides a wait function for blocking until a 
+ * match is found.
+ * 
+ * This should probably not be created manually, but instead should be 
+ * constructed using SerialListener::createBlockingFilter(ComparatorType)
+ * function which returns a BlockingFilter instance.
+ * 
+ * \see serial::SerialListener::ComparatorType,
+ * serial::SerialListener::createBlockingFilter
+ */
+class BlockingFilter
+{
+public:
+  BlockingFilter (ComparatorType comparator,
+                  boost::shared_ptr listener)
+  : listener(listener)
+  {
+    DataCallback cb = boost::bind(&BlockingFilter::callback, this, _1);
+    this->filter_ptr = listener->createFilter(comparator, cb);
+  }
+
+  virtual ~BlockingFilter () {
+    this->listener->removeFilter(filter_ptr);
+    this->result = "";
+    this->cond.notify_all();
+  }
+
+  /*!
+   * Waits a given number of milliseconds or until a token is matched.  If a
+   * token is matched it is returned, otherwise an empty string is returned.
+   * 
+   * \param ms Time in milliseconds to wait on a new token.
+   * 
+   * \return std::string token that was matched or "" if none were matched.
+   */
+  std::string wait(size_t ms) {
+    this->result = "";
+    boost::unique_lock lock(this->mutex);
+    this->cond.timed_wait(lock, boost::posix_time::milliseconds(ms));
+    return this->result;
+  }
+
+  FilterPtr filter_ptr;
+
+  void callback(const std::string& token) {
+    this->cond.notify_all();
+    this->result = token;
+  }
+
+private:
+  boost::shared_ptr listener;
+  boost::condition_variable cond;
+  boost::mutex mutex;
+  std::string result;
+
+};
+
+/*!
+ * This is the a filter that provides a wait function for blocking until a
+ * match is found.  It will also buffer up to a given buffer size of tokens so
+ * that they can be counted or accessed after they are matched by the filter.
+ * 
+ * This should probably not be created manually, but instead should be 
+ * constructed using SerialListener::createBufferedFilter(ComparatorType)
+ * function which returns a BufferedFilter instance.
+ * 
+ * The internal buffer is a circular queue buffer, so when the buffer is full,
+ * the oldest token is dropped and the new one is added.  Additionally, when
+ * wait is a called the oldest available token is returned.
+ * 
+ * \see serial::SerialListener::ComparatorType,
+ * serial::SerialListener::createBufferedFilter
+ */
+class BufferedFilter
+{
+public:
+  BufferedFilter (ComparatorType comparator, size_t buffer_size,
+                  boost::shared_ptr listener)
+  : listener(listener), buffer_size(buffer_size)
+  {
+    DataCallback cb = boost::bind(&BufferedFilter::callback, this, _1);
+    this->filter_ptr = listener->createFilter(comparator, cb);
+  }
+
+  virtual ~BufferedFilter () {
+    this->listener->removeFilter(filter_ptr);
+    this->queue.clear();
+    this->result = "";
+  }
+
+  /*!
+   * Waits a given number of milliseconds or until a matched token is 
+   * available in the buffer.  If a token is matched it is returned, otherwise
+   * an empty string is returned.
+   * 
+   * \param ms Time in milliseconds to wait on a new token.  If ms is set to 0 
+   * then it will try to get a new token if one is available but will not 
+   * block.
+   * 
+   * \return std::string token that was matched or "" if none were matched.
+   */
+  std::string wait(size_t ms) {
+    if (ms == 0)
+      if (!this->queue.try_pop(this->result))
+        this->result = "";
+    else
+      if (!this->queue.timed_wait_and_pop(this->result, ms))
+        this->result = "";
+    return result;
+  }
+
+  /*!
+   * Clears the buffer of any tokens.
+   */
+  void clear() {
+    queue.clear();
+  }
+
+  /*!
+   * Returns the number of tokens waiting in the buffer.
+   */
+  size_t count() {
+    return queue.size();
+  }
+
+  /*!
+   * Returns the capacity of the buffer.
+   */
+  size_t capacity() {
+    return buffer_size;
+  }
+
+  FilterPtr filter_ptr;
+
+  void callback(const std::string &token) {
+    std::string throw_away;
+    if (this->queue.size() == buffer_size)
+      this->queue.wait_and_pop(throw_away);
+    this->queue.push(token);
+  }
+
+private:
+  size_t buffer_size;
+  boost::shared_ptr listener;
+  ConcurrentQueue queue;
+  std::string result;
+
+};
+
+} // namespace serial
 
 #endif // SERIAL_LISTENER_H
\ No newline at end of file
diff --git a/src/serial_listener.cc b/src/serial_listener.cc
index 8306f0c..d3ed2a3 100644
--- a/src/serial_listener.cc
+++ b/src/serial_listener.cc
@@ -111,7 +111,7 @@ SerialListener::stopListening() {
   this->serial_port = NULL;
 
   // Delete all the filters
-  this->stopListeningForAll();
+  this->removeAllFilters();
 }
 
 size_t
@@ -186,7 +186,8 @@ SerialListener::listen() {
 /***** Filter Functions *****/
 
 FilterPtr
-SerialListener::listenFor(ComparatorType comparator, DataCallback callback) {
+SerialListener::createFilter(ComparatorType comparator, DataCallback callback)
+{
   FilterPtr filter_ptr(new Filter(comparator, callback));
 
   boost::mutex::scoped_lock l(filter_mux);
@@ -195,50 +196,40 @@ SerialListener::listenFor(ComparatorType comparator, DataCallback callback) {
   return filter_ptr;
 }
 
-typedef boost::shared_ptr shared_cond_var_ptr_t;
+BlockingFilterPtr
+SerialListener::createBlockingFilter(ComparatorType comparator) {
+  return BlockingFilterPtr(
+    new BlockingFilter(comparator, boost::shared_ptr(this)));
+}
 
-inline void
-listenForOnceCallback(const std::string &token,
-                      shared_cond_var_ptr_t cond,
-                      boost::shared_ptr result)
+BufferedFilterPtr
+SerialListener::createBufferedFilter(ComparatorType comparator,
+                                     size_t buffer_size)
 {
-  (*result) = token;
-  cond->notify_all();
-}
-
-std::string
-SerialListener::listenForOnce(ComparatorType comparator, size_t ms) {
-  boost::shared_ptr result(new std::string(""));
-
-  shared_cond_var_ptr_t cond(new boost::condition_variable());
-  boost::mutex mutex;
-
-  DataCallback callback = boost::bind(listenForOnceCallback,_1,cond,result);
-  FilterPtr filter_id = this->listenFor(comparator, callback);
-
-  boost::unique_lock lock(mutex);
-  cond->timed_wait(lock, boost::posix_time::milliseconds(ms)));
-
-  this->stopListeningFor(filter_id);
-
-  // If the callback never got called then result will be "" because tokens
-  // can never be ""
-  return (*result);
-}
-
-bool
-SerialListener::listenForStringOnce(std::string token, size_t milliseconds) {
-  return this->listenForOnce(exactly(token), milliseconds) == token;
+  return BufferedFilterPtr(
+    new BufferedFilter(comparator,
+                       buffer_size,
+                       boost::shared_ptr(this)));
 }
 
 void
-SerialListener::stopListeningFor(FilterPtr filter_ptr) {
+SerialListener::removeFilter(FilterPtr filter_ptr) {
   boost::mutex::scoped_lock l(filter_mux);
   filters.erase(std::find(filters.begin(),filters.end(),filter_ptr));
 }
 
 void
-SerialListener::stopListeningForAll() {
+SerialListener::removeFilter(BlockingFilterPtr blocking_filter) {
+  this->removeFilter(blocking_filter->filter_ptr);
+}
+
+void
+SerialListener::removeFilter(BufferedFilterPtr buffered_filter) {
+  this->removeFilter(buffered_filter->filter_ptr);
+}
+
+void
+SerialListener::removeAllFilters() {
   boost::mutex::scoped_lock l(filter_mux);
   filters.clear();
   callback_queue.clear();

From 2f36f14e1a32f0332f88e5dd3cbf444d0a02d719 Mon Sep 17 00:00:00 2001
From: William Woodall 
Date: Thu, 12 Jan 2012 00:11:43 -0600
Subject: [PATCH 13/72] Everything builds, but haven't tested it on a serial
 device.

---
 serial.cmake  | 2 +-
 src/serial.cc | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/serial.cmake b/serial.cmake
index 2d31b12..232e4c1 100644
--- a/serial.cmake
+++ b/serial.cmake
@@ -41,7 +41,7 @@ ENDIF(NOT DEFINED(LIBRARY_OUTPUT_PATH))
 include_directories(${PROJECT_SOURCE_DIR}/include)
 
 # Add default source files
-set(SERIAL_SRCS src/serial.cc src/impl/unix.cc) # src/serial_listener.cc)
+set(SERIAL_SRCS src/serial.cc src/impl/unix.cc src/serial_listener.cc)
 # Add default header files
 set(SERIAL_HEADERS include/serial/serial.h include/serial/serial_listener.h)
 
diff --git a/src/serial.cc b/src/serial.cc
index a975aa2..b5b0c05 100644
--- a/src/serial.cc
+++ b/src/serial.cc
@@ -20,7 +20,7 @@ Serial::Serial (const string &port, int baudrate, long timeout,
                 bytesize_t bytesize, parity_t parity, stopbits_t stopbits,
                 flowcontrol_t flowcontrol)
 {
-  pimpl = new Serial_pimpl(port, baudrate, timeout, bytesize, parity,
+  pimpl = new SerialImpl(port, baudrate, timeout, bytesize, parity,
                            stopbits, flowcontrol);
 }
 

From 48a30ec4ffb1bd107d0452acc93da9c2c72f472e Mon Sep 17 00:00:00 2001
From: William Woodall 
Date: Thu, 12 Jan 2012 01:15:04 -0600
Subject: [PATCH 14/72] Fixed some memory problems on destruction.  Serial
 listener maybe working, serial's read doesn't seem to return anything or
 block at all.

---
 examples/serial_listener_example.cc | 15 ++++++++--
 include/serial/serial_listener.h    | 21 +++++++-------
 serial.cmake                        |  1 +
 src/impl/unix.cc                    | 44 +++++++++++++++++++----------
 src/serial_listener.cc              |  9 +++---
 5 files changed, 57 insertions(+), 33 deletions(-)

diff --git a/examples/serial_listener_example.cc b/examples/serial_listener_example.cc
index 5fd23e4..0a39854 100644
--- a/examples/serial_listener_example.cc
+++ b/examples/serial_listener_example.cc
@@ -13,9 +13,9 @@ void callback(std::string token) {
   std::cout << "callback got a: " << token << std::endl;
 }
 
-int main(void) {
+int run() {
   // Assuming this device prints the string 'pre-substr-post\r' at 100Hz
-  Serial serial("/dev/tty.usbmodemfd1231", 115200);
+  Serial serial("/dev/tty.usbserial-A900cfJA", 115200);
 
   SerialListener listener;
   listener.startListening(serial);
@@ -40,7 +40,7 @@ int main(void) {
       listener.createBlockingFilter(SerialListener::endsWith("post"));
     for (size_t i = 0; i < 3; i++) {
       std::string token = f2->wait(100); // Wait for 100 ms or a matched token
-      if (token == "")
+      if (token != "")
         std::cout << "Found something ending with 'post'" << std::endl;
       else
         std::cout << "Did not find something ending with 'post'" << std::endl;
@@ -82,3 +82,12 @@ int main(void) {
   return 0;
 
 }
+
+int main(void) {
+  try {
+    return run();
+  } catch (std::exception &e) {
+    std::cerr << e.what() << std::endl;
+    return 1;
+  }
+}
diff --git a/include/serial/serial_listener.h b/include/serial/serial_listener.h
index 737e146..d8d4e0a 100644
--- a/include/serial/serial_listener.h
+++ b/include/serial/serial_listener.h
@@ -814,12 +814,10 @@ private:
 class BlockingFilter
 {
 public:
-  BlockingFilter (ComparatorType comparator,
-                  boost::shared_ptr listener)
-  : listener(listener)
-  {
+  BlockingFilter (ComparatorType comparator, SerialListener &listener) {
+    this->listener = &listener;
     DataCallback cb = boost::bind(&BlockingFilter::callback, this, _1);
-    this->filter_ptr = listener->createFilter(comparator, cb);
+    this->filter_ptr = this->listener->createFilter(comparator, cb);
   }
 
   virtual ~BlockingFilter () {
@@ -851,7 +849,7 @@ public:
   }
 
 private:
-  boost::shared_ptr listener;
+  SerialListener * listener;
   boost::condition_variable cond;
   boost::mutex mutex;
   std::string result;
@@ -877,12 +875,13 @@ private:
 class BufferedFilter
 {
 public:
-  BufferedFilter (ComparatorType comparator, size_t buffer_size,
-                  boost::shared_ptr listener)
-  : listener(listener), buffer_size(buffer_size)
+  BufferedFilter (ComparatorType comparator, size_t buffer_size, 
+                  SerialListener &listener)
+  : buffer_size(buffer_size)
   {
+    this->listener = &listener;
     DataCallback cb = boost::bind(&BufferedFilter::callback, this, _1);
-    this->filter_ptr = listener->createFilter(comparator, cb);
+    this->filter_ptr = this->listener->createFilter(comparator, cb);
   }
 
   virtual ~BufferedFilter () {
@@ -944,7 +943,7 @@ public:
 
 private:
   size_t buffer_size;
-  boost::shared_ptr listener;
+  SerialListener * listener;
   ConcurrentQueue queue;
   std::string result;
 
diff --git a/serial.cmake b/serial.cmake
index 232e4c1..8985f84 100644
--- a/serial.cmake
+++ b/serial.cmake
@@ -16,6 +16,7 @@ IF(EXISTS /usr/bin/clang)
   set(CMAKE_OSX_DEPLOYMENT_TARGET "")
   # set(CMAKE_CXX_FLAGS "-ferror-limit=5 -std=c++0x -stdlib=libc++")
   set(CMAKE_CXX_FLAGS "-ferror-limit=5")
+  set(CMAKE_BUILD_TYPE Debug)
 ENDIF(EXISTS /usr/bin/clang)
 
 option(SERIAL_BUILD_TESTS "Build all of the Serial tests." OFF)
diff --git a/src/impl/unix.cc b/src/impl/unix.cc
index 1f84a8a..fade322 100644
--- a/src/impl/unix.cc
+++ b/src/impl/unix.cc
@@ -22,6 +22,20 @@
 #endif
 #endif
 
+class UnhandledException : public std::exception {
+    const char * e_what;
+public:
+    UnhandledException(const char * e_what) {this->e_what = e_what;}
+    
+    virtual const char* what() const throw() {
+        std::stringstream ss;
+        ss << "Unhandled Exception: " << this->e_what;
+        return ss.str().c_str();
+    }
+};
+
+typedef UnhandledException e;
+
 using ::serial::Serial;
 using std::string;
 
@@ -42,20 +56,20 @@ Serial::SerialImpl::~SerialImpl () {
 
 void
 Serial::SerialImpl::open () {
-  if (port_.empty() == false) throw "error";
-  if (isOpen_ == false) throw "error";
-  
+  if (port_.empty() == true) throw e("error");
+  if (isOpen_ == true) throw e("error");
+
   fd_ = ::open (port_.c_str(), O_RDWR | O_NOCTTY | O_NONBLOCK);
-  
+
   if (fd_ == -1) throw "Error";
-  
+
   reconfigurePort();
   isOpen_ = true;
 }
 
 void
 Serial::SerialImpl::reconfigurePort () {
-  if (fd_ == -1) throw "Error"; // Can only operate on a valid file descriptor
+  if (fd_ == -1) throw e("Error"); // Can only operate on a valid file descriptor
   
   struct termios options; // The current options for the file descriptor
   struct termios originalTTYAttrs; // The orignal file descriptor options
@@ -66,7 +80,7 @@ Serial::SerialImpl::reconfigurePort () {
     vtime = int(interCharTimeout_ * 10);
   }
   
-  if (tcgetattr(fd_, &originalTTYAttrs) == -1) throw "Error";
+  if (tcgetattr(fd_, &originalTTYAttrs) == -1) throw e("Error");
   
   options = originalTTYAttrs;
   
@@ -99,7 +113,7 @@ Serial::SerialImpl::reconfigurePort () {
   else if (bytesize_ == FIVEBITS)
       options.c_cflag |= CS5;
   else
-      throw "ValueError(Invalid char len: %%r)";
+      throw e("ValueError(Invalid char len: %%r)");
   // setup stopbits
   if (stopbits_ == STOPBITS_ONE)
       options.c_cflag &= ~(CSTOPB);
@@ -108,7 +122,7 @@ Serial::SerialImpl::reconfigurePort () {
   else if (stopbits_ == STOPBITS_TWO)
       options.c_cflag |=  (CSTOPB);
   else 
-      throw "ValueError(Invalid stop bit specification:)";
+      throw e("ValueError(Invalid stop bit specification:)");
   // setup parity
   options.c_iflag &= ~(INPCK|ISTRIP);
   if (parity_ == PARITY_NONE) {
@@ -122,7 +136,7 @@ Serial::SerialImpl::reconfigurePort () {
     options.c_cflag |=  (PARENB|PARODD);
   }
   else {
-    throw "ValueError(Invalid parity:";
+    throw e("ValueError(Invalid parity:");
   }
   // setup flow control
   // xonxoff
@@ -188,13 +202,13 @@ Serial::SerialImpl::available () {
   if (result == 0) {
     return count;
   } else {
-    throw "Error";
+    throw e("Error");
   }
 }
 
 string
 Serial::SerialImpl::read (size_t size) {
-  if (!isOpen_) throw "PortNotOpenError()";
+  if (!isOpen_) throw e("PortNotOpenError()");
   string message = "";
   char buf[1024];
   fd_set readfds;
@@ -223,7 +237,7 @@ Serial::SerialImpl::read (size_t size) {
           // Disconnected devices, at least on Linux, show the
           // behavior that they are always ready to read immediately
           // but reading returns nothing.
-          throw "SerialException('device reports readiness to read but returned no data (device disconnected?)')";
+          throw e("SerialException('device reports readiness to read but returned no data (device disconnected?)')");
       }
       message.append(buf, bytes_read);
     }
@@ -236,12 +250,12 @@ Serial::SerialImpl::read (size_t size) {
 
 size_t
 Serial::SerialImpl::write (const string &data) {
-  if (isOpen_ == false) throw "portNotOpenError";
+  if (isOpen_ == false) throw e("portNotOpenError");
 
   size_t t = data.length();
   size_t n = ::write(fd_, data.c_str(), data.length());
   if (n == -1) {
-    throw "Write error";
+    throw e("Write error");
   }
   return n;
 }
diff --git a/src/serial_listener.cc b/src/serial_listener.cc
index d3ed2a3..b4a392e 100644
--- a/src/serial_listener.cc
+++ b/src/serial_listener.cc
@@ -64,6 +64,8 @@ SerialListener::callback() {
     std::pair pair;
     while (this->listening) {
       if (this->callback_queue.timed_wait_and_pop(pair, 10)) {
+        std::cout << "Got something off the callback queue: ";
+        std::cout << (*pair.second) << std::endl;
         if (this->listening) {
           try {
             pair.first->callback((*pair.second));
@@ -133,6 +135,7 @@ SerialListener::readSomeData(std::string &temp, size_t this_many) {
     this->handle_exc(SerialListenerException("Serial port not open."));
   }
   temp = this->serial_port->read(this_many);
+  std::cout << "Read(" << temp.length() << "): " << temp << std::endl;
 }
 
 void
@@ -199,7 +202,7 @@ SerialListener::createFilter(ComparatorType comparator, DataCallback callback)
 BlockingFilterPtr
 SerialListener::createBlockingFilter(ComparatorType comparator) {
   return BlockingFilterPtr(
-    new BlockingFilter(comparator, boost::shared_ptr(this)));
+    new BlockingFilter(comparator, (*this)));
 }
 
 BufferedFilterPtr
@@ -207,9 +210,7 @@ SerialListener::createBufferedFilter(ComparatorType comparator,
                                      size_t buffer_size)
 {
   return BufferedFilterPtr(
-    new BufferedFilter(comparator,
-                       buffer_size,
-                       boost::shared_ptr(this)));
+    new BufferedFilter(comparator, buffer_size, (*this)));
 }
 
 void

From 368eb0d83c962852d522edea7abfd17e870fafaf Mon Sep 17 00:00:00 2001
From: William Woodall 
Date: Thu, 12 Jan 2012 01:18:09 -0600
Subject: [PATCH 15/72] Quieting tests for now

---
 tests/serial_listener_tests.cc | 250 ++++++++++++++++-----------------
 1 file changed, 125 insertions(+), 125 deletions(-)

diff --git a/tests/serial_listener_tests.cc b/tests/serial_listener_tests.cc
index 7c5e3df..e1360e2 100644
--- a/tests/serial_listener_tests.cc
+++ b/tests/serial_listener_tests.cc
@@ -18,131 +18,131 @@ void default_handler(std::string line) {
 
 namespace {
 
-class SerialListenerTests : public ::testing::Test {
-protected:
-  virtual void SetUp() {
-    listener.listening = true;
-    listener.setDefaultHandler(default_handler);
-    listener.callback_thread =
-     boost::thread(boost::bind(&SerialListener::callback, &listener));
-  }
-
-  virtual void TearDown() {
-    listener.listening = false;
-    listener.callback_thread.join();
-  }
-
-  void stopCallbackThread() {
-    while (true) {
-      boost::this_thread::sleep(boost::posix_time::milliseconds(1));
-      boost::mutex::scoped_lock lock(listener.callback_queue.the_mutex);
-      if (listener.callback_queue.the_queue.empty())
-        break;
-    }
-    listener.listening = false;
-    listener.callback_thread.join();
-  }
-
-  void execute_listenForStringOnce() {
-    listener.listenForStringOnce("?$1E", 50);
-  }
-
-  void simulate_loop(std::string input_str) {
-    std::vector new_tokens;
-    listener.tokenize(input_str, new_tokens);
-    listener.filterNewTokens(new_tokens);
-  }
-
-  SerialListener listener;
-
-};
-
-TEST_F(SerialListenerTests, handlesPartialMessage) {
-  global_count = 0;
-  std::string input_str = "?$1E\r$1E=Robo";
-
-  simulate_loop(input_str);
-
-  // give some time for the callback thread to finish
-  stopCallbackThread();
-
-  ASSERT_EQ(global_count, 1);
-}
-
-TEST_F(SerialListenerTests, listenForOnceWorks) {
-  global_count = 0;
-
-  boost::thread t(
-    boost::bind(&SerialListenerTests::execute_listenForStringOnce, this));
-
-  boost::this_thread::sleep(boost::posix_time::milliseconds(5));
-
-  simulate_loop("\r+\r?$1E\r$1E=Robo");
-
-  ASSERT_TRUE(t.timed_join(boost::posix_time::milliseconds(60)));
-
-  // Make sure the filters are getting deleted
-  ASSERT_EQ(listener.filters.size(), 0);
-
-  // give some time for the callback thread to finish
-  stopCallbackThread();
-
-  ASSERT_EQ(global_count, 1);
-}
-
-// lookForOnce should not find it, but timeout after 1000ms, so it should 
-//  still join.
-TEST_F(SerialListenerTests, listenForOnceTimesout) {
-  global_count = 0;
-
-  boost::thread t(
-    boost::bind(&SerialListenerTests::execute_listenForStringOnce, this));
-
-  boost::this_thread::sleep(boost::posix_time::milliseconds(55));
-
-  simulate_loop("\r+\r?$1ENOTRIGHT\r$1E=Robo");
-
-  ASSERT_TRUE(t.timed_join(boost::posix_time::milliseconds(60)));
-
-  // give some time for the callback thread to finish
-  stopCallbackThread();
-
-  ASSERT_EQ(global_count, 2);
-}
-
-bool listenForComparator(std::string line) {
-  // std::cout << "In listenForComparator(" << line << ")" << std::endl;
-  if (line.substr(0,2) == "V=") {
-    return true;
-  }
-  return false;
-}
-
-void listenForCallback(std::string line) {
-  // std::cout << "In listenForCallback(" << line << ")" << std::endl;
-  global_listen_count++;
-}
-
-TEST_F(SerialListenerTests, listenForWorks) {
-  global_count = 0;
-  global_listen_count = 0;
-
-  FilterPtr filt_uuid =
-    listener.listenFor(listenForComparator, listenForCallback);
-
-  simulate_loop("\r+\rV=05:06\r?$1E\rV=06:05\r$1E=Robo");
-
-  // give some time for the callback thread to finish
-  stopCallbackThread();
-
-  ASSERT_EQ(global_count, 2);
-  ASSERT_EQ(global_listen_count, 2);
-
-  listener.stopListeningFor(filt_uuid);
-
-  ASSERT_EQ(listener.filters.size(), 0);
-
-}
+// class SerialListenerTests : public ::testing::Test {
+// protected:
+//   virtual void SetUp() {
+//     listener.listening = true;
+//     listener.setDefaultHandler(default_handler);
+//     listener.callback_thread =
+//      boost::thread(boost::bind(&SerialListener::callback, &listener));
+//   }
+// 
+//   virtual void TearDown() {
+//     listener.listening = false;
+//     listener.callback_thread.join();
+//   }
+// 
+//   void stopCallbackThread() {
+//     while (true) {
+//       boost::this_thread::sleep(boost::posix_time::milliseconds(1));
+//       boost::mutex::scoped_lock lock(listener.callback_queue.the_mutex);
+//       if (listener.callback_queue.the_queue.empty())
+//         break;
+//     }
+//     listener.listening = false;
+//     listener.callback_thread.join();
+//   }
+// 
+//   void execute_listenForStringOnce() {
+//     listener.listenForStringOnce("?$1E", 50);
+//   }
+// 
+//   void simulate_loop(std::string input_str) {
+//     std::vector new_tokens;
+//     listener.tokenize(input_str, new_tokens);
+//     listener.filterNewTokens(new_tokens);
+//   }
+// 
+//   SerialListener listener;
+// 
+// };
+// 
+// TEST_F(SerialListenerTests, handlesPartialMessage) {
+//   global_count = 0;
+//   std::string input_str = "?$1E\r$1E=Robo";
+// 
+//   simulate_loop(input_str);
+// 
+//   // give some time for the callback thread to finish
+//   stopCallbackThread();
+// 
+//   ASSERT_EQ(global_count, 1);
+// }
+// 
+// TEST_F(SerialListenerTests, listenForOnceWorks) {
+//   global_count = 0;
+// 
+//   boost::thread t(
+//     boost::bind(&SerialListenerTests::execute_listenForStringOnce, this));
+// 
+//   boost::this_thread::sleep(boost::posix_time::milliseconds(5));
+// 
+//   simulate_loop("\r+\r?$1E\r$1E=Robo");
+// 
+//   ASSERT_TRUE(t.timed_join(boost::posix_time::milliseconds(60)));
+// 
+//   // Make sure the filters are getting deleted
+//   ASSERT_EQ(listener.filters.size(), 0);
+// 
+//   // give some time for the callback thread to finish
+//   stopCallbackThread();
+// 
+//   ASSERT_EQ(global_count, 1);
+// }
+// 
+// // lookForOnce should not find it, but timeout after 1000ms, so it should 
+// //  still join.
+// TEST_F(SerialListenerTests, listenForOnceTimesout) {
+//   global_count = 0;
+// 
+//   boost::thread t(
+//     boost::bind(&SerialListenerTests::execute_listenForStringOnce, this));
+// 
+//   boost::this_thread::sleep(boost::posix_time::milliseconds(55));
+// 
+//   simulate_loop("\r+\r?$1ENOTRIGHT\r$1E=Robo");
+// 
+//   ASSERT_TRUE(t.timed_join(boost::posix_time::milliseconds(60)));
+// 
+//   // give some time for the callback thread to finish
+//   stopCallbackThread();
+// 
+//   ASSERT_EQ(global_count, 2);
+// }
+// 
+// bool listenForComparator(std::string line) {
+//   // std::cout << "In listenForComparator(" << line << ")" << std::endl;
+//   if (line.substr(0,2) == "V=") {
+//     return true;
+//   }
+//   return false;
+// }
+// 
+// void listenForCallback(std::string line) {
+//   // std::cout << "In listenForCallback(" << line << ")" << std::endl;
+//   global_listen_count++;
+// }
+// 
+// TEST_F(SerialListenerTests, listenForWorks) {
+//   global_count = 0;
+//   global_listen_count = 0;
+// 
+//   FilterPtr filt_uuid =
+//     listener.listenFor(listenForComparator, listenForCallback);
+// 
+//   simulate_loop("\r+\rV=05:06\r?$1E\rV=06:05\r$1E=Robo");
+// 
+//   // give some time for the callback thread to finish
+//   stopCallbackThread();
+// 
+//   ASSERT_EQ(global_count, 2);
+//   ASSERT_EQ(global_listen_count, 2);
+// 
+//   listener.stopListeningFor(filt_uuid);
+// 
+//   ASSERT_EQ(listener.filters.size(), 0);
+// 
+// }
 
 }  // namespace
 

From 11807e407b544a74906f7862d8c3f59b2839b05a Mon Sep 17 00:00:00 2001
From: John Harrison 
Date: Thu, 12 Jan 2012 12:46:08 -0600
Subject: [PATCH 16/72] Fixed some issues I found while testing my code, also
 implemented the drain, flush, set/send Break, get {CTS, RI, CD, DSR}, set
 {RTS, DTR}

---
 Makefile                   |  16 +-
 include/serial/impl/unix.h |  54 +++----
 include/serial/serial.h    |  12 ++
 src/impl/unix.cc           | 305 ++++++++++++++++++++++++-------------
 src/serial.cc              |  44 +++++-
 tests/serial_tests.cc      |  29 ++++
 6 files changed, 316 insertions(+), 144 deletions(-)
 create mode 100644 tests/serial_tests.cc

diff --git a/Makefile b/Makefile
index 5df6eab..3f1ccf4 100644
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,11 @@
-ifdef ROS_ROOT
-include $(shell rospack find mk)/cmake.mk
-else
-include serial.makefile
-endif
+CXX=clang++
+CXXFLAGS=-g -I./include
+
+test: tests/serial_tests.o src/serial.o src/impl/unix.o
+	$(CXX) -o test tests/serial_tests.o src/serial.o src/impl/unix.o
+
+# ifdef ROS_ROOT
+# include $(shell rospack find mk)/cmake.mk
+# else
+# include serial.makefile
+# endif
diff --git a/include/serial/impl/unix.h b/include/serial/impl/unix.h
index 9fe739a..4590ee6 100644
--- a/include/serial/impl/unix.h
+++ b/include/serial/impl/unix.h
@@ -58,22 +58,22 @@ public:
   void close ();
   bool isOpen ();
 
-	size_t available ();
+  size_t available ();
   string read (size_t size = 1);
   size_t write (const string &data);
-	
-	void flush ();
-	void flushInput ();
-	void flushOutput ();
+  
+  void flush ();
+  void flushInput ();
+  void flushOutput ();
 
-	void sendBreak();
-	void setBreak();
-	void setRTS();
-	void setDTR();
-	void getCTS();
-	void getDSR();
-	void getRI();
-	void getCD();
+  void sendBreak(int duration);
+  void setBreak(bool level);
+  void setRTS(bool level);
+  void setDTR(bool level);
+  bool getCTS();
+  bool getDSR();
+  bool getRI();
+  bool getCD();
 
   void setPort (const string &port);
   string getPort () const;
@@ -97,25 +97,25 @@ public:
   flowcontrol_t getFlowcontrol () const;
 
 protected:
-	void reconfigurePort ();
+  void reconfigurePort ();
 
 private:
   int fd_; // The current file descriptor.
 
-	bool isOpen_;
-	
-	int interCharTimeout_;
-	int writeTimeout_;
-	int xonxoff_;
-	int rtscts_;
+  bool isOpen_;
+  
+  int interCharTimeout_;
+  int writeTimeout_;
+  int xonxoff_;
+  int rtscts_;
 
-	string port_;               // Path to the file descriptor
-	int baudrate_;              // Baudrate
-	long timeout_;              // Timeout for read operations
-	bytesize_t bytesize_;       // Size of the bytes
-	parity_t parity_;           // Parity
-	stopbits_t stopbits_;       // Stop Bits
-	flowcontrol_t flowcontrol_; // Flow Control
+  string port_;               // Path to the file descriptor
+  int baudrate_;              // Baudrate
+  long timeout_;              // Timeout for read operations
+  bytesize_t bytesize_;       // Size of the bytes
+  parity_t parity_;           // Parity
+  stopbits_t stopbits_;       // Stop Bits
+  flowcontrol_t flowcontrol_; // Flow Control
 };
 
 }
diff --git a/include/serial/serial.h b/include/serial/serial.h
index b4bca19..5ec3d85 100644
--- a/include/serial/serial.h
+++ b/include/serial/serial.h
@@ -373,6 +373,18 @@ public:
   flowcontrol_t
   getFlowcontrol () const;
 
+  void flush();
+  void flushInput();
+  void flushOutput();
+  void sendBreak(int duration);
+  void setBreak(bool level = true);
+  void setRTS(bool level = true);
+  void setDTR(bool level = true);
+  bool getCTS();
+  bool getDSR();
+  bool getRI();
+  bool getCD();
+
 private:
   // Disable copy constructors
   Serial(const Serial&);
diff --git a/src/impl/unix.cc b/src/impl/unix.cc
index 766aba4..6b94f69 100644
--- a/src/impl/unix.cc
+++ b/src/impl/unix.cc
@@ -28,14 +28,15 @@ using std::string;
 Serial::SerialImpl::SerialImpl (const string &port, int baudrate,
                                     long timeout, bytesize_t bytesize,
                                     parity_t parity, stopbits_t stopbits,
-																		flowcontrol_t flowcontrol)
+                                    flowcontrol_t flowcontrol)
 : fd_(-1), isOpen_(false), interCharTimeout_(-1), port_(port), baudrate_(baudrate),
   timeout_(timeout), bytesize_(bytesize), parity_(parity), stopbits_(stopbits),
-	flowcontrol_(flowcontrol)
+  flowcontrol_(flowcontrol)
 {
-	if (port_.empty() == false) {
-		this->open();
-	}
+  printf("Got port: %s\n", port.c_str());
+  if (!port_.empty()) {
+    this->open();
+  }
 }
 
 Serial::SerialImpl::~SerialImpl () {
@@ -44,46 +45,48 @@ Serial::SerialImpl::~SerialImpl () {
 
 void
 Serial::SerialImpl::open () {
-	if (port_.empty() == false) {
-		throw "error";
-	}
-	if (isOpen_ == false) {
-		throw "error";
-	}
-	
-	fd_ = ::open (port_.c_str(), O_RDWR | O_NOCTTY | O_NONBLOCK);
-	
-	if (fd_ == -1) {
-//		printf("Error opening serial port %s - %s(%d).\n",
-//		       port.c_str(), strerror(errno), errno);
-		throw "Error"; // Error
-	}
-	
-	reconfigurePort();
-	isOpen_ = true;
+  if (port_.empty()) {
+    printf("Port was empty\n");
+    throw "error";
+  }
+  if (isOpen_ == true) {
+    printf("Port already opened\n");
+    throw "error";
+  }
+  
+  fd_ = ::open (port_.c_str(), O_RDWR | O_NOCTTY | O_NONBLOCK);
+
+  if (fd_ == -1) {
+    printf("Error opening serial port %s - %s(%d).\n",
+           port_.c_str(), strerror(errno), errno);
+    throw "Error"; // Error
+  }
+
+  reconfigurePort();
+  isOpen_ = true;
 }
 
 void
 Serial::SerialImpl::reconfigurePort () {
-	if (fd_ == -1) {
-		throw "Error"; // Can only operate on a valid file descriptor
-	}
-	
-	struct termios options; // The current options for the file descriptor
-	struct termios originalTTYAttrs; // The orignal file descriptor options
-	
+  if (fd_ == -1) {
+    throw "Error"; // Can only operate on a valid file descriptor
+  }
+  
+  struct termios options; // The current options for the file descriptor
+  struct termios originalTTYAttrs; // The orignal file descriptor options
+  
   uint8_t vmin = 0, vtime = 0;                // timeout is done via select
   if (interCharTimeout_ == -1) {
-		vmin = 1;
-  	vtime = int(interCharTimeout_ * 10);
-	}
-	
-	if (tcgetattr(fd_, &originalTTYAttrs) == -1) {
-		throw "Error";
-	}
-	
-	options = originalTTYAttrs;
-	
+    vmin = 1;
+    vtime = int(interCharTimeout_ * 10);
+  }
+  
+  if (tcgetattr(fd_, &originalTTYAttrs) == -1) {
+    throw "Error";
+  }
+  
+  options = originalTTYAttrs;
+  
   // set up raw mode / no echo / binary
   options.c_cflag |=  (CLOCAL|CREAD);
   options.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL|
@@ -95,12 +98,12 @@ Serial::SerialImpl::reconfigurePort () {
   options.c_iflag &= ~IUCLC;
 #endif
 #ifdef PARMRK
-	options.c_iflag &= ~PARMRK;
+  options.c_iflag &= ~PARMRK;
 #endif
 
   // setup baud rate
-	// TODO(ash_git): validate baud rate
-	cfsetspeed(&options, baudrate_);
+  // TODO(ash_git): validate baud rate
+  cfsetspeed(&options, baudrate_);
 
   // setup char len
   options.c_cflag &= ~CSIZE;
@@ -121,23 +124,23 @@ Serial::SerialImpl::reconfigurePort () {
       options.c_cflag |=  (CSTOPB);  // XXX same as TWO.. there is no POSIX support for 1.5
   else if (stopbits_ == STOPBITS_TWO)
       options.c_cflag |=  (CSTOPB);
-	else 
+  else 
       throw "ValueError(Invalid stop bit specification:)";
   // setup parity
   options.c_iflag &= ~(INPCK|ISTRIP);
   if (parity_ == PARITY_NONE) {
     options.c_cflag &= ~(PARENB|PARODD);
   }
-	else if (parity_ == PARITY_EVEN) {
+  else if (parity_ == PARITY_EVEN) {
     options.c_cflag &= ~(PARODD);
     options.c_cflag |=  (PARENB);
   } 
-	else if (parity_ == PARITY_ODD) {
+  else if (parity_ == PARITY_ODD) {
     options.c_cflag |=  (PARENB|PARODD);
   }
-	else {
+  else {
     throw "ValueError(Invalid parity:";
-	}
+  }
   // setup flow control
   // xonxoff
 #ifdef IXANY
@@ -159,7 +162,7 @@ Serial::SerialImpl::reconfigurePort () {
     options.c_cflag &= ~(CRTSCTS);
 #elif defined CNEW_RTSCTS
   if (rtscts_)
-	  options.c_cflag |=  (CNEW_RTSCTS);
+    options.c_cflag |=  (CNEW_RTSCTS);
   else
     options.c_cflag &= ~(CNEW_RTSCTS);
 #else
@@ -173,18 +176,18 @@ Serial::SerialImpl::reconfigurePort () {
   options.c_cc[VTIME] = vtime;
 
   // activate settings
-	::tcsetattr(fd_, TCSANOW, &options);
+  ::tcsetattr(fd_, TCSANOW, &options);
 }
 
 void
 Serial::SerialImpl::close () {
-	if (isOpen_ == true) {
-		if (fd_ != -1) {
-			::close(fd_);
-			fd_ = -1;
-		}
-		isOpen_ = false;
-	}
+  if (isOpen_ == true) {
+    if (fd_ != -1) {
+      ::close(fd_);
+      fd_ = -1;
+    }
+    isOpen_ = false;
+  }
 }
 
 bool
@@ -194,73 +197,73 @@ Serial::SerialImpl::isOpen () {
 
 size_t
 Serial::SerialImpl::available () {
-	if (!isOpen_) {
-		return 0;
-	}
-	int count = 0;
-	int result = ioctl(fd_, TIOCINQ, &count);
-	if (result == 0) {
-		return count;
-	}
-	else {
-		throw "Error";
-	}
+  if (!isOpen_) {
+    return 0;
+  }
+  int count = 0;
+  int result = ioctl(fd_, TIOCINQ, &count);
+  if (result == 0) {
+    return count;
+  }
+  else {
+    throw "Error";
+  }
 }
 
 string
 Serial::SerialImpl::read (size_t size) {
   if (!isOpen_) {
-  	throw "PortNotOpenError()"; //
+    throw "PortNotOpenError()"; //
   }
-	string message = "";
-	char buf[1024];
-	fd_set readfds;
+  string message = "";
+  char buf[1024]; // TODO(ash_gti): Should this be 1024? or...?
+  fd_set readfds;
   while (message.length() < size) {
-		FD_ZERO(&readfds);
-		FD_SET(fd_, &readfds);
-		struct timeval timeout;
-		timeout.tv_sec = timeout_ / 1000;
-		timeout.tv_usec = timeout_ % 1000;
-		int r = select(1, &readfds, NULL, NULL, &timeout);
-	
-		if (r == -1 && errno == EINTR)
-			continue;
+    FD_ZERO(&readfds);
+    FD_SET(fd_, &readfds);
+    struct timeval timeout;
+    timeout.tv_sec = timeout_ / 1000;
+    timeout.tv_usec = timeout_ % 1000;
+    int r = select(fd_ + 1, &readfds, NULL, NULL, &timeout);
 
-		if (r == -1) {
+    if (r == -1 && errno == EINTR)
+      continue;
+
+    if (r == -1) {
       perror("select()");
       exit(EXIT_FAILURE);
     }
 
     if (FD_ISSET(fd_, &readfds)) {
-			memset(buf, 0, 1024);
-			size_t bytes_read = ::read(fd_, buf, 1024);
-	    // read should always return some data as select reported it was
-	    // ready to read when we get to this point.
-	    if (bytes_read < 1) {
-	        // Disconnected devices, at least on Linux, show the
-	        // behavior that they are always ready to read immediately
-	        // but reading returns nothing.
-	        throw "SerialException('device reports readiness to read but returned no data (device disconnected?)')";
-			}
-	    message.append(buf, bytes_read);
-		}
-		else {
-			break; // Timeout
-		}
-	}
+      memset(buf, 0, 1024);
+      size_t bytes_read = ::read(fd_, buf, size-strlen(buf));
+      // read should always return some data as select reported it was
+      // ready to read when we get to this point.
+      if (bytes_read < 1) {
+        // Disconnected devices, at least on Linux, show the
+        // behavior that they are always ready to read immediately
+        // but reading returns nothing.
+        throw "SerialException('device reports readiness to read but returned no data (device disconnected?)')";
+      }
+      message.append(buf, bytes_read);
+    }
+    else {
+      break; // Timeout
+    }
+  }
   return message;
 }
 
 size_t
 Serial::SerialImpl::write (const string &data) {
   if (isOpen_ == false) {
-		throw "portNotOpenError";
-	}
+    throw "portNotOpenError";
+  }
   size_t t = data.length();
-	size_t n = ::write(fd_, data.c_str(), data.length());
-	if (n == -1) {
-		throw "Write error";
-	}
+  size_t n = ::write(fd_, data.c_str(), data.length());
+  if (n == -1) {
+    throw "Write error";
+  }
   return n;
 }
 
@@ -286,8 +289,8 @@ Serial::SerialImpl::getTimeout () const {
 
 void
 Serial::SerialImpl::setBaudrate (int baudrate) {
-	baudrate_ = baudrate;
-	reconfigurePort();
+  baudrate_ = baudrate;
+  reconfigurePort();
 }
 
 int
@@ -297,7 +300,7 @@ Serial::SerialImpl::getBaudrate () const {
 
 void
 Serial::SerialImpl::setBytesize (serial::bytesize_t bytesize) {
-	bytesize_ = bytesize;
+  bytesize_ = bytesize;
 }
 
 serial::bytesize_t
@@ -336,6 +339,96 @@ Serial::SerialImpl::getFlowcontrol () const {
 }
 
 
+void Serial::SerialImpl::flush () {
+  if (isOpen_ == false) {
+    throw "portNotOpen";
+  }
+  tcdrain(fd_);
+}
+void Serial::SerialImpl::flushInput () {
+  if (isOpen_ == false) {
+    throw "portNotOpen";
+  }
+  tcflush(fd_, TCIFLUSH);
+}
+void Serial::SerialImpl::flushOutput () {
+  if (isOpen_ == false) {
+    throw "portNotOpen";
+  }
+  tcflush(fd_, TCOFLUSH);
+}
+
+void Serial::SerialImpl::sendBreak(int duration) {
+  if (isOpen_ == false) {
+    throw "portNotOpen";
+  }
+  tcsendbreak(fd_, int(duration/4));
+}
+void Serial::SerialImpl::setBreak(bool level) {
+  if (isOpen_ == false) {
+    throw "portNotOpen";
+  }
+  if (level) {
+    ioctl(fd_, TIOCSBRK);
+  }
+  else {
+    ioctl(fd_, TIOCCBRK);
+  }
+}
+void Serial::SerialImpl::setRTS(bool level) {
+  if (isOpen_ == false) {
+    throw "portNotOpen";
+  }
+  if (level) {
+    ioctl(fd_, TIOCMBIS, TIOCM_RTS);
+  }
+  else {
+    ioctl(fd_, TIOCMBIC, TIOCM_RTS);
+  }
+}
+void Serial::SerialImpl::setDTR(bool level) {
+  if (isOpen_ == false) {
+    throw "portNotOpen";
+  }
+  if (level) {
+    ioctl(fd_, TIOCMBIS, TIOCM_DTR);
+  }
+  else {
+    ioctl(fd_, TIOCMBIC, TIOCM_DTR);
+  }
+
+}
+bool Serial::SerialImpl::getCTS() {
+  if (isOpen_ == false) {
+    throw "portNotOpen";
+  }
+  int s = ioctl(fd_, TIOCMGET, 0);
+  return (s & TIOCM_CTS) != 0;
+}
+bool Serial::SerialImpl::getDSR() {
+  if (isOpen_ == false) {
+    throw "portNotOpen";
+  }
+  int s = ioctl(fd_, TIOCMGET, 0);
+  return (s & TIOCM_DSR) != 0;
+}
+bool Serial::SerialImpl::getRI() {
+  if (isOpen_ == false) {
+    throw "portNotOpen";
+  }
+  int s = ioctl(fd_, TIOCMGET, 0);
+  return (s & TIOCM_RI) != 0;
+}
+bool Serial::SerialImpl::getCD() {
+  if (isOpen_ == false) {
+    throw "portNotOpen";
+  }
+  int s = ioctl(fd_, TIOCMGET, 0);
+  return (s & TIOCM_CD) != 0;
+}
+
+
+
 
 
 
diff --git a/src/serial.cc b/src/serial.cc
index 3462019..c631b51 100644
--- a/src/serial.cc
+++ b/src/serial.cc
@@ -23,7 +23,6 @@ Serial::Serial (const string &port, int baudrate,
 : pimpl(new SerialImpl(port,baudrate,timeout,bytesize,parity,stopbits,
                         flowcontrol))
 {
-  
 }
 
 Serial::~Serial () {
@@ -62,12 +61,12 @@ Serial::read (size_t size) {
 string
 Serial::readline(size_t size, string eol) {
   size_t leneol = eol.length();
-  string line;
+  string line = "";
   while (true) {
-    string c = read(1);
-    if (c.empty()) {
-      line += c;
-      if (line.substr(line.length() - leneol, leneol) == eol) {
+    string c = pimpl->read(1);
+    if (!c.empty()) {
+      line.append(c);
+      if (line.length() > leneol && line.substr(line.length() - leneol, leneol) == eol) {
         break;
       }
       if (line.length() >= size) {
@@ -186,6 +185,39 @@ Serial::getFlowcontrol () const {
   return this->pimpl->getFlowcontrol ();
 }
 
+void Serial::flush() {
+  this->pimpl->flush();
+}
+void Serial::flushInput() {
+  this->pimpl->flushInput();
+}
+void Serial::flushOutput() {
+  this->pimpl->flushOutput();
+}
+void Serial::sendBreak(int duration) {
+  this->pimpl->sendBreak(duration);
+}
+void Serial::setBreak(bool level) {
+  this->pimpl->setBreak(level);
+}
+void Serial::setRTS(bool level) {
+  this->pimpl->setRTS(level);
+}
+void Serial::setDTR(bool level) {
+  this->pimpl->setDTR(level);
+}
+bool Serial::getCTS() {
+  return this->pimpl->getCTS();
+}
+bool Serial::getDSR() {
+  return this->pimpl->getDSR();
+}
+bool Serial::getRI() {
+  return this->pimpl->getRI();
+}
+bool Serial::getCD() {
+  return this->pimpl->getCD();
+}
 
 
 
diff --git a/tests/serial_tests.cc b/tests/serial_tests.cc
new file mode 100644
index 0000000..663a333
--- /dev/null
+++ b/tests/serial_tests.cc
@@ -0,0 +1,29 @@
+#include "gtest/gtest.h"
+
+#include 
+
+// OMG this is so nasty...
+// #define private public
+// #define protected public
+#include 
+#include 
+
+#include "serial/serial.h"
+
+using std::string;
+using std::cout;
+using std::endl;
+using serial::Serial;
+
+int main(int argc, char **argv) {
+  Serial s("/dev/tty.usbserial-A900adHq", 9600, 2000);
+  s.flush();
+  int count = 0;
+  while (count < 10) {
+    size_t available = s.available();
+    cout << "avialable: " << available << endl;
+    string line = s.readline();
+    cout << count << ": " << line;
+    count++;
+  }
+}

From 8273b7e1539946304758911ef5ead2f8941fde68 Mon Sep 17 00:00:00 2001
From: John Harrison 
Date: Thu, 12 Jan 2012 13:03:26 -0600
Subject: [PATCH 17/72] Correcting the behavior of Serial::setPort and anything
 that modifies stuff related to baud/parity/etc.

---
 Makefile         | 23 ++++++++++++-----------
 src/impl/unix.cc |  7 ++++++-
 src/serial.cc    |  3 +++
 3 files changed, 21 insertions(+), 12 deletions(-)

diff --git a/Makefile b/Makefile
index 3f1ccf4..914dad5 100644
--- a/Makefile
+++ b/Makefile
@@ -1,11 +1,12 @@
-CXX=clang++
-CXXFLAGS=-g -I./include
-
-test: tests/serial_tests.o src/serial.o src/impl/unix.o
-	$(CXX) -o test tests/serial_tests.o src/serial.o src/impl/unix.o
-
-# ifdef ROS_ROOT
-# include $(shell rospack find mk)/cmake.mk
-# else
-# include serial.makefile
-# endif
+# ash_gti's dumb downed makefile so I can more easily test stuff
+# CXX=clang++
+# CXXFLAGS=-g -I./include
+# 
+# test: tests/serial_tests.o src/serial.o src/impl/unix.o
+# 	$(CXX) -o test tests/serial_tests.o src/serial.o src/impl/unix.o
+# 
+ifdef ROS_ROOT
+include $(shell rospack find mk)/cmake.mk
+else
+include serial.makefile
+endif
diff --git a/src/impl/unix.cc b/src/impl/unix.cc
index 1039d3b..6d09558 100644
--- a/src/impl/unix.cc
+++ b/src/impl/unix.cc
@@ -291,6 +291,7 @@ Serial::SerialImpl::getPort () const {
 void
 Serial::SerialImpl::setTimeout (long timeout) {
   timeout_ = timeout;
+  if (isOpen_) reconfigurePort();
 }
 
 long
@@ -301,7 +302,7 @@ Serial::SerialImpl::getTimeout () const {
 void
 Serial::SerialImpl::setBaudrate (int baudrate) {
   baudrate_ = baudrate;
-  reconfigurePort();
+  if (isOpen_) reconfigurePort();
 }
 
 int
@@ -312,6 +313,7 @@ Serial::SerialImpl::getBaudrate () const {
 void
 Serial::SerialImpl::setBytesize (serial::bytesize_t bytesize) {
   bytesize_ = bytesize;
+  if (isOpen_) reconfigurePort();
 }
 
 serial::bytesize_t
@@ -322,6 +324,7 @@ Serial::SerialImpl::getBytesize () const {
 void
 Serial::SerialImpl::setParity (serial::parity_t parity) {
   parity_ = parity;
+  if (isOpen_) reconfigurePort();
 }
 
 serial::parity_t
@@ -332,6 +335,7 @@ Serial::SerialImpl::getParity () const {
 void
 Serial::SerialImpl::setStopbits (serial::stopbits_t stopbits) {
   stopbits_ = stopbits;
+  if (isOpen_) reconfigurePort();
 }
 
 serial::stopbits_t
@@ -342,6 +346,7 @@ Serial::SerialImpl::getStopbits () const {
 void
 Serial::SerialImpl::setFlowcontrol (serial::flowcontrol_t flowcontrol) {
   flowcontrol_ = flowcontrol;
+  if (isOpen_) reconfigurePort();
 }
 
 serial::flowcontrol_t
diff --git a/src/serial.cc b/src/serial.cc
index ca9e8ed..5f06dab 100644
--- a/src/serial.cc
+++ b/src/serial.cc
@@ -106,7 +106,10 @@ Serial::write (const string &data) {
 
 void
 Serial::setPort (const string &port) {
+  bool was_open = pimpl->isOpen();
+  if (was_open) this->close();
   this->pimpl->setPort (port);
+  if (was_open) this->open();
 }
 
 string

From ea3e19a1a07a325ab87de89887e432d9354c0fac Mon Sep 17 00:00:00 2001
From: John Harrison 
Date: Thu, 12 Jan 2012 16:43:53 -0600
Subject: [PATCH 18/72] Enabling warnings, to make sure things are good.

---
 serial.cmake | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/serial.cmake b/serial.cmake
index 8985f84..04efe70 100644
--- a/serial.cmake
+++ b/serial.cmake
@@ -15,7 +15,7 @@ IF(EXISTS /usr/bin/clang)
   set(CMAKE_CXX_COMPILER /usr/bin/clang++)
   set(CMAKE_OSX_DEPLOYMENT_TARGET "")
   # set(CMAKE_CXX_FLAGS "-ferror-limit=5 -std=c++0x -stdlib=libc++")
-  set(CMAKE_CXX_FLAGS "-ferror-limit=5")
+  set(CMAKE_CXX_FLAGS "-ferror-limit=5 -m64 -Wall -Weffc++ -pedantic -pedantic-errors -Wextra  -Wall -Waggregate-return -Wcast-align -Wcast-qual  -Wchar-subscripts  -Wcomment -Wconversion -Wdisabled-optimization -Wfloat-equal  -Wformat  -Wformat=2 -Wformat-nonliteral -Wformat-security  -Wformat-y2k -Wimplicit  -Wimport  -Winit-self  -Winline -Winvalid-pch   -Wlong-long -Wmissing-braces -Wmissing-field-initializers -Wmissing-format-attribute   -Wmissing-include-dirs -Wmissing-noreturn -Wpacked  -Wpadded -Wparentheses  -Wpointer-arith -Wredundant-decls -Wreturn-type -Wsequence-point  -Wshadow -Wsign-compare  -Wstack-protector -Wstrict-aliasing -Wstrict-aliasing=2 -Wswitch  -Wswitch-default -Wswitch-enum -Wtrigraphs  -Wuninitialized -Wunknown-pragmas  -Wunreachable-code -Wunused -Wunused-function  -Wunused-label  -Wunused-parameter -Wunused-value  -Wunused-variable  -Wvariadic-macros -Wvolatile-register-var  -Wwrite-strings")
   set(CMAKE_BUILD_TYPE Debug)
 ENDIF(EXISTS /usr/bin/clang)
 

From c2ad2721f332dd4b797df7e4fb09cab1aace4933 Mon Sep 17 00:00:00 2001
From: John Harrison 
Date: Thu, 12 Jan 2012 16:44:19 -0600
Subject: [PATCH 19/72] Fixing a number of warnings in the serial library. The
 SerialListener still needs some love.

---
 include/serial/impl/unix.h       |  43 +++++----
 include/serial/serial.h          |  98 ++++++++++++---------
 include/serial/serial_listener.h |   2 +-
 src/impl/unix.cc                 | 147 ++++++++++++++-----------------
 src/serial.cc                    |  24 ++---
 5 files changed, 158 insertions(+), 156 deletions(-)

diff --git a/include/serial/impl/unix.h b/include/serial/impl/unix.h
index c885348..ec8e246 100644
--- a/include/serial/impl/unix.h
+++ b/include/serial/impl/unix.h
@@ -41,22 +41,25 @@
 namespace serial {
 
 using std::string;
+using std::invalid_argument;
+using serial::SerialExecption;
+using serial::IOException;
 
 class serial::Serial::SerialImpl {
 public:
   SerialImpl (const string &port,
-             int baudrate,
-             long timeout,
-             bytesize_t bytesize,
-             parity_t parity,
-             stopbits_t stopbits,
-             flowcontrol_t flowcontrol);
+              unsigned long baudrate,
+              long timeout,
+              bytesize_t bytesize,
+              parity_t parity,
+              stopbits_t stopbits,
+              flowcontrol_t flowcontrol);
 
   virtual ~SerialImpl ();
 
   void open ();
-  void close ();
-  bool isOpen ();
+  void close () ;
+  bool isOpen () const;
 
   size_t available ();
   string read (size_t size = 1);
@@ -81,8 +84,8 @@ public:
   void setTimeout (long timeout);
   long getTimeout () const;
 
-  void setBaudrate (int baudrate);
-  int getBaudrate () const;
+  void setBaudrate (unsigned long baudrate);
+  unsigned long getBaudrate () const;
 
   void setBytesize (bytesize_t bytesize);
   bytesize_t getBytesize () const;
@@ -100,24 +103,26 @@ protected:
   void reconfigurePort ();
 
 private:
-  int fd_; // The current file descriptor.
+  string port_;               // Path to the file descriptor
+  int fd_;                    // The current file descriptor.
 
-  bool isOpen_;
-  
   int interCharTimeout_;
   int writeTimeout_;
-  int xonxoff_;
-  int rtscts_;
+  bool isOpen_;
+  bool xonxoff_;
+  bool rtscts_;
+  
+  char ___; // lol padding
 
-  string port_;               // Path to the file descriptor
-  int baudrate_;              // Baudrate
   long timeout_;              // Timeout for read operations
-  bytesize_t bytesize_;       // Size of the bytes
+  unsigned long baudrate_;    // Baudrate
+  
   parity_t parity_;           // Parity
+  bytesize_t bytesize_;       // Size of the bytes  
   stopbits_t stopbits_;       // Stop Bits
   flowcontrol_t flowcontrol_; // Flow Control
 };
 
 }
 
-#endif // SERIAL_IMPL_UNIX_H
\ No newline at end of file
+#endif // SERIAL_IMPL_UNIX_H
diff --git a/include/serial/serial.h b/include/serial/serial.h
index 616aa49..eda593b 100644
--- a/include/serial/serial.h
+++ b/include/serial/serial.h
@@ -36,6 +36,7 @@
 #ifndef SERIAL_H
 #define SERIAL_H
 
+#include 
 #include 
 #include 
 #include 
@@ -80,6 +81,42 @@ typedef enum {
   FLOWCONTROL_HARDWARE
 } flowcontrol_t;
 
+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();
+	}
+};
+
+class IOException : public std::exception {
+    const char * e_what;
+public:
+    IOException(const char * description) {this->e_what = description;}
+  
+    virtual const char* what() const throw() {
+        std::stringstream ss;
+        ss << "IO Exception " << this->e_what << " failed.";
+        return ss.str().c_str();
+    }
+};
+
+class PortNotOpenedException : public std::exception {
+	const char * e_what;
+public:
+    PortNotOpenedException(const char * description) {this->e_what = description;}
+  
+    virtual const char* what() const throw() {
+		std::stringstream ss;
+		ss << e_what << " called before port was opened.";
+        return ss.str().c_str();
+    }
+};
+
+
 /*!
  * Class that provides a portable serial port interface.
  */
@@ -119,7 +156,7 @@ public:
   * \throw PortNotOpenedException
   */
   Serial (const std::string &port = "",
-          int baudrate = 9600,
+          unsigned long baudrate = 9600,
           long timeout = 0,
           bytesize_t bytesize = EIGHTBITS,
           parity_t parity = PARITY_NONE,
@@ -138,7 +175,9 @@ public:
   * 
   * \see Serial::Serial
   * 
-  * \throw PortNotOpenedException
+  * \throw std::invalid_argument
+  * \throw serial::SerialExecption
+  * \throw serial::IOException
   */
   void
   open ();
@@ -148,7 +187,7 @@ public:
   * \return Returns true if the port is open, false otherwise.
   */
   bool
-  isOpen ();
+  isOpen () const;
 
   /*! Closes the serial port. */
   void
@@ -283,7 +322,7 @@ public:
   * \throw InvalidConfigurationException
   */
   void
-  setBaudrate (int baudrate);
+  setBaudrate (unsigned long baudrate);
 
   /*! Gets the baudrate for the serial port.
   * 
@@ -293,7 +332,7 @@ public:
   * 
   * \throw InvalidConfigurationException
   */
-  int
+  unsigned long
   getBaudrate () const;
 
   /*! Sets the bytesize for the serial port.
@@ -397,42 +436,19 @@ private:
   SerialImpl *pimpl;
 };
 
-class IOException : public std::exception {
-    const char * e_what;
-public:
-    IOException(const char * e_what) {this->e_what = e_what;}
-    
-    virtual const char* what() const throw() {
-        std::stringstream ss;
-        ss << "Serial Port failed to open: " << this->e_what;
-        return ss.str().c_str();
-    }
-};
-
-class PortNotOpenedException : public std::exception {
-    const char * e_what;
-public:
-    PortNotOpenedException(const char * e_what) {this->e_what = e_what;}
-    
-    virtual const char* what() const throw() {
-        std::stringstream ss;
-        ss << "Serial Port failed to open: " << this->e_what;
-        return ss.str().c_str();
-    }
-};
-
-class InvalidConfigurationException : public std::exception {
-    int bytesize;
-public:
-    InvalidConfigurationException(int bytesize) {this->bytesize = bytesize;}
-    
-    virtual const char* what() const throw() {
-        std::stringstream ss;
-        ss << "Invalid configuration provided: " << this->bytesize;
-        return ss.str().c_str();
-    }
-};
+// why not use std::invalid_argument?
+// class InvalidConfigurationException : public std::exception {
+//     int bytesize;
+// public:
+//     InvalidConfigurationException(int bytesize) {this->bytesize = bytesize;}
+//     
+//     virtual const char* what() const throw() {
+//         std::stringstream ss;
+//         ss << "Invalid configuration provided: " << this->bytesize;
+//         return ss.str().c_str();
+//     }
+// };
 
 } // namespace serial
 
-#endif
\ No newline at end of file
+#endif
diff --git a/include/serial/serial_listener.h b/include/serial/serial_listener.h
index d8d4e0a..f0ce392 100644
--- a/include/serial/serial_listener.h
+++ b/include/serial/serial_listener.h
@@ -951,4 +951,4 @@ private:
 
 } // namespace serial
 
-#endif // SERIAL_LISTENER_H
\ No newline at end of file
+#endif // SERIAL_LISTENER_H
diff --git a/src/impl/unix.cc b/src/impl/unix.cc
index 6d09558..2fe2fe2 100644
--- a/src/impl/unix.cc
+++ b/src/impl/unix.cc
@@ -22,30 +22,21 @@
 #endif
 #endif
 
-class UnhandledException : public std::exception {
-    const char * e_what;
-public:
-    UnhandledException(const char * e_what) {this->e_what = e_what;}
-    
-    virtual const char* what() const throw() {
-        std::stringstream ss;
-        ss << "Unhandled Exception: " << this->e_what;
-        return ss.str().c_str();
-    }
-};
-
-typedef UnhandledException e;
-
-using ::serial::Serial;
 using std::string;
+using std::invalid_argument;
+using serial::Serial;
+using serial::SerialExecption;
+using serial::PortNotOpenedException;
+using serial::IOException;
 
-Serial::SerialImpl::SerialImpl (const string &port, int baudrate,
+Serial::SerialImpl::SerialImpl (const string &port, unsigned long baudrate,
                                 long timeout, bytesize_t bytesize,
                                 parity_t parity, stopbits_t stopbits,
                                 flowcontrol_t flowcontrol)
-: fd_(-1), isOpen_(false), interCharTimeout_(-1), port_(port),
-  baudrate_(baudrate), timeout_(timeout), bytesize_(bytesize),
-  parity_(parity), stopbits_(stopbits), flowcontrol_(flowcontrol)
+: port_(port), fd_(-1), interCharTimeout_(-1), writeTimeout_(-1),
+  isOpen_(false), xonxoff_(false), rtscts_(false), ___(0), timeout_(timeout),
+  baudrate_(baudrate), parity_(parity), bytesize_(bytesize),
+  stopbits_(stopbits), flowcontrol_(flowcontrol)
 {
   if (port_.empty() == false) this->open();
 }
@@ -57,20 +48,16 @@ Serial::SerialImpl::~SerialImpl () {
 void
 Serial::SerialImpl::open () {
   if (port_.empty()) {
-    printf("Port was empty\n");
-    throw e("error");
+    throw invalid_argument("bad port specified");
   }
   if (isOpen_ == true) {
-    printf("Port already opened\n");
-    throw e("error");
+    throw SerialExecption("port already open");
   }
   
   fd_ = ::open (port_.c_str(), O_RDWR | O_NOCTTY | O_NONBLOCK);
 
   if (fd_ == -1) {
-    printf("Error opening serial port %s - %s(%d).\n",
-           port_.c_str(), strerror(errno), errno);
-    throw e("error");
+    throw IOException("invalid file descriptor");
   }
 
   reconfigurePort();
@@ -80,7 +67,8 @@ Serial::SerialImpl::open () {
 void
 Serial::SerialImpl::reconfigurePort () {
   if (fd_ == -1) {
-    throw e("Error"); // Can only operate on a valid file descriptor
+    // Can only operate on a valid file descriptor
+    throw IOException("invalid file descriptor");
   }
 
   struct termios options; // The current options for the file descriptor
@@ -89,27 +77,27 @@ Serial::SerialImpl::reconfigurePort () {
   uint8_t vmin = 0, vtime = 0;                // timeout is done via select
   if (interCharTimeout_ == -1) {
     vmin = 1;
-    vtime = int(interCharTimeout_ * 10);
+    vtime = uint8_t(interCharTimeout_ * 10);
   }
   
   if (tcgetattr(fd_, &originalTTYAttrs) == -1) {
-    throw e("Error");
+    throw IOException("::tcgetattr");
   }
 
   options = originalTTYAttrs;
 
   // set up raw mode / no echo / binary
-  options.c_cflag |=  (CLOCAL|CREAD);
-  options.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL|
-                     ISIG|IEXTEN); //|ECHOPRT
+  options.c_cflag |= (unsigned long)(CLOCAL|CREAD);
+  options.c_lflag &= (unsigned long) ~(ICANON|ECHO|ECHOE|ECHOK
+	  								   |ECHONL|ISIG|IEXTEN); //|ECHOPRT
 
-  options.c_oflag &= ~(OPOST);
-  options.c_iflag &= ~(INLCR|IGNCR|ICRNL|IGNBRK);
+  options.c_oflag &= (unsigned long) ~(OPOST);
+  options.c_iflag &= (unsigned long) ~(INLCR|IGNCR|ICRNL|IGNBRK);
 #ifdef IUCLC
-  options.c_iflag &= ~IUCLC;
+  options.c_iflag &= (unsigned long) ~IUCLC;
 #endif
 #ifdef PARMRK
-  options.c_iflag &= ~PARMRK;
+  options.c_iflag &= (unsigned long) ~PARMRK;
 #endif
 
   // setup baud rate
@@ -117,7 +105,7 @@ Serial::SerialImpl::reconfigurePort () {
   cfsetspeed(&options, baudrate_);
 
   // setup char len
-  options.c_cflag &= ~CSIZE;
+  options.c_cflag &= (unsigned long) ~CSIZE;
   if (bytesize_ == EIGHTBITS)
       options.c_cflag |= CS8;
   else if (bytesize_ == SEVENBITS)
@@ -127,30 +115,30 @@ Serial::SerialImpl::reconfigurePort () {
   else if (bytesize_ == FIVEBITS)
       options.c_cflag |= CS5;
   else
-      throw e("ValueError(Invalid char len: %%r)");
+      throw invalid_argument("Invalid char len");
   // setup stopbits
   if (stopbits_ == STOPBITS_ONE)
-      options.c_cflag &= ~(CSTOPB);
+      options.c_cflag &= (unsigned long) ~(CSTOPB);
   else if (stopbits_ == STOPBITS_ONE_POINT_FIVE)
       options.c_cflag |=  (CSTOPB);  // XXX same as TWO.. there is no POSIX support for 1.5
   else if (stopbits_ == STOPBITS_TWO)
       options.c_cflag |=  (CSTOPB);
   else 
-      throw e("ValueError(Invalid stop bit specification:)");
+      throw invalid_argument("invalid stop bit");
   // setup parity
-  options.c_iflag &= ~(INPCK|ISTRIP);
+  options.c_iflag &= (unsigned long) ~(INPCK|ISTRIP);
   if (parity_ == PARITY_NONE) {
-    options.c_cflag &= ~(PARENB|PARODD);
+    options.c_cflag &= (unsigned long) ~(PARENB|PARODD);
   }
   else if (parity_ == PARITY_EVEN) {
-    options.c_cflag &= ~(PARODD);
+    options.c_cflag &= (unsigned long) ~(PARODD);
     options.c_cflag |=  (PARENB);
   } 
   else if (parity_ == PARITY_ODD) {
     options.c_cflag |=  (PARENB|PARODD);
   }
   else {
-    throw e("ValueError(Invalid parity:");
+    throw invalid_argument("invalid parity");
   }
   // setup flow control
   // xonxoff
@@ -158,24 +146,24 @@ Serial::SerialImpl::reconfigurePort () {
   if (xonxoff_)
     options.c_iflag |=  (IXON|IXOFF); //|IXANY)
   else
-    options.c_iflag &= ~(IXON|IXOFF|IXANY);
+    options.c_iflag &= (unsigned long) ~(IXON|IXOFF|IXANY);
 #else
   if (xonxoff_)
     options.c_iflag |=  (IXON|IXOFF);
   else
-    options.c_iflag &= ~(IXON|IXOFF);
+    options.c_iflag &= (unsigned long) ~(IXON|IXOFF);
 #endif
   // rtscts
 #ifdef CRTSCTS
   if (rtscts_)
     options.c_cflag |=  (CRTSCTS);
   else
-    options.c_cflag &= ~(CRTSCTS);
+    options.c_cflag &= (unsigned long) ~(CRTSCTS);
 #elif defined CNEW_RTSCTS
   if (rtscts_)
     options.c_cflag |=  (CNEW_RTSCTS);
   else
-    options.c_cflag &= ~(CNEW_RTSCTS);
+    options.c_cflag &= (unsigned long) ~(CNEW_RTSCTS);
 #else
 #error "OS Support seems wrong."
 #endif
@@ -194,7 +182,7 @@ void
 Serial::SerialImpl::close () {
   if (isOpen_ == true) {
     if (fd_ != -1) {
-      ::close(fd_);
+      ::close(fd_); // Ignoring the outcome
       fd_ = -1;
     }
     isOpen_ = false;
@@ -202,7 +190,7 @@ Serial::SerialImpl::close () {
 }
 
 bool
-Serial::SerialImpl::isOpen () {
+Serial::SerialImpl::isOpen () const {
   return isOpen_;
 }
 
@@ -214,17 +202,17 @@ Serial::SerialImpl::available () {
   int count = 0;
   int result = ioctl(fd_, TIOCINQ, &count);
   if (result == 0) {
-    return count;
+    return (size_t)count;
   }
   else {
-    throw e("Error");
+    throw IOException("ioctl");
   }
 }
 
 string
 Serial::SerialImpl::read (size_t size) {
   if (!isOpen_) {
-    throw e("PortNotOpenError()"); //
+    throw PortNotOpenedException("Serial::read");
   }
   string message = "";
   char buf[1024]; // TODO(ash_gti): Should this be 1024? or...?
@@ -233,8 +221,8 @@ Serial::SerialImpl::read (size_t size) {
     FD_ZERO(&readfds);
     FD_SET(fd_, &readfds);
     struct timeval timeout;
-    timeout.tv_sec = timeout_ / 1000;
-    timeout.tv_usec = timeout_ % 1000;
+    timeout.tv_sec =        timeout_ / 1000;
+    timeout.tv_usec = (int) timeout_ % 1000;
     int r = select(fd_ + 1, &readfds, NULL, NULL, &timeout);
 
     if (r == -1 && errno == EINTR)
@@ -247,16 +235,17 @@ Serial::SerialImpl::read (size_t size) {
 
     if (FD_ISSET(fd_, &readfds)) {
       memset(buf, 0, 1024);
-      size_t bytes_read = ::read(fd_, buf, size-strlen(buf));
+      ssize_t bytes_read = ::read(fd_, buf, size-strlen(buf));
       // read should always return some data as select reported it was
       // ready to read when we get to this point.
       if (bytes_read < 1) {
         // Disconnected devices, at least on Linux, show the
         // behavior that they are always ready to read immediately
         // but reading returns nothing.
-        throw e("SerialException('device reports readiness to read but returned no data (device disconnected?)')");
+        throw SerialExecption("device reports readiness to read but returned no "
+							  "data (device disconnected?)");
       }
-      message.append(buf, bytes_read);
+      message.append(buf, (size_t)bytes_read);
     }
     else {
       break; // Timeout
@@ -268,14 +257,13 @@ Serial::SerialImpl::read (size_t size) {
 size_t
 Serial::SerialImpl::write (const string &data) {
   if (isOpen_ == false) {
-    throw e("portNotOpenError");
+    throw PortNotOpenedException("Serial::write");
   }
-  size_t t = data.length();
-  size_t n = ::write(fd_, data.c_str(), data.length());
+  ssize_t n = ::write(fd_, data.c_str(), data.length());
   if (n == -1) {
-    throw e("Write error");
+    throw IOException("Write");
   }
-  return n;
+  return (size_t)n;
 }
 
 void
@@ -300,12 +288,12 @@ Serial::SerialImpl::getTimeout () const {
 }
 
 void
-Serial::SerialImpl::setBaudrate (int baudrate) {
+Serial::SerialImpl::setBaudrate (unsigned long baudrate) {
   baudrate_ = baudrate;
   if (isOpen_) reconfigurePort();
 }
 
-int
+unsigned long
 Serial::SerialImpl::getBaudrate () const {
   return baudrate_;
 }
@@ -357,32 +345,32 @@ Serial::SerialImpl::getFlowcontrol () const {
 
 void Serial::SerialImpl::flush () {
   if (isOpen_ == false) {
-    throw "portNotOpen";
+    throw PortNotOpenedException("Serial::flush");
   }
   tcdrain(fd_);
 }
 void Serial::SerialImpl::flushInput () {
   if (isOpen_ == false) {
-    throw "portNotOpen";
+    throw PortNotOpenedException("Serial::flushInput");
   }
   tcflush(fd_, TCIFLUSH);
 }
 void Serial::SerialImpl::flushOutput () {
   if (isOpen_ == false) {
-    throw "portNotOpen";
+    throw PortNotOpenedException("Serial::flushOutput");
   }
   tcflush(fd_, TCOFLUSH);
 }
 
 void Serial::SerialImpl::sendBreak(int duration) {
   if (isOpen_ == false) {
-    throw "portNotOpen";
+    throw PortNotOpenedException("Serial::sendBreak");
   }
   tcsendbreak(fd_, int(duration/4));
 }
 void Serial::SerialImpl::setBreak(bool level) {
   if (isOpen_ == false) {
-    throw "portNotOpen";
+    throw PortNotOpenedException("Serial::setBreak");
   }
   if (level) {
     ioctl(fd_, TIOCSBRK);
@@ -393,7 +381,7 @@ void Serial::SerialImpl::setBreak(bool level) {
 }
 void Serial::SerialImpl::setRTS(bool level) {
   if (isOpen_ == false) {
-    throw "portNotOpen";
+    throw PortNotOpenedException("Serial::setRTS");
   }
   if (level) {
     ioctl(fd_, TIOCMBIS, TIOCM_RTS);
@@ -404,7 +392,7 @@ void Serial::SerialImpl::setRTS(bool level) {
 }
 void Serial::SerialImpl::setDTR(bool level) {
   if (isOpen_ == false) {
-    throw "portNotOpen";
+    throw PortNotOpenedException("Serial::setDTR");
   }
   if (level) {
     ioctl(fd_, TIOCMBIS, TIOCM_DTR);
@@ -416,36 +404,29 @@ void Serial::SerialImpl::setDTR(bool level) {
 }
 bool Serial::SerialImpl::getCTS() {
   if (isOpen_ == false) {
-    throw "portNotOpen";
+    throw PortNotOpenedException("Serial::getCTS");
   }
   int s = ioctl(fd_, TIOCMGET, 0);
   return (s & TIOCM_CTS) != 0;
 }
 bool Serial::SerialImpl::getDSR() {
   if (isOpen_ == false) {
-    throw "portNotOpen";
+    throw PortNotOpenedException("Serial::getDSR");
   }
   int s = ioctl(fd_, TIOCMGET, 0);
   return (s & TIOCM_DSR) != 0;
 }
 bool Serial::SerialImpl::getRI() {
   if (isOpen_ == false) {
-    throw "portNotOpen";
+    throw PortNotOpenedException("Serial::getRI");
   }
   int s = ioctl(fd_, TIOCMGET, 0);
   return (s & TIOCM_RI) != 0;
 }
 bool Serial::SerialImpl::getCD() {
   if (isOpen_ == false) {
-    throw "portNotOpen";
+    throw PortNotOpenedException("Serial::getCD");
   }
   int s = ioctl(fd_, TIOCMGET, 0);
   return (s & TIOCM_CD) != 0;
 }
-
-
-
-
-
-
-
diff --git a/src/serial.cc b/src/serial.cc
index 5f06dab..1ff386a 100644
--- a/src/serial.cc
+++ b/src/serial.cc
@@ -6,17 +6,20 @@
 #include "serial/impl/unix.h"
 #endif
 
-using serial::Serial;
-using serial::bytesize_t;
-using serial::parity_t;
-using serial::stopbits_t;
-using serial::flowcontrol_t;
 using std::string;
 using std::vector;
 using std::numeric_limits;
 using std::size_t;
+using std::invalid_argument;
+using serial::Serial;
+using serial::SerialExecption;
+using serial::IOException;
+using serial::bytesize_t;
+using serial::parity_t;
+using serial::stopbits_t;
+using serial::flowcontrol_t;
 
-Serial::Serial (const string &port, int baudrate, long timeout,
+Serial::Serial (const string &port, unsigned long baudrate, long timeout,
                 bytesize_t bytesize, parity_t parity, stopbits_t stopbits,
                 flowcontrol_t flowcontrol)
 {
@@ -38,7 +41,7 @@ Serial::close () {
   this->pimpl->close ();
 }
 bool
-Serial::isOpen () {
+Serial::isOpen () const {
   return this->pimpl->isOpen ();
 }
 
@@ -128,11 +131,11 @@ Serial::getTimeout () const {
 }
 
 void
-Serial::setBaudrate (int baudrate) {
+Serial::setBaudrate (unsigned long baudrate) {
   this->pimpl->setBaudrate (baudrate);
 }
 
-int
+unsigned long
 Serial::getBaudrate () const {
   return this->pimpl->getBaudrate ();
 }
@@ -210,6 +213,3 @@ bool Serial::getRI() {
 bool Serial::getCD() {
   return this->pimpl->getCD();
 }
-
-
-

From 4cdb42987fbb0df8f49582fe5dfeb130e9abfa12 Mon Sep 17 00:00:00 2001
From: William Woodall 
Date: Fri, 13 Jan 2012 09:03:39 -0600
Subject: [PATCH 20/72] Removing vestigial files.

---
 include/serial/serial_old.h | 448 ----------------------------------
 src/serial_old.cc           | 468 ------------------------------------
 2 files changed, 916 deletions(-)
 delete mode 100644 include/serial/serial_old.h
 delete mode 100644 src/serial_old.cc

diff --git a/include/serial/serial_old.h b/include/serial/serial_old.h
deleted file mode 100644
index ed8215a..0000000
--- a/include/serial/serial_old.h
+++ /dev/null
@@ -1,448 +0,0 @@
-/**
- * @file serial.h
- * @author  William Woodall 
- * @author  John Harrison   
- * @version 0.1
- *
- * @section LICENSE
- *
- * The MIT License
- *
- * Copyright (c) 2011 William Woodall
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- *
- * @section DESCRIPTION
- *
- * This provides a cross platform interface for interacting with Serial Ports.
- */
-
-
-#ifndef SERIAL_H
-#define SERIAL_H
-
-#include 
-#include 
-#include 
-
-#include 
-#include 
-#include 
-#include 
-#include 
-
-// A macro to disallow the copy constructor and operator= functions
-// This should be used in the private: declarations for a class
-#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
-  TypeName(const TypeName&);               \
-  void operator=(const TypeName&)
-
-// If on Windows undefine the PARITY_* defines that are in winbase.h
-#ifdef _WIN32
-    #undef PARITY_NONE
-    #undef PARITY_ODD
-    #undef PARITY_EVEN
-#endif
-
-// DEFINES
-#ifndef DEFAULT_BAUDRATE
-#define DEFAULT_BAUDRATE 9600
-#endif
-#ifndef DEFAULT_TIMEOUT
-#define DEFAULT_TIMEOUT 0
-#endif
-#ifndef DEFAULT_BYTESIZE
-#define DEFAULT_BYTESIZE EIGHTBITS
-#endif
-#ifndef DEFAULT_PARITY
-#define DEFAULT_PARITY PARITY_NONE
-#endif
-#ifndef DEFAULT_STOPBITS
-#define DEFAULT_STOPBITS STOPBITS_ONE
-#endif 
-#ifndef DEFAULT_FLOWCONTROL
-#define DEFAULT_FLOWCONTROL FLOWCONTROL_NONE
-#endif
-
-namespace serial {
-
-// Serial Port settings CONSTANTS
-enum bytesize_t { FIVEBITS = 5, SIXBITS = 6, SEVENBITS = 7, EIGHTBITS = 8 };
-enum parity_t { PARITY_NONE, PARITY_ODD, PARITY_EVEN };
-enum stopbits_t { STOPBITS_ONE, STOPBITS_ONE_POINT_FIVE, STOPBITS_TWO };
-enum flowcontrol_t { FLOWCONTROL_NONE, FLOWCONTROL_SOFTWARE, FLOWCONTROL_HARDWARE };
-
-class Serial {
-public:
-    /** Constructor, Creates a Serial object but doesn't open the serial port. */
-    Serial();
-    
-    /**
-    * Constructor, creates a SerialPortBoost object and opens the port.
-    * 
-    * @param port A std::string containing the address of the serial port,
-    *        which would be something like 'COM1' on Windows and '/dev/ttyS0'
-    *        on Linux.
-    * 
-    * @param baudrate An integer that represents the buadrate
-    * 
-    * @param timeout A long that represents the time (in milliseconds) until a 
-    *        timeout on reads occur.  Setting this to zero (0) will cause reading
-    *        to be non-blocking, i.e. the available data will be returned immediately,
-    *        but it will not block to wait for more.  Setting this to a number less than
-    *        zero (-1) will result in infinite blocking behaviour, i.e. the serial port will
-    *        block until either size bytes have been read or an exception has occured.
-    * 
-    * @param bytesize Size of each byte in the serial transmission of data, 
-    *        default is EIGHTBITS, possible values are: FIVEBITS, 
-    *        SIXBITS, SEVENBITS, EIGHTBITS
-    * 
-    * @param parity Method of parity, default is PARITY_NONE, possible values
-    *        are: PARITY_NONE, PARITY_ODD, PARITY_EVEN
-    * 
-    * @param stopbits Number of stop bits used, default is STOPBITS_ONE, possible 
-    *        values are: STOPBITS_ONE, STOPBITS_ONE_POINT_FIVE, STOPBITS_TWO
-    * 
-    * @param flowcontrol Type of flowcontrol used, default is FLOWCONTROL_NONE, possible
-    *        values are: FLOWCONTROL_NONE, FLOWCONTROL_SOFTWARE, FLOWCONTROL_HARDWARE
-    * 
-    * @throw SerialPortAlreadyOpenException
-    * @throw SerialPortFailedToOpenException
-    */
-    Serial(std::string port,
-           int baudrate = DEFAULT_BAUDRATE,
-           long timeout = DEFAULT_TIMEOUT,
-           bytesize_t bytesize = DEFAULT_BYTESIZE,
-           parity_t parity = DEFAULT_PARITY,
-           stopbits_t stopbits = DEFAULT_STOPBITS,
-           flowcontrol_t flowcontrol = DEFAULT_FLOWCONTROL);
-    
-    /** Destructor */
-    ~Serial();
-    
-    /** 
-    * Opens the serial port as long as the portname is set and the port isn't alreay open.
-    * 
-    * @throw SerialPortAlreadyOpenException
-    * @throw SerialPortFailedToOpenException
-    */
-    void open();
-    
-    /** Gets the status of the serial port.
-    * 
-    * @return A boolean value that represents whether or not the serial port is open.
-    */
-    bool isOpen();
-    
-    /** Closes the serial port and terminates threads. */
-    void close();
-    
-    /** Read size bytes from the serial port.
-    * If a timeout is set it may return less characters than requested. With no timeout
-    * it will block until the requested number of bytes have been read.
-    * 
-    * @param buffer A char[] of length >= the size parameter to hold incoming data.
-    * 
-    * @param size An integer defining how many bytes to be read.
-    * 
-    * @return An integer representing the number of bytes read.
-    */
-    int read(char* buffer, int size = 1);
-    
-    /** Read size bytes from the serial port.
-    * If a timeout is set it may return less characters than requested. With no timeout
-    * it will block until the requested number of bytes have been read.
-    * 
-    * @param size An integer defining how many bytes to be read.
-    * 
-    * @return A std::string containing the data read.
-    */
-    std::string read(int size = 1);
-    
-    std::string read_until(char delim, size_t size = -1);
-    std::string read_until(std::string delim, size_t size = -1);
-    
-    /** Write length bytes from buffer to the serial port.
-    * 
-    * @param data A char[] with data to be written to the serial port.
-    * 
-    * @param length An integer representing the number of bytes to be written.
-    * 
-    * @return An integer representing the number of bytes written.
-    */
-    int write(char data[], int length);
-    
-    /** Write a string to the serial port.
-    * 
-    * @param data A std::string to be written to the serial port. (must be null terminated)
-    * 
-    * @return An integer representing the number of bytes written to the serial port.
-    */
-    int write(std::string data);
-    
-    /** Sets the logic level of the RTS line.
-    * 
-    * @param level The logic level to set the RTS to. Defaults to true.
-    */
-    void setRTS(bool level = true);
-    
-    /** Sets the logic level of the DTR line.
-    * 
-    * @param level The logic level to set the DTR to. Defaults to true.
-    */
-    void setDTR(bool level = true);
-    
-    /** Gets the status of the CTS line.
-    * 
-    * @return A boolean value that represents the current logic level of the CTS line.
-    */
-    bool getCTS() const;
-    
-    /** Gets the status of the DSR line.
-    * 
-    * @return A boolean value that represents the current logic level of the DSR line.
-    */
-    bool getDSR() const;
-    
-    /** Sets the serial port identifier.
-    * 
-    * @param port A std::string containing the address of the serial port,
-    *        which would be something like 'COM1' on Windows and '/dev/ttyS0'
-    *        on Linux.
-    */
-    void setPort(std::string port);
-    
-    /** Gets the serial port identifier.
-    * 
-    * @return A std::string containing the address of the serial port,
-    *         which would be something like 'COM1' on Windows and '/dev/ttyS0'
-    *         on Linux.
-    */
-    std::string getPort() const;
-    
-    /** Sets the timeout for reads in seconds.
-    * 
-    * @param timeout A long that represents the time (in milliseconds) until a 
-    *        timeout on reads occur.  Setting this to zero (0) will cause reading
-    *        to be non-blocking, i.e. the available data will be returned immediately,
-    *        but it will not block to wait for more.  Setting this to a number less than
-    *        zero (-1) will result in infinite blocking behaviour, i.e. the serial port will
-    *        block until either size bytes have been read or an exception has occured.
-    */
-    void setTimeoutMilliseconds(long timeout);
-    
-    /** Gets the timeout for reads in seconds.
-    * 
-    * @param timeout A long that represents the time (in milliseconds) until a 
-    *        timeout on reads occur.  Setting this to zero (0) will cause reading
-    *        to be non-blocking, i.e. the available data will be returned immediately,
-    *        but it will not block to wait for more.  Setting this to a number less than
-    *        zero (-1) will result in infinite blocking behaviour, i.e. the serial port will
-    *        block until either size bytes have been read or an exception has occured.
-    */
-    long getTimeoutMilliseconds() const;
-    
-    /** Sets the baudrate for the serial port.
-    * 
-    * @param baudrate An integer that sets the baud rate for the serial port.
-    */
-    void setBaudrate(int baudrate);
-    
-    /** Gets the baudrate for the serial port.
-    * 
-    * @return An integer that sets the baud rate for the serial port.
-    */
-    int getBaudrate() const;
-    
-    /** Sets the bytesize for the serial port.
-    * 
-    * @param bytesize Size of each byte in the serial transmission of data, 
-    *        default is EIGHTBITS, possible values are: FIVEBITS, 
-    *        SIXBITS, SEVENBITS, EIGHTBITS
-    * 
-    * @throw InvalidBytesizeException
-    */
-    void setBytesize(bytesize_t bytesize);
-    
-    /** Gets the bytesize for the serial port.
-    * 
-    * @return Size of each byte in the serial transmission of data, 
-    *         default is EIGHTBITS, possible values are: FIVEBITS, 
-    *         SIXBITS, SEVENBITS, EIGHTBITS
-    * 
-    * @throw InvalidBytesizeException
-    */
-    bytesize_t getBytesize() const;
-    
-    /** Sets the parity for the serial port.
-    * 
-    * @param parity Method of parity, default is PARITY_NONE, possible values
-    *        are: PARITY_NONE, PARITY_ODD, PARITY_EVEN
-    * 
-    * @throw InvalidParityException
-    */
-    void setParity(parity_t parity);
-    
-    /** Gets the parity for the serial port.
-    * 
-    * @return Method of parity, default is PARITY_NONE, possible values
-    *         are: PARITY_NONE, PARITY_ODD, PARITY_EVEN
-    * 
-    * @throw InvalidParityException
-    */
-    parity_t getParity() const;
-    
-    /** Sets the stopbits for the serial port.
-    * 
-    * @param stopbits Number of stop bits used, default is STOPBITS_ONE, possible 
-    *        values are: STOPBITS_ONE, STOPBITS_ONE_POINT_FIVE, STOPBITS_TWO
-    * 
-    * @throw InvalidStopbitsException
-    */
-    void setStopbits(stopbits_t stopbits);
-    
-    /** Gets the stopbits for the serial port.
-    * 
-    * @return Number of stop bits used, default is STOPBITS_ONE, possible 
-    *         values are: STOPBITS_ONE, STOPBITS_ONE_POINT_FIVE, STOPBITS_TWO
-    * 
-    * @throw InvalidStopbitsException
-    */
-    stopbits_t getStopbits() const;
-    
-    /** Sets the flow control for the serial port.
-    * 
-    * @param flowcontrol Type of flowcontrol used, default is FLOWCONTROL_NONE, possible
-    *        values are: FLOWCONTROL_NONE, FLOWCONTROL_SOFTWARE, FLOWCONTROL_HARDWARE
-    * 
-    * @throw InvalidFlowcontrolException
-    */
-    void setFlowcontrol(flowcontrol_t flowcontrol);
-    
-    /** Gets the flow control for the serial port.
-    * 
-    * @return Type of flowcontrol used, default is FLOWCONTROL_NONE, possible
-    *         values are: FLOWCONTROL_NONE, FLOWCONTROL_SOFTWARE, FLOWCONTROL_HARDWARE
-    * 
-    * @throw InvalidFlowcontrolException
-    */
-    flowcontrol_t getFlowcontrol() const;
-private:
-    DISALLOW_COPY_AND_ASSIGN(Serial);
-    void init();
-    void read_complete(const boost::system::error_code& error, std::size_t bytes_transferred);
-    void timeout_callback(const boost::system::error_code& error);
-    
-    boost::asio::io_service io_service;
-    
-    boost::asio::io_service::work work;
-    
-    boost::scoped_ptr serial_port;
-    
-    boost::asio::deadline_timer timeout_timer;
-    
-    std::string port;
-    boost::asio::serial_port_base::baud_rate baudrate;
-    boost::posix_time::time_duration timeout;
-    boost::asio::serial_port_base::character_size bytesize;
-    boost::asio::serial_port_base::parity parity;
-    boost::asio::serial_port_base::stop_bits stopbits;
-    boost::asio::serial_port_base::flow_control flowcontrol;
-    
-    int bytes_read;
-    int bytes_to_read;
-    bool reading;
-    bool nonblocking;
-};
-
-class SerialPortAlreadyOpenException : public std::exception {
-    const char * port;
-public:
-    SerialPortAlreadyOpenException(const char * port) {this->port = port;}
-    
-    virtual const char* what() const throw() {
-        std::stringstream ss;
-        ss << "Serial Port already open: " << this->port;
-        return ss.str().c_str();
-    }
-};
-
-class SerialPortFailedToOpenException : public std::exception {
-    const char * e_what;
-public:
-    SerialPortFailedToOpenException(const char * e_what) {this->e_what = e_what;}
-    
-    virtual const char* what() const throw() {
-        std::stringstream ss;
-        ss << "Serial Port failed to open: " << this->e_what;
-        return ss.str().c_str();
-    }
-};
-
-class InvalidBytesizeException : public std::exception {
-    int bytesize;
-public:
-    InvalidBytesizeException(int bytesize) {this->bytesize = bytesize;}
-    
-    virtual const char* what() const throw() {
-        std::stringstream ss;
-        ss << "Invalid bytesize provided: " << this->bytesize;
-        return ss.str().c_str();
-    }
-};
-
-class InvalidParityException : public std::exception {
-    int parity;
-public:
-    InvalidParityException(int parity) {this->parity = parity;}
-    
-    virtual const char* what() const throw() {
-        std::stringstream ss;
-        ss << "Invalid parity provided: " << this->parity;
-        return ss.str().c_str();
-    }
-};
-
-class InvalidStopbitsException : public std::exception {
-    int stopbits;
-public:
-    InvalidStopbitsException(int stopbits) {this->stopbits = stopbits;}
-    
-    virtual const char* what() const throw() {
-        std::stringstream ss;
-        ss << "Invalid stopbits provided: " << this->stopbits;
-        return ss.str().c_str();
-    }
-};
-
-class InvalidFlowcontrolException : public std::exception {
-    int flowcontrol;
-public:
-    InvalidFlowcontrolException(int flowcontrol) {this->flowcontrol = flowcontrol;}
-    
-    virtual const char* what() const throw() {
-        std::stringstream ss;
-        ss << "Invalid flowcontrol provided: " << this->flowcontrol;
-        return ss.str().c_str();
-    }
-};
-
-} // namespace serial
-
-#endif
\ No newline at end of file
diff --git a/src/serial_old.cc b/src/serial_old.cc
deleted file mode 100644
index c304ee3..0000000
--- a/src/serial_old.cc
+++ /dev/null
@@ -1,468 +0,0 @@
-#include "serial/serial.h"
-#include 
-
-using namespace serial;
-
-/** Completion Conditions **/
-
-class transfer_at_least_ignore_invalid_argument {
-public:
-    typedef bool result_type;
-    
-    explicit transfer_at_least_ignore_invalid_argument(std::size_t minimum) : minimum_(minimum) {}
-    
-    template 
-    bool operator()(const Error& err, std::size_t bytes_transferred) {
-        if(err) {// There is an Error
-            if(err == boost::asio::error::invalid_argument)
-                std::cout << "Invalid Argument Error" << std::endl;
-            if(err == boost::asio::error::operation_aborted) {
-                return 1;
-            }
-            if(err != boost::asio::error::invalid_argument) {// The Error is not invalid argument
-                return 1; // Stop reading
-            }
-        }
-        if(bytes_transferred >= minimum_) {// We have all the bytes we need
-            return 1; // Stop
-        } else {
-            return 0; // Continue
-        }
-    }
-    
-private:
-    std::size_t minimum_;
-};
-
-/** Classes for Handshaking control **/
-
-#if defined(BOOST_WINDOWS) || defined(__CYGWIN__)
-# define BOOST_ASIO_OPTION_STORAGE DCB
-#else
-# define BOOST_ASIO_OPTION_STORAGE termios
-#endif
-
-class DTRControl {
-public:
-    explicit DTRControl(bool enable = false) : m_enable(enable) {};
-    
-    boost::system::error_code store(BOOST_ASIO_OPTION_STORAGE& storage,
-                                    boost::system::error_code& ec) const
-    {
-        #if defined(BOOST_WINDOWS) || defined(__CYGWIN__)
-            if(m_enable)
-                storage.fDtrControl = DTR_CONTROL_ENABLE;
-            else
-                storage.fDtrControl = DTR_CONTROL_DISABLE;
-        #else
-            ec = boost::asio::error::operation_not_supported;
-            ec = boost::system::error_code();
-        #endif
-        return ec;
-    };
-    
-    boost::system::error_code load(const BOOST_ASIO_OPTION_STORAGE& storage,
-                                   boost::system::error_code& ec)
-    {
-        #if defined(BOOST_WINDOWS) || defined(__CYGWIN__)
-            if(storage.fDtrControl == DTR_CONTROL_ENABLE)
-                m_enable = true;
-            else
-                m_enable = true;
-        #else
-        #endif
-        return ec;
-    };
-private:
-    bool m_enable;
-};
-
-class RTSControl {
-public:
-    explicit RTSControl(bool enable = false) : m_enable(enable) {};
-    boost::system::error_code store(BOOST_ASIO_OPTION_STORAGE& storage,
-                                    boost::system::error_code& ec) const
-    {
-        #if defined(BOOST_WINDOWS) || defined(__CYGWIN__)
-            if(m_enable)
-                storage.fRtsControl = RTS_CONTROL_ENABLE;
-            else
-                storage.fRtsControl = RTS_CONTROL_DISABLE;
-        #else
-            ec = boost::asio::error::operation_not_supported;
-            ec = boost::system::error_code();
-        #endif
-        return ec;
-    };
-    
-    boost::system::error_code load(const BOOST_ASIO_OPTION_STORAGE& storage,
-                                   boost::system::error_code& ec)
-    {
-        #if defined(BOOST_WINDOWS) || defined(__CYGWIN__)
-            if(storage.fRtsControl == RTS_CONTROL_ENABLE)
-                m_enable = true;
-            else
-                m_enable = true;
-        #else
-        #endif
-        return ec;
-    };
-private:
-    bool m_enable;
-};
-
-/** Serial Class Implementation **/
-
-Serial::Serial() : io_service(), work(io_service), timeout_timer(io_service) {
-    this->init();
-}
-
-Serial::Serial(std::string port,
-               int baudrate,
-               long timeout,
-               bytesize_t bytesize,
-               parity_t parity,
-               stopbits_t stopbits,
-               flowcontrol_t flowcontrol)
-               : io_service(), work(io_service), timeout_timer(io_service)
-{
-    // Call default constructor to initialize variables
-    this->init();
-    
-    // Write provided settings
-    this->port = port;
-    this->setBaudrate(baudrate);
-    this->setTimeoutMilliseconds(timeout);
-    this->setBytesize(bytesize);
-    this->setParity(parity);
-    this->setStopbits(stopbits);
-    this->setFlowcontrol(flowcontrol);
-    
-    // Open the serial port
-    this->open();
-}
-
-void Serial::init() {
-    // Boost asio variables
-    this->serial_port.reset();
-    
-    // Serial Port settings
-    this->port = "";
-    this->setBaudrate(DEFAULT_BAUDRATE);
-    this->setTimeoutMilliseconds(DEFAULT_TIMEOUT);
-    
-    // Private variables
-    this->bytes_read = 0;
-    this->bytes_to_read = 0;
-    this->reading = false;
-    this->nonblocking = false;
-}
-
-Serial::~Serial() {
-    this->close();
-}
-
-void Serial::open() {
-    // Make sure the Serial port is not already open.
-    if(this->serial_port != NULL && this->serial_port->is_open()) {
-        throw(SerialPortAlreadyOpenException(this->port.c_str()));
-    }
-    
-    // Try to open the serial port
-    try {
-        this->serial_port.reset(
-              new boost::asio::serial_port(this->io_service, this->port));
-        
-        this->serial_port->set_option(this->baudrate);
-        this->serial_port->set_option(this->flowcontrol);
-        this->serial_port->set_option(this->parity);
-        this->serial_port->set_option(this->stopbits);
-        this->serial_port->set_option(this->bytesize);
-    } catch(std::exception &e) {
-        this->serial_port.reset();
-        throw(SerialPortFailedToOpenException(e.what()));
-    }
-}
-
-bool Serial::isOpen() {
-    if(this->serial_port != NULL)
-        return this->serial_port->is_open();
-    return false;
-}
-
-void Serial::close() {
-    // Cancel the current timeout timer and async reads
-    this->timeout_timer.cancel();
-    if(this->serial_port != NULL) {
-        this->serial_port->cancel();
-        this->serial_port->close();
-        this->serial_port.reset();
-    }
-}
-
-static const boost::posix_time::time_duration 
-timeout_zero_comparison(boost::posix_time::milliseconds(0));
-
-int Serial::read(char* buffer, int size) {
-    this->reading = true;
-    if(this->nonblocking) {// Do not wait for data
-        this->serial_port->async_read_some(boost::asio::buffer(buffer, size),
-                                boost::bind(&Serial::read_complete, this,
-                                boost::asio::placeholders::error,
-                                boost::asio::placeholders::bytes_transferred));
-    } else {               // Wait for data until size is read or timeout occurs
-        boost::asio::async_read(*this->serial_port, boost::asio::buffer(buffer, size), transfer_at_least_ignore_invalid_argument(size),
-                                boost::bind(&Serial::read_complete, this,
-                                boost::asio::placeholders::error,
-                                boost::asio::placeholders::bytes_transferred));
-    }
-    if(this->timeout > timeout_zero_comparison) { // Only set a timeout_timer if there is a valid timeout
-        this->timeout_timer.expires_from_now(this->timeout);
-        this->timeout_timer.async_wait(boost::bind(&Serial::timeout_callback, this,
-                                 boost::asio::placeholders::error));
-    } else if(this->nonblocking) {
-        this->timeout_timer.expires_from_now(boost::posix_time::milliseconds(1));
-        this->timeout_timer.async_wait(boost::bind(&Serial::timeout_callback, this,
-                                 boost::asio::placeholders::error));
-    }
-    
-    while(this->reading)
-        this->io_service.run_one();
-    
-    this->bytes_to_read = size;
-    
-    return this->bytes_read;
-}
-
-std::string Serial::read(int size) {
-    char *serial_buffer = new char[size];
-    int bytes_read_ = this->read(serial_buffer, size);
-    std::string return_str(serial_buffer, (std::size_t)bytes_read_);
-    delete[] serial_buffer;
-    return return_str;
-}
-
-std::string 
-Serial::read_until(char delim, size_t size) {
-    using namespace std;
-    string r = "";
-    
-    while (r.find(delim) == string::npos) {
-        string s = read(1);
-        if (s.length() > 0) 
-            r += s;
-    }
-    
-    return r;
-}
-
-std::string 
-Serial::read_until(std::string delim, size_t size) {
-    using namespace std;
-    string r = "";
-    
-    while (r.find(delim) == string::npos) {
-        string s = read(1);
-        if (s.length() > 0) 
-            r += s;
-    }
-    
-    return r;
-}
-
-void Serial::read_complete(const boost::system::error_code& error, std::size_t bytes_transferred) {
-    if(!error || error != boost::asio::error::operation_aborted) { // If there was no error OR the error wasn't operation aborted (canceled), Cancel the timer
-        this->timeout_timer.cancel();  // will cause timeout_callback to fire with an error
-    }
-    
-    this->bytes_read = bytes_transferred;
-    
-    this->reading = false;
-}
-
-void Serial::timeout_callback(const boost::system::error_code& error) {
-    if (!error) {
-        // The timeout wasn't canceled, so cancel the async read
-        this->serial_port->cancel();
-    }
-}
-
-int Serial::write(char data[], int length) {
-    return boost::asio::write(*this->serial_port, boost::asio::buffer(data, length), boost::asio::transfer_all());
-}
-
-int Serial::write(std::string data) {
-    char *cstr = new char[data.size()+1];
-    std::strcpy(cstr, data.c_str());
-    int bytes_wrote = this->write(cstr, data.length());
-    delete[] cstr;
-    return bytes_wrote;
-}
-
-void Serial::setRTS(bool level) {
-    this->serial_port->set_option(RTSControl(level));
-}
-
-void Serial::setDTR(bool level) {
-    this->serial_port->set_option(DTRControl(level));
-}
-
-bool Serial::getCTS() const {
-    throw(boost::asio::error::operation_not_supported);
-    return false;
-}
-
-bool Serial::getDSR() const {
-    throw(boost::asio::error::operation_not_supported);
-    return false;
-}
-
-void Serial::setPort(std::string port) {
-    this->port = port;
-}
-
-std::string Serial::getPort() const {
-    return this->port;
-}
-
-void Serial::setTimeoutMilliseconds(long timeout) {
-    // If timeout > 0 then read until size or timeout occurs
-    // If timeout == 0 then read nonblocking, return data available immediately up to size
-    // If timeout < 0 then read blocking, until size is read, period.
-    if(timeout > 0) {
-        this->timeout = boost::posix_time::time_duration(boost::posix_time::milliseconds(timeout));
-    } else {
-        this->timeout = boost::posix_time::time_duration(boost::posix_time::milliseconds(0));
-    }
-    
-    if(timeout == 0)
-        this->nonblocking = true;
-    else // Must be negative
-        this->nonblocking = false;
-}
-
-long Serial::getTimeoutMilliseconds() const {
-    return this->timeout.total_milliseconds();
-}
-
-void Serial::setBaudrate(int baudrate) {
-    this->baudrate = boost::asio::serial_port_base::baud_rate(baudrate);
-}
-
-int Serial::getBaudrate() const {
-    return this->baudrate.value();
-}
-
-void Serial::setBytesize(bytesize_t bytesize) {
-    switch(bytesize) {
-        case FIVEBITS:
-            this->bytesize = boost::asio::serial_port_base::character_size(5);
-            break;
-        case SIXBITS:
-            this->bytesize = boost::asio::serial_port_base::character_size(6);
-            break;
-        case SEVENBITS:
-            this->bytesize = boost::asio::serial_port_base::character_size(7);
-            break;
-        case EIGHTBITS:
-            this->bytesize = boost::asio::serial_port_base::character_size(8);
-            break;
-        default:
-            throw(InvalidBytesizeException(bytesize));
-            break;
-    }
-}
-
-bytesize_t Serial::getBytesize() const {
-    return bytesize_t(this->bytesize.value());
-}
-
-void Serial::setParity(parity_t parity) {
-    switch(parity) {
-        case PARITY_NONE:
-            this->parity = boost::asio::serial_port_base::parity(boost::asio::serial_port_base::parity::none);
-            break;
-        case PARITY_ODD:
-            this->parity = boost::asio::serial_port_base::parity(boost::asio::serial_port_base::parity::odd);
-            break;
-        case PARITY_EVEN:
-            this->parity = boost::asio::serial_port_base::parity(boost::asio::serial_port_base::parity::even);
-            break;
-        default:
-            throw(InvalidParityException(parity));
-            break;
-    }
-}
-
-parity_t Serial::getParity() const {
-    switch(this->parity.value()) {
-        case boost::asio::serial_port_base::parity::none:
-            return parity_t(PARITY_NONE);
-        case boost::asio::serial_port_base::parity::odd:
-            return parity_t(PARITY_ODD);
-        case boost::asio::serial_port_base::parity::even:
-            return parity_t(PARITY_EVEN);
-        default:
-            throw(InvalidParityException(this->parity.value()));
-    }
-}
-
-void Serial::setStopbits(stopbits_t stopbits) {
-    switch(stopbits) {
-        case STOPBITS_ONE:
-            this->stopbits = boost::asio::serial_port_base::stop_bits(boost::asio::serial_port_base::stop_bits::one);
-            break;
-        case STOPBITS_ONE_POINT_FIVE:
-            this->stopbits = boost::asio::serial_port_base::stop_bits(boost::asio::serial_port_base::stop_bits::onepointfive);
-            break;
-        case STOPBITS_TWO:
-            this->stopbits = boost::asio::serial_port_base::stop_bits(boost::asio::serial_port_base::stop_bits::two);
-            break;
-        default:
-            throw(InvalidStopbitsException(stopbits));
-            break;
-    }
-}
-
-stopbits_t Serial::getStopbits() const {
-    switch(this->stopbits.value()) {
-        case boost::asio::serial_port_base::stop_bits::one:
-            return stopbits_t(STOPBITS_ONE);
-        case boost::asio::serial_port_base::stop_bits::onepointfive:
-            return stopbits_t(STOPBITS_ONE_POINT_FIVE);
-        case boost::asio::serial_port_base::stop_bits::two:
-            return stopbits_t(STOPBITS_TWO);
-        default:
-            throw(InvalidStopbitsException(this->stopbits.value()));
-    }
-}
-
-void Serial::setFlowcontrol(flowcontrol_t flowcontrol) {
-    switch(flowcontrol) {
-        case FLOWCONTROL_NONE:
-            this->flowcontrol = boost::asio::serial_port_base::flow_control(boost::asio::serial_port_base::flow_control::none);
-            break;
-        case FLOWCONTROL_SOFTWARE:
-            this->flowcontrol = boost::asio::serial_port_base::flow_control(boost::asio::serial_port_base::flow_control::software);
-            break;
-        case FLOWCONTROL_HARDWARE:
-            this->flowcontrol = boost::asio::serial_port_base::flow_control(boost::asio::serial_port_base::flow_control::hardware);
-            break;
-        default:
-            throw(InvalidFlowcontrolException(flowcontrol));
-            break;
-    }
-}
-
-flowcontrol_t Serial::getFlowcontrol() const {
-    switch(this->flowcontrol.value()) {
-        case boost::asio::serial_port_base::flow_control::none:
-            return flowcontrol_t(FLOWCONTROL_NONE);
-        case boost::asio::serial_port_base::flow_control::software:
-            return flowcontrol_t(FLOWCONTROL_SOFTWARE);
-        case boost::asio::serial_port_base::flow_control::hardware:
-            return flowcontrol_t(FLOWCONTROL_HARDWARE);
-        default:
-            throw(InvalidFlowcontrolException(this->flowcontrol.value()));
-    }
-}

From 0046f3f61f83eaa848e7f8dae1427c6e07ad3dda Mon Sep 17 00:00:00 2001
From: William Woodall 
Date: Fri, 13 Jan 2012 09:08:09 -0600
Subject: [PATCH 21/72] Cleanup of code base

---
 include/serial/impl/unix.h       |  4 +--
 include/serial/serial.h          | 54 ++++++++++++--------------------
 include/serial/serial_listener.h |  8 ++++-
 src/impl/unix.cc                 | 21 ++++++++-----
 src/serial.cc                    | 17 +++++++---
 src/serial_listener.cc           |  7 +++--
 6 files changed, 61 insertions(+), 50 deletions(-)

diff --git a/include/serial/impl/unix.h b/include/serial/impl/unix.h
index ec8e246..0f1801a 100644
--- a/include/serial/impl/unix.h
+++ b/include/serial/impl/unix.h
@@ -104,7 +104,7 @@ protected:
 
 private:
   string port_;               // Path to the file descriptor
-  int fd_;                    // The current file descriptor.
+  int fd_;                    // The current file descriptor
 
   int interCharTimeout_;
   int writeTimeout_;
@@ -118,7 +118,7 @@ private:
   unsigned long baudrate_;    // Baudrate
   
   parity_t parity_;           // Parity
-  bytesize_t bytesize_;       // Size of the bytes  
+  bytesize_t bytesize_;       // Size of the bytes
   stopbits_t stopbits_;       // Stop Bits
   flowcontrol_t flowcontrol_; // Flow Control
 };
diff --git a/include/serial/serial.h b/include/serial/serial.h
index eda593b..d37fb96 100644
--- a/include/serial/serial.h
+++ b/include/serial/serial.h
@@ -82,21 +82,21 @@ typedef enum {
 } flowcontrol_t;
 
 class SerialExecption : public std::exception {
-	const char * e_what;
+  const char * e_what;
 public:
-	SerialExecption(const char *description) {e_what=description;};
-	virtual const char* what() const throw() {
+  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();
-	}
+  }
 };
 
 class IOException : public std::exception {
     const char * e_what;
 public:
-    IOException(const char * description) {this->e_what = description;}
-  
+    IOException(const char * description) {e_what = description;}
+
     virtual const char* what() const throw() {
         std::stringstream ss;
         ss << "IO Exception " << this->e_what << " failed.";
@@ -105,13 +105,13 @@ public:
 };
 
 class PortNotOpenedException : public std::exception {
-	const char * e_what;
+  const char * e_what;
 public:
-    PortNotOpenedException(const char * description) {this->e_what = description;}
-  
+    PortNotOpenedException(const char * description) {e_what = description;}
+
     virtual const char* what() const throw() {
-		std::stringstream ss;
-		ss << e_what << " called before port was opened.";
+    std::stringstream ss;
+    ss << e_what << " called before port was opened.";
         return ss.str().c_str();
     }
 };
@@ -193,8 +193,7 @@ public:
   void
   close ();
 
-  /* Return the number of characters in the buffer.
-  */
+  /*! Return the number of characters in the buffer. */
   size_t
   available();
 
@@ -295,8 +294,8 @@ public:
   * timeout on reads occur.  Setting this to zero (0) will cause reading to be
   * non-blocking, i.e. the available data will be returned immediately, but it
   * will not block to wait for more.  Setting this to a number less than
-  * zero (-1) will result in infinite blocking behaviour, i.e. the serial port 
-  * will block until either size bytes have been read or an exception has 
+  * zero (-1) will result in infinite blocking behaviour, i.e. the serial port
+  * will block until either size bytes have been read or an exception has
   * occured.
   */
   void
@@ -337,8 +336,8 @@ public:
 
   /*! Sets the bytesize for the serial port.
   * 
-  * \param bytesize Size of each byte in the serial transmission of data, 
-  * default is EIGHTBITS, possible values are: FIVEBITS, SIXBITS, SEVENBITS, 
+  * \param bytesize Size of each byte in the serial transmission of data,
+  * default is EIGHTBITS, possible values are: FIVEBITS, SIXBITS, SEVENBITS,
   * EIGHTBITS
   * 
   * \throw InvalidConfigurationException
@@ -357,7 +356,7 @@ public:
 
   /*! Sets the parity for the serial port.
   * 
-  * \param parity Method of parity, default is PARITY_NONE, possible values 
+  * \param parity Method of parity, default is PARITY_NONE, possible values
   * are: PARITY_NONE, PARITY_ODD, PARITY_EVEN
   * 
   * \throw InvalidConfigurationException
@@ -376,7 +375,7 @@ public:
 
   /*! Sets the stopbits for the serial port.
   * 
-  * \param stopbits Number of stop bits used, default is STOPBITS_ONE, 
+  * \param stopbits Number of stop bits used, default is STOPBITS_ONE,
   * possible values are: STOPBITS_ONE, STOPBITS_ONE_POINT_FIVE, STOPBITS_TWO
   * 
   * \throw InvalidConfigurationException
@@ -395,8 +394,8 @@ public:
 
   /*! Sets the flow control for the serial port.
   * 
-  * \param flowcontrol Type of flowcontrol used, default is FLOWCONTROL_NONE, 
-  * possible values are: FLOWCONTROL_NONE, FLOWCONTROL_SOFTWARE, 
+  * \param flowcontrol Type of flowcontrol used, default is FLOWCONTROL_NONE,
+  * possible values are: FLOWCONTROL_NONE, FLOWCONTROL_SOFTWARE,
   * FLOWCONTROL_HARDWARE
   * 
   * \throw InvalidConfigurationException
@@ -436,19 +435,6 @@ private:
   SerialImpl *pimpl;
 };
 
-// why not use std::invalid_argument?
-// class InvalidConfigurationException : public std::exception {
-//     int bytesize;
-// public:
-//     InvalidConfigurationException(int bytesize) {this->bytesize = bytesize;}
-//     
-//     virtual const char* what() const throw() {
-//         std::stringstream ss;
-//         ss << "Invalid configuration provided: " << this->bytesize;
-//         return ss.str().c_str();
-//     }
-// };
-
 } // namespace serial
 
 #endif
diff --git a/include/serial/serial_listener.h b/include/serial/serial_listener.h
index f0ce392..bc53244 100644
--- a/include/serial/serial_listener.h
+++ b/include/serial/serial_listener.h
@@ -731,6 +731,12 @@ private:
   // exact comparator function
   static bool
   _exactly (const std::string& token, std::string exact_str) {
+    std::cout << token << " == " << exact_str << ": ";
+    if (token == exact_str)
+      std::cout << "True";
+    else
+      std::cout << "False";
+    std::cout << std::endl;
     return token == exact_str;
   }
   // startswith comparator function
@@ -748,7 +754,7 @@ private:
   _contains (const std::string& token, std::string substr) {
     return token.find(substr) != std::string::npos;
   }
-  
+
   // Gets some data from the serial port
   void readSomeData (std::string&, size_t);
   // Runs the new tokens through the filters
diff --git a/src/impl/unix.cc b/src/impl/unix.cc
index 2fe2fe2..fd937fe 100644
--- a/src/impl/unix.cc
+++ b/src/impl/unix.cc
@@ -74,12 +74,12 @@ Serial::SerialImpl::reconfigurePort () {
   struct termios options; // The current options for the file descriptor
   struct termios originalTTYAttrs; // The orignal file descriptor options
 
-  uint8_t vmin = 0, vtime = 0;                // timeout is done via select
+  uint8_t vmin = 0, vtime = 0; // timeout is done via select
   if (interCharTimeout_ == -1) {
     vmin = 1;
     vtime = uint8_t(interCharTimeout_ * 10);
   }
-  
+
   if (tcgetattr(fd_, &originalTTYAttrs) == -1) {
     throw IOException("::tcgetattr");
   }
@@ -89,7 +89,7 @@ Serial::SerialImpl::reconfigurePort () {
   // set up raw mode / no echo / binary
   options.c_cflag |= (unsigned long)(CLOCAL|CREAD);
   options.c_lflag &= (unsigned long) ~(ICANON|ECHO|ECHOE|ECHOK
-	  								   |ECHONL|ISIG|IEXTEN); //|ECHOPRT
+                       |ECHONL|ISIG|IEXTEN); //|ECHOPRT
 
   options.c_oflag &= (unsigned long) ~(OPOST);
   options.c_iflag &= (unsigned long) ~(INLCR|IGNCR|ICRNL|IGNBRK);
@@ -242,8 +242,8 @@ Serial::SerialImpl::read (size_t size) {
         // Disconnected devices, at least on Linux, show the
         // behavior that they are always ready to read immediately
         // but reading returns nothing.
-        throw SerialExecption("device reports readiness to read but returned no "
-							  "data (device disconnected?)");
+        throw SerialExecption("device reports readiness to read but "
+                              "returned no data (device disconnected?)");
       }
       message.append(buf, (size_t)bytes_read);
     }
@@ -342,19 +342,20 @@ Serial::SerialImpl::getFlowcontrol () const {
   return flowcontrol_;
 }
 
-
 void Serial::SerialImpl::flush () {
   if (isOpen_ == false) {
     throw PortNotOpenedException("Serial::flush");
   }
   tcdrain(fd_);
 }
+
 void Serial::SerialImpl::flushInput () {
   if (isOpen_ == false) {
     throw PortNotOpenedException("Serial::flushInput");
   }
   tcflush(fd_, TCIFLUSH);
 }
+
 void Serial::SerialImpl::flushOutput () {
   if (isOpen_ == false) {
     throw PortNotOpenedException("Serial::flushOutput");
@@ -368,6 +369,7 @@ void Serial::SerialImpl::sendBreak(int duration) {
   }
   tcsendbreak(fd_, int(duration/4));
 }
+
 void Serial::SerialImpl::setBreak(bool level) {
   if (isOpen_ == false) {
     throw PortNotOpenedException("Serial::setBreak");
@@ -379,6 +381,7 @@ void Serial::SerialImpl::setBreak(bool level) {
     ioctl(fd_, TIOCCBRK);
   }
 }
+
 void Serial::SerialImpl::setRTS(bool level) {
   if (isOpen_ == false) {
     throw PortNotOpenedException("Serial::setRTS");
@@ -390,6 +393,7 @@ void Serial::SerialImpl::setRTS(bool level) {
     ioctl(fd_, TIOCMBIC, TIOCM_RTS);
   }
 }
+
 void Serial::SerialImpl::setDTR(bool level) {
   if (isOpen_ == false) {
     throw PortNotOpenedException("Serial::setDTR");
@@ -400,8 +404,8 @@ void Serial::SerialImpl::setDTR(bool level) {
   else {
     ioctl(fd_, TIOCMBIC, TIOCM_DTR);
   }
-
 }
+
 bool Serial::SerialImpl::getCTS() {
   if (isOpen_ == false) {
     throw PortNotOpenedException("Serial::getCTS");
@@ -409,6 +413,7 @@ bool Serial::SerialImpl::getCTS() {
   int s = ioctl(fd_, TIOCMGET, 0);
   return (s & TIOCM_CTS) != 0;
 }
+
 bool Serial::SerialImpl::getDSR() {
   if (isOpen_ == false) {
     throw PortNotOpenedException("Serial::getDSR");
@@ -416,6 +421,7 @@ bool Serial::SerialImpl::getDSR() {
   int s = ioctl(fd_, TIOCMGET, 0);
   return (s & TIOCM_DSR) != 0;
 }
+
 bool Serial::SerialImpl::getRI() {
   if (isOpen_ == false) {
     throw PortNotOpenedException("Serial::getRI");
@@ -423,6 +429,7 @@ bool Serial::SerialImpl::getRI() {
   int s = ioctl(fd_, TIOCMGET, 0);
   return (s & TIOCM_RI) != 0;
 }
+
 bool Serial::SerialImpl::getCD() {
   if (isOpen_ == false) {
     throw PortNotOpenedException("Serial::getCD");
diff --git a/src/serial.cc b/src/serial.cc
index 1ff386a..9a5def8 100644
--- a/src/serial.cc
+++ b/src/serial.cc
@@ -40,6 +40,7 @@ void
 Serial::close () {
   this->pimpl->close ();
 }
+
 bool
 Serial::isOpen () const {
   return this->pimpl->isOpen ();
@@ -63,9 +64,9 @@ Serial::readline(size_t size, string eol) {
     string c = pimpl->read(1);
     if (!c.empty()) {
       line.append(c);
-      if (line.length() > leneol && line.substr(line.length() - leneol, leneol) == eol) {
+      if (line.length() > leneol
+       && line.substr(line.length() - leneol, leneol) == eol)
         break;
-      }
       if (line.length() >= size) {
         break;
       }
@@ -75,7 +76,6 @@ Serial::readline(size_t size, string eol) {
       break;
     }
   }
-
   return line;
 }
 
@@ -98,7 +98,6 @@ Serial::readlines(string eol) {
       break;
     }
   }
-
   return lines;
 }
 
@@ -183,33 +182,43 @@ Serial::getFlowcontrol () const {
 void Serial::flush() {
   this->pimpl->flush();
 }
+
 void Serial::flushInput() {
   this->pimpl->flushInput();
 }
+
 void Serial::flushOutput() {
   this->pimpl->flushOutput();
 }
+
 void Serial::sendBreak(int duration) {
   this->pimpl->sendBreak(duration);
 }
+
 void Serial::setBreak(bool level) {
   this->pimpl->setBreak(level);
 }
+
 void Serial::setRTS(bool level) {
   this->pimpl->setRTS(level);
 }
+
 void Serial::setDTR(bool level) {
   this->pimpl->setDTR(level);
 }
+
 bool Serial::getCTS() {
   return this->pimpl->getCTS();
 }
+
 bool Serial::getDSR() {
   return this->pimpl->getDSR();
 }
+
 bool Serial::getRI() {
   return this->pimpl->getRI();
 }
+
 bool Serial::getCD() {
   return this->pimpl->getCD();
 }
diff --git a/src/serial_listener.cc b/src/serial_listener.cc
index b4a392e..6da8d76 100644
--- a/src/serial_listener.cc
+++ b/src/serial_listener.cc
@@ -135,7 +135,6 @@ SerialListener::readSomeData(std::string &temp, size_t this_many) {
     this->handle_exc(SerialListenerException("Serial port not open."));
   }
   temp = this->serial_port->read(this_many);
-  std::cout << "Read(" << temp.length() << "): " << temp << std::endl;
 }
 
 void
@@ -146,15 +145,19 @@ SerialListener::filterNewTokens (std::vector new_tokens) {
   for (it=filters.begin(); it!=filters.end(); it++) {
     this->filter((*it), new_tokens);
   } // for (it=filters.begin(); it!=filters.end(); it++)
+  // Put the last token back in the data buffer
+  this->data_buffer = (*new_tokens.back());
 }
 
-// 
 void
 SerialListener::filter (FilterPtr filter, std::vector &tokens)
 {
   // Iterate through the token uuids and run each against the filter
   std::vector::iterator it;
   for (it=tokens.begin(); it!=tokens.end(); it++) {
+    // The last element goes back into the data_buffer, don't filter it
+    if (it == tokens.end()-1)
+      continue;
     TokenPtr token = (*it);
     if (filter->comparator((*token)))
       callback_queue.push(std::make_pair(filter,token));

From a870d49b10468c413ad5db26e69677d35499f48c Mon Sep 17 00:00:00 2001
From: William Woodall 
Date: Fri, 13 Jan 2012 10:09:49 -0600
Subject: [PATCH 22/72] Fixed all of the warnings from serial_listener

---
 include/serial/serial_listener.h | 54 +++++++++++++++++---------------
 src/serial_listener.cc           | 23 +++++++-------
 2 files changed, 39 insertions(+), 38 deletions(-)

diff --git a/include/serial/serial_listener.h b/include/serial/serial_listener.h
index bc53244..37fb8ef 100644
--- a/include/serial/serial_listener.h
+++ b/include/serial/serial_listener.h
@@ -159,11 +159,11 @@ class Filter
 {
 public:
   Filter (ComparatorType comparator, DataCallback callback)
-  : comparator(comparator), callback(callback) {}
+  : comparator_(comparator), callback_(callback) {}
   virtual ~Filter () {}
 
-  ComparatorType comparator;
-  DataCallback callback;
+  ComparatorType comparator_;
+  DataCallback callback_;
 };
 
 /*!
@@ -206,14 +206,14 @@ typedef boost::shared_ptr BufferedFilterPtr;
  * \param e_what is a std::string that describes the cause of the error.
  */
 class SerialListenerException : public std::exception {
-  const std::string e_what;
+  const std::string e_what_;
 public:
-  SerialListenerException(const std::string e_what) : e_what(e_what) {}
+  SerialListenerException(const std::string e_what) : e_what_(e_what) {}
   ~SerialListenerException() throw() {std::exception::~exception();}
 
   virtual const char* what() const throw() {
     std::stringstream ss;
-    ss << "SerialListenerException: " << this->e_what;
+    ss << "SerialListenerException: " << this->e_what_;
     return ss.str().c_str();
   }
 };
@@ -250,7 +250,7 @@ public:
     return true;
   }
 
-  bool timed_wait_and_pop(Data& popped_value, size_t timeout) {
+  bool timed_wait_and_pop(Data& popped_value, long timeout) {
     using namespace boost::posix_time;
     bool result;
     boost::mutex::scoped_lock lock(the_mutex);
@@ -334,7 +334,7 @@ public:
    */
   void
   setChunkSize (size_t chunk_size) {
-    this->chunk_size = chunk_size;
+    this->chunk_size_ = chunk_size;
   }
 
 /***** Start and Stop Listening ******/
@@ -585,10 +585,11 @@ public:
   /*!
    * Sleeps for a given number of milliseconds.
    * 
-   * \param ms number of milliseconds to sleep.
+   * \param milliseconds number of milliseconds to sleep.
    */
   static void
-  sleep (size_t ms) {
+  sleep (long milliseconds) {
+    boost::int64_t ms(milliseconds);
     boost::this_thread::sleep(boost::posix_time::milliseconds(ms));
   }
 
@@ -787,10 +788,11 @@ private:
 
   // Persistent listening variables
   bool listening;
-  serial::Serial * serial_port;
+  char serial_port_padding[7];
+  serial::Serial * serial_port_;
   boost::thread listen_thread;
   std::string data_buffer;
-  size_t chunk_size;
+  size_t chunk_size_;
 
   // Callback related variables
   // filter id, token
@@ -821,13 +823,13 @@ class BlockingFilter
 {
 public:
   BlockingFilter (ComparatorType comparator, SerialListener &listener) {
-    this->listener = &listener;
+    this->listener_ = &listener;
     DataCallback cb = boost::bind(&BlockingFilter::callback, this, _1);
-    this->filter_ptr = this->listener->createFilter(comparator, cb);
+    this->filter_ptr = this->listener_->createFilter(comparator, cb);
   }
 
   virtual ~BlockingFilter () {
-    this->listener->removeFilter(filter_ptr);
+    this->listener_->removeFilter(filter_ptr);
     this->result = "";
     this->cond.notify_all();
   }
@@ -840,7 +842,7 @@ public:
    * 
    * \return std::string token that was matched or "" if none were matched.
    */
-  std::string wait(size_t ms) {
+ std::string wait(long ms) {
     this->result = "";
     boost::unique_lock lock(this->mutex);
     this->cond.timed_wait(lock, boost::posix_time::milliseconds(ms));
@@ -855,7 +857,7 @@ public:
   }
 
 private:
-  SerialListener * listener;
+  SerialListener * listener_;
   boost::condition_variable cond;
   boost::mutex mutex;
   std::string result;
@@ -883,15 +885,15 @@ class BufferedFilter
 public:
   BufferedFilter (ComparatorType comparator, size_t buffer_size, 
                   SerialListener &listener)
-  : buffer_size(buffer_size)
+  : buffer_size_(buffer_size)
   {
-    this->listener = &listener;
+    this->listener_ = &listener;
     DataCallback cb = boost::bind(&BufferedFilter::callback, this, _1);
-    this->filter_ptr = this->listener->createFilter(comparator, cb);
+    this->filter_ptr = this->listener_->createFilter(comparator, cb);
   }
 
   virtual ~BufferedFilter () {
-    this->listener->removeFilter(filter_ptr);
+    this->listener_->removeFilter(filter_ptr);
     this->queue.clear();
     this->result = "";
   }
@@ -907,7 +909,7 @@ public:
    * 
    * \return std::string token that was matched or "" if none were matched.
    */
-  std::string wait(size_t ms) {
+  std::string wait(long ms) {
     if (ms == 0)
       if (!this->queue.try_pop(this->result))
         this->result = "";
@@ -935,21 +937,21 @@ public:
    * Returns the capacity of the buffer.
    */
   size_t capacity() {
-    return buffer_size;
+    return buffer_size_;
   }
 
   FilterPtr filter_ptr;
 
   void callback(const std::string &token) {
     std::string throw_away;
-    if (this->queue.size() == buffer_size)
+    if (this->queue.size() == this->buffer_size_)
       this->queue.wait_and_pop(throw_away);
     this->queue.push(token);
   }
 
 private:
-  size_t buffer_size;
-  SerialListener * listener;
+  size_t buffer_size_;
+  SerialListener * listener_;
   ConcurrentQueue queue;
   std::string result;
 
diff --git a/src/serial_listener.cc b/src/serial_listener.cc
index 6da8d76..85055db 100644
--- a/src/serial_listener.cc
+++ b/src/serial_listener.cc
@@ -17,11 +17,10 @@ inline void defaultInfoCallback(const std::string& msg) {
 inline void defaultExceptionCallback(const std::exception &error) {
   std::cerr << "SerialListener Unhandled Exception: " << error.what();
   std::cerr << std::endl;
-  throw(error);
 }
 
 inline bool defaultComparator(const std::string &token) {
-  return true;
+  return token == token;
 }
 
 using namespace serial;
@@ -34,7 +33,7 @@ SerialListener::default_handler(const std::string &token) {
     this->_default_handler(token);
 }
 
-SerialListener::SerialListener() : listening(false), chunk_size(5) {
+SerialListener::SerialListener() : listening(false), chunk_size_(5) {
   // Set default callbacks
   this->handle_exc = defaultExceptionCallback;
   this->info = defaultInfoCallback;
@@ -68,7 +67,7 @@ SerialListener::callback() {
         std::cout << (*pair.second) << std::endl;
         if (this->listening) {
           try {
-            pair.first->callback((*pair.second));
+            pair.first->callback_((*pair.second));
           } catch (std::exception &e) {
             this->handle_exc(e);
           }// try callback
@@ -88,8 +87,8 @@ SerialListener::startListening(Serial &serial_port) {
   }
   this->listening = true;
   
-  this->serial_port = &serial_port;
-  if (!this->serial_port->isOpen()) {
+  this->serial_port_ = &serial_port;
+  if (!this->serial_port_->isOpen()) {
     throw(SerialListenerException("Serial port not open."));
     return;
   }
@@ -110,7 +109,7 @@ SerialListener::stopListening() {
   callback_thread.join();
 
   this->data_buffer = "";
-  this->serial_port = NULL;
+  this->serial_port_ = NULL;
 
   // Delete all the filters
   this->removeAllFilters();
@@ -121,20 +120,20 @@ SerialListener::determineAmountToRead() {
   // TODO: Make a more intelligent method based on the length of the things 
   //  filters are looking for.  e.g.: if the filter is looking for 'V=XX\r' 
   //  make the read amount at least 5.
-  return this->chunk_size;
+  return this->chunk_size_;
 }
 
 void
 SerialListener::readSomeData(std::string &temp, size_t this_many) {
   // Make sure there is a serial port
-  if (this->serial_port == NULL) {
+  if (this->serial_port_ == NULL) {
     this->handle_exc(SerialListenerException("Invalid serial port."));
   }
   // Make sure the serial port is open
-  if (!this->serial_port->isOpen()) {
+  if (!this->serial_port_->isOpen()) {
     this->handle_exc(SerialListenerException("Serial port not open."));
   }
-  temp = this->serial_port->read(this_many);
+  temp = this->serial_port_->read(this_many);
 }
 
 void
@@ -159,7 +158,7 @@ SerialListener::filter (FilterPtr filter, std::vector &tokens)
     if (it == tokens.end()-1)
       continue;
     TokenPtr token = (*it);
-    if (filter->comparator((*token)))
+    if (filter->comparator_((*token)))
       callback_queue.push(std::make_pair(filter,token));
   }
 }

From 6011f2847c2c7d0e23c0967de2bdef157144afbb Mon Sep 17 00:00:00 2001
From: William Woodall 
Date: Fri, 13 Jan 2012 10:32:25 -0600
Subject: [PATCH 23/72] Adding doxygen support.

---
 doc/Doxyfile    | 1716 +++++++++++++++++++++++++++++++++++++++++++++++
 serial.makefile |    7 +
 2 files changed, 1723 insertions(+)
 create mode 100644 doc/Doxyfile

diff --git a/doc/Doxyfile b/doc/Doxyfile
new file mode 100644
index 0000000..f64a659
--- /dev/null
+++ b/doc/Doxyfile
@@ -0,0 +1,1716 @@
+# Doxyfile 1.7.4
+
+# This file describes the settings to be used by the documentation system
+# doxygen (www.doxygen.org) for a project.
+#
+# All text after a hash (#) is considered a comment and will be ignored.
+# The format is:
+#       TAG = value [value, ...]
+# For lists items can also be appended using:
+#       TAG += value [value, ...]
+# Values that contain spaces should be placed between quotes (" ").
+
+#---------------------------------------------------------------------------
+# Project related configuration options
+#---------------------------------------------------------------------------
+
+# This tag specifies the encoding used for all characters in the config file
+# that follow. The default is UTF-8 which is also the encoding used for all
+# text before the first occurrence of this tag. Doxygen uses libiconv (or the
+# iconv built into libc) for the transcoding. See
+# http://www.gnu.org/software/libiconv for the list of possible encodings.
+
+DOXYFILE_ENCODING      = UTF-8
+
+# The PROJECT_NAME tag is a single word (or a sequence of words surrounded
+# by quotes) that should identify the project.
+
+PROJECT_NAME           = serial
+
+# The PROJECT_NUMBER tag can be used to enter a project or revision number.
+# This could be handy for archiving the generated documentation or
+# if some version control system is used.
+
+PROJECT_NUMBER         = 1.0
+
+# Using the PROJECT_BRIEF tag one can provide an optional one line description
+# for a project that appears at the top of each page and should give viewer
+# a quick idea about the purpose of the project. Keep the description short.
+
+PROJECT_BRIEF          = Cross-platform serial port library for C++
+
+# With the PROJECT_LOGO tag one can specify an logo or icon that is
+# included in the documentation. The maximum height of the logo should not
+# exceed 55 pixels and the maximum width should not exceed 200 pixels.
+# Doxygen will copy the logo to the output directory.
+
+PROJECT_LOGO           =
+
+# The OUTPUT_DIRECTORY tag is used to specify the (relative or absolute)
+# base path where the generated documentation will be put.
+# If a relative path is entered, it will be relative to the location
+# where doxygen was started. If left blank the current directory will be used.
+
+OUTPUT_DIRECTORY       = doc
+
+# If the CREATE_SUBDIRS tag is set to YES, then doxygen will create
+# 4096 sub-directories (in 2 levels) under the output directory of each output
+# format and will distribute the generated files over these directories.
+# Enabling this option can be useful when feeding doxygen a huge amount of
+# source files, where putting all generated files in the same directory would
+# otherwise cause performance problems for the file system.
+
+CREATE_SUBDIRS         = NO
+
+# The OUTPUT_LANGUAGE tag is used to specify the language in which all
+# documentation generated by doxygen is written. Doxygen will use this
+# information to generate all constant output in the proper language.
+# The default language is English, other supported languages are:
+# Afrikaans, Arabic, Brazilian, Catalan, Chinese, Chinese-Traditional,
+# Croatian, Czech, Danish, Dutch, Esperanto, Farsi, Finnish, French, German,
+# Greek, Hungarian, Italian, Japanese, Japanese-en (Japanese with English
+# messages), Korean, Korean-en, Lithuanian, Norwegian, Macedonian, Persian,
+# Polish, Portuguese, Romanian, Russian, Serbian, Serbian-Cyrillic, Slovak,
+# Slovene, Spanish, Swedish, Ukrainian, and Vietnamese.
+
+OUTPUT_LANGUAGE        = English
+
+# If the BRIEF_MEMBER_DESC tag is set to YES (the default) Doxygen will
+# include brief member descriptions after the members that are listed in
+# the file and class documentation (similar to JavaDoc).
+# Set to NO to disable this.
+
+BRIEF_MEMBER_DESC      = YES
+
+# If the REPEAT_BRIEF tag is set to YES (the default) Doxygen will prepend
+# the brief description of a member or function before the detailed description.
+# Note: if both HIDE_UNDOC_MEMBERS and BRIEF_MEMBER_DESC are set to NO, the
+# brief descriptions will be completely suppressed.
+
+REPEAT_BRIEF           = YES
+
+# This tag implements a quasi-intelligent brief description abbreviator
+# that is used to form the text in various listings. Each string
+# in this list, if found as the leading text of the brief description, will be
+# stripped from the text and the result after processing the whole list, is
+# used as the annotated text. Otherwise, the brief description is used as-is.
+# If left blank, the following values are used ("$name" is automatically
+# replaced with the name of the entity): "The $name class" "The $name widget"
+# "The $name file" "is" "provides" "specifies" "contains"
+# "represents" "a" "an" "the"
+
+ABBREVIATE_BRIEF       =
+
+# If the ALWAYS_DETAILED_SEC and REPEAT_BRIEF tags are both set to YES then
+# Doxygen will generate a detailed section even if there is only a brief
+# description.
+
+ALWAYS_DETAILED_SEC    = NO
+
+# If the INLINE_INHERITED_MEMB tag is set to YES, doxygen will show all
+# inherited members of a class in the documentation of that class as if those
+# members were ordinary class members. Constructors, destructors and assignment
+# operators of the base classes will not be shown.
+
+INLINE_INHERITED_MEMB  = NO
+
+# If the FULL_PATH_NAMES tag is set to YES then Doxygen will prepend the full
+# path before files name in the file list and in the header files. If set
+# to NO the shortest path that makes the file name unique will be used.
+
+FULL_PATH_NAMES        = YES
+
+# If the FULL_PATH_NAMES tag is set to YES then the STRIP_FROM_PATH tag
+# can be used to strip a user-defined part of the path. Stripping is
+# only done if one of the specified strings matches the left-hand part of
+# the path. The tag can be used to show relative paths in the file list.
+# If left blank the directory from which doxygen is run is used as the
+# path to strip.
+
+STRIP_FROM_PATH        =
+
+# The STRIP_FROM_INC_PATH tag can be used to strip a user-defined part of
+# the path mentioned in the documentation of a class, which tells
+# the reader which header file to include in order to use a class.
+# If left blank only the name of the header file containing the class
+# definition is used. Otherwise one should specify the include paths that
+# are normally passed to the compiler using the -I flag.
+
+STRIP_FROM_INC_PATH    =
+
+# If the SHORT_NAMES tag is set to YES, doxygen will generate much shorter
+# (but less readable) file names. This can be useful if your file system
+# doesn't support long names like on DOS, Mac, or CD-ROM.
+
+SHORT_NAMES            = NO
+
+# If the JAVADOC_AUTOBRIEF tag is set to YES then Doxygen
+# will interpret the first line (until the first dot) of a JavaDoc-style
+# comment as the brief description. If set to NO, the JavaDoc
+# comments will behave just like regular Qt-style comments
+# (thus requiring an explicit @brief command for a brief description.)
+
+JAVADOC_AUTOBRIEF      = NO
+
+# If the QT_AUTOBRIEF tag is set to YES then Doxygen will
+# interpret the first line (until the first dot) of a Qt-style
+# comment as the brief description. If set to NO, the comments
+# will behave just like regular Qt-style comments (thus requiring
+# an explicit \brief command for a brief description.)
+
+QT_AUTOBRIEF           = NO
+
+# The MULTILINE_CPP_IS_BRIEF tag can be set to YES to make Doxygen
+# treat a multi-line C++ special comment block (i.e. a block of //! or ///
+# comments) as a brief description. This used to be the default behaviour.
+# The new default is to treat a multi-line C++ comment block as a detailed
+# description. Set this tag to YES if you prefer the old behaviour instead.
+
+MULTILINE_CPP_IS_BRIEF = NO
+
+# If the INHERIT_DOCS tag is set to YES (the default) then an undocumented
+# member inherits the documentation from any documented member that it
+# re-implements.
+
+INHERIT_DOCS           = YES
+
+# If the SEPARATE_MEMBER_PAGES tag is set to YES, then doxygen will produce
+# a new page for each member. If set to NO, the documentation of a member will
+# be part of the file/class/namespace that contains it.
+
+SEPARATE_MEMBER_PAGES  = NO
+
+# The TAB_SIZE tag can be used to set the number of spaces in a tab.
+# Doxygen uses this value to replace tabs by spaces in code fragments.
+
+TAB_SIZE               = 4
+
+# This tag can be used to specify a number of aliases that acts
+# as commands in the documentation. An alias has the form "name=value".
+# For example adding "sideeffect=\par Side Effects:\n" will allow you to
+# put the command \sideeffect (or @sideeffect) in the documentation, which
+# will result in a user-defined paragraph with heading "Side Effects:".
+# You can put \n's in the value part of an alias to insert newlines.
+
+ALIASES                =
+
+# Set the OPTIMIZE_OUTPUT_FOR_C tag to YES if your project consists of C
+# sources only. Doxygen will then generate output that is more tailored for C.
+# For instance, some of the names that are used will be different. The list
+# of all members will be omitted, etc.
+
+OPTIMIZE_OUTPUT_FOR_C  = YES
+
+# Set the OPTIMIZE_OUTPUT_JAVA tag to YES if your project consists of Java
+# sources only. Doxygen will then generate output that is more tailored for
+# Java. For instance, namespaces will be presented as packages, qualified
+# scopes will look different, etc.
+
+OPTIMIZE_OUTPUT_JAVA   = NO
+
+# Set the OPTIMIZE_FOR_FORTRAN tag to YES if your project consists of Fortran
+# sources only. Doxygen will then generate output that is more tailored for
+# Fortran.
+
+OPTIMIZE_FOR_FORTRAN   = NO
+
+# Set the OPTIMIZE_OUTPUT_VHDL tag to YES if your project consists of VHDL
+# sources. Doxygen will then generate output that is tailored for
+# VHDL.
+
+OPTIMIZE_OUTPUT_VHDL   = NO
+
+# Doxygen selects the parser to use depending on the extension of the files it
+# parses. With this tag you can assign which parser to use for a given extension.
+# Doxygen has a built-in mapping, but you can override or extend it using this
+# tag. The format is ext=language, where ext is a file extension, and language
+# is one of the parsers supported by doxygen: IDL, Java, Javascript, CSharp, C,
+# C++, D, PHP, Objective-C, Python, Fortran, VHDL, C, C++. For instance to make
+# doxygen treat .inc files as Fortran files (default is PHP), and .f files as C
+# (default is Fortran), use: inc=Fortran f=C. Note that for custom extensions
+# you also need to set FILE_PATTERNS otherwise the files are not read by doxygen.
+
+EXTENSION_MAPPING      =
+
+# If you use STL classes (i.e. std::string, std::vector, etc.) but do not want
+# to include (a tag file for) the STL sources as input, then you should
+# set this tag to YES in order to let doxygen match functions declarations and
+# definitions whose arguments contain STL classes (e.g. func(std::string); v.s.
+# func(std::string) {}). This also makes the inheritance and collaboration
+# diagrams that involve STL classes more complete and accurate.
+
+BUILTIN_STL_SUPPORT    = NO
+
+# If you use Microsoft's C++/CLI language, you should set this option to YES to
+# enable parsing support.
+
+CPP_CLI_SUPPORT        = NO
+
+# Set the SIP_SUPPORT tag to YES if your project consists of sip sources only.
+# Doxygen will parse them like normal C++ but will assume all classes use public
+# instead of private inheritance when no explicit protection keyword is present.
+
+SIP_SUPPORT            = NO
+
+# For Microsoft's IDL there are propget and propput attributes to indicate getter
+# and setter methods for a property. Setting this option to YES (the default)
+# will make doxygen replace the get and set methods by a property in the
+# documentation. This will only work if the methods are indeed getting or
+# setting a simple type. If this is not the case, or you want to show the
+# methods anyway, you should set this option to NO.
+
+IDL_PROPERTY_SUPPORT   = YES
+
+# If member grouping is used in the documentation and the DISTRIBUTE_GROUP_DOC
+# tag is set to YES, then doxygen will reuse the documentation of the first
+# member in the group (if any) for the other members of the group. By default
+# all members of a group must be documented explicitly.
+
+DISTRIBUTE_GROUP_DOC   = NO
+
+# Set the SUBGROUPING tag to YES (the default) to allow class member groups of
+# the same type (for instance a group of public functions) to be put as a
+# subgroup of that type (e.g. under the Public Functions section). Set it to
+# NO to prevent subgrouping. Alternatively, this can be done per class using
+# the \nosubgrouping command.
+
+SUBGROUPING            = YES
+
+# When the INLINE_GROUPED_CLASSES tag is set to YES, classes, structs and
+# unions are shown inside the group in which they are included (e.g. using
+# @ingroup) instead of on a separate page (for HTML and Man pages) or
+# section (for LaTeX and RTF).
+
+INLINE_GROUPED_CLASSES = NO
+
+# When TYPEDEF_HIDES_STRUCT is enabled, a typedef of a struct, union, or enum
+# is documented as struct, union, or enum with the name of the typedef. So
+# typedef struct TypeS {} TypeT, will appear in the documentation as a struct
+# with name TypeT. When disabled the typedef will appear as a member of a file,
+# namespace, or class. And the struct will be named TypeS. This can typically
+# be useful for C code in case the coding convention dictates that all compound
+# types are typedef'ed and only the typedef is referenced, never the tag name.
+
+TYPEDEF_HIDES_STRUCT   = NO
+
+# The SYMBOL_CACHE_SIZE determines the size of the internal cache use to
+# determine which symbols to keep in memory and which to flush to disk.
+# When the cache is full, less often used symbols will be written to disk.
+# For small to medium size projects (<1000 input files) the default value is
+# probably good enough. For larger projects a too small cache size can cause
+# doxygen to be busy swapping symbols to and from disk most of the time
+# causing a significant performance penalty.
+# If the system has enough physical memory increasing the cache will improve the
+# performance by keeping more symbols in memory. Note that the value works on
+# a logarithmic scale so increasing the size by one will roughly double the
+# memory usage. The cache size is given by this formula:
+# 2^(16+SYMBOL_CACHE_SIZE). The valid range is 0..9, the default is 0,
+# corresponding to a cache size of 2^16 = 65536 symbols
+
+SYMBOL_CACHE_SIZE      = 0
+
+#---------------------------------------------------------------------------
+# Build related configuration options
+#---------------------------------------------------------------------------
+
+# If the EXTRACT_ALL tag is set to YES doxygen will assume all entities in
+# documentation are documented, even if no documentation was available.
+# Private class members and static file members will be hidden unless
+# the EXTRACT_PRIVATE and EXTRACT_STATIC tags are set to YES
+
+EXTRACT_ALL            = YES
+
+# If the EXTRACT_PRIVATE tag is set to YES all private members of a class
+# will be included in the documentation.
+
+EXTRACT_PRIVATE        = NO
+
+# If the EXTRACT_STATIC tag is set to YES all static members of a file
+# will be included in the documentation.
+
+EXTRACT_STATIC         = NO
+
+# If the EXTRACT_LOCAL_CLASSES tag is set to YES classes (and structs)
+# defined locally in source files will be included in the documentation.
+# If set to NO only classes defined in header files are included.
+
+EXTRACT_LOCAL_CLASSES  = YES
+
+# This flag is only useful for Objective-C code. When set to YES local
+# methods, which are defined in the implementation section but not in
+# the interface are included in the documentation.
+# If set to NO (the default) only methods in the interface are included.
+
+EXTRACT_LOCAL_METHODS  = NO
+
+# If this flag is set to YES, the members of anonymous namespaces will be
+# extracted and appear in the documentation as a namespace called
+# 'anonymous_namespace{file}', where file will be replaced with the base
+# name of the file that contains the anonymous namespace. By default
+# anonymous namespaces are hidden.
+
+EXTRACT_ANON_NSPACES   = NO
+
+# If the HIDE_UNDOC_MEMBERS tag is set to YES, Doxygen will hide all
+# undocumented members of documented classes, files or namespaces.
+# If set to NO (the default) these members will be included in the
+# various overviews, but no documentation section is generated.
+# This option has no effect if EXTRACT_ALL is enabled.
+
+HIDE_UNDOC_MEMBERS     = NO
+
+# If the HIDE_UNDOC_CLASSES tag is set to YES, Doxygen will hide all
+# undocumented classes that are normally visible in the class hierarchy.
+# If set to NO (the default) these classes will be included in the various
+# overviews. This option has no effect if EXTRACT_ALL is enabled.
+
+HIDE_UNDOC_CLASSES     = NO
+
+# If the HIDE_FRIEND_COMPOUNDS tag is set to YES, Doxygen will hide all
+# friend (class|struct|union) declarations.
+# If set to NO (the default) these declarations will be included in the
+# documentation.
+
+HIDE_FRIEND_COMPOUNDS  = NO
+
+# If the HIDE_IN_BODY_DOCS tag is set to YES, Doxygen will hide any
+# documentation blocks found inside the body of a function.
+# If set to NO (the default) these blocks will be appended to the
+# function's detailed documentation block.
+
+HIDE_IN_BODY_DOCS      = NO
+
+# The INTERNAL_DOCS tag determines if documentation
+# that is typed after a \internal command is included. If the tag is set
+# to NO (the default) then the documentation will be excluded.
+# Set it to YES to include the internal documentation.
+
+INTERNAL_DOCS          = NO
+
+# If the CASE_SENSE_NAMES tag is set to NO then Doxygen will only generate
+# file names in lower-case letters. If set to YES upper-case letters are also
+# allowed. This is useful if you have classes or files whose names only differ
+# in case and if your file system supports case sensitive file names. Windows
+# and Mac users are advised to set this option to NO.
+
+CASE_SENSE_NAMES       = NO
+
+# If the HIDE_SCOPE_NAMES tag is set to NO (the default) then Doxygen
+# will show members with their full class and namespace scopes in the
+# documentation. If set to YES the scope will be hidden.
+
+HIDE_SCOPE_NAMES       = NO
+
+# If the SHOW_INCLUDE_FILES tag is set to YES (the default) then Doxygen
+# will put a list of the files that are included by a file in the documentation
+# of that file.
+
+SHOW_INCLUDE_FILES     = YES
+
+# If the FORCE_LOCAL_INCLUDES tag is set to YES then Doxygen
+# will list include files with double quotes in the documentation
+# rather than with sharp brackets.
+
+FORCE_LOCAL_INCLUDES   = NO
+
+# If the INLINE_INFO tag is set to YES (the default) then a tag [inline]
+# is inserted in the documentation for inline members.
+
+INLINE_INFO            = YES
+
+# If the SORT_MEMBER_DOCS tag is set to YES (the default) then doxygen
+# will sort the (detailed) documentation of file and class members
+# alphabetically by member name. If set to NO the members will appear in
+# declaration order.
+
+SORT_MEMBER_DOCS       = YES
+
+# If the SORT_BRIEF_DOCS tag is set to YES then doxygen will sort the
+# brief documentation of file, namespace and class members alphabetically
+# by member name. If set to NO (the default) the members will appear in
+# declaration order.
+
+SORT_BRIEF_DOCS        = NO
+
+# If the SORT_MEMBERS_CTORS_1ST tag is set to YES then doxygen
+# will sort the (brief and detailed) documentation of class members so that
+# constructors and destructors are listed first. If set to NO (the default)
+# the constructors will appear in the respective orders defined by
+# SORT_MEMBER_DOCS and SORT_BRIEF_DOCS.
+# This tag will be ignored for brief docs if SORT_BRIEF_DOCS is set to NO
+# and ignored for detailed docs if SORT_MEMBER_DOCS is set to NO.
+
+SORT_MEMBERS_CTORS_1ST = NO
+
+# If the SORT_GROUP_NAMES tag is set to YES then doxygen will sort the
+# hierarchy of group names into alphabetical order. If set to NO (the default)
+# the group names will appear in their defined order.
+
+SORT_GROUP_NAMES       = NO
+
+# If the SORT_BY_SCOPE_NAME tag is set to YES, the class list will be
+# sorted by fully-qualified names, including namespaces. If set to
+# NO (the default), the class list will be sorted only by class name,
+# not including the namespace part.
+# Note: This option is not very useful if HIDE_SCOPE_NAMES is set to YES.
+# Note: This option applies only to the class list, not to the
+# alphabetical list.
+
+SORT_BY_SCOPE_NAME     = NO
+
+# If the STRICT_PROTO_MATCHING option is enabled and doxygen fails to
+# do proper type resolution of all parameters of a function it will reject a
+# match between the prototype and the implementation of a member function even
+# if there is only one candidate or it is obvious which candidate to choose
+# by doing a simple string match. By disabling STRICT_PROTO_MATCHING doxygen
+# will still accept a match between prototype and implementation in such cases.
+
+STRICT_PROTO_MATCHING  = NO
+
+# The GENERATE_TODOLIST tag can be used to enable (YES) or
+# disable (NO) the todo list. This list is created by putting \todo
+# commands in the documentation.
+
+GENERATE_TODOLIST      = YES
+
+# The GENERATE_TESTLIST tag can be used to enable (YES) or
+# disable (NO) the test list. This list is created by putting \test
+# commands in the documentation.
+
+GENERATE_TESTLIST      = YES
+
+# The GENERATE_BUGLIST tag can be used to enable (YES) or
+# disable (NO) the bug list. This list is created by putting \bug
+# commands in the documentation.
+
+GENERATE_BUGLIST       = YES
+
+# The GENERATE_DEPRECATEDLIST tag can be used to enable (YES) or
+# disable (NO) the deprecated list. This list is created by putting
+# \deprecated commands in the documentation.
+
+GENERATE_DEPRECATEDLIST= YES
+
+# The ENABLED_SECTIONS tag can be used to enable conditional
+# documentation sections, marked by \if sectionname ... \endif.
+
+ENABLED_SECTIONS       =
+
+# The MAX_INITIALIZER_LINES tag determines the maximum number of lines
+# the initial value of a variable or macro consists of for it to appear in
+# the documentation. If the initializer consists of more lines than specified
+# here it will be hidden. Use a value of 0 to hide initializers completely.
+# The appearance of the initializer of individual variables and macros in the
+# documentation can be controlled using \showinitializer or \hideinitializer
+# command in the documentation regardless of this setting.
+
+MAX_INITIALIZER_LINES  = 30
+
+# Set the SHOW_USED_FILES tag to NO to disable the list of files generated
+# at the bottom of the documentation of classes and structs. If set to YES the
+# list will mention the files that were used to generate the documentation.
+
+SHOW_USED_FILES        = YES
+
+# If the sources in your project are distributed over multiple directories
+# then setting the SHOW_DIRECTORIES tag to YES will show the directory hierarchy
+# in the documentation. The default is NO.
+
+SHOW_DIRECTORIES       = NO
+
+# Set the SHOW_FILES tag to NO to disable the generation of the Files page.
+# This will remove the Files entry from the Quick Index and from the
+# Folder Tree View (if specified). The default is YES.
+
+SHOW_FILES             = YES
+
+# Set the SHOW_NAMESPACES tag to NO to disable the generation of the
+# Namespaces page.
+# This will remove the Namespaces entry from the Quick Index
+# and from the Folder Tree View (if specified). The default is YES.
+
+SHOW_NAMESPACES        = YES
+
+# The FILE_VERSION_FILTER tag can be used to specify a program or script that
+# doxygen should invoke to get the current version for each file (typically from
+# the version control system). Doxygen will invoke the program by executing (via
+# popen()) the command  , where  is the value of
+# the FILE_VERSION_FILTER tag, and  is the name of an input file
+# provided by doxygen. Whatever the program writes to standard output
+# is used as the file version. See the manual for examples.
+
+FILE_VERSION_FILTER    =
+
+# The LAYOUT_FILE tag can be used to specify a layout file which will be parsed
+# by doxygen. The layout file controls the global structure of the generated
+# output files in an output format independent way. The create the layout file
+# that represents doxygen's defaults, run doxygen with the -l option.
+# You can optionally specify a file name after the option, if omitted
+# DoxygenLayout.xml will be used as the name of the layout file.
+
+LAYOUT_FILE            =
+
+#---------------------------------------------------------------------------
+# configuration options related to warning and progress messages
+#---------------------------------------------------------------------------
+
+# The QUIET tag can be used to turn on/off the messages that are generated
+# by doxygen. Possible values are YES and NO. If left blank NO is used.
+
+QUIET                  = NO
+
+# The WARNINGS tag can be used to turn on/off the warning messages that are
+# generated by doxygen. Possible values are YES and NO. If left blank
+# NO is used.
+
+WARNINGS               = YES
+
+# If WARN_IF_UNDOCUMENTED is set to YES, then doxygen will generate warnings
+# for undocumented members. If EXTRACT_ALL is set to YES then this flag will
+# automatically be disabled.
+
+WARN_IF_UNDOCUMENTED   = YES
+
+# If WARN_IF_DOC_ERROR is set to YES, doxygen will generate warnings for
+# potential errors in the documentation, such as not documenting some
+# parameters in a documented function, or documenting parameters that
+# don't exist or using markup commands wrongly.
+
+WARN_IF_DOC_ERROR      = YES
+
+# The WARN_NO_PARAMDOC option can be enabled to get warnings for
+# functions that are documented, but have no documentation for their parameters
+# or return value. If set to NO (the default) doxygen will only warn about
+# wrong or incomplete parameter documentation, but not about the absence of
+# documentation.
+
+WARN_NO_PARAMDOC       = NO
+
+# The WARN_FORMAT tag determines the format of the warning messages that
+# doxygen can produce. The string should contain the $file, $line, and $text
+# tags, which will be replaced by the file and line number from which the
+# warning originated and the warning text. Optionally the format may contain
+# $version, which will be replaced by the version of the file (if it could
+# be obtained via FILE_VERSION_FILTER)
+
+WARN_FORMAT            = "$file:$line: $text"
+
+# The WARN_LOGFILE tag can be used to specify a file to which warning
+# and error messages should be written. If left blank the output is written
+# to stderr.
+
+WARN_LOGFILE           =
+
+#---------------------------------------------------------------------------
+# configuration options related to the input files
+#---------------------------------------------------------------------------
+
+# The INPUT tag can be used to specify the files and/or directories that contain
+# documented source files. You may enter file names like "myfile.cpp" or
+# directories like "/usr/src/myproject". Separate the files or directories
+# with spaces.
+
+INPUT                  = include/serial include/serial/impl src src/impl
+
+# This tag can be used to specify the character encoding of the source files
+# that doxygen parses. Internally doxygen uses the UTF-8 encoding, which is
+# also the default input encoding. Doxygen uses libiconv (or the iconv built
+# into libc) for the transcoding. See http://www.gnu.org/software/libiconv for
+# the list of possible encodings.
+
+INPUT_ENCODING         = UTF-8
+
+# If the value of the INPUT tag contains directories, you can use the
+# FILE_PATTERNS tag to specify one or more wildcard pattern (like *.cpp
+# and *.h) to filter out the source-files in the directories. If left
+# blank the following patterns are tested:
+# *.c *.cc *.cxx *.cpp *.c++ *.d *.java *.ii *.ixx *.ipp *.i++ *.inl *.h *.hh
+# *.hxx *.hpp *.h++ *.idl *.odl *.cs *.php *.php3 *.inc *.m *.mm *.dox *.py
+# *.f90 *.f *.for *.vhd *.vhdl
+
+FILE_PATTERNS          =
+
+# The RECURSIVE tag can be used to turn specify whether or not subdirectories
+# should be searched for input files as well. Possible values are YES and NO.
+# If left blank NO is used.
+
+RECURSIVE              = NO
+
+# The EXCLUDE tag can be used to specify files and/or directories that should
+# excluded from the INPUT source files. This way you can easily exclude a
+# subdirectory from a directory tree whose root is specified with the INPUT tag.
+
+EXCLUDE                =
+
+# The EXCLUDE_SYMLINKS tag can be used select whether or not files or
+# directories that are symbolic links (a Unix file system feature) are excluded
+# from the input.
+
+EXCLUDE_SYMLINKS       = NO
+
+# If the value of the INPUT tag contains directories, you can use the
+# EXCLUDE_PATTERNS tag to specify one or more wildcard patterns to exclude
+# certain files from those directories. Note that the wildcards are matched
+# against the file with absolute path, so to exclude all test directories
+# for example use the pattern */test/*
+
+EXCLUDE_PATTERNS       =
+
+# The EXCLUDE_SYMBOLS tag can be used to specify one or more symbol names
+# (namespaces, classes, functions, etc.) that should be excluded from the
+# output. The symbol name can be a fully qualified name, a word, or if the
+# wildcard * is used, a substring. Examples: ANamespace, AClass,
+# AClass::ANamespace, ANamespace::*Test
+
+EXCLUDE_SYMBOLS        =
+
+# The EXAMPLE_PATH tag can be used to specify one or more files or
+# directories that contain example code fragments that are included (see
+# the \include command).
+
+EXAMPLE_PATH           =
+
+# If the value of the EXAMPLE_PATH tag contains directories, you can use the
+# EXAMPLE_PATTERNS tag to specify one or more wildcard pattern (like *.cpp
+# and *.h) to filter out the source-files in the directories. If left
+# blank all files are included.
+
+EXAMPLE_PATTERNS       =
+
+# If the EXAMPLE_RECURSIVE tag is set to YES then subdirectories will be
+# searched for input files to be used with the \include or \dontinclude
+# commands irrespective of the value of the RECURSIVE tag.
+# Possible values are YES and NO. If left blank NO is used.
+
+EXAMPLE_RECURSIVE      = NO
+
+# The IMAGE_PATH tag can be used to specify one or more files or
+# directories that contain image that are included in the documentation (see
+# the \image command).
+
+IMAGE_PATH             =
+
+# The INPUT_FILTER tag can be used to specify a program that doxygen should
+# invoke to filter for each input file. Doxygen will invoke the filter program
+# by executing (via popen()) the command  , where 
+# is the value of the INPUT_FILTER tag, and  is the name of an
+# input file. Doxygen will then use the output that the filter program writes
+# to standard output.
+# If FILTER_PATTERNS is specified, this tag will be
+# ignored.
+
+INPUT_FILTER           =
+
+# The FILTER_PATTERNS tag can be used to specify filters on a per file pattern
+# basis.
+# Doxygen will compare the file name with each pattern and apply the
+# filter if there is a match.
+# The filters are a list of the form:
+# pattern=filter (like *.cpp=my_cpp_filter). See INPUT_FILTER for further
+# info on how filters are used. If FILTER_PATTERNS is empty or if
+# non of the patterns match the file name, INPUT_FILTER is applied.
+
+FILTER_PATTERNS        =
+
+# If the FILTER_SOURCE_FILES tag is set to YES, the input filter (if set using
+# INPUT_FILTER) will be used to filter the input files when producing source
+# files to browse (i.e. when SOURCE_BROWSER is set to YES).
+
+FILTER_SOURCE_FILES    = NO
+
+# The FILTER_SOURCE_PATTERNS tag can be used to specify source filters per file
+# pattern. A pattern will override the setting for FILTER_PATTERN (if any)
+# and it is also possible to disable source filtering for a specific pattern
+# using *.ext= (so without naming a filter). This option only has effect when
+# FILTER_SOURCE_FILES is enabled.
+
+FILTER_SOURCE_PATTERNS =
+
+#---------------------------------------------------------------------------
+# configuration options related to source browsing
+#---------------------------------------------------------------------------
+
+# If the SOURCE_BROWSER tag is set to YES then a list of source files will
+# be generated. Documented entities will be cross-referenced with these sources.
+# Note: To get rid of all source code in the generated output, make sure also
+# VERBATIM_HEADERS is set to NO.
+
+SOURCE_BROWSER         = NO
+
+# Setting the INLINE_SOURCES tag to YES will include the body
+# of functions and classes directly in the documentation.
+
+INLINE_SOURCES         = YES
+
+# Setting the STRIP_CODE_COMMENTS tag to YES (the default) will instruct
+# doxygen to hide any special comment blocks from generated source code
+# fragments. Normal C and C++ comments will always remain visible.
+
+STRIP_CODE_COMMENTS    = YES
+
+# If the REFERENCED_BY_RELATION tag is set to YES
+# then for each documented function all documented
+# functions referencing it will be listed.
+
+REFERENCED_BY_RELATION = NO
+
+# If the REFERENCES_RELATION tag is set to YES
+# then for each documented function all documented entities
+# called/used by that function will be listed.
+
+REFERENCES_RELATION    = NO
+
+# If the REFERENCES_LINK_SOURCE tag is set to YES (the default)
+# and SOURCE_BROWSER tag is set to YES, then the hyperlinks from
+# functions in REFERENCES_RELATION and REFERENCED_BY_RELATION lists will
+# link to the source code.
+# Otherwise they will link to the documentation.
+
+REFERENCES_LINK_SOURCE = YES
+
+# If the USE_HTAGS tag is set to YES then the references to source code
+# will point to the HTML generated by the htags(1) tool instead of doxygen
+# built-in source browser. The htags tool is part of GNU's global source
+# tagging system (see http://www.gnu.org/software/global/global.html). You
+# will need version 4.8.6 or higher.
+
+USE_HTAGS              = NO
+
+# If the VERBATIM_HEADERS tag is set to YES (the default) then Doxygen
+# will generate a verbatim copy of the header file for each class for
+# which an include is specified. Set to NO to disable this.
+
+VERBATIM_HEADERS       = YES
+
+#---------------------------------------------------------------------------
+# configuration options related to the alphabetical class index
+#---------------------------------------------------------------------------
+
+# If the ALPHABETICAL_INDEX tag is set to YES, an alphabetical index
+# of all compounds will be generated. Enable this if the project
+# contains a lot of classes, structs, unions or interfaces.
+
+ALPHABETICAL_INDEX     = YES
+
+# If the alphabetical index is enabled (see ALPHABETICAL_INDEX) then
+# the COLS_IN_ALPHA_INDEX tag can be used to specify the number of columns
+# in which this list will be split (can be a number in the range [1..20])
+
+COLS_IN_ALPHA_INDEX    = 5
+
+# In case all classes in a project start with a common prefix, all
+# classes will be put under the same header in the alphabetical index.
+# The IGNORE_PREFIX tag can be used to specify one or more prefixes that
+# should be ignored while generating the index headers.
+
+IGNORE_PREFIX          =
+
+#---------------------------------------------------------------------------
+# configuration options related to the HTML output
+#---------------------------------------------------------------------------
+
+# If the GENERATE_HTML tag is set to YES (the default) Doxygen will
+# generate HTML output.
+
+GENERATE_HTML          = YES
+
+# The HTML_OUTPUT tag is used to specify where the HTML docs will be put.
+# If a relative path is entered the value of OUTPUT_DIRECTORY will be
+# put in front of it. If left blank `html' will be used as the default path.
+
+HTML_OUTPUT            = html
+
+# The HTML_FILE_EXTENSION tag can be used to specify the file extension for
+# each generated HTML page (for example: .htm,.php,.asp). If it is left blank
+# doxygen will generate files with .html extension.
+
+HTML_FILE_EXTENSION    = .html
+
+# The HTML_HEADER tag can be used to specify a personal HTML header for
+# each generated HTML page. If it is left blank doxygen will generate a
+# standard header. Note that when using a custom header you are responsible
+# for the proper inclusion of any scripts and style sheets that doxygen
+# needs, which is dependent on the configuration options used.
+# It is adviced to generate a default header using "doxygen -w html
+# header.html footer.html stylesheet.css YourConfigFile" and then modify
+# that header. Note that the header is subject to change so you typically
+# have to redo this when upgrading to a newer version of doxygen or when changing the value of configuration settings such as GENERATE_TREEVIEW!
+
+HTML_HEADER            =
+
+# The HTML_FOOTER tag can be used to specify a personal HTML footer for
+# each generated HTML page. If it is left blank doxygen will generate a
+# standard footer.
+
+HTML_FOOTER            =
+
+# The HTML_STYLESHEET tag can be used to specify a user-defined cascading
+# style sheet that is used by each HTML page. It can be used to
+# fine-tune the look of the HTML output. If the tag is left blank doxygen
+# will generate a default style sheet. Note that doxygen will try to copy
+# the style sheet file to the HTML output directory, so don't put your own
+# stylesheet in the HTML output directory as well, or it will be erased!
+
+HTML_STYLESHEET        =
+
+# The HTML_EXTRA_FILES tag can be used to specify one or more extra images or
+# other source files which should be copied to the HTML output directory. Note
+# that these files will be copied to the base HTML output directory. Use the
+# $relpath$ marker in the HTML_HEADER and/or HTML_FOOTER files to load these
+# files. In the HTML_STYLESHEET file, use the file name only. Also note that
+# the files will be copied as-is; there are no commands or markers available.
+
+HTML_EXTRA_FILES       =
+
+# The HTML_COLORSTYLE_HUE tag controls the color of the HTML output.
+# Doxygen will adjust the colors in the stylesheet and background images
+# according to this color. Hue is specified as an angle on a colorwheel,
+# see http://en.wikipedia.org/wiki/Hue for more information.
+# For instance the value 0 represents red, 60 is yellow, 120 is green,
+# 180 is cyan, 240 is blue, 300 purple, and 360 is red again.
+# The allowed range is 0 to 359.
+
+HTML_COLORSTYLE_HUE    = 220
+
+# The HTML_COLORSTYLE_SAT tag controls the purity (or saturation) of
+# the colors in the HTML output. For a value of 0 the output will use
+# grayscales only. A value of 255 will produce the most vivid colors.
+
+HTML_COLORSTYLE_SAT    = 100
+
+# The HTML_COLORSTYLE_GAMMA tag controls the gamma correction applied to
+# the luminance component of the colors in the HTML output. Values below
+# 100 gradually make the output lighter, whereas values above 100 make
+# the output darker. The value divided by 100 is the actual gamma applied,
+# so 80 represents a gamma of 0.8, The value 220 represents a gamma of 2.2,
+# and 100 does not change the gamma.
+
+HTML_COLORSTYLE_GAMMA  = 80
+
+# If the HTML_TIMESTAMP tag is set to YES then the footer of each generated HTML
+# page will contain the date and time when the page was generated. Setting
+# this to NO can help when comparing the output of multiple runs.
+
+HTML_TIMESTAMP         = YES
+
+# If the HTML_ALIGN_MEMBERS tag is set to YES, the members of classes,
+# files or namespaces will be aligned in HTML using tables. If set to
+# NO a bullet list will be used.
+
+HTML_ALIGN_MEMBERS     = YES
+
+# If the HTML_DYNAMIC_SECTIONS tag is set to YES then the generated HTML
+# documentation will contain sections that can be hidden and shown after the
+# page has loaded. For this to work a browser that supports
+# JavaScript and DHTML is required (for instance Mozilla 1.0+, Firefox
+# Netscape 6.0+, Internet explorer 5.0+, Konqueror, or Safari).
+
+HTML_DYNAMIC_SECTIONS  = NO
+
+# If the GENERATE_DOCSET tag is set to YES, additional index files
+# will be generated that can be used as input for Apple's Xcode 3
+# integrated development environment, introduced with OSX 10.5 (Leopard).
+# To create a documentation set, doxygen will generate a Makefile in the
+# HTML output directory. Running make will produce the docset in that
+# directory and running "make install" will install the docset in
+# ~/Library/Developer/Shared/Documentation/DocSets so that Xcode will find
+# it at startup.
+# See http://developer.apple.com/tools/creatingdocsetswithdoxygen.html
+# for more information.
+
+GENERATE_DOCSET        = NO
+
+# When GENERATE_DOCSET tag is set to YES, this tag determines the name of the
+# feed. A documentation feed provides an umbrella under which multiple
+# documentation sets from a single provider (such as a company or product suite)
+# can be grouped.
+
+DOCSET_FEEDNAME        = "Doxygen generated docs"
+
+# When GENERATE_DOCSET tag is set to YES, this tag specifies a string that
+# should uniquely identify the documentation set bundle. This should be a
+# reverse domain-name style string, e.g. com.mycompany.MyDocSet. Doxygen
+# will append .docset to the name.
+
+DOCSET_BUNDLE_ID       = org.doxygen.Project
+
+# When GENERATE_PUBLISHER_ID tag specifies a string that should uniquely identify
+# the documentation publisher. This should be a reverse domain-name style
+# string, e.g. com.mycompany.MyDocSet.documentation.
+
+DOCSET_PUBLISHER_ID    = org.doxygen.Publisher
+
+# The GENERATE_PUBLISHER_NAME tag identifies the documentation publisher.
+
+DOCSET_PUBLISHER_NAME  = Publisher
+
+# If the GENERATE_HTMLHELP tag is set to YES, additional index files
+# will be generated that can be used as input for tools like the
+# Microsoft HTML help workshop to generate a compiled HTML help file (.chm)
+# of the generated HTML documentation.
+
+GENERATE_HTMLHELP      = NO
+
+# If the GENERATE_HTMLHELP tag is set to YES, the CHM_FILE tag can
+# be used to specify the file name of the resulting .chm file. You
+# can add a path in front of the file if the result should not be
+# written to the html output directory.
+
+CHM_FILE               =
+
+# If the GENERATE_HTMLHELP tag is set to YES, the HHC_LOCATION tag can
+# be used to specify the location (absolute path including file name) of
+# the HTML help compiler (hhc.exe). If non-empty doxygen will try to run
+# the HTML help compiler on the generated index.hhp.
+
+HHC_LOCATION           =
+
+# If the GENERATE_HTMLHELP tag is set to YES, the GENERATE_CHI flag
+# controls if a separate .chi index file is generated (YES) or that
+# it should be included in the master .chm file (NO).
+
+GENERATE_CHI           = NO
+
+# If the GENERATE_HTMLHELP tag is set to YES, the CHM_INDEX_ENCODING
+# is used to encode HtmlHelp index (hhk), content (hhc) and project file
+# content.
+
+CHM_INDEX_ENCODING     =
+
+# If the GENERATE_HTMLHELP tag is set to YES, the BINARY_TOC flag
+# controls whether a binary table of contents is generated (YES) or a
+# normal table of contents (NO) in the .chm file.
+
+BINARY_TOC             = NO
+
+# The TOC_EXPAND flag can be set to YES to add extra items for group members
+# to the contents of the HTML help documentation and to the tree view.
+
+TOC_EXPAND             = NO
+
+# If the GENERATE_QHP tag is set to YES and both QHP_NAMESPACE and
+# QHP_VIRTUAL_FOLDER are set, an additional index file will be generated
+# that can be used as input for Qt's qhelpgenerator to generate a
+# Qt Compressed Help (.qch) of the generated HTML documentation.
+
+GENERATE_QHP           = NO
+
+# If the QHG_LOCATION tag is specified, the QCH_FILE tag can
+# be used to specify the file name of the resulting .qch file.
+# The path specified is relative to the HTML output folder.
+
+QCH_FILE               =
+
+# The QHP_NAMESPACE tag specifies the namespace to use when generating
+# Qt Help Project output. For more information please see
+# http://doc.trolltech.com/qthelpproject.html#namespace
+
+QHP_NAMESPACE          = org.doxygen.Project
+
+# The QHP_VIRTUAL_FOLDER tag specifies the namespace to use when generating
+# Qt Help Project output. For more information please see
+# http://doc.trolltech.com/qthelpproject.html#virtual-folders
+
+QHP_VIRTUAL_FOLDER     = doc
+
+# If QHP_CUST_FILTER_NAME is set, it specifies the name of a custom filter to
+# add. For more information please see
+# http://doc.trolltech.com/qthelpproject.html#custom-filters
+
+QHP_CUST_FILTER_NAME   =
+
+# The QHP_CUST_FILT_ATTRS tag specifies the list of the attributes of the
+# custom filter to add. For more information please see
+# 
+# Qt Help Project / Custom Filters.
+
+QHP_CUST_FILTER_ATTRS  =
+
+# The QHP_SECT_FILTER_ATTRS tag specifies the list of the attributes this
+# project's
+# filter section matches.
+# 
+# Qt Help Project / Filter Attributes.
+
+QHP_SECT_FILTER_ATTRS  =
+
+# If the GENERATE_QHP tag is set to YES, the QHG_LOCATION tag can
+# be used to specify the location of Qt's qhelpgenerator.
+# If non-empty doxygen will try to run qhelpgenerator on the generated
+# .qhp file.
+
+QHG_LOCATION           =
+
+# If the GENERATE_ECLIPSEHELP tag is set to YES, additional index files
+#  will be generated, which together with the HTML files, form an Eclipse help
+# plugin. To install this plugin and make it available under the help contents
+# menu in Eclipse, the contents of the directory containing the HTML and XML
+# files needs to be copied into the plugins directory of eclipse. The name of
+# the directory within the plugins directory should be the same as
+# the ECLIPSE_DOC_ID value. After copying Eclipse needs to be restarted before
+# the help appears.
+
+GENERATE_ECLIPSEHELP   = NO
+
+# A unique identifier for the eclipse help plugin. When installing the plugin
+# the directory name containing the HTML and XML files should also have
+# this name.
+
+ECLIPSE_DOC_ID         = org.doxygen.Project
+
+# The DISABLE_INDEX tag can be used to turn on/off the condensed index at
+# top of each HTML page. The value NO (the default) enables the index and
+# the value YES disables it.
+
+DISABLE_INDEX          = NO
+
+# The ENUM_VALUES_PER_LINE tag can be used to set the number of enum values
+# (range [0,1..20]) that doxygen will group on one line in the generated HTML
+# documentation. Note that a value of 0 will completely suppress the enum
+# values from appearing in the overview section.
+
+ENUM_VALUES_PER_LINE   = 4
+
+# The GENERATE_TREEVIEW tag is used to specify whether a tree-like index
+# structure should be generated to display hierarchical information.
+# If the tag value is set to YES, a side panel will be generated
+# containing a tree-like index structure (just like the one that
+# is generated for HTML Help). For this to work a browser that supports
+# JavaScript, DHTML, CSS and frames is required (i.e. any modern browser).
+# Windows users are probably better off using the HTML help feature.
+
+GENERATE_TREEVIEW      = NO
+
+# By enabling USE_INLINE_TREES, doxygen will generate the Groups, Directories,
+# and Class Hierarchy pages using a tree view instead of an ordered list.
+
+USE_INLINE_TREES       = NO
+
+# If the treeview is enabled (see GENERATE_TREEVIEW) then this tag can be
+# used to set the initial width (in pixels) of the frame in which the tree
+# is shown.
+
+TREEVIEW_WIDTH         = 250
+
+# When the EXT_LINKS_IN_WINDOW option is set to YES doxygen will open
+# links to external symbols imported via tag files in a separate window.
+
+EXT_LINKS_IN_WINDOW    = NO
+
+# Use this tag to change the font size of Latex formulas included
+# as images in the HTML documentation. The default is 10. Note that
+# when you change the font size after a successful doxygen run you need
+# to manually remove any form_*.png images from the HTML output directory
+# to force them to be regenerated.
+
+FORMULA_FONTSIZE       = 10
+
+# Use the FORMULA_TRANPARENT tag to determine whether or not the images
+# generated for formulas are transparent PNGs. Transparent PNGs are
+# not supported properly for IE 6.0, but are supported on all modern browsers.
+# Note that when changing this option you need to delete any form_*.png files
+# in the HTML output before the changes have effect.
+
+FORMULA_TRANSPARENT    = YES
+
+# Enable the USE_MATHJAX option to render LaTeX formulas using MathJax
+# (see http://www.mathjax.org) which uses client side Javascript for the
+# rendering instead of using prerendered bitmaps. Use this if you do not
+# have LaTeX installed or if you want to formulas look prettier in the HTML
+# output. When enabled you also need to install MathJax separately and
+# configure the path to it using the MATHJAX_RELPATH option.
+
+USE_MATHJAX            = NO
+
+# When MathJax is enabled you need to specify the location relative to the
+# HTML output directory using the MATHJAX_RELPATH option. The destination
+# directory should contain the MathJax.js script. For instance, if the mathjax
+# directory is located at the same level as the HTML output directory, then
+# MATHJAX_RELPATH should be ../mathjax. The default value points to the
+# mathjax.org site, so you can quickly see the result without installing
+# MathJax, but it is strongly recommended to install a local copy of MathJax
+# before deployment.
+
+MATHJAX_RELPATH        = http://www.mathjax.org/mathjax
+
+# When the SEARCHENGINE tag is enabled doxygen will generate a search box
+# for the HTML output. The underlying search engine uses javascript
+# and DHTML and should work on any modern browser. Note that when using
+# HTML help (GENERATE_HTMLHELP), Qt help (GENERATE_QHP), or docsets
+# (GENERATE_DOCSET) there is already a search function so this one should
+# typically be disabled. For large projects the javascript based search engine
+# can be slow, then enabling SERVER_BASED_SEARCH may provide a better solution.
+
+SEARCHENGINE           = YES
+
+# When the SERVER_BASED_SEARCH tag is enabled the search engine will be
+# implemented using a PHP enabled web server instead of at the web client
+# using Javascript. Doxygen will generate the search PHP script and index
+# file to put on the web server. The advantage of the server
+# based approach is that it scales better to large projects and allows
+# full text search. The disadvantages are that it is more difficult to setup
+# and does not have live searching capabilities.
+
+SERVER_BASED_SEARCH    = NO
+
+#---------------------------------------------------------------------------
+# configuration options related to the LaTeX output
+#---------------------------------------------------------------------------
+
+# If the GENERATE_LATEX tag is set to YES (the default) Doxygen will
+# generate Latex output.
+
+GENERATE_LATEX         = NO
+
+# The LATEX_OUTPUT tag is used to specify where the LaTeX docs will be put.
+# If a relative path is entered the value of OUTPUT_DIRECTORY will be
+# put in front of it. If left blank `latex' will be used as the default path.
+
+LATEX_OUTPUT           = latex
+
+# The LATEX_CMD_NAME tag can be used to specify the LaTeX command name to be
+# invoked. If left blank `latex' will be used as the default command name.
+# Note that when enabling USE_PDFLATEX this option is only used for
+# generating bitmaps for formulas in the HTML output, but not in the
+# Makefile that is written to the output directory.
+
+LATEX_CMD_NAME         = latex
+
+# The MAKEINDEX_CMD_NAME tag can be used to specify the command name to
+# generate index for LaTeX. If left blank `makeindex' will be used as the
+# default command name.
+
+MAKEINDEX_CMD_NAME     = makeindex
+
+# If the COMPACT_LATEX tag is set to YES Doxygen generates more compact
+# LaTeX documents. This may be useful for small projects and may help to
+# save some trees in general.
+
+COMPACT_LATEX          = NO
+
+# The PAPER_TYPE tag can be used to set the paper type that is used
+# by the printer. Possible values are: a4, letter, legal and
+# executive. If left blank a4wide will be used.
+
+PAPER_TYPE             = a4
+
+# The EXTRA_PACKAGES tag can be to specify one or more names of LaTeX
+# packages that should be included in the LaTeX output.
+
+EXTRA_PACKAGES         =
+
+# The LATEX_HEADER tag can be used to specify a personal LaTeX header for
+# the generated latex document. The header should contain everything until
+# the first chapter. If it is left blank doxygen will generate a
+# standard header. Notice: only use this tag if you know what you are doing!
+
+LATEX_HEADER           =
+
+# The LATEX_FOOTER tag can be used to specify a personal LaTeX footer for
+# the generated latex document. The footer should contain everything after
+# the last chapter. If it is left blank doxygen will generate a
+# standard footer. Notice: only use this tag if you know what you are doing!
+
+LATEX_FOOTER           =
+
+# If the PDF_HYPERLINKS tag is set to YES, the LaTeX that is generated
+# is prepared for conversion to pdf (using ps2pdf). The pdf file will
+# contain links (just like the HTML output) instead of page references
+# This makes the output suitable for online browsing using a pdf viewer.
+
+PDF_HYPERLINKS         = YES
+
+# If the USE_PDFLATEX tag is set to YES, pdflatex will be used instead of
+# plain latex in the generated Makefile. Set this option to YES to get a
+# higher quality PDF documentation.
+
+USE_PDFLATEX           = YES
+
+# If the LATEX_BATCHMODE tag is set to YES, doxygen will add the \\batchmode.
+# command to the generated LaTeX files. This will instruct LaTeX to keep
+# running if errors occur, instead of asking the user for help.
+# This option is also used when generating formulas in HTML.
+
+LATEX_BATCHMODE        = NO
+
+# If LATEX_HIDE_INDICES is set to YES then doxygen will not
+# include the index chapters (such as File Index, Compound Index, etc.)
+# in the output.
+
+LATEX_HIDE_INDICES     = NO
+
+# If LATEX_SOURCE_CODE is set to YES then doxygen will include
+# source code with syntax highlighting in the LaTeX output.
+# Note that which sources are shown also depends on other settings
+# such as SOURCE_BROWSER.
+
+LATEX_SOURCE_CODE      = NO
+
+#---------------------------------------------------------------------------
+# configuration options related to the RTF output
+#---------------------------------------------------------------------------
+
+# If the GENERATE_RTF tag is set to YES Doxygen will generate RTF output
+# The RTF output is optimized for Word 97 and may not look very pretty with
+# other RTF readers or editors.
+
+GENERATE_RTF           = NO
+
+# The RTF_OUTPUT tag is used to specify where the RTF docs will be put.
+# If a relative path is entered the value of OUTPUT_DIRECTORY will be
+# put in front of it. If left blank `rtf' will be used as the default path.
+
+RTF_OUTPUT             = rtf
+
+# If the COMPACT_RTF tag is set to YES Doxygen generates more compact
+# RTF documents. This may be useful for small projects and may help to
+# save some trees in general.
+
+COMPACT_RTF            = NO
+
+# If the RTF_HYPERLINKS tag is set to YES, the RTF that is generated
+# will contain hyperlink fields. The RTF file will
+# contain links (just like the HTML output) instead of page references.
+# This makes the output suitable for online browsing using WORD or other
+# programs which support those fields.
+# Note: wordpad (write) and others do not support links.
+
+RTF_HYPERLINKS         = NO
+
+# Load stylesheet definitions from file. Syntax is similar to doxygen's
+# config file, i.e. a series of assignments. You only have to provide
+# replacements, missing definitions are set to their default value.
+
+RTF_STYLESHEET_FILE    =
+
+# Set optional variables used in the generation of an rtf document.
+# Syntax is similar to doxygen's config file.
+
+RTF_EXTENSIONS_FILE    =
+
+#---------------------------------------------------------------------------
+# configuration options related to the man page output
+#---------------------------------------------------------------------------
+
+# If the GENERATE_MAN tag is set to YES (the default) Doxygen will
+# generate man pages
+
+GENERATE_MAN           = NO
+
+# The MAN_OUTPUT tag is used to specify where the man pages will be put.
+# If a relative path is entered the value of OUTPUT_DIRECTORY will be
+# put in front of it. If left blank `man' will be used as the default path.
+
+MAN_OUTPUT             = man
+
+# The MAN_EXTENSION tag determines the extension that is added to
+# the generated man pages (default is the subroutine's section .3)
+
+MAN_EXTENSION          = .3
+
+# If the MAN_LINKS tag is set to YES and Doxygen generates man output,
+# then it will generate one additional man file for each entity
+# documented in the real man page(s). These additional files
+# only source the real man page, but without them the man command
+# would be unable to find the correct page. The default is NO.
+
+MAN_LINKS              = NO
+
+#---------------------------------------------------------------------------
+# configuration options related to the XML output
+#---------------------------------------------------------------------------
+
+# If the GENERATE_XML tag is set to YES Doxygen will
+# generate an XML file that captures the structure of
+# the code including all documentation.
+
+GENERATE_XML           = NO
+
+# The XML_OUTPUT tag is used to specify where the XML pages will be put.
+# If a relative path is entered the value of OUTPUT_DIRECTORY will be
+# put in front of it. If left blank `xml' will be used as the default path.
+
+XML_OUTPUT             = xml
+
+# The XML_SCHEMA tag can be used to specify an XML schema,
+# which can be used by a validating XML parser to check the
+# syntax of the XML files.
+
+XML_SCHEMA             =
+
+# The XML_DTD tag can be used to specify an XML DTD,
+# which can be used by a validating XML parser to check the
+# syntax of the XML files.
+
+XML_DTD                =
+
+# If the XML_PROGRAMLISTING tag is set to YES Doxygen will
+# dump the program listings (including syntax highlighting
+# and cross-referencing information) to the XML output. Note that
+# enabling this will significantly increase the size of the XML output.
+
+XML_PROGRAMLISTING     = YES
+
+#---------------------------------------------------------------------------
+# configuration options for the AutoGen Definitions output
+#---------------------------------------------------------------------------
+
+# If the GENERATE_AUTOGEN_DEF tag is set to YES Doxygen will
+# generate an AutoGen Definitions (see autogen.sf.net) file
+# that captures the structure of the code including all
+# documentation. Note that this feature is still experimental
+# and incomplete at the moment.
+
+GENERATE_AUTOGEN_DEF   = NO
+
+#---------------------------------------------------------------------------
+# configuration options related to the Perl module output
+#---------------------------------------------------------------------------
+
+# If the GENERATE_PERLMOD tag is set to YES Doxygen will
+# generate a Perl module file that captures the structure of
+# the code including all documentation. Note that this
+# feature is still experimental and incomplete at the
+# moment.
+
+GENERATE_PERLMOD       = NO
+
+# If the PERLMOD_LATEX tag is set to YES Doxygen will generate
+# the necessary Makefile rules, Perl scripts and LaTeX code to be able
+# to generate PDF and DVI output from the Perl module output.
+
+PERLMOD_LATEX          = NO
+
+# If the PERLMOD_PRETTY tag is set to YES the Perl module output will be
+# nicely formatted so it can be parsed by a human reader.
+# This is useful
+# if you want to understand what is going on.
+# On the other hand, if this
+# tag is set to NO the size of the Perl module output will be much smaller
+# and Perl will parse it just the same.
+
+PERLMOD_PRETTY         = YES
+
+# The names of the make variables in the generated doxyrules.make file
+# are prefixed with the string contained in PERLMOD_MAKEVAR_PREFIX.
+# This is useful so different doxyrules.make files included by the same
+# Makefile don't overwrite each other's variables.
+
+PERLMOD_MAKEVAR_PREFIX =
+
+#---------------------------------------------------------------------------
+# Configuration options related to the preprocessor
+#---------------------------------------------------------------------------
+
+# If the ENABLE_PREPROCESSING tag is set to YES (the default) Doxygen will
+# evaluate all C-preprocessor directives found in the sources and include
+# files.
+
+ENABLE_PREPROCESSING   = YES
+
+# If the MACRO_EXPANSION tag is set to YES Doxygen will expand all macro
+# names in the source code. If set to NO (the default) only conditional
+# compilation will be performed. Macro expansion can be done in a controlled
+# way by setting EXPAND_ONLY_PREDEF to YES.
+
+MACRO_EXPANSION        = NO
+
+# If the EXPAND_ONLY_PREDEF and MACRO_EXPANSION tags are both set to YES
+# then the macro expansion is limited to the macros specified with the
+# PREDEFINED and EXPAND_AS_DEFINED tags.
+
+EXPAND_ONLY_PREDEF     = NO
+
+# If the SEARCH_INCLUDES tag is set to YES (the default) the includes files
+# pointed to by INCLUDE_PATH will be searched when a #include is found.
+
+SEARCH_INCLUDES        = YES
+
+# The INCLUDE_PATH tag can be used to specify one or more directories that
+# contain include files that are not input files but should be processed by
+# the preprocessor.
+
+INCLUDE_PATH           =
+
+# You can use the INCLUDE_FILE_PATTERNS tag to specify one or more wildcard
+# patterns (like *.h and *.hpp) to filter out the header-files in the
+# directories. If left blank, the patterns specified with FILE_PATTERNS will
+# be used.
+
+INCLUDE_FILE_PATTERNS  =
+
+# The PREDEFINED tag can be used to specify one or more macro names that
+# are defined before the preprocessor is started (similar to the -D option of
+# gcc). The argument of the tag is a list of macros of the form: name
+# or name=definition (no spaces). If the definition and the = are
+# omitted =1 is assumed. To prevent a macro definition from being
+# undefined via #undef or recursively expanded use the := operator
+# instead of the = operator.
+
+PREDEFINED             =
+
+# If the MACRO_EXPANSION and EXPAND_ONLY_PREDEF tags are set to YES then
+# this tag can be used to specify a list of macro names that should be expanded.
+# The macro definition that is found in the sources will be used.
+# Use the PREDEFINED tag if you want to use a different macro definition that
+# overrules the definition found in the source code.
+
+EXPAND_AS_DEFINED      =
+
+# If the SKIP_FUNCTION_MACROS tag is set to YES (the default) then
+# doxygen's preprocessor will remove all references to function-like macros
+# that are alone on a line, have an all uppercase name, and do not end with a
+# semicolon, because these will confuse the parser if not removed.
+
+SKIP_FUNCTION_MACROS   = YES
+
+#---------------------------------------------------------------------------
+# Configuration::additions related to external references
+#---------------------------------------------------------------------------
+
+# The TAGFILES option can be used to specify one or more tagfiles.
+# Optionally an initial location of the external documentation
+# can be added for each tagfile. The format of a tag file without
+# this location is as follows:
+#
+# TAGFILES = file1 file2 ...
+# Adding location for the tag files is done as follows:
+#
+# TAGFILES = file1=loc1 "file2 = loc2" ...
+# where "loc1" and "loc2" can be relative or absolute paths or
+# URLs. If a location is present for each tag, the installdox tool
+# does not have to be run to correct the links.
+# Note that each tag file must have a unique name
+# (where the name does NOT include the path)
+# If a tag file is not located in the directory in which doxygen
+# is run, you must also specify the path to the tagfile here.
+
+TAGFILES               =
+
+# When a file name is specified after GENERATE_TAGFILE, doxygen will create
+# a tag file that is based on the input files it reads.
+
+GENERATE_TAGFILE       =
+
+# If the ALLEXTERNALS tag is set to YES all external classes will be listed
+# in the class index. If set to NO only the inherited external classes
+# will be listed.
+
+ALLEXTERNALS           = NO
+
+# If the EXTERNAL_GROUPS tag is set to YES all external groups will be listed
+# in the modules index. If set to NO, only the current project's groups will
+# be listed.
+
+EXTERNAL_GROUPS        = YES
+
+# The PERL_PATH should be the absolute path and name of the perl script
+# interpreter (i.e. the result of `which perl').
+
+PERL_PATH              = /usr/bin/perl
+
+#---------------------------------------------------------------------------
+# Configuration options related to the dot tool
+#---------------------------------------------------------------------------
+
+# If the CLASS_DIAGRAMS tag is set to YES (the default) Doxygen will
+# generate a inheritance diagram (in HTML, RTF and LaTeX) for classes with base
+# or super classes. Setting the tag to NO turns the diagrams off. Note that
+# this option also works with HAVE_DOT disabled, but it is recommended to
+# install and use dot, since it yields more powerful graphs.
+
+CLASS_DIAGRAMS         = YES
+
+# You can define message sequence charts within doxygen comments using the \msc
+# command. Doxygen will then run the mscgen tool (see
+# http://www.mcternan.me.uk/mscgen/) to produce the chart and insert it in the
+# documentation. The MSCGEN_PATH tag allows you to specify the directory where
+# the mscgen tool resides. If left empty the tool is assumed to be found in the
+# default search path.
+
+MSCGEN_PATH            =
+
+# If set to YES, the inheritance and collaboration graphs will hide
+# inheritance and usage relations if the target is undocumented
+# or is not a class.
+
+HIDE_UNDOC_RELATIONS   = YES
+
+# If you set the HAVE_DOT tag to YES then doxygen will assume the dot tool is
+# available from the path. This tool is part of Graphviz, a graph visualization
+# toolkit from AT&T and Lucent Bell Labs. The other options in this section
+# have no effect if this option is set to NO (the default)
+
+HAVE_DOT               = YES
+
+# The DOT_NUM_THREADS specifies the number of dot invocations doxygen is
+# allowed to run in parallel. When set to 0 (the default) doxygen will
+# base this on the number of processors available in the system. You can set it
+# explicitly to a value larger than 0 to get control over the balance
+# between CPU load and processing speed.
+
+DOT_NUM_THREADS        = 0
+
+# By default doxygen will write a font called Helvetica to the output
+# directory and reference it in all dot files that doxygen generates.
+# When you want a differently looking font you can specify the font name
+# using DOT_FONTNAME. You need to make sure dot is able to find the font,
+# which can be done by putting it in a standard location or by setting the
+# DOTFONTPATH environment variable or by setting DOT_FONTPATH to the directory
+# containing the font.
+
+DOT_FONTNAME           = Helvetica
+
+# The DOT_FONTSIZE tag can be used to set the size of the font of dot graphs.
+# The default size is 10pt.
+
+DOT_FONTSIZE           = 10
+
+# By default doxygen will tell dot to use the output directory to look for the
+# FreeSans.ttf font (which doxygen will put there itself). If you specify a
+# different font using DOT_FONTNAME you can set the path where dot
+# can find it using this tag.
+
+DOT_FONTPATH           =
+
+# If the CLASS_GRAPH and HAVE_DOT tags are set to YES then doxygen
+# will generate a graph for each documented class showing the direct and
+# indirect inheritance relations. Setting this tag to YES will force the
+# the CLASS_DIAGRAMS tag to NO.
+
+CLASS_GRAPH            = YES
+
+# If the COLLABORATION_GRAPH and HAVE_DOT tags are set to YES then doxygen
+# will generate a graph for each documented class showing the direct and
+# indirect implementation dependencies (inheritance, containment, and
+# class references variables) of the class with other documented classes.
+
+COLLABORATION_GRAPH    = YES
+
+# If the GROUP_GRAPHS and HAVE_DOT tags are set to YES then doxygen
+# will generate a graph for groups, showing the direct groups dependencies
+
+GROUP_GRAPHS           = YES
+
+# If the UML_LOOK tag is set to YES doxygen will generate inheritance and
+# collaboration diagrams in a style similar to the OMG's Unified Modeling
+# Language.
+
+UML_LOOK               = NO
+
+# If set to YES, the inheritance and collaboration graphs will show the
+# relations between templates and their instances.
+
+TEMPLATE_RELATIONS     = NO
+
+# If the ENABLE_PREPROCESSING, SEARCH_INCLUDES, INCLUDE_GRAPH, and HAVE_DOT
+# tags are set to YES then doxygen will generate a graph for each documented
+# file showing the direct and indirect include dependencies of the file with
+# other documented files.
+
+INCLUDE_GRAPH          = YES
+
+# If the ENABLE_PREPROCESSING, SEARCH_INCLUDES, INCLUDED_BY_GRAPH, and
+# HAVE_DOT tags are set to YES then doxygen will generate a graph for each
+# documented header file showing the documented files that directly or
+# indirectly include this file.
+
+INCLUDED_BY_GRAPH      = YES
+
+# If the CALL_GRAPH and HAVE_DOT options are set to YES then
+# doxygen will generate a call dependency graph for every global function
+# or class method. Note that enabling this option will significantly increase
+# the time of a run. So in most cases it will be better to enable call graphs
+# for selected functions only using the \callgraph command.
+
+CALL_GRAPH             = NO
+
+# If the CALLER_GRAPH and HAVE_DOT tags are set to YES then
+# doxygen will generate a caller dependency graph for every global function
+# or class method. Note that enabling this option will significantly increase
+# the time of a run. So in most cases it will be better to enable caller
+# graphs for selected functions only using the \callergraph command.
+
+CALLER_GRAPH           = NO
+
+# If the GRAPHICAL_HIERARCHY and HAVE_DOT tags are set to YES then doxygen
+# will generate a graphical hierarchy of all classes instead of a textual one.
+
+GRAPHICAL_HIERARCHY    = YES
+
+# If the DIRECTORY_GRAPH, SHOW_DIRECTORIES and HAVE_DOT tags are set to YES
+# then doxygen will show the dependencies a directory has on other directories
+# in a graphical way. The dependency relations are determined by the #include
+# relations between the files in the directories.
+
+DIRECTORY_GRAPH        = YES
+
+# The DOT_IMAGE_FORMAT tag can be used to set the image format of the images
+# generated by dot. Possible values are svg, png, jpg, or gif.
+# If left blank png will be used.
+
+DOT_IMAGE_FORMAT       = png
+
+# The tag DOT_PATH can be used to specify the path where the dot tool can be
+# found. If left blank, it is assumed the dot tool can be found in the path.
+
+DOT_PATH               =
+
+# The DOTFILE_DIRS tag can be used to specify one or more directories that
+# contain dot files that are included in the documentation (see the
+# \dotfile command).
+
+DOTFILE_DIRS           =
+
+# The MSCFILE_DIRS tag can be used to specify one or more directories that
+# contain msc files that are included in the documentation (see the
+# \mscfile command).
+
+MSCFILE_DIRS           =
+
+# The DOT_GRAPH_MAX_NODES tag can be used to set the maximum number of
+# nodes that will be shown in the graph. If the number of nodes in a graph
+# becomes larger than this value, doxygen will truncate the graph, which is
+# visualized by representing a node as a red box. Note that doxygen if the
+# number of direct children of the root node in a graph is already larger than
+# DOT_GRAPH_MAX_NODES then the graph will not be shown at all. Also note
+# that the size of a graph can be further restricted by MAX_DOT_GRAPH_DEPTH.
+
+DOT_GRAPH_MAX_NODES    = 50
+
+# The MAX_DOT_GRAPH_DEPTH tag can be used to set the maximum depth of the
+# graphs generated by dot. A depth value of 3 means that only nodes reachable
+# from the root by following a path via at most 3 edges will be shown. Nodes
+# that lay further from the root node will be omitted. Note that setting this
+# option to 1 or 2 may greatly reduce the computation time needed for large
+# code bases. Also note that the size of a graph can be further restricted by
+# DOT_GRAPH_MAX_NODES. Using a depth of 0 means no depth restriction.
+
+MAX_DOT_GRAPH_DEPTH    = 0
+
+# Set the DOT_TRANSPARENT tag to YES to generate images with a transparent
+# background. This is disabled by default, because dot on Windows does not
+# seem to support this out of the box. Warning: Depending on the platform used,
+# enabling this option may lead to badly anti-aliased labels on the edges of
+# a graph (i.e. they become hard to read).
+
+DOT_TRANSPARENT        = NO
+
+# Set the DOT_MULTI_TARGETS tag to YES allow dot to generate multiple output
+# files in one run (i.e. multiple -o and -T options on the command line). This
+# makes dot run faster, but since only newer versions of dot (>1.8.10)
+# support this, this feature is disabled by default.
+
+DOT_MULTI_TARGETS      = NO
+
+# If the GENERATE_LEGEND tag is set to YES (the default) Doxygen will
+# generate a legend page explaining the meaning of the various boxes and
+# arrows in the dot generated graphs.
+
+GENERATE_LEGEND        = YES
+
+# If the DOT_CLEANUP tag is set to YES (the default) Doxygen will
+# remove the intermediate dot files that are used to generate
+# the various graphs.
+
+DOT_CLEANUP            = YES
\ No newline at end of file
diff --git a/serial.makefile b/serial.makefile
index e1a0af8..1667164 100644
--- a/serial.makefile
+++ b/serial.makefile
@@ -20,6 +20,13 @@ clean:
 	-cd build && make clean
 	rm -rf build bin lib
 
+.PHONY: doc
+doc:
+	@doxygen doc/Doxyfile
+ifeq ($(UNAME),Darwin)
+	@open doc/html/index.html
+endif
+
 .PHONY: test
 test:
 	@mkdir -p build

From f81268fdf08eb7e1249f0d933109546fbfad0dff Mon Sep 17 00:00:00 2001
From: John Harrison 
Date: Fri, 13 Jan 2012 11:33:55 -0600
Subject: [PATCH 24/72] Changed how the memory is allocated in the read
 operation and changed how the timeouts function. If the timeout is -1, then
 it will block until it finishes the reads.

---
 src/impl/unix.cc | 50 ++++++++++++++++++++++++++++++++----------------
 1 file changed, 33 insertions(+), 17 deletions(-)

diff --git a/src/impl/unix.cc b/src/impl/unix.cc
index fd937fe..587b5ed 100644
--- a/src/impl/unix.cc
+++ b/src/impl/unix.cc
@@ -215,42 +215,58 @@ Serial::SerialImpl::read (size_t size) {
     throw PortNotOpenedException("Serial::read");
   }
   string message = "";
-  char buf[1024]; // TODO(ash_gti): Should this be 1024? or...?
+  char *buf = NULL;
+  // Using size+1 to leave room for a null character
+  if (size > 1024) {
+    buf = (char*)malloc((size + 1) * sizeof(*buf));
+  }
+  else {
+    buf = (char*)alloca((size + 1) * sizeof(*buf));
+  }
   fd_set readfds;
+  memset(buf, 0, (size + 1) * sizeof(*buf));
+  ssize_t bytes_read = 0;
   while (message.length() < size) {
-    FD_ZERO(&readfds);
-    FD_SET(fd_, &readfds);
-    struct timeval timeout;
-    timeout.tv_sec =        timeout_ / 1000;
-    timeout.tv_usec = (int) timeout_ % 1000;
-    int r = select(fd_ + 1, &readfds, NULL, NULL, &timeout);
+    if (timeout_ == -1) {
+      FD_ZERO(&readfds);
+      FD_SET(fd_, &readfds);
+      struct timeval timeout;
+      timeout.tv_sec =        timeout_ / 1000;
+      timeout.tv_usec = (int) timeout_ % 1000;
+      int r = select(fd_ + 1, &readfds, NULL, NULL, &timeout);
 
-    if (r == -1 && errno == EINTR)
-      continue;
+      if (r == -1 && errno == EINTR)
+        continue;
 
-    if (r == -1) {
-      perror("select()");
-      exit(EXIT_FAILURE);
+      if (r == -1) {
+        perror("select()");
+        exit(EXIT_FAILURE);
+      }
     }
 
-    if (FD_ISSET(fd_, &readfds)) {
-      memset(buf, 0, 1024);
-      ssize_t bytes_read = ::read(fd_, buf, size-strlen(buf));
+    if (timeout_ == 1 || FD_ISSET(fd_, &readfds)) {
+      ssize_t newest_read = ::read(fd_,
+                                   buf + bytes_read,
+                                   size - static_cast(bytes_read));
       // read should always return some data as select reported it was
       // ready to read when we get to this point.
-      if (bytes_read < 1) {
+      if (newest_read < 1) {
         // Disconnected devices, at least on Linux, show the
         // behavior that they are always ready to read immediately
         // but reading returns nothing.
         throw SerialExecption("device reports readiness to read but "
                               "returned no data (device disconnected?)");
       }
-      message.append(buf, (size_t)bytes_read);
+      bytes_read += newest_read;
     }
     else {
       break; // Timeout
     }
   }
+  if (bytes_read > 0)
+    message.append(buf, (size_t)bytes_read);
+  if (size > 1024)
+    free(buf);
   return message;
 }
 

From 760784e730f25de8af5b5e3c7eebedff079a27e1 Mon Sep 17 00:00:00 2001
From: William Woodall 
Date: Fri, 13 Jan 2012 11:40:59 -0600
Subject: [PATCH 25/72] Updating ROS make system.

---
 include/serial/serial_listener.h |  2 +-
 serial_ros.cmake                 | 21 +++++++++++++++------
 2 files changed, 16 insertions(+), 7 deletions(-)

diff --git a/include/serial/serial_listener.h b/include/serial/serial_listener.h
index 37fb8ef..0aebffa 100644
--- a/include/serial/serial_listener.h
+++ b/include/serial/serial_listener.h
@@ -209,7 +209,7 @@ class SerialListenerException : public std::exception {
   const std::string e_what_;
 public:
   SerialListenerException(const std::string e_what) : e_what_(e_what) {}
-  ~SerialListenerException() throw() {std::exception::~exception();}
+  ~SerialListenerException() throw() {}
 
   virtual const char* what() const throw() {
     std::stringstream ss;
diff --git a/serial_ros.cmake b/serial_ros.cmake
index 431e49e..0c69171 100644
--- a/serial_ros.cmake
+++ b/serial_ros.cmake
@@ -17,20 +17,29 @@ set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
 #set the default path for built libraries to the "lib" directory
 set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
 
-# Check for OS X and if so disable kqueue support in asio
-IF(CMAKE_SYSTEM_NAME MATCHES Darwin)
-    add_definitions(-DBOOST_ASIO_DISABLE_KQUEUE)
-ENDIF(CMAKE_SYSTEM_NAME MATCHES Darwin)
+include_directories(include)
+
+set(SERIAL_SRCS src/serial.cc)
+if(UNIX)
+  list(APPEND SERIAL_SRCS src/impl/unix.cc)
+else(UNIX)
+  list(APPEND SERIAL_SRCS src/impl/windows.cc)
+endif(UNIX)
+list(APPEND SERIAL_SRCS src/serial_listener.cc)
 
 # Build the serial library
-rosbuild_add_library(${PROJECT_NAME} src/serial.cpp include/serial/serial.h)
+rosbuild_add_library(${PROJECT_NAME} ${SERIAL_SRCS})
 
 # Add boost dependencies
 rosbuild_add_boost_directories()
 rosbuild_link_boost(${PROJECT_NAME} system filesystem thread)
 
 # Build example
-rosbuild_add_executable(serial_example examples/serial_example.cpp)
+rosbuild_add_executable(serial_example examples/serial_example.cc)
 target_link_libraries(serial_example ${PROJECT_NAME})
 
+rosbuild_add_executable(serial_listener_example
+  examples/serial_listener_example.cc)
+target_link_libraries(serial_listener_example ${PROJECT_NAME})
+
 endmacro(build_serial)

From 50972cbf411aa2bd786d21838709f87a99b57230 Mon Sep 17 00:00:00 2001
From: John Harrison 
Date: Fri, 13 Jan 2012 11:58:33 -0600
Subject: [PATCH 26/72] Correcting some bad logic and making my test run
 forever to try to judge some basic performance characteristics.

---
 serial.cmake          | 5 +++++
 src/impl/unix.cc      | 6 +++---
 tests/serial_tests.cc | 5 +++--
 3 files changed, 11 insertions(+), 5 deletions(-)

diff --git a/serial.cmake b/serial.cmake
index 04efe70..e7d5e4b 100644
--- a/serial.cmake
+++ b/serial.cmake
@@ -106,12 +106,17 @@ IF(SERIAL_BUILD_TESTS)
 
     # Compile the Serial Listener Test program
     add_executable(serial_listener_tests tests/serial_listener_tests.cc)
+    add_executable(serial_tests tests/serial_tests.cc)
     # Link the Test program to the serial library
     target_link_libraries(serial_listener_tests ${GTEST_BOTH_LIBRARIES}
                           serial)
+    target_link_libraries(serial_tests ${GTEST_BOTH_LIBRARIES}
+                          serial)
+
     # # See: http://code.google.com/p/googlemock/issues/detail?id=146
     # add_definitions(-DGTEST_USE_OWN_TR1_TUPLE=1)
     add_test(AllTestsIntest_serial serial_listener_tests)
+    add_test(AllTestsIntest_serial serial_tests)
 ENDIF(SERIAL_BUILD_TESTS)
 
 ## Setup install and uninstall
diff --git a/src/impl/unix.cc b/src/impl/unix.cc
index 587b5ed..f583eb4 100644
--- a/src/impl/unix.cc
+++ b/src/impl/unix.cc
@@ -226,8 +226,8 @@ Serial::SerialImpl::read (size_t size) {
   fd_set readfds;
   memset(buf, 0, (size + 1) * sizeof(*buf));
   ssize_t bytes_read = 0;
-  while (message.length() < size) {
-    if (timeout_ == -1) {
+  while (bytes_read < size) {
+    if (timeout_ != -1) {
       FD_ZERO(&readfds);
       FD_SET(fd_, &readfds);
       struct timeval timeout;
@@ -244,7 +244,7 @@ Serial::SerialImpl::read (size_t size) {
       }
     }
 
-    if (timeout_ == 1 || FD_ISSET(fd_, &readfds)) {
+    if (timeout_ == -1 || FD_ISSET(fd_, &readfds)) {
       ssize_t newest_read = ::read(fd_,
                                    buf + bytes_read,
                                    size - static_cast(bytes_read));
diff --git a/tests/serial_tests.cc b/tests/serial_tests.cc
index 663a333..bef6352 100644
--- a/tests/serial_tests.cc
+++ b/tests/serial_tests.cc
@@ -16,14 +16,15 @@ using std::endl;
 using serial::Serial;
 
 int main(int argc, char **argv) {
-  Serial s("/dev/tty.usbserial-A900adHq", 9600, 2000);
+  Serial s("/dev/tty.usbserial-A900adHq", 115200, 2000);
   s.flush();
   int count = 0;
-  while (count < 10) {
+  while (1) {
     size_t available = s.available();
     cout << "avialable: " << available << endl;
     string line = s.readline();
     cout << count << ": " << line;
     count++;
   }
+  cout << endl << endl;
 }

From dc53f3d1329ddbbf777a9ba573797dc270fecc75 Mon Sep 17 00:00:00 2001
From: William Woodall 
Date: Fri, 13 Jan 2012 15:39:17 -0600
Subject: [PATCH 27/72] Fixing a warning.

---
 src/impl/unix.cc | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/impl/unix.cc b/src/impl/unix.cc
index f583eb4..77ca79c 100644
--- a/src/impl/unix.cc
+++ b/src/impl/unix.cc
@@ -226,7 +226,7 @@ Serial::SerialImpl::read (size_t size) {
   fd_set readfds;
   memset(buf, 0, (size + 1) * sizeof(*buf));
   ssize_t bytes_read = 0;
-  while (bytes_read < size) {
+  while (bytes_read < (ssize_t)size) {
     if (timeout_ != -1) {
       FD_ZERO(&readfds);
       FD_SET(fd_, &readfds);

From 4afa6e2e7ce0c943a9e9903202b1f9644100d79f Mon Sep 17 00:00:00 2001
From: William Woodall 
Date: Sat, 14 Jan 2012 20:52:50 -0600
Subject: [PATCH 28/72] Fixing Findserial.cmake and updating serial_listener.h
 to remove unused functions.

---
 Findserial.cmake                 |  6 ++-
 include/serial/serial_listener.h | 70 ++++++++++++++++++++------------
 2 files changed, 49 insertions(+), 27 deletions(-)

diff --git a/Findserial.cmake b/Findserial.cmake
index 26877ab..cb839a6 100644
--- a/Findserial.cmake
+++ b/Findserial.cmake
@@ -1,6 +1,8 @@
-find_path(serial_INCLUDE_DIRS serial.h serial_listener.h /usr/include/serial "$ENV{NAMER_ROOT}")
+find_path(serial_INCLUDE_DIRS serial.h serial_listener.h /usr/include/serial 
+          /usr/local/include/serial "$ENV{NAMER_ROOT}")
 
-find_library(serial_LIBRARIES serial /usr/lib "$ENV{NAMER_ROOT}")
+find_library(serial_LIBRARIES serial /usr/lib /usr/local/lib
+             "$ENV{NAMER_ROOT}")
 
 set(serial_FOUND TRUE)
 
diff --git a/include/serial/serial_listener.h b/include/serial/serial_listener.h
index 0aebffa..318ab6f 100644
--- a/include/serial/serial_listener.h
+++ b/include/serial/serial_listener.h
@@ -90,31 +90,6 @@ typedef boost::function DataCallback;
  */
 typedef boost::function ComparatorType;
 
-/*!
- * This function type describes the prototype for the logging callbacks.
- * 
- * The function takes a std::string reference and returns nothing.  It is
- * called from the library when a logging message occurs.  This
- * allows the library user to hook into this and integrate it with their own
- * logging system.  It can be set with any of the setHandler
- * functions.
- * 
- * \see SerialListener::setInfoHandler, SerialListener::setDebugHandler, 
- * SerialListener::setWarningHandler
- */
-typedef boost::function LoggingCallback;
-
-/*!
- * This function type describes the prototype for the exception callback.
- * 
- * The function takes a std::exception reference and returns nothing.  It is 
- * called from the library when an exception occurs in a library thread.
- * This exposes these exceptions to the user so they can to error handling.
- * 
- * \see SerialListener::setExceptionHandler
- */
-typedef boost::function ExceptionCallback;
-
 /*!
  * This function type describes the prototype for the tokenizer callback.
  * 
@@ -138,6 +113,33 @@ typedef boost::function ExceptionCallback;
 typedef boost::function&)>
 TokenizerType;
 
+#if 0
+/*!
+ * This function type describes the prototype for the logging callbacks.
+ * 
+ * The function takes a std::string reference and returns nothing.  It is
+ * called from the library when a logging message occurs.  This
+ * allows the library user to hook into this and integrate it with their own
+ * logging system.  It can be set with any of the setHandler
+ * functions.
+ * 
+ * \see SerialListener::setInfoHandler, SerialListener::setDebugHandler, 
+ * SerialListener::setWarningHandler
+ */
+typedef boost::function LoggingCallback;
+#endif
+
+/*!
+ * This function type describes the prototype for the exception callback.
+ * 
+ * The function takes a std::exception reference and returns nothing.  It is 
+ * called from the library when an exception occurs in a library thread.
+ * This exposes these exceptions to the user so they can to error handling.
+ * 
+ * \see SerialListener::setExceptionHandler
+ */
+typedef boost::function ExceptionCallback;
+
 /*!
  * Represents a filter which new data is passed through.
  * 
@@ -472,6 +474,7 @@ public:
 
 /***** Hooks and Handlers ******/
 
+#if 0
   /*!
    * Sets the handler to be called when a lines is not caught by a filter.
    * 
@@ -579,6 +582,23 @@ public:
   setWarningHandler (LoggingCallback warning_handler) {
     this->warn = warning_handler;
   }
+#endif
+
+/*!
+ * Sets the function to be called when an exception occurs internally.
+ * 
+ * This allows you to hook into the exceptions that occur in threads inside 
+ * the serial listener library.
+ * 
+ * \param exception_handler A function pointer to the callback to handle new 
+ * interal exceptions.
+ * 
+ * \see serial::ExceptionCallback
+ */
+void
+setWarningHandler (ExceptionCallback exception_handler) {
+  this->handle_exc = exception_handler;
+}
 
 /***** Static Functions ******/
 

From ae3e4a1f51def531fbf4dc595e69f30c026c6b95 Mon Sep 17 00:00:00 2001
From: William Woodall 
Date: Sat, 14 Jan 2012 21:01:55 -0600
Subject: [PATCH 29/72] Fixing compile errors with serial listener.

---
 include/serial/serial_listener.h | 2 ++
 src/serial_listener.cc           | 4 ++++
 2 files changed, 6 insertions(+)

diff --git a/include/serial/serial_listener.h b/include/serial/serial_listener.h
index 318ab6f..ba274aa 100644
--- a/include/serial/serial_listener.h
+++ b/include/serial/serial_listener.h
@@ -792,10 +792,12 @@ private:
   // Tokenizer
   TokenizerType tokenize;
 
+#if 0
   // Logging handlers
   LoggingCallback warn;
   LoggingCallback info;
   LoggingCallback debug;
+#endif
 
   // Exception handler
   ExceptionCallback handle_exc;
diff --git a/src/serial_listener.cc b/src/serial_listener.cc
index 85055db..84adc87 100644
--- a/src/serial_listener.cc
+++ b/src/serial_listener.cc
@@ -2,6 +2,7 @@
 
 /***** Inline Functions *****/
 
+#if 0
 inline void defaultWarningCallback(const std::string& msg) {
   std::cout << "SerialListener Warning: " << msg << std::endl;
 }
@@ -13,6 +14,7 @@ inline void defaultDebugCallback(const std::string& msg) {
 inline void defaultInfoCallback(const std::string& msg) {
   std::cout << "SerialListener Info: " << msg << std::endl;
 }
+#endif
 
 inline void defaultExceptionCallback(const std::exception &error) {
   std::cerr << "SerialListener Unhandled Exception: " << error.what();
@@ -36,9 +38,11 @@ SerialListener::default_handler(const std::string &token) {
 SerialListener::SerialListener() : listening(false), chunk_size_(5) {
   // Set default callbacks
   this->handle_exc = defaultExceptionCallback;
+#if 0
   this->info = defaultInfoCallback;
   this->debug = defaultDebugCallback;
   this->warn = defaultWarningCallback;
+#endif
 
   // Default handler stuff
   this->_default_handler = NULL;

From 5ec0707418dce5f0d47316bc79f9913bdd33ecbf Mon Sep 17 00:00:00 2001
From: William Woodall 
Date: Sat, 14 Jan 2012 21:24:48 -0600
Subject: [PATCH 30/72] Changed the stopListening function, now it no longer
 removes filters, just stops listening and cleans the data buffer of partial
 messages.

---
 src/serial_listener.cc | 3 ---
 1 file changed, 3 deletions(-)

diff --git a/src/serial_listener.cc b/src/serial_listener.cc
index 84adc87..599ee88 100644
--- a/src/serial_listener.cc
+++ b/src/serial_listener.cc
@@ -114,9 +114,6 @@ SerialListener::stopListening() {
 
   this->data_buffer = "";
   this->serial_port_ = NULL;
-
-  // Delete all the filters
-  this->removeAllFilters();
 }
 
 size_t

From 8f4b34cc03921be6e5145d7a18fb9186cb9f21f4 Mon Sep 17 00:00:00 2001
From: John Harrison 
Date: Sun, 15 Jan 2012 02:06:38 -0600
Subject: [PATCH 31/72] Adding in an internal buffer to Serial, this is
 independent of the SerialImpl and buffers reads to help performance.

Also correcting my styles to match the style guide and adding in some documentation.
---
 Makefile                         |   5 +-
 README.md                        |   8 +-
 include/serial/impl/unix.h       | 126 +++++++---
 include/serial/serial.h          | 237 +++++++++---------
 include/serial/serial_listener.h | 314 ++++++++++++------------
 serial.cmake                     |   2 +-
 src/impl/unix.cc                 | 403 ++++++++++++++++++-------------
 src/serial.cc                    | 293 +++++++++++++++-------
 src/serial_listener.cc           |   2 +
 tests/serial_tests.cc            |  37 +--
 10 files changed, 827 insertions(+), 600 deletions(-)

diff --git a/Makefile b/Makefile
index 914dad5..c4c5448 100644
--- a/Makefile
+++ b/Makefile
@@ -1,10 +1,9 @@
-# ash_gti's dumb downed makefile so I can more easily test stuff
+# # ash_gti's dumb downed makefile so I can more easily test stuff
 # CXX=clang++
-# CXXFLAGS=-g -I./include
+# CXXFLAGS=-g -I./include -ferror-limit=5 -O3 -Wall -Weffc++ -pedantic -pedantic-errors -Wextra  -Wall -Waggregate-return -Wcast-align -Wcast-qual  -Wchar-subscripts  -Wcomment -Wconversion -Wdisabled-optimization -Wfloat-equal  -Wformat  -Wformat=2 -Wformat-nonliteral -Wformat-security  -Wformat-y2k -Wimplicit  -Wimport  -Winit-self  -Winline -Winvalid-pch   -Wlong-long -Wmissing-braces -Wmissing-field-initializers -Wmissing-format-attribute   -Wmissing-include-dirs -Wmissing-noreturn -Wpacked -Wparentheses  -Wpointer-arith -Wredundant-decls -Wreturn-type -Wsequence-point  -Wshadow -Wsign-compare  -Wstack-protector -Wstrict-aliasing -Wstrict-aliasing=2 -Wswitch  -Wswitch-default -Wswitch-enum -Wtrigraphs  -Wuninitialized -Wunknown-pragmas  -Wunreachable-code -Wunused -Wunused-function  -Wunused-label  -Wunused-parameter -Wunused-value  -Wunused-variable  -Wvariadic-macros -Wvolatile-register-var  -Wwrite-strings
 # 
 # test: tests/serial_tests.o src/serial.o src/impl/unix.o
 # 	$(CXX) -o test tests/serial_tests.o src/serial.o src/impl/unix.o
-# 
 ifdef ROS_ROOT
 include $(shell rospack find mk)/cmake.mk
 else
diff --git a/README.md b/README.md
index ed820fe..201b165 100644
--- a/README.md
+++ b/README.md
@@ -49,13 +49,13 @@ Setup workspace (skip if you already have one):
     rosws init some_ros_workspace
     cd some_ros_workspace
     source setup.bash
-    
+
 Add the rosinstall entry for this stack:
-    
+
     echo "-git: {local-name: serial, uri: 'https://github.com/wjwwood/serial.git', version: 'master'}" >> .rosinstall
-    
+
 Rerun rosinstall:
-    
+
     rosinstall .
     source setup.bash
 
diff --git a/include/serial/impl/unix.h b/include/serial/impl/unix.h
index 0f1801a..1bb1eb3 100644
--- a/include/serial/impl/unix.h
+++ b/include/serial/impl/unix.h
@@ -8,7 +8,7 @@
  *
  * The MIT License
  *
- * Copyright (c) 2011 William Woodall
+ * Copyright (c) 2011 William Woodall, John Harrison
  *
  * Permission is hereby granted, free of charge, to any person obtaining a 
  * copy of this software and associated documentation files (the "Software"),
@@ -30,7 +30,9 @@
  *
  * \section DESCRIPTION
  *
- * This provides a unix based pimpl for the Serial class.
+ * This provides a unix based pimpl for the Serial class. This implementation is
+ * based off termios.h and uses select for multiplexing the IO ports.
+ *
  */
 
 #ifndef SERIAL_IMPL_UNIX_H
@@ -42,6 +44,7 @@ namespace serial {
 
 using std::string;
 using std::invalid_argument;
+
 using serial::SerialExecption;
 using serial::IOException;
 
@@ -57,47 +60,98 @@ public:
 
   virtual ~SerialImpl ();
 
-  void open ();
-  void close () ;
-  bool isOpen () const;
+  void
+  open ();
 
-  size_t available ();
-  string read (size_t size = 1);
-  size_t write (const string &data);
+  void
+  close ();
 
-  void flush ();
-  void flushInput ();
-  void flushOutput ();
+  bool
+  isOpen () const;
 
-  void sendBreak(int duration);
-  void setBreak(bool level);
-  void setRTS(bool level);
-  void setDTR(bool level);
-  bool getCTS();
-  bool getDSR();
-  bool getRI();
-  bool getCD();
+  size_t
+  available ();
 
-  void setPort (const string &port);
-  string getPort () const;
+  size_t
+  read (char* buf, size_t size = 1);
 
-  void setTimeout (long timeout);
-  long getTimeout () const;
+  size_t
+  write (const string &data);
 
-  void setBaudrate (unsigned long baudrate);
-  unsigned long getBaudrate () const;
+  void
+  flush ();
 
-  void setBytesize (bytesize_t bytesize);
-  bytesize_t getBytesize () const;
+  void
+  flushInput ();
 
-  void setParity (parity_t parity);
-  parity_t getParity () const;
+  void
+  flushOutput ();
 
-  void setStopbits (stopbits_t stopbits);
-  stopbits_t getStopbits () const;
+  void
+  sendBreak(int duration);
 
-  void setFlowcontrol (flowcontrol_t flowcontrol);
-  flowcontrol_t getFlowcontrol () const;
+  void
+  setBreak(bool level);
+
+  void
+  setRTS(bool level);
+
+  void
+  setDTR(bool level);
+  
+  bool
+  getCTS();
+  
+  bool
+  getDSR();
+  
+  bool
+  getRI();
+  
+  bool
+  getCD();
+
+  void
+  setPort (const string &port);
+  
+  string
+  getPort () const;
+
+  void
+  setTimeout (long timeout);
+  
+  long
+  getTimeout () const;
+
+  void
+  setBaudrate (unsigned long baudrate);
+  
+  unsigned long
+  getBaudrate () const;
+
+  void
+  setBytesize (bytesize_t bytesize);
+  
+  bytesize_t
+  getBytesize () const;
+
+  void
+  setParity (parity_t parity);
+
+  parity_t
+  getParity () const;
+
+  void
+  setStopbits (stopbits_t stopbits);
+
+  stopbits_t
+  getStopbits () const;
+
+  void
+  setFlowcontrol (flowcontrol_t flowcontrol);
+
+  flowcontrol_t
+  getFlowcontrol () const;
 
 protected:
   void reconfigurePort ();
@@ -106,17 +160,13 @@ private:
   string port_;               // Path to the file descriptor
   int fd_;                    // The current file descriptor
 
-  int interCharTimeout_;
-  int writeTimeout_;
   bool isOpen_;
   bool xonxoff_;
   bool rtscts_;
-  
-  char ___; // lol padding
 
   long timeout_;              // Timeout for read operations
   unsigned long baudrate_;    // Baudrate
-  
+
   parity_t parity_;           // Parity
   bytesize_t bytesize_;       // Size of the bytes
   stopbits_t stopbits_;       // Stop Bits
diff --git a/include/serial/serial.h b/include/serial/serial.h
index d37fb96..473907e 100644
--- a/include/serial/serial.h
+++ b/include/serial/serial.h
@@ -10,7 +10,7 @@
  *
  * Copyright (c) 2011 William Woodall
  *
- * Permission is hereby granted, free of charge, to any person obtaining a 
+ * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
  * to deal in the Software without restriction, including without limitation
  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
@@ -24,8 +24,8 @@
  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
- * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  * DEALINGS IN THE SOFTWARE.
  *
  * \section DESCRIPTION
@@ -124,35 +124,38 @@ class Serial {
 public:
   /*!
   * Constructor, creates a SerialPortBoost object and opens the port.
-  * 
+  *
   * \param port A std::string containing the address of the serial port,
   *        which would be something like 'COM1' on Windows and '/dev/ttyS0'
   *        on Linux.
-  * 
+  *
   * \param baudrate An integer that represents the buadrate
-  * 
-  * \param timeout A long that represents the time (in milliseconds) until a 
+  *
+  * \param timeout A long that represents the time (in milliseconds) until a
   * timeout on reads occur. Setting this to zero (0) will cause reading to
   * be non-blocking, i.e. the available data will be returned immediately,
   * but it will not block to wait for more. Setting this to a number less
   * than zero (-1) will result in infinite blocking behaviour, i.e. the
   * serial port will block until either size bytes have been read or an
   * exception has occured.
-  * 
-  * \param bytesize Size of each byte in the serial transmission of data, 
-  * default is EIGHTBITS, possible values are: FIVEBITS, SIXBITS, SEVENBITS, 
+  *
+  * \param bytesize Size of each byte in the serial transmission of data,
+  * default is EIGHTBITS, possible values are: FIVEBITS, SIXBITS, SEVENBITS,
   * EIGHTBITS
-  * 
+  *
   * \param parity Method of parity, default is PARITY_NONE, possible values
   * are: PARITY_NONE, PARITY_ODD, PARITY_EVEN
-  * 
-  * \param stopbits Number of stop bits used, default is STOPBITS_ONE, 
+  *
+  * \param stopbits Number of stop bits used, default is STOPBITS_ONE,
   * possible values are: STOPBITS_ONE, STOPBITS_ONE_POINT_FIVE, STOPBITS_TWO
-  * 
-  * \param flowcontrol Type of flowcontrol used, default is  
-  * FLOWCONTROL_NONE, possible values are: FLOWCONTROL_NONE, 
+  *
+  * \param flowcontrol Type of flowcontrol used, default is
+  * FLOWCONTROL_NONE, possible values are: FLOWCONTROL_NONE,
   * FLOWCONTROL_SOFTWARE, FLOWCONTROL_HARDWARE
-  * 
+  *
+  * \param buffer_size The maximum size of the internal buffer, defaults
+  * to 256 bytes (2^8).
+  *
   * \throw PortNotOpenedException
   */
   Serial (const std::string &port = "",
@@ -161,7 +164,8 @@ public:
           bytesize_t bytesize = EIGHTBITS,
           parity_t parity = PARITY_NONE,
           stopbits_t stopbits = STOPBITS_ONE,
-          flowcontrol_t flowcontrol = FLOWCONTROL_NONE);
+          flowcontrol_t flowcontrol = FLOWCONTROL_NONE,
+          const size_t buffer_size = 256);
 
   /*! Destructor */
   virtual ~Serial ();
@@ -169,12 +173,12 @@ public:
   /*!
   * Opens the serial port as long as the portname is set and the port isn't
   * alreay open.
-  * 
-  * If the port is provided to the constructor then an explicit call to open 
+  *
+  * If the port is provided to the constructor then an explicit call to open
   * is not needed.
-  * 
+  *
   * \see Serial::Serial
-  * 
+  *
   * \throw std::invalid_argument
   * \throw serial::SerialExecption
   * \throw serial::IOException
@@ -183,7 +187,7 @@ public:
   open ();
 
   /*! Gets the open status of the serial port.
-  * 
+  *
   * \return Returns true if the port is open, false otherwise.
   */
   bool
@@ -198,70 +202,48 @@ public:
   available();
 
   /*! Read a given amount of bytes from the serial port.
-  * 
+  *
   * If a timeout is set it may return less characters than requested. With
   * no timeout it will block until the requested number of bytes have been
   * read or until an exception occurs.
-  * 
-  * \param buffer An unsigned char array large enough to hold incoming data 
-  * up to the requested size.
-  * 
+  *
   * \param size A size_t defining how many bytes to be read.
-  * 
-  * \return A size_t representing the number of bytes actually read.
-  */
-  //size_t
-  //read (unsigned char* buffer, size_t size = 1);
-
-  /*! Read a given amount of bytes from the serial port.
-  * 
-  * If a timeout is set it may return less characters than requested. With
-  * no timeout it will block until the requested number of bytes have been
-  * read or until an exception occurs.
-  * 
-  * \param size A size_t defining how many bytes to be read.
-  * 
+  *
   * \return A std::string containing the data read.
   */
   std::string
   read (size_t size = 1);
 
-  std::string readline(size_t size = std::numeric_limits::max(),
-                       std::string eol = "\n");
-
-  std::vector readlines(std::string eol = "\n");
-
-  /*! Read a given amount of bytes from the serial port.
-  * 
-  * Reads into a std::string by reference rather than returning it.
-  * 
-  * \param buffer A std::string reference for reading to.
-  * \param size A size_t defining how many bytes to be read.
-  * 
-  * \return A size_t that represents how many bytes were read.
-  * 
-  * \see Serial::read(size_t)
+  /*! Reads in a line or until a given delimiter has been processed
+  *
+  * Reads from the serial port until a single line has been read.
+  *
+  * \param size A maximum length of a line defaults to size_t::max()
+  * \param eol A string to match against for the EOL.
+  *
+  * \return A std::string containing the line.
   */
-  //size_t
-  //read (std::string &buffer, size_t size = 1);
+  std::string
+  readline(size_t      size = std::numeric_limits::max(),
+           std::string eol  = "\n");
 
-  /*! Write bytes from the data to the serial port by given length.
-  * 
-  * \param data An unsigned char array containing data to be written to the
-  * serial port.
-  * 
-  * \param length A size_t representing the number of bytes to be written.
-  * 
-  * \return A size_t representing the number of bytes actually written.
+  /*! Reads in multiple lines until the serail port times out.
+  *
+  * This requires a timeout > 0 before it can be run. It will read until a
+  * timeout occurs and return a list of strings.
+  *
+  * \param eol A string to match against for the EOL.
+  *
+  * \return A vector containing the lines.
   */
-  //size_t
-  //write (unsigned char* data, size_t length);
+  std::vector
+  readlines(std::string eol = "\n");
 
   /*! Write a string to the serial port.
-  * 
-  * \param data A const std::string reference containg the data to be written 
+  *
+  * \param data A const std::string reference containg the data to be written
   * to the serial port.
-  * 
+  *
   * \return A size_t representing the number of bytes actually written to
   * the serial port.
   */
@@ -269,27 +251,27 @@ public:
   write (const std::string &data);
 
   /*! Sets the serial port identifier.
-  * 
-  * \param port A const std::string reference containing the address of the 
-  * serial port, which would be something like 'COM1' on Windows and 
+  *
+  * \param port A const std::string reference containing the address of the
+  * serial port, which would be something like 'COM1' on Windows and
   * '/dev/ttyS0' on Linux.
-  * 
+  *
   * \throw InvalidConfigurationException
   */
   void
   setPort (const std::string &port);
 
   /*! Gets the serial port identifier.
-  * 
+  *
   * \see Serial::setPort
-  * 
+  *
   * \throw InvalidConfigurationException
   */
   std::string
   getPort () const;
 
   /*! Sets the timeout for reads in milliseconds.
-  * 
+  *
   * \param timeout A long that represents the time (in milliseconds) until a
   * timeout on reads occur.  Setting this to zero (0) will cause reading to be
   * non-blocking, i.e. the available data will be returned immediately, but it
@@ -302,127 +284,151 @@ public:
   setTimeout (long timeout);
 
   /*! Gets the timeout for reads in seconds.
-  * 
+  *
   * \see Serial::setTimeout
   */
   long
   getTimeout () const;
 
   /*! Sets the baudrate for the serial port.
-  * 
+  *
   * Possible baudrates depends on the system but some safe baudrates include:
   * 110, 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 56000,
   * 57600, 115200
   * Some other baudrates that are supported by some comports:
   * 128000, 153600, 230400, 256000, 460800, 921600
-  * 
+  *
   * \param baudrate An integer that sets the baud rate for the serial port.
-  * 
+  *
   * \throw InvalidConfigurationException
   */
   void
   setBaudrate (unsigned long baudrate);
 
   /*! Gets the baudrate for the serial port.
-  * 
+  *
   * \return An integer that sets the baud rate for the serial port.
-  * 
+  *
   * \see Serial::setBaudrate
-  * 
+  *
   * \throw InvalidConfigurationException
   */
   unsigned long
   getBaudrate () const;
 
   /*! Sets the bytesize for the serial port.
-  * 
+  *
   * \param bytesize Size of each byte in the serial transmission of data,
   * default is EIGHTBITS, possible values are: FIVEBITS, SIXBITS, SEVENBITS,
   * EIGHTBITS
-  * 
+  *
   * \throw InvalidConfigurationException
   */
   void
   setBytesize (bytesize_t bytesize);
 
   /*! Gets the bytesize for the serial port.
-  * 
+  *
   * \see Serial::setBytesize
-  * 
+  *
   * \throw InvalidConfigurationException
   */
   bytesize_t
   getBytesize () const;
 
   /*! Sets the parity for the serial port.
-  * 
+  *
   * \param parity Method of parity, default is PARITY_NONE, possible values
   * are: PARITY_NONE, PARITY_ODD, PARITY_EVEN
-  * 
+  *
   * \throw InvalidConfigurationException
   */
   void
   setParity (parity_t parity);
 
   /*! Gets the parity for the serial port.
-  * 
+  *
   * \see Serial::setParity
-  * 
+  *
   * \throw InvalidConfigurationException
   */
   parity_t
   getParity () const;
 
   /*! Sets the stopbits for the serial port.
-  * 
+  *
   * \param stopbits Number of stop bits used, default is STOPBITS_ONE,
   * possible values are: STOPBITS_ONE, STOPBITS_ONE_POINT_FIVE, STOPBITS_TWO
-  * 
+  *
   * \throw InvalidConfigurationException
   */
   void
   setStopbits (stopbits_t stopbits);
 
   /*! Gets the stopbits for the serial port.
-  * 
+  *
   * \see Serial::setStopbits
-  * 
+  *
   * \throw InvalidConfigurationException
   */
   stopbits_t
   getStopbits () const;
 
   /*! Sets the flow control for the serial port.
-  * 
+  *
   * \param flowcontrol Type of flowcontrol used, default is FLOWCONTROL_NONE,
   * possible values are: FLOWCONTROL_NONE, FLOWCONTROL_SOFTWARE,
   * FLOWCONTROL_HARDWARE
-  * 
+  *
   * \throw InvalidConfigurationException
   */
   void
   setFlowcontrol (flowcontrol_t flowcontrol);
 
   /*! Gets the flow control for the serial port.
-  * 
+  *
   * \see Serial::setFlowcontrol
-  * 
+  *
   * \throw InvalidConfigurationException
   */
   flowcontrol_t
   getFlowcontrol () const;
 
-  void flush();
-  void flushInput();
-  void flushOutput();
-  void sendBreak(int duration);
-  void setBreak(bool level = true);
-  void setRTS(bool level = true);
-  void setDTR(bool level = true);
-  bool getCTS();
-  bool getDSR();
-  bool getRI();
-  bool getCD();
+  /*! Flush the input and output buffers */
+  void
+  flush ();
+
+  /*! Flush only the input buffer */
+  void
+  flushInput ();
+
+  /*! Flush only the output buffer */
+  void
+  flushOutput ();
+
+  void
+  sendBreak (int duration);
+
+  void
+  setBreak (bool level = true);
+
+  void
+  setRTS (bool level = true);
+
+  void
+  setDTR (bool level = true);
+
+  bool
+  getCTS ();
+
+  bool
+  getDSR ();
+
+  bool
+  getRI ();
+
+  bool
+  getCD ();
 
 private:
   // Disable copy constructors
@@ -430,9 +436,12 @@ private:
   void operator=(const Serial&);
   const Serial& operator=(Serial);
 
+  const size_t buffer_size_;
+  char *read_cache_; //!< Cache for doing reads in chunks.
+
   // Pimpl idiom, d_pointer
   class SerialImpl;
-  SerialImpl *pimpl;
+  SerialImpl *pimpl_;
 };
 
 } // namespace serial
diff --git a/include/serial/serial_listener.h b/include/serial/serial_listener.h
index 0aebffa..781acbb 100644
--- a/include/serial/serial_listener.h
+++ b/include/serial/serial_listener.h
@@ -9,11 +9,11 @@
  *
  * Copyright (c) 2011 William Woodall
  *
- * Permission is hereby granted, free of charge, to any person obtaining a 
- * copy of this software and associated documentation files (the "Software"), 
- * to deal in the Software without restriction, including without limitation 
- * the rights to use, copy, modify, merge, publish, distribute, sublicense, 
- * and/or sell copies of the Software, and to permit persons to whom the 
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
  * Software is furnished to do so, subject to the following conditions:
  *
  * The above copyright notice and this permission notice shall be included in
@@ -23,14 +23,14 @@
  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
- * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  * DEALINGS IN THE SOFTWARE.
  *
  * \section DESCRIPTION
  *
  * This provides a class that allows for asynchronous serial port reading.
- * 
+ *
  */
 
 #ifndef SERIAL_LISTENER_H
@@ -53,14 +53,14 @@ namespace serial {
 
 /*!
  * This is an alias to boost::shared_ptr used for tokens.
- * 
+ *
  * This is the type used internally and is the type returned in a vector by
  * the tokenizer.  The shared_ptr allows for the token to be stored and kept
  * around long enough to be used by the comparators and callbacks, but no
  * longer.  This internal storage is passed as a const std::string reference
  * to callbacks, like the DataCallback function type, to prevent implicit
  * copying.
- * 
+ *
  * \see serial::TokenizerType, serial::SerialListener::setTokenizer
  */
 typedef boost::shared_ptr TokenPtr;
@@ -69,11 +69,11 @@ typedef boost::shared_ptr TokenPtr;
  * This is a general function type that is used as the callback prototype
  * for asynchronous functions like the default handler callback and the
  * listenFor callbacks.
- * 
+ *
  * The function takes a std::string reference and returns nothing, it is
  * simply passing the resulting line detected by the comparator to the user's
  * callback for processing.
- * 
+ *
  * \see SerialListener::listenFor, SerialListener::setDefaultHandler
  */
 typedef boost::function DataCallback;
@@ -81,57 +81,57 @@ typedef boost::function DataCallback;
 /*!
  * This is a general function type that is used as the comparator callback
  * prototpe for the listenFor* type functions.
- * 
+ *
  * The function takes a std::string reference and returns true if the string
  * matches what the comparator is looking for and false if it does not, unless
  * otherwise specified.
- * 
+ *
  * \see SerialListener::listenFor, SerialListener::listenForOnce
  */
 typedef boost::function ComparatorType;
 
 /*!
  * This function type describes the prototype for the logging callbacks.
- * 
+ *
  * The function takes a std::string reference and returns nothing.  It is
  * called from the library when a logging message occurs.  This
  * allows the library user to hook into this and integrate it with their own
  * logging system.  It can be set with any of the setHandler
  * functions.
- * 
- * \see SerialListener::setInfoHandler, SerialListener::setDebugHandler, 
+ *
+ * \see SerialListener::setInfoHandler, SerialListener::setDebugHandler,
  * SerialListener::setWarningHandler
  */
 typedef boost::function LoggingCallback;
 
 /*!
  * This function type describes the prototype for the exception callback.
- * 
- * The function takes a std::exception reference and returns nothing.  It is 
+ *
+ * The function takes a std::exception reference and returns nothing.  It is
  * called from the library when an exception occurs in a library thread.
  * This exposes these exceptions to the user so they can to error handling.
- * 
+ *
  * \see SerialListener::setExceptionHandler
  */
 typedef boost::function ExceptionCallback;
 
 /*!
  * This function type describes the prototype for the tokenizer callback.
- * 
+ *
  * The function should take a std::string reference and tokenize it into a
- * several TokenPtr's and store them in the given std::vector 
+ * several TokenPtr's and store them in the given std::vector
  * reference.  There are some default ones or the user can create their own.
- * 
+ *
  * The last element in the std::vector of TokenPtr's should always be
  * either an empty string ("") or the last partial message.  The last element
  * in the std::vector will be put back into the data buffer so that if it is
  * incomplete it can be completed when more data is read.
- * 
+ *
  * Example: A delimeter tokenizer with a delimeter of "\r".  The result would
  * be: "msg1\rmsg2\r" -> ["msg1", "msg2", ""] for two complete messages, or:
- * "msg1\rpartial_msg2" -> ["msg1","partial_msg2"] for one complete message 
+ * "msg1\rpartial_msg2" -> ["msg1","partial_msg2"] for one complete message
  * and one partial message.
- * 
+ *
  * \see SerialListener::setTokenizer, serial::delimeter_tokenizer,
  * serial::TokenPtr
  */
@@ -140,19 +140,19 @@ TokenizerType;
 
 /*!
  * Represents a filter which new data is passed through.
- * 
- * The filter consists of a comparator and a callback.  The comparator takes a 
- * token and returns true if it matches, false if it doesn't.  If a match 
- * occurs the serial listener will dispatch a call of the callback with the 
- * matched data in a another thread.  The comparator should be as short as 
- * possible, but the callback can be longer since it is executed in a thread 
+ *
+ * The filter consists of a comparator and a callback.  The comparator takes a
+ * token and returns true if it matches, false if it doesn't.  If a match
+ * occurs the serial listener will dispatch a call of the callback with the
+ * matched data in a another thread.  The comparator should be as short as
+ * possible, but the callback can be longer since it is executed in a thread
  * or thread pool.
- * 
- * \param comparator A ComparatorType that matches incoming data, returns true 
+ *
+ * \param comparator A ComparatorType that matches incoming data, returns true
  * for a match, false othewise.
- * 
+ *
  * \param callback A DataCallback that gets called when a match occurs.
- * 
+ *
  * \see serial::ComparatorType, serial::DataCallback, serial::FilterPtr
  */
 class Filter
@@ -168,11 +168,11 @@ public:
 
 /*!
  * This is an alias to boost::shared_ptr used for tokens.
- * 
+ *
  * This is used internally and is returned from SerialListener::listenFor like
  * functions so that users can later remove those filters by passing the
  * FilterPtr.
- * 
+ *
  * \see serial::Filter, serial::SerialListener::listenFor,
  * serial::SerialListener::listenForOnce
  */
@@ -183,7 +183,7 @@ class BlockingFilter;
 /*!
  * Shared Pointer of BlockingFilter, returned by
  * SerialListener::createBlockingFilter.
- * 
+ *
  * \see serial::BlockingFilter, SerialListener::createBlockingFilter
  */
 typedef boost::shared_ptr BlockingFilterPtr;
@@ -193,16 +193,16 @@ class BufferedFilter;
 /*!
  * Shared Pointer of BufferedFilter, returned by
  * SerialListener::createBufferedFilter.
- * 
+ *
  * \see serial::BufferedFilter, SerialListener::createBufferedFilter
  */
 typedef boost::shared_ptr BufferedFilterPtr;
 
 /*!
  * This is a general exception generated by the SerialListener class.
- * 
+ *
  * Check the SerialListenerException::what function for the cause.
- * 
+ *
  * \param e_what is a std::string that describes the cause of the error.
  */
 class SerialListenerException : public std::exception {
@@ -275,11 +275,11 @@ public:
     popped_value=the_queue.front();
     the_queue.pop();
   }
-  
+
   size_t size() const {
     return the_queue.size();
   }
-  
+
   void cancel() {
     the_condition_variable.notify_one();
   }
@@ -312,14 +312,14 @@ public:
 
   /*!
    * Sets the tokenizer to be used when tokenizing the data into tokens.
-   * 
-   * This function is given a std::string of data and is responsible for 
+   *
+   * This function is given a std::string of data and is responsible for
    * tokenizing that data into a std::vector of data tokens.
    * The default tokenizer splits the data by the ascii return carriage.
    * The user can create their own tokenizer or use one of the default ones.
-   * 
+   *
    * \param tokenizer Function for tokenizing the incoming data.
-   * 
+   *
    * \see serial::TokenizerType, serial::delimeter_tokenizer
    */
   void
@@ -329,7 +329,7 @@ public:
 
   /*!
    * Sets the number of bytes to be read at a time by the listener.
-   * 
+   *
    * \param chunk_size Number of bytes to be read at a time.
    */
   void
@@ -341,8 +341,8 @@ public:
 
   /*!
    * Starts a thread to listen for messages and process them through filters.
-   * 
-   * \param serial_port Pointer to a serial::Serial object that is used to 
+   *
+   * \param serial_port Pointer to a serial::Serial object that is used to
    * retrieve new data.
    */
   void
@@ -350,8 +350,8 @@ public:
 
   /*!
    * Stops the listening thread and blocks until it completely stops.
-   * 
-   * This function also clears all of the active filters from listenFor and 
+   *
+   * This function also clears all of the active filters from listenFor and
    * similar functions.
    */
   void
@@ -361,21 +361,21 @@ public:
 
   /*!
    * Creates a filter that calls a callback when the comparator returns true.
-   * 
+   *
    * The user provides a comparator and a callback, and every time a line is
    * received the comparator is called and the comparator has to evaluate the
    * line and return true if it matches and false if it doesn't.  If it does
    * match, the callback is called with the resulting line.
-   * 
+   *
    * \param comparator This is a comparator for detecting if a line matches.
    * The comparartor receives a std::string reference and must return a true
    * if it matches and false if it doesn't.
-   * 
+   *
    * \param callback This is the handler for when a match occurs. It is given
    * a std::string reference of the line that matched your comparator.
-   * 
+   *
    * \return boost::shared_ptr so you can remove it later.
-   * 
+   *
    * \see SerialListener::removeFilter
    */
   FilterPtr
@@ -383,21 +383,21 @@ public:
 
   /*!
    * Creates a BlockingFilter which blocks until the comparator returns true.
-   * 
+   *
    * The user provides a comparator, and every time a line is
    * received the comparator is called and the comparator has to evaluate the
    * line and return true if it matches and false if it doesn't.  If it does
    * match, any threads that have called BlockingFilter::wait will be
    * notified.  The BlockingFilter will remove itself when its destructor is
-   * called, i.e. when it leaves the scope, so in those cases an explicit call 
+   * called, i.e. when it leaves the scope, so in those cases an explicit call
    * to SerialListener::removeFilter is not needed.
-   * 
+   *
    * \param comparator This is a comparator for detecting if a line matches.
    * The comparartor receives a std::string reference and must return a true
    * if it matches and false if it doesn't.
-   * 
+   *
    * \return BlockingFilterPtr So you can call BlockingFilter::wait on it.
-   * 
+   *
    * \see SerialListener::removeFilter, serial::BlockingFilter,
    * serial::BlockingFilterPtr
    */
@@ -406,24 +406,24 @@ public:
 
   /*!
    * Creates a BlockingFilter blocks until the comparator returns true.
-   * 
+   *
    * The user provides a comparator, and every time a line is
    * received the comparator is called and the comparator has to evaluate the
    * line and return true if it matches and false if it doesn't.  If it does
    * match, any threads that have called BlockingFilter::wait will be
    * notified.  The BlockingFilter will remove itself when its destructor is
-   * called, i.e. when it leaves the scope, so in those cases an explicit call 
+   * called, i.e. when it leaves the scope, so in those cases an explicit call
    * to SerialListener::removeFilter is not needed.
-   * 
+   *
    * \param comparator This is a comparator for detecting if a line matches.
    * The comparartor receives a std::string reference and must return a true
    * if it matches and false if it doesn't.
-   * 
+   *
    * \param buffer_size This is the number of tokens to be buffered by the
    * BufferedFilter, defaults to 1024.
-   * 
+   *
    * \return BlockingFilter So you can call BlockingFilter::wait on it.
-   * 
+   *
    * \see SerialListener::removeFilter, serial::BufferedFilter,
    * serial::BufferedFilterPtr
    */
@@ -432,9 +432,9 @@ public:
 
   /*!
    * Removes a filter by a given FilterPtr.
-   * 
+   *
    * \param filter_ptr A shared pointer to the filter to be removed.
-   * 
+   *
    * \see SerialListener::createFilter
    */
   void
@@ -442,11 +442,11 @@ public:
 
   /*!
    * Removes a BlockingFilter.
-   * 
+   *
    * The BlockingFilter will remove itself if the destructor is called.
-   * 
+   *
    * \param blocking_filter A BlockingFilter to be removed.
-   * 
+   *
    * \see SerialListener::createBlockingFilter
    */
   void
@@ -454,11 +454,11 @@ public:
 
   /*!
    * Removes a BufferedFilter.
-   * 
+   *
    * The BufferedFilter will remove itself if the destructor is called.
-   * 
+   *
    * \param buffered_filter A BufferedFilter to be removed.
-   * 
+   *
    * \see SerialListener::createBufferedFilter
    */
   void
@@ -474,15 +474,15 @@ public:
 
   /*!
    * Sets the handler to be called when a lines is not caught by a filter.
-   * 
-   * This allows you to set a catch all function that will get called 
+   *
+   * This allows you to set a catch all function that will get called
    * everytime a line is not matched by a filter and the ttl expires.
-   * 
+   *
    * Setting the callbacks works just like SerialListener::setInfoHandler.
-   * 
-   * \param default_handler A function pointer to the callback to handle 
+   *
+   * \param default_handler A function pointer to the callback to handle
    * unmatched and expired messages.
-   * 
+   *
    * \see serial::DataCallback, SerialListener::setInfoHandler
    */
   void
@@ -492,10 +492,10 @@ public:
 
   /*!
    * Sets the function to be called when an info logging message occurs.
-   * 
+   *
    * This allows you to hook into the message reporting of the library and use
    * your own logging facilities.
-   * 
+   *
    * The provided function must follow this prototype:
    * 
    *    void yourInfoCallback(const std::string &msg)
@@ -514,9 +514,9 @@ public:
    * Alternatively you can use a class method as a callback using boost::bind:
    * 
    *    #include 
-   *    
+   *
    *    #include "serial/serial_listener.h"
-   *    
+   *
    *    class MyClass
    *    {
    *    public:
@@ -524,55 +524,55 @@ public:
    *      listener.setInfoHandler(
    *          boost::bind(&MyClass::handleInfo, this, _1));
    *     }
-   *    
+   *
    *     void handleInfo(const std::string &msg) {
    *       std::cout << "MyClass Info: " << msg << std::endl;
    *     }
-   *    
+   *
    *    private:
    *     serial::SerialListener listener;
    *    };
    * 
- * - * \param info_handler A function pointer to the callback to handle new + * + * \param info_handler A function pointer to the callback to handle new * Info messages. - * + * * \see serial::LoggingCallback */ void setInfoHandler (LoggingCallback info_handler) { this->info = info_handler; } - + /*! * Sets the function to be called when a debug logging message occurs. - * + * * This allows you to hook into the message reporting of the library and use * your own logging facilities. - * + * * This works just like SerialListener::setInfoHandler. - * - * \param debug_handler A function pointer to the callback to handle new + * + * \param debug_handler A function pointer to the callback to handle new * Debug messages. - * + * * \see serial::LoggingCallback, SerialListener::setInfoHandler */ void setDebugHandler (LoggingCallback debug_handler) { this->debug = debug_handler; } - + /*! * Sets the function to be called when a warning logging message occurs. - * + * * This allows you to hook into the message reporting of the library and use * your own logging facilities. - * + * * This works just like SerialListener::setInfoHandler. - * - * \param warning_handler A function pointer to the callback to handle new + * + * \param warning_handler A function pointer to the callback to handle new * Warning messages. - * + * * \see serial::LoggingCallback, SerialListener::setInfoHandler */ void @@ -584,7 +584,7 @@ public: /*! * Sleeps for a given number of milliseconds. - * + * * \param milliseconds number of milliseconds to sleep. */ static void @@ -595,21 +595,21 @@ public: /*! * This returns a tokenizer that splits on a given delimeter. - * - * The delimeter is passed into the function and a TokenizerType is returned + * + * The delimeter is passed into the function and a TokenizerType is returned * that can be passed to SerialListener::setTokenizer. - * + * * Example: *
    *   my_listener.setTokenizer(SerialListener::delimeter_tokenizer("\r"));
    * <\pre>
-   * 
-   * \param delimeter A std::string that is used as a delimeter when 
+   *
+   * \param delimeter A std::string that is used as a delimeter when
    * tokenizing data.
-   * 
-   * \return TokenizerType A tokenizer function type that can be passed to 
+   *
+   * \return TokenizerType A tokenizer function type that can be passed to
    * SerialListener::setTokenizer.
-   * 
+   *
    * \see SerialListener::setTokenizer, serial::TokenizerType
    */
   static TokenizerType
@@ -620,22 +620,22 @@ public:
 
   /*!
    * This returns a comparator that matches only the exact string given.
-   * 
+   *
    * This can be used with listenFor or listenForOnce:
-   * 
+   *
    * Example:
    * 
    *   my_listener.listenFor(SerialListener::exactly("my_string"),
    *                         my_callback);
    * <\pre>
-   * 
-   * \param exact_str A std::string that is used as the exact string to match 
+   *
+   * \param exact_str A std::string that is used as the exact string to match
    * when comparing tokens for matching.
-   * 
-   * \return ComparatorType A comparator function type that can be passed to 
+   *
+   * \return ComparatorType A comparator function type that can be passed to
    * SerialListener::listenFor or SerialListener::listenForOnce.
    *
-   * \see SerialListener::listenFor, SerialListener::listenForOnce, 
+   * \see SerialListener::listenFor, SerialListener::listenForOnce,
    * serial::ComparatorType
    */
   static ComparatorType
@@ -645,21 +645,21 @@ public:
 
   /*!
    * This returns a comparator that looks for a given prefix.
-   * 
+   *
    * This can be used with listenFor or listenForOnce:
-   * 
+   *
    * Example:
    * 
    *   my_listener.listenFor(SerialListener::startsWith("V="), my_callback);
    * <\pre>
-   * 
-   * \param prefix A std::string that is used as the prefix string to match 
+   *
+   * \param prefix A std::string that is used as the prefix string to match
    * when comparing tokens for matching.
-   * 
-   * \return ComparatorType A comparator function type that can be passed to 
+   *
+   * \return ComparatorType A comparator function type that can be passed to
    * SerialListener::listenFor or SerialListener::listenForOnce.
    *
-   * \see SerialListener::listenFor, SerialListener::listenForOnce, 
+   * \see SerialListener::listenFor, SerialListener::listenForOnce,
    * serial::ComparatorType
    */
   static ComparatorType
@@ -669,21 +669,21 @@ public:
 
   /*!
    * This returns a comparator that looks for a given postfix.
-   * 
+   *
    * This can be used with listenFor or listenForOnce:
-   * 
+   *
    * Example:
    * 
    *   my_listener.listenFor(SerialListener::endsWith(";"), my_callback);
    * <\pre>
-   * 
-   * \param postfix A std::string that is used as the postfix string to match 
+   *
+   * \param postfix A std::string that is used as the postfix string to match
    * when comparing tokens for matching.
-   * 
-   * \return ComparatorType A comparator function type that can be passed to 
+   *
+   * \return ComparatorType A comparator function type that can be passed to
    * SerialListener::listenFor or SerialListener::listenForOnce.
    *
-   * \see SerialListener::listenFor, SerialListener::listenForOnce, 
+   * \see SerialListener::listenFor, SerialListener::listenForOnce,
    * serial::ComparatorType
    */
   static ComparatorType
@@ -693,22 +693,22 @@ public:
 
   /*!
    * This returns a comparator that looks for a given substring in the token.
-   * 
+   *
    * This can be used with listenFor or listenForOnce:
-   * 
+   *
    * Example:
    * 
    *   my_listener.listenFor(SerialListener::contains("some string"),
    *                         my_callback);
    * <\pre>
-   * 
-   * \param substr A std::string that is used as the search substring to match 
+   *
+   * \param substr A std::string that is used as the search substring to match
    * when comparing tokens for matching.
-   * 
-   * \return ComparatorType A comparator function type that can be passed to 
+   *
+   * \return ComparatorType A comparator function type that can be passed to
    * SerialListener::listenFor or SerialListener::listenForOnce.
    *
-   * \see SerialListener::listenFor, SerialListener::listenForOnce, 
+   * \see SerialListener::listenFor, SerialListener::listenForOnce,
    * serial::ComparatorType
    */
   static ComparatorType
@@ -809,13 +809,13 @@ private:
 };
 
 /*!
- * This is the a filter that provides a wait function for blocking until a 
+ * This is the a filter that provides a wait function for blocking until a
  * match is found.
- * 
- * This should probably not be created manually, but instead should be 
+ *
+ * This should probably not be created manually, but instead should be
  * constructed using SerialListener::createBlockingFilter(ComparatorType)
  * function which returns a BlockingFilter instance.
- * 
+ *
  * \see serial::SerialListener::ComparatorType,
  * serial::SerialListener::createBlockingFilter
  */
@@ -837,9 +837,9 @@ public:
   /*!
    * Waits a given number of milliseconds or until a token is matched.  If a
    * token is matched it is returned, otherwise an empty string is returned.
-   * 
+   *
    * \param ms Time in milliseconds to wait on a new token.
-   * 
+   *
    * \return std::string token that was matched or "" if none were matched.
    */
  std::string wait(long ms) {
@@ -868,22 +868,22 @@ private:
  * This is the a filter that provides a wait function for blocking until a
  * match is found.  It will also buffer up to a given buffer size of tokens so
  * that they can be counted or accessed after they are matched by the filter.
- * 
- * This should probably not be created manually, but instead should be 
+ *
+ * This should probably not be created manually, but instead should be
  * constructed using SerialListener::createBufferedFilter(ComparatorType)
  * function which returns a BufferedFilter instance.
- * 
+ *
  * The internal buffer is a circular queue buffer, so when the buffer is full,
  * the oldest token is dropped and the new one is added.  Additionally, when
  * wait is a called the oldest available token is returned.
- * 
+ *
  * \see serial::SerialListener::ComparatorType,
  * serial::SerialListener::createBufferedFilter
  */
 class BufferedFilter
 {
 public:
-  BufferedFilter (ComparatorType comparator, size_t buffer_size, 
+  BufferedFilter (ComparatorType comparator, size_t buffer_size,
                   SerialListener &listener)
   : buffer_size_(buffer_size)
   {
@@ -899,14 +899,14 @@ public:
   }
 
   /*!
-   * Waits a given number of milliseconds or until a matched token is 
+   * Waits a given number of milliseconds or until a matched token is
    * available in the buffer.  If a token is matched it is returned, otherwise
    * an empty string is returned.
-   * 
-   * \param ms Time in milliseconds to wait on a new token.  If ms is set to 0 
-   * then it will try to get a new token if one is available but will not 
+   *
+   * \param ms Time in milliseconds to wait on a new token.  If ms is set to 0
+   * then it will try to get a new token if one is available but will not
    * block.
-   * 
+   *
    * \return std::string token that was matched or "" if none were matched.
    */
   std::string wait(long ms) {
diff --git a/serial.cmake b/serial.cmake
index e7d5e4b..f6d81a6 100644
--- a/serial.cmake
+++ b/serial.cmake
@@ -15,7 +15,7 @@ IF(EXISTS /usr/bin/clang)
   set(CMAKE_CXX_COMPILER /usr/bin/clang++)
   set(CMAKE_OSX_DEPLOYMENT_TARGET "")
   # set(CMAKE_CXX_FLAGS "-ferror-limit=5 -std=c++0x -stdlib=libc++")
-  set(CMAKE_CXX_FLAGS "-ferror-limit=5 -m64 -Wall -Weffc++ -pedantic -pedantic-errors -Wextra  -Wall -Waggregate-return -Wcast-align -Wcast-qual  -Wchar-subscripts  -Wcomment -Wconversion -Wdisabled-optimization -Wfloat-equal  -Wformat  -Wformat=2 -Wformat-nonliteral -Wformat-security  -Wformat-y2k -Wimplicit  -Wimport  -Winit-self  -Winline -Winvalid-pch   -Wlong-long -Wmissing-braces -Wmissing-field-initializers -Wmissing-format-attribute   -Wmissing-include-dirs -Wmissing-noreturn -Wpacked  -Wpadded -Wparentheses  -Wpointer-arith -Wredundant-decls -Wreturn-type -Wsequence-point  -Wshadow -Wsign-compare  -Wstack-protector -Wstrict-aliasing -Wstrict-aliasing=2 -Wswitch  -Wswitch-default -Wswitch-enum -Wtrigraphs  -Wuninitialized -Wunknown-pragmas  -Wunreachable-code -Wunused -Wunused-function  -Wunused-label  -Wunused-parameter -Wunused-value  -Wunused-variable  -Wvariadic-macros -Wvolatile-register-var  -Wwrite-strings")
+  set(CMAKE_CXX_FLAGS "-ferror-limit=5 -O3 -Wall -Weffc++ -pedantic -pedantic-errors -Wextra  -Wall -Waggregate-return -Wcast-align -Wcast-qual  -Wchar-subscripts  -Wcomment -Wconversion -Wdisabled-optimization -Wfloat-equal  -Wformat  -Wformat=2 -Wformat-nonliteral -Wformat-security  -Wformat-y2k -Wimplicit  -Wimport  -Winit-self  -Winline -Winvalid-pch   -Wlong-long -Wmissing-braces -Wmissing-field-initializers -Wmissing-format-attribute   -Wmissing-include-dirs -Wmissing-noreturn -Wpacked -Wparentheses  -Wpointer-arith -Wredundant-decls -Wreturn-type -Wsequence-point  -Wshadow -Wsign-compare  -Wstack-protector -Wstrict-aliasing -Wstrict-aliasing=2 -Wswitch  -Wswitch-default -Wswitch-enum -Wtrigraphs  -Wuninitialized -Wunknown-pragmas  -Wunreachable-code -Wunused -Wunused-function  -Wunused-label  -Wunused-parameter -Wunused-value  -Wunused-variable  -Wvariadic-macros -Wvolatile-register-var  -Wwrite-strings")
   set(CMAKE_BUILD_TYPE Debug)
 ENDIF(EXISTS /usr/bin/clang)
 
diff --git a/src/impl/unix.cc b/src/impl/unix.cc
index f583eb4..5a7c89f 100644
--- a/src/impl/unix.cc
+++ b/src/impl/unix.cc
@@ -1,3 +1,5 @@
+/* Copyright 2012 William Woodall and John Harrison */
+
 #include 
 #include 
 #include 
@@ -33,31 +35,36 @@ Serial::SerialImpl::SerialImpl (const string &port, unsigned long baudrate,
                                 long timeout, bytesize_t bytesize,
                                 parity_t parity, stopbits_t stopbits,
                                 flowcontrol_t flowcontrol)
-: port_(port), fd_(-1), interCharTimeout_(-1), writeTimeout_(-1),
-  isOpen_(false), xonxoff_(false), rtscts_(false), ___(0), timeout_(timeout),
-  baudrate_(baudrate), parity_(parity), bytesize_(bytesize),
-  stopbits_(stopbits), flowcontrol_(flowcontrol)
+: port_ (port), fd_ (-1), isOpen_ (false), xonxoff_ (true), rtscts_ (false),
+  timeout_ (timeout), baudrate_ (baudrate), parity_ (parity), bytesize_ (bytesize),
+  stopbits_ (stopbits), flowcontrol_ (flowcontrol)
 {
-  if (port_.empty() == false) this->open();
+  if (port_.empty () == false)
+    open ();
 }
 
-Serial::SerialImpl::~SerialImpl () {
-  this->close();
+Serial::SerialImpl::~SerialImpl ()
+{
+  close();
 }
 
 void
-Serial::SerialImpl::open () {
-  if (port_.empty()) {
-    throw invalid_argument("bad port specified");
+Serial::SerialImpl::open ()
+{
+  if (port_.empty())
+  {
+    throw invalid_argument ("bad port specified");
   }
-  if (isOpen_ == true) {
-    throw SerialExecption("port already open");
+  if (isOpen_ == true)
+  {
+    throw SerialExecption ("port already open");
   }
-  
+
   fd_ = ::open (port_.c_str(), O_RDWR | O_NOCTTY | O_NONBLOCK);
 
-  if (fd_ == -1) {
-    throw IOException("invalid file descriptor");
+  if (fd_ == -1)
+  {
+    throw IOException ("invalid file descriptor");
   }
 
   reconfigurePort();
@@ -65,31 +72,25 @@ Serial::SerialImpl::open () {
 }
 
 void
-Serial::SerialImpl::reconfigurePort () {
-  if (fd_ == -1) {
+Serial::SerialImpl::reconfigurePort ()
+{
+  if (fd_ == -1)
+  {
     // Can only operate on a valid file descriptor
-    throw IOException("invalid file descriptor");
+    throw IOException ("invalid file descriptor");
   }
 
-  struct termios options; // The current options for the file descriptor
-  struct termios originalTTYAttrs; // The orignal file descriptor options
+  struct termios options; // The options for the file descriptor
 
-  uint8_t vmin = 0, vtime = 0; // timeout is done via select
-  if (interCharTimeout_ == -1) {
-    vmin = 1;
-    vtime = uint8_t(interCharTimeout_ * 10);
+  if (tcgetattr(fd_, &options) == -1)
+  {
+    throw IOException ("::tcgetattr");
   }
 
-  if (tcgetattr(fd_, &originalTTYAttrs) == -1) {
-    throw IOException("::tcgetattr");
-  }
-
-  options = originalTTYAttrs;
-
   // set up raw mode / no echo / binary
-  options.c_cflag |= (unsigned long)(CLOCAL|CREAD);
-  options.c_lflag &= (unsigned long) ~(ICANON|ECHO|ECHOE|ECHOK
-                       |ECHONL|ISIG|IEXTEN); //|ECHOPRT
+  options.c_cflag |= (unsigned long)  (CLOCAL|CREAD);
+  options.c_lflag &= (unsigned long) ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL|
+                                       ISIG|IEXTEN); //|ECHOPRT
 
   options.c_oflag &= (unsigned long) ~(OPOST);
   options.c_iflag &= (unsigned long) ~(INLCR|IGNCR|ICRNL|IGNBRK);
@@ -115,7 +116,7 @@ Serial::SerialImpl::reconfigurePort () {
   else if (bytesize_ == FIVEBITS)
       options.c_cflag |= CS5;
   else
-      throw invalid_argument("Invalid char len");
+      throw invalid_argument ("Invalid char len");
   // setup stopbits
   if (stopbits_ == STOPBITS_ONE)
       options.c_cflag &= (unsigned long) ~(CSTOPB);
@@ -123,22 +124,26 @@ Serial::SerialImpl::reconfigurePort () {
       options.c_cflag |=  (CSTOPB);  // XXX same as TWO.. there is no POSIX support for 1.5
   else if (stopbits_ == STOPBITS_TWO)
       options.c_cflag |=  (CSTOPB);
-  else 
-      throw invalid_argument("invalid stop bit");
+  else
+      throw invalid_argument ("invalid stop bit");
   // setup parity
   options.c_iflag &= (unsigned long) ~(INPCK|ISTRIP);
-  if (parity_ == PARITY_NONE) {
+  if (parity_ == PARITY_NONE)
+  {
     options.c_cflag &= (unsigned long) ~(PARENB|PARODD);
   }
-  else if (parity_ == PARITY_EVEN) {
+  else if (parity_ == PARITY_EVEN)
+  {
     options.c_cflag &= (unsigned long) ~(PARODD);
     options.c_cflag |=  (PARENB);
-  } 
-  else if (parity_ == PARITY_ODD) {
+  }
+  else if (parity_ == PARITY_ODD)
+  {
     options.c_cflag |=  (PARENB|PARODD);
   }
-  else {
-    throw invalid_argument("invalid parity");
+  else
+  {
+    throw invalid_argument ("invalid parity");
   }
   // setup flow control
   // xonxoff
@@ -168,21 +173,21 @@ Serial::SerialImpl::reconfigurePort () {
 #error "OS Support seems wrong."
 #endif
 
-  // buffer
-  // vmin "minimal number of characters to be read. = for non blocking"
-  options.c_cc[VMIN] = vmin;
-  // vtime
-  options.c_cc[VTIME] = vtime;
+  options.c_cc[VMIN] = 1; // Minimum of 1 character in the buffer
+  options.c_cc[VTIME] = 0; // timeout on waiting for new data
 
   // activate settings
-  ::tcsetattr(fd_, TCSANOW, &options);
+  ::tcsetattr (fd_, TCSANOW, &options);
 }
 
 void
-Serial::SerialImpl::close () {
-  if (isOpen_ == true) {
-    if (fd_ != -1) {
-      ::close(fd_); // Ignoring the outcome
+Serial::SerialImpl::close ()
+{
+  if (isOpen_ == true)
+  {
+    if (fd_ != -1)
+    {
+      ::close (fd_); // Ignoring the outcome
       fd_ = -1;
     }
     isOpen_ = false;
@@ -190,266 +195,318 @@ Serial::SerialImpl::close () {
 }
 
 bool
-Serial::SerialImpl::isOpen () const {
+Serial::SerialImpl::isOpen () const
+{
   return isOpen_;
 }
 
 size_t
-Serial::SerialImpl::available () {
-  if (!isOpen_) {
+Serial::SerialImpl::available ()
+{
+  if (!isOpen_)
+  {
     return 0;
   }
   int count = 0;
-  int result = ioctl(fd_, TIOCINQ, &count);
-  if (result == 0) {
-    return (size_t)count;
+  int result = ioctl (fd_, TIOCINQ, &count);
+  if (result == 0)
+  {
+    return static_cast (count);
   }
-  else {
-    throw IOException("ioctl");
+  else
+  {
+    throw IOException ("ioctl");
   }
 }
 
-string
-Serial::SerialImpl::read (size_t size) {
-  if (!isOpen_) {
-    throw PortNotOpenedException("Serial::read");
-  }
-  string message = "";
-  char *buf = NULL;
-  // Using size+1 to leave room for a null character
-  if (size > 1024) {
-    buf = (char*)malloc((size + 1) * sizeof(*buf));
-  }
-  else {
-    buf = (char*)alloca((size + 1) * sizeof(*buf));
+size_t
+Serial::SerialImpl::read (char* buf, size_t size)
+{
+  if (!isOpen_)
+  {
+    throw PortNotOpenedException ("Serial::read");
   }
   fd_set readfds;
-  memset(buf, 0, (size + 1) * sizeof(*buf));
   ssize_t bytes_read = 0;
-  while (bytes_read < size) {
-    if (timeout_ != -1) {
-      FD_ZERO(&readfds);
-      FD_SET(fd_, &readfds);
+  while (true)
+  {
+    if (timeout_ != -1)
+    {
+      FD_ZERO (&readfds);
+      FD_SET (fd_, &readfds);
       struct timeval timeout;
-      timeout.tv_sec =        timeout_ / 1000;
-      timeout.tv_usec = (int) timeout_ % 1000;
-      int r = select(fd_ + 1, &readfds, NULL, NULL, &timeout);
+      timeout.tv_sec =                    timeout_ / 1000;
+      timeout.tv_usec = static_cast (timeout_ % 1000);
+      int r = select (fd_ + 1, &readfds, NULL, NULL, &timeout);
 
       if (r == -1 && errno == EINTR)
         continue;
 
-      if (r == -1) {
+      if (r == -1)
+      {
         perror("select()");
         exit(EXIT_FAILURE);
       }
     }
 
-    if (timeout_ == -1 || FD_ISSET(fd_, &readfds)) {
-      ssize_t newest_read = ::read(fd_,
-                                   buf + bytes_read,
-                                   size - static_cast(bytes_read));
+    if (timeout_ == -1 || FD_ISSET (fd_, &readfds))
+    {
+      bytes_read = ::read (fd_, buf, size);
       // read should always return some data as select reported it was
       // ready to read when we get to this point.
-      if (newest_read < 1) {
+      // printf("bytes_read: %lu\n", bytes_read);
+      if (bytes_read < 1)
+      {
         // Disconnected devices, at least on Linux, show the
         // behavior that they are always ready to read immediately
         // but reading returns nothing.
-        throw SerialExecption("device reports readiness to read but "
-                              "returned no data (device disconnected?)");
+        throw SerialExecption ("device reports readiness to read but "
+                               "returned no data (device disconnected?)");
       }
-      bytes_read += newest_read;
-    }
-    else {
-      break; // Timeout
+      break;
     }
   }
-  if (bytes_read > 0)
-    message.append(buf, (size_t)bytes_read);
-  if (size > 1024)
-    free(buf);
-  return message;
+  return static_cast (bytes_read);
 }
 
 size_t
-Serial::SerialImpl::write (const string &data) {
+Serial::SerialImpl::write (const string &data)
+{
   if (isOpen_ == false) {
-    throw PortNotOpenedException("Serial::write");
+    throw PortNotOpenedException ("Serial::write");
   }
-  ssize_t n = ::write(fd_, data.c_str(), data.length());
+  ssize_t n = ::write (fd_, data.c_str (), data.length ());
   if (n == -1) {
-    throw IOException("Write");
+    throw IOException ("Write");
   }
-  return (size_t)n;
+  return static_cast (n);
 }
 
 void
-Serial::SerialImpl::setPort (const string &port) {
+Serial::SerialImpl::setPort (const string &port)
+{
   port_ = port;
 }
 
 string
-Serial::SerialImpl::getPort () const {
+Serial::SerialImpl::getPort () const
+{
   return port_;
 }
 
 void
-Serial::SerialImpl::setTimeout (long timeout) {
+Serial::SerialImpl::setTimeout (long timeout)
+{
   timeout_ = timeout;
-  if (isOpen_) reconfigurePort();
+  if (isOpen_)
+    reconfigurePort ();
 }
 
 long
-Serial::SerialImpl::getTimeout () const {
+Serial::SerialImpl::getTimeout () const
+{
   return timeout_;
 }
 
 void
-Serial::SerialImpl::setBaudrate (unsigned long baudrate) {
+Serial::SerialImpl::setBaudrate (unsigned long baudrate)
+{
   baudrate_ = baudrate;
-  if (isOpen_) reconfigurePort();
+  if (isOpen_)
+    reconfigurePort ();
 }
 
 unsigned long
-Serial::SerialImpl::getBaudrate () const {
+Serial::SerialImpl::getBaudrate () const
+{
   return baudrate_;
 }
 
 void
-Serial::SerialImpl::setBytesize (serial::bytesize_t bytesize) {
+Serial::SerialImpl::setBytesize (serial::bytesize_t bytesize)
+{
   bytesize_ = bytesize;
-  if (isOpen_) reconfigurePort();
+  if (isOpen_)
+    reconfigurePort ();
 }
 
 serial::bytesize_t
-Serial::SerialImpl::getBytesize () const {
+Serial::SerialImpl::getBytesize () const
+{
   return bytesize_;
 }
 
 void
-Serial::SerialImpl::setParity (serial::parity_t parity) {
+Serial::SerialImpl::setParity (serial::parity_t parity)
+{
   parity_ = parity;
-  if (isOpen_) reconfigurePort();
+  if (isOpen_)
+    reconfigurePort ();
 }
 
 serial::parity_t
-Serial::SerialImpl::getParity () const {
+Serial::SerialImpl::getParity () const
+{
   return parity_;
 }
 
 void
-Serial::SerialImpl::setStopbits (serial::stopbits_t stopbits) {
+Serial::SerialImpl::setStopbits (serial::stopbits_t stopbits)
+{
   stopbits_ = stopbits;
-  if (isOpen_) reconfigurePort();
+  if (isOpen_)
+    reconfigurePort ();
 }
 
 serial::stopbits_t
-Serial::SerialImpl::getStopbits () const {
+Serial::SerialImpl::getStopbits () const
+{
   return stopbits_;
 }
 
 void
-Serial::SerialImpl::setFlowcontrol (serial::flowcontrol_t flowcontrol) {
+Serial::SerialImpl::setFlowcontrol (serial::flowcontrol_t flowcontrol)
+{
   flowcontrol_ = flowcontrol;
-  if (isOpen_) reconfigurePort();
+  if (isOpen_)
+    reconfigurePort ();
 }
 
 serial::flowcontrol_t
-Serial::SerialImpl::getFlowcontrol () const {
+Serial::SerialImpl::getFlowcontrol () const
+{
   return flowcontrol_;
 }
 
-void Serial::SerialImpl::flush () {
-  if (isOpen_ == false) {
-    throw PortNotOpenedException("Serial::flush");
+void
+Serial::SerialImpl::flush ()
+{
+  if (isOpen_ == false)
+  {
+    throw PortNotOpenedException ("Serial::flush");
   }
-  tcdrain(fd_);
+  tcdrain (fd_);
 }
 
-void Serial::SerialImpl::flushInput () {
-  if (isOpen_ == false) {
-    throw PortNotOpenedException("Serial::flushInput");
+void
+Serial::SerialImpl::flushInput ()
+{
+  if (isOpen_ == false)
+  {
+    throw PortNotOpenedException ("Serial::flushInput");
   }
-  tcflush(fd_, TCIFLUSH);
+  tcflush (fd_, TCIFLUSH);
 }
 
-void Serial::SerialImpl::flushOutput () {
-  if (isOpen_ == false) {
-    throw PortNotOpenedException("Serial::flushOutput");
+void
+Serial::SerialImpl::flushOutput ()
+{
+  if (isOpen_ == false)
+  {
+    throw PortNotOpenedException ("Serial::flushOutput");
   }
-  tcflush(fd_, TCOFLUSH);
+  tcflush (fd_, TCOFLUSH);
 }
 
-void Serial::SerialImpl::sendBreak(int duration) {
-  if (isOpen_ == false) {
-    throw PortNotOpenedException("Serial::sendBreak");
+void
+Serial::SerialImpl::sendBreak (int duration)
+{
+  if (isOpen_ == false)
+  {
+    throw PortNotOpenedException ("Serial::sendBreak");
   }
-  tcsendbreak(fd_, int(duration/4));
+  tcsendbreak (fd_, static_cast (duration/4));
 }
 
-void Serial::SerialImpl::setBreak(bool level) {
-  if (isOpen_ == false) {
-    throw PortNotOpenedException("Serial::setBreak");
+void
+Serial::SerialImpl::setBreak (bool level)
+{
+  if (isOpen_ == false)
+  {
+    throw PortNotOpenedException ("Serial::setBreak");
   }
-  if (level) {
-    ioctl(fd_, TIOCSBRK);
+  if (level)
+  {
+    ioctl (fd_, TIOCSBRK);
   }
   else {
-    ioctl(fd_, TIOCCBRK);
+    ioctl (fd_, TIOCCBRK);
   }
 }
 
-void Serial::SerialImpl::setRTS(bool level) {
-  if (isOpen_ == false) {
-    throw PortNotOpenedException("Serial::setRTS");
+void
+Serial::SerialImpl::setRTS (bool level)
+{
+  if (isOpen_ == false)
+  {
+    throw PortNotOpenedException ("Serial::setRTS");
   }
-  if (level) {
-    ioctl(fd_, TIOCMBIS, TIOCM_RTS);
+  if (level)
+  {
+    ioctl (fd_, TIOCMBIS, TIOCM_RTS);
   }
   else {
-    ioctl(fd_, TIOCMBIC, TIOCM_RTS);
+    ioctl (fd_, TIOCMBIC, TIOCM_RTS);
   }
 }
 
-void Serial::SerialImpl::setDTR(bool level) {
-  if (isOpen_ == false) {
-    throw PortNotOpenedException("Serial::setDTR");
+void
+Serial::SerialImpl::setDTR (bool level)
+{
+  if (isOpen_ == false)
+  {
+    throw PortNotOpenedException ("Serial::setDTR");
   }
-  if (level) {
-    ioctl(fd_, TIOCMBIS, TIOCM_DTR);
+  if (level)
+  {
+    ioctl (fd_, TIOCMBIS, TIOCM_DTR);
   }
-  else {
-    ioctl(fd_, TIOCMBIC, TIOCM_DTR);
+  else
+  {
+    ioctl (fd_, TIOCMBIC, TIOCM_DTR);
   }
 }
 
-bool Serial::SerialImpl::getCTS() {
-  if (isOpen_ == false) {
-    throw PortNotOpenedException("Serial::getCTS");
+bool
+Serial::SerialImpl::getCTS ()
+{
+  if (isOpen_ == false)
+  {
+    throw PortNotOpenedException ("Serial::getCTS");
   }
-  int s = ioctl(fd_, TIOCMGET, 0);
+  int s = ioctl (fd_, TIOCMGET, 0);
   return (s & TIOCM_CTS) != 0;
 }
 
-bool Serial::SerialImpl::getDSR() {
-  if (isOpen_ == false) {
-    throw PortNotOpenedException("Serial::getDSR");
+bool
+Serial::SerialImpl::getDSR()
+{
+  if (isOpen_ == false)
+  {
+    throw PortNotOpenedException ("Serial::getDSR");
   }
   int s = ioctl(fd_, TIOCMGET, 0);
   return (s & TIOCM_DSR) != 0;
 }
 
-bool Serial::SerialImpl::getRI() {
-  if (isOpen_ == false) {
-    throw PortNotOpenedException("Serial::getRI");
+bool
+Serial::SerialImpl::getRI()
+{
+  if (isOpen_ == false)
+  {
+    throw PortNotOpenedException ("Serial::getRI");
   }
-  int s = ioctl(fd_, TIOCMGET, 0);
+  int s = ioctl (fd_, TIOCMGET, 0);
   return (s & TIOCM_RI) != 0;
 }
 
-bool Serial::SerialImpl::getCD() {
-  if (isOpen_ == false) {
-    throw PortNotOpenedException("Serial::getCD");
+bool
+Serial::SerialImpl::getCD()
+{
+  if (isOpen_ == false)
+  {
+    throw PortNotOpenedException ("Serial::getCD");
   }
-  int s = ioctl(fd_, TIOCMGET, 0);
+  int s = ioctl (fd_, TIOCMGET, 0);
   return (s & TIOCM_CD) != 0;
 }
diff --git a/src/serial.cc b/src/serial.cc
index 9a5def8..beed5ba 100644
--- a/src/serial.cc
+++ b/src/serial.cc
@@ -1,3 +1,10 @@
+/* Copyright 2012 William Woodall and John Harrison */
+
+#include 
+
+#include 
+#include 
+
 #include "serial/serial.h"
 
 #ifdef _WIN32
@@ -6,11 +13,14 @@
 #include "serial/impl/unix.h"
 #endif
 
-using std::string;
-using std::vector;
-using std::numeric_limits;
-using std::size_t;
 using std::invalid_argument;
+using std::memset;
+using std::min;
+using std::numeric_limits;
+using std::vector;
+using std::size_t;
+using std::string;
+
 using serial::Serial;
 using serial::SerialExecption;
 using serial::IOException;
@@ -21,204 +31,301 @@ using serial::flowcontrol_t;
 
 Serial::Serial (const string &port, unsigned long baudrate, long timeout,
                 bytesize_t bytesize, parity_t parity, stopbits_t stopbits,
-                flowcontrol_t flowcontrol)
+                flowcontrol_t flowcontrol, const size_t buffer_size)
+ : buffer_size_(buffer_size)
 {
-  pimpl = new SerialImpl(port, baudrate, timeout, bytesize, parity,
+  pimpl_ = new SerialImpl (port, baudrate, timeout, bytesize, parity,
                            stopbits, flowcontrol);
+  read_cache_ = new char[buffer_size_];
+  memset (read_cache_, 0, buffer_size_ * sizeof (char));
 }
 
-Serial::~Serial () {
-  delete pimpl;
+Serial::~Serial ()
+{
+  delete   pimpl_;
+  delete[] read_cache_;
 }
 
 void
-Serial::open () {
-  this->pimpl->open ();
+Serial::open ()
+{
+  pimpl_->open ();
 }
 
 void
-Serial::close () {
-  this->pimpl->close ();
+Serial::close ()
+{
+  pimpl_->close ();
+  memset (read_cache_, 0, buffer_size_ * sizeof (char));
 }
 
 bool
-Serial::isOpen () const {
-  return this->pimpl->isOpen ();
+Serial::isOpen () const
+{
+  return pimpl_->isOpen ();
 }
 
 size_t
-Serial::available () {
-  return this->pimpl->available();
+Serial::available ()
+{
+  return pimpl_->available ();
 }
 
 string
-Serial::read (size_t size) {
-  return this->pimpl->read (size);
+Serial::read (size_t size)
+{
+  size_t cache_size = strlen (read_cache_);
+  if (cache_size >= size)
+  {
+    // Don't need to do a new read.
+    string result (read_cache_, size);
+    memmove (read_cache_, read_cache_ + size, cache_size - size);
+    *(read_cache_ + cache_size - size) = '\0';
+    return result;
+  }
+  else
+  {
+    // Needs to read, loop until we have read enough... or timeout
+    size_t chars_left = 0;
+    string result = "";
+
+    if (cache_size > 0)
+    {
+      result.append (read_cache_, cache_size);
+      memset (read_cache_, 0, buffer_size_);
+      chars_left = size - cache_size;
+    }
+    else
+    {
+      chars_left = size;
+    }
+
+    while (true)
+    {
+      size_t chars_read = pimpl_->read (read_cache_, buffer_size_ - 1);
+      if (chars_read > 0)
+      {
+        *(read_cache_ + chars_read) = '\0';
+        if (chars_left > chars_read)
+        {
+          memset (read_cache_, 0, buffer_size_);
+          result.append (read_cache_);
+          chars_left -= chars_read;
+        }
+        else
+        {
+          result.append (read_cache_, static_cast (chars_left));
+          memmove (read_cache_, read_cache_ + chars_left, chars_read - chars_left);
+          *(read_cache_ + chars_read - chars_left) = '\0';
+          memset (read_cache_ + chars_read - chars_left, 0,
+                  buffer_size_ - chars_read - chars_left);
+          // Finished reading all of the data
+          break;
+        }
+      }
+      else
+        break; // Timeout occured
+    }
+    return result;
+  }
 }
 
 string
-Serial::readline(size_t size, string eol) {
-  size_t leneol = eol.length();
+Serial::readline (size_t size, string eol)
+{
+  size_t leneol = eol.length ();
   string line = "";
-  while (true) {
-    string c = pimpl->read(1);
-    if (!c.empty()) {
-      line.append(c);
-      if (line.length() > leneol
-       && line.substr(line.length() - leneol, leneol) == eol)
+  while (true)
+  {
+    string c = read (1);
+    if (!c.empty ())
+    {
+      line.append (c);
+      if (line.length () > leneol &&
+          line.substr (line.length () - leneol, leneol) == eol)
         break;
-      if (line.length() >= size) {
+      if (line.length () >= size)
+      {
         break;
       }
     }
-    else {
+    else
       // Timeout
       break;
-    }
   }
   return line;
 }
 
 vector
-Serial::readlines(string eol) {
-  if (this->pimpl->getTimeout() < 0) {
+Serial::readlines(string eol)
+{
+  if (pimpl_->getTimeout () < 0)
+  {
     throw "Error, must be set for readlines";
   }
-  size_t leneol = eol.length();
+  size_t leneol = eol.length ();
   vector lines;
-  while (true) {
-    string line = readline(numeric_limits::max(), eol);
-    if (!line.empty()) {
-      lines.push_back(line);
-      if (line.substr(line.length() - leneol, leneol) == eol)
+  while (true)
+  {
+    string line = readline (numeric_limits::max (), eol);
+    if (!line.empty ())
+    {
+      lines.push_back (line);
+      if (line.substr (line.length () - leneol, leneol) == eol)
         break;
     }
-    else {
+    else
       // Timeout
       break;
-    }
   }
   return lines;
 }
 
 size_t
-Serial::write (const string &data) {
-  return this->pimpl->write (data);
+Serial::write (const string &data)
+{
+  return pimpl_->write (data);
 }
 
 void
-Serial::setPort (const string &port) {
-  bool was_open = pimpl->isOpen();
-  if (was_open) this->close();
-  this->pimpl->setPort (port);
-  if (was_open) this->open();
+Serial::setPort (const string &port)
+{
+  bool was_open = pimpl_->isOpen();
+  if (was_open) close();
+  pimpl_->setPort (port);
+  if (was_open) open();
 }
 
 string
-Serial::getPort () const {
-  return this->pimpl->getPort ();
+Serial::getPort () const
+{
+  return pimpl_->getPort ();
 }
 
 void
-Serial::setTimeout (long timeout) {
-  this->pimpl->setTimeout (timeout);
+Serial::setTimeout (long timeout)
+{
+  pimpl_->setTimeout (timeout);
 }
 
 long
 Serial::getTimeout () const {
-  return this->pimpl->getTimeout ();
+  return pimpl_->getTimeout ();
 }
 
 void
-Serial::setBaudrate (unsigned long baudrate) {
-  this->pimpl->setBaudrate (baudrate);
+Serial::setBaudrate (unsigned long baudrate)
+{
+  pimpl_->setBaudrate (baudrate);
 }
 
 unsigned long
-Serial::getBaudrate () const {
-  return this->pimpl->getBaudrate ();
+Serial::getBaudrate () const
+{
+  return pimpl_->getBaudrate ();
 }
 
 void
-Serial::setBytesize (bytesize_t bytesize) {
-  this->pimpl->setBytesize (bytesize);
+Serial::setBytesize (bytesize_t bytesize)
+{
+  pimpl_->setBytesize (bytesize);
 }
 
 bytesize_t
-Serial::getBytesize () const {
-  return this->pimpl->getBytesize ();
+Serial::getBytesize () const
+{
+  return pimpl_->getBytesize ();
 }
 
 void
-Serial::setParity (parity_t parity) {
-  this->pimpl->setParity (parity);
+Serial::setParity (parity_t parity)
+{
+  pimpl_->setParity (parity);
 }
 
 parity_t
-Serial::getParity () const {
-  return this->pimpl->getParity ();
+Serial::getParity () const
+{
+  return pimpl_->getParity ();
 }
 
 void
-Serial::setStopbits (stopbits_t stopbits) {
-  this->pimpl->setStopbits (stopbits);
+Serial::setStopbits (stopbits_t stopbits)
+{
+  pimpl_->setStopbits (stopbits);
 }
 
 stopbits_t
-Serial::getStopbits () const {
-  return this->pimpl->getStopbits ();
+Serial::getStopbits () const
+{
+  return pimpl_->getStopbits ();
 }
 
 void
-Serial::setFlowcontrol (flowcontrol_t flowcontrol) {
-  this->pimpl->setFlowcontrol (flowcontrol);
+Serial::setFlowcontrol (flowcontrol_t flowcontrol)
+{
+  pimpl_->setFlowcontrol (flowcontrol);
 }
 
 flowcontrol_t
-Serial::getFlowcontrol () const {
-  return this->pimpl->getFlowcontrol ();
+Serial::getFlowcontrol () const
+{
+  return pimpl_->getFlowcontrol ();
 }
 
-void Serial::flush() {
-  this->pimpl->flush();
+void Serial::flush ()
+{
+  pimpl_->flush ();
+  memset (read_cache_, 0, buffer_size_);
 }
 
-void Serial::flushInput() {
-  this->pimpl->flushInput();
+void Serial::flushInput ()
+{
+  pimpl_->flushInput ();
 }
 
-void Serial::flushOutput() {
-  this->pimpl->flushOutput();
+void Serial::flushOutput ()
+{
+  pimpl_->flushOutput ();
+  memset (read_cache_, 0, buffer_size_);
 }
 
-void Serial::sendBreak(int duration) {
-  this->pimpl->sendBreak(duration);
+void Serial::sendBreak (int duration)
+{
+  pimpl_->sendBreak (duration);
 }
 
-void Serial::setBreak(bool level) {
-  this->pimpl->setBreak(level);
+void Serial::setBreak (bool level)
+{
+  pimpl_->setBreak (level);
 }
 
-void Serial::setRTS(bool level) {
-  this->pimpl->setRTS(level);
+void Serial::setRTS (bool level)
+{
+  pimpl_->setRTS (level);
 }
 
-void Serial::setDTR(bool level) {
-  this->pimpl->setDTR(level);
+void Serial::setDTR (bool level)
+{
+  pimpl_->setDTR (level);
 }
 
-bool Serial::getCTS() {
-  return this->pimpl->getCTS();
+bool Serial::getCTS ()
+{
+  return pimpl_->getCTS ();
 }
 
-bool Serial::getDSR() {
-  return this->pimpl->getDSR();
+bool Serial::getDSR ()
+{
+  return pimpl_->getDSR ();
 }
 
-bool Serial::getRI() {
-  return this->pimpl->getRI();
+bool Serial::getRI ()
+{
+  return pimpl_->getRI ();
 }
 
-bool Serial::getCD() {
-  return this->pimpl->getCD();
+bool Serial::getCD ()
+{
+  return pimpl_->getCD ();
 }
diff --git a/src/serial_listener.cc b/src/serial_listener.cc
index 85055db..b106d00 100644
--- a/src/serial_listener.cc
+++ b/src/serial_listener.cc
@@ -1,3 +1,5 @@
+/* Copyright 2012 William Woodall and John Harrison */
+
 #include "serial/serial_listener.h"
 
 /***** Inline Functions *****/
diff --git a/tests/serial_tests.cc b/tests/serial_tests.cc
index bef6352..184d99d 100644
--- a/tests/serial_tests.cc
+++ b/tests/serial_tests.cc
@@ -1,10 +1,3 @@
-#include "gtest/gtest.h"
-
-#include 
-
-// OMG this is so nasty...
-// #define private public
-// #define protected public
 #include 
 #include 
 
@@ -14,17 +7,27 @@ using std::string;
 using std::cout;
 using std::endl;
 using serial::Serial;
+using serial::SerialExecption;
 
 int main(int argc, char **argv) {
-  Serial s("/dev/tty.usbserial-A900adHq", 115200, 2000);
-  s.flush();
-  int count = 0;
-  while (1) {
-    size_t available = s.available();
-    cout << "avialable: " << available << endl;
-    string line = s.readline();
-    cout << count << ": " << line;
-    count++;
+  try {
+    Serial s("/dev/tty.usbserial-A900adHq", 115200, 2000);
+    s.flush();
+    long long count = 0;
+    while (1) {
+      // size_t available = s.available();
+      // cout << "avialable: " << available << endl;
+      string line = s.readline();
+      cout << count << ": " << line << line.length() << endl;
+      count++;
+    }
+  }
+  catch (SerialExecption e)
+  {
+    cout << "Caught SerialException: " << e.what() << endl;
+  }
+  catch (...)
+  {
+    cout << "Caught an error." << endl;
   }
-  cout << endl << endl;
 }

From 31d0913410bf15af9b0277b2ad7b5197d1200edf Mon Sep 17 00:00:00 2001
From: William Woodall 
Date: Sun, 15 Jan 2012 16:18:42 -0600
Subject: [PATCH 32/72] Removing dead code.

---
 include/serial/serial_listener.h | 133 -------------------------------
 src/serial_listener.cc           |  19 -----
 2 files changed, 152 deletions(-)

diff --git a/include/serial/serial_listener.h b/include/serial/serial_listener.h
index ba274aa..938054e 100644
--- a/include/serial/serial_listener.h
+++ b/include/serial/serial_listener.h
@@ -113,22 +113,6 @@ typedef boost::function ComparatorType;
 typedef boost::function&)>
 TokenizerType;
 
-#if 0
-/*!
- * This function type describes the prototype for the logging callbacks.
- * 
- * The function takes a std::string reference and returns nothing.  It is
- * called from the library when a logging message occurs.  This
- * allows the library user to hook into this and integrate it with their own
- * logging system.  It can be set with any of the setHandler
- * functions.
- * 
- * \see SerialListener::setInfoHandler, SerialListener::setDebugHandler, 
- * SerialListener::setWarningHandler
- */
-typedef boost::function LoggingCallback;
-#endif
-
 /*!
  * This function type describes the prototype for the exception callback.
  * 
@@ -474,116 +458,6 @@ public:
 
 /***** Hooks and Handlers ******/
 
-#if 0
-  /*!
-   * Sets the handler to be called when a lines is not caught by a filter.
-   * 
-   * This allows you to set a catch all function that will get called 
-   * everytime a line is not matched by a filter and the ttl expires.
-   * 
-   * Setting the callbacks works just like SerialListener::setInfoHandler.
-   * 
-   * \param default_handler A function pointer to the callback to handle 
-   * unmatched and expired messages.
-   * 
-   * \see serial::DataCallback, SerialListener::setInfoHandler
-   */
-  void
-  setDefaultHandler (DataCallback default_handler) {
-    this->_default_handler = default_handler;
-  }
-
-  /*!
-   * Sets the function to be called when an info logging message occurs.
-   * 
-   * This allows you to hook into the message reporting of the library and use
-   * your own logging facilities.
-   * 
-   * The provided function must follow this prototype:
-   * 
-   *    void yourInfoCallback(const std::string &msg)
-   * 
- * Here is an example: - *
-   *    void yourInfoCallback(const std::string &msg) {
-   *        std::cout << "SerialListener Info: " << msg << std::endl;
-   *    }
-   * 
- * And the resulting call to make it the callback: - *
-   *    serial::SerialListener listener;
-   *    listener.setInfoCallback(yourInfoCallback);
-   * 
- * Alternatively you can use a class method as a callback using boost::bind: - *
-   *    #include 
-   *    
-   *    #include "serial/serial_listener.h"
-   *    
-   *    class MyClass
-   *    {
-   *    public:
-   *     MyClass () {
-   *      listener.setInfoHandler(
-   *          boost::bind(&MyClass::handleInfo, this, _1));
-   *     }
-   *    
-   *     void handleInfo(const std::string &msg) {
-   *       std::cout << "MyClass Info: " << msg << std::endl;
-   *     }
-   *    
-   *    private:
-   *     serial::SerialListener listener;
-   *    };
-   * 
- * - * \param info_handler A function pointer to the callback to handle new - * Info messages. - * - * \see serial::LoggingCallback - */ - void - setInfoHandler (LoggingCallback info_handler) { - this->info = info_handler; - } - - /*! - * Sets the function to be called when a debug logging message occurs. - * - * This allows you to hook into the message reporting of the library and use - * your own logging facilities. - * - * This works just like SerialListener::setInfoHandler. - * - * \param debug_handler A function pointer to the callback to handle new - * Debug messages. - * - * \see serial::LoggingCallback, SerialListener::setInfoHandler - */ - void - setDebugHandler (LoggingCallback debug_handler) { - this->debug = debug_handler; - } - - /*! - * Sets the function to be called when a warning logging message occurs. - * - * This allows you to hook into the message reporting of the library and use - * your own logging facilities. - * - * This works just like SerialListener::setInfoHandler. - * - * \param warning_handler A function pointer to the callback to handle new - * Warning messages. - * - * \see serial::LoggingCallback, SerialListener::setInfoHandler - */ - void - setWarningHandler (LoggingCallback warning_handler) { - this->warn = warning_handler; - } -#endif - /*! * Sets the function to be called when an exception occurs internally. * @@ -792,13 +666,6 @@ private: // Tokenizer TokenizerType tokenize; -#if 0 - // Logging handlers - LoggingCallback warn; - LoggingCallback info; - LoggingCallback debug; -#endif - // Exception handler ExceptionCallback handle_exc; diff --git a/src/serial_listener.cc b/src/serial_listener.cc index 599ee88..e3af13a 100644 --- a/src/serial_listener.cc +++ b/src/serial_listener.cc @@ -2,20 +2,6 @@ /***** Inline Functions *****/ -#if 0 -inline void defaultWarningCallback(const std::string& msg) { - std::cout << "SerialListener Warning: " << msg << std::endl; -} - -inline void defaultDebugCallback(const std::string& msg) { - std::cout << "SerialListener Debug: " << msg << std::endl; -} - -inline void defaultInfoCallback(const std::string& msg) { - std::cout << "SerialListener Info: " << msg << std::endl; -} -#endif - inline void defaultExceptionCallback(const std::exception &error) { std::cerr << "SerialListener Unhandled Exception: " << error.what(); std::cerr << std::endl; @@ -38,11 +24,6 @@ SerialListener::default_handler(const std::string &token) { SerialListener::SerialListener() : listening(false), chunk_size_(5) { // Set default callbacks this->handle_exc = defaultExceptionCallback; -#if 0 - this->info = defaultInfoCallback; - this->debug = defaultDebugCallback; - this->warn = defaultWarningCallback; -#endif // Default handler stuff this->_default_handler = NULL; From 154ef46c20f61b3f338b307f3325f4ac5a8f271b Mon Sep 17 00:00:00 2001 From: William Woodall Date: Sun, 15 Jan 2012 17:32:33 -0600 Subject: [PATCH 33/72] Fixing exception handler setting function --- include/serial/serial_listener.h | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/include/serial/serial_listener.h b/include/serial/serial_listener.h index 543e29f..42d30d8 100644 --- a/include/serial/serial_listener.h +++ b/include/serial/serial_listener.h @@ -458,21 +458,21 @@ public: /***** Hooks and Handlers ******/ -/*! - * Sets the function to be called when an exception occurs internally. - * - * This allows you to hook into the exceptions that occur in threads inside - * the serial listener library. - * - * \param exception_handler A function pointer to the callback to handle new - * interal exceptions. - * - * \see serial::ExceptionCallback - */ -void -setWarningHandler (ExceptionCallback exception_handler) { - this->handle_exc = exception_handler; -} + /*! + * Sets the function to be called when an exception occurs internally. + * + * This allows you to hook into the exceptions that occur in threads inside + * the serial listener library. + * + * \param exception_handler A function pointer to the callback to handle new + * interal exceptions. + * + * \see serial::ExceptionCallback + */ + void + setExceptionHandler (ExceptionCallback exception_handler) { + this->handle_exc = exception_handler; + } /***** Static Functions ******/ From 1bceb66e0e5de74dc56e6291990b80ec8397b25a Mon Sep 17 00:00:00 2001 From: William Woodall Date: Sun, 15 Jan 2012 20:48:21 -0600 Subject: [PATCH 34/72] Fixing serial manifest.xml for ROS and replacing an accidental missing function --- include/serial/serial_listener.h | 17 +++++++++++++++++ manifest.xml | 4 ++++ 2 files changed, 21 insertions(+) diff --git a/include/serial/serial_listener.h b/include/serial/serial_listener.h index 42d30d8..a5a9f02 100644 --- a/include/serial/serial_listener.h +++ b/include/serial/serial_listener.h @@ -458,6 +458,23 @@ public: /***** Hooks and Handlers ******/ + /*! + * Sets the handler to be called when a lines is not caught by a filter. + * + * This allows you to set a catch all function that will get called + * everytime a line is not matched by a filter and the ttl expires. + * + * Setting the callbacks works just like SerialListener::setInfoHandler. + * + * \param default_handler A function pointer to the callback to handle + * unmatched and expired messages. + * + * \see serial::DataCallback, SerialListener::setInfoHandler + */ + void setDefaultHandler(DataCallback default_handler) { + this->default_handler = default_handler; + } + /*! * Sets the function to be called when an exception occurs internally. * diff --git a/manifest.xml b/manifest.xml index 64e44c9..1f2ff5c 100644 --- a/manifest.xml +++ b/manifest.xml @@ -8,6 +8,10 @@ BSD http://ros.org/wiki/serial + + + + From 04f81f23ed6143b6586f5eca6a51e0a2a1370901 Mon Sep 17 00:00:00 2001 From: William Woodall Date: Sun, 15 Jan 2012 20:54:39 -0600 Subject: [PATCH 35/72] Fixing another issue with the default handler setter. --- include/serial/serial_listener.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/serial/serial_listener.h b/include/serial/serial_listener.h index a5a9f02..cb6307a 100644 --- a/include/serial/serial_listener.h +++ b/include/serial/serial_listener.h @@ -472,7 +472,7 @@ public: * \see serial::DataCallback, SerialListener::setInfoHandler */ void setDefaultHandler(DataCallback default_handler) { - this->default_handler = default_handler; + this->_default_handler = default_handler; } /*! From fe61b346da76b330be63c37dd6ebb1238fee5799 Mon Sep 17 00:00:00 2001 From: John Harrison Date: Tue, 17 Jan 2012 12:35:32 -0600 Subject: [PATCH 36/72] Fixing timeouts, there were not functioning correctly. --- src/impl/unix.cc | 8 +++++--- tests/serial_tests.cc | 6 ++++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/impl/unix.cc b/src/impl/unix.cc index 5a7c89f..ed05141 100644 --- a/src/impl/unix.cc +++ b/src/impl/unix.cc @@ -236,7 +236,7 @@ Serial::SerialImpl::read (char* buf, size_t size) FD_SET (fd_, &readfds); struct timeval timeout; timeout.tv_sec = timeout_ / 1000; - timeout.tv_usec = static_cast (timeout_ % 1000); + timeout.tv_usec = static_cast (timeout_ % 1000) * 1000; int r = select (fd_ + 1, &readfds, NULL, NULL, &timeout); if (r == -1 && errno == EINTR) @@ -265,6 +265,10 @@ Serial::SerialImpl::read (char* buf, size_t size) } break; } + else + { + break; + } } return static_cast (bytes_read); } @@ -298,8 +302,6 @@ void Serial::SerialImpl::setTimeout (long timeout) { timeout_ = timeout; - if (isOpen_) - reconfigurePort (); } long diff --git a/tests/serial_tests.cc b/tests/serial_tests.cc index 184d99d..d52e1e6 100644 --- a/tests/serial_tests.cc +++ b/tests/serial_tests.cc @@ -11,13 +11,15 @@ using serial::SerialExecption; int main(int argc, char **argv) { try { - Serial s("/dev/tty.usbserial-A900adHq", 115200, 2000); + Serial s("/dev/tty.usbserial-A900adHq", 115200, 100); s.flush(); long long count = 0; while (1) { // size_t available = s.available(); // cout << "avialable: " << available << endl; - string line = s.readline(); + string line = s.read(); + if (line.empty()) + cout << "Nothing\n"; cout << count << ": " << line << line.length() << endl; count++; } From 976307626d640ba5c79bbf0c888ed39560b66aaf Mon Sep 17 00:00:00 2001 From: John Harrison Date: Tue, 17 Jan 2012 15:52:57 -0600 Subject: [PATCH 37/72] =?UTF-8?q?Trying=20to=20do=20a=20first=20pass=20to?= =?UTF-8?q?=20make=20this=20thread=20safe=E2=80=A6=20not=20sure=E2=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/serial/serial.h | 4 ++++ serial.cmake | 5 ----- src/serial.cc | 10 ++++++++++ tests/serial_tests.cc | 2 +- 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/include/serial/serial.h b/include/serial/serial.h index 473907e..fa21c72 100644 --- a/include/serial/serial.h +++ b/include/serial/serial.h @@ -42,6 +42,8 @@ #include #include +#include + namespace serial { /*! @@ -442,6 +444,8 @@ private: // Pimpl idiom, d_pointer class SerialImpl; SerialImpl *pimpl_; + + boost::mutex mut; }; } // namespace serial diff --git a/serial.cmake b/serial.cmake index f6d81a6..8fa0f68 100644 --- a/serial.cmake +++ b/serial.cmake @@ -74,11 +74,6 @@ IF( WIN32 ) target_link_libraries(serial wsock32) ENDIF( ) -# Check for OS X and if so disable kqueue support in asio -IF(CMAKE_SYSTEM_NAME MATCHES Darwin) - add_definitions(-DBOOST_ASIO_DISABLE_KQUEUE) -ENDIF(CMAKE_SYSTEM_NAME MATCHES Darwin) - ## Build Examples # If asked to diff --git a/src/serial.cc b/src/serial.cc index beed5ba..a024538 100644 --- a/src/serial.cc +++ b/src/serial.cc @@ -5,6 +5,8 @@ #include #include +#include + #include "serial/serial.h" #ifdef _WIN32 @@ -29,11 +31,14 @@ using serial::parity_t; using serial::stopbits_t; using serial::flowcontrol_t; +using boost::mutex; + Serial::Serial (const string &port, unsigned long baudrate, long timeout, bytesize_t bytesize, parity_t parity, stopbits_t stopbits, flowcontrol_t flowcontrol, const size_t buffer_size) : buffer_size_(buffer_size) { + mutex::scoped_lock scoped_lock(mut); pimpl_ = new SerialImpl (port, baudrate, timeout, bytesize, parity, stopbits, flowcontrol); read_cache_ = new char[buffer_size_]; @@ -74,6 +79,7 @@ Serial::available () string Serial::read (size_t size) { + mutex::scoped_lock scoped_lock (mut); size_t cache_size = strlen (read_cache_); if (cache_size >= size) { @@ -184,12 +190,14 @@ Serial::readlines(string eol) size_t Serial::write (const string &data) { + mutex::scoped_lock scoped_lock(mut); return pimpl_->write (data); } void Serial::setPort (const string &port) { + mutex::scoped_lock scoped_lock(mut); bool was_open = pimpl_->isOpen(); if (was_open) close(); pimpl_->setPort (port); @@ -275,6 +283,7 @@ Serial::getFlowcontrol () const void Serial::flush () { + mutex::scoped_lock scoped_lock (mut); pimpl_->flush (); memset (read_cache_, 0, buffer_size_); } @@ -286,6 +295,7 @@ void Serial::flushInput () void Serial::flushOutput () { + mutex::scoped_lock scoped_lock (mut); pimpl_->flushOutput (); memset (read_cache_, 0, buffer_size_); } diff --git a/tests/serial_tests.cc b/tests/serial_tests.cc index d52e1e6..42983a1 100644 --- a/tests/serial_tests.cc +++ b/tests/serial_tests.cc @@ -17,7 +17,7 @@ int main(int argc, char **argv) { while (1) { // size_t available = s.available(); // cout << "avialable: " << available << endl; - string line = s.read(); + string line = s.readline(); if (line.empty()) cout << "Nothing\n"; cout << count << ": " << line << line.length() << endl; From 241daf307374130a66aa2e092996e97d1792d32c Mon Sep 17 00:00:00 2001 From: William Woodall Date: Mon, 23 Jan 2012 09:54:31 -0600 Subject: [PATCH 38/72] working on tests and stuff --- examples/serial_example.cc | 18 ++++- include/serial/serial_listener.h | 6 -- src/impl/unix.cc | 2 +- src/serial_listener.cc | 8 +- tests/proof_of_concepts/mdc2250.cc | 1 + tests/serial_listener_tests.cc | 115 ++++++++++++++++------------- 6 files changed, 86 insertions(+), 64 deletions(-) create mode 100644 tests/proof_of_concepts/mdc2250.cc diff --git a/examples/serial_example.cc b/examples/serial_example.cc index 7ebe30c..91ae497 100644 --- a/examples/serial_example.cc +++ b/examples/serial_example.cc @@ -1,9 +1,11 @@ #include #include +#include + #include "serial/serial.h" -int main(int argc, char **argv) +int run(int argc, char **argv) { if(argc < 2) { std::cerr << "Usage: test_serial " << std::endl; @@ -11,6 +13,7 @@ int main(int argc, char **argv) } std::string port(argv[1]); + // port, baudrate, timeout in milliseconds serial::Serial serial(port, 115200, 250); std::cout << "Is the serial port open?"; @@ -23,11 +26,20 @@ int main(int argc, char **argv) while (count >= 0) { int bytes_wrote = serial.write("Testing."); std::string result = serial.read(8); - if(count % 10 == 0) - std::cout << ">" << count << ">" << bytes_wrote << ">" << result << std::endl; + std::cout << ">" << count << ">" << bytes_wrote << ">"; + std::cout << result.length() << "<" << result << std::endl; count += 1; + boost::this_thread::sleep(boost::posix_time::milliseconds(100)); } return 0; } + +int main(int argc, char **argv) { + try { + return run(argc, argv); + } catch (std::exception &e) { + std::cerr << "Unhandled Exception: " << e.what() << std::endl; + } +} diff --git a/include/serial/serial_listener.h b/include/serial/serial_listener.h index cb6307a..da9213a 100644 --- a/include/serial/serial_listener.h +++ b/include/serial/serial_listener.h @@ -643,12 +643,6 @@ private: // exact comparator function static bool _exactly (const std::string& token, std::string exact_str) { - std::cout << token << " == " << exact_str << ": "; - if (token == exact_str) - std::cout << "True"; - else - std::cout << "False"; - std::cout << std::endl; return token == exact_str; } // startswith comparator function diff --git a/src/impl/unix.cc b/src/impl/unix.cc index ed05141..b598024 100644 --- a/src/impl/unix.cc +++ b/src/impl/unix.cc @@ -116,7 +116,7 @@ Serial::SerialImpl::reconfigurePort () else if (bytesize_ == FIVEBITS) options.c_cflag |= CS5; else - throw invalid_argument ("Invalid char len"); + throw invalid_argument ("invalid char len"); // setup stopbits if (stopbits_ == STOPBITS_ONE) options.c_cflag &= (unsigned long) ~(CSTOPB); diff --git a/src/serial_listener.cc b/src/serial_listener.cc index 9a1172a..0f2073c 100644 --- a/src/serial_listener.cc +++ b/src/serial_listener.cc @@ -50,8 +50,8 @@ SerialListener::callback() { std::pair pair; while (this->listening) { if (this->callback_queue.timed_wait_and_pop(pair, 10)) { - std::cout << "Got something off the callback queue: "; - std::cout << (*pair.second) << std::endl; + std::cerr << "Got something off the callback queue: "; + std::cerr << (*pair.second) << std::endl; if (this->listening) { try { pair.first->callback_((*pair.second)); @@ -118,6 +118,10 @@ SerialListener::readSomeData(std::string &temp, size_t this_many) { this->handle_exc(SerialListenerException("Serial port not open.")); } temp = this->serial_port_->read(this_many); + // if (temp.length() > 0) { + std::cerr << "SerialListener read (" << temp.length() << "): "; + std::cerr << temp << std::endl; + // } } void diff --git a/tests/proof_of_concepts/mdc2250.cc b/tests/proof_of_concepts/mdc2250.cc new file mode 100644 index 0000000..ff7ec1b --- /dev/null +++ b/tests/proof_of_concepts/mdc2250.cc @@ -0,0 +1 @@ +#include "" \ No newline at end of file diff --git a/tests/serial_listener_tests.cc b/tests/serial_listener_tests.cc index e1360e2..cca0853 100644 --- a/tests/serial_listener_tests.cc +++ b/tests/serial_listener_tests.cc @@ -1,3 +1,24 @@ +/* To run these tests you need to change the define below to the serial port + * with a loop back device attached. + * + * Alternatively you could use an Arduino: + + void setup() + { + Serial.begin(9600); + } + + void loop() + { + while (Serial.available() > 0) { + Serial.write(Serial.read()); + } + } + + */ + +#define SERIAL_PORT_NAME "/dev/tty.usbserial" + #include "gtest/gtest.h" #include @@ -13,62 +34,47 @@ static size_t global_count, global_listen_count; void default_handler(std::string line) { global_count++; - // std::cout << "default_handler got: " << line << std::endl; + std::cout << "default_handler got: " << line << std::endl; } namespace { -// class SerialListenerTests : public ::testing::Test { -// protected: -// virtual void SetUp() { -// listener.listening = true; -// listener.setDefaultHandler(default_handler); -// listener.callback_thread = -// boost::thread(boost::bind(&SerialListener::callback, &listener)); -// } -// -// virtual void TearDown() { -// listener.listening = false; -// listener.callback_thread.join(); -// } -// -// void stopCallbackThread() { -// while (true) { -// boost::this_thread::sleep(boost::posix_time::milliseconds(1)); -// boost::mutex::scoped_lock lock(listener.callback_queue.the_mutex); -// if (listener.callback_queue.the_queue.empty()) -// break; -// } -// listener.listening = false; -// listener.callback_thread.join(); -// } -// -// void execute_listenForStringOnce() { -// listener.listenForStringOnce("?$1E", 50); -// } -// -// void simulate_loop(std::string input_str) { -// std::vector new_tokens; -// listener.tokenize(input_str, new_tokens); -// listener.filterNewTokens(new_tokens); -// } -// -// SerialListener listener; -// -// }; -// -// TEST_F(SerialListenerTests, handlesPartialMessage) { -// global_count = 0; -// std::string input_str = "?$1E\r$1E=Robo"; -// -// simulate_loop(input_str); -// -// // give some time for the callback thread to finish -// stopCallbackThread(); -// -// ASSERT_EQ(global_count, 1); -// } -// +class SerialListenerTests : public ::testing::Test { +protected: + virtual void SetUp() { + port1 = new Serial(SERIAL_PORT_NAME, 115200, 250); + + listener.setDefaultHandler(default_handler); + listener.startListening((*port1)); + } + + virtual void TearDown() { + listener.stopListening(); + port1->close(); + delete port1; + } + + SerialListener listener; + Serial * port1; + +}; + +void my_sleep(long milliseconds) { + boost::this_thread::sleep(boost::posix_time::milliseconds(milliseconds)); +} + +TEST_F(SerialListenerTests, handlesPartialMessage) { + global_count = 0; + std::string input_str = "?$1E\r$1E=Robo"; + + port1->write(input_str); + + // give some time for the callback thread to finish + my_sleep(1000); + + ASSERT_EQ(1, global_count); +} + // TEST_F(SerialListenerTests, listenForOnceWorks) { // global_count = 0; // @@ -147,6 +153,11 @@ namespace { } // namespace int main(int argc, char **argv) { + try { ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); + } catch (std::exception &e) { + std::cerr << "Unhandled Exception: " << e.what() << std::endl; + } + return 1; } From da2307c8a6f2288a47236553274eb3b1ee64990e Mon Sep 17 00:00:00 2001 From: William Woodall Date: Mon, 23 Jan 2012 11:05:19 -0600 Subject: [PATCH 39/72] Fixing message in serial_example. --- tests/serial_listener_tests.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/serial_listener_tests.cc b/tests/serial_listener_tests.cc index cca0853..64d41b5 100644 --- a/tests/serial_listener_tests.cc +++ b/tests/serial_listener_tests.cc @@ -5,7 +5,7 @@ void setup() { - Serial.begin(9600); + Serial.begin(115200); } void loop() From 5324caf2a3547f5a088304c3823e86b109808b0e Mon Sep 17 00:00:00 2001 From: John Harrison Date: Mon, 23 Jan 2012 11:55:17 -0600 Subject: [PATCH 40/72] Updating the example to allow variable bauds --- examples/serial_example.cc | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/examples/serial_example.cc b/examples/serial_example.cc index 91ae497..af7b666 100644 --- a/examples/serial_example.cc +++ b/examples/serial_example.cc @@ -7,14 +7,17 @@ int run(int argc, char **argv) { - if(argc < 2) { - std::cerr << "Usage: test_serial " << std::endl; + if(argc < 3) { + std::cerr << "Usage: test_serial " << std::endl; return 0; } std::string port(argv[1]); - + unsigned long baud = 0; + + sscanf(argv[2], "%lu", &baud); + // port, baudrate, timeout in milliseconds - serial::Serial serial(port, 115200, 250); + serial::Serial serial(port, baud, 250); std::cout << "Is the serial port open?"; if(serial.isOpen()) From 8b2c7d43599236cc01f4c46822c6117fcdfe82ac Mon Sep 17 00:00:00 2001 From: John Harrison Date: Mon, 23 Jan 2012 13:09:14 -0600 Subject: [PATCH 41/72] Updating the error handling. --- include/serial/serial.h | 58 +++++++++++++++++++++++++---------------- src/impl/unix.cc | 40 +++++++++++++++++++++++----- src/serial.cc | 3 ++- 3 files changed, 71 insertions(+), 30 deletions(-) diff --git a/include/serial/serial.h b/include/serial/serial.h index fa21c72..e924abc 100644 --- a/include/serial/serial.h +++ b/include/serial/serial.h @@ -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) {} - virtual const char* what() const throw() { - std::stringstream ss; - ss << "IO Exception " << this->e_what << " failed."; - return ss.str().c_str(); - } + int getErrorNumber () { return errno_; } + + 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 (); + } }; diff --git a/src/impl/unix.cc b/src/impl/unix.cc index b598024..24ad257 100644 --- a/src/impl/unix.cc +++ b/src/impl/unix.cc @@ -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 (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 (n); } diff --git a/src/serial.cc b/src/serial.cc index a024538..c8ac752 100644 --- a/src/serial.cc +++ b/src/serial.cc @@ -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 lines; @@ -339,3 +339,4 @@ bool Serial::getCD () { return pimpl_->getCD (); } + From cac9ae42c003c5be7a51b8cc9820b7949c3ed7cd Mon Sep 17 00:00:00 2001 From: Scott Martin Date: Mon, 23 Jan 2012 14:23:01 -0600 Subject: [PATCH 42/72] Updating tests while testing on linux --- examples/serial_example.cc | 1 + tests/proof_of_concepts/python_serial_test.py | 15 +++++++++++++++ 2 files changed, 16 insertions(+) create mode 100644 tests/proof_of_concepts/python_serial_test.py diff --git a/examples/serial_example.cc b/examples/serial_example.cc index af7b666..17474ca 100644 --- a/examples/serial_example.cc +++ b/examples/serial_example.cc @@ -1,5 +1,6 @@ #include #include +#include #include diff --git a/tests/proof_of_concepts/python_serial_test.py b/tests/proof_of_concepts/python_serial_test.py new file mode 100644 index 0000000..6f92b84 --- /dev/null +++ b/tests/proof_of_concepts/python_serial_test.py @@ -0,0 +1,15 @@ +#!/usr/bin/env python + +import serial, sys + +if len(sys.argv) != 2: + print "python: Usage_serial_test " + sys.exit(1) + +sio = serial.Serial(sys.argv[1], 115200) +sio.timeout = 250 + +while True: + sio.write("Testing.") + print sio.read(8) + From 0ea153a9b4c89039b182ac7beeaf5133f403a796 Mon Sep 17 00:00:00 2001 From: William Woodall Date: Mon, 23 Jan 2012 14:28:14 -0600 Subject: [PATCH 43/72] Updating tests while on the mac --- examples/serial_example.cc | 2 +- serial_ros.cmake | 6 +++ tests/serial_listener_tests.cc | 14 ++++-- tests/serial_tests.cc | 83 +++++++++++++++++++++++----------- 4 files changed, 72 insertions(+), 33 deletions(-) diff --git a/examples/serial_example.cc b/examples/serial_example.cc index 91ae497..35c4a74 100644 --- a/examples/serial_example.cc +++ b/examples/serial_example.cc @@ -24,7 +24,7 @@ int run(int argc, char **argv) int count = 0; while (count >= 0) { - int bytes_wrote = serial.write("Testing."); + size_t bytes_wrote = serial.write("Testing."); std::string result = serial.read(8); std::cout << ">" << count << ">" << bytes_wrote << ">"; std::cout << result.length() << "<" << result << std::endl; diff --git a/serial_ros.cmake b/serial_ros.cmake index 0c69171..029b83a 100644 --- a/serial_ros.cmake +++ b/serial_ros.cmake @@ -42,4 +42,10 @@ rosbuild_add_executable(serial_listener_example examples/serial_listener_example.cc) target_link_libraries(serial_listener_example ${PROJECT_NAME}) +# Create unit tests +rosbuild_add_gtest(serial_tests tests/serial_tests.cc) +target_link_libraries(serial_tests ${PROJECT_NAME}) +rosbuild_add_gtest(serial_listener_tests tests/serial_listener_tests.cc) +target_link_libraries(serial_listener_tests ${PROJECT_NAME}) + endmacro(build_serial) diff --git a/tests/serial_listener_tests.cc b/tests/serial_listener_tests.cc index 64d41b5..ee84c9c 100644 --- a/tests/serial_listener_tests.cc +++ b/tests/serial_listener_tests.cc @@ -17,7 +17,7 @@ */ -#define SERIAL_PORT_NAME "/dev/tty.usbserial" +#define SERIAL_PORT_NAME "/dev/tty.usbserial-A900cfJA" #include "gtest/gtest.h" @@ -39,11 +39,19 @@ void default_handler(std::string line) { namespace { + +void my_sleep(long milliseconds) { + boost::this_thread::sleep(boost::posix_time::milliseconds(milliseconds)); +} + class SerialListenerTests : public ::testing::Test { protected: virtual void SetUp() { port1 = new Serial(SERIAL_PORT_NAME, 115200, 250); + // Need to wait a bit for the Arduino to come up + my_sleep(1000); + listener.setDefaultHandler(default_handler); listener.startListening((*port1)); } @@ -59,10 +67,6 @@ protected: }; -void my_sleep(long milliseconds) { - boost::this_thread::sleep(boost::posix_time::milliseconds(milliseconds)); -} - TEST_F(SerialListenerTests, handlesPartialMessage) { global_count = 0; std::string input_str = "?$1E\r$1E=Robo"; diff --git a/tests/serial_tests.cc b/tests/serial_tests.cc index 42983a1..6a5a0c5 100644 --- a/tests/serial_tests.cc +++ b/tests/serial_tests.cc @@ -1,35 +1,64 @@ -#include -#include +/* To run these tests you need to change the define below to the serial port + * with a loop back device attached. + * + * Alternatively you could use an Arduino: + + void setup() + { + Serial.begin(115200); + } + + void loop() + { + while (Serial.available() > 0) { + Serial.write(Serial.read()); + } + } + + */ + +#define SERIAL_PORT_NAME "/dev/tty.usbserial" + +#include "gtest/gtest.h" + +#include + +// OMG this is so nasty... +#define private public +#define protected public #include "serial/serial.h" +using namespace serial; -using std::string; -using std::cout; -using std::endl; -using serial::Serial; -using serial::SerialExecption; +namespace { + +class SerialTests : public ::testing::Test { +protected: + virtual void SetUp() { + port1 = new Serial(SERIAL_PORT_NAME, 115200, 250); + } + + virtual void TearDown() { + port1->close(); + delete port1; + } + + Serial * port1; + +}; + +// TEST_F(SerialTests, throwsOnInvalidPort) { +// +// } + +} // namespace int main(int argc, char **argv) { try { - Serial s("/dev/tty.usbserial-A900adHq", 115200, 100); - s.flush(); - long long count = 0; - while (1) { - // size_t available = s.available(); - // cout << "avialable: " << available << endl; - string line = s.readline(); - if (line.empty()) - cout << "Nothing\n"; - cout << count << ": " << line << line.length() << endl; - count++; - } - } - catch (SerialExecption e) - { - cout << "Caught SerialException: " << e.what() << endl; - } - catch (...) - { - cout << "Caught an error." << endl; + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); + } catch (std::exception &e) { + std::cerr << "Unhandled Exception: " << e.what() << std::endl; } + return 1; } From 49ae05877072aeaa500546415bf950416b20694e Mon Sep 17 00:00:00 2001 From: John Harrison Date: Mon, 23 Jan 2012 14:28:16 -0600 Subject: [PATCH 44/72] Found an error in my buffering code and added a timeout to write --- src/impl/unix.cc | 54 ++++++++++++++++++++++++++++++++++++------------ src/serial.cc | 4 ++-- 2 files changed, 43 insertions(+), 15 deletions(-) diff --git a/src/impl/unix.cc b/src/impl/unix.cc index 24ad257..207eaf0 100644 --- a/src/impl/unix.cc +++ b/src/impl/unix.cc @@ -265,7 +265,6 @@ Serial::SerialImpl::read (char* buf, size_t size) bytes_read = ::read (fd_, buf, size); // read should always return some data as select reported it was // ready to read when we get to this point. - // printf("bytes_read: %lu\n", bytes_read); if (bytes_read < 1) { // Disconnected devices, at least on Linux, show the @@ -291,25 +290,54 @@ Serial::SerialImpl::write (const string &data) { throw PortNotOpenedException ("Serial::write"); } - - ssize_t n = ::write (fd_, data.c_str (), data.length ()); - - if (n != static_cast (data.length ())) + + fd_set writefds; + ssize_t bytes_written = 0; + while (true) { - throw IOException ("Write did not complete"); - } - else if (n == -1) - { - if (errno == EINTR) + if (timeout_ != -1) { - return write (data); + FD_ZERO (&writefds); + FD_SET (fd_, &writefds); + struct timeval timeout; + timeout.tv_sec = timeout_ / 1000; + timeout.tv_usec = static_cast (timeout_ % 1000) * 1000; + int r = select (fd_ + 1, NULL, &writefds, NULL, &timeout); + + if (r == -1 && errno == EINTR) + continue; + + if (r == -1) + { + throw IOException (errno); + } + } + + if (timeout_ == -1 || FD_ISSET (fd_, &writefds)) + { + bytes_written = ::write (fd_, data.c_str (), data.length ()); + // read should always return some data as select reported it was + // ready to read when we get to this point. + if (bytes_written < 1) + { + // Disconnected devices, at least on Linux, show the + // behavior that they are always ready to read immediately + // but reading returns nothing. + throw SerialExecption ("device reports readiness to read but " + "returned no data (device disconnected?)"); + } + break; } else { - throw IOException (errno); + break; } } - return static_cast (n); + if (bytes_written != static_cast (data.length ())) + { + throw IOException ("Write did not complete"); + } + return static_cast (bytes_written); } void diff --git a/src/serial.cc b/src/serial.cc index c8ac752..6e79459 100644 --- a/src/serial.cc +++ b/src/serial.cc @@ -91,7 +91,7 @@ Serial::read (size_t size) } else { - // Needs to read, loop until we have read enough... or timeout + // Needs to read, loop until we have read enough or timeout size_t chars_left = 0; string result = ""; @@ -114,8 +114,8 @@ Serial::read (size_t size) *(read_cache_ + chars_read) = '\0'; if (chars_left > chars_read) { + result.append (read_cache_, chars_read); memset (read_cache_, 0, buffer_size_); - result.append (read_cache_); chars_left -= chars_read; } else From 35c93caf42cdccaefcf5e60d9d6ffd8a6bc9c8a0 Mon Sep 17 00:00:00 2001 From: William Woodall Date: Mon, 23 Jan 2012 15:28:01 -0600 Subject: [PATCH 45/72] Still working on the tests --- include/serial/serial.h | 2 +- tests/serial_listener_tests.cc | 34 +++++++++++++++++----------------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/include/serial/serial.h b/include/serial/serial.h index e924abc..3b4c20d 100644 --- a/include/serial/serial.h +++ b/include/serial/serial.h @@ -240,7 +240,7 @@ public: * \return A std::string containing the line. */ std::string - readline(size_t size = std::numeric_limits::max(), + readline(size_t size = std::numeric_limits::max(), std::string eol = "\n"); /*! Reads in multiple lines until the serail port times out. diff --git a/tests/serial_listener_tests.cc b/tests/serial_listener_tests.cc index ee84c9c..e6197ed 100644 --- a/tests/serial_listener_tests.cc +++ b/tests/serial_listener_tests.cc @@ -2,22 +2,22 @@ * with a loop back device attached. * * Alternatively you could use an Arduino: - - void setup() - { - Serial.begin(115200); - } - - void loop() - { - while (Serial.available() > 0) { - Serial.write(Serial.read()); - } - } - + * + * void setup() + * { + * Serial.begin(115200); + * } + * + * void loop() + * { + * while (Serial.available() > 0) { + * Serial.write(Serial.read()); + * } + * } + * */ -#define SERIAL_PORT_NAME "/dev/tty.usbserial-A900cfJA" +#define SERIAL_PORT_NAME "/dev/tty.usbserial" #include "gtest/gtest.h" @@ -50,7 +50,7 @@ protected: port1 = new Serial(SERIAL_PORT_NAME, 115200, 250); // Need to wait a bit for the Arduino to come up - my_sleep(1000); + // my_sleep(1000); listener.setDefaultHandler(default_handler); listener.startListening((*port1)); @@ -71,10 +71,10 @@ TEST_F(SerialListenerTests, handlesPartialMessage) { global_count = 0; std::string input_str = "?$1E\r$1E=Robo"; - port1->write(input_str); + ASSERT_EQ(input_str.length(), port1->write(input_str)); // give some time for the callback thread to finish - my_sleep(1000); + my_sleep(2000); ASSERT_EQ(1, global_count); } From 9b0fdfc2f57d514b2766f31a693a5ae9dc9d3a62 Mon Sep 17 00:00:00 2001 From: John Harrison Date: Mon, 23 Jan 2012 16:17:57 -0600 Subject: [PATCH 46/72] Validate bauds and add the ability to set custom bauds. --- src/impl/unix.cc | 133 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 127 insertions(+), 6 deletions(-) diff --git a/src/impl/unix.cc b/src/impl/unix.cc index 207eaf0..394ad05 100644 --- a/src/impl/unix.cc +++ b/src/impl/unix.cc @@ -31,6 +31,7 @@ using serial::SerialExecption; using serial::PortNotOpenedException; using serial::IOException; + Serial::SerialImpl::SerialImpl (const string &port, unsigned long baudrate, long timeout, bytesize_t bytesize, parity_t parity, stopbits_t stopbits, @@ -114,8 +115,131 @@ Serial::SerialImpl::reconfigurePort () #endif // setup baud rate - // TODO(ash_git): validate baud rate - cfsetspeed(&options, baudrate_); + bool custom_baud = false; + speed_t baud; + switch (baudrate_) + { +#ifdef B0 + case 0: baud = B0; break; +#endif +#ifdef B50 + case 50: baud = B50; break; +#endif +#ifdef B75 + case 75: baud = B75; break; +#endif +#ifdef B110 + case 110: baud = B110; break; +#endif +#ifdef B134 + case 134: baud = B134; break; +#endif +#ifdef B150 + case 150: baud = B150; break; +#endif +#ifdef B200 + case 200: baud = B200; break; +#endif +#ifdef B300 + case 300: baud = B300; break; +#endif +#ifdef B600 + case 600: baud = B600; break; +#endif +#ifdef B1200 + case 1200: baud = B1200; break; +#endif +#ifdef B1800 + case 1800: baud = B1800; break; +#endif +#ifdef B2400 + case 2400: baud = B2400; break; +#endif +#ifdef B4800 + case 4800: baud = B4800; break; +#endif +#ifdef B7200 + case 7200: baud = B7200; break; +#endif +#ifdef B9600 + case 9600: baud = B9600; break; +#endif +#ifdef B14400 + case 14400: baud = B14400; break; +#endif +#ifdef B19200 + case 19200: baud = B19200; break; +#endif +#ifdef B28800 + case 28800: baud = B28800; break; +#endif +#ifdef B57600 + case 57600: baud = B57600; break; +#endif +#ifdef B76800 + case 76800: baud = B76800; break; +#endif +#ifdef B38400 + case 38400: baud = B38400; break; +#endif +#ifdef B115200 + case 115200: baud = B115200; break; +#endif +#ifdef B128000 + case 128000: baud = B128000; break; +#endif +#ifdef B153600 + case 153600: baud = B153600; break; +#endif +#ifdef B230400 + case 230400: baud = B230400; break; +#endif +#ifdef B256000 + case 256000: baud = B256000; break; +#endif +#ifdef B460800 + case 460800: baud = B460800; break; +#endif +#ifdef B921600 + case 921600: baud = B921600; break; +#endif + default: + custom_baud = true; +// Mac OS X 10.x Support +#if defined(__APPLE__) && defined(__MACH__) +#define IOSSIOSPEED _IOW('T', 2, speed_t) + int new_baud = static_cast (baudrate_); + if (ioctl (fd_, IOSSIOSPEED, &new_baud, 1) < 0) + { + throw IOException (errno); + } +// Linux Support +#elif defined(__linux__) + struct serial_struct ser; + ioctl(fd_, TIOCGSERIAL, &ser); + // set custom divisor + ser.custom_divisor = ser.baud_base / baudrate; + // update flags + ser.flags &= ~ASYNC_SPD_MASK; + ser.flags |= ASYNC_SPD_CUST; + + if (ioctl(fd_, TIOCSSERIAL, buf) < 0) + { + throw IOException (errno); + } +#else + throw invalid_argument ("OS does not currently support custom bauds"); +#endif + } + if (custom_baud == false) + { +#ifdef _BSD_SOURCE + ::cfsetspeed(&options, baud); +#else + ::cfsetispeed(&options, baud); + ::cfsetospeed(&options, baud); +#endif + } // setup char len options.c_cflag &= (unsigned long) ~CSIZE; @@ -333,10 +457,7 @@ Serial::SerialImpl::write (const string &data) break; } } - if (bytes_written != static_cast (data.length ())) - { - throw IOException ("Write did not complete"); - } + return static_cast (bytes_written); } From 20f552bc8049be46025c402d1d7f9fddda64d344 Mon Sep 17 00:00:00 2001 From: William Woodall Date: Tue, 24 Jan 2012 12:20:46 -0600 Subject: [PATCH 47/72] Got some serial listener tests working --- examples/serial_example.cc | 2 +- include/serial/serial.h | 8 +- include/serial/serial_listener.h | 6 +- src/impl/unix.cc | 179 +++---------------------------- src/serial_listener.cc | 50 ++++----- tests/serial_listener_tests.cc | 126 +++++++--------------- 6 files changed, 87 insertions(+), 284 deletions(-) diff --git a/examples/serial_example.cc b/examples/serial_example.cc index 27857ff..4499a61 100644 --- a/examples/serial_example.cc +++ b/examples/serial_example.cc @@ -18,7 +18,7 @@ int run(int argc, char **argv) sscanf(argv[2], "%lu", &baud); // port, baudrate, timeout in milliseconds - serial::Serial serial(port, baud, 250); + serial::Serial serial(port, baud, 1000); std::cout << "Is the serial port open?"; if(serial.isOpen()) diff --git a/include/serial/serial.h b/include/serial/serial.h index 3b4c20d..c05dad6 100644 --- a/include/serial/serial.h +++ b/include/serial/serial.h @@ -102,8 +102,10 @@ class IOException : public std::exception const char* e_what_; int errno_; public: - explicit IOException (int errnum) : e_what_ (strerror (errnum)), errno_(errnum) {} - explicit IOException (const char * description) : e_what_ (description), errno_(0) {} + explicit IOException (int errnum) + : e_what_ (strerror (errnum)), errno_(errnum) {} + explicit IOException (const char * description) + : e_what_ (description), errno_(0) {} int getErrorNumber () { return errno_; } @@ -113,7 +115,7 @@ public: if (errno_ == 0) ss << "IO Exception " << e_what_ << " failed."; else - ss << "IO Exception " << errno_ << ":" << e_what_ << " failed."; + ss << "IO Exception (" << errno_ << "): " << e_what_ << " failed."; return ss.str ().c_str (); } }; diff --git a/include/serial/serial_listener.h b/include/serial/serial_listener.h index da9213a..35c93fc 100644 --- a/include/serial/serial_listener.h +++ b/include/serial/serial_listener.h @@ -663,10 +663,8 @@ private: // Gets some data from the serial port void readSomeData (std::string&, size_t); - // Runs the new tokens through the filters - void filterNewTokens (std::vector new_tokens); - // Given a filter_id and a list of tokens, return list of matched tokens - void filter (FilterPtr filter, std::vector &tokens); + // Runs the new_tokens through all the filters + void filter (std::vector &tokens); // Function that loops while listening is true void listen (); // Target of callback thread diff --git a/src/impl/unix.cc b/src/impl/unix.cc index 394ad05..24ad257 100644 --- a/src/impl/unix.cc +++ b/src/impl/unix.cc @@ -31,7 +31,6 @@ using serial::SerialExecption; using serial::PortNotOpenedException; using serial::IOException; - Serial::SerialImpl::SerialImpl (const string &port, unsigned long baudrate, long timeout, bytesize_t bytesize, parity_t parity, stopbits_t stopbits, @@ -115,131 +114,8 @@ Serial::SerialImpl::reconfigurePort () #endif // setup baud rate - bool custom_baud = false; - speed_t baud; - switch (baudrate_) - { -#ifdef B0 - case 0: baud = B0; break; -#endif -#ifdef B50 - case 50: baud = B50; break; -#endif -#ifdef B75 - case 75: baud = B75; break; -#endif -#ifdef B110 - case 110: baud = B110; break; -#endif -#ifdef B134 - case 134: baud = B134; break; -#endif -#ifdef B150 - case 150: baud = B150; break; -#endif -#ifdef B200 - case 200: baud = B200; break; -#endif -#ifdef B300 - case 300: baud = B300; break; -#endif -#ifdef B600 - case 600: baud = B600; break; -#endif -#ifdef B1200 - case 1200: baud = B1200; break; -#endif -#ifdef B1800 - case 1800: baud = B1800; break; -#endif -#ifdef B2400 - case 2400: baud = B2400; break; -#endif -#ifdef B4800 - case 4800: baud = B4800; break; -#endif -#ifdef B7200 - case 7200: baud = B7200; break; -#endif -#ifdef B9600 - case 9600: baud = B9600; break; -#endif -#ifdef B14400 - case 14400: baud = B14400; break; -#endif -#ifdef B19200 - case 19200: baud = B19200; break; -#endif -#ifdef B28800 - case 28800: baud = B28800; break; -#endif -#ifdef B57600 - case 57600: baud = B57600; break; -#endif -#ifdef B76800 - case 76800: baud = B76800; break; -#endif -#ifdef B38400 - case 38400: baud = B38400; break; -#endif -#ifdef B115200 - case 115200: baud = B115200; break; -#endif -#ifdef B128000 - case 128000: baud = B128000; break; -#endif -#ifdef B153600 - case 153600: baud = B153600; break; -#endif -#ifdef B230400 - case 230400: baud = B230400; break; -#endif -#ifdef B256000 - case 256000: baud = B256000; break; -#endif -#ifdef B460800 - case 460800: baud = B460800; break; -#endif -#ifdef B921600 - case 921600: baud = B921600; break; -#endif - default: - custom_baud = true; -// Mac OS X 10.x Support -#if defined(__APPLE__) && defined(__MACH__) -#define IOSSIOSPEED _IOW('T', 2, speed_t) - int new_baud = static_cast (baudrate_); - if (ioctl (fd_, IOSSIOSPEED, &new_baud, 1) < 0) - { - throw IOException (errno); - } -// Linux Support -#elif defined(__linux__) - struct serial_struct ser; - ioctl(fd_, TIOCGSERIAL, &ser); - // set custom divisor - ser.custom_divisor = ser.baud_base / baudrate; - // update flags - ser.flags &= ~ASYNC_SPD_MASK; - ser.flags |= ASYNC_SPD_CUST; - - if (ioctl(fd_, TIOCSSERIAL, buf) < 0) - { - throw IOException (errno); - } -#else - throw invalid_argument ("OS does not currently support custom bauds"); -#endif - } - if (custom_baud == false) - { -#ifdef _BSD_SOURCE - ::cfsetspeed(&options, baud); -#else - ::cfsetispeed(&options, baud); - ::cfsetospeed(&options, baud); -#endif - } + // TODO(ash_git): validate baud rate + cfsetspeed(&options, baudrate_); // setup char len options.c_cflag &= (unsigned long) ~CSIZE; @@ -389,6 +265,7 @@ Serial::SerialImpl::read (char* buf, size_t size) bytes_read = ::read (fd_, buf, size); // read should always return some data as select reported it was // ready to read when we get to this point. + // printf("bytes_read: %lu\n", bytes_read); if (bytes_read < 1) { // Disconnected devices, at least on Linux, show the @@ -414,51 +291,25 @@ Serial::SerialImpl::write (const string &data) { throw PortNotOpenedException ("Serial::write"); } - - fd_set writefds; - ssize_t bytes_written = 0; - while (true) + + ssize_t n = ::write (fd_, data.c_str (), data.length ()); + + if (n != static_cast (data.length ())) { - if (timeout_ != -1) + throw IOException ("Write did not complete"); + } + else if (n == -1) + { + if (errno == EINTR) { - FD_ZERO (&writefds); - FD_SET (fd_, &writefds); - struct timeval timeout; - timeout.tv_sec = timeout_ / 1000; - timeout.tv_usec = static_cast (timeout_ % 1000) * 1000; - int r = select (fd_ + 1, NULL, &writefds, NULL, &timeout); - - if (r == -1 && errno == EINTR) - continue; - - if (r == -1) - { - throw IOException (errno); - } - } - - if (timeout_ == -1 || FD_ISSET (fd_, &writefds)) - { - bytes_written = ::write (fd_, data.c_str (), data.length ()); - // read should always return some data as select reported it was - // ready to read when we get to this point. - if (bytes_written < 1) - { - // Disconnected devices, at least on Linux, show the - // behavior that they are always ready to read immediately - // but reading returns nothing. - throw SerialExecption ("device reports readiness to read but " - "returned no data (device disconnected?)"); - } - break; + return write (data); } else { - break; + throw IOException (errno); } } - - return static_cast (bytes_written); + return static_cast (n); } void diff --git a/src/serial_listener.cc b/src/serial_listener.cc index 0f2073c..fc6c298 100644 --- a/src/serial_listener.cc +++ b/src/serial_listener.cc @@ -50,8 +50,6 @@ SerialListener::callback() { std::pair pair; while (this->listening) { if (this->callback_queue.timed_wait_and_pop(pair, 10)) { - std::cerr << "Got something off the callback queue: "; - std::cerr << (*pair.second) << std::endl; if (this->listening) { try { pair.first->callback_((*pair.second)); @@ -118,37 +116,32 @@ SerialListener::readSomeData(std::string &temp, size_t this_many) { this->handle_exc(SerialListenerException("Serial port not open.")); } temp = this->serial_port_->read(this_many); - // if (temp.length() > 0) { - std::cerr << "SerialListener read (" << temp.length() << "): "; - std::cerr << temp << std::endl; - // } } void -SerialListener::filterNewTokens (std::vector new_tokens) { - // Iterate through the filters, checking each against new tokens +SerialListener::filter(std::vector &tokens) { + // Lock the filters while filtering boost::mutex::scoped_lock lock(filter_mux); - std::vector::iterator it; - for (it=filters.begin(); it!=filters.end(); it++) { - this->filter((*it), new_tokens); - } // for (it=filters.begin(); it!=filters.end(); it++) - // Put the last token back in the data buffer - this->data_buffer = (*new_tokens.back()); -} - -void -SerialListener::filter (FilterPtr filter, std::vector &tokens) -{ - // Iterate through the token uuids and run each against the filter + // Iterate through each new token and filter them std::vector::iterator it; for (it=tokens.begin(); it!=tokens.end(); it++) { - // The last element goes back into the data_buffer, don't filter it - if (it == tokens.end()-1) - continue; TokenPtr token = (*it); - if (filter->comparator_((*token))) - callback_queue.push(std::make_pair(filter,token)); - } + bool matched = false; + // Iterate through each filter + std::vector::iterator itt; + for (itt=filters.begin(); itt!=filters.end(); itt++) { + FilterPtr filter = (*itt); + if (filter->comparator_((*token))) { + callback_queue.push(std::make_pair(filter,token)); + matched = true; + break; + } + } // for (itt=filters.begin(); itt!=filters.end(); itt++) + // If matched is false then send it to the default handler + if (!matched) { + callback_queue.push(std::make_pair(default_filter,token)); + } + } // for (it=tokens.begin(); it!=tokens.end(); it++) } void @@ -166,8 +159,11 @@ SerialListener::listen() { // Call the tokenizer on the updated buffer std::vector new_tokens; this->tokenize(this->data_buffer, new_tokens); + // Put the last token back in the data buffer + this->data_buffer = (*new_tokens.back()); + new_tokens.pop_back(); // Run the new tokens through existing filters - this->filterNewTokens(new_tokens); + this->filter(new_tokens); } // Done parsing lines and buffer should now be set to the left overs } // while (this->listening) diff --git a/tests/serial_listener_tests.cc b/tests/serial_listener_tests.cc index e6197ed..8581dc4 100644 --- a/tests/serial_listener_tests.cc +++ b/tests/serial_listener_tests.cc @@ -17,7 +17,8 @@ * */ -#define SERIAL_PORT_NAME "/dev/tty.usbserial" +// #define SERIAL_PORT_NAME "/dev/tty.usbserial-A900cfJA" +#define SERIAL_PORT_NAME "p0" #include "gtest/gtest.h" @@ -32,6 +33,12 @@ using namespace serial; static size_t global_count, global_listen_count; +void filter_handler(std::string token) { + global_listen_count++; + std::cout << "filter_handler got: " << token << std::endl; + return true; +} + void default_handler(std::string line) { global_count++; std::cout << "default_handler got: " << line << std::endl; @@ -39,7 +46,6 @@ void default_handler(std::string line) { namespace { - void my_sleep(long milliseconds) { boost::this_thread::sleep(boost::posix_time::milliseconds(milliseconds)); } @@ -47,10 +53,8 @@ void my_sleep(long milliseconds) { class SerialListenerTests : public ::testing::Test { protected: virtual void SetUp() { - port1 = new Serial(SERIAL_PORT_NAME, 115200, 250); - - // Need to wait a bit for the Arduino to come up - // my_sleep(1000); + port1 = new Serial("/dev/pty"SERIAL_PORT_NAME, 115200, 10); + port2 = new Serial("/dev/tty"SERIAL_PORT_NAME, 115200, 250); listener.setDefaultHandler(default_handler); listener.startListening((*port1)); @@ -58,12 +62,13 @@ protected: virtual void TearDown() { listener.stopListening(); - port1->close(); delete port1; + delete port2; } SerialListener listener; Serial * port1; + Serial * port2; }; @@ -71,88 +76,39 @@ TEST_F(SerialListenerTests, handlesPartialMessage) { global_count = 0; std::string input_str = "?$1E\r$1E=Robo"; - ASSERT_EQ(input_str.length(), port1->write(input_str)); - - // give some time for the callback thread to finish - my_sleep(2000); + std::cout << "writing: ?$1E$1E=Robo" << std::endl; + port2->write(input_str); + // Allow time for processing + my_sleep(50); ASSERT_EQ(1, global_count); + + input_str = "?$1E\r$1E=Roboteq\r"; + std::cout << "writing: ?$1E$1E=Roboteq" << std::endl; + port2->write(input_str); + // Allow time for processing + my_sleep(50); + + ASSERT_EQ(3, global_count); } -// TEST_F(SerialListenerTests, listenForOnceWorks) { -// global_count = 0; -// -// boost::thread t( -// boost::bind(&SerialListenerTests::execute_listenForStringOnce, this)); -// -// boost::this_thread::sleep(boost::posix_time::milliseconds(5)); -// -// simulate_loop("\r+\r?$1E\r$1E=Robo"); -// -// ASSERT_TRUE(t.timed_join(boost::posix_time::milliseconds(60))); -// -// // Make sure the filters are getting deleted -// ASSERT_EQ(listener.filters.size(), 0); -// -// // give some time for the callback thread to finish -// stopCallbackThread(); -// -// ASSERT_EQ(global_count, 1); -// } -// -// // lookForOnce should not find it, but timeout after 1000ms, so it should -// // still join. -// TEST_F(SerialListenerTests, listenForOnceTimesout) { -// global_count = 0; -// -// boost::thread t( -// boost::bind(&SerialListenerTests::execute_listenForStringOnce, this)); -// -// boost::this_thread::sleep(boost::posix_time::milliseconds(55)); -// -// simulate_loop("\r+\r?$1ENOTRIGHT\r$1E=Robo"); -// -// ASSERT_TRUE(t.timed_join(boost::posix_time::milliseconds(60))); -// -// // give some time for the callback thread to finish -// stopCallbackThread(); -// -// ASSERT_EQ(global_count, 2); -// } -// -// bool listenForComparator(std::string line) { -// // std::cout << "In listenForComparator(" << line << ")" << std::endl; -// if (line.substr(0,2) == "V=") { -// return true; -// } -// return false; -// } -// -// void listenForCallback(std::string line) { -// // std::cout << "In listenForCallback(" << line << ")" << std::endl; -// global_listen_count++; -// } -// -// TEST_F(SerialListenerTests, listenForWorks) { -// global_count = 0; -// global_listen_count = 0; -// -// FilterPtr filt_uuid = -// listener.listenFor(listenForComparator, listenForCallback); -// -// simulate_loop("\r+\rV=05:06\r?$1E\rV=06:05\r$1E=Robo"); -// -// // give some time for the callback thread to finish -// stopCallbackThread(); -// -// ASSERT_EQ(global_count, 2); -// ASSERT_EQ(global_listen_count, 2); -// -// listener.stopListeningFor(filt_uuid); -// -// ASSERT_EQ(listener.filters.size(), 0); -// -// } +TEST_F(SerialListenerTests, normalFilterWorks) { + global_count = 0; + std::string input_str = "?$1E\r$1E=Robo\rV=1334:1337\rT=123"; + + // Setup filter + FilterPtr filt_1 = + listener.createFilter(SerialListener::startsWith("V="), filter_handler); + + std::cout << "writing: ?$1E$1E=RoboV=1334:1337T=123"; + std::cout << std::endl; + port2->write(input_str); + // Allow time for processing + my_sleep(50); + + ASSERT_EQ(2, global_count); + ASSERT_EQ(1, global_listen_count); +} } // namespace From bca4eba6724ee3c07abf3463b5aa3f40b77b5199 Mon Sep 17 00:00:00 2001 From: John Harrison Date: Tue, 24 Jan 2012 12:26:03 -0600 Subject: [PATCH 48/72] Reverting the deletion of the baud rate validation. --- src/impl/unix.cc | 180 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 165 insertions(+), 15 deletions(-) diff --git a/src/impl/unix.cc b/src/impl/unix.cc index 24ad257..28243de 100644 --- a/src/impl/unix.cc +++ b/src/impl/unix.cc @@ -31,6 +31,7 @@ using serial::SerialExecption; using serial::PortNotOpenedException; using serial::IOException; + Serial::SerialImpl::SerialImpl (const string &port, unsigned long baudrate, long timeout, bytesize_t bytesize, parity_t parity, stopbits_t stopbits, @@ -114,8 +115,131 @@ Serial::SerialImpl::reconfigurePort () #endif // setup baud rate - // TODO(ash_git): validate baud rate - cfsetspeed(&options, baudrate_); + bool custom_baud = false; + speed_t baud; + switch (baudrate_) + { +#ifdef B0 + case 0: baud = B0; break; +#endif +#ifdef B50 + case 50: baud = B50; break; +#endif +#ifdef B75 + case 75: baud = B75; break; +#endif +#ifdef B110 + case 110: baud = B110; break; +#endif +#ifdef B134 + case 134: baud = B134; break; +#endif +#ifdef B150 + case 150: baud = B150; break; +#endif +#ifdef B200 + case 200: baud = B200; break; +#endif +#ifdef B300 + case 300: baud = B300; break; +#endif +#ifdef B600 + case 600: baud = B600; break; +#endif +#ifdef B1200 + case 1200: baud = B1200; break; +#endif +#ifdef B1800 + case 1800: baud = B1800; break; +#endif +#ifdef B2400 + case 2400: baud = B2400; break; +#endif +#ifdef B4800 + case 4800: baud = B4800; break; +#endif +#ifdef B7200 + case 7200: baud = B7200; break; +#endif +#ifdef B9600 + case 9600: baud = B9600; break; +#endif +#ifdef B14400 + case 14400: baud = B14400; break; +#endif +#ifdef B19200 + case 19200: baud = B19200; break; +#endif +#ifdef B28800 + case 28800: baud = B28800; break; +#endif +#ifdef B57600 + case 57600: baud = B57600; break; +#endif +#ifdef B76800 + case 76800: baud = B76800; break; +#endif +#ifdef B38400 + case 38400: baud = B38400; break; +#endif +#ifdef B115200 + case 115200: baud = B115200; break; +#endif +#ifdef B128000 + case 128000: baud = B128000; break; +#endif +#ifdef B153600 + case 153600: baud = B153600; break; +#endif +#ifdef B230400 + case 230400: baud = B230400; break; +#endif +#ifdef B256000 + case 256000: baud = B256000; break; +#endif +#ifdef B460800 + case 460800: baud = B460800; break; +#endif +#ifdef B921600 + case 921600: baud = B921600; break; +#endif + default: + custom_baud = true; +// Mac OS X 10.x Support +#if defined(__APPLE__) && defined(__MACH__) +#define IOSSIOSPEED _IOW('T', 2, speed_t) + int new_baud = static_cast (baudrate_); + if (ioctl (fd_, IOSSIOSPEED, &new_baud, 1) < 0) + { + throw IOException (errno); + } +// Linux Support +#elif defined(__linux__) + struct serial_struct ser; + ioctl(fd_, TIOCGSERIAL, &ser); + // set custom divisor + ser.custom_divisor = ser.baud_base / baudrate; + // update flags + ser.flags &= ~ASYNC_SPD_MASK; + ser.flags |= ASYNC_SPD_CUST; + + if (ioctl(fd_, TIOCSSERIAL, buf) < 0) + { + throw IOException (errno); + } +#else + throw invalid_argument ("OS does not currently support custom bauds"); +#endif + } + if (custom_baud == false) + { +#ifdef _BSD_SOURCE + ::cfsetspeed(&options, baud); +#else + ::cfsetispeed(&options, baud); + ::cfsetospeed(&options, baud); +#endif + } // setup char len options.c_cflag &= (unsigned long) ~CSIZE; @@ -265,7 +389,6 @@ Serial::SerialImpl::read (char* buf, size_t size) bytes_read = ::read (fd_, buf, size); // read should always return some data as select reported it was // ready to read when we get to this point. - // printf("bytes_read: %lu\n", bytes_read); if (bytes_read < 1) { // Disconnected devices, at least on Linux, show the @@ -291,25 +414,51 @@ Serial::SerialImpl::write (const string &data) { throw PortNotOpenedException ("Serial::write"); } - - ssize_t n = ::write (fd_, data.c_str (), data.length ()); - - if (n != static_cast (data.length ())) + + fd_set writefds; + ssize_t bytes_written = 0; + while (true) { - throw IOException ("Write did not complete"); - } - else if (n == -1) - { - if (errno == EINTR) + if (timeout_ != -1) { - return write (data); + FD_ZERO (&writefds); + FD_SET (fd_, &writefds); + struct timeval timeout; + timeout.tv_sec = timeout_ / 1000; + timeout.tv_usec = static_cast (timeout_ % 1000) * 1000; + int r = select (fd_ + 1, NULL, &writefds, NULL, &timeout); + + if (r == -1 && errno == EINTR) + continue; + + if (r == -1) + { + throw IOException (errno); + } + } + + if (timeout_ == -1 || FD_ISSET (fd_, &writefds)) + { + bytes_written = ::write (fd_, data.c_str (), data.length ()); + // read should always return some data as select reported it was + // ready to read when we get to this point. + if (bytes_written < 1) + { + // Disconnected devices, at least on Linux, show the + // behavior that they are always ready to read immediately + // but reading returns nothing. + throw SerialExecption ("device reports readiness to read but " + "returned no data (device disconnected?)"); + } + break; } else { - throw IOException (errno); + break; } } - return static_cast (n); + + return static_cast (bytes_written); } void @@ -538,3 +687,4 @@ Serial::SerialImpl::getCD() int s = ioctl (fd_, TIOCMGET, 0); return (s & TIOCM_CD) != 0; } + From 0d464469ccdd214777f8a8da9f6cc451e3a2ae9c Mon Sep 17 00:00:00 2001 From: William Woodall Date: Tue, 24 Jan 2012 14:19:09 -0600 Subject: [PATCH 49/72] Serial listener tests complete and working --- include/serial/serial_listener.h | 14 ++-- tests/serial_listener_tests.cc | 117 ++++++++++++++++++++++++++++++- 2 files changed, 125 insertions(+), 6 deletions(-) diff --git a/include/serial/serial_listener.h b/include/serial/serial_listener.h index 35c93fc..c62910f 100644 --- a/include/serial/serial_listener.h +++ b/include/serial/serial_listener.h @@ -808,12 +808,15 @@ public: * \return std::string token that was matched or "" if none were matched. */ std::string wait(long ms) { - if (ms == 0) - if (!this->queue.try_pop(this->result)) + if (ms == 0) { + if (!this->queue.try_pop(this->result)) { this->result = ""; - else - if (!this->queue.timed_wait_and_pop(this->result, ms)) + } + } else { + if (!this->queue.timed_wait_and_pop(this->result, ms)) { this->result = ""; + } + } return result; } @@ -842,8 +845,9 @@ public: void callback(const std::string &token) { std::string throw_away; - if (this->queue.size() == this->buffer_size_) + if (this->queue.size() == this->buffer_size_) { this->queue.wait_and_pop(throw_away); + } this->queue.push(token); } diff --git a/tests/serial_listener_tests.cc b/tests/serial_listener_tests.cc index 8581dc4..52e12f1 100644 --- a/tests/serial_listener_tests.cc +++ b/tests/serial_listener_tests.cc @@ -32,11 +32,11 @@ using namespace serial; static size_t global_count, global_listen_count; +static bool matched; void filter_handler(std::string token) { global_listen_count++; std::cout << "filter_handler got: " << token << std::endl; - return true; } void default_handler(std::string line) { @@ -94,6 +94,7 @@ TEST_F(SerialListenerTests, handlesPartialMessage) { TEST_F(SerialListenerTests, normalFilterWorks) { global_count = 0; + global_listen_count = 0; std::string input_str = "?$1E\r$1E=Robo\rV=1334:1337\rT=123"; // Setup filter @@ -110,6 +111,120 @@ TEST_F(SerialListenerTests, normalFilterWorks) { ASSERT_EQ(1, global_listen_count); } +void run_blocking_filter(BlockingFilterPtr filt_1) { + // Wait 100 ms for a match + std::string temp = filt_1->wait(100); + if (temp.empty()) { + return; + } + std::cout << "blocking filter matched: " << temp << std::endl; + global_listen_count++; + matched = true; +} + +TEST_F(SerialListenerTests, blockingFilterWorks) { + global_count = 0; + global_listen_count = 0; + matched = false; + std::string input_str = "?$1E\r$1E=Robo\rV=1334:1337\rT=123"; + + // Setup blocking filter + BlockingFilterPtr filt_1 = + listener.createBlockingFilter(SerialListener::startsWith("$1E=")); + + boost::thread t(boost::bind(run_blocking_filter, filt_1)); + + std::cout << "writing: ?$1E$1E=RoboV=1334:1337T=123"; + std::cout << std::endl; + port2->write(input_str); + // Allow time for processing + my_sleep(50); + + using boost::posix_time::milliseconds; + ASSERT_TRUE(t.timed_join(milliseconds(10))); + ASSERT_EQ(2, global_count); + ASSERT_EQ(1, global_listen_count); + ASSERT_TRUE(matched); +} + +TEST_F(SerialListenerTests, blockingFilterTimesOut) { + global_count = 0; + global_listen_count = 0; + matched = false; + std::string input_str = "?$1E\r$1E=Robo\rV=1334:1337\rT=123"; + + // Setup blocking filter + BlockingFilterPtr filt_1 = + listener.createBlockingFilter(SerialListener::startsWith("T=")); + + boost::thread t(boost::bind(run_blocking_filter, filt_1)); + + std::cout << "writing: ?$1E$1E=RoboV=1334:1337T=123"; + std::cout << std::endl; + port2->write(input_str); + // Allow time for processing + my_sleep(50); + + using boost::posix_time::milliseconds; + // First one should not be within timeout, should be false + ASSERT_FALSE(t.timed_join(milliseconds(10))); + // Second one should capture timeout and return true to join + ASSERT_TRUE(t.timed_join(milliseconds(60))); + ASSERT_EQ(3, global_count); + ASSERT_EQ(0, global_listen_count); + ASSERT_FALSE(matched); +} + +void write_later(Serial *port, std::string input_str, long wait_for) { + my_sleep(wait_for); + port->write(input_str); +} + +TEST_F(SerialListenerTests, bufferedFilterWorks) { + global_count = 0; + std::string input_str = "?$1E\r+\r$1E=Robo\rV=1334:1337\rT=123"; + + // Setup buffered filter, buffer size 3 + BufferedFilterPtr filt_1 = + listener.createBufferedFilter(SerialListener::exactly("+"), 3); + + // Write the string to the port 10 ms in the future + boost::thread t(boost::bind(write_later, port2, input_str, 10)); + + // This should be empty because of a timeout + ASSERT_TRUE(filt_1->wait(2).empty()); + // Make sure wait works properly + ASSERT_EQ("+", filt_1->wait(20)); + // This should be empty cause there was only one + ASSERT_TRUE(filt_1->wait(2).empty()); + // The queue in the filter should be empty + ASSERT_EQ(0, filt_1->queue.size()); + ASSERT_EQ(3, global_count); + t.join(); +} + +TEST_F(SerialListenerTests, bufferedFilterQueueWorks) { + global_count = 0; + std::string input_str = "?$1E$\r+\r$1E=Robo$\rV=1334:1337$\rT=123$\r"; + + // Setup buffered filter, buffer size 3 + BufferedFilterPtr filt_1 = + listener.createBufferedFilter(SerialListener::endsWith("$"), 3); + + // write the string + port2->write(input_str); + + my_sleep(20); // Let things process + // There should have been four matches + // therefore the first one should the second match. + ASSERT_EQ("$1E=Robo$", filt_1->wait(1)); + ASSERT_EQ("V=1334:1337$", filt_1->wait(1)); + ASSERT_EQ("T=123$", filt_1->wait(1)); + ASSERT_EQ(0, filt_1->queue.size()); + + ASSERT_EQ(1, global_count); +} + } // namespace int main(int argc, char **argv) { From f610fb79ff07e7d9c88530e294ccfccbc199354f Mon Sep 17 00:00:00 2001 From: William Woodall Date: Tue, 24 Jan 2012 23:05:28 -0600 Subject: [PATCH 50/72] fixing empty token bug, and adding some debugging stuff. --- include/serial/serial_listener.h | 59 +++++++++++++++++++++++++++++++- src/serial_listener.cc | 17 +++------ 2 files changed, 62 insertions(+), 14 deletions(-) diff --git a/include/serial/serial_listener.h b/include/serial/serial_listener.h index c62910f..022623b 100644 --- a/include/serial/serial_listener.h +++ b/include/serial/serial_listener.h @@ -36,9 +36,14 @@ #ifndef SERIAL_LISTENER_H #define SERIAL_LISTENER_H +#ifndef SERIAL_LISTENER_DEBUG +#define SERIAL_LISTENER_DEBUG 0 +#endif + // STL #include #include +#include // Serial #include @@ -49,6 +54,10 @@ #include #include +#if SERIAL_LISTENER_DEBUG +# warning SerialListener in debug mode +#endif + namespace serial { /*! @@ -643,16 +652,46 @@ private: // exact comparator function static bool _exactly (const std::string& token, std::string exact_str) { +#if SERIAL_LISTENER_DEBUG + std::cerr << "In exactly callback(" << token.length() << "): "; + std::cerr << token << " == " << exact_str << ": "; + if (token == exact_str) + std::cerr << "True"; + else + std::cerr << "False"; + std::cerr << std::endl; +#endif return token == exact_str; } // startswith comparator function static bool _startsWith (const std::string& token, std::string prefix) { +#if SERIAL_LISTENER_DEBUG + std::cerr << "In startsWith callback(" << token.length() << "): "; + std::cerr << token << " starts with " << prefix; + std::cerr << "?: "; + if (token.substr(0,prefix.length()) == prefix) + std::cerr << "True"; + else + std::cerr << "False"; + std::cerr << std::endl; +#endif return token.substr(0,prefix.length()) == prefix; } // endswith comparator function static bool _endsWith (const std::string& token, std::string postfix) { +#if SERIAL_LISTENER_DEBUG + std::cerr << "In endsWith callback("; + std::cerr << token.length(); + std::cerr << "): " << token; + std::cerr << " ends with " << postfix << "?: "; + if (token.substr(token.length()-postfix.length()) == postfix) + std::cerr << "True"; + else + std::cerr << "False"; + std::cerr << std::endl; +#endif return token.substr(token.length()-postfix.length()) == postfix; } // contains comparator function @@ -662,7 +701,17 @@ private: } // Gets some data from the serial port - void readSomeData (std::string&, size_t); + void readSomeData (std::string &temp, size_t this_many) { + // Make sure there is a serial port + if (this->serial_port_ == NULL) { + this->handle_exc(SerialListenerException("Invalid serial port.")); + } + // Make sure the serial port is open + if (!this->serial_port_->isOpen()) { + this->handle_exc(SerialListenerException("Serial port not open.")); + } + temp = this->serial_port_->read(this_many); + } // Runs the new_tokens through all the filters void filter (std::vector &tokens); // Function that loops while listening is true @@ -750,6 +799,10 @@ public: FilterPtr filter_ptr; void callback(const std::string& token) { +#if SERIAL_LISTENER_DEBUG + std::cerr << "In BlockingFilter callback(" << token.length() << "): "; + std::cerr << token << std::endl; +#endif this->cond.notify_all(); this->result = token; } @@ -844,6 +897,10 @@ public: FilterPtr filter_ptr; void callback(const std::string &token) { +#if SERIAL_LISTENER_DEBUG + std::cerr << "In BufferedFilter callback(" << token.length() << "): "; + std::cerr << token << std::endl; +#endif std::string throw_away; if (this->queue.size() == this->buffer_size_) { this->queue.wait_and_pop(throw_away); diff --git a/src/serial_listener.cc b/src/serial_listener.cc index fc6c298..bc1258d 100644 --- a/src/serial_listener.cc +++ b/src/serial_listener.cc @@ -105,19 +105,6 @@ SerialListener::determineAmountToRead() { return this->chunk_size_; } -void -SerialListener::readSomeData(std::string &temp, size_t this_many) { - // Make sure there is a serial port - if (this->serial_port_ == NULL) { - this->handle_exc(SerialListenerException("Invalid serial port.")); - } - // Make sure the serial port is open - if (!this->serial_port_->isOpen()) { - this->handle_exc(SerialListenerException("Serial port not open.")); - } - temp = this->serial_port_->read(this_many); -} - void SerialListener::filter(std::vector &tokens) { // Lock the filters while filtering @@ -126,6 +113,10 @@ SerialListener::filter(std::vector &tokens) { std::vector::iterator it; for (it=tokens.begin(); it!=tokens.end(); it++) { TokenPtr token = (*it); + // If it is empty then pass it + if (token->empty()) { + continue; + } bool matched = false; // Iterate through each filter std::vector::iterator itt; From d8874120a527dcc6cf6c694368da0d6d0cbb201a Mon Sep 17 00:00:00 2001 From: John Harrison Date: Fri, 27 Jan 2012 20:21:10 -0600 Subject: [PATCH 51/72] Change the buffer to a generic C++ std::string --- examples/serial_example.cc | 10 +++--- include/serial/serial.h | 6 ++-- src/serial.cc | 71 ++++++++++++++------------------------ 3 files changed, 33 insertions(+), 54 deletions(-) diff --git a/examples/serial_example.cc b/examples/serial_example.cc index 4499a61..0c87081 100644 --- a/examples/serial_example.cc +++ b/examples/serial_example.cc @@ -29,7 +29,7 @@ int run(int argc, char **argv) int count = 0; while (count >= 0) { size_t bytes_wrote = serial.write("Testing."); - std::string result = serial.read(8); + std::string result = serial.readline(); std::cout << ">" << count << ">" << bytes_wrote << ">"; std::cout << result.length() << "<" << result << std::endl; @@ -41,9 +41,9 @@ int run(int argc, char **argv) } int main(int argc, char **argv) { - try { + // try { return run(argc, argv); - } catch (std::exception &e) { - std::cerr << "Unhandled Exception: " << e.what() << std::endl; - } + // } catch (std::exception &e) { + // std::cerr << "Unhandled Exception: " << e.what() << std::endl; + // } } diff --git a/include/serial/serial.h b/include/serial/serial.h index c05dad6..d658d50 100644 --- a/include/serial/serial.h +++ b/include/serial/serial.h @@ -182,8 +182,7 @@ public: bytesize_t bytesize = EIGHTBITS, parity_t parity = PARITY_NONE, stopbits_t stopbits = STOPBITS_ONE, - flowcontrol_t flowcontrol = FLOWCONTROL_NONE, - const size_t buffer_size = 256); + flowcontrol_t flowcontrol = FLOWCONTROL_NONE); /*! Destructor */ virtual ~Serial (); @@ -454,8 +453,7 @@ private: void operator=(const Serial&); const Serial& operator=(Serial); - const size_t buffer_size_; - char *read_cache_; //!< Cache for doing reads in chunks. + std::string read_cache_; //!< Cache for doing reads in chunks. // Pimpl idiom, d_pointer class SerialImpl; diff --git a/src/serial.cc b/src/serial.cc index 6e79459..92623f5 100644 --- a/src/serial.cc +++ b/src/serial.cc @@ -35,20 +35,17 @@ using boost::mutex; Serial::Serial (const string &port, unsigned long baudrate, long timeout, bytesize_t bytesize, parity_t parity, stopbits_t stopbits, - flowcontrol_t flowcontrol, const size_t buffer_size) - : buffer_size_(buffer_size) + flowcontrol_t flowcontrol) + : read_cache_("") { mutex::scoped_lock scoped_lock(mut); pimpl_ = new SerialImpl (port, baudrate, timeout, bytesize, parity, stopbits, flowcontrol); - read_cache_ = new char[buffer_size_]; - memset (read_cache_, 0, buffer_size_ * sizeof (char)); } Serial::~Serial () { - delete pimpl_; - delete[] read_cache_; + delete pimpl_; } void @@ -61,7 +58,6 @@ void Serial::close () { pimpl_->close (); - memset (read_cache_, 0, buffer_size_ * sizeof (char)); } bool @@ -80,57 +76,42 @@ string Serial::read (size_t size) { mutex::scoped_lock scoped_lock (mut); - size_t cache_size = strlen (read_cache_); - if (cache_size >= size) + if (read_cache_.size() >= size) { // Don't need to do a new read. - string result (read_cache_, size); - memmove (read_cache_, read_cache_ + size, cache_size - size); - *(read_cache_ + cache_size - size) = '\0'; + string result = read_cache_.substr (0, size); + read_cache_ = read_cache_.substr (size, read_cache_.size ()); return result; } else { // Needs to read, loop until we have read enough or timeout - size_t chars_left = 0; - string result = ""; - - if (cache_size > 0) - { - result.append (read_cache_, cache_size); - memset (read_cache_, 0, buffer_size_); - chars_left = size - cache_size; - } - else - { - chars_left = size; - } + string result (read_cache_.substr (0, size)); + read_cache_.clear (); while (true) { - size_t chars_read = pimpl_->read (read_cache_, buffer_size_ - 1); + char buf[256]; + size_t chars_read = pimpl_->read (buf, 256); if (chars_read > 0) { - *(read_cache_ + chars_read) = '\0'; - if (chars_left > chars_read) - { - result.append (read_cache_, chars_read); - memset (read_cache_, 0, buffer_size_); - chars_left -= chars_read; - } - else - { - result.append (read_cache_, static_cast (chars_left)); - memmove (read_cache_, read_cache_ + chars_left, chars_read - chars_left); - *(read_cache_ + chars_read - chars_left) = '\0'; - memset (read_cache_ + chars_read - chars_left, 0, - buffer_size_ - chars_read - chars_left); - // Finished reading all of the data - break; - } + read_cache_.append(buf, chars_read); } else break; // Timeout occured + + if (chars_read > size) + { + result.append (read_cache_.substr (0, size)); + read_cache_ = read_cache_.substr (size, read_cache_.size ()); + break; + } + else + { + result.append (read_cache_.substr (0, size)); + read_cache_.clear (); + size -= chars_read; + } } return result; } @@ -285,7 +266,7 @@ void Serial::flush () { mutex::scoped_lock scoped_lock (mut); pimpl_->flush (); - memset (read_cache_, 0, buffer_size_); + read_cache_.clear (); } void Serial::flushInput () @@ -297,7 +278,7 @@ void Serial::flushOutput () { mutex::scoped_lock scoped_lock (mut); pimpl_->flushOutput (); - memset (read_cache_, 0, buffer_size_); + read_cache_.clear (); } void Serial::sendBreak (int duration) From c3a82750e69847c41d61dbc75896c4d99043f861 Mon Sep 17 00:00:00 2001 From: William Woodall Date: Sat, 28 Jan 2012 14:39:55 -0600 Subject: [PATCH 52/72] Some small changes to fix things discovered while testing serial listener. --- include/serial/serial_listener.h | 14 ++++++++++++-- src/serial_listener.cc | 4 +++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/include/serial/serial_listener.h b/include/serial/serial_listener.h index 022623b..7e85b4c 100644 --- a/include/serial/serial_listener.h +++ b/include/serial/serial_listener.h @@ -159,6 +159,12 @@ public: ComparatorType comparator_; DataCallback callback_; + +private: + // Disable copy constructors + Filter(const Filter&); + void operator=(const Filter&); + const Filter& operator=(Filter); }; /*! @@ -173,7 +179,7 @@ public: */ typedef boost::shared_ptr FilterPtr; -class BlockingFilter; +class BlockingFilter; // Forward declaration /*! * Shared Pointer of BlockingFilter, returned by @@ -183,7 +189,7 @@ class BlockingFilter; */ typedef boost::shared_ptr BlockingFilterPtr; -class BufferedFilter; +class BufferedFilter; // Forward declaration /*! * Shared Pointer of BufferedFilter, returned by @@ -637,6 +643,10 @@ public: } private: + // Disable copy constructors + SerialListener(const SerialListener&); + void operator=(const SerialListener&); + const SerialListener& operator=(SerialListener); // delimeter tokenizer function static void _delimeter_tokenizer (const std::string &data, diff --git a/src/serial_listener.cc b/src/serial_listener.cc index bc1258d..a7bbe98 100644 --- a/src/serial_listener.cc +++ b/src/serial_listener.cc @@ -52,7 +52,9 @@ SerialListener::callback() { if (this->callback_queue.timed_wait_and_pop(pair, 10)) { if (this->listening) { try { - pair.first->callback_((*pair.second)); + if (pair.first != NULL && pair.second != NULL) { + pair.first->callback_((*pair.second)); + } } catch (std::exception &e) { this->handle_exc(e); }// try callback From cea751402e9135a4d994c597c82027e22b73ecee Mon Sep 17 00:00:00 2001 From: William Woodall Date: Sun, 29 Jan 2012 15:41:38 -0600 Subject: [PATCH 53/72] Adding some linux specific fixes. fixes #13 --- include/serial/serial.h | 3 +++ src/impl/unix.cc | 8 ++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/include/serial/serial.h b/include/serial/serial.h index c05dad6..b9b033a 100644 --- a/include/serial/serial.h +++ b/include/serial/serial.h @@ -38,6 +38,9 @@ #include #include +#if defined(__linux__) +#include +#endif #include #include #include diff --git a/src/impl/unix.cc b/src/impl/unix.cc index 28243de..dae30de 100644 --- a/src/impl/unix.cc +++ b/src/impl/unix.cc @@ -14,6 +14,10 @@ #include #include +#if defined(__linux__) +#include +#endif + #include "serial/impl/unix.h" #ifndef TIOCINQ @@ -218,12 +222,12 @@ Serial::SerialImpl::reconfigurePort () struct serial_struct ser; ioctl(fd_, TIOCGSERIAL, &ser); // set custom divisor - ser.custom_divisor = ser.baud_base / baudrate; + ser.custom_divisor = ser.baud_base / baudrate_; // update flags ser.flags &= ~ASYNC_SPD_MASK; ser.flags |= ASYNC_SPD_CUST; - if (ioctl(fd_, TIOCSSERIAL, buf) < 0) + if (ioctl(fd_, TIOCSSERIAL, ser) < 0) { throw IOException (errno); } From 119be4630e9b15878fc828c5f0ed82fd36e5cd1d Mon Sep 17 00:00:00 2001 From: John Harrison Date: Mon, 30 Jan 2012 14:53:33 -0600 Subject: [PATCH 54/72] Testing the new buffer --- examples/serial_example.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/serial_example.cc b/examples/serial_example.cc index 0c87081..b3a3636 100644 --- a/examples/serial_example.cc +++ b/examples/serial_example.cc @@ -18,7 +18,7 @@ int run(int argc, char **argv) sscanf(argv[2], "%lu", &baud); // port, baudrate, timeout in milliseconds - serial::Serial serial(port, baud, 1000); + serial::Serial serial(port, baud, 30000); std::cout << "Is the serial port open?"; if(serial.isOpen()) @@ -28,7 +28,7 @@ int run(int argc, char **argv) int count = 0; while (count >= 0) { - size_t bytes_wrote = serial.write("Testing."); + size_t bytes_wrote = serial.write("Testing.\n"); std::string result = serial.readline(); std::cout << ">" << count << ">" << bytes_wrote << ">"; std::cout << result.length() << "<" << result << std::endl; From f7cee5e175dd05fd0f33dcd6aa9ab21b5b1ffed0 Mon Sep 17 00:00:00 2001 From: John Harrison Date: Mon, 30 Jan 2012 16:33:58 -0600 Subject: [PATCH 55/72] Testing my changes to make sure they are consistant. --- examples/serial_example.cc | 84 +++++++++++++++++++++++++++++++++++--- serial.cmake | 2 +- src/impl/unix.cc | 3 ++ src/serial.cc | 2 + 4 files changed, 85 insertions(+), 6 deletions(-) diff --git a/examples/serial_example.cc b/examples/serial_example.cc index b3a3636..788764d 100644 --- a/examples/serial_example.cc +++ b/examples/serial_example.cc @@ -1,11 +1,21 @@ #include #include #include +#include + +#ifdef __MACH__ +#include +#include +#endif #include #include "serial/serial.h" +using std::string; +using std::cout; +using std::endl; + int run(int argc, char **argv) { if(argc < 3) { @@ -18,7 +28,7 @@ int run(int argc, char **argv) sscanf(argv[2], "%lu", &baud); // port, baudrate, timeout in milliseconds - serial::Serial serial(port, baud, 30000); + serial::Serial serial(port, baud, 10000); std::cout << "Is the serial port open?"; if(serial.isOpen()) @@ -28,13 +38,77 @@ int run(int argc, char **argv) int count = 0; while (count >= 0) { - size_t bytes_wrote = serial.write("Testing.\n"); - std::string result = serial.readline(); + struct timespec start, end; + double diff; + + #ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time + clock_serv_t cclock; + mach_timespec_t mts; + host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock); + clock_get_time(cclock, &mts); + mach_port_deallocate(mach_task_self(), cclock); + start.tv_sec = mts.tv_sec; + start.tv_nsec = mts.tv_nsec; + + #else + clock_gettime(CLOCK_REALTIME, &start); + #endif + + size_t bytes_wrote = serial.write("Testing.\n"); + + #ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time + host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock); + clock_get_time(cclock, &mts); + mach_port_deallocate(mach_task_self(), cclock); + end.tv_sec = mts.tv_sec; + end.tv_nsec = mts.tv_nsec; + + #else + clock_gettime(CLOCK_REALTIME, &end); + #endif + + end.tv_sec -= start.tv_sec; + end.tv_nsec -= start.tv_nsec; + printf("write: %05lu.%09lu\n", end.tv_sec, end.tv_nsec); + + #ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time + host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock); + clock_get_time(cclock, &mts); + mach_port_deallocate(mach_task_self(), cclock); + start.tv_sec = mts.tv_sec; + start.tv_nsec = mts.tv_nsec; + + #else + clock_gettime(CLOCK_REALTIME, &start); + #endif + + std::string result = serial.readline(); + #ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time + host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock); + clock_get_time(cclock, &mts); + mach_port_deallocate(mach_task_self(), cclock); + end.tv_sec = mts.tv_sec; + end.tv_nsec = mts.tv_nsec; + + #else + clock_gettime(CLOCK_REALTIME, &end); + #endif + + + end.tv_sec -= start.tv_sec; + end.tv_nsec -= start.tv_nsec; + printf("read: %05lu.%09lu\n", end.tv_sec, end.tv_nsec); + + if (result == string("Testing.\n")) { + } + else { std::cout << ">" << count << ">" << bytes_wrote << ">"; std::cout << result.length() << "<" << result << std::endl; + cout << "No" << endl; + } - count += 1; - boost::this_thread::sleep(boost::posix_time::milliseconds(100)); + count += 1; + boost::this_thread::sleep(boost::posix_time::milliseconds(100)); } return 0; diff --git a/serial.cmake b/serial.cmake index 8fa0f68..328b91e 100644 --- a/serial.cmake +++ b/serial.cmake @@ -15,7 +15,7 @@ IF(EXISTS /usr/bin/clang) set(CMAKE_CXX_COMPILER /usr/bin/clang++) set(CMAKE_OSX_DEPLOYMENT_TARGET "") # set(CMAKE_CXX_FLAGS "-ferror-limit=5 -std=c++0x -stdlib=libc++") - set(CMAKE_CXX_FLAGS "-ferror-limit=5 -O3 -Wall -Weffc++ -pedantic -pedantic-errors -Wextra -Wall -Waggregate-return -Wcast-align -Wcast-qual -Wchar-subscripts -Wcomment -Wconversion -Wdisabled-optimization -Wfloat-equal -Wformat -Wformat=2 -Wformat-nonliteral -Wformat-security -Wformat-y2k -Wimplicit -Wimport -Winit-self -Winline -Winvalid-pch -Wlong-long -Wmissing-braces -Wmissing-field-initializers -Wmissing-format-attribute -Wmissing-include-dirs -Wmissing-noreturn -Wpacked -Wparentheses -Wpointer-arith -Wredundant-decls -Wreturn-type -Wsequence-point -Wshadow -Wsign-compare -Wstack-protector -Wstrict-aliasing -Wstrict-aliasing=2 -Wswitch -Wswitch-default -Wswitch-enum -Wtrigraphs -Wuninitialized -Wunknown-pragmas -Wunreachable-code -Wunused -Wunused-function -Wunused-label -Wunused-parameter -Wunused-value -Wunused-variable -Wvariadic-macros -Wvolatile-register-var -Wwrite-strings") + set(CMAKE_CXX_FLAGS "-ferror-limit=5 -Wall -Weffc++ -pedantic -pedantic-errors -Wextra -Wall -Waggregate-return -Wcast-align -Wcast-qual -Wchar-subscripts -Wcomment -Wconversion -Wdisabled-optimization -Wfloat-equal -Wformat -Wformat=2 -Wformat-nonliteral -Wformat-security -Wformat-y2k -Wimplicit -Wimport -Winit-self -Winline -Winvalid-pch -Wlong-long -Wmissing-braces -Wmissing-field-initializers -Wmissing-format-attribute -Wmissing-include-dirs -Wmissing-noreturn -Wpacked -Wparentheses -Wpointer-arith -Wredundant-decls -Wreturn-type -Wsequence-point -Wshadow -Wsign-compare -Wstack-protector -Wstrict-aliasing -Wstrict-aliasing=2 -Wswitch -Wswitch-default -Wswitch-enum -Wtrigraphs -Wuninitialized -Wunknown-pragmas -Wunreachable-code -Wunused -Wunused-function -Wunused-label -Wunused-parameter -Wunused-value -Wunused-variable -Wvariadic-macros -Wvolatile-register-var -Wwrite-strings") set(CMAKE_BUILD_TYPE Debug) ENDIF(EXISTS /usr/bin/clang) diff --git a/src/impl/unix.cc b/src/impl/unix.cc index dae30de..c621d04 100644 --- a/src/impl/unix.cc +++ b/src/impl/unix.cc @@ -368,8 +368,11 @@ Serial::SerialImpl::read (char* buf, size_t size) } fd_set readfds; ssize_t bytes_read = 0; + int count = 0; while (true) { + count++; + // printf("Counting: %u\n", count); if (timeout_ != -1) { FD_ZERO (&readfds); diff --git a/src/serial.cc b/src/serial.cc index 92623f5..112436c 100644 --- a/src/serial.cc +++ b/src/serial.cc @@ -89,8 +89,10 @@ Serial::read (size_t size) string result (read_cache_.substr (0, size)); read_cache_.clear (); + int count = 0; while (true) { + // printf("%u\n", count++); char buf[256]; size_t chars_read = pimpl_->read (buf, 256); if (chars_read > 0) From 05fa4b8d771960f3251d766713792b82718750c9 Mon Sep 17 00:00:00 2001 From: William Woodall Date: Thu, 2 Feb 2012 23:35:40 -0600 Subject: [PATCH 56/72] Removing serial listener, next make sure I can compile without boost completely. --- Findserial.cmake | 2 +- Makefile | 6 - examples/serial_listener_example.cc | 93 --- include/serial/serial_listener.h | 931 ---------------------------- serial.cmake | 237 ++++--- serial.makefile | 2 +- serial_ros.cmake | 68 +- src/serial_listener.cc | 217 ------- tests/serial_listener_tests.cc | 238 ------- 9 files changed, 137 insertions(+), 1657 deletions(-) delete mode 100644 examples/serial_listener_example.cc delete mode 100644 include/serial/serial_listener.h delete mode 100644 src/serial_listener.cc delete mode 100644 tests/serial_listener_tests.cc diff --git a/Findserial.cmake b/Findserial.cmake index cb839a6..6d27e7e 100644 --- a/Findserial.cmake +++ b/Findserial.cmake @@ -1,4 +1,4 @@ -find_path(serial_INCLUDE_DIRS serial.h serial_listener.h /usr/include/serial +find_path(serial_INCLUDE_DIRS serial.h /usr/include/serial /usr/local/include/serial "$ENV{NAMER_ROOT}") find_library(serial_LIBRARIES serial /usr/lib /usr/local/lib diff --git a/Makefile b/Makefile index c4c5448..5df6eab 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,3 @@ -# # ash_gti's dumb downed makefile so I can more easily test stuff -# CXX=clang++ -# CXXFLAGS=-g -I./include -ferror-limit=5 -O3 -Wall -Weffc++ -pedantic -pedantic-errors -Wextra -Wall -Waggregate-return -Wcast-align -Wcast-qual -Wchar-subscripts -Wcomment -Wconversion -Wdisabled-optimization -Wfloat-equal -Wformat -Wformat=2 -Wformat-nonliteral -Wformat-security -Wformat-y2k -Wimplicit -Wimport -Winit-self -Winline -Winvalid-pch -Wlong-long -Wmissing-braces -Wmissing-field-initializers -Wmissing-format-attribute -Wmissing-include-dirs -Wmissing-noreturn -Wpacked -Wparentheses -Wpointer-arith -Wredundant-decls -Wreturn-type -Wsequence-point -Wshadow -Wsign-compare -Wstack-protector -Wstrict-aliasing -Wstrict-aliasing=2 -Wswitch -Wswitch-default -Wswitch-enum -Wtrigraphs -Wuninitialized -Wunknown-pragmas -Wunreachable-code -Wunused -Wunused-function -Wunused-label -Wunused-parameter -Wunused-value -Wunused-variable -Wvariadic-macros -Wvolatile-register-var -Wwrite-strings -# -# test: tests/serial_tests.o src/serial.o src/impl/unix.o -# $(CXX) -o test tests/serial_tests.o src/serial.o src/impl/unix.o ifdef ROS_ROOT include $(shell rospack find mk)/cmake.mk else diff --git a/examples/serial_listener_example.cc b/examples/serial_listener_example.cc deleted file mode 100644 index 0a39854..0000000 --- a/examples/serial_listener_example.cc +++ /dev/null @@ -1,93 +0,0 @@ -#include - -#include -#include - -using namespace serial; - -void default_handler(std::string token) { - std::cout << "default_handler got a: " << token << std::endl; -} - -void callback(std::string token) { - std::cout << "callback got a: " << token << std::endl; -} - -int run() { - // Assuming this device prints the string 'pre-substr-post\r' at 100Hz - Serial serial("/dev/tty.usbserial-A900cfJA", 115200); - - SerialListener listener; - listener.startListening(serial); - - // Set the tokenizer - // This is the same as the default delimeter, so an explicit call to - // setTokenizer is not necessary if your data is \r delimited. - // You can create your own Tokenizer as well. - listener.setTokenizer(SerialListener::delimeter_tokenizer("\r")); - - // Method #1: - // comparator, callback - async - FilterPtr f1 = - listener.createFilter(SerialListener::startsWith("pre"), callback); - SerialListener::sleep(15); // Sleep 15ms, to let the data come in - listener.removeFilter(f1); // Not scoped, must be removed explicity - - // Method #2: - // comparator - blocking - { - BlockingFilterPtr f2 = - listener.createBlockingFilter(SerialListener::endsWith("post")); - for (size_t i = 0; i < 3; i++) { - std::string token = f2->wait(100); // Wait for 100 ms or a matched token - if (token != "") - std::cout << "Found something ending with 'post'" << std::endl; - else - std::cout << "Did not find something ending with 'post'" << std::endl; - } - } - // BlockingFilter is scoped and will remove itself, so no removeFilter - // required, but a call like `listener.removeFilter(BlockingFilter) will - // remove it from the filter list so wait will always timeout. - - // Method #3: - // comparator, token buffer size - blocking - { - // Give it a comparator, then a buffer size of 10 - BufferedFilterPtr f3 = - listener.createBufferedFilter(SerialListener::contains("substr"), 10); - SerialListener::sleep(75); // Sleep 75ms, should have about 7 - std::cout << "Caught " << f3->count(); - std::cout << " tokens containing 'substr'" << std::endl; - for(size_t i = 0; i < 20; ++i) { - std::string token = f3->wait(5); // Pull message from the buffer - if (token == "") // If an empty string is returned, a timeout occured - break; - } - f3->clear(); // Empties the buffer - if (f3->wait(0) == "") // Non-blocking wait - std::cout << "We won the race condition!" << std::endl; - else - std::cout << "We lost the race condition..." << std::endl; - // The buffer is circular, so the oldest matches will be dropped first - } - // BufferedFilter is scoped and will remove itself just like BlockingFilter. - - // Method #4: - // callback - async - // Gets called if a token doesn't match a filter - listener.setDefaultHandler(default_handler); - SerialListener::sleep(25); // Sleep 25 ms, so some default callbacks occur - - return 0; - -} - -int main(void) { - try { - return run(); - } catch (std::exception &e) { - std::cerr << e.what() << std::endl; - return 1; - } -} diff --git a/include/serial/serial_listener.h b/include/serial/serial_listener.h deleted file mode 100644 index 7e85b4c..0000000 --- a/include/serial/serial_listener.h +++ /dev/null @@ -1,931 +0,0 @@ -/*! - * \file serial/serial_listener.h - * \author William Woodall - * \version 0.1 - * - * \section LICENSE - * - * The BSD License - * - * Copyright (c) 2011 William Woodall - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - * - * \section DESCRIPTION - * - * This provides a class that allows for asynchronous serial port reading. - * - */ - -#ifndef SERIAL_LISTENER_H -#define SERIAL_LISTENER_H - -#ifndef SERIAL_LISTENER_DEBUG -#define SERIAL_LISTENER_DEBUG 0 -#endif - -// STL -#include -#include -#include - -// Serial -#include - -// Boost -#include -#include -#include -#include - -#if SERIAL_LISTENER_DEBUG -# warning SerialListener in debug mode -#endif - -namespace serial { - -/*! - * This is an alias to boost::shared_ptr used for tokens. - * - * This is the type used internally and is the type returned in a vector by - * the tokenizer. The shared_ptr allows for the token to be stored and kept - * around long enough to be used by the comparators and callbacks, but no - * longer. This internal storage is passed as a const std::string reference - * to callbacks, like the DataCallback function type, to prevent implicit - * copying. - * - * \see serial::TokenizerType, serial::SerialListener::setTokenizer - */ -typedef boost::shared_ptr TokenPtr; - -/*! - * This is a general function type that is used as the callback prototype - * for asynchronous functions like the default handler callback and the - * listenFor callbacks. - * - * The function takes a std::string reference and returns nothing, it is - * simply passing the resulting line detected by the comparator to the user's - * callback for processing. - * - * \see SerialListener::listenFor, SerialListener::setDefaultHandler - */ -typedef boost::function DataCallback; - -/*! - * This is a general function type that is used as the comparator callback - * prototpe for the listenFor* type functions. - * - * The function takes a std::string reference and returns true if the string - * matches what the comparator is looking for and false if it does not, unless - * otherwise specified. - * - * \see SerialListener::listenFor, SerialListener::listenForOnce - */ -typedef boost::function ComparatorType; - -/*! - * This function type describes the prototype for the tokenizer callback. - * - * The function should take a std::string reference and tokenize it into a - * several TokenPtr's and store them in the given std::vector - * reference. There are some default ones or the user can create their own. - * - * The last element in the std::vector of TokenPtr's should always be - * either an empty string ("") or the last partial message. The last element - * in the std::vector will be put back into the data buffer so that if it is - * incomplete it can be completed when more data is read. - * - * Example: A delimeter tokenizer with a delimeter of "\r". The result would - * be: "msg1\rmsg2\r" -> ["msg1", "msg2", ""] for two complete messages, or: - * "msg1\rpartial_msg2" -> ["msg1","partial_msg2"] for one complete message - * and one partial message. - * - * \see SerialListener::setTokenizer, serial::delimeter_tokenizer, - * serial::TokenPtr - */ -typedef boost::function&)> -TokenizerType; - -/*! - * This function type describes the prototype for the exception callback. - * - * The function takes a std::exception reference and returns nothing. It is - * called from the library when an exception occurs in a library thread. - * This exposes these exceptions to the user so they can to error handling. - * - * \see SerialListener::setExceptionHandler - */ -typedef boost::function ExceptionCallback; - -/*! - * Represents a filter which new data is passed through. - * - * The filter consists of a comparator and a callback. The comparator takes a - * token and returns true if it matches, false if it doesn't. If a match - * occurs the serial listener will dispatch a call of the callback with the - * matched data in a another thread. The comparator should be as short as - * possible, but the callback can be longer since it is executed in a thread - * or thread pool. - * - * \param comparator A ComparatorType that matches incoming data, returns true - * for a match, false othewise. - * - * \param callback A DataCallback that gets called when a match occurs. - * - * \see serial::ComparatorType, serial::DataCallback, serial::FilterPtr - */ -class Filter -{ -public: - Filter (ComparatorType comparator, DataCallback callback) - : comparator_(comparator), callback_(callback) {} - virtual ~Filter () {} - - ComparatorType comparator_; - DataCallback callback_; - -private: - // Disable copy constructors - Filter(const Filter&); - void operator=(const Filter&); - const Filter& operator=(Filter); -}; - -/*! - * This is an alias to boost::shared_ptr used for tokens. - * - * This is used internally and is returned from SerialListener::listenFor like - * functions so that users can later remove those filters by passing the - * FilterPtr. - * - * \see serial::Filter, serial::SerialListener::listenFor, - * serial::SerialListener::listenForOnce - */ -typedef boost::shared_ptr FilterPtr; - -class BlockingFilter; // Forward declaration - -/*! - * Shared Pointer of BlockingFilter, returned by - * SerialListener::createBlockingFilter. - * - * \see serial::BlockingFilter, SerialListener::createBlockingFilter - */ -typedef boost::shared_ptr BlockingFilterPtr; - -class BufferedFilter; // Forward declaration - -/*! - * Shared Pointer of BufferedFilter, returned by - * SerialListener::createBufferedFilter. - * - * \see serial::BufferedFilter, SerialListener::createBufferedFilter - */ -typedef boost::shared_ptr BufferedFilterPtr; - -/*! - * This is a general exception generated by the SerialListener class. - * - * Check the SerialListenerException::what function for the cause. - * - * \param e_what is a std::string that describes the cause of the error. - */ -class SerialListenerException : public std::exception { - const std::string e_what_; -public: - SerialListenerException(const std::string e_what) : e_what_(e_what) {} - ~SerialListenerException() throw() {} - - virtual const char* what() const throw() { - std::stringstream ss; - ss << "SerialListenerException: " << this->e_what_; - return ss.str().c_str(); - } -}; - -// Based on: http://www.justsoftwaresolutions.co.uk/threading/implementing-a-thread-safe-queue-using-condition-variables.html -template -class ConcurrentQueue -{ -private: - std::queue the_queue; - mutable boost::mutex the_mutex; - boost::condition_variable the_condition_variable; -public: - void push(Data const& data) { - boost::mutex::scoped_lock lock(the_mutex); - the_queue.push(data); - lock.unlock(); - the_condition_variable.notify_one(); - } - - bool empty() const { - boost::mutex::scoped_lock lock(the_mutex); - return the_queue.empty(); - } - - bool try_pop(Data& popped_value) { - boost::mutex::scoped_lock lock(the_mutex); - if(the_queue.empty()) { - return false; - } - - popped_value=the_queue.front(); - the_queue.pop(); - return true; - } - - bool timed_wait_and_pop(Data& popped_value, long timeout) { - using namespace boost::posix_time; - bool result; - boost::mutex::scoped_lock lock(the_mutex); - result = !the_queue.empty(); - if (!result) { - result = the_condition_variable.timed_wait(lock, milliseconds(timeout)); - } - - if (result) { - popped_value=the_queue.front(); - the_queue.pop(); - } - return result; - } - - void wait_and_pop(Data& popped_value) { - boost::mutex::scoped_lock lock(the_mutex); - while(the_queue.empty()) { - the_condition_variable.wait(lock); - } - - popped_value=the_queue.front(); - the_queue.pop(); - } - - size_t size() const { - return the_queue.size(); - } - - void cancel() { - the_condition_variable.notify_one(); - } - - void clear() { - boost::mutex::scoped_lock lock(the_mutex); - while (!the_queue.empty()) { - the_queue.pop(); - } - } -}; - -/*! - * Listens to a serial port, facilitates asynchronous reading - */ -class SerialListener -{ -public: - /*! - * Creates a new Serial Listener. - */ - SerialListener (); - - /*! - * Destructor. - */ - virtual ~SerialListener (); - -/***** Configurations ******/ - - /*! - * Sets the tokenizer to be used when tokenizing the data into tokens. - * - * This function is given a std::string of data and is responsible for - * tokenizing that data into a std::vector of data tokens. - * The default tokenizer splits the data by the ascii return carriage. - * The user can create their own tokenizer or use one of the default ones. - * - * \param tokenizer Function for tokenizing the incoming data. - * - * \see serial::TokenizerType, serial::delimeter_tokenizer - */ - void - setTokenizer (TokenizerType tokenizer) { - this->tokenize = tokenizer; - } - - /*! - * Sets the number of bytes to be read at a time by the listener. - * - * \param chunk_size Number of bytes to be read at a time. - */ - void - setChunkSize (size_t chunk_size) { - this->chunk_size_ = chunk_size; - } - -/***** Start and Stop Listening ******/ - - /*! - * Starts a thread to listen for messages and process them through filters. - * - * \param serial_port Pointer to a serial::Serial object that is used to - * retrieve new data. - */ - void - startListening (serial::Serial &serial_port); - - /*! - * Stops the listening thread and blocks until it completely stops. - * - * This function also clears all of the active filters from listenFor and - * similar functions. - */ - void - stopListening (); - -/***** Filter Functions ******/ - - /*! - * Creates a filter that calls a callback when the comparator returns true. - * - * The user provides a comparator and a callback, and every time a line is - * received the comparator is called and the comparator has to evaluate the - * line and return true if it matches and false if it doesn't. If it does - * match, the callback is called with the resulting line. - * - * \param comparator This is a comparator for detecting if a line matches. - * The comparartor receives a std::string reference and must return a true - * if it matches and false if it doesn't. - * - * \param callback This is the handler for when a match occurs. It is given - * a std::string reference of the line that matched your comparator. - * - * \return boost::shared_ptr so you can remove it later. - * - * \see SerialListener::removeFilter - */ - FilterPtr - createFilter (ComparatorType comparator, DataCallback callback); - - /*! - * Creates a BlockingFilter which blocks until the comparator returns true. - * - * The user provides a comparator, and every time a line is - * received the comparator is called and the comparator has to evaluate the - * line and return true if it matches and false if it doesn't. If it does - * match, any threads that have called BlockingFilter::wait will be - * notified. The BlockingFilter will remove itself when its destructor is - * called, i.e. when it leaves the scope, so in those cases an explicit call - * to SerialListener::removeFilter is not needed. - * - * \param comparator This is a comparator for detecting if a line matches. - * The comparartor receives a std::string reference and must return a true - * if it matches and false if it doesn't. - * - * \return BlockingFilterPtr So you can call BlockingFilter::wait on it. - * - * \see SerialListener::removeFilter, serial::BlockingFilter, - * serial::BlockingFilterPtr - */ - BlockingFilterPtr - createBlockingFilter (ComparatorType comparator); - - /*! - * Creates a BlockingFilter blocks until the comparator returns true. - * - * The user provides a comparator, and every time a line is - * received the comparator is called and the comparator has to evaluate the - * line and return true if it matches and false if it doesn't. If it does - * match, any threads that have called BlockingFilter::wait will be - * notified. The BlockingFilter will remove itself when its destructor is - * called, i.e. when it leaves the scope, so in those cases an explicit call - * to SerialListener::removeFilter is not needed. - * - * \param comparator This is a comparator for detecting if a line matches. - * The comparartor receives a std::string reference and must return a true - * if it matches and false if it doesn't. - * - * \param buffer_size This is the number of tokens to be buffered by the - * BufferedFilter, defaults to 1024. - * - * \return BlockingFilter So you can call BlockingFilter::wait on it. - * - * \see SerialListener::removeFilter, serial::BufferedFilter, - * serial::BufferedFilterPtr - */ - BufferedFilterPtr - createBufferedFilter (ComparatorType comparator, size_t buffer_size = 1024); - - /*! - * Removes a filter by a given FilterPtr. - * - * \param filter_ptr A shared pointer to the filter to be removed. - * - * \see SerialListener::createFilter - */ - void - removeFilter (FilterPtr filter_ptr); - - /*! - * Removes a BlockingFilter. - * - * The BlockingFilter will remove itself if the destructor is called. - * - * \param blocking_filter A BlockingFilter to be removed. - * - * \see SerialListener::createBlockingFilter - */ - void - removeFilter (BlockingFilterPtr blocking_filter); - - /*! - * Removes a BufferedFilter. - * - * The BufferedFilter will remove itself if the destructor is called. - * - * \param buffered_filter A BufferedFilter to be removed. - * - * \see SerialListener::createBufferedFilter - */ - void - removeFilter (BufferedFilterPtr buffered_filter); - - /*! - * Removes all filters. - */ - void - removeAllFilters (); - -/***** Hooks and Handlers ******/ - - /*! - * Sets the handler to be called when a lines is not caught by a filter. - * - * This allows you to set a catch all function that will get called - * everytime a line is not matched by a filter and the ttl expires. - * - * Setting the callbacks works just like SerialListener::setInfoHandler. - * - * \param default_handler A function pointer to the callback to handle - * unmatched and expired messages. - * - * \see serial::DataCallback, SerialListener::setInfoHandler - */ - void setDefaultHandler(DataCallback default_handler) { - this->_default_handler = default_handler; - } - - /*! - * Sets the function to be called when an exception occurs internally. - * - * This allows you to hook into the exceptions that occur in threads inside - * the serial listener library. - * - * \param exception_handler A function pointer to the callback to handle new - * interal exceptions. - * - * \see serial::ExceptionCallback - */ - void - setExceptionHandler (ExceptionCallback exception_handler) { - this->handle_exc = exception_handler; - } - -/***** Static Functions ******/ - - /*! - * Sleeps for a given number of milliseconds. - * - * \param milliseconds number of milliseconds to sleep. - */ - static void - sleep (long milliseconds) { - boost::int64_t ms(milliseconds); - boost::this_thread::sleep(boost::posix_time::milliseconds(ms)); - } - - /*! - * This returns a tokenizer that splits on a given delimeter. - * - * The delimeter is passed into the function and a TokenizerType is returned - * that can be passed to SerialListener::setTokenizer. - * - * Example: - *
-   *   my_listener.setTokenizer(SerialListener::delimeter_tokenizer("\r"));
-   * <\pre>
-   *
-   * \param delimeter A std::string that is used as a delimeter when
-   * tokenizing data.
-   *
-   * \return TokenizerType A tokenizer function type that can be passed to
-   * SerialListener::setTokenizer.
-   *
-   * \see SerialListener::setTokenizer, serial::TokenizerType
-   */
-  static TokenizerType
-  delimeter_tokenizer (std::string delimeter) {
-    return boost::bind(&SerialListener::_delimeter_tokenizer,
-                       _1, _2, delimeter);
-  }
-
-  /*!
-   * This returns a comparator that matches only the exact string given.
-   *
-   * This can be used with listenFor or listenForOnce:
-   *
-   * Example:
-   * 
-   *   my_listener.listenFor(SerialListener::exactly("my_string"),
-   *                         my_callback);
-   * <\pre>
-   *
-   * \param exact_str A std::string that is used as the exact string to match
-   * when comparing tokens for matching.
-   *
-   * \return ComparatorType A comparator function type that can be passed to
-   * SerialListener::listenFor or SerialListener::listenForOnce.
-   *
-   * \see SerialListener::listenFor, SerialListener::listenForOnce,
-   * serial::ComparatorType
-   */
-  static ComparatorType
-  exactly (std::string exact_str) {
-    return boost::bind(&SerialListener::_exactly, _1, exact_str);
-  }
-
-  /*!
-   * This returns a comparator that looks for a given prefix.
-   *
-   * This can be used with listenFor or listenForOnce:
-   *
-   * Example:
-   * 
-   *   my_listener.listenFor(SerialListener::startsWith("V="), my_callback);
-   * <\pre>
-   *
-   * \param prefix A std::string that is used as the prefix string to match
-   * when comparing tokens for matching.
-   *
-   * \return ComparatorType A comparator function type that can be passed to
-   * SerialListener::listenFor or SerialListener::listenForOnce.
-   *
-   * \see SerialListener::listenFor, SerialListener::listenForOnce,
-   * serial::ComparatorType
-   */
-  static ComparatorType
-  startsWith (std::string prefix) {
-    return boost::bind(&SerialListener::_startsWith, _1, prefix);
-  }
-
-  /*!
-   * This returns a comparator that looks for a given postfix.
-   *
-   * This can be used with listenFor or listenForOnce:
-   *
-   * Example:
-   * 
-   *   my_listener.listenFor(SerialListener::endsWith(";"), my_callback);
-   * <\pre>
-   *
-   * \param postfix A std::string that is used as the postfix string to match
-   * when comparing tokens for matching.
-   *
-   * \return ComparatorType A comparator function type that can be passed to
-   * SerialListener::listenFor or SerialListener::listenForOnce.
-   *
-   * \see SerialListener::listenFor, SerialListener::listenForOnce,
-   * serial::ComparatorType
-   */
-  static ComparatorType
-  endsWith (std::string postfix) {
-    return boost::bind(&SerialListener::_endsWith, _1, postfix);
-  }
-
-  /*!
-   * This returns a comparator that looks for a given substring in the token.
-   *
-   * This can be used with listenFor or listenForOnce:
-   *
-   * Example:
-   * 
-   *   my_listener.listenFor(SerialListener::contains("some string"),
-   *                         my_callback);
-   * <\pre>
-   *
-   * \param substr A std::string that is used as the search substring to match
-   * when comparing tokens for matching.
-   *
-   * \return ComparatorType A comparator function type that can be passed to
-   * SerialListener::listenFor or SerialListener::listenForOnce.
-   *
-   * \see SerialListener::listenFor, SerialListener::listenForOnce,
-   * serial::ComparatorType
-   */
-  static ComparatorType
-  contains (std::string substr) {
-    return boost::bind(_contains, _1, substr);
-  }
-
-private:
-  // Disable copy constructors
-  SerialListener(const SerialListener&);
-  void operator=(const SerialListener&);
-  const SerialListener& operator=(SerialListener);
-  // delimeter tokenizer function
-  static void
-  _delimeter_tokenizer (const std::string &data,
-                        std::vector &tokens,
-                        std::string delimeter)
-  {
-    typedef std::vector find_vector_type;
-    find_vector_type t;
-    boost::split(t, data, boost::is_any_of(delimeter));
-    for (find_vector_type::iterator it = t.begin(); it != t.end(); it++)
-      tokens.push_back(TokenPtr( new std::string(*it) ));
-  }
-  // exact comparator function
-  static bool
-  _exactly (const std::string& token, std::string exact_str) {
-#if SERIAL_LISTENER_DEBUG
-    std::cerr << "In exactly callback(" << token.length() << "): ";
-    std::cerr << token << " == " << exact_str << ": ";
-    if (token == exact_str)
-      std::cerr << "True";
-    else
-      std::cerr << "False";
-    std::cerr << std::endl;
-#endif
-    return token == exact_str;
-  }
-  // startswith comparator function
-  static bool
-  _startsWith (const std::string& token, std::string prefix) {
-#if SERIAL_LISTENER_DEBUG
-    std::cerr << "In startsWith callback(" << token.length() << "): ";
-    std::cerr << token << " starts with " << prefix;
-    std::cerr << "?: ";
-    if (token.substr(0,prefix.length()) == prefix)
-      std::cerr << "True";
-    else
-      std::cerr << "False";
-    std::cerr << std::endl;
-#endif
-    return token.substr(0,prefix.length()) == prefix;
-  }
-  // endswith comparator function
-  static bool
-  _endsWith (const std::string& token, std::string postfix) {
-#if SERIAL_LISTENER_DEBUG
-    std::cerr << "In endsWith callback(";
-    std::cerr << token.length();
-    std::cerr << "): " << token;
-    std::cerr << " ends with " << postfix << "?: ";
-    if (token.substr(token.length()-postfix.length()) == postfix)
-      std::cerr << "True";
-    else
-      std::cerr << "False";
-    std::cerr << std::endl;
-#endif
-    return token.substr(token.length()-postfix.length()) == postfix;
-  }
-  // contains comparator function
-  static bool
-  _contains (const std::string& token, std::string substr) {
-    return token.find(substr) != std::string::npos;
-  }
-
-  // Gets some data from the serial port
-  void readSomeData (std::string &temp, size_t this_many) {
-    // Make sure there is a serial port
-    if (this->serial_port_ == NULL) {
-      this->handle_exc(SerialListenerException("Invalid serial port."));
-    }
-    // Make sure the serial port is open
-    if (!this->serial_port_->isOpen()) {
-      this->handle_exc(SerialListenerException("Serial port not open."));
-    }
-    temp = this->serial_port_->read(this_many);
-  }
-  // Runs the new_tokens through all the filters
-  void filter (std::vector &tokens);
-  // Function that loops while listening is true
-  void listen ();
-  // Target of callback thread
-  void callback ();
-  // Determines how much to read on each loop of listen
-  size_t determineAmountToRead ();
-
-  // Tokenizer
-  TokenizerType tokenize;
-
-  // Exception handler
-  ExceptionCallback handle_exc;
-
-  // Default handler
-  FilterPtr default_filter;
-  DataCallback _default_handler;
-  ComparatorType default_comparator;
-  void default_handler(const std::string &token);
-
-  // Persistent listening variables
-  bool listening;
-  char serial_port_padding[7];
-  serial::Serial * serial_port_;
-  boost::thread listen_thread;
-  std::string data_buffer;
-  size_t chunk_size_;
-
-  // Callback related variables
-  // filter id, token
-  // filter id == 0 is going to be default handled
-  ConcurrentQueue >
-  callback_queue;
-  boost::thread callback_thread;
-
-  // Mutex for locking use of filters
-  boost::mutex filter_mux;
-  // vector of filter ids
-  std::vector filters;
-
-};
-
-/*!
- * This is the a filter that provides a wait function for blocking until a
- * match is found.
- *
- * This should probably not be created manually, but instead should be
- * constructed using SerialListener::createBlockingFilter(ComparatorType)
- * function which returns a BlockingFilter instance.
- *
- * \see serial::SerialListener::ComparatorType,
- * serial::SerialListener::createBlockingFilter
- */
-class BlockingFilter
-{
-public:
-  BlockingFilter (ComparatorType comparator, SerialListener &listener) {
-    this->listener_ = &listener;
-    DataCallback cb = boost::bind(&BlockingFilter::callback, this, _1);
-    this->filter_ptr = this->listener_->createFilter(comparator, cb);
-  }
-
-  virtual ~BlockingFilter () {
-    this->listener_->removeFilter(filter_ptr);
-    this->result = "";
-    this->cond.notify_all();
-  }
-
-  /*!
-   * Waits a given number of milliseconds or until a token is matched.  If a
-   * token is matched it is returned, otherwise an empty string is returned.
-   *
-   * \param ms Time in milliseconds to wait on a new token.
-   *
-   * \return std::string token that was matched or "" if none were matched.
-   */
- std::string wait(long ms) {
-    this->result = "";
-    boost::unique_lock lock(this->mutex);
-    this->cond.timed_wait(lock, boost::posix_time::milliseconds(ms));
-    return this->result;
-  }
-
-  FilterPtr filter_ptr;
-
-  void callback(const std::string& token) {
-#if SERIAL_LISTENER_DEBUG
-    std::cerr << "In BlockingFilter callback(" << token.length() << "): ";
-    std::cerr << token << std::endl;
-#endif
-    this->cond.notify_all();
-    this->result = token;
-  }
-
-private:
-  SerialListener * listener_;
-  boost::condition_variable cond;
-  boost::mutex mutex;
-  std::string result;
-
-};
-
-/*!
- * This is the a filter that provides a wait function for blocking until a
- * match is found.  It will also buffer up to a given buffer size of tokens so
- * that they can be counted or accessed after they are matched by the filter.
- *
- * This should probably not be created manually, but instead should be
- * constructed using SerialListener::createBufferedFilter(ComparatorType)
- * function which returns a BufferedFilter instance.
- *
- * The internal buffer is a circular queue buffer, so when the buffer is full,
- * the oldest token is dropped and the new one is added.  Additionally, when
- * wait is a called the oldest available token is returned.
- *
- * \see serial::SerialListener::ComparatorType,
- * serial::SerialListener::createBufferedFilter
- */
-class BufferedFilter
-{
-public:
-  BufferedFilter (ComparatorType comparator, size_t buffer_size,
-                  SerialListener &listener)
-  : buffer_size_(buffer_size)
-  {
-    this->listener_ = &listener;
-    DataCallback cb = boost::bind(&BufferedFilter::callback, this, _1);
-    this->filter_ptr = this->listener_->createFilter(comparator, cb);
-  }
-
-  virtual ~BufferedFilter () {
-    this->listener_->removeFilter(filter_ptr);
-    this->queue.clear();
-    this->result = "";
-  }
-
-  /*!
-   * Waits a given number of milliseconds or until a matched token is
-   * available in the buffer.  If a token is matched it is returned, otherwise
-   * an empty string is returned.
-   *
-   * \param ms Time in milliseconds to wait on a new token.  If ms is set to 0
-   * then it will try to get a new token if one is available but will not
-   * block.
-   *
-   * \return std::string token that was matched or "" if none were matched.
-   */
-  std::string wait(long ms) {
-    if (ms == 0) {
-      if (!this->queue.try_pop(this->result)) {
-        this->result = "";
-      }
-    } else {
-      if (!this->queue.timed_wait_and_pop(this->result, ms)) {
-        this->result = "";
-      }
-    }
-    return result;
-  }
-
-  /*!
-   * Clears the buffer of any tokens.
-   */
-  void clear() {
-    queue.clear();
-  }
-
-  /*!
-   * Returns the number of tokens waiting in the buffer.
-   */
-  size_t count() {
-    return queue.size();
-  }
-
-  /*!
-   * Returns the capacity of the buffer.
-   */
-  size_t capacity() {
-    return buffer_size_;
-  }
-
-  FilterPtr filter_ptr;
-
-  void callback(const std::string &token) {
-#if SERIAL_LISTENER_DEBUG
-    std::cerr << "In BufferedFilter callback(" << token.length() << "): ";
-    std::cerr << token << std::endl;
-#endif
-    std::string throw_away;
-    if (this->queue.size() == this->buffer_size_) {
-      this->queue.wait_and_pop(throw_away);
-    }
-    this->queue.push(token);
-  }
-
-private:
-  size_t buffer_size_;
-  SerialListener * listener_;
-  ConcurrentQueue queue;
-  std::string result;
-
-};
-
-} // namespace serial
-
-#endif // SERIAL_LISTENER_H
diff --git a/serial.cmake b/serial.cmake
index 328b91e..7e339aa 100644
--- a/serial.cmake
+++ b/serial.cmake
@@ -1,158 +1,133 @@
 macro(build_serial)
-## Project Setup
-cmake_minimum_required(VERSION 2.4.6)
 
-if(COMMAND cmake_policy)
-    cmake_policy(SET CMP0003 NEW)
-endif(COMMAND cmake_policy)
+  ## Project Setup
+  cmake_minimum_required(VERSION 2.4.6)
 
-project(Serial)
+  if(COMMAND cmake_policy)
+      cmake_policy(SET CMP0003 NEW)
+  endif(COMMAND cmake_policy)
 
-## Configurations
+  project(Serial)
 
-# Use clang if available
-IF(EXISTS /usr/bin/clang)
-  set(CMAKE_CXX_COMPILER /usr/bin/clang++)
-  set(CMAKE_OSX_DEPLOYMENT_TARGET "")
-  # set(CMAKE_CXX_FLAGS "-ferror-limit=5 -std=c++0x -stdlib=libc++")
-  set(CMAKE_CXX_FLAGS "-ferror-limit=5 -Wall -Weffc++ -pedantic -pedantic-errors -Wextra  -Wall -Waggregate-return -Wcast-align -Wcast-qual  -Wchar-subscripts  -Wcomment -Wconversion -Wdisabled-optimization -Wfloat-equal  -Wformat  -Wformat=2 -Wformat-nonliteral -Wformat-security  -Wformat-y2k -Wimplicit  -Wimport  -Winit-self  -Winline -Winvalid-pch   -Wlong-long -Wmissing-braces -Wmissing-field-initializers -Wmissing-format-attribute   -Wmissing-include-dirs -Wmissing-noreturn -Wpacked -Wparentheses  -Wpointer-arith -Wredundant-decls -Wreturn-type -Wsequence-point  -Wshadow -Wsign-compare  -Wstack-protector -Wstrict-aliasing -Wstrict-aliasing=2 -Wswitch  -Wswitch-default -Wswitch-enum -Wtrigraphs  -Wuninitialized -Wunknown-pragmas  -Wunreachable-code -Wunused -Wunused-function  -Wunused-label  -Wunused-parameter -Wunused-value  -Wunused-variable  -Wvariadic-macros -Wvolatile-register-var  -Wwrite-strings")
-  set(CMAKE_BUILD_TYPE Debug)
-ENDIF(EXISTS /usr/bin/clang)
+  ## Configurations
 
-option(SERIAL_BUILD_TESTS "Build all of the Serial tests." OFF)
-option(SERIAL_BUILD_EXAMPLES "Build all of the Serial examples." OFF)
+  # Use clang if available
+  IF(EXISTS /usr/bin/clang)
+    set(CMAKE_CXX_COMPILER /usr/bin/clang++)
+    set(CMAKE_OSX_DEPLOYMENT_TARGET "")
+    set(SERIAL_BUILD_WARNINGS TRUE)
+    IF(SERIAL_BUILD_WARNINGS)
+      set(CMAKE_CXX_FLAGS "-ferror-limit=5 -Wall -Weffc++ -pedantic -pedantic-errors -Wextra  -Wall -Waggregate-return -Wcast-align -Wcast-qual  -Wchar-subscripts  -Wcomment -Wconversion -Wdisabled-optimization -Wfloat-equal  -Wformat  -Wformat=2 -Wformat-nonliteral -Wformat-security  -Wformat-y2k -Wimplicit  -Wimport  -Winit-self  -Winline -Winvalid-pch   -Wlong-long -Wmissing-braces -Wmissing-field-initializers -Wmissing-format-attribute   -Wmissing-include-dirs -Wmissing-noreturn -Wpacked -Wparentheses  -Wpointer-arith -Wredundant-decls -Wreturn-type -Wsequence-point  -Wshadow -Wsign-compare  -Wstack-protector -Wstrict-aliasing -Wstrict-aliasing=2 -Wswitch  -Wswitch-default -Wswitch-enum -Wtrigraphs  -Wuninitialized -Wunknown-pragmas  -Wunreachable-code -Wunused -Wunused-function  -Wunused-label  -Wunused-parameter -Wunused-value  -Wunused-variable  -Wvariadic-macros -Wvolatile-register-var  -Wwrite-strings")
+    ELSE(SERIAL_BUILD_WARNINGS)
+      set(CMAKE_CXX_FLAGS "-ferror-limit=5")
+    ENDIF(SERIAL_BUILD_WARNINGS)
+    set(CMAKE_BUILD_TYPE Debug)
+  ENDIF(EXISTS /usr/bin/clang)
 
-# Allow for building shared libs override
-IF(NOT BUILD_SHARED_LIBS)
-    set(BUILD_SHARED_LIBS OFF)
-ENDIF(NOT BUILD_SHARED_LIBS)
+  option(SERIAL_BUILD_TESTS "Build all of the Serial tests." OFF)
+  option(SERIAL_BUILD_EXAMPLES "Build all of the Serial examples." OFF)
 
-# Set the default path for built executables to the "bin" directory
-IF(NOT DEFINED(EXECUTABLE_OUTPUT_PATH))
-    set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
-ENDIF(NOT DEFINED(EXECUTABLE_OUTPUT_PATH))
-# set the default path for built libraries to the "lib" directory
-IF(NOT DEFINED(LIBRARY_OUTPUT_PATH))
-    set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
-ENDIF(NOT DEFINED(LIBRARY_OUTPUT_PATH))
+  # Allow for building shared libs override
+  IF(NOT BUILD_SHARED_LIBS)
+      set(BUILD_SHARED_LIBS OFF)
+  ENDIF(NOT BUILD_SHARED_LIBS)
 
-## Configure the build system
+  # Set the default path for built executables to the "bin" directory
+  IF(NOT DEFINED(EXECUTABLE_OUTPUT_PATH))
+      set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
+  ENDIF(NOT DEFINED(EXECUTABLE_OUTPUT_PATH))
+  # set the default path for built libraries to the "lib" directory
+  IF(NOT DEFINED(LIBRARY_OUTPUT_PATH))
+      set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
+  ENDIF(NOT DEFINED(LIBRARY_OUTPUT_PATH))
 
-# Add the include folder to the include path
-include_directories(${PROJECT_SOURCE_DIR}/include)
+  ## Configure the build system
 
-# Add default source files
-set(SERIAL_SRCS src/serial.cc src/impl/unix.cc src/serial_listener.cc)
-# Add default header files
-set(SERIAL_HEADERS include/serial/serial.h include/serial/serial_listener.h)
+  # Add the include folder to the include path
+  include_directories(${PROJECT_SOURCE_DIR}/include)
 
-IF(UNIX)
-  list(APPEND SERIAL_SRCS src/impl/unix.cc)
-  list(APPEND SERIAL_HEADERS include/serial/impl/unix.h)
-ELSE(UNIX)
-  
-ENDIF(UNIX)
+  # Add default source files
+  set(SERIAL_SRCS src/serial.cc)
+  IF(WIN32)
+    list(APPEND SERIAL_SRCS src/impl/windows.cc)
+  ELSE(WIN32)
+    list(APPEND SERIAL_SRCS src/impl/unix.cc)
+  ENDIF(WIN32)
+  # Add default header files
+  set(SERIAL_HEADERS include/serial/serial.h)
 
-# Find Boost, if it hasn't already been found
-IF(NOT Boost_FOUND OR NOT Boost_SYSTEM_FOUND OR NOT Boost_FILESYSTEM_FOUND OR NOT Boost_THREAD_FOUND)
-    find_package(Boost COMPONENTS system filesystem thread REQUIRED)
-ENDIF(NOT Boost_FOUND OR NOT Boost_SYSTEM_FOUND OR NOT Boost_FILESYSTEM_FOUND OR NOT Boost_THREAD_FOUND)
+  ## Build the Serial Library
 
-link_directories(${Boost_LIBRARY_DIRS})
-include_directories(${Boost_INCLUDE_DIRS})
+  # Compile the Library
+  add_library(serial ${SERIAL_SRCS})
 
-set(SERIAL_LINK_LIBS ${Boost_SYSTEM_LIBRARY}
-                     ${Boost_FILESYSTEM_LIBRARY}
-                     ${Boost_THREAD_LIBRARY})
+  ## Build Examples
 
-## Build the Serial Library
+  # If asked to
+  IF(SERIAL_BUILD_EXAMPLES)
+      # Compile the Serial Test program
+      add_executable(serial_example examples/serial_example.cc)
+      # Link the Test program to the Serial library
+      target_link_libraries(serial_example serial)
+  ENDIF(SERIAL_BUILD_EXAMPLES)
 
-# Compile the Library
-add_library(serial ${SERIAL_SRCS} ${SERIAL_HEADERS})
-target_link_libraries(serial ${SERIAL_LINK_LIBS})
-IF( WIN32 )
-  target_link_libraries(serial wsock32)
-ENDIF( )
+  ## Build tests
 
-## Build Examples
+  # If asked to
+  IF(SERIAL_BUILD_TESTS)
+      # Find Google Test
+      enable_testing()
+      find_package(GTest REQUIRED)
+      include_directories(${GTEST_INCLUDE_DIRS})
 
-# If asked to
-IF(SERIAL_BUILD_EXAMPLES)
-    # Compile the Serial Test program
-    add_executable(serial_example examples/serial_example.cc)
-    # Link the Test program to the Serial library
-    target_link_libraries(serial_example serial)
+      # Compile the Serial Test program
+      add_executable(serial_tests tests/serial_tests.cc)
+      # Link the Test program to the serial library
+      target_link_libraries(serial_tests ${GTEST_BOTH_LIBRARIES}
+                            serial)
+
+      add_test(AllTestsIntest_serial serial_tests)
+  ENDIF(SERIAL_BUILD_TESTS)
+
+  ## Setup install and uninstall
+
+  # Unless asked not to...
+  IF(NOT SERIAL_DONT_CONFIGURE_INSTALL)
+      # Configure make install
+      IF(NOT CMAKE_INSTALL_PREFIX)
+          SET(CMAKE_INSTALL_PREFIX /usr/local)
+      ENDIF(NOT CMAKE_INSTALL_PREFIX)
     
-    # Compile the Serial Listener Test program
-    add_executable(serial_listener_example 
-                   examples/serial_listener_example.cc)
-    # Link the Test program to the Serial library
-    target_link_libraries(serial_listener_example serial)
-ENDIF(SERIAL_BUILD_EXAMPLES)
-
-## Build tests
-
-# If asked to
-IF(SERIAL_BUILD_TESTS)
-    # Find Google Test
-    enable_testing()
-    find_package(GTest REQUIRED)
-    include_directories(${GTEST_INCLUDE_DIRS})
-
-    # Compile the Serial Listener Test program
-    add_executable(serial_listener_tests tests/serial_listener_tests.cc)
-    add_executable(serial_tests tests/serial_tests.cc)
-    # Link the Test program to the serial library
-    target_link_libraries(serial_listener_tests ${GTEST_BOTH_LIBRARIES}
-                          serial)
-    target_link_libraries(serial_tests ${GTEST_BOTH_LIBRARIES}
-                          serial)
-
-    # # See: http://code.google.com/p/googlemock/issues/detail?id=146
-    # add_definitions(-DGTEST_USE_OWN_TR1_TUPLE=1)
-    add_test(AllTestsIntest_serial serial_listener_tests)
-    add_test(AllTestsIntest_serial serial_tests)
-ENDIF(SERIAL_BUILD_TESTS)
-
-## Setup install and uninstall
-
-# Unless asked not to...
-IF(NOT SERIAL_DONT_CONFIGURE_INSTALL)
-    # Configure make install
-    IF(NOT CMAKE_INSTALL_PREFIX)
-        SET(CMAKE_INSTALL_PREFIX /usr/local)
-    ENDIF(NOT CMAKE_INSTALL_PREFIX)
+      INSTALL(TARGETS serial
+        RUNTIME DESTINATION bin
+        LIBRARY DESTINATION lib
+        ARCHIVE DESTINATION lib
+      )
     
-    INSTALL(TARGETS serial
-      RUNTIME DESTINATION bin
-      LIBRARY DESTINATION lib
-      ARCHIVE DESTINATION lib
-    )
+      INSTALL(FILES include/serial/serial.h
+              DESTINATION include/serial)
     
-    INSTALL(FILES include/serial/serial.h
-                  include/serial/serial_listener.h
-            DESTINATION include/serial)
+      IF(NOT CMAKE_FIND_INSTALL_PATH)
+          set(CMAKE_FIND_INSTALL_PATH ${CMAKE_ROOT})
+      ENDIF(NOT CMAKE_FIND_INSTALL_PATH)
     
-    IF(NOT CMAKE_FIND_INSTALL_PATH)
-        set(CMAKE_FIND_INSTALL_PATH ${CMAKE_ROOT})
-    ENDIF(NOT CMAKE_FIND_INSTALL_PATH)
+      INSTALL(FILES Findserial.cmake
+              DESTINATION ${CMAKE_FIND_INSTALL_PATH}/Modules/)
     
-    INSTALL(FILES Findserial.cmake DESTINATION ${CMAKE_FIND_INSTALL_PATH}/Modules/)
+      ADD_CUSTOM_TARGET(uninstall @echo uninstall package)
     
-    ADD_CUSTOM_TARGET(uninstall @echo uninstall package)
-    
-    IF (UNIX)
-      ADD_CUSTOM_COMMAND(
-        COMMENT "uninstall package"
-        COMMAND xargs ARGS rm < install_manifest.txt
+      IF (UNIX)
+        ADD_CUSTOM_COMMAND(
+          COMMENT "uninstall package"
+          COMMAND xargs ARGS rm < install_manifest.txt
         
-        TARGET  uninstall
-      )
-    ELSE(UNIX)
-      ADD_CUSTOM_COMMAND(
-        COMMENT "uninstall only implemented in unix"
-        TARGET  uninstall
-      )
-    ENDIF(UNIX)
-ENDIF(NOT SERIAL_DONT_CONFIGURE_INSTALL)
+          TARGET  uninstall
+        )
+      ELSE(UNIX)
+        ADD_CUSTOM_COMMAND(
+          COMMENT "uninstall only implemented in unix"
+          TARGET  uninstall
+        )
+      ENDIF(UNIX)
+  ENDIF(NOT SERIAL_DONT_CONFIGURE_INSTALL)
 endmacro(build_serial)
diff --git a/serial.makefile b/serial.makefile
index 1667164..7ddb536 100644
--- a/serial.makefile
+++ b/serial.makefile
@@ -37,4 +37,4 @@ ifneq ($(MAKE),)
 else
 	cd build && make
 endif
-	cd bin && ./serial_listener_tests
\ No newline at end of file
+	cd bin && ./serial_tests
\ No newline at end of file
diff --git a/serial_ros.cmake b/serial_ros.cmake
index 029b83a..940721d 100644
--- a/serial_ros.cmake
+++ b/serial_ros.cmake
@@ -1,51 +1,41 @@
 macro(build_serial)
-cmake_minimum_required(VERSION 2.4.6)
-include($ENV{ROS_ROOT}/core/rosbuild/rosbuild.cmake)
 
-# Set the build type.  Options are:
-#  Coverage       : w/ debug symbols, w/o optimization, w/ code-coverage
-#  Debug          : w/ debug symbols, w/o optimization
-#  Release        : w/o debug symbols, w/ optimization
-#  RelWithDebInfo : w/ debug symbols, w/ optimization
-#  MinSizeRel     : w/o debug symbols, w/ optimization, stripped binaries
-set(ROS_BUILD_TYPE RelWithDebInfo)
+  cmake_minimum_required(VERSION 2.4.6)
+  include($ENV{ROS_ROOT}/core/rosbuild/rosbuild.cmake)
 
-rosbuild_init()
+  # Set the build type.  Options are:
+  #  Coverage       : w/ debug symbols, w/o optimization, w/ code-coverage
+  #  Debug          : w/ debug symbols, w/o optimization
+  #  Release        : w/o debug symbols, w/ optimization
+  #  RelWithDebInfo : w/ debug symbols, w/ optimization
+  #  MinSizeRel     : w/o debug symbols, w/ optimization, stripped binaries
+  set(ROS_BUILD_TYPE RelWithDebInfo)
 
-#set the default path for built executables to the "bin" directory
-set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
-#set the default path for built libraries to the "lib" directory
-set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
+  rosbuild_init()
 
-include_directories(include)
+  #set the default path for built executables to the "bin" directory
+  set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
+  #set the default path for built libraries to the "lib" directory
+  set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
 
-set(SERIAL_SRCS src/serial.cc)
-if(UNIX)
-  list(APPEND SERIAL_SRCS src/impl/unix.cc)
-else(UNIX)
-  list(APPEND SERIAL_SRCS src/impl/windows.cc)
-endif(UNIX)
-list(APPEND SERIAL_SRCS src/serial_listener.cc)
+  include_directories(include)
 
-# Build the serial library
-rosbuild_add_library(${PROJECT_NAME} ${SERIAL_SRCS})
+  set(SERIAL_SRCS src/serial.cc)
+  if(UNIX)
+    list(APPEND SERIAL_SRCS src/impl/unix.cc)
+  else(UNIX)
+    list(APPEND SERIAL_SRCS src/impl/winows.cc)
+  endif(UNIX)
 
-# Add boost dependencies
-rosbuild_add_boost_directories()
-rosbuild_link_boost(${PROJECT_NAME} system filesystem thread)
+  # Build the serial library
+  rosbuild_add_library(${PROJECT_NAME} ${SERIAL_SRCS})
 
-# Build example
-rosbuild_add_executable(serial_example examples/serial_example.cc)
-target_link_libraries(serial_example ${PROJECT_NAME})
+  # Build example
+  rosbuild_add_executable(serial_example examples/serial_example.cc)
+  target_link_libraries(serial_example ${PROJECT_NAME})
 
-rosbuild_add_executable(serial_listener_example
-  examples/serial_listener_example.cc)
-target_link_libraries(serial_listener_example ${PROJECT_NAME})
-
-# Create unit tests
-rosbuild_add_gtest(serial_tests tests/serial_tests.cc)
-target_link_libraries(serial_tests ${PROJECT_NAME})
-rosbuild_add_gtest(serial_listener_tests tests/serial_listener_tests.cc)
-target_link_libraries(serial_listener_tests ${PROJECT_NAME})
+  # Create unit tests
+  rosbuild_add_gtest(serial_tests tests/serial_tests.cc)
+  target_link_libraries(serial_tests ${PROJECT_NAME})
 
 endmacro(build_serial)
diff --git a/src/serial_listener.cc b/src/serial_listener.cc
deleted file mode 100644
index a7bbe98..0000000
--- a/src/serial_listener.cc
+++ /dev/null
@@ -1,217 +0,0 @@
-/* Copyright 2012 William Woodall and John Harrison */
-
-#include "serial/serial_listener.h"
-
-/***** Inline Functions *****/
-
-inline void defaultExceptionCallback(const std::exception &error) {
-  std::cerr << "SerialListener Unhandled Exception: " << error.what();
-  std::cerr << std::endl;
-}
-
-inline bool defaultComparator(const std::string &token) {
-  return token == token;
-}
-
-using namespace serial;
-
-/***** Listener Class Functions *****/
-
-void
-SerialListener::default_handler(const std::string &token) {
-  if (this->_default_handler)
-    this->_default_handler(token);
-}
-
-SerialListener::SerialListener() : listening(false), chunk_size_(5) {
-  // Set default callbacks
-  this->handle_exc = defaultExceptionCallback;
-
-  // Default handler stuff
-  this->_default_handler = NULL;
-  this->default_comparator = defaultComparator;
-  DataCallback tmp = boost::bind(&SerialListener::default_handler, this, _1);
-  this->default_filter = FilterPtr(new Filter(default_comparator, tmp));
-
-  // Set default tokenizer
-  this->setTokenizer(delimeter_tokenizer("\r"));
-}
-
-SerialListener::~SerialListener() {
-  if (this->listening) {
-    this->stopListening();
-  }
-}
-
-void
-SerialListener::callback() {
-  try {
-    // 
-    std::pair pair;
-    while (this->listening) {
-      if (this->callback_queue.timed_wait_and_pop(pair, 10)) {
-        if (this->listening) {
-          try {
-            if (pair.first != NULL && pair.second != NULL) {
-              pair.first->callback_((*pair.second));
-            }
-          } catch (std::exception &e) {
-            this->handle_exc(e);
-          }// try callback
-        } // if listening
-      } // if popped
-    } // while (this->listening)
-  } catch (std::exception &e) {
-    this->handle_exc(SerialListenerException(e.what()));
-  }
-}
-
-void
-SerialListener::startListening(Serial &serial_port) {
-  if (this->listening) {
-    throw(SerialListenerException("Already listening."));
-    return;
-  }
-  this->listening = true;
-  
-  this->serial_port_ = &serial_port;
-  if (!this->serial_port_->isOpen()) {
-    throw(SerialListenerException("Serial port not open."));
-    return;
-  }
-  
-  listen_thread = boost::thread(boost::bind(&SerialListener::listen, this));
-  
-  // Start the callback thread
-  callback_thread =
-   boost::thread(boost::bind(&SerialListener::callback, this));
-}
-
-void
-SerialListener::stopListening() {
-  // Stop listening and clear buffers
-  listening = false;
-
-  listen_thread.join();
-  callback_thread.join();
-
-  this->data_buffer = "";
-  this->serial_port_ = NULL;
-}
-
-size_t
-SerialListener::determineAmountToRead() {
-  // TODO: Make a more intelligent method based on the length of the things 
-  //  filters are looking for.  e.g.: if the filter is looking for 'V=XX\r' 
-  //  make the read amount at least 5.
-  return this->chunk_size_;
-}
-
-void
-SerialListener::filter(std::vector &tokens) {
-  // Lock the filters while filtering
-  boost::mutex::scoped_lock lock(filter_mux);
-  // Iterate through each new token and filter them
-  std::vector::iterator it;
-  for (it=tokens.begin(); it!=tokens.end(); it++) {
-    TokenPtr token = (*it);
-    // If it is empty then pass it
-    if (token->empty()) {
-      continue;
-    }
-    bool matched = false;
-    // Iterate through each filter
-    std::vector::iterator itt;
-    for (itt=filters.begin(); itt!=filters.end(); itt++) {
-      FilterPtr filter = (*itt);
-      if (filter->comparator_((*token))) {
-        callback_queue.push(std::make_pair(filter,token));
-        matched = true;
-        break;
-      }
-    } // for (itt=filters.begin(); itt!=filters.end(); itt++)
-    // If matched is false then send it to the default handler
-    if (!matched) {
-      callback_queue.push(std::make_pair(default_filter,token));
-    }
-  } // for (it=tokens.begin(); it!=tokens.end(); it++)
-}
-
-void
-SerialListener::listen() {
-  try {
-    while (this->listening) {
-      // Read some data
-      std::string temp;
-      this->readSomeData(temp, determineAmountToRead());
-      // If nothing was read then we
-      //  don't need to iterate through the filters
-      if (temp.length() != 0) {
-        // Add the new data to the buffer
-        this->data_buffer += temp;
-        // Call the tokenizer on the updated buffer
-        std::vector new_tokens;
-        this->tokenize(this->data_buffer, new_tokens);
-        // Put the last token back in the data buffer
-        this->data_buffer = (*new_tokens.back());
-        new_tokens.pop_back();
-        // Run the new tokens through existing filters
-        this->filter(new_tokens);
-      }
-      // Done parsing lines and buffer should now be set to the left overs
-    } // while (this->listening)
-  } catch (std::exception &e) {
-    this->handle_exc(SerialListenerException(e.what()));
-  }
-}
-
-/***** Filter Functions *****/
-
-FilterPtr
-SerialListener::createFilter(ComparatorType comparator, DataCallback callback)
-{
-  FilterPtr filter_ptr(new Filter(comparator, callback));
-
-  boost::mutex::scoped_lock l(filter_mux);
-  this->filters.push_back(filter_ptr);
-
-  return filter_ptr;
-}
-
-BlockingFilterPtr
-SerialListener::createBlockingFilter(ComparatorType comparator) {
-  return BlockingFilterPtr(
-    new BlockingFilter(comparator, (*this)));
-}
-
-BufferedFilterPtr
-SerialListener::createBufferedFilter(ComparatorType comparator,
-                                     size_t buffer_size)
-{
-  return BufferedFilterPtr(
-    new BufferedFilter(comparator, buffer_size, (*this)));
-}
-
-void
-SerialListener::removeFilter(FilterPtr filter_ptr) {
-  boost::mutex::scoped_lock l(filter_mux);
-  filters.erase(std::find(filters.begin(),filters.end(),filter_ptr));
-}
-
-void
-SerialListener::removeFilter(BlockingFilterPtr blocking_filter) {
-  this->removeFilter(blocking_filter->filter_ptr);
-}
-
-void
-SerialListener::removeFilter(BufferedFilterPtr buffered_filter) {
-  this->removeFilter(buffered_filter->filter_ptr);
-}
-
-void
-SerialListener::removeAllFilters() {
-  boost::mutex::scoped_lock l(filter_mux);
-  filters.clear();
-  callback_queue.clear();
-}
-
diff --git a/tests/serial_listener_tests.cc b/tests/serial_listener_tests.cc
deleted file mode 100644
index 52e12f1..0000000
--- a/tests/serial_listener_tests.cc
+++ /dev/null
@@ -1,238 +0,0 @@
-/* To run these tests you need to change the define below to the serial port 
- * with a loop back device attached.
- * 
- * Alternatively you could use an Arduino:
- * 
- *     void setup()
- *     {
- *       Serial.begin(115200);
- *     }
- *     
- *     void loop()
- *     {
- *       while (Serial.available() > 0) {
- *         Serial.write(Serial.read());
- *       }
- *     }
- * 
- */
-
-// #define SERIAL_PORT_NAME "/dev/tty.usbserial-A900cfJA"
-#define SERIAL_PORT_NAME "p0"
-
-#include "gtest/gtest.h"
-
-#include 
-
-// OMG this is so nasty...
-#define private public
-#define protected public
-
-#include "serial/serial_listener.h"
-using namespace serial;
-
-static size_t global_count, global_listen_count;
-static bool matched;
-
-void filter_handler(std::string token) {
-  global_listen_count++;
-  std::cout << "filter_handler got: " << token << std::endl;
-}
-
-void default_handler(std::string line) {
-  global_count++;
-  std::cout << "default_handler got: " << line << std::endl;
-}
-
-namespace {
-
-void my_sleep(long milliseconds) {
-  boost::this_thread::sleep(boost::posix_time::milliseconds(milliseconds));
-}
-
-class SerialListenerTests : public ::testing::Test {
-protected:
-  virtual void SetUp() {
-    port1 = new Serial("/dev/pty"SERIAL_PORT_NAME, 115200, 10);
-    port2 = new Serial("/dev/tty"SERIAL_PORT_NAME, 115200, 250);
-
-    listener.setDefaultHandler(default_handler);
-    listener.startListening((*port1));
-  }
-
-  virtual void TearDown() {
-    listener.stopListening();
-    delete port1;
-    delete port2;
-  }
-
-  SerialListener listener;
-  Serial * port1;
-  Serial * port2;
-
-};
-
-TEST_F(SerialListenerTests, handlesPartialMessage) {
-  global_count = 0;
-  std::string input_str = "?$1E\r$1E=Robo";
-
-  std::cout << "writing: ?$1E$1E=Robo" << std::endl;
-  port2->write(input_str);
-  // Allow time for processing
-  my_sleep(50);
-
-  ASSERT_EQ(1, global_count);
-
-  input_str = "?$1E\r$1E=Roboteq\r";
-  std::cout << "writing: ?$1E$1E=Roboteq" << std::endl;
-  port2->write(input_str);
-  // Allow time for processing
-  my_sleep(50);
-
-  ASSERT_EQ(3, global_count);
-}
-
-TEST_F(SerialListenerTests, normalFilterWorks) {
-  global_count = 0;
-  global_listen_count = 0;
-  std::string input_str = "?$1E\r$1E=Robo\rV=1334:1337\rT=123";
-
-  // Setup filter
-  FilterPtr filt_1 =
-    listener.createFilter(SerialListener::startsWith("V="), filter_handler);
-
-  std::cout << "writing: ?$1E$1E=RoboV=1334:1337T=123";
-  std::cout << std::endl;
-  port2->write(input_str);
-  // Allow time for processing
-  my_sleep(50);
-
-  ASSERT_EQ(2, global_count);
-  ASSERT_EQ(1, global_listen_count);
-}
-
-void run_blocking_filter(BlockingFilterPtr filt_1) {
-  // Wait 100 ms for a match
-  std::string temp = filt_1->wait(100);
-  if (temp.empty()) {
-    return;
-  }
-  std::cout << "blocking filter matched: " << temp << std::endl;
-  global_listen_count++;
-  matched = true;
-}
-
-TEST_F(SerialListenerTests, blockingFilterWorks) {
-  global_count = 0;
-  global_listen_count = 0;
-  matched = false;
-  std::string input_str = "?$1E\r$1E=Robo\rV=1334:1337\rT=123";
-
-  // Setup blocking filter
-  BlockingFilterPtr filt_1 =
-    listener.createBlockingFilter(SerialListener::startsWith("$1E="));
-
-  boost::thread t(boost::bind(run_blocking_filter, filt_1));
-
-  std::cout << "writing: ?$1E$1E=RoboV=1334:1337T=123";
-  std::cout << std::endl;
-  port2->write(input_str);
-  // Allow time for processing
-  my_sleep(50);
-
-  using boost::posix_time::milliseconds;
-  ASSERT_TRUE(t.timed_join(milliseconds(10)));
-  ASSERT_EQ(2, global_count);
-  ASSERT_EQ(1, global_listen_count);
-  ASSERT_TRUE(matched);
-}
-
-TEST_F(SerialListenerTests, blockingFilterTimesOut) {
-  global_count = 0;
-  global_listen_count = 0;
-  matched = false;
-  std::string input_str = "?$1E\r$1E=Robo\rV=1334:1337\rT=123";
-
-  // Setup blocking filter
-  BlockingFilterPtr filt_1 =
-    listener.createBlockingFilter(SerialListener::startsWith("T="));
-
-  boost::thread t(boost::bind(run_blocking_filter, filt_1));
-
-  std::cout << "writing: ?$1E$1E=RoboV=1334:1337T=123";
-  std::cout << std::endl;
-  port2->write(input_str);
-  // Allow time for processing
-  my_sleep(50);
-
-  using boost::posix_time::milliseconds;
-  // First one should not be within timeout, should be false
-  ASSERT_FALSE(t.timed_join(milliseconds(10)));
-  // Second one should capture timeout and return true to join
-  ASSERT_TRUE(t.timed_join(milliseconds(60)));
-  ASSERT_EQ(3, global_count);
-  ASSERT_EQ(0, global_listen_count);
-  ASSERT_FALSE(matched);
-}
-
-void write_later(Serial *port, std::string input_str, long wait_for) {
-  my_sleep(wait_for);
-  port->write(input_str);
-}
-
-TEST_F(SerialListenerTests, bufferedFilterWorks) {
-  global_count = 0;
-  std::string input_str = "?$1E\r+\r$1E=Robo\rV=1334:1337\rT=123";
-
-  // Setup buffered filter, buffer size 3
-  BufferedFilterPtr filt_1 =
-    listener.createBufferedFilter(SerialListener::exactly("+"), 3);
-
-  // Write the string to the port 10 ms in the future
-  boost::thread t(boost::bind(write_later, port2, input_str, 10));
-
-  // This should be empty because of a timeout
-  ASSERT_TRUE(filt_1->wait(2).empty());
-  // Make sure wait works properly
-  ASSERT_EQ("+", filt_1->wait(20));
-  // This should be empty cause there was only one
-  ASSERT_TRUE(filt_1->wait(2).empty());
-  // The queue in the filter should be empty
-  ASSERT_EQ(0, filt_1->queue.size());
-  ASSERT_EQ(3, global_count);
-  t.join();
-}
-
-TEST_F(SerialListenerTests, bufferedFilterQueueWorks) {
-  global_count = 0;
-  std::string input_str = "?$1E$\r+\r$1E=Robo$\rV=1334:1337$\rT=123$\r";
-
-  // Setup buffered filter, buffer size 3
-  BufferedFilterPtr filt_1 =
-    listener.createBufferedFilter(SerialListener::endsWith("$"), 3);
-
-  // write the string
-  port2->write(input_str);
-
-  my_sleep(20); // Let things process
-  // There should have been four matches
-  //   therefore the first one should the second match.
-  ASSERT_EQ("$1E=Robo$", filt_1->wait(1));
-  ASSERT_EQ("V=1334:1337$", filt_1->wait(1));
-  ASSERT_EQ("T=123$", filt_1->wait(1));
-  ASSERT_EQ(0, filt_1->queue.size());
-
-  ASSERT_EQ(1, global_count);
-}
-
-}  // namespace
-
-int main(int argc, char **argv) {
-  try {
-    ::testing::InitGoogleTest(&argc, argv);
-    return RUN_ALL_TESTS();
-  } catch (std::exception &e) {
-    std::cerr << "Unhandled Exception: " << e.what() << std::endl;
-  }
-  return 1;
-}

From c429b0eedef87e07bd0c330e8126b2f55c0b0652 Mon Sep 17 00:00:00 2001
From: William Woodall 
Date: Fri, 3 Feb 2012 01:43:42 -0600
Subject: [PATCH 57/72] Removed serial listener, reworking the example,
 completely removed boost.  Builds on my laptop with boost uninstalled.

---
 README.md                     |   1 -
 examples/serial_example.cc    | 134 +++----
 include/serial/impl/unix.h    |  35 +-
 include/serial/impl/windows.h | 177 +++++++++
 include/serial/serial.h       | 122 +++----
 serial_ros.cmake              |   1 +
 src/impl/unix.cc              |  40 +-
 src/impl/windows.cc           | 671 ++++++++++++++++++++++++++++++++++
 src/serial.cc                 |  48 ++-
 9 files changed, 1050 insertions(+), 179 deletions(-)
 create mode 100644 include/serial/impl/windows.h
 create mode 100644 src/impl/windows.cc

diff --git a/README.md b/README.md
index 201b165..31297de 100644
--- a/README.md
+++ b/README.md
@@ -7,7 +7,6 @@ Coming Soon!
 ## Dependencies
 
 * CMake, for the build system: http://www.cmake.org/
-* Boost, for threading: http://www.boost.org/
 * (Optional) ROS
 
 ## Stand Alone Installation
diff --git a/examples/serial_example.cc b/examples/serial_example.cc
index 788764d..60be39a 100644
--- a/examples/serial_example.cc
+++ b/examples/serial_example.cc
@@ -1,123 +1,79 @@
 #include 
 #include 
-#include 
-#include 
 
-#ifdef __MACH__
-#include 
-#include 
+// OS Specific sleep
+#ifdef __WIN32__
+#include 
+#else
+#include 
 #endif
 
-#include 
-
 #include "serial/serial.h"
 
 using std::string;
+using std::exception;
 using std::cout;
+using std::cerr;
 using std::endl;
 
+void my_sleep(unsigned long milliseconds) {
+#ifdef __WIN32__
+      Sleep(milliseconds); // 100 ms
+#else
+      usleep(milliseconds*1000); // 100 ms
+#endif
+}
+
 int run(int argc, char **argv)
 {
     if(argc < 3) {
-        std::cerr << "Usage: test_serial  " << std::endl;
-        return 0;
+      cerr << "Usage: test_serial  ";
+      cerr << " [test string]" << endl;
+      return 0;
     }
-    std::string port(argv[1]);
-    unsigned long baud = 0;
+    // Argument 1 is the serial port
+    string port(argv[1]);
 
+    // Argument 2 is the baudrate
+    unsigned long baud = 0;
     sscanf(argv[2], "%lu", &baud);
 
     // port, baudrate, timeout in milliseconds
-    serial::Serial serial(port, baud, 10000);
-    
-    std::cout << "Is the serial port open?";
+    serial::Serial serial(port, baud, 1000);
+
+    cout << "Is the serial port open?";
     if(serial.isOpen())
-        std::cout << " Yes." << std::endl;
+        cout << " Yes." << endl;
     else
-        std::cout << " No." << std::endl;
-    
+        cout << " No." << endl;
+
     int count = 0;
-    while (count >= 0) {
-      struct timespec start, end;
-      double diff;
+    string test_string;
+    if (argc == 4) {
+      test_string = argv[3];
+    } else {
+      test_string = "Testing.";
+    }
+    while (true) {
+      size_t bytes_wrote = serial.write(test_string);
 
-      #ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
-      clock_serv_t cclock;
-      mach_timespec_t mts;
-      host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
-      clock_get_time(cclock, &mts);
-      mach_port_deallocate(mach_task_self(), cclock);
-      start.tv_sec = mts.tv_sec;
-      start.tv_nsec = mts.tv_nsec;
+      string result = serial.read(test_string.length());
 
-      #else
-      clock_gettime(CLOCK_REALTIME, &start);
-      #endif
+      cout << "Iteration: " << count << ", Bytes written: ";
+      cout << bytes_wrote << ", Bytes read: ";
+      cout << result.length() << ", String read: " << result << endl;
 
-      size_t bytes_wrote = serial.write("Testing.\n");
-
-      #ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
-      host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
-      clock_get_time(cclock, &mts);
-      mach_port_deallocate(mach_task_self(), cclock);
-      end.tv_sec = mts.tv_sec;
-      end.tv_nsec = mts.tv_nsec;
-
-      #else
-      clock_gettime(CLOCK_REALTIME, &end);
-      #endif
-
-      end.tv_sec -= start.tv_sec;
-      end.tv_nsec -= start.tv_nsec;
-      printf("write: %05lu.%09lu\n", end.tv_sec, end.tv_nsec);
-
-      #ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
-      host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
-      clock_get_time(cclock, &mts);
-      mach_port_deallocate(mach_task_self(), cclock);
-      start.tv_sec = mts.tv_sec;
-      start.tv_nsec = mts.tv_nsec;
-
-      #else
-      clock_gettime(CLOCK_REALTIME, &start);
-      #endif
-
-      std::string result = serial.readline();
-      #ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
-      host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
-      clock_get_time(cclock, &mts);
-      mach_port_deallocate(mach_task_self(), cclock);
-      end.tv_sec = mts.tv_sec;
-      end.tv_nsec = mts.tv_nsec;
-
-      #else
-      clock_gettime(CLOCK_REALTIME, &end);
-      #endif
-
-
-      end.tv_sec -= start.tv_sec;
-      end.tv_nsec -= start.tv_nsec;
-      printf("read: %05lu.%09lu\n", end.tv_sec, end.tv_nsec);
-      
-      if (result == string("Testing.\n")) {
-      }
-      else {
-        std::cout << ">" << count << ">" << bytes_wrote << ">";
-        std::cout << result.length() << "<" << result << std::endl;
-        cout << "No" << endl;
-      }
-        
       count += 1;
-      boost::this_thread::sleep(boost::posix_time::milliseconds(100));
+      my_sleep(10);
     }
     
     return 0;
 }
 
 int main(int argc, char **argv) {
-  // try {
+  try {
     return run(argc, argv);
-  // } catch (std::exception &e) {
-  //   std::cerr << "Unhandled Exception: " << e.what() << std::endl;
-  // }
+  } catch (exception &e) {
+    cerr << "Unhandled Exception: " << e.what() << endl;
+  }
 }
diff --git a/include/serial/impl/unix.h b/include/serial/impl/unix.h
index 1bb1eb3..b0d630b 100644
--- a/include/serial/impl/unix.h
+++ b/include/serial/impl/unix.h
@@ -40,6 +40,8 @@
 
 #include "serial/serial.h"
 
+#include 
+
 namespace serial {
 
 using std::string;
@@ -98,40 +100,40 @@ public:
 
   void
   setDTR(bool level);
-  
+
   bool
   getCTS();
-  
+
   bool
   getDSR();
-  
+
   bool
   getRI();
-  
+
   bool
   getCD();
 
   void
   setPort (const string &port);
-  
+
   string
   getPort () const;
 
   void
   setTimeout (long timeout);
-  
+
   long
   getTimeout () const;
 
   void
   setBaudrate (unsigned long baudrate);
-  
+
   unsigned long
   getBaudrate () const;
 
   void
   setBytesize (bytesize_t bytesize);
-  
+
   bytesize_t
   getBytesize () const;
 
@@ -153,6 +155,18 @@ public:
   flowcontrol_t
   getFlowcontrol () const;
 
+  void
+  readLock();
+
+  void
+  readUnlock();
+
+  void
+  writeLock();
+
+  void
+  writeUnlock();
+
 protected:
   void reconfigurePort ();
 
@@ -171,6 +185,11 @@ private:
   bytesize_t bytesize_;       // Size of the bytes
   stopbits_t stopbits_;       // Stop Bits
   flowcontrol_t flowcontrol_; // Flow Control
+
+  // Mutex used to lock the read functions
+  pthread_mutex_t read_mutex;
+  // Mutex used to lock the write functions
+  pthread_mutex_t write_mutex;
 };
 
 }
diff --git a/include/serial/impl/windows.h b/include/serial/impl/windows.h
new file mode 100644
index 0000000..aa06706
--- /dev/null
+++ b/include/serial/impl/windows.h
@@ -0,0 +1,177 @@
+/*!
+ * \file serial/impl/windows.h
+ * \author  William Woodall 
+ * \author  John Harrison 
+ * \version 0.1
+ *
+ * \section LICENSE
+ *
+ * The MIT License
+ *
+ * Copyright (c) 2011 William Woodall, John Harrison
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a 
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
+ * DEALINGS IN THE SOFTWARE.
+ *
+ * \section DESCRIPTION
+ *
+ * This provides a windows implementation of the Serial class interface.
+ *
+ */
+
+#ifndef SERIAL_IMPL_WINDOWS_H
+#define SERIAL_IMPL_WINDOWS_H
+
+#include "serial/serial.h"
+
+namespace serial {
+
+using std::string;
+using std::invalid_argument;
+
+using serial::SerialExecption;
+using serial::IOException;
+
+class serial::Serial::SerialImpl {
+public:
+  SerialImpl (const string &port,
+              unsigned long baudrate,
+              long timeout,
+              bytesize_t bytesize,
+              parity_t parity,
+              stopbits_t stopbits,
+              flowcontrol_t flowcontrol);
+
+  virtual ~SerialImpl ();
+
+  void
+  open ();
+
+  void
+  close ();
+
+  bool
+  isOpen () const;
+
+  size_t
+  available ();
+
+  size_t
+  read (char* buf, size_t size = 1);
+
+  size_t
+  write (const string &data);
+
+  void
+  flush ();
+
+  void
+  flushInput ();
+
+  void
+  flushOutput ();
+
+  void
+  sendBreak(int duration);
+
+  void
+  setBreak(bool level);
+
+  void
+  setRTS(bool level);
+
+  void
+  setDTR(bool level);
+  
+  bool
+  getCTS();
+  
+  bool
+  getDSR();
+  
+  bool
+  getRI();
+  
+  bool
+  getCD();
+
+  void
+  setPort (const string &port);
+  
+  string
+  getPort () const;
+
+  void
+  setTimeout (long timeout);
+  
+  long
+  getTimeout () const;
+
+  void
+  setBaudrate (unsigned long baudrate);
+  
+  unsigned long
+  getBaudrate () const;
+
+  void
+  setBytesize (bytesize_t bytesize);
+  
+  bytesize_t
+  getBytesize () const;
+
+  void
+  setParity (parity_t parity);
+
+  parity_t
+  getParity () const;
+
+  void
+  setStopbits (stopbits_t stopbits);
+
+  stopbits_t
+  getStopbits () const;
+
+  void
+  setFlowcontrol (flowcontrol_t flowcontrol);
+
+  flowcontrol_t
+  getFlowcontrol () const;
+
+protected:
+  void reconfigurePort ();
+
+private:
+  string port_;               // Path to the file descriptor
+  int fd_;                    // The current file descriptor
+
+  bool isOpen_;
+  bool xonxoff_;
+  bool rtscts_;
+
+  long timeout_;              // Timeout for read operations
+  unsigned long baudrate_;    // Baudrate
+
+  parity_t parity_;           // Parity
+  bytesize_t bytesize_;       // Size of the bytes
+  stopbits_t stopbits_;       // Stop Bits
+  flowcontrol_t flowcontrol_; // Flow Control
+};
+
+}
+
+#endif // SERIAL_IMPL_WINDOWS_H
diff --git a/include/serial/serial.h b/include/serial/serial.h
index f23c6b1..0311577 100644
--- a/include/serial/serial.h
+++ b/include/serial/serial.h
@@ -36,16 +36,12 @@
 #ifndef SERIAL_H
 #define SERIAL_H
 
-#include 
-#include 
-#if defined(__linux__)
-#include 
-#endif
-#include 
-#include 
 #include 
-
-#include 
+#include 
+#include 
+#include 
+#include 
+#include 
 
 namespace serial {
 
@@ -86,58 +82,6 @@ typedef enum {
   FLOWCONTROL_HARDWARE
 } flowcontrol_t;
 
-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 " << e_what_ << " failed.";
-    return ss.str ().c_str ();
-  }
-};
-
-class IOException : public std::exception
-{
-  const char* e_what_;
-  int errno_;
-public:
-  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;
-    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_;
-public:
-  PortNotOpenedException (const char * description) : e_what_ (description) {}
-
-  virtual const char* what () const throw ()
-  {
-    std::stringstream ss;
-    ss << e_what_ << " called before port was opened.";
-    return ss.str ().c_str ();
-  }
-};
-
-
 /*!
  * Class that provides a portable serial port interface.
  */
@@ -462,7 +406,61 @@ private:
   class SerialImpl;
   SerialImpl *pimpl_;
 
-  boost::mutex mut;
+  // Scoped Lock Classes
+  class ScopedReadLock;
+  class ScopedWriteLock;
+
+};
+
+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 " << e_what_ << " failed.";
+    return ss.str ().c_str ();
+  }
+};
+
+class IOException : public std::exception
+{
+  const char* e_what_;
+  int errno_;
+public:
+  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;
+    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_;
+public:
+  PortNotOpenedException (const char * description) : e_what_ (description) {}
+
+  virtual const char* what () const throw ()
+  {
+    std::stringstream ss;
+    ss << e_what_ << " called before port was opened.";
+    return ss.str ().c_str ();
+  }
 };
 
 } // namespace serial
diff --git a/serial_ros.cmake b/serial_ros.cmake
index 940721d..53f8817 100644
--- a/serial_ros.cmake
+++ b/serial_ros.cmake
@@ -19,6 +19,7 @@ macro(build_serial)
   set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
 
   include_directories(include)
+  include_directories(vendor)
 
   set(SERIAL_SRCS src/serial.cc)
   if(UNIX)
diff --git a/src/impl/unix.cc b/src/impl/unix.cc
index c621d04..9a5c23b 100644
--- a/src/impl/unix.cc
+++ b/src/impl/unix.cc
@@ -13,6 +13,7 @@
 #include 
 #include 
 #include 
+#include 
 
 #if defined(__linux__)
 #include 
@@ -41,9 +42,11 @@ Serial::SerialImpl::SerialImpl (const string &port, unsigned long baudrate,
                                 parity_t parity, stopbits_t stopbits,
                                 flowcontrol_t flowcontrol)
 : port_ (port), fd_ (-1), isOpen_ (false), xonxoff_ (true), rtscts_ (false),
-  timeout_ (timeout), baudrate_ (baudrate), parity_ (parity), bytesize_ (bytesize),
-  stopbits_ (stopbits), flowcontrol_ (flowcontrol)
+  timeout_ (timeout), baudrate_ (baudrate), parity_ (parity),
+  bytesize_ (bytesize), stopbits_ (stopbits), flowcontrol_ (flowcontrol)
 {
+  pthread_mutex_init(&this->read_mutex, NULL);
+  pthread_mutex_init(&this->write_mutex, NULL);
   if (port_.empty () == false)
     open ();
 }
@@ -51,6 +54,8 @@ Serial::SerialImpl::SerialImpl (const string &port, unsigned long baudrate,
 Serial::SerialImpl::~SerialImpl ()
 {
   close();
+  pthread_mutex_destroy(&this->read_mutex);
+  pthread_mutex_destroy(&this->write_mutex);
 }
 
 void
@@ -695,3 +700,34 @@ Serial::SerialImpl::getCD()
   return (s & TIOCM_CD) != 0;
 }
 
+void
+Serial::SerialImpl::readLock() {
+  int result = pthread_mutex_lock(&this->read_mutex);
+  if (result) {
+    throw (IOException (result));
+  }
+}
+
+void
+Serial::SerialImpl::readUnlock() {
+  int result = pthread_mutex_unlock(&this->read_mutex);
+  if (result) {
+    throw (IOException (result));
+  }
+}
+
+void
+Serial::SerialImpl::writeLock() {
+  int result = pthread_mutex_lock(&this->write_mutex);
+  if (result) {
+    throw (IOException (result));
+  }
+}
+
+void
+Serial::SerialImpl::writeUnlock() {
+  int result = pthread_mutex_unlock(&this->write_mutex);
+  if (result) {
+    throw (IOException (result));
+  }
+}
diff --git a/src/impl/windows.cc b/src/impl/windows.cc
new file mode 100644
index 0000000..48030a9
--- /dev/null
+++ b/src/impl/windows.cc
@@ -0,0 +1,671 @@
+/* Copyright 2012 William Woodall and John Harrison */
+
+#include "serial/impl/windows.h"
+
+using std::string;
+using std::invalid_argument;
+using serial::Serial;
+using serial::SerialExecption;
+using serial::PortNotOpenedException;
+using serial::IOException;
+
+
+Serial::SerialImpl::SerialImpl (const string &port, unsigned long baudrate,
+                                long timeout, bytesize_t bytesize,
+                                parity_t parity, stopbits_t stopbits,
+                                flowcontrol_t flowcontrol)
+: port_ (port), fd_ (-1), isOpen_ (false), xonxoff_ (true), rtscts_ (false),
+  timeout_ (timeout), baudrate_ (baudrate), parity_ (parity), bytesize_ (bytesize),
+  stopbits_ (stopbits), flowcontrol_ (flowcontrol)
+{
+  if (port_.empty () == false)
+    open ();
+}
+
+Serial::SerialImpl::~SerialImpl ()
+{
+  close();
+}
+
+void
+Serial::SerialImpl::open ()
+{
+  if (port_.empty())
+  {
+    throw invalid_argument ("bad port specified");
+  }
+  if (isOpen_ == true)
+  {
+    throw SerialExecption ("port already open");
+  }
+
+  fd_ = ::open (port_.c_str(), O_RDWR | O_NOCTTY | O_NONBLOCK);
+
+  if (fd_ == -1)
+  {
+    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();
+  isOpen_ = true;
+}
+
+void
+Serial::SerialImpl::reconfigurePort ()
+{
+  if (fd_ == -1)
+  {
+    // Can only operate on a valid file descriptor
+    throw IOException ("invalid file descriptor");
+  }
+
+  struct termios options; // The options for the file descriptor
+
+  if (tcgetattr(fd_, &options) == -1)
+  {
+    throw IOException ("::tcgetattr");
+  }
+
+  // set up raw mode / no echo / binary
+  options.c_cflag |= (unsigned long)  (CLOCAL|CREAD);
+  options.c_lflag &= (unsigned long) ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL|
+                                       ISIG|IEXTEN); //|ECHOPRT
+
+  options.c_oflag &= (unsigned long) ~(OPOST);
+  options.c_iflag &= (unsigned long) ~(INLCR|IGNCR|ICRNL|IGNBRK);
+#ifdef IUCLC
+  options.c_iflag &= (unsigned long) ~IUCLC;
+#endif
+#ifdef PARMRK
+  options.c_iflag &= (unsigned long) ~PARMRK;
+#endif
+
+  // setup baud rate
+  bool custom_baud = false;
+  speed_t baud;
+  switch (baudrate_)
+  {
+#ifdef B0
+    case 0: baud = B0; break;
+#endif
+#ifdef B50
+    case 50: baud = B50; break;
+#endif
+#ifdef B75
+    case 75: baud = B75; break;
+#endif
+#ifdef B110
+    case 110: baud = B110; break;
+#endif
+#ifdef B134
+    case 134: baud = B134; break;
+#endif
+#ifdef B150
+    case 150: baud = B150; break;
+#endif
+#ifdef B200
+    case 200: baud = B200; break;
+#endif
+#ifdef B300
+    case 300: baud = B300; break;
+#endif
+#ifdef B600
+    case 600: baud = B600; break;
+#endif
+#ifdef B1200
+    case 1200: baud = B1200; break;
+#endif
+#ifdef B1800
+    case 1800: baud = B1800; break;
+#endif
+#ifdef B2400
+    case 2400: baud = B2400; break;
+#endif
+#ifdef B4800
+    case 4800: baud = B4800; break;
+#endif
+#ifdef B7200
+    case 7200: baud = B7200; break;
+#endif
+#ifdef B9600
+    case 9600: baud = B9600; break;
+#endif
+#ifdef B14400
+    case 14400: baud = B14400; break;
+#endif
+#ifdef B19200
+    case 19200: baud = B19200; break;
+#endif
+#ifdef B28800
+    case 28800: baud = B28800; break;
+#endif
+#ifdef B57600
+    case 57600: baud = B57600; break;
+#endif
+#ifdef B76800
+    case 76800: baud = B76800; break;
+#endif
+#ifdef B38400
+    case 38400: baud = B38400; break;
+#endif
+#ifdef B115200
+    case 115200: baud = B115200; break;
+#endif
+#ifdef B128000
+    case 128000: baud = B128000; break;
+#endif
+#ifdef B153600
+    case 153600: baud = B153600; break;
+#endif
+#ifdef B230400
+    case 230400: baud = B230400; break;
+#endif
+#ifdef B256000
+    case 256000: baud = B256000; break;
+#endif
+#ifdef B460800
+    case 460800: baud = B460800; break;
+#endif
+#ifdef B921600
+    case 921600: baud = B921600; break;
+#endif
+    default:
+      custom_baud = true;
+// Mac OS X 10.x Support
+#if defined(__APPLE__) && defined(__MACH__)
+#define IOSSIOSPEED _IOW('T', 2, speed_t)
+      int new_baud = static_cast (baudrate_);
+      if (ioctl (fd_, IOSSIOSPEED, &new_baud, 1) < 0)
+      {
+        throw IOException (errno);
+      }
+// Linux Support
+#elif defined(__linux__)
+      struct serial_struct ser;
+      ioctl(fd_, TIOCGSERIAL, &ser);
+      // set custom divisor
+      ser.custom_divisor = ser.baud_base / baudrate_;
+      // update flags
+      ser.flags &= ~ASYNC_SPD_MASK;
+      ser.flags |= ASYNC_SPD_CUST;
+
+      if (ioctl(fd_, TIOCSSERIAL, ser) < 0)
+      {
+        throw IOException (errno);
+      }
+#else
+      throw invalid_argument ("OS does not currently support custom bauds");
+#endif
+  }
+  if (custom_baud == false)
+  {
+#ifdef _BSD_SOURCE
+    ::cfsetspeed(&options, baud);
+#else
+    ::cfsetispeed(&options, baud);
+    ::cfsetospeed(&options, baud);
+#endif
+  }
+
+  // setup char len
+  options.c_cflag &= (unsigned long) ~CSIZE;
+  if (bytesize_ == EIGHTBITS)
+      options.c_cflag |= CS8;
+  else if (bytesize_ == SEVENBITS)
+      options.c_cflag |= CS7;
+  else if (bytesize_ == SIXBITS)
+      options.c_cflag |= CS6;
+  else if (bytesize_ == FIVEBITS)
+      options.c_cflag |= CS5;
+  else
+      throw invalid_argument ("invalid char len");
+  // setup stopbits
+  if (stopbits_ == STOPBITS_ONE)
+      options.c_cflag &= (unsigned long) ~(CSTOPB);
+  else if (stopbits_ == STOPBITS_ONE_POINT_FIVE)
+      options.c_cflag |=  (CSTOPB);  // XXX same as TWO.. there is no POSIX support for 1.5
+  else if (stopbits_ == STOPBITS_TWO)
+      options.c_cflag |=  (CSTOPB);
+  else
+      throw invalid_argument ("invalid stop bit");
+  // setup parity
+  options.c_iflag &= (unsigned long) ~(INPCK|ISTRIP);
+  if (parity_ == PARITY_NONE)
+  {
+    options.c_cflag &= (unsigned long) ~(PARENB|PARODD);
+  }
+  else if (parity_ == PARITY_EVEN)
+  {
+    options.c_cflag &= (unsigned long) ~(PARODD);
+    options.c_cflag |=  (PARENB);
+  }
+  else if (parity_ == PARITY_ODD)
+  {
+    options.c_cflag |=  (PARENB|PARODD);
+  }
+  else
+  {
+    throw invalid_argument ("invalid parity");
+  }
+  // setup flow control
+  // xonxoff
+#ifdef IXANY
+  if (xonxoff_)
+    options.c_iflag |=  (IXON|IXOFF); //|IXANY)
+  else
+    options.c_iflag &= (unsigned long) ~(IXON|IXOFF|IXANY);
+#else
+  if (xonxoff_)
+    options.c_iflag |=  (IXON|IXOFF);
+  else
+    options.c_iflag &= (unsigned long) ~(IXON|IXOFF);
+#endif
+  // rtscts
+#ifdef CRTSCTS
+  if (rtscts_)
+    options.c_cflag |=  (CRTSCTS);
+  else
+    options.c_cflag &= (unsigned long) ~(CRTSCTS);
+#elif defined CNEW_RTSCTS
+  if (rtscts_)
+    options.c_cflag |=  (CNEW_RTSCTS);
+  else
+    options.c_cflag &= (unsigned long) ~(CNEW_RTSCTS);
+#else
+#error "OS Support seems wrong."
+#endif
+
+  options.c_cc[VMIN] = 1; // Minimum of 1 character in the buffer
+  options.c_cc[VTIME] = 0; // timeout on waiting for new data
+
+  // activate settings
+  ::tcsetattr (fd_, TCSANOW, &options);
+}
+
+void
+Serial::SerialImpl::close ()
+{
+  if (isOpen_ == true)
+  {
+    if (fd_ != -1)
+    {
+      ::close (fd_); // Ignoring the outcome
+      fd_ = -1;
+    }
+    isOpen_ = false;
+  }
+}
+
+bool
+Serial::SerialImpl::isOpen () const
+{
+  return isOpen_;
+}
+
+size_t
+Serial::SerialImpl::available ()
+{
+  if (!isOpen_)
+  {
+    return 0;
+  }
+  int count = 0;
+  int result = ioctl (fd_, TIOCINQ, &count);
+  if (result == 0)
+  {
+    return static_cast (count);
+  }
+  else
+  {
+    throw IOException (errno);
+  }
+}
+
+size_t
+Serial::SerialImpl::read (char* buf, size_t size)
+{
+  if (!isOpen_)
+  {
+    throw PortNotOpenedException ("Serial::read");
+  }
+  fd_set readfds;
+  ssize_t bytes_read = 0;
+  int count = 0;
+  while (true)
+  {
+    count++;
+    // printf("Counting: %u\n", count);
+    if (timeout_ != -1)
+    {
+      FD_ZERO (&readfds);
+      FD_SET (fd_, &readfds);
+      struct timeval timeout;
+      timeout.tv_sec =                    timeout_ / 1000;
+      timeout.tv_usec = static_cast (timeout_ % 1000) * 1000;
+      int r = select (fd_ + 1, &readfds, NULL, NULL, &timeout);
+
+      if (r == -1 && errno == EINTR)
+        continue;
+
+      if (r == -1)
+      {
+        throw IOException (errno);
+      }
+    }
+
+    if (timeout_ == -1 || FD_ISSET (fd_, &readfds))
+    {
+      bytes_read = ::read (fd_, buf, size);
+      // read should always return some data as select reported it was
+      // ready to read when we get to this point.
+      if (bytes_read < 1)
+      {
+        // Disconnected devices, at least on Linux, show the
+        // behavior that they are always ready to read immediately
+        // but reading returns nothing.
+        throw SerialExecption ("device reports readiness to read but "
+                               "returned no data (device disconnected?)");
+      }
+      break;
+    }
+    else
+    {
+      break;
+    }
+  }
+  return static_cast (bytes_read);
+}
+
+size_t
+Serial::SerialImpl::write (const string &data)
+{
+  if (isOpen_ == false)
+  {
+    throw PortNotOpenedException ("Serial::write");
+  }
+  
+  fd_set writefds;
+  ssize_t bytes_written = 0;
+  while (true)
+  {
+    if (timeout_ != -1)
+    {
+      FD_ZERO (&writefds);
+      FD_SET (fd_, &writefds);
+      struct timeval timeout;
+      timeout.tv_sec =                    timeout_ / 1000;
+      timeout.tv_usec = static_cast (timeout_ % 1000) * 1000;
+      int r = select (fd_ + 1, NULL, &writefds, NULL, &timeout);
+
+      if (r == -1 && errno == EINTR)
+        continue;
+
+      if (r == -1)
+      {
+        throw IOException (errno);
+      }
+    }
+
+    if (timeout_ == -1 || FD_ISSET (fd_, &writefds))
+    {
+      bytes_written = ::write (fd_, data.c_str (), data.length ());
+      // read should always return some data as select reported it was
+      // ready to read when we get to this point.
+      if (bytes_written < 1)
+      {
+        // Disconnected devices, at least on Linux, show the
+        // behavior that they are always ready to read immediately
+        // but reading returns nothing.
+        throw SerialExecption ("device reports readiness to read but "
+                               "returned no data (device disconnected?)");
+      }
+      break;
+    }
+    else
+    {
+      break;
+    }
+  }
+
+  return static_cast (bytes_written);
+}
+
+void
+Serial::SerialImpl::setPort (const string &port)
+{
+  port_ = port;
+}
+
+string
+Serial::SerialImpl::getPort () const
+{
+  return port_;
+}
+
+void
+Serial::SerialImpl::setTimeout (long timeout)
+{
+  timeout_ = timeout;
+}
+
+long
+Serial::SerialImpl::getTimeout () const
+{
+  return timeout_;
+}
+
+void
+Serial::SerialImpl::setBaudrate (unsigned long baudrate)
+{
+  baudrate_ = baudrate;
+  if (isOpen_)
+    reconfigurePort ();
+}
+
+unsigned long
+Serial::SerialImpl::getBaudrate () const
+{
+  return baudrate_;
+}
+
+void
+Serial::SerialImpl::setBytesize (serial::bytesize_t bytesize)
+{
+  bytesize_ = bytesize;
+  if (isOpen_)
+    reconfigurePort ();
+}
+
+serial::bytesize_t
+Serial::SerialImpl::getBytesize () const
+{
+  return bytesize_;
+}
+
+void
+Serial::SerialImpl::setParity (serial::parity_t parity)
+{
+  parity_ = parity;
+  if (isOpen_)
+    reconfigurePort ();
+}
+
+serial::parity_t
+Serial::SerialImpl::getParity () const
+{
+  return parity_;
+}
+
+void
+Serial::SerialImpl::setStopbits (serial::stopbits_t stopbits)
+{
+  stopbits_ = stopbits;
+  if (isOpen_)
+    reconfigurePort ();
+}
+
+serial::stopbits_t
+Serial::SerialImpl::getStopbits () const
+{
+  return stopbits_;
+}
+
+void
+Serial::SerialImpl::setFlowcontrol (serial::flowcontrol_t flowcontrol)
+{
+  flowcontrol_ = flowcontrol;
+  if (isOpen_)
+    reconfigurePort ();
+}
+
+serial::flowcontrol_t
+Serial::SerialImpl::getFlowcontrol () const
+{
+  return flowcontrol_;
+}
+
+void
+Serial::SerialImpl::flush ()
+{
+  if (isOpen_ == false)
+  {
+    throw PortNotOpenedException ("Serial::flush");
+  }
+  tcdrain (fd_);
+}
+
+void
+Serial::SerialImpl::flushInput ()
+{
+  if (isOpen_ == false)
+  {
+    throw PortNotOpenedException ("Serial::flushInput");
+  }
+  tcflush (fd_, TCIFLUSH);
+}
+
+void
+Serial::SerialImpl::flushOutput ()
+{
+  if (isOpen_ == false)
+  {
+    throw PortNotOpenedException ("Serial::flushOutput");
+  }
+  tcflush (fd_, TCOFLUSH);
+}
+
+void
+Serial::SerialImpl::sendBreak (int duration)
+{
+  if (isOpen_ == false)
+  {
+    throw PortNotOpenedException ("Serial::sendBreak");
+  }
+  tcsendbreak (fd_, static_cast (duration/4));
+}
+
+void
+Serial::SerialImpl::setBreak (bool level)
+{
+  if (isOpen_ == false)
+  {
+    throw PortNotOpenedException ("Serial::setBreak");
+  }
+  if (level)
+  {
+    ioctl (fd_, TIOCSBRK);
+  }
+  else {
+    ioctl (fd_, TIOCCBRK);
+  }
+}
+
+void
+Serial::SerialImpl::setRTS (bool level)
+{
+  if (isOpen_ == false)
+  {
+    throw PortNotOpenedException ("Serial::setRTS");
+  }
+  if (level)
+  {
+    ioctl (fd_, TIOCMBIS, TIOCM_RTS);
+  }
+  else {
+    ioctl (fd_, TIOCMBIC, TIOCM_RTS);
+  }
+}
+
+void
+Serial::SerialImpl::setDTR (bool level)
+{
+  if (isOpen_ == false)
+  {
+    throw PortNotOpenedException ("Serial::setDTR");
+  }
+  if (level)
+  {
+    ioctl (fd_, TIOCMBIS, TIOCM_DTR);
+  }
+  else
+  {
+    ioctl (fd_, TIOCMBIC, TIOCM_DTR);
+  }
+}
+
+bool
+Serial::SerialImpl::getCTS ()
+{
+  if (isOpen_ == false)
+  {
+    throw PortNotOpenedException ("Serial::getCTS");
+  }
+  int s = ioctl (fd_, TIOCMGET, 0);
+  return (s & TIOCM_CTS) != 0;
+}
+
+bool
+Serial::SerialImpl::getDSR()
+{
+  if (isOpen_ == false)
+  {
+    throw PortNotOpenedException ("Serial::getDSR");
+  }
+  int s = ioctl(fd_, TIOCMGET, 0);
+  return (s & TIOCM_DSR) != 0;
+}
+
+bool
+Serial::SerialImpl::getRI()
+{
+  if (isOpen_ == false)
+  {
+    throw PortNotOpenedException ("Serial::getRI");
+  }
+  int s = ioctl (fd_, TIOCMGET, 0);
+  return (s & TIOCM_RI) != 0;
+}
+
+bool
+Serial::SerialImpl::getCD()
+{
+  if (isOpen_ == false)
+  {
+    throw PortNotOpenedException ("Serial::getCD");
+  }
+  int s = ioctl (fd_, TIOCMGET, 0);
+  return (s & TIOCM_CD) != 0;
+}
+
diff --git a/src/serial.cc b/src/serial.cc
index 112436c..b058675 100644
--- a/src/serial.cc
+++ b/src/serial.cc
@@ -1,12 +1,5 @@
 /* Copyright 2012 William Woodall and John Harrison */
 
-#include 
-
-#include 
-#include 
-
-#include 
-
 #include "serial/serial.h"
 
 #ifdef _WIN32
@@ -31,14 +24,35 @@ using serial::parity_t;
 using serial::stopbits_t;
 using serial::flowcontrol_t;
 
-using boost::mutex;
+class Serial::ScopedReadLock {
+public:
+  ScopedReadLock(SerialImpl *pimpl) : pimpl_(pimpl) {
+    this->pimpl_->readLock();
+  }
+  ~ScopedReadLock() {
+    this->pimpl_->readUnlock();
+  }
+private:
+  SerialImpl *pimpl_;
+};
+
+class Serial::ScopedWriteLock {
+public:
+  ScopedWriteLock(SerialImpl *pimpl) : pimpl_(pimpl) {
+    this->pimpl_->writeLock();
+  }
+  ~ScopedWriteLock() {
+    this->pimpl_->writeUnlock();
+  }
+private:
+  SerialImpl *pimpl_;
+};
 
 Serial::Serial (const string &port, unsigned long baudrate, long timeout,
                 bytesize_t bytesize, parity_t parity, stopbits_t stopbits,
                 flowcontrol_t flowcontrol)
  : read_cache_("")
 {
-  mutex::scoped_lock scoped_lock(mut);
   pimpl_ = new SerialImpl (port, baudrate, timeout, bytesize, parity,
                            stopbits, flowcontrol);
 }
@@ -75,7 +89,7 @@ Serial::available ()
 string
 Serial::read (size_t size)
 {
-  mutex::scoped_lock scoped_lock (mut);
+  ScopedReadLock(this->pimpl_);
   if (read_cache_.size() >= size)
   {
     // Don't need to do a new read.
@@ -89,10 +103,8 @@ Serial::read (size_t size)
     string result (read_cache_.substr (0, size));
     read_cache_.clear ();
 
-	int count = 0;
     while (true)
     {
-	  // printf("%u\n", count++);
       char buf[256];
       size_t chars_read = pimpl_->read (buf, 256);
       if (chars_read > 0)
@@ -173,14 +185,15 @@ Serial::readlines(string eol)
 size_t
 Serial::write (const string &data)
 {
-  mutex::scoped_lock scoped_lock(mut);
+  ScopedWriteLock(this->pimpl_);
   return pimpl_->write (data);
 }
 
 void
 Serial::setPort (const string &port)
 {
-  mutex::scoped_lock scoped_lock(mut);
+  ScopedReadLock(this->pimpl_);
+  ScopedWriteLock(this->pimpl_);
   bool was_open = pimpl_->isOpen();
   if (was_open) close();
   pimpl_->setPort (port);
@@ -266,19 +279,21 @@ Serial::getFlowcontrol () const
 
 void Serial::flush ()
 {
-  mutex::scoped_lock scoped_lock (mut);
+  ScopedReadLock(this->pimpl_);
+  ScopedWriteLock(this->pimpl_);
   pimpl_->flush ();
   read_cache_.clear ();
 }
 
 void Serial::flushInput ()
 {
+  ScopedReadLock(this->pimpl_);
   pimpl_->flushInput ();
 }
 
 void Serial::flushOutput ()
 {
-  mutex::scoped_lock scoped_lock (mut);
+  ScopedWriteLock(this->pimpl_);
   pimpl_->flushOutput ();
   read_cache_.clear ();
 }
@@ -322,4 +337,3 @@ bool Serial::getCD ()
 {
   return pimpl_->getCD ();
 }
-

From 2978386696bea6f5b33b39399417cb7d61650a57 Mon Sep 17 00:00:00 2001
From: William Woodall 
Date: Sat, 4 Feb 2012 21:14:22 -0600
Subject: [PATCH 58/72] Read/write seem to be working on linux, need to test on
 OS X.

---
 examples/serial_example.cc |   1 +
 include/serial/impl/unix.h |   4 +-
 include/serial/serial.h    | 187 ++++++++++++++------------
 src/impl/unix.cc           | 262 ++++++++++++++++++++-----------------
 src/serial.cc              | 168 +++++++++++++-----------
 5 files changed, 341 insertions(+), 281 deletions(-)

diff --git a/examples/serial_example.cc b/examples/serial_example.cc
index 60be39a..3ba886b 100644
--- a/examples/serial_example.cc
+++ b/examples/serial_example.cc
@@ -1,5 +1,6 @@
 #include 
 #include 
+#include 
 
 // OS Specific sleep
 #ifdef __WIN32__
diff --git a/include/serial/impl/unix.h b/include/serial/impl/unix.h
index b0d630b..98f0fec 100644
--- a/include/serial/impl/unix.h
+++ b/include/serial/impl/unix.h
@@ -75,7 +75,7 @@ public:
   available ();
 
   size_t
-  read (char* buf, size_t size = 1);
+  read (unsigned char* buf, size_t size = 1);
 
   size_t
   write (const string &data);
@@ -174,7 +174,7 @@ private:
   string port_;               // Path to the file descriptor
   int fd_;                    // The current file descriptor
 
-  bool isOpen_;
+  bool is_open_;
   bool xonxoff_;
   bool rtscts_;
 
diff --git a/include/serial/serial.h b/include/serial/serial.h
index 0311577..ee3ef39 100644
--- a/include/serial/serial.h
+++ b/include/serial/serial.h
@@ -88,41 +88,38 @@ typedef enum {
 class Serial {
 public:
   /*!
-  * Constructor, creates a SerialPortBoost object and opens the port.
-  *
-  * \param port A std::string containing the address of the serial port,
-  *        which would be something like 'COM1' on Windows and '/dev/ttyS0'
-  *        on Linux.
-  *
-  * \param baudrate An integer that represents the buadrate
-  *
-  * \param timeout A long that represents the time (in milliseconds) until a
-  * timeout on reads occur. Setting this to zero (0) will cause reading to
-  * be non-blocking, i.e. the available data will be returned immediately,
-  * but it will not block to wait for more. Setting this to a number less
-  * than zero (-1) will result in infinite blocking behaviour, i.e. the
-  * serial port will block until either size bytes have been read or an
-  * exception has occured.
-  *
-  * \param bytesize Size of each byte in the serial transmission of data,
-  * default is EIGHTBITS, possible values are: FIVEBITS, SIXBITS, SEVENBITS,
-  * EIGHTBITS
-  *
-  * \param parity Method of parity, default is PARITY_NONE, possible values
-  * are: PARITY_NONE, PARITY_ODD, PARITY_EVEN
-  *
-  * \param stopbits Number of stop bits used, default is STOPBITS_ONE,
-  * possible values are: STOPBITS_ONE, STOPBITS_ONE_POINT_FIVE, STOPBITS_TWO
-  *
-  * \param flowcontrol Type of flowcontrol used, default is
-  * FLOWCONTROL_NONE, possible values are: FLOWCONTROL_NONE,
-  * FLOWCONTROL_SOFTWARE, FLOWCONTROL_HARDWARE
-  *
-  * \param buffer_size The maximum size of the internal buffer, defaults
-  * to 256 bytes (2^8).
-  *
-  * \throw PortNotOpenedException
-  */
+   * Constructor, creates a SerialPortBoost object and opens the port.
+   *
+   * \param port A std::string containing the address of the serial port,
+   *        which would be something like 'COM1' on Windows and '/dev/ttyS0'
+   *        on Linux.
+   *
+   * \param baudrate An integer that represents the buadrate
+   *
+   * \param timeout A long that represents the time (in milliseconds) until a
+   * timeout on reads occur. Setting this to zero (0) will cause reading to
+   * be non-blocking, i.e. the available data will be returned immediately,
+   * but it will not block to wait for more. Setting this to a number less
+   * than zero (-1) will result in infinite blocking behaviour, i.e. the
+   * serial port will block until either size bytes have been read or an
+   * exception has occured.
+   *
+   * \param bytesize Size of each byte in the serial transmission of data,
+   * default is EIGHTBITS, possible values are: FIVEBITS, SIXBITS, SEVENBITS,
+   * EIGHTBITS
+   *
+   * \param parity Method of parity, default is PARITY_NONE, possible values
+   * are: PARITY_NONE, PARITY_ODD, PARITY_EVEN
+   *
+   * \param stopbits Number of stop bits used, default is STOPBITS_ONE,
+   * possible values are: STOPBITS_ONE, STOPBITS_ONE_POINT_FIVE, STOPBITS_TWO
+   *
+   * \param flowcontrol Type of flowcontrol used, default is
+   * FLOWCONTROL_NONE, possible values are: FLOWCONTROL_NONE,
+   * FLOWCONTROL_SOFTWARE, FLOWCONTROL_HARDWARE
+   *
+   * \throw PortNotOpenedException
+   */
   Serial (const std::string &port = "",
           unsigned long baudrate = 9600,
           long timeout = 0,
@@ -135,25 +132,25 @@ public:
   virtual ~Serial ();
 
   /*!
-  * Opens the serial port as long as the portname is set and the port isn't
-  * alreay open.
-  *
-  * If the port is provided to the constructor then an explicit call to open
-  * is not needed.
-  *
-  * \see Serial::Serial
-  *
-  * \throw std::invalid_argument
-  * \throw serial::SerialExecption
-  * \throw serial::IOException
-  */
+   * Opens the serial port as long as the portname is set and the port isn't
+   * alreay open.
+   *
+   * If the port is provided to the constructor then an explicit call to open
+   * is not needed.
+   *
+   * \see Serial::Serial
+   *
+   * \throw std::invalid_argument
+   * \throw serial::SerialExecption
+   * \throw serial::IOException
+   */
   void
   open ();
 
   /*! Gets the open status of the serial port.
-  *
-  * \return Returns true if the port is open, false otherwise.
-  */
+   *
+   * \return Returns true if the port is open, false otherwise.
+   */
   bool
   isOpen () const;
 
@@ -166,51 +163,67 @@ public:
   available();
 
   /*! Read a given amount of bytes from the serial port.
-  *
-  * If a timeout is set it may return less characters than requested. With
-  * no timeout it will block until the requested number of bytes have been
-  * read or until an exception occurs.
-  *
-  * \param size A size_t defining how many bytes to be read.
-  *
-  * \return A std::string containing the data read.
-  */
+   *
+   * If a timeout is set it may return less characters than requested. With
+   * no timeout it will block until the requested number of bytes have been
+   * read or until an exception occurs.
+   *
+   * \param size A size_t defining how many bytes to be read.
+   *
+   * \return A std::string containing the data read.
+   */
+  size_t
+  read (unsigned char *buffer, size_t size);
+  size_t
+  read (std::vector &buffer, size_t size = 1);
+  size_t
+  read (std::string &buffer, size_t size = 1);
   std::string
   read (size_t size = 1);
 
   /*! Reads in a line or until a given delimiter has been processed
-  *
-  * Reads from the serial port until a single line has been read.
-  *
-  * \param size A maximum length of a line defaults to size_t::max()
-  * \param eol A string to match against for the EOL.
-  *
-  * \return A std::string containing the line.
-  */
+   *
+   * Reads from the serial port until a single line has been read.
+   *
+   * \param size A maximum length of a line, defaults to 65536 (2^16)
+   * \param eol A string to match against for the EOL.
+   *
+   * \return A std::string containing the line.
+   */
+  size_t
+  readline (std::string &buffer,
+            size_t size = 65536,
+            std::string eol = "\n");
   std::string
-  readline(size_t size = std::numeric_limits::max(),
-           std::string eol  = "\n");
+  readline (size_t size = 65536,
+            std::string eol = "\n");
 
   /*! Reads in multiple lines until the serail port times out.
-  *
-  * This requires a timeout > 0 before it can be run. It will read until a
-  * timeout occurs and return a list of strings.
-  *
-  * \param eol A string to match against for the EOL.
-  *
-  * \return A vector containing the lines.
-  */
+   *
+   * This requires a timeout > 0 before it can be run. It will read until a
+   * timeout occurs and return a list of strings.
+   *
+   * \param size A maximum length of combined lines, defaults to 65536 (2^16)
+   * 
+   * \param eol A string to match against for the EOL.
+   *
+   * \return A vector containing the lines.
+   */
   std::vector
-  readlines(std::string eol = "\n");
+  readlines (size_t size = 65536, std::string eol = "\n");
 
   /*! Write a string to the serial port.
-  *
-  * \param data A const std::string reference containg the data to be written
-  * to the serial port.
-  *
-  * \return A size_t representing the number of bytes actually written to
-  * the serial port.
-  */
+   *
+   * \param data A const reference containg the data to be written
+   * to the serial port.
+   *
+   * \return A size_t representing the number of bytes actually written to
+   * the serial port.
+   */
+  size_t
+  write (const unsigned char *data, size_t size);
+  size_t
+  write (const std::vector &data);
   size_t
   write (const std::string &data);
 
@@ -410,6 +423,10 @@ private:
   class ScopedReadLock;
   class ScopedWriteLock;
 
+  // Read common function
+  size_t
+  read_ (unsigned char *buffer, size_t size);
+
 };
 
 class SerialExecption : public std::exception
diff --git a/src/impl/unix.cc b/src/impl/unix.cc
index 9a5c23b..4896d63 100644
--- a/src/impl/unix.cc
+++ b/src/impl/unix.cc
@@ -10,15 +10,20 @@
 #include 
 #include 
 #include 
-#include 
-#include 
-#include 
 #include 
 
 #if defined(__linux__)
 #include 
 #endif
 
+#include 
+#include 
+#include 
+#ifdef __MACH__
+#include 
+#include 
+#endif
+
 #include "serial/impl/unix.h"
 
 #ifndef TIOCINQ
@@ -41,7 +46,7 @@ Serial::SerialImpl::SerialImpl (const string &port, unsigned long baudrate,
                                 long timeout, bytesize_t bytesize,
                                 parity_t parity, stopbits_t stopbits,
                                 flowcontrol_t flowcontrol)
-: port_ (port), fd_ (-1), isOpen_ (false), xonxoff_ (true), rtscts_ (false),
+: port_ (port), fd_ (-1), is_open_ (false), xonxoff_ (true), rtscts_ (false),
   timeout_ (timeout), baudrate_ (baudrate), parity_ (parity),
   bytesize_ (bytesize), stopbits_ (stopbits), flowcontrol_ (flowcontrol)
 {
@@ -61,13 +66,13 @@ Serial::SerialImpl::~SerialImpl ()
 void
 Serial::SerialImpl::open ()
 {
-  if (port_.empty())
+  if (port_.empty ())
   {
-    throw invalid_argument ("bad port specified");
+    throw invalid_argument ("Empty port is invalid.");
   }
-  if (isOpen_ == true)
+  if (is_open_ == true)
   {
-    throw SerialExecption ("port already open");
+    throw SerialExecption ("Serial port already open.");
   }
 
   fd_ = ::open (port_.c_str(), O_RDWR | O_NOCTTY | O_NONBLOCK);
@@ -82,7 +87,7 @@ Serial::SerialImpl::open ()
         return;
       case ENFILE:
       case EMFILE:
-        throw IOException ("to many file handles open");
+        throw IOException ("Too many file handles open.");
         break;
       default:
         throw IOException (errno);
@@ -90,7 +95,7 @@ Serial::SerialImpl::open ()
   }
 
   reconfigurePort();
-  isOpen_ = true;
+  is_open_ = true;
 }
 
 void
@@ -99,7 +104,7 @@ Serial::SerialImpl::reconfigurePort ()
   if (fd_ == -1)
   {
     // Can only operate on a valid file descriptor
-    throw IOException ("invalid file descriptor");
+    throw IOException ("Invalid file descriptor, is the serial port open?");
   }
 
   struct termios options; // The options for the file descriptor
@@ -266,7 +271,8 @@ Serial::SerialImpl::reconfigurePort ()
   if (stopbits_ == STOPBITS_ONE)
       options.c_cflag &= (unsigned long) ~(CSTOPB);
   else if (stopbits_ == STOPBITS_ONE_POINT_FIVE)
-      options.c_cflag |=  (CSTOPB);  // XXX same as TWO.. there is no POSIX support for 1.5
+  // ONE POINT FIVE same as TWO.. there is no POSIX support for 1.5
+      options.c_cflag |=  (CSTOPB);
   else if (stopbits_ == STOPBITS_TWO)
       options.c_cflag |=  (CSTOPB);
   else
@@ -318,8 +324,12 @@ Serial::SerialImpl::reconfigurePort ()
 #error "OS Support seems wrong."
 #endif
 
-  options.c_cc[VMIN] = 1; // Minimum of 1 character in the buffer
-  options.c_cc[VTIME] = 0; // timeout on waiting for new data
+  // http://www.unixwiz.net/techtips/termios-vmin-vtime.html
+  // this basically sets the read call up to be a polling read, 
+  // but we are using select to ensure there is data available 
+  // to read before each call, so we should never needlessly poll
+  options.c_cc[VMIN] = 0;
+  options.c_cc[VTIME] = 0;
 
   // activate settings
   ::tcsetattr (fd_, TCSANOW, &options);
@@ -328,27 +338,27 @@ Serial::SerialImpl::reconfigurePort ()
 void
 Serial::SerialImpl::close ()
 {
-  if (isOpen_ == true)
+  if (is_open_ == true)
   {
     if (fd_ != -1)
     {
       ::close (fd_); // Ignoring the outcome
       fd_ = -1;
     }
-    isOpen_ = false;
+    is_open_ = false;
   }
 }
 
 bool
 Serial::SerialImpl::isOpen () const
 {
-  return isOpen_;
+  return is_open_;
 }
 
 size_t
 Serial::SerialImpl::available ()
 {
-  if (!isOpen_)
+  if (!is_open_)
   {
     return 0;
   }
@@ -364,113 +374,131 @@ Serial::SerialImpl::available ()
   }
 }
 
+inline void get_time_now(struct timespec &time) {
+# ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
+  clock_serv_t cclock;
+  mach_timespec_t mts;
+  host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
+  clock_get_time(cclock, &mts);
+  mach_port_deallocate(mach_task_self(), cclock);
+  time.tv_sec = mts.tv_sec;
+  time.tv_nsec = mts.tv_nsec;
+# else
+  clock_gettime(CLOCK_REALTIME, &time);
+# endif
+}
+
 size_t
-Serial::SerialImpl::read (char* buf, size_t size)
+Serial::SerialImpl::read (unsigned char* buf, size_t size)
 {
-  if (!isOpen_)
+  if (!is_open_)
   {
     throw PortNotOpenedException ("Serial::read");
   }
   fd_set readfds;
-  ssize_t bytes_read = 0;
-  int count = 0;
-  while (true)
+  size_t bytes_read = 0;
+  struct timeval timeout;
+  timeout.tv_sec =                    timeout_ / 1000;
+  timeout.tv_usec = static_cast (timeout_ % 1000) * 1000;
+  while (bytes_read < size)
   {
-    count++;
-    // printf("Counting: %u\n", count);
-    if (timeout_ != -1)
-    {
-      FD_ZERO (&readfds);
-      FD_SET (fd_, &readfds);
-      struct timeval timeout;
-      timeout.tv_sec =                    timeout_ / 1000;
-      timeout.tv_usec = static_cast (timeout_ % 1000) * 1000;
-      int r = select (fd_ + 1, &readfds, NULL, NULL, &timeout);
+    FD_ZERO (&readfds);
+    FD_SET (fd_, &readfds);
+    // On Linux the timeout struct is updated by select to contain the time 
+    // left on the timeout to make looping easier, but on other platforms this 
+    // does not occur.
+#if !defined(__linux__)
+    // Begin timing select
+    struct timespec start, end;
+    get_time_now(start);
+#endif
+    // Do the select
+    int r = select (fd_ + 1, &readfds, NULL, NULL, &timeout);
+#if !defined(__linux__)
+    // Calculate difference and update the structure
+    get_time_now(end);
+    // Calculate the time select took
+    struct timeval diff;
+    diff.tv_sec = end.tv_sec-start.tv_sec;
+    diff.tv_usec = (end.tv_nsec-start.tv_nsec)/1000;
+    // Update the timeout
+    if (timeout.tv_sec <= diff.tv_sec) {
+      timeout.tv_sec = 0;
+    } else {
+      timeout.tv_sec -= diff.tv_sec;
+    }
+    if (timeout.tv_usec <= diff.tv_usec) {
+      timeout.tv_usec = 0;
+    } else {
+      timeout.tv_usec -= diff.tv_usec;
+    }
+#endif
 
-      if (r == -1 && errno == EINTR)
+    // Figure out what happened by looking at select's response 'r'
+/** Error **/
+    if (r < 0) {
+      // Select was interrupted, try again
+      if (errno == EINTR) {
         continue;
-
-      if (r == -1)
-      {
-        throw IOException (errno);
       }
+      // Otherwise there was some error
+      throw IOException (errno);
     }
-
-    if (timeout_ == -1 || FD_ISSET (fd_, &readfds))
-    {
-      bytes_read = ::read (fd_, buf, size);
-      // read should always return some data as select reported it was
-      // ready to read when we get to this point.
-      if (bytes_read < 1)
-      {
-        // Disconnected devices, at least on Linux, show the
-        // behavior that they are always ready to read immediately
-        // but reading returns nothing.
-        throw SerialExecption ("device reports readiness to read but "
-                               "returned no data (device disconnected?)");
-      }
+/** Timeout **/
+    if (r == 0) {
       break;
     }
-    else
-    {
-      break;
+/** Something ready to read **/
+    if (r > 0) {
+      // Make sure our file descriptor is in the ready to read list
+      if (FD_ISSET (fd_, &readfds)) {
+        // This should be non-blocking returning only what is avaialble now
+        //  Then returning so that select can block again.
+        ssize_t bytes_read_now = ::read (fd_, buf, size-bytes_read);
+        // read should always return some data as select reported it was
+        // ready to read when we get to this point.
+        if (bytes_read_now < 1)
+        {
+          // Disconnected devices, at least on Linux, show the
+          // behavior that they are always ready to read immediately
+          // but reading returns nothing.
+          throw SerialExecption ("device reports readiness to read but "
+                                 "returned no data (device disconnected?)");
+        }
+        // Update bytes_read
+        bytes_read += static_cast (bytes_read_now);
+        // If bytes_read == size then we have read everything we need
+        if (bytes_read == size) {
+          break;
+        }
+        // If bytes_read < size then we have more to read
+        if (bytes_read < size) {
+          continue;
+        }
+        // If bytes_read > size then we have over read, which shouldn't happen
+        if (bytes_read > size) {
+          throw SerialExecption ("read over read, too many bytes where "
+                                 "read, this shouldn't happen, might be "
+                                 "a logical error!");
+        }
+      }
+      // This shouldn't happen, if r > 0 our fd has to be in the list!
+      throw IOException ("select reports ready to read, but our fd isn't"
+                         " in the list, this shouldn't happen!");
     }
   }
-  return static_cast (bytes_read);
+  return bytes_read;
 }
 
 size_t
 Serial::SerialImpl::write (const string &data)
 {
-  if (isOpen_ == false)
+  if (is_open_ == false)
   {
     throw PortNotOpenedException ("Serial::write");
   }
-  
-  fd_set writefds;
-  ssize_t bytes_written = 0;
-  while (true)
-  {
-    if (timeout_ != -1)
-    {
-      FD_ZERO (&writefds);
-      FD_SET (fd_, &writefds);
-      struct timeval timeout;
-      timeout.tv_sec =                    timeout_ / 1000;
-      timeout.tv_usec = static_cast (timeout_ % 1000) * 1000;
-      int r = select (fd_ + 1, NULL, &writefds, NULL, &timeout);
 
-      if (r == -1 && errno == EINTR)
-        continue;
-
-      if (r == -1)
-      {
-        throw IOException (errno);
-      }
-    }
-
-    if (timeout_ == -1 || FD_ISSET (fd_, &writefds))
-    {
-      bytes_written = ::write (fd_, data.c_str (), data.length ());
-      // read should always return some data as select reported it was
-      // ready to read when we get to this point.
-      if (bytes_written < 1)
-      {
-        // Disconnected devices, at least on Linux, show the
-        // behavior that they are always ready to read immediately
-        // but reading returns nothing.
-        throw SerialExecption ("device reports readiness to read but "
-                               "returned no data (device disconnected?)");
-      }
-      break;
-    }
-    else
-    {
-      break;
-    }
-  }
-
-  return static_cast (bytes_written);
+  return static_cast (::write (fd_, data.c_str (), data.length ()));
 }
 
 void
@@ -501,7 +529,7 @@ void
 Serial::SerialImpl::setBaudrate (unsigned long baudrate)
 {
   baudrate_ = baudrate;
-  if (isOpen_)
+  if (is_open_)
     reconfigurePort ();
 }
 
@@ -515,7 +543,7 @@ void
 Serial::SerialImpl::setBytesize (serial::bytesize_t bytesize)
 {
   bytesize_ = bytesize;
-  if (isOpen_)
+  if (is_open_)
     reconfigurePort ();
 }
 
@@ -529,7 +557,7 @@ void
 Serial::SerialImpl::setParity (serial::parity_t parity)
 {
   parity_ = parity;
-  if (isOpen_)
+  if (is_open_)
     reconfigurePort ();
 }
 
@@ -543,7 +571,7 @@ void
 Serial::SerialImpl::setStopbits (serial::stopbits_t stopbits)
 {
   stopbits_ = stopbits;
-  if (isOpen_)
+  if (is_open_)
     reconfigurePort ();
 }
 
@@ -557,7 +585,7 @@ void
 Serial::SerialImpl::setFlowcontrol (serial::flowcontrol_t flowcontrol)
 {
   flowcontrol_ = flowcontrol;
-  if (isOpen_)
+  if (is_open_)
     reconfigurePort ();
 }
 
@@ -570,7 +598,7 @@ Serial::SerialImpl::getFlowcontrol () const
 void
 Serial::SerialImpl::flush ()
 {
-  if (isOpen_ == false)
+  if (is_open_ == false)
   {
     throw PortNotOpenedException ("Serial::flush");
   }
@@ -580,7 +608,7 @@ Serial::SerialImpl::flush ()
 void
 Serial::SerialImpl::flushInput ()
 {
-  if (isOpen_ == false)
+  if (is_open_ == false)
   {
     throw PortNotOpenedException ("Serial::flushInput");
   }
@@ -590,7 +618,7 @@ Serial::SerialImpl::flushInput ()
 void
 Serial::SerialImpl::flushOutput ()
 {
-  if (isOpen_ == false)
+  if (is_open_ == false)
   {
     throw PortNotOpenedException ("Serial::flushOutput");
   }
@@ -600,7 +628,7 @@ Serial::SerialImpl::flushOutput ()
 void
 Serial::SerialImpl::sendBreak (int duration)
 {
-  if (isOpen_ == false)
+  if (is_open_ == false)
   {
     throw PortNotOpenedException ("Serial::sendBreak");
   }
@@ -610,7 +638,7 @@ Serial::SerialImpl::sendBreak (int duration)
 void
 Serial::SerialImpl::setBreak (bool level)
 {
-  if (isOpen_ == false)
+  if (is_open_ == false)
   {
     throw PortNotOpenedException ("Serial::setBreak");
   }
@@ -626,7 +654,7 @@ Serial::SerialImpl::setBreak (bool level)
 void
 Serial::SerialImpl::setRTS (bool level)
 {
-  if (isOpen_ == false)
+  if (is_open_ == false)
   {
     throw PortNotOpenedException ("Serial::setRTS");
   }
@@ -642,7 +670,7 @@ Serial::SerialImpl::setRTS (bool level)
 void
 Serial::SerialImpl::setDTR (bool level)
 {
-  if (isOpen_ == false)
+  if (is_open_ == false)
   {
     throw PortNotOpenedException ("Serial::setDTR");
   }
@@ -659,7 +687,7 @@ Serial::SerialImpl::setDTR (bool level)
 bool
 Serial::SerialImpl::getCTS ()
 {
-  if (isOpen_ == false)
+  if (is_open_ == false)
   {
     throw PortNotOpenedException ("Serial::getCTS");
   }
@@ -670,7 +698,7 @@ Serial::SerialImpl::getCTS ()
 bool
 Serial::SerialImpl::getDSR()
 {
-  if (isOpen_ == false)
+  if (is_open_ == false)
   {
     throw PortNotOpenedException ("Serial::getDSR");
   }
@@ -681,7 +709,7 @@ Serial::SerialImpl::getDSR()
 bool
 Serial::SerialImpl::getRI()
 {
-  if (isOpen_ == false)
+  if (is_open_ == false)
   {
     throw PortNotOpenedException ("Serial::getRI");
   }
@@ -692,7 +720,7 @@ Serial::SerialImpl::getRI()
 bool
 Serial::SerialImpl::getCD()
 {
-  if (isOpen_ == false)
+  if (is_open_ == false)
   {
     throw PortNotOpenedException ("Serial::getCD");
   }
diff --git a/src/serial.cc b/src/serial.cc
index b058675..462e000 100644
--- a/src/serial.cc
+++ b/src/serial.cc
@@ -9,7 +9,6 @@
 #endif
 
 using std::invalid_argument;
-using std::memset;
 using std::min;
 using std::numeric_limits;
 using std::vector;
@@ -86,98 +85,113 @@ Serial::available ()
   return pimpl_->available ();
 }
 
+size_t
+Serial::read_ (unsigned char *buffer, size_t size)
+{
+  return this->pimpl_->read (buffer, size);
+}
+
+size_t
+Serial::read (unsigned char *buffer, size_t size)
+{
+  ScopedReadLock (this->pimpl_);
+  return this->pimpl_->read (buffer, size);
+}
+
+size_t
+Serial::read (std::vector &buffer, size_t size)
+{
+  ScopedReadLock (this->pimpl_);
+  unsigned char *buffer_ = new unsigned char[size];
+  size_t bytes_read = this->pimpl_->read (buffer_, size);
+  buffer.insert (buffer.end (), buffer_, buffer_+bytes_read);
+  delete[] buffer_;
+  return bytes_read;
+}
+
+size_t
+Serial::read (std::string &buffer, size_t size)
+{
+  ScopedReadLock (this->pimpl_);
+  unsigned char *buffer_ = new unsigned char[size];
+  size_t bytes_read = this->pimpl_->read (buffer_, size);
+  buffer.append (reinterpret_cast(buffer_), bytes_read);
+  delete[] buffer_;
+  return bytes_read;
+}
+
 string
 Serial::read (size_t size)
 {
-  ScopedReadLock(this->pimpl_);
-  if (read_cache_.size() >= size)
-  {
-    // Don't need to do a new read.
-    string result = read_cache_.substr (0, size);
-    read_cache_ = read_cache_.substr (size, read_cache_.size ());
-    return result;
-  }
-  else
-  {
-    // Needs to read, loop until we have read enough or timeout
-    string result (read_cache_.substr (0, size));
-    read_cache_.clear ();
+  std::string buffer;
+  this->read (buffer, size);
+  return buffer;
+}
 
-    while (true)
-    {
-      char buf[256];
-      size_t chars_read = pimpl_->read (buf, 256);
-      if (chars_read > 0)
-      {
-        read_cache_.append(buf, chars_read);
-      }
-      else
-        break; // Timeout occured
-      
-      if (chars_read > size)
-      {
-        result.append (read_cache_.substr (0, size));
-        read_cache_ = read_cache_.substr (size, read_cache_.size ());
-        break;
-      }
-      else
-      {
-        result.append (read_cache_.substr (0, size));
-        read_cache_.clear ();
-        size -= chars_read;
-      }
+size_t
+Serial::readline (string &buffer, size_t size, string eol)
+{
+  ScopedReadLock (this->pimpl_);
+  size_t eol_len = eol.length();
+  unsigned char buffer_[size];
+  size_t read_so_far = 0;
+  while (true)
+  {
+    size_t bytes_read = this->read_ (buffer_+read_so_far, 1);
+    read_so_far += bytes_read;
+    if (bytes_read == 0) {
+      break; // Timeout occured on reading 1 byte
+    }
+    if (string(buffer_[read_so_far-eol_len], eol_len) == eol) {
+      break; // EOL found
+    }
+    if (read_so_far == size) {
+      break; // Reached the maximum read length
     }
-    return result;
   }
+  return read_so_far;
 }
 
 string
 Serial::readline (size_t size, string eol)
 {
-  size_t leneol = eol.length ();
-  string line = "";
-  while (true)
-  {
-    string c = read (1);
-    if (!c.empty ())
-    {
-      line.append (c);
-      if (line.length () > leneol &&
-          line.substr (line.length () - leneol, leneol) == eol)
-        break;
-      if (line.length () >= size)
-      {
-        break;
-      }
-    }
-    else
-      // Timeout
-      break;
-  }
-  return line;
+  std::string buffer;
+  this->readline (buffer, size, eol);
+  return buffer;
 }
 
 vector
-Serial::readlines(string eol)
+Serial::readlines (size_t size, string eol)
 {
-  if (pimpl_->getTimeout () < 0)
-  {
-    throw invalid_argument ("Error, must be set for readlines");
-  }
-  size_t leneol = eol.length ();
-  vector lines;
-  while (true)
-  {
-    string line = readline (numeric_limits::max (), eol);
-    if (!line.empty ())
-    {
-      lines.push_back (line);
-      if (line.substr (line.length () - leneol, leneol) == eol)
-        break;
+  ScopedReadLock (this->pimpl_);
+  std::vector lines;
+  size_t eol_len = eol.length();
+  unsigned char buffer_[size];
+  size_t read_so_far = 0;
+  size_t start_of_line = 0;
+  while (read_so_far < size) {
+    size_t bytes_read = this->read_ (buffer_+read_so_far, 1);
+    read_so_far += bytes_read;
+    if (bytes_read == 0) {
+      if (start_of_line != read_so_far) {
+        lines.push_back(
+          std::string(buffer_[start_of_line], read_so_far-start_of_line));
+      }
+      break; // Timeout occured on reading 1 byte
+    }
+    if (string(buffer_[read_so_far-eol_len], eol_len) == eol) {
+      // EOL found
+      lines.push_back(
+        std::string(buffer_[start_of_line], read_so_far-start_of_line));
+      start_of_line = read_so_far;
+    }
+    if (read_so_far == size) {
+      if (start_of_line != read_so_far) {
+        lines.push_back(
+          std::string(buffer_[start_of_line], read_so_far-start_of_line));
+      }
+      break; // Reached the maximum read length
     }
-    else
-      // Timeout
-      break;
   }
   return lines;
 }

From aa59c9517f1133d10dbf059b55cc1c9f00c70ed6 Mon Sep 17 00:00:00 2001
From: William Woodall 
Date: Sat, 4 Feb 2012 21:15:23 -0600
Subject: [PATCH 59/72] Some style changes

---
 include/serial/serial.h | 68 ++++++++++++++++++++---------------------
 src/impl/unix.cc        |  2 +-
 2 files changed, 35 insertions(+), 35 deletions(-)

diff --git a/include/serial/serial.h b/include/serial/serial.h
index 0311577..e6a4cda 100644
--- a/include/serial/serial.h
+++ b/include/serial/serial.h
@@ -49,37 +49,37 @@ namespace serial {
  * Enumeration defines the possible bytesizes for the serial port.
  */
 typedef enum {
-  FIVEBITS = 5,
-  SIXBITS = 6,
-  SEVENBITS = 7,
-  EIGHTBITS = 8
+  fivebits = 5,
+  sixbits = 6,
+  sevenbits = 7,
+  eightbits = 8
 } bytesize_t;
 
 /*!
  * Enumeration defines the possible parity types for the serial port.
  */
 typedef enum {
-  PARITY_NONE = 0,
-  PARITY_ODD = 1,
-  PARITY_EVEN = 2
+  parity_none = 0,
+  parity_odd = 1,
+  parity_even = 2
 } parity_t;
 
 /*!
  * Enumeration defines the possible stopbit types for the serial port.
  */
 typedef enum {
-  STOPBITS_ONE = 1,
-  STOPBITS_ONE_POINT_FIVE,
-  STOPBITS_TWO = 2
+  stopbits_one = 1,
+  stopbits_one_point_five,
+  stopbits_two = 2
 } stopbits_t;
 
 /*!
  * Enumeration defines the possible flowcontrol types for the serial port.
  */
 typedef enum {
-  FLOWCONTROL_NONE = 0,
-  FLOWCONTROL_SOFTWARE,
-  FLOWCONTROL_HARDWARE
+  flowcontrol_none = 0,
+  flowcontrol_software,
+  å
 } flowcontrol_t;
 
 /*!
@@ -105,18 +105,18 @@ public:
   * exception has occured.
   *
   * \param bytesize Size of each byte in the serial transmission of data,
-  * default is EIGHTBITS, possible values are: FIVEBITS, SIXBITS, SEVENBITS,
-  * EIGHTBITS
+  * default is eightbits, possible values are: fivebits, sixbits, sevenbits,
+  * eightbits
   *
-  * \param parity Method of parity, default is PARITY_NONE, possible values
-  * are: PARITY_NONE, PARITY_ODD, PARITY_EVEN
+  * \param parity Method of parity, default is parity_none, possible values
+  * are: parity_none, parity_odd, parity_even
   *
-  * \param stopbits Number of stop bits used, default is STOPBITS_ONE,
-  * possible values are: STOPBITS_ONE, STOPBITS_ONE_POINT_FIVE, STOPBITS_TWO
+  * \param stopbits Number of stop bits used, default is stopbits_one,
+  * possible values are: stopbits_one, stopbits_one_point_five, stopbits_two
   *
   * \param flowcontrol Type of flowcontrol used, default is
-  * FLOWCONTROL_NONE, possible values are: FLOWCONTROL_NONE,
-  * FLOWCONTROL_SOFTWARE, FLOWCONTROL_HARDWARE
+  * flowcontrol_none, possible values are: flowcontrol_none,
+  * flowcontrol_software, flowcontrol_hardware
   *
   * \param buffer_size The maximum size of the internal buffer, defaults
   * to 256 bytes (2^8).
@@ -126,10 +126,10 @@ public:
   Serial (const std::string &port = "",
           unsigned long baudrate = 9600,
           long timeout = 0,
-          bytesize_t bytesize = EIGHTBITS,
-          parity_t parity = PARITY_NONE,
-          stopbits_t stopbits = STOPBITS_ONE,
-          flowcontrol_t flowcontrol = FLOWCONTROL_NONE);
+          bytesize_t bytesize = eightbits,
+          parity_t parity = parity_none,
+          stopbits_t stopbits = stopbits_one,
+          flowcontrol_t flowcontrol = flowcontrol_none);
 
   /*! Destructor */
   virtual ~Serial ();
@@ -283,8 +283,8 @@ public:
   /*! Sets the bytesize for the serial port.
   *
   * \param bytesize Size of each byte in the serial transmission of data,
-  * default is EIGHTBITS, possible values are: FIVEBITS, SIXBITS, SEVENBITS,
-  * EIGHTBITS
+  * default is eightbits, possible values are: fivebits, sixbits, sevenbits,
+  * eightbits
   *
   * \throw InvalidConfigurationException
   */
@@ -302,8 +302,8 @@ public:
 
   /*! Sets the parity for the serial port.
   *
-  * \param parity Method of parity, default is PARITY_NONE, possible values
-  * are: PARITY_NONE, PARITY_ODD, PARITY_EVEN
+  * \param parity Method of parity, default is parity_none, possible values
+  * are: parity_none, parity_odd, parity_even
   *
   * \throw InvalidConfigurationException
   */
@@ -321,8 +321,8 @@ public:
 
   /*! Sets the stopbits for the serial port.
   *
-  * \param stopbits Number of stop bits used, default is STOPBITS_ONE,
-  * possible values are: STOPBITS_ONE, STOPBITS_ONE_POINT_FIVE, STOPBITS_TWO
+  * \param stopbits Number of stop bits used, default is stopbits_one,
+  * possible values are: stopbits_one, stopbits_one_point_five, stopbits_two
   *
   * \throw InvalidConfigurationException
   */
@@ -340,9 +340,9 @@ public:
 
   /*! Sets the flow control for the serial port.
   *
-  * \param flowcontrol Type of flowcontrol used, default is FLOWCONTROL_NONE,
-  * possible values are: FLOWCONTROL_NONE, FLOWCONTROL_SOFTWARE,
-  * FLOWCONTROL_HARDWARE
+  * \param flowcontrol Type of flowcontrol used, default is flowcontrol_none,
+  * possible values are: flowcontrol_none, flowcontrol_software,
+  * flowcontrol_hardware
   *
   * \throw InvalidConfigurationException
   */
diff --git a/src/impl/unix.cc b/src/impl/unix.cc
index 9a5c23b..edc2b48 100644
--- a/src/impl/unix.cc
+++ b/src/impl/unix.cc
@@ -16,7 +16,7 @@
 #include 
 
 #if defined(__linux__)
-#include 
+# include 
 #endif
 
 #include "serial/impl/unix.h"

From 34ce9596726301cd95710350864ffc8f244f0ecd Mon Sep 17 00:00:00 2001
From: William Woodall 
Date: Sat, 4 Feb 2012 21:18:26 -0600
Subject: [PATCH 60/72] =?UTF-8?q?wtf:=20'Users/william/devel/atrv=5Fws/ser?=
 =?UTF-8?q?ial/include/serial/serial.h:82:=20error:=20stray=20=E2=80=98\30?=
 =?UTF-8?q?3=E2=80=99=20in=20program',=20fixed...?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 include/serial/serial.h | 1 -
 1 file changed, 1 deletion(-)

diff --git a/include/serial/serial.h b/include/serial/serial.h
index 450dcc2..1f73fdf 100644
--- a/include/serial/serial.h
+++ b/include/serial/serial.h
@@ -79,7 +79,6 @@ typedef enum {
 typedef enum {
   flowcontrol_none = 0,
   flowcontrol_software,
-  å
 } flowcontrol_t;
 
 /*!

From 0eab7f1f38c01552e9b3b351b590616082c76807 Mon Sep 17 00:00:00 2001
From: William Woodall 
Date: Sat, 4 Feb 2012 21:20:21 -0600
Subject: [PATCH 61/72] fixing some stuff in unix.cc to match changes in enums

---
 src/impl/unix.cc | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)

diff --git a/src/impl/unix.cc b/src/impl/unix.cc
index 89cf1c4..cb84943 100644
--- a/src/impl/unix.cc
+++ b/src/impl/unix.cc
@@ -257,38 +257,38 @@ Serial::SerialImpl::reconfigurePort ()
 
   // setup char len
   options.c_cflag &= (unsigned long) ~CSIZE;
-  if (bytesize_ == EIGHTBITS)
+  if (bytesize_ == eightbits)
       options.c_cflag |= CS8;
-  else if (bytesize_ == SEVENBITS)
+  else if (bytesize_ == sevenbits)
       options.c_cflag |= CS7;
-  else if (bytesize_ == SIXBITS)
+  else if (bytesize_ == sixbits)
       options.c_cflag |= CS6;
-  else if (bytesize_ == FIVEBITS)
+  else if (bytesize_ == fivebits)
       options.c_cflag |= CS5;
   else
       throw invalid_argument ("invalid char len");
   // setup stopbits
-  if (stopbits_ == STOPBITS_ONE)
+  if (stopbits_ == stopbits_one)
       options.c_cflag &= (unsigned long) ~(CSTOPB);
-  else if (stopbits_ == STOPBITS_ONE_POINT_FIVE)
+  else if (stopbits_ == stopbits_one_point_five)
   // ONE POINT FIVE same as TWO.. there is no POSIX support for 1.5
       options.c_cflag |=  (CSTOPB);
-  else if (stopbits_ == STOPBITS_TWO)
+  else if (stopbits_ == stopbits_two)
       options.c_cflag |=  (CSTOPB);
   else
       throw invalid_argument ("invalid stop bit");
   // setup parity
   options.c_iflag &= (unsigned long) ~(INPCK|ISTRIP);
-  if (parity_ == PARITY_NONE)
+  if (parity_ == parity_none)
   {
     options.c_cflag &= (unsigned long) ~(PARENB|PARODD);
   }
-  else if (parity_ == PARITY_EVEN)
+  else if (parity_ == parity_even)
   {
     options.c_cflag &= (unsigned long) ~(PARODD);
     options.c_cflag |=  (PARENB);
   }
-  else if (parity_ == PARITY_ODD)
+  else if (parity_ == parity_odd)
   {
     options.c_cflag |=  (PARENB|PARODD);
   }

From 6853f8c05db6aa183e0dec674649f9508a937fc9 Mon Sep 17 00:00:00 2001
From: William Woodall 
Date: Sat, 4 Feb 2012 21:56:10 -0600
Subject: [PATCH 62/72] Fixing a reading buffer bug and expanding the example

---
 examples/serial_example.cc | 149 +++++++++++++++++++++++++++----------
 src/impl/unix.cc           |   3 +-
 2 files changed, 110 insertions(+), 42 deletions(-)

diff --git a/examples/serial_example.cc b/examples/serial_example.cc
index 3ba886b..93de9af 100644
--- a/examples/serial_example.cc
+++ b/examples/serial_example.cc
@@ -1,3 +1,21 @@
+/***
+ * This example expects the serial port has a loopback on it.
+ *
+ * Alternatively, you could use an Arduino:
+ * 
+ * 
+ *  setup() {
+ *    Serial.begin();
+ *  }
+ * 
+ *  loop() {
+ *    if (Serial.available()) {
+ *      Serial.write(Serial.read());
+ *    }
+ *  }
+ * 
+ */ + #include #include #include @@ -27,48 +45,97 @@ void my_sleep(unsigned long milliseconds) { int run(int argc, char **argv) { - if(argc < 3) { - cerr << "Usage: test_serial "; - cerr << " [test string]" << endl; - return 0; - } - // Argument 1 is the serial port - string port(argv[1]); - - // Argument 2 is the baudrate - unsigned long baud = 0; - sscanf(argv[2], "%lu", &baud); - - // port, baudrate, timeout in milliseconds - serial::Serial serial(port, baud, 1000); - - cout << "Is the serial port open?"; - if(serial.isOpen()) - cout << " Yes." << endl; - else - cout << " No." << endl; - - int count = 0; - string test_string; - if (argc == 4) { - test_string = argv[3]; - } else { - test_string = "Testing."; - } - while (true) { - size_t bytes_wrote = serial.write(test_string); - - string result = serial.read(test_string.length()); - - cout << "Iteration: " << count << ", Bytes written: "; - cout << bytes_wrote << ", Bytes read: "; - cout << result.length() << ", String read: " << result << endl; - - count += 1; - my_sleep(10); - } - + if(argc < 3) { + cerr << "Usage: test_serial "; + cerr << " [test string]" << endl; return 0; + } + // Argument 1 is the serial port + string port(argv[1]); + + // Argument 2 is the baudrate + unsigned long baud = 0; + sscanf(argv[2], "%lu", &baud); + + // port, baudrate, timeout in milliseconds + serial::Serial my_serial(port, baud, 1000); + + cout << "Is the serial port open?"; + if(my_serial.isOpen()) + cout << " Yes." << endl; + else + cout << " No." << endl; + + // Get the Test string + int count = 0; + string test_string; + if (argc == 4) { + test_string = argv[3]; + } else { + test_string = "Testing."; + } + + // Test the timeout, there should be 1 second between prints + cout << "Timeout == 1000ms, asking for 1 more byte than written." << endl; + while (count < 10) { + size_t bytes_wrote = my_serial.write(test_string); + + string result = my_serial.read(test_string.length()+1); + + cout << "Iteration: " << count << ", Bytes written: "; + cout << bytes_wrote << ", Bytes read: "; + cout << result.length() << ", String read: " << result << endl; + + count += 1; + } + + // Test the timeout at 250ms + my_serial.setTimeout(250); + count = 0; + cout << "Timeout == 250ms, asking for 1 more byte than written." << endl; + while (count < 10) { + size_t bytes_wrote = my_serial.write(test_string); + + string result = my_serial.read(test_string.length()+1); + + cout << "Iteration: " << count << ", Bytes written: "; + cout << bytes_wrote << ", Bytes read: "; + cout << result.length() << ", String read: " << result << endl; + + count += 1; + } + + // Test the timeout at 250ms, but asking exactly for what was written + count = 0; + cout << "Timeout == 250ms, asking for exactly what was written." << endl; + while (count < 10) { + size_t bytes_wrote = my_serial.write(test_string); + + string result = my_serial.read(test_string.length()); + + cout << "Iteration: " << count << ", Bytes written: "; + cout << bytes_wrote << ", Bytes read: "; + cout << result.length() << ", String read: " << result << endl; + + count += 1; + } + + // Test the timeout at 250ms, but asking for 1 less than what was written + count = 0; + cout << "Timeout == 250ms, asking for 1 less than was written." << endl; + while (count < 10) { + size_t bytes_wrote = my_serial.write(test_string); + + string result = my_serial.read(test_string.length()-1); + + cout << "Iteration: " << count << ", Bytes written: "; + cout << bytes_wrote << ", Bytes read: "; + cout << result.length() << ", String read: " << result << endl; + + count += 1; + } + + return 0; } int main(int argc, char **argv) { diff --git a/src/impl/unix.cc b/src/impl/unix.cc index cb84943..b8038a6 100644 --- a/src/impl/unix.cc +++ b/src/impl/unix.cc @@ -454,7 +454,8 @@ Serial::SerialImpl::read (unsigned char* buf, size_t size) if (FD_ISSET (fd_, &readfds)) { // This should be non-blocking returning only what is avaialble now // Then returning so that select can block again. - ssize_t bytes_read_now = ::read (fd_, buf, size-bytes_read); + ssize_t bytes_read_now = + ::read (fd_, buf+bytes_read, size-bytes_read); // read should always return some data as select reported it was // ready to read when we get to this point. if (bytes_read_now < 1) From 18f86ebf4bff425f04a82aefdfff37a4be30d500 Mon Sep 17 00:00:00 2001 From: William Woodall Date: Sun, 5 Feb 2012 22:31:06 -0600 Subject: [PATCH 63/72] Tracking down segfaults on Linux --- serial_ros.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/serial_ros.cmake b/serial_ros.cmake index 53f8817..2877acf 100644 --- a/serial_ros.cmake +++ b/serial_ros.cmake @@ -9,7 +9,7 @@ macro(build_serial) # Release : w/o debug symbols, w/ optimization # RelWithDebInfo : w/ debug symbols, w/ optimization # MinSizeRel : w/o debug symbols, w/ optimization, stripped binaries - set(ROS_BUILD_TYPE RelWithDebInfo) + set(ROS_BUILD_TYPE Debug) rosbuild_init() From 61c193f5d851fad9671e7f3cc79899791aa1e108 Mon Sep 17 00:00:00 2001 From: John Harrison Date: Mon, 6 Feb 2012 18:20:15 -0600 Subject: [PATCH 64/72] Configuring CMake to link against thread libraries This is used to link against pthread and friends that are used for threading and mutex's. --- serial.cmake | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/serial.cmake b/serial.cmake index 7e339aa..f705af5 100644 --- a/serial.cmake +++ b/serial.cmake @@ -32,6 +32,9 @@ macro(build_serial) set(BUILD_SHARED_LIBS OFF) ENDIF(NOT BUILD_SHARED_LIBS) + # Threading libraries added for mutexs + FIND_PACKAGE (Threads) + # Set the default path for built executables to the "bin" directory IF(NOT DEFINED(EXECUTABLE_OUTPUT_PATH)) set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin) @@ -60,6 +63,7 @@ macro(build_serial) # Compile the Library add_library(serial ${SERIAL_SRCS}) + target_link_libraries(serial ${CMAKE_THREAD_LIBS_INIT}) ## Build Examples From 9734f943cb5e787d88c42993d3970f760dfcab8c Mon Sep 17 00:00:00 2001 From: John Harrison Date: Mon, 6 Feb 2012 18:22:14 -0600 Subject: [PATCH 65/72] Fixing bad C++ constructors. I am not sure if this is related to the random crashes on Linux, but the wrong C++ std::string constructors were being called because of bad use of c-style array dereferencing. The correct C++ std::string constructors are being called now. --- include/serial/serial.h | 2 +- src/impl/unix.cc | 8 ++++---- src/serial.cc | 33 ++++++++++++++++++++------------- 3 files changed, 25 insertions(+), 18 deletions(-) diff --git a/include/serial/serial.h b/include/serial/serial.h index 1f73fdf..7d9fff9 100644 --- a/include/serial/serial.h +++ b/include/serial/serial.h @@ -78,7 +78,7 @@ typedef enum { */ typedef enum { flowcontrol_none = 0, - flowcontrol_software, + flowcontrol_software } flowcontrol_t; /*! diff --git a/src/impl/unix.cc b/src/impl/unix.cc index b8038a6..f9785e5 100644 --- a/src/impl/unix.cc +++ b/src/impl/unix.cc @@ -230,14 +230,14 @@ Serial::SerialImpl::reconfigurePort () // Linux Support #elif defined(__linux__) struct serial_struct ser; - ioctl(fd_, TIOCGSERIAL, &ser); + ioctl (fd_, TIOCGSERIAL, &ser); // set custom divisor ser.custom_divisor = ser.baud_base / baudrate_; // update flags ser.flags &= ~ASYNC_SPD_MASK; ser.flags |= ASYNC_SPD_CUST; - if (ioctl(fd_, TIOCSSERIAL, ser) < 0) + if (ioctl (fd_, TIOCSSERIAL, ser) < 0) { throw IOException (errno); } @@ -420,7 +420,7 @@ Serial::SerialImpl::read (unsigned char* buf, size_t size) // Calculate the time select took struct timeval diff; diff.tv_sec = end.tv_sec-start.tv_sec; - diff.tv_usec = (end.tv_nsec-start.tv_nsec)/1000; + diff.tv_usec = static_cast ((end.tv_nsec-start.tv_nsec)/1000); // Update the timeout if (timeout.tv_sec <= diff.tv_sec) { timeout.tv_sec = 0; @@ -703,7 +703,7 @@ Serial::SerialImpl::getDSR() { throw PortNotOpenedException ("Serial::getDSR"); } - int s = ioctl(fd_, TIOCMGET, 0); + int s = ioctl (fd_, TIOCMGET, 0); return (s & TIOCM_DSR) != 0; } diff --git a/src/serial.cc b/src/serial.cc index 462e000..b236baf 100644 --- a/src/serial.cc +++ b/src/serial.cc @@ -132,17 +132,19 @@ size_t Serial::readline (string &buffer, size_t size, string eol) { ScopedReadLock (this->pimpl_); - size_t eol_len = eol.length(); - unsigned char buffer_[size]; + size_t eol_len = eol.length (); + unsigned char *buffer_ = static_cast + (alloca (size * sizeof (unsigned char))); size_t read_so_far = 0; while (true) { - size_t bytes_read = this->read_ (buffer_+read_so_far, 1); + size_t bytes_read = this->read_ (buffer_ + read_so_far, 1); read_so_far += bytes_read; if (bytes_read == 0) { break; // Timeout occured on reading 1 byte } - if (string(buffer_[read_so_far-eol_len], eol_len) == eol) { + if (string (reinterpret_cast + (buffer_ + read_so_far - eol_len), eol_len) == eol) { break; // EOL found } if (read_so_far == size) { @@ -165,8 +167,9 @@ Serial::readlines (size_t size, string eol) { ScopedReadLock (this->pimpl_); std::vector lines; - size_t eol_len = eol.length(); - unsigned char buffer_[size]; + size_t eol_len = eol.length (); + unsigned char *buffer_ = static_cast + (alloca (size * sizeof (unsigned char))); size_t read_so_far = 0; size_t start_of_line = 0; while (read_so_far < size) { @@ -174,21 +177,25 @@ Serial::readlines (size_t size, string eol) read_so_far += bytes_read; if (bytes_read == 0) { if (start_of_line != read_so_far) { - lines.push_back( - std::string(buffer_[start_of_line], read_so_far-start_of_line)); + lines.push_back ( + string (reinterpret_cast (buffer_ + start_of_line), + read_so_far - start_of_line)); } break; // Timeout occured on reading 1 byte } - if (string(buffer_[read_so_far-eol_len], eol_len) == eol) { + if (string (reinterpret_cast + (buffer_ + read_so_far - eol_len), eol_len) == eol) { // EOL found lines.push_back( - std::string(buffer_[start_of_line], read_so_far-start_of_line)); + string(reinterpret_cast (buffer_ + start_of_line), + read_so_far - start_of_line)); start_of_line = read_so_far; } if (read_so_far == size) { if (start_of_line != read_so_far) { lines.push_back( - std::string(buffer_[start_of_line], read_so_far-start_of_line)); + string(reinterpret_cast (buffer_ + start_of_line), + read_so_far - start_of_line)); } break; // Reached the maximum read length } @@ -208,10 +215,10 @@ Serial::setPort (const string &port) { ScopedReadLock(this->pimpl_); ScopedWriteLock(this->pimpl_); - bool was_open = pimpl_->isOpen(); + bool was_open = pimpl_->isOpen (); if (was_open) close(); pimpl_->setPort (port); - if (was_open) open(); + if (was_open) open (); } string From 7d35eea4f77642e2d1be337f662a15ed339b363c Mon Sep 17 00:00:00 2001 From: John Harrison Date: Mon, 6 Feb 2012 18:27:39 -0600 Subject: [PATCH 66/72] Enabling warnings! They catch things, like Pokemon MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit And bad casts and bad constructors and and … Ya, Warnings good! --- serial.cmake | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/serial.cmake b/serial.cmake index f705af5..ade12bd 100644 --- a/serial.cmake +++ b/serial.cmake @@ -10,6 +10,18 @@ macro(build_serial) project(Serial) ## Configurations + # Enable warnings + # Assuming unix means a gcc style compiler, eg. g++ or clang++. + IF(UNIX) + set(CMAKE_CXX_FLAGS "-ferror-limit=5 -Wall -Weffc++ -pedantic -pedantic-errors -Wextra -Wall -Waggregate-return -Wcast-align -Wcast-qual -Wchar-subscripts -Wcomment -Wconversion -Wdisabled-optimization -Wfloat-equal -Wformat -Wformat=2 -Wformat-nonliteral -Wformat-security -Wformat-y2k -Wimplicit -Wimport -Winit-self -Winline -Winvalid-pch -Wlong-long -Wmissing-braces -Wmissing-field-initializers -Wmissing-format-attribute -Wmissing-include-dirs -Wmissing-noreturn -Wpacked -Wparentheses -Wpointer-arith -Wredundant-decls -Wreturn-type -Wsequence-point -Wshadow -Wsign-compare -Wstack-protector -Wstrict-aliasing -Wstrict-aliasing=2 -Wswitch -Wswitch-default -Wswitch-enum -Wtrigraphs -Wuninitialized -Wunknown-pragmas -Wunreachable-code -Wunused -Wunused-function -Wunused-label -Wunused-parameter -Wunused-value -Wunused-variable -Wvariadic-macros -Wvolatile-register-var -Wwrite-strings") + ELSEIF(WIN32) + # Force to always compile with W4 + if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]") + string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") + else() + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4") + endif() + endif(UNIX) # Use clang if available IF(EXISTS /usr/bin/clang) @@ -17,7 +29,6 @@ macro(build_serial) set(CMAKE_OSX_DEPLOYMENT_TARGET "") set(SERIAL_BUILD_WARNINGS TRUE) IF(SERIAL_BUILD_WARNINGS) - set(CMAKE_CXX_FLAGS "-ferror-limit=5 -Wall -Weffc++ -pedantic -pedantic-errors -Wextra -Wall -Waggregate-return -Wcast-align -Wcast-qual -Wchar-subscripts -Wcomment -Wconversion -Wdisabled-optimization -Wfloat-equal -Wformat -Wformat=2 -Wformat-nonliteral -Wformat-security -Wformat-y2k -Wimplicit -Wimport -Winit-self -Winline -Winvalid-pch -Wlong-long -Wmissing-braces -Wmissing-field-initializers -Wmissing-format-attribute -Wmissing-include-dirs -Wmissing-noreturn -Wpacked -Wparentheses -Wpointer-arith -Wredundant-decls -Wreturn-type -Wsequence-point -Wshadow -Wsign-compare -Wstack-protector -Wstrict-aliasing -Wstrict-aliasing=2 -Wswitch -Wswitch-default -Wswitch-enum -Wtrigraphs -Wuninitialized -Wunknown-pragmas -Wunreachable-code -Wunused -Wunused-function -Wunused-label -Wunused-parameter -Wunused-value -Wunused-variable -Wvariadic-macros -Wvolatile-register-var -Wwrite-strings") ELSE(SERIAL_BUILD_WARNINGS) set(CMAKE_CXX_FLAGS "-ferror-limit=5") ENDIF(SERIAL_BUILD_WARNINGS) From 6871cf7d56598ebb6464d211e5da59848cb223fa Mon Sep 17 00:00:00 2001 From: John Harrison Date: Mon, 6 Feb 2012 18:29:06 -0600 Subject: [PATCH 67/72] Removing a Clang specific compiler warning. --- serial.cmake | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/serial.cmake b/serial.cmake index ade12bd..0b6e5f4 100644 --- a/serial.cmake +++ b/serial.cmake @@ -13,7 +13,7 @@ macro(build_serial) # Enable warnings # Assuming unix means a gcc style compiler, eg. g++ or clang++. IF(UNIX) - set(CMAKE_CXX_FLAGS "-ferror-limit=5 -Wall -Weffc++ -pedantic -pedantic-errors -Wextra -Wall -Waggregate-return -Wcast-align -Wcast-qual -Wchar-subscripts -Wcomment -Wconversion -Wdisabled-optimization -Wfloat-equal -Wformat -Wformat=2 -Wformat-nonliteral -Wformat-security -Wformat-y2k -Wimplicit -Wimport -Winit-self -Winline -Winvalid-pch -Wlong-long -Wmissing-braces -Wmissing-field-initializers -Wmissing-format-attribute -Wmissing-include-dirs -Wmissing-noreturn -Wpacked -Wparentheses -Wpointer-arith -Wredundant-decls -Wreturn-type -Wsequence-point -Wshadow -Wsign-compare -Wstack-protector -Wstrict-aliasing -Wstrict-aliasing=2 -Wswitch -Wswitch-default -Wswitch-enum -Wtrigraphs -Wuninitialized -Wunknown-pragmas -Wunreachable-code -Wunused -Wunused-function -Wunused-label -Wunused-parameter -Wunused-value -Wunused-variable -Wvariadic-macros -Wvolatile-register-var -Wwrite-strings") + set(CMAKE_CXX_FLAGS "-Wall -Weffc++ -pedantic -pedantic-errors -Wextra -Wall -Waggregate-return -Wcast-align -Wcast-qual -Wchar-subscripts -Wcomment -Wconversion -Wdisabled-optimization -Wfloat-equal -Wformat -Wformat=2 -Wformat-nonliteral -Wformat-security -Wformat-y2k -Wimplicit -Wimport -Winit-self -Winline -Winvalid-pch -Wlong-long -Wmissing-braces -Wmissing-field-initializers -Wmissing-format-attribute -Wmissing-include-dirs -Wmissing-noreturn -Wpacked -Wparentheses -Wpointer-arith -Wredundant-decls -Wreturn-type -Wsequence-point -Wshadow -Wsign-compare -Wstack-protector -Wstrict-aliasing -Wstrict-aliasing=2 -Wswitch -Wswitch-default -Wswitch-enum -Wtrigraphs -Wuninitialized -Wunknown-pragmas -Wunreachable-code -Wunused -Wunused-function -Wunused-label -Wunused-parameter -Wunused-value -Wunused-variable -Wvariadic-macros -Wvolatile-register-var -Wwrite-strings") ELSEIF(WIN32) # Force to always compile with W4 if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]") @@ -28,10 +28,6 @@ macro(build_serial) set(CMAKE_CXX_COMPILER /usr/bin/clang++) set(CMAKE_OSX_DEPLOYMENT_TARGET "") set(SERIAL_BUILD_WARNINGS TRUE) - IF(SERIAL_BUILD_WARNINGS) - ELSE(SERIAL_BUILD_WARNINGS) - set(CMAKE_CXX_FLAGS "-ferror-limit=5") - ENDIF(SERIAL_BUILD_WARNINGS) set(CMAKE_BUILD_TYPE Debug) ENDIF(EXISTS /usr/bin/clang) From 6138aceedff025364ec0425b092b1121f11b6c1c Mon Sep 17 00:00:00 2001 From: John Harrison Date: Mon, 6 Feb 2012 18:29:06 -0600 Subject: [PATCH 68/72] Removing a Clang specific compiler warning. --- serial.cmake | 6 +----- src/serial.cc | 1 + 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/serial.cmake b/serial.cmake index ade12bd..0b6e5f4 100644 --- a/serial.cmake +++ b/serial.cmake @@ -13,7 +13,7 @@ macro(build_serial) # Enable warnings # Assuming unix means a gcc style compiler, eg. g++ or clang++. IF(UNIX) - set(CMAKE_CXX_FLAGS "-ferror-limit=5 -Wall -Weffc++ -pedantic -pedantic-errors -Wextra -Wall -Waggregate-return -Wcast-align -Wcast-qual -Wchar-subscripts -Wcomment -Wconversion -Wdisabled-optimization -Wfloat-equal -Wformat -Wformat=2 -Wformat-nonliteral -Wformat-security -Wformat-y2k -Wimplicit -Wimport -Winit-self -Winline -Winvalid-pch -Wlong-long -Wmissing-braces -Wmissing-field-initializers -Wmissing-format-attribute -Wmissing-include-dirs -Wmissing-noreturn -Wpacked -Wparentheses -Wpointer-arith -Wredundant-decls -Wreturn-type -Wsequence-point -Wshadow -Wsign-compare -Wstack-protector -Wstrict-aliasing -Wstrict-aliasing=2 -Wswitch -Wswitch-default -Wswitch-enum -Wtrigraphs -Wuninitialized -Wunknown-pragmas -Wunreachable-code -Wunused -Wunused-function -Wunused-label -Wunused-parameter -Wunused-value -Wunused-variable -Wvariadic-macros -Wvolatile-register-var -Wwrite-strings") + set(CMAKE_CXX_FLAGS "-Wall -Weffc++ -pedantic -pedantic-errors -Wextra -Wall -Waggregate-return -Wcast-align -Wcast-qual -Wchar-subscripts -Wcomment -Wconversion -Wdisabled-optimization -Wfloat-equal -Wformat -Wformat=2 -Wformat-nonliteral -Wformat-security -Wformat-y2k -Wimplicit -Wimport -Winit-self -Winline -Winvalid-pch -Wlong-long -Wmissing-braces -Wmissing-field-initializers -Wmissing-format-attribute -Wmissing-include-dirs -Wmissing-noreturn -Wpacked -Wparentheses -Wpointer-arith -Wredundant-decls -Wreturn-type -Wsequence-point -Wshadow -Wsign-compare -Wstack-protector -Wstrict-aliasing -Wstrict-aliasing=2 -Wswitch -Wswitch-default -Wswitch-enum -Wtrigraphs -Wuninitialized -Wunknown-pragmas -Wunreachable-code -Wunused -Wunused-function -Wunused-label -Wunused-parameter -Wunused-value -Wunused-variable -Wvariadic-macros -Wvolatile-register-var -Wwrite-strings") ELSEIF(WIN32) # Force to always compile with W4 if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]") @@ -28,10 +28,6 @@ macro(build_serial) set(CMAKE_CXX_COMPILER /usr/bin/clang++) set(CMAKE_OSX_DEPLOYMENT_TARGET "") set(SERIAL_BUILD_WARNINGS TRUE) - IF(SERIAL_BUILD_WARNINGS) - ELSE(SERIAL_BUILD_WARNINGS) - set(CMAKE_CXX_FLAGS "-ferror-limit=5") - ENDIF(SERIAL_BUILD_WARNINGS) set(CMAKE_BUILD_TYPE Debug) ENDIF(EXISTS /usr/bin/clang) diff --git a/src/serial.cc b/src/serial.cc index b236baf..b817a77 100644 --- a/src/serial.cc +++ b/src/serial.cc @@ -1,4 +1,5 @@ /* Copyright 2012 William Woodall and John Harrison */ +#include #include "serial/serial.h" From 68fb40e057dfcc4d215ef2ad614d769e2b24769d Mon Sep 17 00:00:00 2001 From: John Harrison Date: Mon, 6 Feb 2012 22:44:30 -0600 Subject: [PATCH 69/72] Adding my basic tests --- tests/serial_tests.cc | 94 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 74 insertions(+), 20 deletions(-) diff --git a/tests/serial_tests.cc b/tests/serial_tests.cc index 6a5a0c5..a416f49 100644 --- a/tests/serial_tests.cc +++ b/tests/serial_tests.cc @@ -3,39 +3,57 @@ * * Alternatively you could use an Arduino: - void setup() - { - Serial.begin(115200); - } +void setup() +{ + Serial.begin(115200); +} - void loop() - { - while (Serial.available() > 0) { - Serial.write(Serial.read()); - } +void loop() +{ + while (Serial.available() > 0) { + Serial.write(Serial.read()); } +} - */ - -#define SERIAL_PORT_NAME "/dev/tty.usbserial" +*/ +#include #include "gtest/gtest.h" #include -// OMG this is so nasty... -#define private public -#define protected public +// Use FRIEND_TEST... its not as nasty, thats what friends are for +// // OMG this is so nasty... +// #define private public +// #define protected public #include "serial/serial.h" + +#ifdef __linux__ +#include +#else +#include +#endif + using namespace serial; +using std::string; + namespace { class SerialTests : public ::testing::Test { protected: virtual void SetUp() { - port1 = new Serial(SERIAL_PORT_NAME, 115200, 250); + if (openpty(&master_fd, &slave_fd, name, NULL, NULL) == -1) { + perror("openpty"); + exit(127); + } + + ASSERT_TRUE(master_fd > 0); + ASSERT_TRUE(slave_fd > 0); + ASSERT_TRUE(string(name).length() > 0); + + port1 = new Serial(string(name), 115200, 250); } virtual void TearDown() { @@ -44,12 +62,48 @@ protected: } Serial * port1; - + int master_fd; + int slave_fd; + char name[100]; }; -// TEST_F(SerialTests, throwsOnInvalidPort) { -// -// } +TEST_F(SerialTests, readWorks) { + write(master_fd, "abc\n", 4); + string r = port1->read(4); + EXPECT_EQ(r, string("abc\n")); +} + +TEST_F(SerialTests, writeWorks) { + char buf[5] = ""; + port1->write("abc\n"); + read(master_fd, buf, 4); + EXPECT_EQ(string(buf, 4), string("abc\n")); +} + +TEST_F(SerialTests, timeoutWorks) { + // Timeout a read, returns an empty string + string empty = port1->read(); + EXPECT_EQ(empty, string("")); + + // Ensure that writing/reading still works after a timeout. + write(master_fd, "abc\n", 4); + string r = port1->read(4); + EXPECT_EQ(r, string("abc\n")); +} + +TEST_F(SerialTests, partialRead) { + // Write some data, but request more than was written. + write(master_fd, "abc\n", 4); + + // Should timeout, but return what was in the buffer. + string empty = port1->read(10); + EXPECT_EQ(empty, string("abc\n")); + + // Ensure that writing/reading still works after a timeout. + write(master_fd, "abc\n", 4); + string r = port1->read(4); + EXPECT_EQ(r, string("abc\n")); +} } // namespace From c3fb62a7d0d807392d7375fa646aa143a5fb3892 Mon Sep 17 00:00:00 2001 From: John Harrison Date: Mon, 6 Feb 2012 22:49:36 -0600 Subject: [PATCH 70/72] Missing a linked library on linux --- serial.cmake | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/serial.cmake b/serial.cmake index 0b6e5f4..ed63438 100644 --- a/serial.cmake +++ b/serial.cmake @@ -66,11 +66,17 @@ macro(build_serial) # Add default header files set(SERIAL_HEADERS include/serial/serial.h) + + set(OTHER_LIBS "") + if(UNIX) + set(OTHER_LIBS util) + endif(UNIX) + ## Build the Serial Library # Compile the Library add_library(serial ${SERIAL_SRCS}) - target_link_libraries(serial ${CMAKE_THREAD_LIBS_INIT}) + target_link_libraries(serial ${CMAKE_THREAD_LIBS_INIT} ${OTHER_LIBS}) ## Build Examples From a8259fcda21620a5ef4a2c976c56f513f8c3a51d Mon Sep 17 00:00:00 2001 From: William Woodall Date: Tue, 7 Feb 2012 14:11:17 -0600 Subject: [PATCH 71/72] Changing the throw to add file and line number --- include/serial/serial.h | 23 +++++++++++++++++------ src/impl/unix.cc | 28 ++++++++++++++-------------- src/impl/windows.cc | 1 - 3 files changed, 31 insertions(+), 21 deletions(-) diff --git a/include/serial/serial.h b/include/serial/serial.h index 7d9fff9..c1a9269 100644 --- a/include/serial/serial.h +++ b/include/serial/serial.h @@ -43,6 +43,8 @@ #include #include +#define THROW(exceptionClass, message) throw exceptionClass(__FILE__, __LINE__, (message) ) + namespace serial { /*! @@ -447,13 +449,16 @@ public: class IOException : public std::exception { + std::string file_; + int line_; const char* e_what_; int errno_; public: - explicit IOException (int errnum) - : e_what_ (strerror (errnum)), errno_(errnum) {} - explicit IOException (const char * description) - : e_what_ (description), errno_(0) {} + explicit IOException (std::string file, int line, int errnum) + : file_(file), line_(line), e_what_ (strerror (errnum)), errno_(errnum) {} + explicit IOException (std::string file, int line, const char * description) + : file_(file), line_(line), e_what_ (description), errno_(0) {} + virtual ~IOException() throw() {} int getErrorNumber () { return errno_; } @@ -461,9 +466,10 @@ public: { std::stringstream ss; if (errno_ == 0) - ss << "IO Exception " << e_what_ << " failed."; + ss << "IO Exception: " << e_what_; else - ss << "IO Exception (" << errno_ << "): " << e_what_ << " failed."; + ss << "IO Exception (" << errno_ << "): " << e_what_; + ss << ", file " << file_ << ", line " << line_ << "."; return ss.str ().c_str (); } }; @@ -482,6 +488,11 @@ public: } }; +class SerialExceptionBase : public std::exception +{ + +}; + } // namespace serial #endif diff --git a/src/impl/unix.cc b/src/impl/unix.cc index f9785e5..a98128b 100644 --- a/src/impl/unix.cc +++ b/src/impl/unix.cc @@ -87,10 +87,10 @@ Serial::SerialImpl::open () return; case ENFILE: case EMFILE: - throw IOException ("Too many file handles open."); + THROW (IOException, "Too many file handles open."); break; default: - throw IOException (errno); + THROW (IOException, errno); } } @@ -104,14 +104,14 @@ Serial::SerialImpl::reconfigurePort () if (fd_ == -1) { // Can only operate on a valid file descriptor - throw IOException ("Invalid file descriptor, is the serial port open?"); + THROW (IOException, "Invalid file descriptor, is the serial port open?"); } struct termios options; // The options for the file descriptor if (tcgetattr(fd_, &options) == -1) { - throw IOException ("::tcgetattr"); + THROW (IOException, "::tcgetattr"); } // set up raw mode / no echo / binary @@ -225,7 +225,7 @@ Serial::SerialImpl::reconfigurePort () int new_baud = static_cast (baudrate_); if (ioctl (fd_, IOSSIOSPEED, &new_baud, 1) < 0) { - throw IOException (errno); + THROW (IOException, errno); } // Linux Support #elif defined(__linux__) @@ -239,7 +239,7 @@ Serial::SerialImpl::reconfigurePort () if (ioctl (fd_, TIOCSSERIAL, ser) < 0) { - throw IOException (errno); + THROW (IOException, errno); } #else throw invalid_argument ("OS does not currently support custom bauds"); @@ -370,7 +370,7 @@ Serial::SerialImpl::available () } else { - throw IOException (errno); + THROW (IOException, errno); } } @@ -442,7 +442,7 @@ Serial::SerialImpl::read (unsigned char* buf, size_t size) continue; } // Otherwise there was some error - throw IOException (errno); + THROW (IOException, errno); } /** Timeout **/ if (r == 0) { @@ -484,8 +484,8 @@ Serial::SerialImpl::read (unsigned char* buf, size_t size) } } // This shouldn't happen, if r > 0 our fd has to be in the list! - throw IOException ("select reports ready to read, but our fd isn't" - " in the list, this shouldn't happen!"); + THROW (IOException, "select reports ready to read, but our fd isn't" + " in the list, this shouldn't happen!"); } } return bytes_read; @@ -733,7 +733,7 @@ void Serial::SerialImpl::readLock() { int result = pthread_mutex_lock(&this->read_mutex); if (result) { - throw (IOException (result)); + THROW (IOException, result); } } @@ -741,7 +741,7 @@ void Serial::SerialImpl::readUnlock() { int result = pthread_mutex_unlock(&this->read_mutex); if (result) { - throw (IOException (result)); + THROW (IOException, result); } } @@ -749,7 +749,7 @@ void Serial::SerialImpl::writeLock() { int result = pthread_mutex_lock(&this->write_mutex); if (result) { - throw (IOException (result)); + THROW (IOException, result); } } @@ -757,6 +757,6 @@ void Serial::SerialImpl::writeUnlock() { int result = pthread_mutex_unlock(&this->write_mutex); if (result) { - throw (IOException (result)); + THROW (IOException, result); } } diff --git a/src/impl/windows.cc b/src/impl/windows.cc index 48030a9..5e82227 100644 --- a/src/impl/windows.cc +++ b/src/impl/windows.cc @@ -346,7 +346,6 @@ Serial::SerialImpl::read (char* buf, size_t size) while (true) { count++; - // printf("Counting: %u\n", count); if (timeout_ != -1) { FD_ZERO (&readfds); From 81fc77dd47cd528cc852a42daaf41c4ed72b96f1 Mon Sep 17 00:00:00 2001 From: William Woodall Date: Mon, 2 Apr 2012 21:20:45 -0500 Subject: [PATCH 72/72] Fixed some warnings from clang --- src/impl/unix.cc | 1 - src/serial.cc | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/src/impl/unix.cc b/src/impl/unix.cc index a98128b..e87125d 100644 --- a/src/impl/unix.cc +++ b/src/impl/unix.cc @@ -88,7 +88,6 @@ Serial::SerialImpl::open () case ENFILE: case EMFILE: THROW (IOException, "Too many file handles open."); - break; default: THROW (IOException, errno); } diff --git a/src/serial.cc b/src/serial.cc index b817a77..2133b06 100644 --- a/src/serial.cc +++ b/src/serial.cc @@ -152,6 +152,7 @@ Serial::readline (string &buffer, size_t size, string eol) break; // Reached the maximum read length } } + buffer.append(reinterpret_cast (buffer_), read_so_far); return read_so_far; }