Commit 4de88caad1eb49364d2b84172a2887d28c3565b3

Authored by Geoffrey Hunter
1 parent b50efff3

Removed api folder. Added CMakeLists.txt files for src. Added unit test folder.

.gitignore 0 → 100644
  1 +build/
0 2 \ No newline at end of file
... ...
CMakeLists.txt 0 → 100644
  1 +cmake_minimum_required(VERSION 3.1.0)
  2 +project(CppLinuxSerial)
  3 +
  4 +add_definitions(-std=c++11)
  5 +
  6 +option(BUILD_TESTS "If set to true, unit tests will be build as part of make all." TRUE)
  7 +if (BUILD_TESTS)
  8 + message("BUILD_TESTS=TRUE, unit tests will be built.")
  9 +else ()
  10 + message("BUILD_TESTS=FALSE, unit tests will NOT be built.")
  11 +endif ()
  12 +
  13 +
  14 +
  15 +#=================================================================================================#
  16 +#========================================= This Project ==========================================#
  17 +#=================================================================================================#
  18 +
  19 +# Now simply link your own targets against gtest, gmock,
  20 +# etc. as appropriate
  21 +include_directories(include)
  22 +
  23 +add_subdirectory(src)
  24 +if(BUILD_TESTS)
  25 + add_subdirectory(test/unit)
  26 +endif()
... ...
api/SerialPortApi.hpp deleted
1   -//!
2   -//! @file SerialPortApi.hpp
3   -//! @author Geoffrey Hunter <gbmhunter@gmail.com> (www.cladlab.com)
4   -//! @created 2014/05/15
5   -//! @last-modified 2014/05/15
6   -//! @brief File which contains all the API definitions needed to use the serial-port-cpp library.
7   -//! @details
8   -//! See README.rst in repo root dir for more info.
9   -
10   -// Header guard
11   -#ifndef SERIAL_PORT_SERIAL_PORT_API_H
12   -#define SERIAL_PORT_SERIAL_PORT_API_H
13   -
14   -// User headers
15   -#include "../include/SerialPort.hpp"
16   -
17   -#endif // #ifndef SERIAL_PORT_SERIAL_PORT_API_H
include/CppLinuxSerial/Exception.hpp 0 → 100644
include/SerialPort/SerialPort.hpp renamed to include/CppLinuxSerial/SerialPort.hpp
... ... @@ -12,89 +12,88 @@
12 12 #define SERIAL_PORT_SERIAL_PORT_H
13 13  
14 14 // System headers
15   -#include <fstream> // For file I/O (reading/writing to COM port)
  15 +#include <fstream> // For file I/O (reading/writing to COM port)
16 16 #include <sstream>
17   -#include <termios.h> // POSIX terminal control definitions (struct termios)
  17 +#include <termios.h> // POSIX terminal control definitions (struct termios)
18 18  
19 19 // User headers
20 20  
21   -namespace mn {
22   -namespace SerialPort
  21 +namespace mn
  22 +{
  23 +namespace CppLinuxSerial
23 24 {
24 25  
25   - /// \brief Strongly-typed enumeration of baud rates for use with the SerialPort class
26   - enum class BaudRates {
27   - none,
28   - b9600,
29   - b57600
30   - };
31   -
32   - //! @brief SerialPort object is used to perform rx/tx serial communication.
33   - class SerialPort
34   - {
35   -
36   - public:
37   -
38   - //! @brief Constructor
39   - SerialPort();
  26 +/// \brief Strongly-typed enumeration of baud rates for use with the SerialPort class
  27 +enum class BaudRates
  28 +{
  29 + none,
  30 + b9600,
  31 + b57600
  32 +};
40 33  
41   - //! @brief Destructor
42   - virtual ~SerialPort();
  34 +//! @brief SerialPort object is used to perform rx/tx serial communication.
  35 +class SerialPort
  36 +{
43 37  
44   - //! @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.
45   - void SetFilePath(std::string filePath);
  38 + public:
  39 + //! @brief Constructor
  40 + SerialPort();
46 41  
47   - void SetBaudRate(BaudRates baudRate);
  42 + //! @brief Destructor
  43 + virtual ~SerialPort();
48 44  
49   - //! @brief Controls what happens when Read() is called.
50   - //! @param numOfCharToWait Minimum number of characters to wait for before returning. Set to 0 for non-blocking mode.
51   - void SetNumCharsToWait(uint32_t numCharsToWait);
  45 + //! @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.
  46 + void SetFilePath(std::string filePath);
52 47  
53   - //! @brief Enables/disables echo.
54   - //! param echoOn Pass in true to enable echo, false to disable echo.
55   - void EnableEcho(bool echoOn);
  48 + void SetBaudRate(BaudRates baudRate);
56 49  
57   - //! @brief Opens the COM port for use.
58   - //! @throws {std::runtime_error} if filename has not been set.
59   - //! {std::system_error} if system open() operation fails.
60   - //! @note Must call this before you can configure the COM port.
61   - void Open();
  50 + //! @brief Controls what happens when Read() is called.
  51 + //! @param numOfCharToWait Minimum number of characters to wait for before returning. Set to 0 for non-blocking mode.
  52 + void SetNumCharsToWait(uint32_t numCharsToWait);
62 53  
63   - //! @brief Sets all settings for the com port to common defaults.
64   - void SetEverythingToCommonDefaults();
  54 + //! @brief Enables/disables echo.
  55 + //! param echoOn Pass in true to enable echo, false to disable echo.
  56 + void EnableEcho(bool echoOn);
65 57  
66   - //! @brief Closes the COM port.
67   - void Close();
  58 + //! @brief Opens the COM port for use.
  59 + //! @throws {std::runtime_error} if filename has not been set.
  60 + //! {std::system_error} if system open() operation fails.
  61 + //! @note Must call this before you can configure the COM port.
  62 + void Open();
68 63  
69   - //! @brief Sends a message over the com port.
70   - //! @param str Reference to an string containing the characters to write to the COM port.
71   - //! @throws {std::runtime_error} if filename has not been set.
72   - //! {std::system_error} if system write() operation fails.
73   - void Write(std::string* str);
  64 + //! @brief Sets all settings for the com port to common defaults.
  65 + void SetEverythingToCommonDefaults();
74 66  
75   - //! @brief Use to read from the COM port.
76   - //! @param str Reference to a string that the read characters from the COM port will be saved to.
77   - //! @throws {std::runtime_error} if filename has not been set.
78   - //! {std::system_error} if system read() operation fails.
79   - void Read(std::string* str);
  67 + //! @brief Closes the COM port.
  68 + void Close();
80 69  
81   - private:
  70 + //! @brief Sends a message over the com port.
  71 + //! @param str Reference to an string containing the characters to write to the COM port.
  72 + //! @throws {std::runtime_error} if filename has not been set.
  73 + //! {std::system_error} if system write() operation fails.
  74 + void Write(std::string *str);
82 75  
83   - std::string filePath;
  76 + //! @brief Use to read from the COM port.
  77 + //! @param str Reference to a string that the read characters from the COM port will be saved to.
  78 + //! @throws {std::runtime_error} if filename has not been set.
  79 + //! {std::system_error} if system read() operation fails.
  80 + void Read(std::string *str);
84 81  
85   - BaudRates baudRate;
  82 + private:
  83 + std::string filePath;
86 84  
87   - //! @brief The file descriptor for the open file. This gets written to when Open() is called.
88   - int fileDesc;
  85 + BaudRates baudRate;
89 86  
90   - //! @brief Returns a populated termios structure for the passed in file descriptor.
91   - termios GetTermios();
  87 + //! @brief The file descriptor for the open file. This gets written to when Open() is called.
  88 + int fileDesc;
92 89  
93   - void SetTermios(termios myTermios);
  90 + //! @brief Returns a populated termios structure for the passed in file descriptor.
  91 + termios GetTermios();
94 92  
95   - };
  93 + void SetTermios(termios myTermios);
  94 +};
96 95  
97   -} // namespace SerialPort
  96 +} // namespace CppLinuxSerial
98 97 } // namespace mn
99 98  
100 99 #endif // #ifndef SERIAL_PORT_SERIAL_PORT_H
... ...
src/CMakeLists.txt 0 → 100644
  1 +
  2 +
  3 +file(GLOB_RECURSE CppLinuxSerial_SRC
  4 + "*.cpp")
  5 +
  6 +file(GLOB_RECURSE CppLinuxSerial_HEADERS
  7 + "${CMAKE_SOURCE_DIR}/include/*.hpp")
  8 +
  9 +add_library(CppLinuxSerial ${CppLinuxSerial_SRC} ${CppLinuxSerial_HEADERS})
  10 +
  11 +target_include_directories(CppLinuxSerial PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
  12 +
  13 +# On Linux, "sudo make install" will typically copy the library
  14 +# into the folder /usr/local/bin
  15 +install(TARGETS CppLinuxSerial DESTINATION lib)
  16 +
  17 +# On Linux, "sudo make install" will typically copy the
  18 +# folder into /usr/local/include
  19 +install(DIRECTORY ${CMAKE_SOURCE_DIR}/include/CppLinuxSerial DESTINATION include)
0 20 \ No newline at end of file
... ...
src/SerialPort.cpp
... ... @@ -17,10 +17,10 @@
17 17 #include <termios.h> // POSIX terminal control definitions (struct termios)
18 18 #include <system_error> // For throwing std::system_error
19 19  
20   -#include "SerialPort/SerialPort.hpp"
  20 +#include "CppLinuxSerial/SerialPort.hpp"
21 21  
22   -namespace SerialPort
23   -{
  22 +namespace mn {
  23 +namespace CppLinuxSerial {
24 24  
25 25 SerialPort::SerialPort() :
26 26 filePath(std::string()),
... ... @@ -325,4 +325,5 @@ namespace SerialPort
325 325 // Successful!
326 326 }
327 327  
328   -} // namespace ComPort
  328 +} // namespace CppLinuxSerial
  329 +} // namespace mn
... ...
test/unit/BasicTests.cpp 0 → 100644
  1 +#include "gtest/gtest.h"
  2 +
  3 +#include "CppLinuxSerial/SerialPort.hpp"
  4 +
  5 +using namespace mn::CppLinuxSerial;
  6 +
  7 +namespace {
  8 +
  9 + class BasicTest : public ::testing::Test {
  10 + protected:
  11 +
  12 + BasicTest() {
  13 + }
  14 +
  15 + virtual ~BasicTest() {
  16 + }
  17 + };
  18 +
  19 + TEST_F(BasicTest, SimplePacket) {
  20 + EXPECT_EQ(true, true);
  21 + }
  22 +
  23 +} // namespace
0 24 \ No newline at end of file
... ...
test/unit/CMakeLists.txt 0 → 100644
  1 +#
  2 +# \file CMakeLists.txt
  3 +# \author Geoffrey Hunter <gbmhunter@gmail.com> (www.mbedded.ninja)
  4 +# \edited n/a
  5 +# \created 2017-11-24
  6 +# \last-modified 2017-11-24
  7 +# \brief Contains instructions for building the unit tests.
  8 +# \details
  9 +# See README.md in root dir for more info.
  10 +
  11 +enable_testing()
  12 +find_package (Threads)
  13 +find_package(GTest REQUIRED)
  14 +message("gtest libraries found at ${GTEST_BOTH_LIBRARIES}")
  15 +
  16 +file(GLOB_RECURSE CppLinuxSerialUnitTests_SRC
  17 + "*.cpp"
  18 + "*.hpp")
  19 +
  20 +add_executable(CppLinuxSerialUnitTests ${CppLinuxSerialUnitTests_SRC})
  21 +
  22 +target_link_libraries(CppLinuxSerialUnitTests LINK_PUBLIC CppLinuxSerial ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
  23 +
  24 +# The custom target and custom command below allow the unit tests
  25 +# to be run.
  26 +# If you want them to run automatically by CMake, uncomment #ALL
  27 +add_custom_target(
  28 + run_unit_tests #ALL
  29 + DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/CppLinuxSerialUnitTests.touch CppLinuxSerialUnitTests)
  30 +
  31 +add_custom_command(
  32 + OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/CppLinuxSerialUnitTests.touch
  33 + COMMAND ${CMAKE_CURRENT_BINARY_DIR}/CppLinuxSerialUnitTests)
0 34 \ No newline at end of file
... ...