Skip to content

Commit

Permalink
Closes #7
Browse files Browse the repository at this point in the history
Stack has the capacity of storing all the received raw data in a file passed as argument
  • Loading branch information
Fábio Gonçalves committed Jul 12, 2024
1 parent a1fe7ef commit 716cdae
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 46 deletions.
40 changes: 5 additions & 35 deletions tools/socktap/cam_application.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "cam_application.hpp"
#include <vanetza/btp/ports.hpp>
#include <vanetza/asn1/cam.hpp>
#include <vanetza/asn1/denm.hpp>
#include <vanetza/asn1/packet_visitor.hpp>
#include <vanetza/facilities/cam_functions.hpp>
#include <boost/units/cmath.hpp>
Expand All @@ -19,13 +20,11 @@ using namespace std::chrono;
CamApplication::CamApplication(PositionProvider& positioning, Runtime& rt) :
positioning_(positioning), runtime_(rt), cam_interval_(seconds(1))
{
this->send_to_server = false;
schedule_timer();

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 @@ -61,28 +60,11 @@ 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 Down Expand Up @@ -137,6 +119,8 @@ void CamApplication::indicate(const DataIndication& indication, UpPacketPtr pack
asn1::PacketVisitor<asn1::Cam> visitor;
std::shared_ptr<const asn1::Cam> cam = boost::apply_visitor(visitor, *packet);

packet.get();

std::cout << "CAM application received a packet with " << (cam ? "decodable" : "broken") << " content" << std::endl;
if (cam && print_rx_msg_) {
std::cout << "Received CAM contains\n";
Expand All @@ -149,20 +133,6 @@ 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;
}

}

Expand Down
12 changes: 4 additions & 8 deletions tools/socktap/cam_application.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,15 @@ 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 @@ -47,14 +44,13 @@ 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 */
4 changes: 2 additions & 2 deletions tools/socktap/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ int main(int argc, const char** argv)
ca->setSendToServer(true);
}
if(vm.count("send-to-file") > 0){
ca->setSendToFile(true);
ca->setFile(vm["file"].as<std::string>().data());
context.setSendToFile(true);
context.setFile(vm["file"].as<std::string>().data());
}
apps.emplace(app_name, std::move(ca));
} /*
Expand Down
24 changes: 23 additions & 1 deletion tools/socktap/router_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <vanetza/dcc/interface.hpp>
#include <iostream>
#include <vanetza/common/byte_order.hpp>
#include <fstream>

using namespace vanetza;

Expand All @@ -22,6 +23,7 @@ RouterContext::RouterContext(const geonet::MIB& mib, TimeTrigger& trigger, vanet
update_position_vector();

trigger_.schedule();
this->send_to_file = false;
}

RouterContext::~RouterContext()
Expand All @@ -31,6 +33,13 @@ RouterContext::~RouterContext()
}
}

void RouterContext::setSendToFile(bool send_to_file){
this->send_to_file = send_to_file;
}
void RouterContext::setFile(const char * file_path){
this->file_path = file_path;
}

void RouterContext::log_packet_drop(geonet::Router::PacketDropReason reason)
{
auto reason_string = stringify(reason);
Expand All @@ -55,7 +64,20 @@ void RouterContext::set_link_layer(LinkLayer* link_layer)
void RouterContext::indicate(CohesivePacket&& packet, const EthernetHeader& hdr)
{
if (hdr.source != mib_.itsGnLocalGnAddr.mid() && hdr.type == access::ethertype::GeoNetworking) {
std::cout << "received packet from " << hdr.source << " (" << packet.size() << " bytes)\n";
std::cout << "received packet from " << hdr.source << " (" << packet.size() << " bytes)\n";

const std::vector<uint8_t> buffer = packet.buffer();
if(this->send_to_file){
std::cout << "storing receveived in file\n";
std::ofstream output_file(this->file_path, std::ios::binary | std::ios::app);
long size = (long) packet.size();
output_file.write(reinterpret_cast<const char*>(&size), sizeof(size_t));

output_file.write((const char *)buffer.data(), size);
output_file.close();
}


std::unique_ptr<PacketVariant> up { new PacketVariant(std::move(packet)) };
trigger_.schedule(); // ensure the clock is up-to-date for the security entity
router_.indicate(std::move(up), hdr.source, hdr.destination);
Expand Down
6 changes: 6 additions & 0 deletions tools/socktap/router_context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ class RouterContext

void set_link_layer(LinkLayer*);

void setSendToFile(bool send_to_file);
void setFile(const char * file_path);


private:
void indicate(vanetza::CohesivePacket&& packet, const vanetza::EthernetHeader& hdr);
void log_packet_drop(vanetza::geonet::Router::PacketDropReason);
Expand All @@ -45,6 +49,8 @@ class RouterContext
std::unique_ptr<DccPassthrough> request_interface_;
std::list<Application*> applications_;
bool require_position_fix_ = false;
bool send_to_file = false;
const char* file_path;
};

#endif /* ROUTER_CONTEXT_HPP_KIPUYBY2 */

0 comments on commit 716cdae

Please sign in to comment.