From 2945e8f206bab91d1b66a7bae2adbcd6017c5713 Mon Sep 17 00:00:00 2001 From: JDierkse Date: Mon, 4 May 2020 14:54:28 +0200 Subject: [PATCH] Initial commit --- .gitignore | 10 +++ .gitmodules | 3 + Application/Makefile | 1 + Application/VStarCamFinder.cc | 48 ++++++++++++ Libraries/Logging | 1 + Libraries/Network | 1 + Libraries/Utilities | 1 + Libraries/asio | 1 + Libraries/clipp | 1 + Makefile | 1 + Makefile.conf | 22 ++++++ Makefile.target | 13 ++++ Makefiles | 1 + VStarCam/Finder.cpp | 142 ++++++++++++++++++++++++++++++++++ VStarCam/Finder.h | 47 +++++++++++ VStarCam/Makefile | 1 + 16 files changed, 294 insertions(+) create mode 100644 .gitignore create mode 100644 .gitmodules create mode 120000 Application/Makefile create mode 100644 Application/VStarCamFinder.cc create mode 120000 Libraries/Logging create mode 120000 Libraries/Network create mode 120000 Libraries/Utilities create mode 120000 Libraries/asio create mode 120000 Libraries/clipp create mode 120000 Makefile create mode 100644 Makefile.conf create mode 100644 Makefile.target create mode 160000 Makefiles create mode 100644 VStarCam/Finder.cpp create mode 100644 VStarCam/Finder.h create mode 120000 VStarCam/Makefile diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ac69caf --- /dev/null +++ b/.gitignore @@ -0,0 +1,10 @@ +*.o.* +*.d.* +*.a.* +.*.swp +.AppleDouble +.debug +VStarCamFinder* +!VStarCamFinder.cc +!VStarCamFinder.cpp +!VStarCamFinder.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/Makefile b/Application/Makefile new file mode 120000 index 0000000..d0b0e8e --- /dev/null +++ b/Application/Makefile @@ -0,0 +1 @@ +../Makefile \ No newline at end of file diff --git a/Application/VStarCamFinder.cc b/Application/VStarCamFinder.cc new file mode 100644 index 0000000..ba8d169 --- /dev/null +++ b/Application/VStarCamFinder.cc @@ -0,0 +1,48 @@ +#include "VStarCam/Finder.h" +#include +#include +#include + + +int main(int argc, char** argv) +{ + try + { + Logging::OpenLog(); + Logging::SetLogMask(Logging::Severity::Info); + + if (argc != 2) + { + std::stringstream ss; + ss << "Usage: " << argv[0] << " "; + Logging::Log(Logging::Severity::Error, ss.str()); + return 1; + } + + std::string ipAddress(argv[1]); + + int port = 0; + + VStarCam::Finder finder("192.168.101.100"); + port = finder.GetPort(ipAddress); + + if (port != 0) + { + std::stringstream ss; + ss << port; + Logging::Log(Logging::Severity::Error, ss.str()); + return 0; + } + + return 1; + } + catch (const std::exception& e) + { + std::stringstream ss; + ss << "ERROR: " << e.what() << std::endl; + + Logging::Log(Logging::Severity::Error, ss.str()); + + return 1; + } +} 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/Network b/Libraries/Network new file mode 120000 index 0000000..bcf44d8 --- /dev/null +++ b/Libraries/Network @@ -0,0 +1 @@ +../../Libraries/Network \ 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/Libraries/asio b/Libraries/asio new file mode 120000 index 0000000..98b68c2 --- /dev/null +++ b/Libraries/asio @@ -0,0 +1 @@ +../../Libraries/asio \ No newline at end of file diff --git a/Libraries/clipp b/Libraries/clipp new file mode 120000 index 0000000..ffe3902 --- /dev/null +++ b/Libraries/clipp @@ -0,0 +1 @@ +../../Libraries/clipp \ No newline at end of file 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..7e700d4 --- /dev/null +++ b/Makefile.conf @@ -0,0 +1,22 @@ +# +# Makefile.conf +# + +LFLAGS += -lNetwork +LFLAGS += -L$(ROOTPATH)/Libraries/Network/lib/$(ARCH) +CFLAGS += -I$(ROOTPATH)/Libraries/Network/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/asio/asio/include +CFLAGS += -I$(ROOTPATH)/Libraries/clipp + +LFLAGS += -pthread -lm +CFLAGS += -I$(ROOTPATH) -I$(ROOTPATH)/Libraries +DEBUGDIR := .debug diff --git a/Makefile.target b/Makefile.target new file mode 100644 index 0000000..269c26a --- /dev/null +++ b/Makefile.target @@ -0,0 +1,13 @@ +# +# Makefile.target +# + +VStarCamFinder.$(ARCH): $(OBJECTS) Application/VStarCamFinder.o.$(ARCH) + $(call build_target_arch,$@,$^) + +VStarCamFinder: + $(call build_target,$@) + +.DEFAULT_GOAL := VStarCamFinder + +TARGETS += VStarCamFinder diff --git a/Makefiles b/Makefiles new file mode 160000 index 0000000..cd9ef1a --- /dev/null +++ b/Makefiles @@ -0,0 +1 @@ +Subproject commit cd9ef1a1b0f7b88f36b0c51b4209d1859aca83aa diff --git a/VStarCam/Finder.cpp b/VStarCam/Finder.cpp new file mode 100644 index 0000000..9325771 --- /dev/null +++ b/VStarCam/Finder.cpp @@ -0,0 +1,142 @@ +#include "Finder.h" +#include +#include +#include +#include + + +namespace VStarCam { + +Finder::Finder(const std::string& sourceAddress) : + m_startTime(asio::system_timer::time_point(std::chrono::system_clock::duration::max())), + m_timeout(std::chrono::seconds(5)), + m_sourceAddress(sourceAddress), + m_sourcePort(8600), + m_targetPort(9600), + m_port(0) +{ +} + +Finder::~Finder() +{ +} + +int Finder::GetPort(const std::string& ipAddress) +{ + std::stringstream ss; + ss << "Retrieving port for " << ipAddress << std::endl; + Logging::Log(Logging::Severity::Info, ss.str()); + + { + std::unique_lock lock(m_mutex); + m_port = 0; + m_targetAddress = ipAddress; + } + + Network::BroadcastClient client(m_sourceAddress, m_targetPort, std::bind(&Finder::ProcessMessage, this, std::placeholders::_1)); + Network::BroadcastServer server(m_sourceAddress, m_sourcePort, std::function()); + + m_startTime = std::chrono::system_clock::now(); + std::string message("\x44\x48\x01\x01\x0a"); + server.Broadcast(message, m_targetPort); + + while (!PortFound() && !Timeout()) + server.Broadcast(message, m_targetPort); + + std::unique_lock lock(m_mutex); + int port = m_port; + m_port = 0; + + ss.str(""); + ss << "New port for " << ipAddress << ": " << port << std::endl; + Logging::Log(Logging::Severity::Info, ss.str()); + + return port; +} + +bool Finder::Timeout() const +{ + return m_startTime + m_timeout <= std::chrono::system_clock::now(); +} + +bool Finder::PortFound() +{ + std::unique_lock lock(m_mutex); + return m_condition.wait_for(lock, std::chrono::milliseconds(1000)) == std::cv_status::no_timeout; +} + +void Finder::ProcessMessage(const std::string& message) +{ + std::string::const_iterator it = message.begin(); + + // Skip Header + it += 4; + + std::string ipAddress = GetString(it); + std::string subnetMask = GetString(it); + std::string gateway = GetString(it); + std::string dns1 = GetString(it); + std::string dns2 = GetString(it); + + // Skip Header + it += 6; + + // Get Port + unsigned int port; + + unsigned char low = *it++; + unsigned char high = *it++; + + std::string hexPort; + hexPort += high; + hexPort += low; + hexPort = StringToHex(hexPort); + + std::stringstream ss; + ss << std::hex << hexPort; + ss >> port; + + std::unique_lock lock(m_mutex); + if (ipAddress == m_targetAddress) + { + m_port = port; + lock.unlock(); + m_condition.notify_one(); + } +} + +std::string Finder::GetString(std::string::const_iterator& it) const +{ + std::string output; + while (static_cast(*it) != 0) + output += *it++; + + Advance(it); + + return output; +} + +void Finder::Advance(std::string::const_iterator& it) const +{ + while (static_cast(*it) == 0) + it++; +} + +std::string Finder::StringToHex(const std::string& input) const +{ + static const char* const lut = "0123456789ABCDEF"; + size_t len = input.length(); + + std::string output; + output.reserve(2 * len); + for (size_t i = 0; i < len; ++i) + { + const unsigned char c = input[i]; + output.push_back(lut[c >> 4]); + output.push_back(lut[c & 15]); + } + + return output; +} + +} // namespace VStarCam diff --git a/VStarCam/Finder.h b/VStarCam/Finder.h new file mode 100644 index 0000000..8c0c55b --- /dev/null +++ b/VStarCam/Finder.h @@ -0,0 +1,47 @@ +#ifndef VSTARCAM_FINDER_H +#define VSTARCAM_FINDER_H + +#include +#include +#include +#include +#include + + +namespace VStarCam { + +class Finder +{ +public: + explicit Finder(const std::string& ipAddress); + ~Finder(); + + int GetPort(const std::string& ipAddress); + +private: + bool Timeout() const; + bool PortFound(); + void ProcessMessage(const std::string& message); + std::string GetString(std::string::const_iterator& it) const; + void Advance(std::string::const_iterator& it) const; + std::string StringToHex(const std::string& input) const; + +private: + asio::system_timer::time_point m_startTime; + asio::system_timer::duration m_timeout; + + mutable std::mutex m_mutex; + std::condition_variable m_condition; + + std::string m_sourceAddress; + std::string m_targetAddress; + + int m_sourcePort; + int m_targetPort; + + int m_port; +}; + +} // namespace VStarCam + +#endif // UTIL_VSTARCAMFINDER_H diff --git a/VStarCam/Makefile b/VStarCam/Makefile new file mode 120000 index 0000000..d0b0e8e --- /dev/null +++ b/VStarCam/Makefile @@ -0,0 +1 @@ +../Makefile \ No newline at end of file