From 76cd0da42e9838a88bd23a63eed6158388df7ca2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A1bio=20Gon=C3=A7alves?= Date: Tue, 30 Apr 2024 12:26:46 +0100 Subject: [PATCH] Added UDP server Added new command line options to setup server and station id --- .vscode/settings.json | 69 +++++++++++++++++++++++++++++ tools/socktap/CMakeLists.txt | 7 ++- tools/socktap/cam_application.cpp | 72 +++++++++++++++++++++++++++++-- tools/socktap/cam_application.hpp | 24 ++++++++++- tools/socktap/its_lci.cpp | 29 +++++++++++++ tools/socktap/its_lci.hpp | 23 ++++++++++ tools/socktap/link_layer.cpp | 4 +- tools/socktap/main.cpp | 26 ++++++++++- 8 files changed, 245 insertions(+), 9 deletions(-) create mode 100644 .vscode/settings.json create mode 100644 tools/socktap/its_lci.cpp create mode 100644 tools/socktap/its_lci.hpp diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 000000000..d3f0a59c1 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,69 @@ +{ + "files.associations": { + "array": "cpp", + "atomic": "cpp", + "bit": "cpp", + "*.tcc": "cpp", + "cctype": "cpp", + "chrono": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "compare": "cpp", + "concepts": "cpp", + "condition_variable": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cstring": "cpp", + "ctime": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "deque": "cpp", + "list": "cpp", + "map": "cpp", + "set": "cpp", + "string": "cpp", + "unordered_map": "cpp", + "unordered_set": "cpp", + "vector": "cpp", + "exception": "cpp", + "algorithm": "cpp", + "functional": "cpp", + "iterator": "cpp", + "memory": "cpp", + "memory_resource": "cpp", + "numeric": "cpp", + "random": "cpp", + "ratio": "cpp", + "string_view": "cpp", + "system_error": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp", + "fstream": "cpp", + "future": "cpp", + "initializer_list": "cpp", + "iomanip": "cpp", + "iosfwd": "cpp", + "iostream": "cpp", + "istream": "cpp", + "limits": "cpp", + "mutex": "cpp", + "new": "cpp", + "numbers": "cpp", + "ostream": "cpp", + "semaphore": "cpp", + "sstream": "cpp", + "stdexcept": "cpp", + "stop_token": "cpp", + "streambuf": "cpp", + "thread": "cpp", + "cinttypes": "cpp", + "typeindex": "cpp", + "typeinfo": "cpp", + "asn_application.h": "c", + "basicvehiclecontainerhighfrequency.h": "c" + } +} \ No newline at end of file diff --git a/tools/socktap/CMakeLists.txt b/tools/socktap/CMakeLists.txt index fbfb180c9..afabbf1cd 100644 --- a/tools/socktap/CMakeLists.txt +++ b/tools/socktap/CMakeLists.txt @@ -14,11 +14,13 @@ add_executable(socktap application.cpp benchmark_application.cpp cam_application.cpp + #its_lci.cpp dcc_passthrough.cpp ethernet_device.cpp hello_application.cpp link_layer.cpp main.cpp + #gps_position_provider.cpp positioning.cpp raw_socket_link.cpp router_context.cpp @@ -87,9 +89,12 @@ if(SOCKTAP_WITH_COHDA_LLC) target_sources(socktap PRIVATE cohda.cpp cohda_link.cpp) endif() -find_package(GPS QUIET) +find_package(GPS) +message(Found "${GPS}") +message(GPS found: "${GPS_FOUND}") option(SOCKTAP_WITH_GPSD "Enable gpsd positioning for socktap" ${GPS_FOUND}) if(SOCKTAP_WITH_GPSD) + message(Configuring GPS) find_package(GPS REQUIRED) target_compile_definitions(socktap PUBLIC "SOCKTAP_WITH_GPSD") target_link_libraries(socktap PUBLIC GPS::GPS) diff --git a/tools/socktap/cam_application.cpp b/tools/socktap/cam_application.cpp index f7e40e6fe..ff0b3fa46 100644 --- a/tools/socktap/cam_application.cpp +++ b/tools/socktap/cam_application.cpp @@ -19,7 +19,57 @@ using namespace std::chrono; CamApplication::CamApplication(PositionProvider& positioning, Runtime& rt) : positioning_(positioning), runtime_(rt), cam_interval_(seconds(1)) { - schedule_timer(); + this->send_to_server = false; + schedule_timer(); + + this->station_id = 1; + this->server_port = 9000; + this->serverIP = strdup("192.168.1.124"); +} + +int CamApplication::createSocket(){ + // Creating socket file descriptor + if ( (this->sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) { + perror("socket creation failed"); + return -1; + } + printf("socket created\n"); + + memset(&this->servaddr, 0, sizeof(this->servaddr)); + + // Filling server information + this->servaddr.sin_family = AF_INET; + this->servaddr.sin_port = htons(this->server_port); + this->servaddr.sin_addr.s_addr = inet_addr(this->serverIP); + return 0; +} + +int CamApplication::closeSocket(){ + return close(this->sockfd); +} + +void CamApplication::setSendToServer(bool send_to_server){ + this->send_to_server = send_to_server; +} + +void CamApplication::setServerPort(int serverPort){ + this->server_port = serverPort; +} + +void CamApplication::setServerIP(const char * serverIP){ + this->serverIP = serverIP; +} + +void CamApplication::setStationID(int station_id){ + this->station_id = station_id; +} + +int CamApplication::sendToServer(u_int64_t* dataToSend, int size){ + int sent = sendto(this->sockfd, (const u_int64_t *)dataToSend, size, + MSG_CONFIRM, (const struct sockaddr *) &this->servaddr, + sizeof(this->servaddr)); + std::cout<<"Sending message to UDP Server:" << this->serverIP << " " << this->server_port <header; + const CoopAwareness_t& cam = recvd->cam; + const BasicContainer_t& basic = cam.camParameters.basicContainer; + int size = sprintf(message, "%ld;%ld;%ld",header.stationID,basic.referencePosition.longitude,basic.referencePosition.latitude); + return strlen(message); +} + void CamApplication::indicate(const DataIndication& indication, UpPacketPtr packet) { asn1::PacketVisitor visitor; @@ -54,6 +112,14 @@ void CamApplication::indicate(const DataIndication& indication, UpPacketPtr pack std::cout << "Received CAM contains\n"; print_indented(std::cout, *cam, " ", 1); } + + if(cam && send_to_server){ + std::cout << "sending to udp server" << std::endl; + char message [100]; + int size = decodeCAM(*cam, message); + this->sendToServer((u_int64_t*)message, size); + } + } void CamApplication::schedule_timer() @@ -69,7 +135,7 @@ void CamApplication::on_timer(Clock::time_point) ItsPduHeader_t& header = message->header; header.protocolVersion = 2; header.messageID = ItsPduHeader__messageID_cam; - header.stationID = 1; // some dummy value + header.stationID = this->station_id; // some dummy value const auto time_now = duration_cast(runtime_.now().time_since_epoch()); uint16_t gen_delta_time = time_now.count(); @@ -80,7 +146,7 @@ void CamApplication::on_timer(Clock::time_point) auto position = positioning_.position_fix(); if (!position.confidence) { - std::cerr << "Skipping CAM, because no good position is available, yet." << std::endl; + std::cerr << "Skipping CAM, because no good position is available, yet." << position.confidence << std::endl; return; } diff --git a/tools/socktap/cam_application.hpp b/tools/socktap/cam_application.hpp index 6b975b253..25c8e752d 100644 --- a/tools/socktap/cam_application.hpp +++ b/tools/socktap/cam_application.hpp @@ -5,26 +5,48 @@ #include #include #include +#include +#include +#include +#include +#include +#include +#include class CamApplication : public Application { public: CamApplication(vanetza::PositionProvider& positioning, vanetza::Runtime& rt); + PortType port() override; void indicate(const DataIndication&, UpPacketPtr) override; void set_interval(vanetza::Clock::duration); void print_received_message(bool flag); void print_generated_message(bool flag); - + void setSendToServer(bool send_to_server); + void setServerPort(int serverPort); + void setServerIP(const char * serverIP); + void setStationID(int station_id); + int createSocket(); private: void schedule_timer(); void on_timer(vanetza::Clock::time_point); + + int closeSocket(); + int sendToServer(u_int64_t* dataToSend, int size); + vanetza::PositionProvider& positioning_; vanetza::Runtime& runtime_; vanetza::Clock::duration cam_interval_; bool print_rx_msg_ = false; bool print_tx_msg_ = false; + bool send_to_server = false; + int sockfd; + int server_port; + const char* serverIP; + struct sockaddr_in servaddr; + int station_id; }; #endif /* CAM_APPLICATION_HPP_EUIC2VFR */ diff --git a/tools/socktap/its_lci.cpp b/tools/socktap/its_lci.cpp new file mode 100644 index 000000000..99c8a8769 --- /dev/null +++ b/tools/socktap/its_lci.cpp @@ -0,0 +1,29 @@ +#include "its_lci.hpp" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// This is a very simple CA application sending CAMs at a fixed rate. + +using namespace vanetza; +using namespace vanetza::facilities; +using namespace std::chrono; + +ITC_LCI_Application::ITC_LCI_Application(PositionProvider& positioning, Runtime& rt) : + positioning_(positioning), runtime_(rt), cam_interval_(seconds(1)) +{ + int i = 0; + printf("hellow %d", i); +} + +void ITC_LCI_Application::indicate(const DataIndication& indication, UpPacketPtr packet) +{ + +} \ No newline at end of file diff --git a/tools/socktap/its_lci.hpp b/tools/socktap/its_lci.hpp new file mode 100644 index 000000000..e30940529 --- /dev/null +++ b/tools/socktap/its_lci.hpp @@ -0,0 +1,23 @@ +#ifndef ITC_LCI_HPP +#define ITC_LCI_HPP + +#include "application.hpp" +#include +#include +#include + +class ITC_LCI_Application : public Application +{ +public: + ITC_LCI_Application(vanetza::PositionProvider& positioning, vanetza::Runtime& rt); + PortType port() override; + void indicate(const DataIndication&, UpPacketPtr) override; + +private: + + vanetza::PositionProvider& positioning_; + vanetza::Runtime& runtime_; + vanetza::Clock::duration cam_interval_; +}; + +#endif \ No newline at end of file diff --git a/tools/socktap/link_layer.cpp b/tools/socktap/link_layer.cpp index 48a47a292..3afce5166 100644 --- a/tools/socktap/link_layer.cpp +++ b/tools/socktap/link_layer.cpp @@ -77,7 +77,7 @@ create_link_layer(boost::asio::io_service& io_service, const EthernetDevice& dev if (vm.count("tcp-connect")) { for (const std::string& ip_port : vm["tcp-connect"].as>()) { auto ip_port_pair = parse_ip_port(ip_port); - if (ip_port_pair.has_value()) { + if (ip_port_pair.is_initialized()) { tcp->connect(ip::tcp::endpoint(ip_port_pair.value().first, ip_port_pair.value().second)); } } @@ -86,7 +86,7 @@ create_link_layer(boost::asio::io_service& io_service, const EthernetDevice& dev if (vm.count("tcp-accept")) { for (const std::string& ip_port : vm["tcp-accept"].as>()) { auto ip_port_pair = parse_ip_port(ip_port); - if (ip_port_pair.has_value()) { + if (ip_port_pair.is_initialized()) { tcp->accept(ip::tcp::endpoint(ip_port_pair.value().first, ip_port_pair.value().second)); } } diff --git a/tools/socktap/main.cpp b/tools/socktap/main.cpp index a26375248..f2387ffa6 100644 --- a/tools/socktap/main.cpp +++ b/tools/socktap/main.cpp @@ -1,6 +1,7 @@ #include "ethernet_device.hpp" #include "benchmark_application.hpp" #include "cam_application.hpp" +//#include "its_lci.hpp" #include "hello_application.hpp" #include "link_layer.hpp" #include "positioning.hpp" @@ -35,7 +36,11 @@ int main(int argc, const char** argv) ("print-rx-cam", "Print received CAMs") ("print-tx-cam", "Print generated CAMs") ("benchmark", "Enable benchmarking") - ("applications,a", po::value>()->default_value({"ca"}, "ca")->multitoken(), "Run applications [ca,hello,benchmark]") + ("send-to-server", "Send V2X data to server") + ("server-ip",po::value()->default_value("192.168.1.124"), "Server IP") + ("server-port", po::value()->default_value(9000), "Server Port") + ("station-id", po::value()->default_value(1), "Station ID") + ("applications,a", po::value>()->default_value({"ca"}, "ca")->multitoken(), "Run applications [ca,de,hello,benchmark]") ("non-strict", "Set MIB parameter ItsGnSnDecapResultHandling to NON_STRICT") ; add_positioning_options(options); @@ -149,8 +154,25 @@ int main(int argc, const char** argv) ca->set_interval(std::chrono::milliseconds(vm["cam-interval"].as())); ca->print_received_message(vm.count("print-rx-cam") > 0); ca->print_generated_message(vm.count("print-tx-cam") > 0); + ca->setStationID(vm["station-id"].as()); + if(vm.count("send-to-server") > 0){ + ca->setServerIP(vm["server-ip"].as().data()); + ca->setServerPort(vm["server-port"].as()); + + ca->createSocket(); + ca->setSendToServer(true); + } apps.emplace(app_name, std::move(ca)); - } else if (app_name == "hello") { + } /* + else if (app_name == "DE") { + std::unique_ptr de { + new ITC_LCI_Application(*positioning, trigger.runtime()) + }; + //ca->set_interval(std::chrono::milliseconds(vm["cam-interval"].as())); + //ca->print_received_message(vm.count("print-rx-cam") > 0); + //ca->print_generated_message(vm.count("print-tx-cam") > 0); + apps.emplace(app_name, std::move(de)); + }*/ else if (app_name == "hello") { std::unique_ptr hello { new HelloApplication(io_service, std::chrono::milliseconds(800)) };