From 157b1ceb51248abb12efe8738d6e84cd1576c73d Mon Sep 17 00:00:00 2001 From: JDierkse Date: Sat, 7 Mar 2020 14:46:26 +0100 Subject: [PATCH] Update to use Libraries and fix include order --- .gitmodules | 3 + Application/PresenceDetection.cc | 40 +- Application/Test.cc | 21 +- Bluetooth/Device.cpp | 24 +- Bluetooth/Device.h | 14 +- Bluetooth/Functions.cpp | 15 +- Bluetooth/Socket.cpp | 5 +- Libraries/Http | 1 + Libraries/Logging | 1 + Libraries/Timer | 1 + Libraries/Utilities | 1 + Makefile | 235 +----------- Makefile.conf | 25 ++ Makefile.conf.armv6 | 14 - Makefile.conf.armv8 | 14 - Makefile.conf.base | 18 - Makefile.conf.x86-64 | 10 - Makefiles | 1 + UniFi/Device.cpp | 29 +- UniFi/Device.h | 12 +- Util/Device.cpp | 2 +- Util/Helpers.h | 36 -- Util/HttpClient.cpp | 630 ------------------------------- Util/HttpClient.h | 42 --- Util/HttpClientHelpers.h | 46 --- Util/Logger.cpp | 20 - Util/Logger.h | 37 -- Util/Timer.cpp | 59 --- Util/Timer.h | 106 ------ Util/Util.cpp | 25 -- Util/Util.h | 15 - Util/clipp.h | 17 - 32 files changed, 114 insertions(+), 1405 deletions(-) create mode 100644 .gitmodules create mode 120000 Libraries/Http create mode 120000 Libraries/Logging create mode 120000 Libraries/Timer create mode 120000 Libraries/Utilities mode change 100644 => 120000 Makefile create mode 100644 Makefile.conf delete mode 100644 Makefile.conf.armv6 delete mode 100644 Makefile.conf.armv8 delete mode 100644 Makefile.conf.base delete mode 100644 Makefile.conf.x86-64 create mode 160000 Makefiles delete mode 100644 Util/Helpers.h delete mode 100644 Util/HttpClient.cpp delete mode 100644 Util/HttpClient.h delete mode 100644 Util/HttpClientHelpers.h delete mode 100644 Util/Logger.cpp delete mode 100644 Util/Logger.h delete mode 100644 Util/Timer.cpp delete mode 100644 Util/Timer.h delete mode 100644 Util/Util.cpp delete mode 100644 Util/Util.h delete mode 100644 Util/clipp.h diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..7da2786 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "Makefiles"] + path = Makefiles + url = https://gogs.dierkse.nl/JDierkse/Makefiles.git diff --git a/Application/PresenceDetection.cc b/Application/PresenceDetection.cc index 8965725..060f69e 100644 --- a/Application/PresenceDetection.cc +++ b/Application/PresenceDetection.cc @@ -1,34 +1,34 @@ +#include "Bluetooth/Device.h" +#include "UniFi/Device.h" +#include "Util/Device.h" +#include "Util/INIh.h" +#include +#include +#include +#include +#include #include #include #include #include -#include #include -#include -#include -#include "Util/clipp.h" -#include "Util/Device.h" -#include "Util/INIh.h" -#include "Util/Logger.h" -#include "UniFi/Device.h" -#include "Bluetooth/Device.h" int main(int argc, char** argv) { try { - setlogmask(LOG_UPTO(LOG_DEBUG)); - openlog("PresenceDetection", LOG_NDELAY | LOG_PID, LOG_USER); + Logging::OpenLog(); + Logging::SetLogMask(Logging::Severity::Debug); bool daemon = false; - PresenceDetection::Util::clipp::group cli { - PresenceDetection::Util::clipp::option("-d", "--daemon").set(daemon).doc("Run the process as Daemon") + clipp::group cli { + clipp::option("-d", "--daemon").set(daemon).doc("Run the process as Daemon") }; - if (!PresenceDetection::Util::clipp::parse(argc, argv, cli)) + if (!clipp::parse(argc, argv, cli)) { - std::cout << PresenceDetection::Util::clipp::make_man_page(cli, argv[0]) << std::endl; + std::cout << clipp::make_man_page(cli, argv[0]) << std::endl; return 1; } @@ -41,7 +41,7 @@ int main(int argc, char** argv) if (daemon) { - PresenceDetection::Util::Logger::Log(PresenceDetection::Util::Logger::Severity::Info, "Starting Daemon"); + Logging::Log(Logging::Severity::Info, "Starting Daemon"); pid_t pid, sid; pid = fork(); @@ -164,7 +164,7 @@ int main(int argc, char** argv) int sig; sigwait(&wset,&sig); - PresenceDetection::Util::Logger::Log(PresenceDetection::Util::Logger::Severity::Info, "Stopping..."); + Logging::Log(Logging::Severity::Info, "Stopping..."); if (pUniFiDevice) pUniFiDevice->Stop(); @@ -178,7 +178,7 @@ int main(int argc, char** argv) if (pBluetoothDevice) pBluetoothDevice->Wait(); - closelog(); + Logging::CloseLog(); } catch (const std::exception& e) { @@ -186,9 +186,9 @@ int main(int argc, char** argv) ss << "ERROR: " << e.what() << std::endl; std::cerr << ss.str() << std::endl; - PresenceDetection::Util::Logger::Log(PresenceDetection::Util::Logger::Severity::Error, ss.str()); + Logging::Log(Logging::Severity::Error, ss.str()); - closelog(); + Logging::CloseLog(); return -1; } diff --git a/Application/Test.cc b/Application/Test.cc index b49d7a3..bda1986 100644 --- a/Application/Test.cc +++ b/Application/Test.cc @@ -1,14 +1,12 @@ -#include -#include - -#include -#include +#include "Util/INIh.h" +#include #include +#include #include #include - -#include "Util/Timer.h" -#include "Util/INIh.h" +#include +#include +#include void print() @@ -18,7 +16,7 @@ void print() void TestTimerAbort() { - PresenceDetection::Util::Timer timer; + Timer::Timer timer; std::function f_print = print; std::cout << "---" << std::endl; @@ -51,7 +49,7 @@ void TestTimerAbort() std::cout << "---" << std::endl << std::endl; } -typedef std::unique_ptr Pointer; +typedef std::unique_ptr Pointer; template std::unique_ptr make_unique(Args&&... args) { @@ -70,7 +68,7 @@ void StartNewTimer(std::deque& timers) int id(10); // Protect! - auto timer = make_unique(); + auto timer = make_unique(); timer->StartSingle(delay, static_cast>(std::bind(&OnTimerEnd, std::placeholders::_1, std::placeholders::_2)), identifier, id); timers.push_back(std::move(timer)); } @@ -123,4 +121,3 @@ int main(int /*argc*/, char** /*argv[]*/) return 0; } - diff --git a/Bluetooth/Device.cpp b/Bluetooth/Device.cpp index 4e72869..bd409e1 100644 --- a/Bluetooth/Device.cpp +++ b/Bluetooth/Device.cpp @@ -1,9 +1,9 @@ +#include "Device.h" +#include "Functions.h" +#include "Util/JSON.h" +#include #include #include -#include "Util/JSON.h" -#include "Util/Logger.h" -#include "Functions.h" -#include "Device.h" namespace PresenceDetection { @@ -62,7 +62,7 @@ void Device::ClearDevices() { std::stringstream ss; ss << "Bluetooth::Device::ClearDevices() - Error: " << e.what() << std::endl; - Util::Logger::Log(Util::Logger::Severity::Error, ss.str()); + Logging::Log(Logging::Severity::Error, ss.str()); } } @@ -78,7 +78,7 @@ void Device::ClearDevices() { std::stringstream ss; ss << "Bluetooth::Device::ClearDevices() - Error: " << e.what() << std::endl; - Util::Logger::Log(Util::Logger::Severity::Error, ss.str()); + Logging::Log(Logging::Severity::Error, ss.str()); } } } @@ -104,7 +104,7 @@ void Device::UpdateDevicesFromInventory() { std::stringstream ss; ss << "Bluetooth::Device::GetDevicesFromInventory() - Error: " << e.what() << std::endl; - Util::Logger::Log(Util::Logger::Severity::Error, ss.str()); + Logging::Log(Logging::Severity::Error, ss.str()); } } @@ -147,7 +147,7 @@ void Device::UpdatePresentDevices(bool updateOnlineDevices) { std::stringstream ss; ss << "Bluetooth::Device::UpdatePresentDevices() - Error: " << e.what() << std::endl; - Util::Logger::Log(Util::Logger::Severity::Error, ss.str()); + Logging::Log(Logging::Severity::Error, ss.str()); } } @@ -177,7 +177,7 @@ void Device::UpdatePresentDevices(bool updateOnlineDevices) { std::stringstream ss; ss << "Bluetooth::Device::UpdatePresentDevices() - Error: " << e.what() << std::endl; - Util::Logger::Log(Util::Logger::Severity::Error, ss.str()); + Logging::Log(Logging::Severity::Error, ss.str()); } } } @@ -204,7 +204,7 @@ void Device::SendStateChange(bool present, const std::string& macAddress) std::stringstream ss; ss << "Bluetooth: " << sign << " " << macAddress; - Util::Logger::Log(Util::Logger::Severity::Info, ss.str()); + Logging::Log(Logging::Severity::Info, ss.str()); if (!m_target.empty()) { @@ -219,7 +219,7 @@ void Device::SendStateChange(bool present, const std::string& macAddress) { std::stringstream ss; ss << "Bluetooth::Device::SendStateChange() - Error: " << e.what() << std::endl; - Util::Logger::Log(Util::Logger::Severity::Error, ss.str()); + Logging::Log(Logging::Severity::Error, ss.str()); } } @@ -256,7 +256,7 @@ void Device::SendStateChange(bool present, const std::string& macAddress) { std::stringstream ss; ss << "Bluetooth::Device::SendStateChange() - Error: " << e.what() << std::endl; - Util::Logger::Log(Util::Logger::Severity::Error, ss.str()); + Logging::Log(Logging::Severity::Error, ss.str()); } } } diff --git a/Bluetooth/Device.h b/Bluetooth/Device.h index a329e94..6e199d9 100644 --- a/Bluetooth/Device.h +++ b/Bluetooth/Device.h @@ -1,11 +1,11 @@ #ifndef BLUETOOTH_DEVICE_H #define BLUETOOTH_DEVICE_H +#include "Util/Device.h" +#include +#include #include #include -#include "Util/Device.h" -#include "Util/Timer.h" -#include "Util/HttpClient.h" namespace PresenceDetection { @@ -30,10 +30,10 @@ private: void SendStateChange(bool present, const std::string& macAddress); private: - Util::Timer m_onlineDeviceTimer; - Util::Timer m_offlineDeviceTimer; - Util::Timer m_inventoryTimer; - Util::HttpClient m_httpClient; + Timer::Timer m_onlineDeviceTimer; + Timer::Timer m_offlineDeviceTimer; + Timer::Timer m_inventoryTimer; + Http::HttpClient m_httpClient; std::vector m_devices; diff --git a/Bluetooth/Functions.cpp b/Bluetooth/Functions.cpp index 54db8ab..77048f8 100644 --- a/Bluetooth/Functions.cpp +++ b/Bluetooth/Functions.cpp @@ -1,15 +1,14 @@ -#include -#include -#include +#include "Functions.h" +#include "Socket.h" +#include +#include #include #include #include - -#include -#include #include -#include "Socket.h" -#include "Functions.h" +#include +#include +#include namespace PresenceDetection { diff --git a/Bluetooth/Socket.cpp b/Bluetooth/Socket.cpp index fd641e0..99e6459 100644 --- a/Bluetooth/Socket.cpp +++ b/Bluetooth/Socket.cpp @@ -1,8 +1,8 @@ -#include +#include "Socket.h" #include #include #include -#include "Socket.h" +#include namespace PresenceDetection { @@ -27,4 +27,3 @@ int Socket::Descriptor() const } // namespace Bluetooth } // namespace PresenceDetection - diff --git a/Libraries/Http b/Libraries/Http new file mode 120000 index 0000000..5fe3319 --- /dev/null +++ b/Libraries/Http @@ -0,0 +1 @@ +../../Libraries/Http \ No newline at end of file diff --git a/Libraries/Logging b/Libraries/Logging new file mode 120000 index 0000000..2b4f659 --- /dev/null +++ b/Libraries/Logging @@ -0,0 +1 @@ +../../Libraries/Logging \ No newline at end of file diff --git a/Libraries/Timer b/Libraries/Timer new file mode 120000 index 0000000..c0ddbc9 --- /dev/null +++ b/Libraries/Timer @@ -0,0 +1 @@ +../../Libraries/Timer \ No newline at end of file diff --git a/Libraries/Utilities b/Libraries/Utilities new file mode 120000 index 0000000..88f16bc --- /dev/null +++ b/Libraries/Utilities @@ -0,0 +1 @@ +../../Libraries/Utilities \ No newline at end of file diff --git a/Makefile b/Makefile deleted file mode 100644 index e9b8bc0..0000000 --- a/Makefile +++ /dev/null @@ -1,234 +0,0 @@ -# -# Makefile -# - -ROOTPATH := /opt/build/PresenceDetection -COMMANDLINE := $(shell ps -o args= $$PPID) - -ARCHS := $(shell ls $(ROOTPATH)/Makefile.conf.* | grep -v base | sed 's/.*Makefile\.conf\.//g') -ARCHS += any -ARCH := $(filter $(ARCHS),$(MAKECMDGOALS)) -MAKECMDGOALS := $(filter-out $(ARCH),$(MAKECMDGOALS)) -ARCHS := $(filter-out any,$(ARCHS)) - -ifeq ($(ARCH),) -ARCH := x86-64 -endif - -ifeq (,$(findstring -S,$(COMMANDLINE))) -ifneq ($(ARCH),) -$(info ARCH: $(ARCH)) -endif -endif - --include $(ROOTPATH)/Makefile.conf.$(ARCH) --include $(ROOTPATH)/Makefile.conf.base - -PID := $(shell echo $$$$) -PREFIX = nice -n 19 /usr/bin/time --format='%E (%U)' -o /dev/shm/t-$(PID) -POSTFIX = && cat /dev/shm/t-$(PID) | xargs -I "%" echo -en '\033[1A\033[100D\033[60C' "%\n" && rm -f /dev/shm/t-$(PID) - -ARNAME = $(notdir $(CURDIR)).a.$(ARCH) - -SUBDIRS := $(wildcard */.) -SUBDIRS := $(foreach dir,$(SUBDIRS),$(subst /.,,$(dir))) -FILTER = crash% -SUBDIRS := $(filter-out $(FILTER),$(SUBDIRS)) -FILTER = doc% -SUBDIRS := $(filter-out $(FILTER),$(SUBDIRS)) -FILTER = Libraries% -SUBDIRS := $(filter-out $(FILTER),$(SUBDIRS)) -SUBDIRARS = $(foreach dir,$(SUBDIRS),$(dir)/$(dir).a) - -LOCALSOURCES = $(wildcard *.cpp *.cc) -LOCALOBJECTS := $(LOCALSOURCES:.cpp=.o.$(ARCH)) -LOCALOBJECTS := $(LOCALOBJECTS:.cc=.o.$(ARCH)) -LOCALDEPENDS = $(LOCALOBJECTS:.o.$(ARCH)=.d.$(ARCH)) -SOURCES = $(wildcard *.cpp */*.cpp */*/*.cpp */*/*/*.cpp) -OBJECTS = $(SOURCES:.cpp=.o.$(ARCH)) -DEPENDS = $(OBJECTS:.o.$(ARCH)=.d.$(ARCH)) -TARGETS = - -define build_target_arch - $(eval $@_DATETIME := $(DATETIME)) - @echo -n " [LD] $(1)\n" - @$(PREFIX) $(CC) -o $(1) $(2) $(LFLAGS) $(CFLAGS) $(POSTFIX) - @$(OBJCOPY) --only-keep-debug $(1) $(DEBUGDIR)/$(1)-$($@_DATETIME).debug - @$(STRIP) $(SFLAGS) $(1) - @$(OBJCOPY) --add-gnu-debuglink="$(DEBUGDIR)/$(1)-$($@_DATETIME).debug" $(1) - @chmod -x $(DEBUGDIR)/$(1)-$($@_DATETIME).debug -endef - -define build_target - @if [ "$(ARCH)" = "any" ]; then \ - for arch in $(ARCHS); do \ - echo "ARCH: $$arch"; \ - $(MAKE) -s -S $(1) $$arch; \ - done; \ - true; \ - else \ - $(MAKE) -s -S $(1).$(ARCH) $(ARCH); \ - fi -endef - -$(LOCALOBJECTS): | $(SUBDIRS) -$(OBJECTS): | $(SUBDIRS) - -artifacts: $(LOCALOBJECTS) - -.DEFAULT_GOAL := artifacts - --include Makefile.target - -ifneq ($(MAKECMDGOALS),clean) --include $(LOCALDEPENDS) -endif - -%.d.$(ARCH): %.cpp - @if [ "$(ARCH)" != "any" ]; then \ - depth=$(MAKELEVEL); \ - while [ $${depth} -gt 0 ] ; do \ - echo -n " "; \ - depth=`expr $$depth - 1`; \ - done; \ - true; \ - echo -n " [MM] $@\n"; \ - $(PREFIX) $(CC) $(CFLAGS) $< -MM -MT $(@:.d.$(ARCH)=.o.$(ARCH)) >$@ $(POSTFIX); \ - fi - -%.o.$(ARCH): %.cpp - @depth=$(MAKELEVEL); \ - while [ $${depth} -gt 0 ] ; do \ - echo -n " "; \ - depth=`expr $$depth - 1`; \ - done; \ - true - @echo -n " [CC] $@\n" - @$(PREFIX) $(CC) -c $(CFLAGS) $< -o $@ $(POSTFIX) - -%.d.$(ARCH): %.cc - @if [ "$(ARCH)" != "any" ]; then \ - depth=$(MAKELEVEL); \ - while [ $${depth} -gt 0 ] ; do \ - echo -n " "; \ - depth=`expr $$depth - 1`; \ - done; \ - true; \ - echo -n " [MM] $@\n"; \ - $(PREFIX) $(CC) $(CFLAGS) $< -MM -MT $(@:.d.$(ARCH)=.o.$(ARCH)) >$@ $(POSTFIX); \ - fi - -%.o.$(ARCH): %.cc - @depth=$(MAKELEVEL); \ - while [ $${depth} -gt 0 ] ; do \ - echo -n " "; \ - depth=`expr $$depth - 1`; \ - done; \ - true - @echo -n " [CC] $@\n" - @$(PREFIX) $(CC) -c $(CFLAGS) $< -o $@ $(POSTFIX) - -$(ARNAME): $(SUBDIRS) $(LOCALOBJECTS) - @depth=$(MAKELEVEL); \ - while [ $${depth} -gt 0 ] ; do \ - echo -n " "; \ - depth=`expr $$depth - 1`; \ - done; \ - true - @echo -n " [AR] $@\n" - @$(PREFIX) $(AR) rcuT $@ $(LOCALOBJECTS) $(SUBDIRARS) $(POSTFIX) - -.PHONY: $(SUBDIRS) -$(SUBDIRS): - @depth=$(MAKELEVEL); \ - while [ $${depth} -gt 0 ] ; do \ - echo -n " "; \ - depth=`expr $$depth - 1`; \ - done; \ - true - @echo " <$@>" - @$(MAKE) -s -S -C $@ artifacts $(ARCH) - @depth=$(MAKELEVEL); \ - while [ $${depth} -gt 0 ] ; do \ - echo -n " "; \ - depth=`expr $$depth - 1`; \ - done; \ - true - @echo " " - -.PHONY: all -all: $(TARGETS) - -.PHONY: clean -clean: - @if [ "$(ARCH)" = "any" ]; then \ - for arch in $(ARCHS); do \ - echo "ARCH: $$arch"; \ - $(MAKE) -s -S $(MAKECMDGOALS) $$arch; \ - done; \ - true; \ - else \ - for dir in $(SUBDIRS); do \ - depth=$(MAKELEVEL); \ - while [ $${depth} -gt 0 ] ; do \ - echo -n " "; \ - depth=`expr $$depth - 1`; \ - done; \ - echo " <$$dir>"; \ - $(MAKE) -s -S -C $$dir $@ $(ARCH); \ - while [ $${depth} -gt 0 ] ; do \ - echo -n " "; \ - depth=`expr $$depth - 1`; \ - done; \ - echo " "; \ - done; \ - for target in $(TARGETS); do \ - rm -f $$target.$(ARCH); \ - done; \ - true; \ - fi - @rm -f $(LOCALOBJECTS) $(LOCALDEPENDS) $(ARNAME) || true - -.PHONY: cleandeps -cleandeps: - @if [ "$(ARCH)" = "any" ]; then \ - for arch in $(ARCHS); do \ - echo "ARCH: $$arch"; \ - $(MAKE) -s -S $(MAKECMDGOALS) $$arch; \ - done; \ - true; \ - else \ - for dir in $(SUBDIRS); do \ - depth=$(MAKELEVEL); \ - while [ $${depth} -gt 0 ] ; do \ - echo -n " "; \ - depth=`expr $$depth - 1`; \ - done; \ - echo " <$$dir>"; \ - $(MAKE) -s -S -C $$dir $@ $(ARCH); \ - while [ $${depth} -gt 0 ] ; do \ - echo -n " "; \ - depth=`expr $$depth - 1`; \ - done; \ - echo " "; \ - done; \ - for target in $(TARGETS); do \ - rm -f $$target.$(ARCH); \ - done; \ - true; \ - fi - @rm -f $(LOCALDEPENDS) || true - -.PHONY: $(ARCH) -$(ARCH): - @if [ "$(words $(MAKECMDGOALS))" = "0" ]; then \ - if [ "$(ARCH)" = "any" ]; then \ - for arch in $(ARCHS); do \ - echo "ARCH: $$arch"; \ - $(MAKE) -s -S $(.DEFAULT_GOAL) $$arch; \ - done; \ - true; \ - else \ - $(MAKE) -s -S $(.DEFAULT_GOAL) $(ARCH); \ - fi \ - fi diff --git a/Makefile b/Makefile new file mode 120000 index 0000000..6aa5187 --- /dev/null +++ b/Makefile @@ -0,0 +1 @@ +Makefiles/Makefile \ No newline at end of file diff --git a/Makefile.conf b/Makefile.conf new file mode 100644 index 0000000..683b1a4 --- /dev/null +++ b/Makefile.conf @@ -0,0 +1,25 @@ +# +# Makefile.conf +# + +LFLAGS += -lHttp +LFLAGS += -L$(ROOTPATH)/Libraries/Http/lib/$(ARCH) +CFLAGS += -I$(ROOTPATH)/Libraries/Http/include + +LFLAGS += -lTimer +LFLAGS += -L$(ROOTPATH)/Libraries/Timer/lib/$(ARCH) +CFLAGS += -I$(ROOTPATH)/Libraries/Timer/include + +LFLAGS += -lLogging +LFLAGS += -L$(ROOTPATH)/Libraries/Logging/lib/$(ARCH) +CFLAGS += -I$(ROOTPATH)/Libraries/Logging/include + +LFLAGS += -lUtilities +LFLAGS += -L$(ROOTPATH)/Libraries/Utilities/lib/$(ARCH) +CFLAGS += -I$(ROOTPATH)/Libraries/Utilities/include + +CFLAGS += -I$(ROOTPATH)/Libraries/clipp + +LFLAGS += -lpthread -lm -lssl -lcrypto -lcurl -lz -ldl -lbluetooth +CFLAGS += -I$(ROOTPATH) -I$(ROOTPATH)/Libraries -I$(ROOTPATH)/Libraries/JSON +DEBUGDIR := .debug diff --git a/Makefile.conf.armv6 b/Makefile.conf.armv6 deleted file mode 100644 index 366e552..0000000 --- a/Makefile.conf.armv6 +++ /dev/null @@ -1,14 +0,0 @@ -# -# Makefile.conf.armv6 -# - -ARCH := armv6 -CFLAGS := -marm -march=armv6 - -AR := /opt/build/rpi-armv6/tools/arm-bcm2708/arm-linux-gnueabihf/bin/arm-linux-gnueabihf-ar -CC := /opt/build/rpi-armv6/tools/arm-bcm2708/arm-linux-gnueabihf/bin/arm-linux-gnueabihf-g++ -STRIP := /opt/build/rpi-armv6/tools/arm-bcm2708/arm-linux-gnueabihf/bin/arm-linux-gnueabihf-strip -OBJCOPY := /opt/build/rpi-armv6/tools/arm-bcm2708/arm-linux-gnueabihf/bin/arm-linux-gnueabihf-objcopy - -CFLAGS += -I/opt/build/rpi-armv6/include -LFLAGS += -L/opt/build/rpi-armv6/lib --sysroot /opt/build/rpi-armv6/tools/arm-bcm2708/arm-linux-gnueabihf/arm-linux-gnueabihf/sysroot -Wl,-rpath,/opt/build/rpi-armv6/lib diff --git a/Makefile.conf.armv8 b/Makefile.conf.armv8 deleted file mode 100644 index 9106c75..0000000 --- a/Makefile.conf.armv8 +++ /dev/null @@ -1,14 +0,0 @@ -# -# Makefile.conf.armv8 -# - -ARCH := armv8 -CFLAGS := -marm -march=armv8-a - -AR := /opt/build/rpi-armv8/tools/arm-bcm2708/arm-linux-gnueabihf/bin/arm-linux-gnueabihf-ar -CC := /opt/build/rpi-armv8/tools/arm-bcm2708/arm-linux-gnueabihf/bin/arm-linux-gnueabihf-g++ -STRIP := /opt/build/rpi-armv8/tools/arm-bcm2708/arm-linux-gnueabihf/bin/arm-linux-gnueabihf-strip -OBJCOPY := /opt/build/rpi-armv8/tools/arm-bcm2708/arm-linux-gnueabihf/bin/arm-linux-gnueabihf-objcopy - -CFLAGS += -I/opt/build/rpi-armv8/include -LFLAGS += -L/opt/build/rpi-armv8/lib --sysroot /opt/build/rpi-armv8/tools/arm-bcm2708/arm-linux-gnueabihf/arm-linux-gnueabihf/sysroot -Wl,-rpath,/opt/build/rpi-armv8/lib diff --git a/Makefile.conf.base b/Makefile.conf.base deleted file mode 100644 index 7a63a0d..0000000 --- a/Makefile.conf.base +++ /dev/null @@ -1,18 +0,0 @@ -# -# Makefile.conf.base -# - -CFLAGS += -g3 -O3 -fPIC -std=c++11 -CFLAGS += -Wall -Wextra -Wstrict-aliasing -fmax-errors=5 -Werror -Wunreachable-code -Wdisabled-optimization -Wformat=2 -Winit-self -Wlogical-op -Wmissing-include-dirs -Wnoexcept -Woverloaded-virtual -Wsign-promo -Wstrict-null-sentinel -Wswitch-default -Wno-unused -Wno-variadic-macros -Wno-parentheses -fdiagnostics-show-option -# CFLAGS += -Wold-style-cast -pedantic -Wcast-qual -Wundef -Wshadow -Wctor-dtor-privacy -Wredundant-decls -Wstrict-overflow=5 -Wcast-align -LFLAGS += -lpthread -lm -lssl -lcrypto -lcurl -lz -ldl -lbluetooth - -CFLAGS += -fpermissive -CFLAGS += -I$(ROOTPATH) -I$(ROOTPATH)/Libraries/JSON - -CFLAGS += -ffunction-sections -fdata-sections -LFLAGS += -Wl,--gc-sections -SFLAGS := -s -R .comment -R .gnu.version --strip-unneeded - -DEBUGDIR := .debug -DATETIME := `date +'%y%m%d-%H%M'` diff --git a/Makefile.conf.x86-64 b/Makefile.conf.x86-64 deleted file mode 100644 index 8b6f107..0000000 --- a/Makefile.conf.x86-64 +++ /dev/null @@ -1,10 +0,0 @@ -# -# Makefile.conf.x86-64 -# - -ARCH := x86-64 - -AR := ar -CC := g++ -STRIP := strip -OBJCOPY := objcopy diff --git a/Makefiles b/Makefiles new file mode 160000 index 0000000..61894c4 --- /dev/null +++ b/Makefiles @@ -0,0 +1 @@ +Subproject commit 61894c4588eb2b85e342a6f923eeb7ac97c3fe64 diff --git a/UniFi/Device.cpp b/UniFi/Device.cpp index b259505..d2af0db 100644 --- a/UniFi/Device.cpp +++ b/UniFi/Device.cpp @@ -1,8 +1,8 @@ +#include "Device.h" +#include "Util/JSON.h" +#include #include #include -#include "Util/JSON.h" -#include "Util/Logger.h" -#include "Device.h" namespace PresenceDetection { @@ -63,8 +63,11 @@ bool Device::Login() try { + std::vector headers; + headers.push_back("Content-Type: application/json"); + std::stringstream output; - output << m_httpClient.GetUrlPostContents(url.str(), m_cookieFile, json.dump(), "application/json"); + output << m_httpClient.GetUrlPostContents(url.str(), headers, m_cookieFile, json.dump()); Util::JSON outputJSON = Util::JSON::parse(output); @@ -79,7 +82,7 @@ bool Device::Login() { std::stringstream ss; ss << "UniFi::Device::Login() - Error: " << e.what() << std::endl; - Util::Logger::Log(Util::Logger::Severity::Error, ss.str()); + Logging::Log(Logging::Severity::Error, ss.str()); std::lock_guard lock(m_mutex); m_loggedIn = false; return m_loggedIn; @@ -106,7 +109,7 @@ void Device::Logout() { std::stringstream ss; ss << "UniFi::Device::Logout() - Error: " << e.what() << std::endl; - Util::Logger::Log(Util::Logger::Severity::Error, ss.str()); + Logging::Log(Logging::Severity::Error, ss.str()); } } @@ -122,7 +125,7 @@ void Device::ClearDevices() { std::stringstream ss; ss << "UniFi::Device::ClearDevices() - Error: " << e.what() << std::endl; - Util::Logger::Log(Util::Logger::Severity::Error, ss.str()); + Logging::Log(Logging::Severity::Error, ss.str()); } } @@ -138,7 +141,7 @@ void Device::ClearDevices() { std::stringstream ss; ss << "UniFi::Device::ClearDevices() - Error: " << e.what() << std::endl; - Util::Logger::Log(Util::Logger::Severity::Error, ss.str()); + Logging::Log(Logging::Severity::Error, ss.str()); } } } @@ -164,7 +167,7 @@ void Device::UpdateDevicesFromInventory() { std::stringstream ss; ss << "UniFi::Device::GetDevicesFromInventory() - Error: " << e.what() << std::endl; - Util::Logger::Log(Util::Logger::Severity::Error, ss.str()); + Logging::Log(Logging::Severity::Error, ss.str()); } } @@ -236,7 +239,7 @@ void Device::UpdatePresentDevices() { std::stringstream ss; ss << "UniFi::Device::IsDevicePresent() - Error: " << e.what() << std::endl; - Util::Logger::Log(Util::Logger::Severity::Error, ss.str()); + Logging::Log(Logging::Severity::Error, ss.str()); Logout(); return; } @@ -264,7 +267,7 @@ void Device::SendStateChange(bool present, const std::string& macAddress) std::stringstream ss; ss << "UniFi: " << sign << " " << macAddress; - Util::Logger::Log(Util::Logger::Severity::Info, ss.str()); + Logging::Log(Logging::Severity::Info, ss.str()); if (!m_target.empty()) { @@ -279,7 +282,7 @@ void Device::SendStateChange(bool present, const std::string& macAddress) { std::stringstream ss; ss << "UniFi::Device::SendStateChange() - Error: " << e.what() << std::endl; - Util::Logger::Log(Util::Logger::Severity::Error, ss.str()); + Logging::Log(Logging::Severity::Error, ss.str()); } } @@ -316,7 +319,7 @@ void Device::SendStateChange(bool present, const std::string& macAddress) { std::stringstream ss; ss << "UniFi::Device::SendStateChange() - Error: " << e.what() << std::endl; - Util::Logger::Log(Util::Logger::Severity::Error, ss.str()); + Logging::Log(Logging::Severity::Error, ss.str()); } } } diff --git a/UniFi/Device.h b/UniFi/Device.h index 5df2f82..02242fc 100644 --- a/UniFi/Device.h +++ b/UniFi/Device.h @@ -1,12 +1,12 @@ #ifndef UNIFI_DEVICE_H #define UNIFI_DEVICE_H +#include "Util/Device.h" +#include +#include #include #include #include -#include "Util/Device.h" -#include "Util/Timer.h" -#include "Util/HttpClient.h" namespace PresenceDetection { @@ -32,9 +32,9 @@ private: void SendStateChange(bool present, const std::string& macAddress); private: - Util::Timer m_deviceTimer; - Util::Timer m_inventoryTimer; - Util::HttpClient m_httpClient; + Timer::Timer m_deviceTimer; + Timer::Timer m_inventoryTimer; + Http::HttpClient m_httpClient; std::mutex m_mutex; bool m_loggedIn; diff --git a/Util/Device.cpp b/Util/Device.cpp index 99fcea8..a6d3ebb 100644 --- a/Util/Device.cpp +++ b/Util/Device.cpp @@ -1,5 +1,5 @@ -#include #include "Device.h" +#include namespace PresenceDetection { diff --git a/Util/Helpers.h b/Util/Helpers.h deleted file mode 100644 index a062611..0000000 --- a/Util/Helpers.h +++ /dev/null @@ -1,36 +0,0 @@ -#ifndef UTIL_HELPERS_H -#define UTIL_HELPERS_H - -#include - - -namespace PresenceDetection { -namespace Util { -namespace Helpers { - -template -struct index {}; - -template -struct gen_seq : gen_seq {}; - -template -struct gen_seq<0, Is...> : index {}; - -template -void execute(std::function const & function, std::tuple& tuple, index) -{ - function(std::get(tuple)...); -} - -template -void execute(std::function const & function, std::tuple& tuple) -{ - execute(function, tuple, gen_seq{}); -} - -} // namespace Helpers -} // namespace Util -} // namespace PresenceDetection - -#endif // UTIL_HELPERS_H diff --git a/Util/HttpClient.cpp b/Util/HttpClient.cpp deleted file mode 100644 index df925a5..0000000 --- a/Util/HttpClient.cpp +++ /dev/null @@ -1,630 +0,0 @@ -#include -#include -#include -#include -#include -#include "HttpClientHelpers.h" -#include "HttpClient.h" - - -namespace PresenceDetection { -namespace Util { - -HttpClient::HttpClient() -{ - curl_global_init(CURL_GLOBAL_ALL); - - m_userAgents.push_back("Opera/9.80 (X11; Linux i686; U; en) Presto/2.7.62 Version/11.00"); - - InitializeLocks(); -} - -HttpClient::~HttpClient() -{ - FreeLocks(); - curl_global_cleanup(); -} - -std::string HttpClient::GetUrlContents(const std::string& url) const -{ - std::string buffer; - - CURL* curl = curl_easy_init(); - - curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); - curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1); - curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0); - curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0); - curl_easy_setopt(curl, CURLOPT_USERAGENT, m_userAgents[rand() % m_userAgents.size()].c_str()); - curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &WriteCallback); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer); - - int code = 0; - curl_easy_perform(curl); - curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code); - curl_easy_cleanup(curl); - - if (code != 200) - { - std::stringstream error; - error << "Error (" << code << ") encountered while retrieving " << url << "\n"; - error << "Data: " << buffer; - throw std::runtime_error(error.str()); - } - - return buffer; -} - -void HttpClient::GetUrlSilent(const std::string& url) const -{ - CURL* curl = curl_easy_init(); - - curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); - curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1); - curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0); - curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0); - curl_easy_setopt(curl, CURLOPT_USERAGENT, m_userAgents[rand() % m_userAgents.size()].c_str()); - curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &WriteCallback); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, nullptr); - - int code = 0; - curl_easy_perform(curl); - curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code); - curl_easy_cleanup(curl); - - if (code != 200) - { - std::stringstream error; - error << "Error (" << code << ") encountered while retrieving " << url << "\n"; - throw std::runtime_error(error.str()); - } -} - -std::string HttpClient::GetUrlContents(const std::string& url, const std::string& cookieFile) const -{ - std::string buffer; - - CURL* curl = curl_easy_init(); - - curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); - curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1); - curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0); - curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0); - curl_easy_setopt(curl, CURLOPT_USERAGENT, m_userAgents[rand() % m_userAgents.size()].c_str()); - curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookieFile.c_str()); - curl_easy_setopt(curl, CURLOPT_COOKIEJAR, cookieFile.c_str()); - curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &WriteCallback); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer); - - int code = 0; - curl_easy_perform(curl); - curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code); - curl_easy_cleanup(curl); - - if (code != 200) - { - std::stringstream error; - error << "Error (" << code << ") encountered while retrieving " << url << "\n"; - error << "Data: " << buffer; - throw std::runtime_error(error.str()); - } - - return buffer; -} - -void HttpClient::GetUrlSilent(const std::string& url, const std::string& cookieFile) const -{ - CURL* curl = curl_easy_init(); - - curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); - curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1); - curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0); - curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0); - curl_easy_setopt(curl, CURLOPT_USERAGENT, m_userAgents[rand() % m_userAgents.size()].c_str()); - curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookieFile.c_str()); - curl_easy_setopt(curl, CURLOPT_COOKIEJAR, cookieFile.c_str()); - curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &WriteCallback); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, nullptr); - - int code = 0; - curl_easy_perform(curl); - curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code); - curl_easy_cleanup(curl); - - if (code != 200) - { - std::stringstream error; - error << "Error (" << code << ") encountered while retrieving " << url << "\n"; - throw std::runtime_error(error.str()); - } -} - -std::string HttpClient::GetUrlPostContents(const std::string& url, const std::string& postData, const std::string& contentType) const -{ - std::string buffer; - - CURL* curl = curl_easy_init(); - - std::stringstream contentTypeString; - contentTypeString << "Content-Type: " << contentType; - - struct curl_slist *list = NULL; - list = curl_slist_append(list, contentTypeString.str().c_str()); - - curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); - curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1); - curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0); - curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0); - curl_easy_setopt(curl, CURLOPT_USERAGENT, m_userAgents[rand() % m_userAgents.size()].c_str()); - curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list); - curl_easy_setopt(curl, CURLOPT_POST, 1); - curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postData.c_str()); - curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, postData.size()); - curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &WriteCallback); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer); - - int code = 0; - curl_easy_perform(curl); - curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code); - curl_easy_cleanup(curl); - - if (code != 200) - { - std::stringstream error; - error << "Error (" << code << ") encountered while retrieving " << url << "\n"; - error << "Data: " << buffer; - throw std::runtime_error(error.str()); - } - - return buffer; -} - -void HttpClient::GetUrlPostSilent(const std::string& url, const std::string& postData, const std::string& contentType) const -{ - CURL* curl = curl_easy_init(); - - std::stringstream contentTypeString; - contentTypeString << "Content-Type: " << contentType; - - struct curl_slist *list = NULL; - list = curl_slist_append(list, contentTypeString.str().c_str()); - - curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); - curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1); - curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0); - curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0); - curl_easy_setopt(curl, CURLOPT_USERAGENT, m_userAgents[rand() % m_userAgents.size()].c_str()); - curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list); - curl_easy_setopt(curl, CURLOPT_POST, 1); - curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postData.c_str()); - curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, postData.size()); - curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &WriteCallback); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, nullptr); - - int code = 0; - curl_easy_perform(curl); - curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code); - curl_easy_cleanup(curl); - - if (code != 200) - { - std::stringstream error; - error << "Error (" << code << ") encountered while retrieving " << url << "\n"; - throw std::runtime_error(error.str()); - } -} - -std::string HttpClient::GetUrlPostContents(const std::string& url, const std::string& cookieFile, const std::string& postData, const std::string& contentType) const -{ - std::string buffer; - - CURL* curl = curl_easy_init(); - - std::stringstream contentTypeString; - contentTypeString << "Content-Type: " << contentType; - - struct curl_slist *list = NULL; - list = curl_slist_append(list, contentTypeString.str().c_str()); - - curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); - curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1); - curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0); - curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0); - curl_easy_setopt(curl, CURLOPT_USERAGENT, m_userAgents[rand() % m_userAgents.size()].c_str()); - curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list); - curl_easy_setopt(curl, CURLOPT_POST, 1); - curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postData.c_str()); - curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, postData.size()); - curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookieFile.c_str()); - curl_easy_setopt(curl, CURLOPT_COOKIEJAR, cookieFile.c_str()); - curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &WriteCallback); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer); - - int code = 0; - curl_easy_perform(curl); - curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code); - curl_easy_cleanup(curl); - - if (code != 200) - { - std::stringstream error; - error << "Error (" << code << ") encountered while retrieving " << url << "\n"; - error << "Data: " << buffer; - throw std::runtime_error(error.str()); - } - - return buffer; -} - -void HttpClient::GetUrlPostSilent(const std::string& url, const std::string& cookieFile, const std::string& postData, const std::string& contentType) const -{ - CURL* curl = curl_easy_init(); - - std::stringstream contentTypeString; - contentTypeString << "Content-Type: " << contentType; - - struct curl_slist *list = NULL; - list = curl_slist_append(list, contentTypeString.str().c_str()); - - curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); - curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1); - curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0); - curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0); - curl_easy_setopt(curl, CURLOPT_USERAGENT, m_userAgents[rand() % m_userAgents.size()].c_str()); - curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list); - curl_easy_setopt(curl, CURLOPT_POST, 1); - curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postData.c_str()); - curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, postData.size()); - curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookieFile.c_str()); - curl_easy_setopt(curl, CURLOPT_COOKIEJAR, cookieFile.c_str()); - curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &WriteCallback); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, nullptr); - - int code = 0; - curl_easy_perform(curl); - curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code); - curl_easy_cleanup(curl); - - if (code != 200) - { - std::stringstream error; - error << "Error (" << code << ") encountered while retrieving " << url << "\n"; - throw std::runtime_error(error.str()); - } -} - -std::string HttpClient::GetUrlPostAttachmentContents(const std::string& url, const std::string& postData, const std::string& filename, const std::string& fileFieldname) const -{ - std::string buffer; - std::string contents; - std::ifstream fileStream(filename, std::ios::in | std::ios::binary); - - if (fileStream) - { - fileStream.seekg(0, std::ios::end); - contents.resize(fileStream.tellg()); - fileStream.seekg(0, std::ios::beg); - fileStream.read(&contents[0], contents.size()); - fileStream.close(); - } - - CURL* curl = curl_easy_init(); - CURLcode result; - - struct curl_httppost *formpost = nullptr; - struct curl_httppost *lastptr = nullptr; - struct curl_slist *headerlist = nullptr; - static const char headerBuffer[] = "Expect:"; - - curl_global_init(CURL_GLOBAL_ALL); - - curl_formadd(&formpost, &lastptr, - CURLFORM_COPYNAME, "cache-control:", - CURLFORM_COPYCONTENTS, "no-cache", - CURLFORM_END); - - curl_formadd(&formpost, &lastptr, - CURLFORM_COPYNAME, "content-type:", - CURLFORM_COPYCONTENTS, "multipart/form-data", - CURLFORM_END); - - std::vector postTokens; - boost::split(postTokens, postData, boost::is_any_of("&")); - - for (std::vector::iterator it = postTokens.begin(); it != postTokens.end(); ++it) - { - std::vector tokens; - boost::split(tokens, *it, boost::is_any_of("=")); - - curl_formadd(&formpost, &lastptr, - CURLFORM_COPYNAME, tokens[0].c_str(), - CURLFORM_COPYCONTENTS, tokens[1].c_str(), - CURLFORM_END); - } - - curl_formadd(&formpost, &lastptr, - CURLFORM_COPYNAME, fileFieldname.c_str(), - CURLFORM_BUFFER, "data", - CURLFORM_BUFFERPTR, contents.data(), - CURLFORM_BUFFERLENGTH, contents.size(), - CURLFORM_END); - - headerlist = curl_slist_append(headerlist, headerBuffer); - - curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); - curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &WriteCallback); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer); - - int code = 0; - curl_easy_perform(curl); - curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code); - curl_easy_cleanup(curl); - curl_formfree(formpost); - curl_slist_free_all(headerlist); - - if (code != 200) - { - std::stringstream error; - error << "Error (" << code << ") encountered while retrieving " << url << "\n"; - error << "Data: " << buffer; - throw std::runtime_error(error.str()); - } - - return buffer; -} - -void HttpClient::GetUrlPostAttachmentSilent(const std::string& url, const std::string& postData, const std::string& filename, const std::string& fileFieldname) const -{ - std::string contents; - std::ifstream fileStream(filename, std::ios::in | std::ios::binary); - - if (fileStream) - { - fileStream.seekg(0, std::ios::end); - contents.resize(fileStream.tellg()); - fileStream.seekg(0, std::ios::beg); - fileStream.read(&contents[0], contents.size()); - fileStream.close(); - } - - CURL* curl = curl_easy_init(); - CURLcode result; - - struct curl_httppost *formpost = nullptr; - struct curl_httppost *lastptr = nullptr; - struct curl_slist *headerlist = nullptr; - static const char headerBuffer[] = "Expect:"; - - curl_global_init(CURL_GLOBAL_ALL); - - curl_formadd(&formpost, &lastptr, - CURLFORM_COPYNAME, "cache-control:", - CURLFORM_COPYCONTENTS, "no-cache", - CURLFORM_END); - - curl_formadd(&formpost, &lastptr, - CURLFORM_COPYNAME, "content-type:", - CURLFORM_COPYCONTENTS, "multipart/form-data", - CURLFORM_END); - - std::vector postTokens; - boost::split(postTokens, postData, boost::is_any_of("&")); - - for (std::vector::iterator it = postTokens.begin(); it != postTokens.end(); ++it) - { - std::vector tokens; - boost::split(tokens, *it, boost::is_any_of("=")); - - curl_formadd(&formpost, &lastptr, - CURLFORM_COPYNAME, tokens[0].c_str(), - CURLFORM_COPYCONTENTS, tokens[1].c_str(), - CURLFORM_END); - } - - curl_formadd(&formpost, &lastptr, - CURLFORM_COPYNAME, fileFieldname.c_str(), - CURLFORM_BUFFER, "data", - CURLFORM_BUFFERPTR, contents.data(), - CURLFORM_BUFFERLENGTH, contents.size(), - CURLFORM_END); - - headerlist = curl_slist_append(headerlist, headerBuffer); - - curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); - curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost); - - int code = 0; - curl_easy_perform(curl); - curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code); - curl_easy_cleanup(curl); - curl_formfree(formpost); - curl_slist_free_all(headerlist); - - if (code != 200) - { - std::stringstream error; - error << "Error (" << code << ") encountered while retrieving " << url << "\n"; - throw std::runtime_error(error.str()); - } -} - -std::string HttpClient::GetUrlPostAttachmentContents(const std::string& url, const std::string& cookieFile, const std::string& postData, const std::string& filename, const std::string& fileFieldname) const -{ - std::string buffer; - std::string contents; - std::ifstream fileStream(filename, std::ios::in | std::ios::binary); - - if (fileStream) - { - fileStream.seekg(0, std::ios::end); - contents.resize(fileStream.tellg()); - fileStream.seekg(0, std::ios::beg); - fileStream.read(&contents[0], contents.size()); - fileStream.close(); - } - - CURL* curl = curl_easy_init(); - CURLcode result; - - struct curl_httppost *formpost = nullptr; - struct curl_httppost *lastptr = nullptr; - struct curl_slist *headerlist = nullptr; - static const char headerBuffer[] = "Expect:"; - - curl_global_init(CURL_GLOBAL_ALL); - - curl_formadd(&formpost, &lastptr, - CURLFORM_COPYNAME, "cache-control:", - CURLFORM_COPYCONTENTS, "no-cache", - CURLFORM_END); - - curl_formadd(&formpost, &lastptr, - CURLFORM_COPYNAME, "content-type:", - CURLFORM_COPYCONTENTS, "multipart/form-data", - CURLFORM_END); - - std::vector postTokens; - boost::split(postTokens, postData, boost::is_any_of("&")); - - for (std::vector::iterator it = postTokens.begin(); it != postTokens.end(); ++it) - { - std::vector tokens; - boost::split(tokens, *it, boost::is_any_of("=")); - - curl_formadd(&formpost, &lastptr, - CURLFORM_COPYNAME, tokens[0].c_str(), - CURLFORM_COPYCONTENTS, tokens[1].c_str(), - CURLFORM_END); - } - - curl_formadd(&formpost, &lastptr, - CURLFORM_COPYNAME, fileFieldname.c_str(), - CURLFORM_BUFFER, "data", - CURLFORM_BUFFERPTR, contents.data(), - CURLFORM_BUFFERLENGTH, contents.size(), - CURLFORM_END); - - headerlist = curl_slist_append(headerlist, headerBuffer); - - curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); - curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost); - curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookieFile.c_str()); - curl_easy_setopt(curl, CURLOPT_COOKIEJAR, cookieFile.c_str()); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &WriteCallback); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer); - - int code = 0; - curl_easy_perform(curl); - curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code); - curl_easy_cleanup(curl); - curl_formfree(formpost); - curl_slist_free_all(headerlist); - - if (code != 200) - { - std::stringstream error; - error << "Error (" << code << ") encountered while retrieving " << url << "\n"; - error << "Data: " << buffer; - throw std::runtime_error(error.str()); - } - - return buffer; -} - -void HttpClient::GetUrlPostAttachmentSilent(const std::string& url, const std::string& cookieFile, const std::string& postData, const std::string& filename, const std::string& fileFieldname) const -{ - std::string contents; - std::ifstream fileStream(filename, std::ios::in | std::ios::binary); - - if (fileStream) - { - fileStream.seekg(0, std::ios::end); - contents.resize(fileStream.tellg()); - fileStream.seekg(0, std::ios::beg); - fileStream.read(&contents[0], contents.size()); - fileStream.close(); - } - - CURL* curl = curl_easy_init(); - CURLcode result; - - struct curl_httppost *formpost = nullptr; - struct curl_httppost *lastptr = nullptr; - struct curl_slist *headerlist = nullptr; - static const char headerBuffer[] = "Expect:"; - - curl_global_init(CURL_GLOBAL_ALL); - - curl_formadd(&formpost, &lastptr, - CURLFORM_COPYNAME, "cache-control:", - CURLFORM_COPYCONTENTS, "no-cache", - CURLFORM_END); - - curl_formadd(&formpost, &lastptr, - CURLFORM_COPYNAME, "content-type:", - CURLFORM_COPYCONTENTS, "multipart/form-data", - CURLFORM_END); - - std::vector postTokens; - boost::split(postTokens, postData, boost::is_any_of("&")); - - for (std::vector::iterator it = postTokens.begin(); it != postTokens.end(); ++it) - { - std::vector tokens; - boost::split(tokens, *it, boost::is_any_of("=")); - - curl_formadd(&formpost, &lastptr, - CURLFORM_COPYNAME, tokens[0].c_str(), - CURLFORM_COPYCONTENTS, tokens[1].c_str(), - CURLFORM_END); - } - - curl_formadd(&formpost, &lastptr, - CURLFORM_COPYNAME, fileFieldname.c_str(), - CURLFORM_BUFFER, "data", - CURLFORM_BUFFERPTR, contents.data(), - CURLFORM_BUFFERLENGTH, contents.size(), - CURLFORM_END); - - headerlist = curl_slist_append(headerlist, headerBuffer); - - curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); - curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost); - curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookieFile.c_str()); - curl_easy_setopt(curl, CURLOPT_COOKIEJAR, cookieFile.c_str()); - - int code = 0; - curl_easy_perform(curl); - curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code); - curl_easy_cleanup(curl); - curl_formfree(formpost); - curl_slist_free_all(headerlist); - - if (code != 200) - { - std::stringstream error; - error << "Error (" << code << ") encountered while retrieving " << url << "\n"; - throw std::runtime_error(error.str()); - } -} - -size_t HttpClient::WriteCallback(char* data, size_t size, size_t nmemb, std::string* writerData) -{ - if (writerData == nullptr) - return size * nmemb; - - writerData->append(data, size * nmemb); - return size * nmemb; -} - -} // namespace Util -} // namespace PresenceDetection diff --git a/Util/HttpClient.h b/Util/HttpClient.h deleted file mode 100644 index 710943f..0000000 --- a/Util/HttpClient.h +++ /dev/null @@ -1,42 +0,0 @@ -#ifndef UTIL_HTTPCLIENT_H -#define UTIL_HTTPCLIENT_H - -#include -#include - - -namespace PresenceDetection { -namespace Util { - -class HttpClient -{ -public: - HttpClient(); - ~HttpClient(); - - HttpClient(const HttpClient&) = delete; - - std::string GetUrlContents(const std::string& url) const; - void GetUrlSilent(const std::string& url) const; - std::string GetUrlContents(const std::string& url, const std::string& cookieFile) const; - void GetUrlSilent(const std::string& url, const std::string& cookieFile) const; - std::string GetUrlPostContents(const std::string& url, const std::string& postData, const std::string& contentType) const; - void GetUrlPostSilent(const std::string& url, const std::string& postData, const std::string& contentType) const; - std::string GetUrlPostContents(const std::string& url, const std::string& cookieFile, const std::string& postData, const std::string& contentType) const; - void GetUrlPostSilent(const std::string& url, const std::string& cookieFile, const std::string& postData, const std::string& contentType) const; - std::string GetUrlPostAttachmentContents(const std::string& url, const std::string& postData, const std::string& filename, const std::string& fileFieldname) const; - void GetUrlPostAttachmentSilent(const std::string& url, const std::string& postData, const std::string& filename, const std::string& fileFieldname) const; - std::string GetUrlPostAttachmentContents(const std::string& url, const std::string& cookieFile, const std::string& postData, const std::string& filename, const std::string& fileFieldname) const; - void GetUrlPostAttachmentSilent(const std::string& url, const std::string& cookieFile, const std::string& postData, const std::string& filename, const std::string& fileFieldname) const; - -private: - static size_t WriteCallback(char* data, size_t size, size_t nmemb, std::string* writerData); - -private: - std::vector m_userAgents; -}; - -} // namespace Util -} // namespace PresenceDetection - -#endif // UTIL_HTTPCLIENT_H diff --git a/Util/HttpClientHelpers.h b/Util/HttpClientHelpers.h deleted file mode 100644 index c2dd04c..0000000 --- a/Util/HttpClientHelpers.h +++ /dev/null @@ -1,46 +0,0 @@ -#ifndef UTIL_HTTPCLIENTHELPERS_H -#define UTIL_HTTPCLIENTHELPERS_H - -#include -#include -#include -#include - - -namespace PresenceDetection { -namespace Util { - -static std::vector > g_mutexes; - -static void LockCallback(int mode, int type, const char* /*file*/, int /*line*/) -{ - if (mode & CRYPTO_LOCK) - g_mutexes[type]->lock(); - else - g_mutexes[type]->unlock(); -} - -static unsigned long ThreadId(void) -{ - return (unsigned long)pthread_self(); -} - -static void InitializeLocks(void) -{ - g_mutexes.resize(CRYPTO_num_locks()); - for (int i = 0; i < CRYPTO_num_locks(); ++i) - g_mutexes[i] = std::shared_ptr(new std::mutex()); - - CRYPTO_set_id_callback(ThreadId); - CRYPTO_set_locking_callback(LockCallback); -} - -static void FreeLocks(void) -{ - CRYPTO_set_locking_callback(NULL); -} - -} // namespace Util -} // namespace PresenceDetection - -#endif // UTIL_HTTPCLIENTHELPERS_H diff --git a/Util/Logger.cpp b/Util/Logger.cpp deleted file mode 100644 index 48eeedc..0000000 --- a/Util/Logger.cpp +++ /dev/null @@ -1,20 +0,0 @@ -#include -#include -#include "Logger.h" - - -namespace PresenceDetection { -namespace Util { - -void Logger::Log(Severity::type severity, const std::string& message) -{ - if (severity > Severity::Error) - std::cout << message << std::endl; - else - std::cerr << message << std::endl; - - syslog(static_cast(severity), "%s", message.c_str()); -} - -} // namespace Util -} // namespace PresenceDetection diff --git a/Util/Logger.h b/Util/Logger.h deleted file mode 100644 index b807d07..0000000 --- a/Util/Logger.h +++ /dev/null @@ -1,37 +0,0 @@ -#ifndef UTIL_LOGGER_H -#define UTIL_LOGGER_H - -#include - - -namespace PresenceDetection { -namespace Util { - -class Logger -{ -public: - class Severity - { - public: - enum type - { - Emergency = 0, - Alert = 1, - Critical = 2, - Error = 3, - Warning = 4, - Notice = 5, - Info = 6, - Debug = 7 - }; - }; - - -public: - static void Log(Severity::type severity, const std::string& message); -}; - -} // namespace Util -} // namespace PresenceDetection - -#endif // UTIL_LOGGER_H diff --git a/Util/Timer.cpp b/Util/Timer.cpp deleted file mode 100644 index 5d29bf6..0000000 --- a/Util/Timer.cpp +++ /dev/null @@ -1,59 +0,0 @@ -#include -#include "Util.h" -#include "Timer.h" - - -namespace PresenceDetection { -namespace Util { - -Timer::Timer() : - m_run(false), - m_aborted(false), - m_identifier(CreateRandomString(10)) -{ -} - -Timer::~Timer() -{ - Abort(); - Wait(); -} - -bool Timer::operator==(const Timer& other) const -{ - return m_identifier == other.Identifier(); -} - -std::string Timer::Identifier() const -{ - return m_identifier; -} - -void Timer::Stop() -{ - m_run.store(false, std::memory_order_release); -} - -void Timer::Abort() -{ - m_run.store(false, std::memory_order_release); - { - std::unique_lock lock(m_mutex); - m_aborted = true; - } - m_conditionVariable.notify_all(); -} - -bool Timer::IsRunning() const noexcept -{ - return (m_run.load(std::memory_order_acquire) && m_thread.joinable()); -} - -void Timer::Wait() -{ - if (m_thread.joinable()) - m_thread.join(); -} - -} // namespace Util -} // namespace PresenceDetection diff --git a/Util/Timer.h b/Util/Timer.h deleted file mode 100644 index 8597b0d..0000000 --- a/Util/Timer.h +++ /dev/null @@ -1,106 +0,0 @@ -#ifndef UTIL_TIMER_H -#define UTIL_TIMER_H - -#include -#include -#include -#include -#include -#include -#include "Helpers.h" - - -namespace PresenceDetection { -namespace Util { - -class Timer -{ -public: - Timer(); - ~Timer(); - - Timer(const Timer&) = delete; - Timer& operator= (const Timer&) = delete; - - bool operator==(const Timer& other) const; - -public: - std::string Identifier() const; - - template - void StartSingle(int milliSeconds, std::function const & function, Arguments && ...arguments) - { - Start(TimerType::Single, milliSeconds, function, std::forward(arguments)...); - } - - template - void StartContinuous(int milliSeconds, std::function const & function, Arguments && ...arguments) - { - Start(TimerType::Continuous, milliSeconds, function, std::forward(arguments)...); - } - - void Stop(); - void Abort(); - bool IsRunning() const noexcept; - void Wait(); - -private: - class TimerType - { - public: - enum type - { - Unknown, - Single, - Continuous - }; - }; - -private: - template - void Start(TimerType::type type, int milliSeconds, std::function const & function, Arguments && ...arguments) - { - if (type == TimerType::Unknown) - return; - - if (m_run.load(std::memory_order_acquire)) - Stop(); - - m_thread = std::thread([this, type, milliSeconds, function, &arguments...]() - { - auto argumentsCopy = std::make_tuple(std::forward(arguments)...); - m_run.store(true, std::memory_order_release); - m_aborted = false; - - while (m_run.load(std::memory_order_acquire)) - { - std::unique_lock lock(m_mutex); - if (!m_conditionVariable.wait_for(lock, std::chrono::milliseconds(milliSeconds), [&]{return m_aborted;})) - { - Helpers::execute(function, argumentsCopy); - if (type == TimerType::Single) - m_run.store(false, std::memory_order_release); - } - } - }); - - while (!IsRunning()) - { - } - } - -private: - std::atomic m_run; - bool m_aborted; - std::mutex m_mutex; - std::condition_variable m_conditionVariable; - std::string m_identifier; - std::thread m_thread; -}; - -typedef std::unique_ptr TimerPointer; - -} // namespace Util -} // namespace PresenceDetection - -#endif // UTIL_TIMER_H diff --git a/Util/Util.cpp b/Util/Util.cpp deleted file mode 100644 index 21f669a..0000000 --- a/Util/Util.cpp +++ /dev/null @@ -1,25 +0,0 @@ -#include -#include "Util.h" - - -namespace PresenceDetection { -namespace Util { - -std::string CreateRandomString(size_t length) -{ - auto randchar = []() -> char - { - const char charset[] = - "0123456789" - "ABCDEFGHIJKLMNOPQRSTUVWXYZ" - "abcdefghijklmnopqrstuvwxyz"; - const size_t max_index = (sizeof(charset) - 1); - return charset[ rand() % max_index ]; - }; - std::string str(length,0); - std::generate_n(str.begin(), length, randchar); - return str; -} - -} // namespace Util -} // namespace PresenceDetection diff --git a/Util/Util.h b/Util/Util.h deleted file mode 100644 index 3c26d86..0000000 --- a/Util/Util.h +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef UTIL_UTIL_H -#define UTIL_UTIL_H - -#include - - -namespace PresenceDetection { -namespace Util { - -std::string CreateRandomString(size_t length); - -} // namespace Util -} // namespace PresenceDetection - -#endif // UTIL_UTIL_H diff --git a/Util/clipp.h b/Util/clipp.h deleted file mode 100644 index 1df1ab8..0000000 --- a/Util/clipp.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef UTIL_CLIPP_H -#define UTIL_CLIPP_H - -#include "Libraries/clipp/clipp.h" - - -namespace PresenceDetection { -namespace Util { -namespace clipp { - -using namespace ::clipp; - -} // namespace clipp -} // namespace Util -} // namespace PresenceDetection - -#endif // UTIL_CLIPP_H