From f0f851a5645235de7936fea9ba2ad164c0e7115f Mon Sep 17 00:00:00 2001 From: Sergi Hernandez Juan <shernand@iri.upc.edu> Date: Wed, 19 May 2021 17:49:32 +0200 Subject: [PATCH] Changed the name of the get_num_bytes() function to get_num_data(). Changed the map of data queues to use a pointer. Added a new map for the new data events. Both maps are updated with the add_filter and clear_filter functions. --- include/can/can.h | 9 ++++-- src/can/can.cpp | 63 +++++++++++++++++++++++++----------- src/examples/test_can_rx.cpp | 2 +- 3 files changed, 53 insertions(+), 21 deletions(-) diff --git a/include/can/can.h b/include/can/can.h index aa2bcdf..00129f1 100755 --- a/include/can/can.h +++ b/include/can/can.h @@ -46,7 +46,8 @@ class CCAN : protected CComm int num_filters; CMutex can_access; // map of ID/data pair - std::map<unsigned int, CQueue> can_messages_map; + std::map<unsigned int, CQueue *> can_data_map; + std::map<unsigned int, std::string> can_event_map; protected: /** * \brief Function to actually open the device @@ -207,6 +208,10 @@ class CCAN : protected CComm * \brief */ std::string get_new_frame_event_id(void); + /** + * \brief + */ + std::string get_new_frame_event_id(unsigned int can_id); /** * \brief */ @@ -234,7 +239,7 @@ class CCAN : protected CComm /** * \brief */ - unsigned int get_num_bytes(unsigned int can_id); + unsigned int get_num_data(unsigned int can_id); /** * \brief */ diff --git a/src/can/can.cpp b/src/can/can.cpp index abe4748..02a3402 100755 --- a/src/can/can.cpp +++ b/src/can/can.cpp @@ -176,7 +176,7 @@ void *CCAN::can_thread(void *param) { can->can_access.enter(); // process the incomming data - num=can->get_num_data(); + num=((CComm *)can)->get_num_data(); while(num>=sizeof(struct can_frame))// there is a whole frame in the input buffer { can->CComm::read((unsigned char *)&new_frame,sizeof(struct can_frame)); @@ -191,14 +191,15 @@ void *CCAN::can_thread(void *param) { //push the new frame into the queue can->rx_frames.push(new_frame); - can->can_messages_map[new_frame.can_id].write(new_frame.data,new_frame.can_dlc); + can->can_data_map[new_frame.can_id]->write(new_frame.data,new_frame.can_dlc); //for (unsigned int i=0; i<new_frame.can_dlc; i++) - // can->can_messages_map[new_frame.can_id].push_back(new_frame.data[i]); + // can->can_data_map[new_frame.can_id].push_back(new_frame.data[i]); // activate the event can->event_server->set_event(can->new_frame_event_id); can->event_server->set_event((std::to_string(new_frame.can_id) + "_new_data_event_id")); } - num-=sizeof(struct can_frame); + num=((CComm *)can)->get_num_data(); + //num-=sizeof(struct can_frame); } can->can_access.exit(); } @@ -218,6 +219,13 @@ std::string CCAN::get_new_frame_event_id(void) return this->new_frame_event_id; } +std::string CCAN::get_new_frame_event_id(unsigned int can_id) +{ + if(this->can_event_map.find(can_id)==this->can_event_map.end()) + throw CCommException(_HERE_,"Invalid can ID",this->comm_id); + return this->can_event_map[can_id]; +} + std::string CCAN::get_frame_error_event_id(void) { return this->frame_error_event_id; @@ -272,13 +280,10 @@ void CCAN::write(unsigned int can_id, unsigned char *data, int len) void CCAN::read(unsigned int can_id, unsigned char *data, int len) { - struct can_frame frame; - int i=0; - this->can_access.enter(); - if(!this->can_messages_map[can_id].is_empty()) + if(!this->can_data_map[can_id]->is_empty()) { - this->can_messages_map[can_id].read(data,len); + this->can_data_map[can_id]->read(data,len); } else { @@ -289,11 +294,16 @@ void CCAN::read(unsigned int can_id, unsigned char *data, int len) this->can_access.exit(); } -unsigned int CCAN::get_num_bytes(unsigned int can_id) +unsigned int CCAN::get_num_data(unsigned int can_id) { - unsigned int num_of_frames = this->can_messages_map[can_id].get_num_data(); + unsigned int num_data; + + if(this->can_data_map.find(can_id)==this->can_data_map.end()) + throw CCommException(_HERE_,"Invalid can ID",this->comm_id); + + num_data = this->can_data_map[can_id]->get_num_data(); - return num_of_frames; + return num_data; } std::string CCAN::add_id_filter(unsigned short int can_id) @@ -325,10 +335,11 @@ std::string CCAN::add_id_filter(unsigned short int can_id) /* handle exceptions */ throw CCommException(_HERE_,"Error while setting up the filters",this->comm_id); } - std::string event_id=std::to_string(can_id) + "_new_data_event_id"; - this->event_server->create_event(event_id); + this->can_data_map[can_id]=new CQueue(); + this->can_event_map[can_id]=std::to_string(can_id) + "_new_data_event_id"; + this->event_server->create_event(this->can_event_map[can_id]); - return event_id; + return this->can_event_map[can_id]; } std::string CCAN::add_id_filter(unsigned short int can_id,unsigned short int mask) @@ -361,14 +372,18 @@ std::string CCAN::add_id_filter(unsigned short int can_id,unsigned short int mas throw CCommException(_HERE_,"Error while setting up the filters",this->comm_id); } - std::string event_id=std::to_string(can_id) + "_new_data_event_id"; - this->event_server->create_event(event_id); + this->can_data_map[can_id]=new CQueue(); + this->can_event_map[can_id]=std::to_string(can_id) + "_new_data_event_id"; + this->event_server->create_event(this->can_event_map[can_id]); - return event_id; + return this->can_event_map[can_id]; } void CCAN::clear_id_filters(void) { + std::map<unsigned int,std::string>::iterator it; + std::map<unsigned int,CQueue *>::iterator data_it; + if(this->rx_filters!=NULL) { delete this->rx_filters; @@ -380,6 +395,11 @@ void CCAN::clear_id_filters(void) /* handle exceptions */ throw CCommException(_HERE_,"Error while clearing the existing filters",this->comm_id); } + + for(it=this->can_event_map.begin();it!=this->can_event_map.end();it++) + this->event_server->delete_event(it->second); + for(data_it=this->can_data_map.begin();data_it!=this->can_data_map.end();data_it++) + delete data_it->second; } void CCAN::close(void) @@ -390,6 +410,9 @@ void CCAN::close(void) CCAN::~CCAN() { + std::map<unsigned int,std::string>::iterator it; + std::map<unsigned int,CQueue *>::iterator data_it; + // close the can device if(this->can_socket_fd!=-1) CComm::close(); @@ -419,5 +442,9 @@ CCAN::~CCAN() this->event_server->delete_event(this->data_requested_event_id); this->data_requested_event_id=""; } + for(it=this->can_event_map.begin();it!=this->can_event_map.end();it++) + this->event_server->delete_event(it->second); + for(data_it=this->can_data_map.begin();data_it!=this->can_data_map.end();data_it++) + delete data_it->second; } diff --git a/src/examples/test_can_rx.cpp b/src/examples/test_can_rx.cpp index 3ae38a9..049504e 100755 --- a/src/examples/test_can_rx.cpp +++ b/src/examples/test_can_rx.cpp @@ -33,7 +33,7 @@ int main(int argc,char *argv[]) { event_server->wait_all(events); std::cout << "[" << std::dec << can_port.get_last_rx_timestamp() << "]" << std::endl; - len = can_port.get_num_bytes(can_id); + len = can_port.get_num_data(can_id); can_port.read(can_id,data,len); std::cout << "can id: 0x" << std::hex << can_id << std::endl; std::cout << "length: " << len << std::endl; -- GitLab