From 2084ae1182921af239d6b31dae470fc16e7b4e83 Mon Sep 17 00:00:00 2001 From: jwillmartin Date: Wed, 3 Jan 2024 14:51:12 -0500 Subject: [PATCH] modified: src/install_coverage_dependencies.sh modified: src/tmx/TmxUtils/test/J2735MessageTest.cpp new file: src/v2i-hub/RSAPlugin/CMakeLists.txt new file: src/v2i-hub/RSAPlugin/manifest.json new file: src/v2i-hub/RSAPlugin/src/RsaPlugin.cpp new file: src/v2i-hub/RSAPlugin/src/RsaPluginWorker.cpp new file: src/v2i-hub/RSAPlugin/src/include/RsaPlugin.hpp new file: src/v2i-hub/RSAPlugin/src/include/RsaPluginWorker.hpp --- src/install_coverage_dependencies.sh | 2 +- src/tmx/TmxUtils/test/J2735MessageTest.cpp | 14 ++ src/v2i-hub/RSAPlugin/CMakeLists.txt | 36 +++ src/v2i-hub/RSAPlugin/manifest.json | 38 ++++ src/v2i-hub/RSAPlugin/src/RsaPlugin.cpp | 207 ++++++++++++++++++ src/v2i-hub/RSAPlugin/src/RsaPluginWorker.cpp | 15 ++ .../RSAPlugin/src/include/RsaPlugin.hpp | 88 ++++++++ .../RSAPlugin/src/include/RsaPluginWorker.hpp | 44 ++++ 8 files changed, 443 insertions(+), 1 deletion(-) create mode 100644 src/v2i-hub/RSAPlugin/CMakeLists.txt create mode 100644 src/v2i-hub/RSAPlugin/manifest.json create mode 100644 src/v2i-hub/RSAPlugin/src/RsaPlugin.cpp create mode 100644 src/v2i-hub/RSAPlugin/src/RsaPluginWorker.cpp create mode 100644 src/v2i-hub/RSAPlugin/src/include/RsaPlugin.hpp create mode 100644 src/v2i-hub/RSAPlugin/src/include/RsaPluginWorker.hpp diff --git a/src/install_coverage_dependencies.sh b/src/install_coverage_dependencies.sh index 5491440ba..e3be7ce1b 100755 --- a/src/install_coverage_dependencies.sh +++ b/src/install_coverage_dependencies.sh @@ -18,4 +18,4 @@ set -ex apt update export DEBIAN_FRONTEND=noninteractive -apt-get install -y curl zip git gcovr \ No newline at end of file +apt-get install -y curl zip git gcovr diff --git a/src/tmx/TmxUtils/test/J2735MessageTest.cpp b/src/tmx/TmxUtils/test/J2735MessageTest.cpp index c885ecdd9..2c8649d22 100644 --- a/src/tmx/TmxUtils/test/J2735MessageTest.cpp +++ b/src/tmx/TmxUtils/test/J2735MessageTest.cpp @@ -603,6 +603,20 @@ TEST_F(J2735MessageTest, EncodePersonalSafetyMessage){ std::cout << psmENC.get_payload_str()<(ss); +// rsamessage.set_contents(container.get_storage().get_tree()); +// rsaENC.encode_j2735_message(rsamessage); +// std::cout << rsaENC.get_payload_str()<::create(); + QString st; + while(socket->bytesAvailable()>0) + { + st.append(socket->readAll()); + } + QByteArray array = st.toLocal8Bit(); + + char* rsaMsgdef = array.data(); + // Catch parse exceptions + try { + BroadcastRsa(rsaMsgdef); + writeResponse(QHttpEngine::Socket::Created, socket); + } + catch(const J2735Exception &e) { + PLOG(logERROR) << "Error parsing file: " << e.what() << std::endl; + writeResponse(QHttpEngine::Socket::BadRequest, socket); + } +} + + +int RsaPlugin::StartWebService() +{ + //Web services + char *placeholderX[1]={0}; + int placeholderC=1; + QCoreApplication a(placeholderC,placeholderX); + + QHostAddress address = QHostAddress(QString::fromStdString (webip)); + quint16 port = static_cast(webport); + + + QSharedPointer handler(new OpenAPI::OAIApiRequestHandler()); + handler = QSharedPointer (new OpenAPI::OAIApiRequestHandler()); + + QObject::connect(handler.data(), &OpenAPI::OAIApiRequestHandler::requestReceived, [&](QHttpEngine::Socket *socket) { + + this->RsaRequestHandler(socket); + }); + + QHttpEngine::Server server(handler.data()); + + if (!server.listen(address, port)) { + qCritical("Unable to listen on the specified port."); + return 1; + } + return a.exec(); + +} + +RsaPlugin::~RsaPlugin() +{ + if (_signSimClient != NULL) + delete _signSimClient; +} + +void RsaPlugin::UpdateConfigSettings() +{ + // Configuration settings are retrieved from the API using the GetConfigValue template class. + // This method does NOT execute in the main thread, so variables must be protected + // (e.g. using std::atomic, std::mutex, etc.). + + int instance; + std::lock_guard lock(_cfgLock); + + GetConfigValue("WebServiceIP",webip); + GetConfigValue("WebServicePort",webport); + GetConfigValue("Instance", instance); + + std::thread webthread(&RsaPlugin::StartWebService,this); + webthread.detach(); // wait for the thread to finish +} + +void RsaPlugin::OnConfigChanged(const char *key, const char *value) +{ + PluginClient::OnConfigChanged(key, value); + UpdateConfigSettings(); +} + +void RsaPlugin::OnStateChange(IvpPluginState state) +{ + PluginClient::OnStateChange(state); + + if (state == IvpPluginState_registered) + { + UpdateConfigSettings(); + } +} + + +void RsaPlugin::BroadcastRsa(char * rsaJson) +{ //overloaded + + RsaMessage rsamessage; + RsaEncodedMessage rsaENC; + tmx::message_container_type container; + std::unique_ptr msg; + + try + { + std::stringstream ss; + ss << rsaJson; + + container.load(ss); + rsamessage.set_contents(container.get_storage().get_tree()); + + const std::string rsaString(rsaJson); + + rsaENC.encode_j2735_message(rsamessage); + + msg.reset(); + msg.reset(dynamic_cast(factory.NewMessage(api::MSGSUBTYPE_ROADSIDEALERT_STRING))); + + string enc = rsaENC.get_encoding(); + msg->refresh_timestamp(); + msg->set_payload(rsaENC.get_payload_str()); + msg->set_encoding(enc); + msg->set_flags(IvpMsgFlags_RouteDSRC); + msg->addDsrcMetadata(0x8003); + msg->refresh_timestamp(); + + routeable_message *rMsg = dynamic_cast(msg.get()); + BroadcastMessage(*rMsg); + + PLOG(logINFO) << " RSA Plugin :: Broadcast RSA:: " << rsaENC.get_payload_str(); + } + catch(const std::exception& e) + { + PLOG(logWARNING) << "Error: " << e.what() << " broadcasting RSA for xml: " << rsaJson << std::endl; + } + + + +} + +/** + * Write HTTP response. + */ +void RsaPlugin::writeResponse(int responseCode , QHttpEngine::Socket *socket) { + socket->setStatusCode(responseCode); + socket->writeHeaders(); + if(socket->isOpen()){ + socket->close(); + } + +} + + +int RsaPlugin::Main() +{ + PLOG(logINFO) << "Starting plugin."; + + uint msCount = 0; + while (_plugin->state != IvpPluginState_error) + { + + msCount += 10; + + if (_plugin->state == IvpPluginState_registered) + { + RoadSideAlert rsa_1; + RoadSideAlert &rsa = rsa_1; + + this_thread::sleep_for(chrono::milliseconds(100)); + + msCount = 0; + } + } + + PLOG(logINFO) << "Plugin terminating gracefully."; + return EXIT_SUCCESS; +} + +} /* namespace RsaPlugin */ + +int main(int argc, char *argv[]) +{ + return run_plugin("RsaPlugin", argc, argv); +} diff --git a/src/v2i-hub/RSAPlugin/src/RsaPluginWorker.cpp b/src/v2i-hub/RSAPlugin/src/RsaPluginWorker.cpp new file mode 100644 index 000000000..c02f3cde0 --- /dev/null +++ b/src/v2i-hub/RSAPlugin/src/RsaPluginWorker.cpp @@ -0,0 +1,15 @@ +//========================================================================== +// Name : RsaPlugin.cpp +// Author : FHWA Saxton Transportation Operations Laboratory +// Version : +// Copyright : Copyright (c) 2023 FHWA Saxton Transportation Operations Laboratory. All rights reserved. +// Description : RSA Plugin +//========================================================================== + +#include "include/RsaPluginWorker.hpp" + +using namespace std; + +namespace RsaPlugin { + +}; \ No newline at end of file diff --git a/src/v2i-hub/RSAPlugin/src/include/RsaPlugin.hpp b/src/v2i-hub/RSAPlugin/src/include/RsaPlugin.hpp new file mode 100644 index 000000000..cdd51a5cf --- /dev/null +++ b/src/v2i-hub/RSAPlugin/src/include/RsaPlugin.hpp @@ -0,0 +1,88 @@ +//========================================================================== +// Name : RsaPlugin.cpp +// Author : FHWA Saxton Transportation Operations Laboratory +// Version : +// Copyright : Copyright (c) 2019 FHWA Saxton Transportation Operations Laboratory. All rights reserved. +// Description : RSA Plugin +//========================================================================== +#pragma once +#include + +#include "PluginClient.h" +#include "PluginDataMonitor.h" + +#include +#include +#include +#include +#include + +#include +#include +#include "RsaPluginWorker.hpp" + + +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef __linux__ +#include +#include +#endif +#include +#include +#include +#include + + +using namespace std; +using namespace tmx; +using namespace tmx::messages; +using namespace tmx::utils; +using namespace OpenAPI; + +namespace RsaPlugin +{ + +/** + * This plugin is an example to demonstrate the capabilities of a TMX plugin. + */ +class RsaPlugin: public PluginClient +{ +public: + RsaPlugin(std::string); + RsaPlugin(); + virtual ~RsaPlugin(); + int Main(); + + uint16_t webport; + std::string webip; + +protected: + void UpdateConfigSettings(); + + // Virtual method overrides. + void OnConfigChanged(const char *key, const char *value); + void OnStateChange(IvpPluginState state); + + void BroadcastRsa(char *rsaJson); + int StartWebService(); + + void RsaRequestHandler(QHttpEngine::Socket *socket); + void writeResponse(int responseCode , QHttpEngine::Socket *socket); + +private: + tmx::utils::UdpClient *_signSimClient = NULL; + J2735MessageFactory factory; + + +}; +std::mutex _cfgLock; + +}; diff --git a/src/v2i-hub/RSAPlugin/src/include/RsaPluginWorker.hpp b/src/v2i-hub/RSAPlugin/src/include/RsaPluginWorker.hpp new file mode 100644 index 000000000..f51615fbf --- /dev/null +++ b/src/v2i-hub/RSAPlugin/src/include/RsaPluginWorker.hpp @@ -0,0 +1,44 @@ +//========================================================================== +// Name : RsaPlugin.cpp +// Author : FHWA Saxton Transportation Operations Laboratory +// Version : +// Copyright : Copyright (c) 2023 FHWA Saxton Transportation Operations Laboratory. All rights reserved. +// Description : RSA Plugin +//========================================================================== +#pragma once +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "PluginClient.h" +#include "PluginDataMonitor.h" + +using namespace std; +using namespace tmx; +using namespace tmx::messages; +using namespace tmx::utils; + +namespace RsaPlugin { + + class RsaPluginWorker { + public: + // struct RSA { + // RoadSideAlert rsa; + // RSA(int anInt, double aDouble) : rsa.TemporaryID_t(anInt), number(aDouble) { } + + // }; + + }; + + + +}; +