From 4de88caad1eb49364d2b84172a2887d28c3565b3 Mon Sep 17 00:00:00 2001 From: Geoffrey Hunter Date: Fri, 24 Nov 2017 09:44:34 -0800 Subject: [PATCH] Removed api folder. Added CMakeLists.txt files for src. Added unit test folder. --- .gitignore | 1 + CMakeLists.txt | 26 ++++++++++++++++++++++++++ api/SerialPortApi.hpp | 17 ----------------- include/CppLinuxSerial/Exception.hpp | 0 include/CppLinuxSerial/SerialPort.hpp | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ include/SerialPort/SerialPort.hpp | 100 ---------------------------------------------------------------------------------------------------- src/CMakeLists.txt | 19 +++++++++++++++++++ src/SerialPort.cpp | 9 +++++---- test/unit/BasicTests.cpp | 23 +++++++++++++++++++++++ test/unit/CMakeLists.txt | 33 +++++++++++++++++++++++++++++++++ 10 files changed, 206 insertions(+), 121 deletions(-) create mode 100644 .gitignore create mode 100644 CMakeLists.txt delete mode 100644 api/SerialPortApi.hpp create mode 100644 include/CppLinuxSerial/Exception.hpp create mode 100644 include/CppLinuxSerial/SerialPort.hpp delete mode 100644 include/SerialPort/SerialPort.hpp create mode 100644 src/CMakeLists.txt create mode 100644 test/unit/BasicTests.cpp create mode 100644 test/unit/CMakeLists.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d163863 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +build/ \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..b56b1e0 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,26 @@ +cmake_minimum_required(VERSION 3.1.0) +project(CppLinuxSerial) + +add_definitions(-std=c++11) + +option(BUILD_TESTS "If set to true, unit tests will be build as part of make all." TRUE) +if (BUILD_TESTS) + message("BUILD_TESTS=TRUE, unit tests will be built.") +else () + message("BUILD_TESTS=FALSE, unit tests will NOT be built.") +endif () + + + +#=================================================================================================# +#========================================= This Project ==========================================# +#=================================================================================================# + +# Now simply link your own targets against gtest, gmock, +# etc. as appropriate +include_directories(include) + +add_subdirectory(src) +if(BUILD_TESTS) + add_subdirectory(test/unit) +endif() diff --git a/api/SerialPortApi.hpp b/api/SerialPortApi.hpp deleted file mode 100644 index 72acf9b..0000000 --- a/api/SerialPortApi.hpp +++ /dev/null @@ -1,17 +0,0 @@ -//! -//! @file SerialPortApi.hpp -//! @author Geoffrey Hunter (www.cladlab.com) -//! @created 2014/05/15 -//! @last-modified 2014/05/15 -//! @brief File which contains all the API definitions needed to use the serial-port-cpp library. -//! @details -//! See README.rst in repo root dir for more info. - -// Header guard -#ifndef SERIAL_PORT_SERIAL_PORT_API_H -#define SERIAL_PORT_SERIAL_PORT_API_H - -// User headers -#include "../include/SerialPort.hpp" - -#endif // #ifndef SERIAL_PORT_SERIAL_PORT_API_H diff --git a/include/CppLinuxSerial/Exception.hpp b/include/CppLinuxSerial/Exception.hpp new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/include/CppLinuxSerial/Exception.hpp diff --git a/include/CppLinuxSerial/SerialPort.hpp b/include/CppLinuxSerial/SerialPort.hpp new file mode 100644 index 0000000..b942d62 --- /dev/null +++ b/include/CppLinuxSerial/SerialPort.hpp @@ -0,0 +1,99 @@ +/// +/// \file SerialPort.hpp +/// \author Geoffrey Hunter () +/// \created 2014-01-07 +/// \last-modified 2017-11-23 +/// \brief The main serial port class. +/// \details +/// See README.rst in repo root dir for more info. + +// Header guard +#ifndef SERIAL_PORT_SERIAL_PORT_H +#define SERIAL_PORT_SERIAL_PORT_H + +// System headers +#include // For file I/O (reading/writing to COM port) +#include +#include // POSIX terminal control definitions (struct termios) + +// User headers + +namespace mn +{ +namespace CppLinuxSerial +{ + +/// \brief Strongly-typed enumeration of baud rates for use with the SerialPort class +enum class BaudRates +{ + none, + b9600, + b57600 +}; + +//! @brief SerialPort object is used to perform rx/tx serial communication. +class SerialPort +{ + + public: + //! @brief Constructor + SerialPort(); + + //! @brief Destructor + virtual ~SerialPort(); + + //! @brief Sets the file path to use for communications. The file path must be set before Open() is called, otherwise Open() will return an error. + void SetFilePath(std::string filePath); + + void SetBaudRate(BaudRates baudRate); + + //! @brief Controls what happens when Read() is called. + //! @param numOfCharToWait Minimum number of characters to wait for before returning. Set to 0 for non-blocking mode. + void SetNumCharsToWait(uint32_t numCharsToWait); + + //! @brief Enables/disables echo. + //! param echoOn Pass in true to enable echo, false to disable echo. + void EnableEcho(bool echoOn); + + //! @brief Opens the COM port for use. + //! @throws {std::runtime_error} if filename has not been set. + //! {std::system_error} if system open() operation fails. + //! @note Must call this before you can configure the COM port. + void Open(); + + //! @brief Sets all settings for the com port to common defaults. + void SetEverythingToCommonDefaults(); + + //! @brief Closes the COM port. + void Close(); + + //! @brief Sends a message over the com port. + //! @param str Reference to an string containing the characters to write to the COM port. + //! @throws {std::runtime_error} if filename has not been set. + //! {std::system_error} if system write() operation fails. + void Write(std::string *str); + + //! @brief Use to read from the COM port. + //! @param str Reference to a string that the read characters from the COM port will be saved to. + //! @throws {std::runtime_error} if filename has not been set. + //! {std::system_error} if system read() operation fails. + void Read(std::string *str); + + private: + std::string filePath; + + BaudRates baudRate; + + //! @brief The file descriptor for the open file. This gets written to when Open() is called. + int fileDesc; + + //! @brief Returns a populated termios structure for the passed in file descriptor. + termios GetTermios(); + + void SetTermios(termios myTermios); +}; + +} // namespace CppLinuxSerial +} // namespace mn + +#endif // #ifndef SERIAL_PORT_SERIAL_PORT_H diff --git a/include/SerialPort/SerialPort.hpp b/include/SerialPort/SerialPort.hpp deleted file mode 100644 index 3f3c6c8..0000000 --- a/include/SerialPort/SerialPort.hpp +++ /dev/null @@ -1,100 +0,0 @@ -/// -/// \file SerialPort.hpp -/// \author Geoffrey Hunter () -/// \created 2014-01-07 -/// \last-modified 2017-11-23 -/// \brief The main serial port class. -/// \details -/// See README.rst in repo root dir for more info. - -// Header guard -#ifndef SERIAL_PORT_SERIAL_PORT_H -#define SERIAL_PORT_SERIAL_PORT_H - -// System headers -#include // For file I/O (reading/writing to COM port) -#include -#include // POSIX terminal control definitions (struct termios) - -// User headers - -namespace mn { -namespace SerialPort -{ - - /// \brief Strongly-typed enumeration of baud rates for use with the SerialPort class - enum class BaudRates { - none, - b9600, - b57600 - }; - - //! @brief SerialPort object is used to perform rx/tx serial communication. - class SerialPort - { - - public: - - //! @brief Constructor - SerialPort(); - - //! @brief Destructor - virtual ~SerialPort(); - - //! @brief Sets the file path to use for communications. The file path must be set before Open() is called, otherwise Open() will return an error. - void SetFilePath(std::string filePath); - - void SetBaudRate(BaudRates baudRate); - - //! @brief Controls what happens when Read() is called. - //! @param numOfCharToWait Minimum number of characters to wait for before returning. Set to 0 for non-blocking mode. - void SetNumCharsToWait(uint32_t numCharsToWait); - - //! @brief Enables/disables echo. - //! param echoOn Pass in true to enable echo, false to disable echo. - void EnableEcho(bool echoOn); - - //! @brief Opens the COM port for use. - //! @throws {std::runtime_error} if filename has not been set. - //! {std::system_error} if system open() operation fails. - //! @note Must call this before you can configure the COM port. - void Open(); - - //! @brief Sets all settings for the com port to common defaults. - void SetEverythingToCommonDefaults(); - - //! @brief Closes the COM port. - void Close(); - - //! @brief Sends a message over the com port. - //! @param str Reference to an string containing the characters to write to the COM port. - //! @throws {std::runtime_error} if filename has not been set. - //! {std::system_error} if system write() operation fails. - void Write(std::string* str); - - //! @brief Use to read from the COM port. - //! @param str Reference to a string that the read characters from the COM port will be saved to. - //! @throws {std::runtime_error} if filename has not been set. - //! {std::system_error} if system read() operation fails. - void Read(std::string* str); - - private: - - std::string filePath; - - BaudRates baudRate; - - //! @brief The file descriptor for the open file. This gets written to when Open() is called. - int fileDesc; - - //! @brief Returns a populated termios structure for the passed in file descriptor. - termios GetTermios(); - - void SetTermios(termios myTermios); - - }; - -} // namespace SerialPort -} // namespace mn - -#endif // #ifndef SERIAL_PORT_SERIAL_PORT_H diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..12773f3 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,19 @@ + + +file(GLOB_RECURSE CppLinuxSerial_SRC + "*.cpp") + +file(GLOB_RECURSE CppLinuxSerial_HEADERS + "${CMAKE_SOURCE_DIR}/include/*.hpp") + +add_library(CppLinuxSerial ${CppLinuxSerial_SRC} ${CppLinuxSerial_HEADERS}) + +target_include_directories(CppLinuxSerial PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) + +# On Linux, "sudo make install" will typically copy the library +# into the folder /usr/local/bin +install(TARGETS CppLinuxSerial DESTINATION lib) + +# On Linux, "sudo make install" will typically copy the +# folder into /usr/local/include +install(DIRECTORY ${CMAKE_SOURCE_DIR}/include/CppLinuxSerial DESTINATION include) \ No newline at end of file diff --git a/src/SerialPort.cpp b/src/SerialPort.cpp index 8dd67db..e46cbf8 100644 --- a/src/SerialPort.cpp +++ b/src/SerialPort.cpp @@ -17,10 +17,10 @@ #include // POSIX terminal control definitions (struct termios) #include // For throwing std::system_error -#include "SerialPort/SerialPort.hpp" +#include "CppLinuxSerial/SerialPort.hpp" -namespace SerialPort -{ +namespace mn { +namespace CppLinuxSerial { SerialPort::SerialPort() : filePath(std::string()), @@ -325,4 +325,5 @@ namespace SerialPort // Successful! } -} // namespace ComPort +} // namespace CppLinuxSerial +} // namespace mn diff --git a/test/unit/BasicTests.cpp b/test/unit/BasicTests.cpp new file mode 100644 index 0000000..e92a8fc --- /dev/null +++ b/test/unit/BasicTests.cpp @@ -0,0 +1,23 @@ +#include "gtest/gtest.h" + +#include "CppLinuxSerial/SerialPort.hpp" + +using namespace mn::CppLinuxSerial; + +namespace { + + class BasicTest : public ::testing::Test { + protected: + + BasicTest() { + } + + virtual ~BasicTest() { + } + }; + + TEST_F(BasicTest, SimplePacket) { + EXPECT_EQ(true, true); + } + +} // namespace \ No newline at end of file diff --git a/test/unit/CMakeLists.txt b/test/unit/CMakeLists.txt new file mode 100644 index 0000000..a6b287d --- /dev/null +++ b/test/unit/CMakeLists.txt @@ -0,0 +1,33 @@ +# +# \file CMakeLists.txt +# \author Geoffrey Hunter (www.mbedded.ninja) +# \edited n/a +# \created 2017-11-24 +# \last-modified 2017-11-24 +# \brief Contains instructions for building the unit tests. +# \details +# See README.md in root dir for more info. + +enable_testing() +find_package (Threads) +find_package(GTest REQUIRED) +message("gtest libraries found at ${GTEST_BOTH_LIBRARIES}") + +file(GLOB_RECURSE CppLinuxSerialUnitTests_SRC + "*.cpp" + "*.hpp") + +add_executable(CppLinuxSerialUnitTests ${CppLinuxSerialUnitTests_SRC}) + +target_link_libraries(CppLinuxSerialUnitTests LINK_PUBLIC CppLinuxSerial ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT}) + +# The custom target and custom command below allow the unit tests +# to be run. +# If you want them to run automatically by CMake, uncomment #ALL +add_custom_target( + run_unit_tests #ALL + DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/CppLinuxSerialUnitTests.touch CppLinuxSerialUnitTests) + +add_custom_command( + OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/CppLinuxSerialUnitTests.touch + COMMAND ${CMAKE_CURRENT_BINARY_DIR}/CppLinuxSerialUnitTests) \ No newline at end of file -- libgit2 0.21.4