uh the fucking tests are fucking compiling :sigh: fuck itgit add *git add *git add *git add *git add *

This commit is contained in:
Jonas Zeunert
2018-06-15 02:30:05 +02:00
parent 70b25595e6
commit ed450b0409
21 changed files with 182 additions and 252 deletions

View File

@@ -4,6 +4,7 @@ project(flippr-driver)
set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") # enable C++11 standard
add_definitions(-lcrypt)
set(CMAKE_BINARY_DIR ${CMAKE_SOURCE_DIR}/bin)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR})
set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR})
@@ -18,37 +19,40 @@ add_library(libwiringPi SHARED IMPORTED )
set_property(TARGET libwiringPi PROPERTY IMPORTED_LOCATION ${LIB_DIR}/libwiringPi.so.2.44)
set_property(TARGET libwiringPi PROPERTY INTERFACE_INCLUDE_DIRECTORIES /home/rhetenor/FlippR/flippr-code/FlippR-Driver/src/lib/wiringPi)
find_package(Threads)
find_package(Boost)
find_package(Threads REQUIRED)
find_package(Boost COMPONENTS system thread timer chrono REQUIRED)
include_directories(${SOURCE_DIR}/input)
include_directories(${SOURCE_DIR}/output)
include_directories(${SOURCE_DIR}/lib)
include_directories(${SOURCE_DIR}/utilities)
include_directories(${Boost_INCLUDE_DIRS})
file(GLOB SOURCES ${SOURCE_DIR}/*/*.?pp)
file(GLOB SOURCES ${SOURCE_DIR}/*/*.cpp)
add_library(flippr_driver STATIC ${SOURCES})
target_link_libraries(flippr_driver ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries(flippr_driver ${Boost_LIBRARIES})
target_link_libraries(flippr_driver libwiringPi)
target_link_libraries(flippr_driver Easylogging)
# enable_testing(TRUE)
#
# Prepare "Catch" library for other executables
# set(CATCH_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../src/tests)
# add_library(Catch INTERFACE)
# target_include_directories(Catch INTERFACE ${CATCH_INCLUDE_DIR}/*)
#
# Make test executable
# set(TEST_SOURCES ${SOURCE_DIR}/../tests)
# include_directories(${TEST_SOURCES})
#
# file(GLOB SOURCES ${TEST_SOURCES}/*/*.cpp)
# file(GLOB HEADER_SOURCES ${TEST_SOURCES}/*/*.hpp)
#
# add_executable(tests ${SOURCES} ${HEADER_SOURCES})
#
# target_link_libraries(tests Easylogging)
# target_link_libraries(tests flippr_driver)
# target_link_libraries(tests Catch)
enable_testing(TRUE)
#Prepare "Catch" library for other executables
set(CATCH_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../src/tests)
add_library(Catch INTERFACE)
target_include_directories(Catch INTERFACE ${CATCH_INCLUDE_DIR}/*)
#Make test executable
set(TEST_SOURCES ${SOURCE_DIR}/../tests)
include_directories(${TEST_SOURCES})
file(GLOB SOURCES ${TEST_SOURCES}/*/*.cpp)
file(GLOB HEADER_SOURCES ${TEST_SOURCES}/*/*.hpp)
add_executable(tests ${SOURCES} ${HEADER_SOURCES})
target_link_libraries(tests Easylogging)
target_link_libraries(tests flippr_driver)
target_link_libraries(tests Catch)

View File

@@ -16,6 +16,13 @@
#include "IDetector.h"
#include <thread>
#include <map>
#include "../utilities/InputGPIOInterface.h"
#include "Event.h"
#include "IEventNotifier.h"
namespace Input
{

View File

@@ -7,19 +7,14 @@
#include "Event.h"
using namespace Input;
Event::Event(char address, char priority, std::string name) :
address(address), priority(priority), name(name)
{
CLOG_IF(VLOG_IS_ON(HIGH_VERBOSITY), INFO, INPUT_LOGGER) << "Created event: " << name << ", address: " << address;
}
bool Event::operator==(const Event& other)
{
return this->name == other.name;
}
friend bool operator<(const Event& left, const Event& right)
{
return left.priority < right.priority;
}
Event::Event(char address, char priority, std::string name) :
address(address), priority(priority), name(name)
{
CLOG_IF(VLOG_IS_ON(HIGH_VERBOSITY), INFO, INPUT_LOGGER) << "Created event: " << name << ", address: " << address;
}
bool Event::operator==(const Event& other)
{
return this->name == other.name;
}

View File

@@ -21,7 +21,10 @@ class Event
public:
Event(char address, char priority, std::string name);
bool operator==(const Event& other);
friend bool operator<(const Event& left, const Event& right);
friend bool operator<(const Event& left, const Event& right)
{
return left.priority < right.priority;
}
private:
char address;

View File

@@ -16,14 +16,14 @@ using namespace Input;
CLOG(INFO, INPUT_LOGGER) << "Created EventHandler";
}
virtual EventHandler::~EventHandler()
EventHandler::~EventHandler()
{
this->input_driver->unregister_event_handler(this);
this->input_driver = NULL;
}
// This function is intended to be non pure, if it is called when the derived class doesn't exist anymore
virtual void EventHandler::handle(Event& event)
void EventHandler::handle(Event& event)
{
CLOG(WARNING, INPUT_LOGGER) << "Called EventHandler parent class";
}

View File

@@ -14,7 +14,7 @@
#include <thread>
#include <mutex>
#include "../utilities/BlockingQueue.h"
#include "../utilities/BlockingQueue.hpp"
#include "Event.h"
#include "EventHandler.h"

View File

@@ -8,12 +8,6 @@
#ifndef SRC_INPUT_IDETECTOR_H_
#define SRC_INPUT_IDETECTOR_H_
#include <thread>
#include <map>
#include "../utilities/InputGPIOInterface.h"
#include "Event.h"
#include "EventNotifier.h"
namespace Input
{
@@ -22,7 +16,7 @@ class IDetector
{
public:
virtual ~IDetector() = 0;
virtual ~IDetector(){};
};
}

View File

@@ -17,7 +17,7 @@ namespace Input
class IEventNotifier
{
public:
virtual ~IEventNotifier() = 0;
virtual ~IEventNotifier(){};
virtual void register_event_handler(IEventHandler* handler) = 0;
virtual void unregister_event_handler(IEventHandler* handler) = 0;

View File

@@ -16,7 +16,7 @@ namespace Input {
class IInputDriver
{
public:
virtual ~IInputDriver() = 0;
virtual ~IInputDriver(){};
virtual void register_event_handler(IEventHandler* handler) = 0;
virtual void unregister_event_handler(IEventHandler* handler) = 0;
};

View File

@@ -7,6 +7,7 @@
#include "InputDriver.h"
using namespace Input;
InputDriver::InputDriver(IEventNotifier* event_notifier, IDetector* detector) :
event_notifier(event_notifier), detector(detector)
{

View File

@@ -22,8 +22,8 @@ public:
InputDriver(IEventNotifier* event_notifier, IDetector* detector);
~InputDriver();
void register_event_handler(IEventHandler* handler);
void unregister_event_handler(IEventHandler* handler);
virtual void register_event_handler(IEventHandler* handler) override;
virtual void unregister_event_handler(IEventHandler* handler) override;
private:
IEventNotifier* event_notifier;

View File

@@ -8,77 +8,71 @@
#include "InputDriverFactory.h"
#include "Detector.h"
#include "EventNotifier.h"
using namespace nlohmann;
using namespace Input;
static shared_ptr<IInputDriver*> InputDriverFactory::get_InputDriver(std::string& input_config_path, std::string& matrix_config_path)
{
this->ConfigureLogger();
auto event_notifier = new EventNotifier();
std::shared_ptr<InputDriver> InputDriverFactory::get_InputDriver(std::string& input_config_path, std::string& matrix_config_path)
{
ConfigureLogger();
auto event_notifier = new EventNotifier();
auto detector = this->get_detector(input_config_path, matrix_config_path);
auto detector = get_detector(input_config_path, matrix_config_path);
return shared_ptr<InputDriver*>(new InputDriver(event_notifier, detector));
}
static IDetector* get_detector(std::string& input_config_path, std::string& matrix_config_path)
{
std::ifstream input_config_stream(input_config_path);
json input_config;
input_config << input_config_stream;
std::ifstream matrix_config_stream(matrix_config_path);
json matrix_config;
matrix_config << matrix_config_stream;
auto input_gpio_interface = new InputGPIOInterface(input_config);
auto input_notifier = new InputEventNotifier();
std::map<char, Event> input_events = this->create_input_events(matrix_config);
return new Detector(input_gpio_interface, input_events, input_notifier);
}
static std::map<char, Event> InputDriverFactory::create_input_events(json matrix_config)
{
std::map<char, Event> events;
for(auto& json_event : matrix_config)
{
try
{
std::string name = json_event.at("name");
char address = json_event.at("address");
int priority = json_event.at("priority");
Event event(address, priority, name);
events.emplace(address, event);
}
catch(json::exception& e)
{
CLOG(ERROR, INPUT_LOGGER) << "Matrix config-file corrupted: " << e.what();
exit(EXIT_FAILURE);
}
}
return events;
}
static void InputDriverFactory::ConfigureLogger()
{
el::Loggers::getLogger(INPUT_LOGGER);
//TODO may configure?
el::Configurations conf;
conf.setToDefault();
conf.set(el::Level::Global, el::ConfigurationType::Filename, DRIVER_LOG_FILE);
conf.set(el::Level::Global, el::ConfigurationType::Format, "%datetime [%level] [%func] : %msg");
el::Loggers::reconfigureLogger(INPUT_LOGGER, conf);
}
};
return std::shared_ptr<InputDriver>(new InputDriver(event_notifier, detector));
}
Detector* InputDriverFactory::get_detector(std::string& input_config_path, std::string& matrix_config_path)
{
std::ifstream matrix_config_stream(matrix_config_path);
json matrix_config;
matrix_config_stream >> matrix_config;
auto input_gpio_interface = new InputGPIOInterface(input_config_path);
auto input_notifier = new EventNotifier();
std::map<char, Event> input_events = create_input_events(matrix_config);
return new Detector(input_gpio_interface, input_events, input_notifier);
}
std::map<char, Event> InputDriverFactory::create_input_events(json matrix_config)
{
std::map<char, Event> events;
for(auto& json_event : matrix_config)
{
try
{
std::string name = json_event.at("name");
char address = json_event.at("address").get<json::number_integer_t>();
int priority = json_event.at("priority").get<json::number_integer_t>();
Event event(address, priority, name);
events.emplace(address, event);
}
catch(json::exception& e)
{
CLOG(ERROR, INPUT_LOGGER) << "Matrix config-file corrupted: " << e.what();
exit(EXIT_FAILURE);
}
}
return events;
}
void InputDriverFactory::ConfigureLogger()
{
el::Loggers::getLogger(INPUT_LOGGER);
//TODO may configure?
el::Configurations conf;
conf.setToDefault();
conf.set(el::Level::Global, el::ConfigurationType::Filename, DRIVER_LOG_FILE);
conf.set(el::Level::Global, el::ConfigurationType::Format, "%datetime [%level] [%func] : %msg");
el::Loggers::reconfigureLogger(INPUT_LOGGER, conf);
}

View File

@@ -9,8 +9,10 @@
#define INPUTFACTORY_H_
#include <fstream>
#include <atomic>
#include "IDetector.h"
#include "Detector.h"
#include "InputDriver.h"
#include "../utilities/InputGPIOInterface.h"
#include "../utilities/config.h"
@@ -18,21 +20,21 @@
#include "../lib/easylogging/easylogging++.h"
#include "IEventNotifier.h"
using namespace nlohmann;
INITIALIZE_EASYLOGGINGPP
namespace Input
{
class InputFactory
class InputDriverFactory
{
public:
static shared_ptr<IInputDriver*> get_InputDriver(std::string& input_config_path, std::string& matrix_config_path);
static std::shared_ptr<InputDriver> get_InputDriver(std::string& input_config_path, std::string& matrix_config_path);
private:
static IDetector* get_detector(std::string& input_config_path, std::string& matrix_config_path);
static std::map<char, Event> create_input_events(json matrix_config);
static Detector* get_detector(std::string& input_config_path, std::string& matrix_config_path);
static std::map<char, Event> create_input_events(nlohmann::json matrix_config);
static void ConfigureLogger();
};
}
#endif /* INPUTFACTORY_H_ */

View File

@@ -1,24 +0,0 @@
/*
* EventHandler.cpp
*
* Created on: Jun 14, 2018
* Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert, Rafael Vinci, Dr. Franca Rupprecht
*/
#include "EventHandler.h"
namespace output
{
EventHandler::EventHandler ()
{
// TODO Auto-generated constructor stub
}
EventHandler::~EventHandler ()
{
// TODO Auto-generated destructor stub
}
} /* namespace output */

View File

@@ -1,28 +0,0 @@
/*
* EventHandler.h
*
* Created on: Jun 14, 2018
* Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert, Rafael Vinci, Dr. Franca Rupprecht
*/
#include "../utilities/IEventHandler.h"
#ifndef SRC_OUTPUT_EVENTHANDLER_H_
#define SRC_OUTPUT_EVENTHANDLER_H_
namespace output
{
class EventHandler : public IEventHandler
{
public:
EventHandler();
virtual ~EventHandler();
virtual void handle() override;
};
} /* namespace output */
#endif /* SRC_OUTPUT_EVENTHANDLER_H_ */

View File

@@ -1,29 +0,0 @@
/*
* BlockingQueue.cpp
*
* Created on: Jun 15, 2018
* Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert, Rafael Vinci, Dr. Franca Rupprecht
*/
#include "BlockingQueue.h"
template <typename T>
void BlockingQueue<T>::push(T const& value)
{
std::unique_lock<std::mutex> lock(this->d_mutex);
p_queue.push(value);
this->d_condition.notify_one();
}
template <typename T>
T BlockingQueue<T>::pop()
{
std::unique_lock<std::mutex> lock(this->d_mutex);
this->d_condition.wait(lock, [=]{ return !this->p_queue.empty(); });
T rc = *this->p_queue.end();
this->p_queue.pop();
return rc;
}

View File

@@ -1,33 +0,0 @@
/*
* BlockingQueue.hpp
*
* Created on: May 17, 2018
* Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert, Rafael Vinci, Dr. Franca Rupprecht
*/
#ifndef SRC_UTILITIES_BLOCKINGQUEUE_H_
#define SRC_UTILITIES_BLOCKINGQUEUE_H_
#include <mutex>
#include <condition_variable>
#include <boost/heap/priority_queue.hpp>
using namespace boost;
template <typename T>
class BlockingQueue
{
private:
std::mutex d_mutex;
std::condition_variable d_condition;
heap::priority_queue<T, heap::stable<true>> p_queue;
public:
void push(T const& value);
T pop();
};
#endif /* SRC_UTILITIES_BLOCKINGQUEUE_H_ */

View File

@@ -0,0 +1,45 @@
/*
* BlockingQueue.hpp
*
* Created on: May 17, 2018
* Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert, Rafael Vinci, Dr. Franca Rupprecht
*/
#ifndef SRC_UTILITIES_BLOCKINGQUEUE_HPP_
#define SRC_UTILITIES_BLOCKINGQUEUE_HPP_
#include <mutex>
#include <condition_variable>
#include <boost/heap/priority_queue.hpp>
using namespace boost;
template <typename T>
class BlockingQueue
{
private:
std::mutex d_mutex;
std::condition_variable d_condition;
heap::priority_queue<T, heap::stable<true>> p_queue;
public:
void push(T const& value)
{
std::unique_lock<std::mutex> lock(this->d_mutex);
p_queue.push(value);
this->d_condition.notify_one();
}
T pop()
{
std::unique_lock<std::mutex> lock(this->d_mutex);
this->d_condition.wait(lock, [=]{ return !this->p_queue.empty(); });
T rc = *this->p_queue.end();
this->p_queue.pop();
return rc;
}
};
#endif /* SRC_UTILITIES_BLOCKINGQUEUE_HPP_ */

View File

@@ -7,17 +7,17 @@
#include "GPIOInterface.h"
#include "../lib/wiringPi/wiringPi.h"
//#include "../lib/wiringPi/wiringPi.h"
#include "../lib/json/json.hpp"
static void GPIOInterface::write_pin(char address, char data)
void GPIOInterface::write_pin(char address, char data)
{
digitalWrite(address, data);
//digitalWrite(address, data);
}
static bool GPIOInterface::read_pin(char address)
bool GPIOInterface::read_pin(char address)
{
return digitalRead(address);
//return digitalRead(address);
}

View File

@@ -19,8 +19,7 @@
class GPIOInterface
{
public:
GPIOInterface();
virtual ~GPIOInterface();
virtual ~GPIOInterface() {};
static void write_pin(char address, char data);

View File

@@ -12,7 +12,7 @@
#include "../../src/input/InputDriver.h"
#include "../../src/input/Event.hpp"
#include "../../src/input/Event.h"
#include "../../src/input/IEventNotifier.h"
#include "../../src/input/IDetector.h"
@@ -26,7 +26,7 @@ SCENARIO("An InputDriver gets created", "[construction}")
Mock<Input::IDetector> detector_mock;
Mock<Input::IEventNotifier> event_notifier_mock;
Mock<Input::IEventHandler> event_handler_mock;
Mock<IEventHandler> event_handler_mock;
WHEN("The InputDriver gets created")
{
@@ -40,13 +40,13 @@ SCENARIO("An InputDriver gets created", "[construction}")
}
}
SCENARIO("An EventHandler [un]registers at the driver", "[[un]register]")
SCENARIO("An EventHandler [un]registers at the driver", "[un-register]")
{
GIVEN("An InputDriver, EventHandler and an EventNotifier")
{
Mock<Input::IDetector> detector_mock;
Mock<Input::IEventHandler> event_handler_mock;
Mock<IEventHandler> event_handler_mock;
Mock<Input::IEventNotifier> event_notifier_mock;
When(Method(event_notifier_mock, register_event_handler));
When(Method(event_notifier_mock, unregister_event_handler));