Commit 3b913101 authored by John Harrison's avatar John Harrison
Browse files

Look ma, no pointers! (directly controlled, also now there are no memory leaks)

parent 1ca16c47
......@@ -46,6 +46,7 @@
#include <boost/asio/serial_port.hpp>
#include <boost/bind.hpp>
#include <boost/thread.hpp>
#include <boost/scoped_ptr.hpp>
// A macro to disallow the copy constructor and operator= functions
// This should be used in the private: declarations for a class
......@@ -54,12 +55,24 @@
void operator=(const TypeName&)
// DEFINES
#ifndef DEFAULT_BAUDRATE
#define DEFAULT_BAUDRATE 9600
#endif
#ifndef DEFAULT_TIMEOUT
#define DEFAULT_TIMEOUT 0.0
#endif
#ifndef DEFAULT_BYTESIZE
#define DEFAULT_BYTESIZE EIGHTBITS
#endif
#ifndef DEFAULT_PARITY
#define DEFAULT_PARITY NONE
#endif
#ifndef DEFAULT_STOPBITS
#define DEFAULT_STOPBITS STOPBITS_ONE
#endif
#ifndef DEFAULT_FLOWCONTROL
#define DEFAULT_FLOWCONTROL FLOWCONTROL_NONE
#endif
namespace serial {
......@@ -184,13 +197,13 @@ public:
*
* @return A boolean value that represents the current logic level of the CTS line.
*/
const bool getCTS();
const bool getCTS() const;
/** Gets the status of the DSR line.
*
* @return A boolean value that represents the current logic level of the DSR line.
*/
const bool getDSR();
const bool getDSR() const;
/** Sets the timeout for reads in seconds.
*
......@@ -212,7 +225,7 @@ public:
* 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.
*/
const long getTimeoutMilliseconds();
const long getTimeoutMilliseconds() const;
/** Sets the baudrate for the serial port.
*
......@@ -224,7 +237,7 @@ public:
*
* @return An integer that sets the baud rate for the serial port.
*/
const int getBaudrate();
const int getBaudrate() const;
/** Sets the bytesize for the serial port.
*
......@@ -244,7 +257,7 @@ public:
*
* @throw InvalidBytesizeException
*/
const bytesize_t getBytesize();
const bytesize_t getBytesize() const;
/** Sets the parity for the serial port.
*
......@@ -262,7 +275,7 @@ public:
*
* @throw InvalidParityException
*/
const parity_t getParity();
const parity_t getParity() const;
/** Sets the stopbits for the serial port.
*
......@@ -280,7 +293,7 @@ public:
*
* @throw InvalidStopbitsException
*/
const stopbits_t getStopbits();
const stopbits_t getStopbits() const;
/** Sets the flow control for the serial port.
*
......@@ -298,7 +311,7 @@ public:
*
* @throw InvalidFlowcontrolException
*/
const flowcontrol_t getFlowcontrol();
const flowcontrol_t getFlowcontrol() const;
private:
DISALLOW_COPY_AND_ASSIGN(Serial);
void init();
......@@ -309,13 +322,13 @@ private:
boost::asio::io_service::work work;
boost::asio::serial_port * serial_port;
boost::scoped_ptr<boost::asio::serial_port> 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::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;
......
......@@ -143,7 +143,7 @@ Serial::Serial(std::string port,
void Serial::init() {
// Boost asio variables
this->serial_port = NULL;
this->serial_port.reset();
// Serial Port settings
this->port = "";
......@@ -169,7 +169,7 @@ void Serial::open() {
// Try to open the serial port
try {
this->serial_port = 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);
......@@ -177,8 +177,8 @@ void Serial::open() {
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()));
this->serial_port = NULL;
}
}
......@@ -189,6 +189,8 @@ void Serial::close() {
this->serial_port->close();
}
static const boost::posix_time::time_duration timeout_zero_comparison(boost::posix_time::milliseconds(0));
const int Serial::read(char* buffer, int size) {
this->reading = true;
if(this->nonblocking) // Do not wait for data
......@@ -201,8 +203,8 @@ const int Serial::read(char* buffer, int size) {
boost::bind(&Serial::read_complete, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
if(this->timeout != NULL) { // Only set a timeout_timer if there is a valid timeout
this->timeout_timer.expires_from_now(*this->timeout);
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));
}
......@@ -219,7 +221,7 @@ const 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;
delete[] serial_buffer;
return return_str;
}
......@@ -248,7 +250,7 @@ const 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;
delete[] cstr;
return bytes_wrote;
}
......@@ -260,12 +262,12 @@ void Serial::setDTR(bool level) {
this->serial_port->set_option(DTRControl(level));
}
const bool Serial::getCTS() {
const bool Serial::getCTS() const {
throw(boost::asio::error::operation_not_supported);
return false;
}
const bool Serial::getDSR() {
const bool Serial::getDSR() const {
throw(boost::asio::error::operation_not_supported);
return false;
}
......@@ -275,9 +277,9 @@ void Serial::setTimeoutMilliseconds(long timeout) {
// 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 = new boost::posix_time::milliseconds(timeout);
this->timeout = boost::posix_time::time_duration(boost::posix_time::milliseconds(timeout));
} else {
this->timeout = NULL;
this->timeout = boost::posix_time::time_duration(boost::posix_time::milliseconds(0));
}
if(timeout == 0)
......@@ -286,15 +288,15 @@ void Serial::setTimeoutMilliseconds(long timeout) {
this->nonblocking = false;
}
const long Serial::getTimeoutMilliseconds() {
return this->timeout->total_milliseconds();
const long Serial::getTimeoutMilliseconds() const {
return this->timeout.total_milliseconds();
}
void Serial::setBaudrate(int baudrate) {
this->baudrate = boost::asio::serial_port_base::baud_rate(baudrate);
}
const int Serial::getBaudrate() {
const int Serial::getBaudrate() const {
return this->baudrate.value();
}
......@@ -318,7 +320,7 @@ void Serial::setBytesize(bytesize_t bytesize) {
}
}
const bytesize_t Serial::getBytesize() {
const bytesize_t Serial::getBytesize() const {
return bytesize_t(this->bytesize.value());
}
......@@ -339,7 +341,7 @@ void Serial::setParity(parity_t parity) {
}
}
const parity_t Serial::getParity() {
const parity_t Serial::getParity() const {
switch(this->parity.value()) {
case boost::asio::serial_port_base::parity::none:
return parity_t(NONE);
......@@ -369,7 +371,7 @@ void Serial::setStopbits(stopbits_t stopbits) {
}
}
const stopbits_t Serial::getStopbits() {
const stopbits_t Serial::getStopbits() const {
switch(this->stopbits.value()) {
case boost::asio::serial_port_base::stop_bits::one:
return stopbits_t(STOPBITS_ONE);
......@@ -399,7 +401,7 @@ void Serial::setFlowcontrol(flowcontrol_t flowcontrol) {
}
}
const flowcontrol_t Serial::getFlowcontrol() {
const flowcontrol_t Serial::getFlowcontrol() const {
switch(this->flowcontrol.value()) {
case boost::asio::serial_port_base::flow_control::none:
return flowcontrol_t(FLOWCONTROL_NONE);
......@@ -410,4 +412,4 @@ const flowcontrol_t Serial::getFlowcontrol() {
default:
throw(InvalidFlowcontrolException(this->flowcontrol.value()));
}
}
\ No newline at end of file
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment