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/SerialPort/SerialPort.hpp b/include/CppLinuxSerial/SerialPort.hpp index 3f3c6c8..b942d62 100644 --- a/include/SerialPort/SerialPort.hpp +++ b/include/CppLinuxSerial/SerialPort.hpp @@ -12,89 +12,88 @@ #define SERIAL_PORT_SERIAL_PORT_H // System headers -#include // For file I/O (reading/writing to COM port) +#include // For file I/O (reading/writing to COM port) #include -#include // POSIX terminal control definitions (struct termios) +#include // POSIX terminal control definitions (struct termios) // User headers -namespace mn { -namespace SerialPort +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 Strongly-typed enumeration of baud rates for use with the SerialPort class +enum class BaudRates +{ + none, + b9600, + b57600 +}; - //! @brief Destructor - virtual ~SerialPort(); +//! @brief SerialPort object is used to perform rx/tx serial communication. +class 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); + public: + //! @brief Constructor + SerialPort(); - void SetBaudRate(BaudRates baudRate); + //! @brief Destructor + virtual ~SerialPort(); - //! @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 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); - //! @brief Enables/disables echo. - //! param echoOn Pass in true to enable echo, false to disable echo. - void EnableEcho(bool echoOn); + void SetBaudRate(BaudRates baudRate); - //! @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 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 Sets all settings for the com port to common defaults. - void SetEverythingToCommonDefaults(); + //! @brief Enables/disables echo. + //! param echoOn Pass in true to enable echo, false to disable echo. + void EnableEcho(bool echoOn); - //! @brief Closes the COM port. - void Close(); + //! @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 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 Sets all settings for the com port to common defaults. + void SetEverythingToCommonDefaults(); - //! @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); + //! @brief Closes the COM port. + void Close(); - private: + //! @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); - std::string filePath; + //! @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); - BaudRates baudRate; + private: + std::string filePath; - //! @brief The file descriptor for the open file. This gets written to when Open() is called. - int fileDesc; + BaudRates baudRate; - //! @brief Returns a populated termios structure for the passed in file descriptor. - termios GetTermios(); + //! @brief The file descriptor for the open file. This gets written to when Open() is called. + int fileDesc; - void SetTermios(termios myTermios); + //! @brief Returns a populated termios structure for the passed in file descriptor. + termios GetTermios(); - }; + void SetTermios(termios myTermios); +}; -} // namespace SerialPort +} // namespace CppLinuxSerial } // 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