diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..a9aa566 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,27 @@ +# Changelog +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) +and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). + +## [Unreleased] + +### Added +- CMake build support. +- Unit tests using gtest. + +### Changed +- Updated serial port to use C++14. + +## [v1.0.1] - 2014-05-21 + +### Changed +- Added ability to enable/disable echo with 'SerialPort::EnableEcho()'. + +## [v1.0.0] - 2014-05-15 + +### Added +- Initial commit. serial-port-cpp library has basic functions up and running. + +[Unreleased]: https://github.com/mbedded-ninja/CppLinuxSerial/compare/v1.0.1...HEAD +[1.0.1]: https://github.com/mbedded-ninja/CppLinuxSerial/compare/v1.0.1...v1.0.0 \ No newline at end of file diff --git a/README.rst b/README.rst index 5ce1e48..a996c2a 100644 --- a/README.rst +++ b/README.rst @@ -6,8 +6,8 @@ CppLinuxSerial Serial port library written in C++ ---------------------------------- -.. image:: https://api.travis-ci.org/gbmhunter/serial-port-cpp.png?branch=master - :target: https://travis-ci.org/gbmhunter/serial-port-cpp +.. image:: https://api.travis-ci.org/gbmhunter/CppLinuxSerial.png?branch=master + :target: https://travis-ci.org/gbmhunter/CppLinuxSerial .. role:: bash(code) :language: bash @@ -34,11 +34,13 @@ Dependencies The following table lists all of the libraries dependencies. -====================== ==================== ====================================================================== -Dependency Delivery Usage -====================== ==================== ====================================================================== - Standard C library snprintf() -====================== ==================== ====================================================================== +====================== ====================================================================== +Dependency Comments +====================== ====================================================================== +C++14 C++14 used for strongly typed enums, std::chrono and literals. + snprintf() +stty Used in unit tests to verify the serial port is configured correctly. +====================== ====================================================================== Issues ====== @@ -48,20 +50,7 @@ See GitHub Issues. Usage ===== -In main.c add... - -:: - - - - - int main() - { - - - } - - +Nothing here yet... FAQ === @@ -72,9 +61,4 @@ FAQ Changelog ========= -========= ========== =================================================================================================== -Version Date Comment -========= ========== =================================================================================================== -v1.0.1.0 2014/05/21 Added ability to enable/disable echo with 'SerialPort::EnableEcho()'. -v1.0.0.0 2014/05/15 Initial commit. serial-port-cpp library has basic functions up and running. -========= ========== =================================================================================================== \ No newline at end of file +See CHANGELOG.md. \ No newline at end of file diff --git a/test/unit/BasicTests.cpp b/test/unit/BasicTests.cpp index 4319183..26cf15e 100644 --- a/test/unit/BasicTests.cpp +++ b/test/unit/BasicTests.cpp @@ -23,21 +23,6 @@ namespace { class BasicTests : public ::testing::Test { protected: - static void SetUpTestCase() { - GetTestUtil().CreateVirtualSerialPortPair(); - } - - static void TearDownTestCase() { - std::cout << "Destroying virtual serial ports..." << std::endl; - GetTestUtil().CloseSerialPorts(); - } - - static TestUtil& GetTestUtil() { - static TestUtil testUtil; - return testUtil; - } - - BasicTests() { } @@ -53,17 +38,6 @@ namespace { EXPECT_EQ(true, true); } - TEST_F(BasicTests, BaudRateSetCorrectly) { - SerialPort serialPort0(device0_, BaudRate::B_57600); - serialPort0.Open(); - auto retVal = TestUtil::Exec("stty -a -F " + device0_); - EXPECT_NE(std::string::npos, retVal.find("speed 57600 baud")); - - serialPort0.SetBaudRate(BaudRate::B_115200); - retVal = TestUtil::Exec("stty -a -F " + device0_); - EXPECT_NE(std::string::npos, retVal.find("speed 115200 baud")); - } - TEST_F(BasicTests, CanOpen) { SerialPort serialPort0(device0_, BaudRate::B_57600); serialPort0.Open(); diff --git a/test/unit/ConfigTests.cpp b/test/unit/ConfigTests.cpp new file mode 100644 index 0000000..b73ef96 --- /dev/null +++ b/test/unit/ConfigTests.cpp @@ -0,0 +1,61 @@ +/// +/// \file ConfigTests.cpp +/// \author Geoffrey Hunter (www.mbedded.ninja) +/// \created 2017-11-24 +/// \last-modified 2017-11-24 +/// \brief Configuration tests for the SerialPort class. +/// \details +/// See README.rst in repo root dir for more info. + +// System includes +#include "gtest/gtest.h" + +// 3rd party includes +#include "CppLinuxSerial/SerialPort.hpp" + +// User includes +#include "TestUtil.hpp" + +using namespace mn::CppLinuxSerial; + +namespace { + + class ConfigTests : public ::testing::Test { + protected: + + ConfigTests() { + } + + virtual ~ConfigTests() { + } + + std::string device0_ = "/dev/ttyS10"; + std::string device1_ = "/dev/ttyS11"; + }; + + TEST_F(ConfigTests, BaudRateSetCorrectly) { + SerialPort serialPort0(device0_, BaudRate::B_57600); + serialPort0.Open(); + auto retVal = TestUtil::Exec("stty -a -F " + device0_); + EXPECT_NE(std::string::npos, retVal.find("speed 57600 baud")); + + serialPort0.SetBaudRate(BaudRate::B_115200); + retVal = TestUtil::Exec("stty -a -F " + device0_); + EXPECT_NE(std::string::npos, retVal.find("speed 115200 baud")); + } + + TEST_F(ConfigTests, CanonicalModeOff) { + SerialPort serialPort0(device0_, BaudRate::B_57600); + serialPort0.Open(); + auto retVal = TestUtil::Exec("stty -a -F " + device0_); + EXPECT_NE(std::string::npos, retVal.find("-icanon")); + } + + TEST_F(ConfigTests, EchoModeOff) { + SerialPort serialPort0(device0_, BaudRate::B_57600); + serialPort0.Open(); + auto retVal = TestUtil::Exec("stty -a -F " + device0_); + EXPECT_NE(std::string::npos, retVal.find("-echo")); + } + +} // namespace \ No newline at end of file diff --git a/test/unit/TestUtil.hpp b/test/unit/TestUtil.hpp index e580e04..dd42570 100644 --- a/test/unit/TestUtil.hpp +++ b/test/unit/TestUtil.hpp @@ -47,30 +47,20 @@ namespace mn { return result; } - void CreateVirtualSerialPortPair() { + static void CreateVirtualSerialPortPair() { std::cout << "Creating virtual serial port pair..." << std::endl; -// StartProcess("sudo socat -d -d pty,raw,echo=0,link=/dev/ttyS10 pty,raw,echo=0,link=/dev/ttyS11"); -// std::this_thread::sleep_for(1s); -// StartProcess("sudo chmod a+rw /dev/ttyS10"); -// StartProcess("sudo chmod a+rw /dev/ttyS11"); -// std::this_thread::sleep_for(1s); -// std::cout << "Finished creating virtual serial port pair." << std::endl; -// std::system("./run.sh"); std::system("nohup sudo socat -d -d pty,raw,echo=0,link=/dev/ttyS10 pty,raw,echo=0,link=/dev/ttyS11 &"); - auto pid = std::system("echo $!"); - std::cout << "pid = " << pid << std::endl; + + // Hacky! Since socat is detached, we have no idea at what point it has created + // ttyS10 and ttyS11. Assume 1 second is long enough... std::this_thread::sleep_for(1s); std::system("sudo chmod a+rw /dev/ttyS10"); std::system("sudo chmod a+rw /dev/ttyS11"); } - void CloseSerialPorts() { -// for(const auto& filePointer : processes_) { -// std::cout << "Sending SIGINT..." << std::endl; -// kill(filePointer.pid, SIGINT); -// std::cout << "Calling pclose2()..." << std::endl; -// pclose2(filePointer.fp, filePointer.pid); -// } + static void CloseSerialPorts() { + // Dangerous! Kills all socat processes running + // on computer std::system("sudo pkill socat"); } diff --git a/test/unit/main.cpp b/test/unit/main.cpp new file mode 100644 index 0000000..7fa883a --- /dev/null +++ b/test/unit/main.cpp @@ -0,0 +1,42 @@ +/// +/// \file main.cpp +/// \author Geoffrey Hunter (www.mbedded.ninja) +/// \edited n/a +/// \created 2017-11-24 +/// \last-modified 2017-11-24 +/// \brief Contains the main entry point for the unit tests. +/// \details +/// See README.md in root dir for more info. + +// System includes +#include "gtest/gtest.h" + +// User includes +#include "TestUtil.hpp" + +using namespace mn::CppLinuxSerial; + +class Environment : public testing::Environment { +public: + virtual ~Environment() {} + // Override this to define how to set up the environment. + virtual void SetUp() { + std::cout << __PRETTY_FUNCTION__ << " called." << std::endl; + TestUtil::CreateVirtualSerialPortPair(); + } + // Override this to define how to tear down the environment. + virtual void TearDown() { + TestUtil::CloseSerialPorts(); + } +}; + +int main(int argc, char **argv) +{ + ::testing::InitGoogleTest(&argc, argv); + + // Create and register global test setup + // (gtest takes ownership of pointer, do not delete manaully!) + ::testing::AddGlobalTestEnvironment(new Environment); + + return RUN_ALL_TESTS(); +} \ No newline at end of file