Skip to content

Commit

Permalink
added write to file
Browse files Browse the repository at this point in the history
  • Loading branch information
fabio-r-goncalves authored and Fábio Gonçalves committed Jun 19, 2024
1 parent 76cd0da commit fb6b08a
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 53 deletions.
48 changes: 47 additions & 1 deletion tools/socktap/cam_application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(){
Expand Down Expand Up @@ -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;
}
Expand All @@ -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();
}

Expand All @@ -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);
}

Expand All @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions tools/socktap/cam_application.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,18 @@ 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();
void on_timer(vanetza::Clock::time_point);

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_;
Expand All @@ -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 */
6 changes: 6 additions & 0 deletions tools/socktap/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string>()->default_value("v2x_data.bin"), "File")
("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")
Expand Down Expand Up @@ -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<std::string>().data());
}
apps.emplace(app_name, std::move(ca));
} /*
else if (app_name == "DE") {
Expand Down
104 changes: 52 additions & 52 deletions vanetza/btp/ports.hpp
Original file line number Diff line number Diff line change
@@ -1,52 +1,52 @@
#ifndef PORTS_HPP_T2IEFSSC
#define PORTS_HPP_T2IEFSSC

#include <vanetza/common/byte_order.hpp>

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<uint16_t>(2001);
static const port_type DENM = host_cast<uint16_t>(2002);
static const port_type TOPO = host_cast<uint16_t>(2003);
static const port_type SPAT = host_cast<uint16_t>(2004);
static const port_type SAM = host_cast<uint16_t>(2005);
static const port_type IVIM = host_cast<uint16_t>(2006);
static const port_type SREM = host_cast<uint16_t>(2007);
static const port_type SSEM = host_cast<uint16_t>(2008);
static const port_type CPM = host_cast<uint16_t>(2009);
static const port_type EVCSN_POI = host_cast<uint16_t>(2010);
static const port_type TRM = host_cast<uint16_t>(2011);
static const port_type TCM = host_cast<uint16_t>(2011);
static const port_type VDRM = host_cast<uint16_t>(2011);
static const port_type VDPM = host_cast<uint16_t>(2011);
static const port_type EOFM = host_cast<uint16_t>(2011);
static const port_type EV_RSR = host_cast<uint16_t>(2012);
static const port_type RTCMEM = host_cast<uint16_t>(2013);
static const port_type CTLM = host_cast<uint16_t>(2014);
static const port_type CRLM = host_cast<uint16_t>(2015);
static const port_type EC_AT_REQUEST = host_cast<uint16_t>(2016);
static const port_type MCDM = host_cast<uint16_t>(2017);
static const port_type VAM = host_cast<uint16_t>(2018);
static const port_type IMZM = host_cast<uint16_t>(2019);
static const port_type DSM = host_cast<uint16_t>(2020);
static const port_type P2P_CRL = host_cast<uint16_t>(2021);
static const port_type P2P_DCTL = host_cast<uint16_t>(2022);
static const port_type MRM = host_cast<uint16_t>(2023);
static const port_type P2P_FCTL = host_cast<uint16_t>(2024);

} // namespace ports

} // namespace btp
} // namespace vanetza

#endif /* PORTS_HPP_T2IEFSSC */

#ifndef PORTS_HPP_T2IEFSSC
#define PORTS_HPP_T2IEFSSC

#include <vanetza/common/byte_order.hpp>

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<uint16_t>(2001);
static const port_type DENM = host_cast<uint16_t>(2002);
static const port_type TOPO = host_cast<uint16_t>(2003);
static const port_type SPAT = host_cast<uint16_t>(2004);
static const port_type SAM = host_cast<uint16_t>(2005);
static const port_type IVIM = host_cast<uint16_t>(2006);
static const port_type SREM = host_cast<uint16_t>(2007);
static const port_type SSEM = host_cast<uint16_t>(2008);
static const port_type CPM = host_cast<uint16_t>(2009);
static const port_type EVCSN_POI = host_cast<uint16_t>(2010);
static const port_type TRM = host_cast<uint16_t>(2011);
static const port_type TCM = host_cast<uint16_t>(2011);
static const port_type VDRM = host_cast<uint16_t>(2011);
static const port_type VDPM = host_cast<uint16_t>(2011);
static const port_type EOFM = host_cast<uint16_t>(2011);
static const port_type EV_RSR = host_cast<uint16_t>(2012);
static const port_type RTCMEM = host_cast<uint16_t>(2013);
static const port_type CTLM = host_cast<uint16_t>(2014);
static const port_type CRLM = host_cast<uint16_t>(2015);
static const port_type EC_AT_REQUEST = host_cast<uint16_t>(2016);
static const port_type MCDM = host_cast<uint16_t>(2017);
static const port_type VAM = host_cast<uint16_t>(2018);
static const port_type IMZM = host_cast<uint16_t>(2019);
static const port_type DSM = host_cast<uint16_t>(2020);
static const port_type P2P_CRL = host_cast<uint16_t>(2021);
static const port_type P2P_DCTL = host_cast<uint16_t>(2022);
static const port_type MRM = host_cast<uint16_t>(2023);
static const port_type P2P_FCTL = host_cast<uint16_t>(2024);

} // namespace ports

} // namespace btp
} // namespace vanetza

#endif /* PORTS_HPP_T2IEFSSC */

0 comments on commit fb6b08a

Please sign in to comment.