Communication Library for Autonomous Systems v1.0
Reliable and secure communication library for autonomous vehicle systems
Loading...
Searching...
No Matches
csv_agent.h
Go to the documentation of this file.
1#ifndef CSV_AGENT_HPP
2#define CSV_AGENT_HPP
3
4#include <cstring>
5#include <cstdint>
7
8class CSVAgent : public Agent {
9 public:
10 CSVAgent(CAN* bus, const std::string& name, Unit unit, Type type, Address address,
11 DataProducer producer, ResponseHandler handler, std::unique_ptr<ComponentData> data, bool external);
12 ~CSVAgent();
13
14 protected:
15 void reply(Unit unit);
16
17};
18
19inline CSVAgent::CSVAgent(CAN* bus, const std::string& name, Unit unit, Type type, Address address,
20 DataProducer producer, ResponseHandler handler, std::unique_ptr<ComponentData> data, bool external) :
21 Agent(bus, name, unit, type, address, producer, handler, std::move(data), external) {}
22
24
25inline void CSVAgent::reply(Unit unit) {
26 // Safety check: don't reply if agent is being destroyed
27 if (!running()) {
28 return;
29 }
30
31 if (!thread_running()) {
32 return;
33 }
34
35 // Final safety check before calling function pointer
36 if (!running()) {
37 return;
38 }
39
40 // CRITICAL: Call function pointer instead of virtual method
41 // This eliminates the race condition that caused "pure virtual method called"
42 Value value = get(unit);
43
44 // Extract timestamp from the beginning of the CSV data (first 8 bytes)
45 if (value.size() >= sizeof(std::uint64_t)) {
46 std::uint64_t csv_timestamp;
47 std::memcpy(&csv_timestamp, value.data(), sizeof(std::uint64_t));
48
49 // Create message with CSV data (without timestamp) and set timestamp separately
50 const std::uint8_t* csv_data = value.data() + sizeof(std::uint64_t);
51 std::size_t csv_data_size = value.size() - sizeof(std::uint64_t);
52
53 Message msg(Message::Type::RESPONSE, address(), unit, Microseconds::zero(), csv_data, csv_data_size);
54 msg.timestamp(Microseconds(csv_timestamp));
55 msg.external(external());
56
57 // Log sent message to CSV
58 log_message(msg, "SEND");
59
60 can_send(&msg);
61 }
62}
63
64#endif
EPOS-inspired Agent implementation using composition over inheritance.
Definition agent.h:35
Message::Array Value
Definition agent.h:39
Message::Microseconds Microseconds
Definition agent.h:42
Message::Origin Address
Definition agent.h:38
Address address() const
Definition agent.h:648
bool thread_running()
Definition agent.h:656
void log_message(const Message &msg, const std::string &direction)
Definition agent.h:493
bool running()
Definition agent.h:399
const bool external() const
Definition agent.h:66
int can_send(Message *msg)
Definition agent.h:652
Message::Unit Unit
Definition agent.h:40
Value get(Unit unit)
Get data using function pointer instead of virtual method.
Definition agent.h:260
std::string name() const
Definition agent.h:67
Definition bus.h:52
Definition csv_agent.h:8
CSVAgent(CAN *bus, const std::string &name, Unit unit, Type type, Address address, DataProducer producer, ResponseHandler handler, std::unique_ptr< ComponentData > data, bool external)
Definition csv_agent.h:19
void reply(Unit unit)
Reply method that calls function pointer instead of virtual method.
Definition csv_agent.h:25
~CSVAgent()
Definition csv_agent.h:23
Template class for network messages with Clock integration.
Definition message.h:31
Type
Definition message.h:35
std::vector< std::uint8_t >(* DataProducer)(std::uint32_t unit, ComponentData *data)
Function pointer type for data production.
Definition component_functions.hpp:22
void(* ResponseHandler)(void *msg, ComponentData *data)
Function pointer type for response handling.
Definition component_functions.hpp:34
Select_Debug<(Traits< T >::debugged &&Traits< Debug >::error)> db(Debug_Error l)
Definition debug.h:166
T * data()
Definition protocol.h:24