Skip to content

Commit

Permalink
Added UDP server
Browse files Browse the repository at this point in the history
Added new command line options to setup server and station id
  • Loading branch information
Fábio Gonçalves committed Jun 19, 2024
1 parent 067c01a commit 76cd0da
Show file tree
Hide file tree
Showing 8 changed files with 245 additions and 9 deletions.
69 changes: 69 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
7 changes: 6 additions & 1 deletion tools/socktap/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
72 changes: 69 additions & 3 deletions tools/socktap/cam_application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <<std::endl;
return sent;
}

void CamApplication::set_interval(Clock::duration interval)
Expand All @@ -44,6 +94,14 @@ CamApplication::PortType CamApplication::port()
return btp::ports::CAM;
}

int decodeCAM(const asn1::Cam& recvd, char* message){
const ItsPduHeader_t& header = recvd->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<asn1::Cam> visitor;
Expand All @@ -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()
Expand All @@ -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<milliseconds>(runtime_.now().time_since_epoch());
uint16_t gen_delta_time = time_now.count();
Expand All @@ -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;
}

Expand Down
24 changes: 23 additions & 1 deletion tools/socktap/cam_application.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,48 @@
#include <vanetza/common/clock.hpp>
#include <vanetza/common/position_provider.hpp>
#include <vanetza/common/runtime.hpp>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>

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 */
29 changes: 29 additions & 0 deletions tools/socktap/its_lci.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "its_lci.hpp"
#include <vanetza/btp/ports.hpp>
#include <vanetza/asn1/cam.hpp>
#include <vanetza/asn1/packet_visitor.hpp>
#include <vanetza/facilities/cam_functions.hpp>
#include <boost/units/cmath.hpp>
#include <boost/units/systems/si/prefixes.hpp>
#include <chrono>
#include <exception>
#include <functional>
#include <iostream>

// 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)
{

}
23 changes: 23 additions & 0 deletions tools/socktap/its_lci.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef ITC_LCI_HPP
#define ITC_LCI_HPP

#include "application.hpp"
#include <vanetza/common/clock.hpp>
#include <vanetza/common/position_provider.hpp>
#include <vanetza/common/runtime.hpp>

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
4 changes: 2 additions & 2 deletions tools/socktap/link_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::vector<std::string>>()) {
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));
}
}
Expand All @@ -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<std::vector<std::string>>()) {
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));
}
}
Expand Down
26 changes: 24 additions & 2 deletions tools/socktap/main.cpp
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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<std::vector<std::string>>()->default_value({"ca"}, "ca")->multitoken(), "Run applications [ca,hello,benchmark]")
("send-to-server", "Send V2X data to server")
("server-ip",po::value<std::string>()->default_value("192.168.1.124"), "Server IP")
("server-port", po::value<unsigned>()->default_value(9000), "Server Port")
("station-id", po::value<unsigned>()->default_value(1), "Station ID")
("applications,a", po::value<std::vector<std::string>>()->default_value({"ca"}, "ca")->multitoken(), "Run applications [ca,de,hello,benchmark]")
("non-strict", "Set MIB parameter ItsGnSnDecapResultHandling to NON_STRICT")
;
add_positioning_options(options);
Expand Down Expand Up @@ -149,8 +154,25 @@ int main(int argc, const char** argv)
ca->set_interval(std::chrono::milliseconds(vm["cam-interval"].as<unsigned>()));
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<unsigned>());
if(vm.count("send-to-server") > 0){
ca->setServerIP(vm["server-ip"].as<std::string>().data());
ca->setServerPort(vm["server-port"].as<unsigned>());

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<ITC_LCI_Application> de {
new ITC_LCI_Application(*positioning, trigger.runtime())
};
//ca->set_interval(std::chrono::milliseconds(vm["cam-interval"].as<unsigned>()));
//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<HelloApplication> hello {
new HelloApplication(io_service, std::chrono::milliseconds(800))
};
Expand Down

0 comments on commit 76cd0da

Please sign in to comment.