diff --git a/tools/socktap/cam_application.cpp b/tools/socktap/cam_application.cpp index ff0b3fa46..4958ffc28 100644 --- a/tools/socktap/cam_application.cpp +++ b/tools/socktap/cam_application.cpp @@ -25,6 +25,7 @@ CamApplication::CamApplication(PositionProvider& positioning, Runtime& rt) : this->station_id = 1; this->server_port = 9000; this->serverIP = strdup("192.168.1.124"); + this->file = NULL; } int CamApplication::createSocket(){ @@ -60,6 +61,29 @@ void CamApplication::setServerIP(const char * serverIP){ this->serverIP = serverIP; } +void CamApplication::setSendToFile(bool send_to_file){ + this->send_to_file = send_to_file; +} + +void CamApplication::setFile(const char * file_path){ + this->file_path = file_path; +} + +int CamApplication::openFile(const char * file_path){ + this->file = fopen(file_path,"a+"); + if(file){ + return 0; + }else{ + return -1; + } +} + +int CamApplication::writeToFile(u_int64_t* dataToSend, int size){ + int writen = fwrite(dataToSend, sizeof(char), size, this->file); + fflush(this->file); + return writen; +} + void CamApplication::setStationID(int station_id){ this->station_id = station_id; } @@ -76,6 +100,11 @@ void CamApplication::set_interval(Clock::duration interval) { cam_interval_ = interval; runtime_.cancel(this); + if(cam_interval_<=vanetza::Clock::duration{0}){ + std::cout << "CAM period to low, disabling" << std::endl; + return; + } + schedule_timer(); } @@ -98,7 +127,7 @@ 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); + int size = sprintf(message, "%ld;%ld;%ld\n",header.stationID,basic.referencePosition.longitude,basic.referencePosition.latitude); return strlen(message); } @@ -119,17 +148,34 @@ void CamApplication::indicate(const DataIndication& indication, UpPacketPtr pack int size = decodeCAM(*cam, message); this->sendToServer((u_int64_t*)message, size); } + + if(this->send_to_file){ + if(this->file == NULL){ + int result = this->openFile(this->file_path); + if(result < 0){ + std::cout << "Unable to open file, exiting" << std::endl; + exit(-1); + } + } + char message [100]; + int size = decodeCAM(*cam, message); + int writen = this->writeToFile((u_int64_t*)message, size); + std::cout << "sent to file " << size << " bytes " << writen << " bytes " << std::endl; + } } void CamApplication::schedule_timer() { + runtime_.schedule(cam_interval_, std::bind(&CamApplication::on_timer, this, std::placeholders::_1), this); } void CamApplication::on_timer(Clock::time_point) { schedule_timer(); + + vanetza::asn1::Cam message; ItsPduHeader_t& header = message->header; diff --git a/tools/socktap/cam_application.hpp b/tools/socktap/cam_application.hpp index 25c8e752d..c84c90e4a 100644 --- a/tools/socktap/cam_application.hpp +++ b/tools/socktap/cam_application.hpp @@ -27,6 +27,8 @@ class CamApplication : public Application void setServerPort(int serverPort); void setServerIP(const char * serverIP); void setStationID(int station_id); + void setSendToFile(bool send_to_file); + void setFile(const char * file_path); int createSocket(); private: void schedule_timer(); @@ -34,6 +36,9 @@ class CamApplication : public Application int closeSocket(); int sendToServer(u_int64_t* dataToSend, int size); + + int openFile(const char * filename); + int writeToFile(u_int64_t* dataToSend, int size); vanetza::PositionProvider& positioning_; @@ -42,11 +47,14 @@ class CamApplication : public Application bool print_rx_msg_ = false; bool print_tx_msg_ = false; bool send_to_server = false; + bool send_to_file = false; int sockfd; int server_port; const char* serverIP; + const char* file_path; struct sockaddr_in servaddr; int station_id; + FILE *file; }; #endif /* CAM_APPLICATION_HPP_EUIC2VFR */ diff --git a/tools/socktap/main.cpp b/tools/socktap/main.cpp index f2387ffa6..67112bfb0 100644 --- a/tools/socktap/main.cpp +++ b/tools/socktap/main.cpp @@ -37,6 +37,8 @@ int main(int argc, const char** argv) ("print-tx-cam", "Print generated CAMs") ("benchmark", "Enable benchmarking") ("send-to-server", "Send V2X data to server") + ("send-to-file", "Store V2X data in a file") + ("file",po::value()->default_value("v2x_data.bin"), "File") ("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") @@ -162,6 +164,10 @@ int main(int argc, const char** argv) ca->createSocket(); ca->setSendToServer(true); } + if(vm.count("send-to-file") > 0){ + ca->setSendToFile(true); + ca->setFile(vm["file"].as().data()); + } apps.emplace(app_name, std::move(ca)); } /* else if (app_name == "DE") { diff --git a/vanetza/btp/ports.hpp b/vanetza/btp/ports.hpp index c74d9b353..d4508ee2d 100644 --- a/vanetza/btp/ports.hpp +++ b/vanetza/btp/ports.hpp @@ -1,52 +1,52 @@ -#ifndef PORTS_HPP_T2IEFSSC -#define PORTS_HPP_T2IEFSSC - -#include - -namespace vanetza -{ -namespace btp -{ - -typedef uint16be_t port_type; - -namespace ports -{ - -// Port numbers according to ETSI TS 103 248 v2.3.1 (2024-03) -static const port_type CAM = host_cast(2001); -static const port_type DENM = host_cast(2002); -static const port_type TOPO = host_cast(2003); -static const port_type SPAT = host_cast(2004); -static const port_type SAM = host_cast(2005); -static const port_type IVIM = host_cast(2006); -static const port_type SREM = host_cast(2007); -static const port_type SSEM = host_cast(2008); -static const port_type CPM = host_cast(2009); -static const port_type EVCSN_POI = host_cast(2010); -static const port_type TRM = host_cast(2011); -static const port_type TCM = host_cast(2011); -static const port_type VDRM = host_cast(2011); -static const port_type VDPM = host_cast(2011); -static const port_type EOFM = host_cast(2011); -static const port_type EV_RSR = host_cast(2012); -static const port_type RTCMEM = host_cast(2013); -static const port_type CTLM = host_cast(2014); -static const port_type CRLM = host_cast(2015); -static const port_type EC_AT_REQUEST = host_cast(2016); -static const port_type MCDM = host_cast(2017); -static const port_type VAM = host_cast(2018); -static const port_type IMZM = host_cast(2019); -static const port_type DSM = host_cast(2020); -static const port_type P2P_CRL = host_cast(2021); -static const port_type P2P_DCTL = host_cast(2022); -static const port_type MRM = host_cast(2023); -static const port_type P2P_FCTL = host_cast(2024); - -} // namespace ports - -} // namespace btp -} // namespace vanetza - -#endif /* PORTS_HPP_T2IEFSSC */ - +#ifndef PORTS_HPP_T2IEFSSC +#define PORTS_HPP_T2IEFSSC + +#include + +namespace vanetza +{ +namespace btp +{ + +typedef uint16be_t port_type; + +namespace ports +{ + +// Port numbers according to ETSI TS 103 248 v2.3.1 (2024-03) +static const port_type CAM = host_cast(2001); +static const port_type DENM = host_cast(2002); +static const port_type TOPO = host_cast(2003); +static const port_type SPAT = host_cast(2004); +static const port_type SAM = host_cast(2005); +static const port_type IVIM = host_cast(2006); +static const port_type SREM = host_cast(2007); +static const port_type SSEM = host_cast(2008); +static const port_type CPM = host_cast(2009); +static const port_type EVCSN_POI = host_cast(2010); +static const port_type TRM = host_cast(2011); +static const port_type TCM = host_cast(2011); +static const port_type VDRM = host_cast(2011); +static const port_type VDPM = host_cast(2011); +static const port_type EOFM = host_cast(2011); +static const port_type EV_RSR = host_cast(2012); +static const port_type RTCMEM = host_cast(2013); +static const port_type CTLM = host_cast(2014); +static const port_type CRLM = host_cast(2015); +static const port_type EC_AT_REQUEST = host_cast(2016); +static const port_type MCDM = host_cast(2017); +static const port_type VAM = host_cast(2018); +static const port_type IMZM = host_cast(2019); +static const port_type DSM = host_cast(2020); +static const port_type P2P_CRL = host_cast(2021); +static const port_type P2P_DCTL = host_cast(2022); +static const port_type MRM = host_cast(2023); +static const port_type P2P_FCTL = host_cast(2024); + +} // namespace ports + +} // namespace btp +} // namespace vanetza + +#endif /* PORTS_HPP_T2IEFSSC */ +