diff --git a/include/can/can.h b/include/can/can.h index 03fd41459585e1434a9eb3283b203cb6a6cf3a33..57251edcbbe8cead6f980e9f6f8cb78c4090e895 100755 --- a/include/can/can.h +++ b/include/can/can.h @@ -12,11 +12,13 @@ #include <linux/can.h> #include <linux/can/bcm.h> #include <linux/can/raw.h> +#include <map> #include <linux/sockios.h> #include "threadserver.h" #include "eventserver.h" #include <queue> +#include "cqueue.h" /** * \brief CAN driver @@ -44,6 +46,9 @@ class CCAN : protected CComm struct can_filter *rx_filters; int num_filters; CMutex can_access; + // map of ID/data pair + 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 @@ -204,6 +209,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 */ @@ -223,23 +232,27 @@ class CCAN : protected CComm /** * \brief */ - void write_frame(unsigned int can_id, unsigned char *data, int len); + void write(unsigned int can_id, unsigned char *data, int len); /** * \brief */ - void read_frame(unsigned int *can_id, unsigned char *data, int *len); + void read(unsigned int can_id, unsigned char *data, int len); /** * \brief */ + unsigned int get_num_data(unsigned int can_id); + /** + * \brief + */ void request_frame(unsigned int can_id, int len); /** * \brief */ - void add_id_filter(unsigned short int can_id,bool invert); + std::string add_id_filter(unsigned short int can_id); /** * \brief */ - void add_id_filter(unsigned short int can_id, unsigned short int mask,bool invert); + std::string add_id_filter(unsigned short int can_id, unsigned short int mask); /** * \brief */ diff --git a/src/can/can.cpp b/src/can/can.cpp index 292ca74fd01cbdff2d9d04fe3ecd2b6c4eaab603..02a3402150e545dffbc64b0f9246bd301191445e 100755 --- a/src/can/can.cpp +++ b/src/can/can.cpp @@ -176,10 +176,10 @@ 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->read((unsigned char *)&new_frame,sizeof(struct can_frame)); + can->CComm::read((unsigned char *)&new_frame,sizeof(struct can_frame)); if(new_frame.can_id&CAN_RTR_FLAG) { // save the requested can_id @@ -189,12 +189,17 @@ void *CCAN::can_thread(void *param) } else { - //puch the new frame into the queue + //push the new frame into the queue can->rx_frames.push(new_frame); + 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_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(); } @@ -214,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; @@ -242,7 +254,7 @@ unsigned long CCAN::get_last_rx_timestamp(void) return tv.tv_sec*1000000+tv.tv_usec; } -void CCAN::write_frame(unsigned int can_id, unsigned char *data, int len) +void CCAN::write(unsigned int can_id, unsigned char *data, int len) { int num_frames=ceil((double)len/8.0),i,j; struct can_frame frame; @@ -262,24 +274,16 @@ void CCAN::write_frame(unsigned int can_id, unsigned char *data, int len) for(j=0;j<frame.can_dlc;j++) frame.data[j]=data[i*8+j]; // send the frames - this->write((unsigned char *)&frame,sizeof(struct can_frame)); + this->CComm::write((unsigned char *)&frame,sizeof(struct can_frame)); } } -void CCAN::read_frame(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->rx_frames.empty()) + if(!this->can_data_map[can_id]->is_empty()) { - frame=this->rx_frames.front(); - this->rx_frames.pop(); - *can_id=frame.can_id; - *len=frame.can_dlc; //COMMENT: Do we need a cast ? frame.can_dlc is __u8 type variable (see /usr/include/linux/can.h) - for(i=0;i<frame.can_dlc;i++) - data[i]=frame.data[i]; + this->can_data_map[can_id]->read(data,len); } else { @@ -290,7 +294,19 @@ void CCAN::read_frame(unsigned int *can_id, unsigned char *data, int *len) this->can_access.exit(); } -void CCAN::add_id_filter(unsigned short int can_id,bool invert) +unsigned int CCAN::get_num_data(unsigned int can_id) +{ + 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_data; +} + +std::string CCAN::add_id_filter(unsigned short int can_id) { struct can_filter *new_filters; @@ -310,8 +326,8 @@ void CCAN::add_id_filter(unsigned short int can_id,bool invert) this->rx_filters[this->num_filters].can_id=can_id; this->rx_filters[this->num_filters].can_mask=CAN_SFF_MASK; } - if(invert) - this->rx_filters[this->num_filters].can_id|=CAN_INV_FILTER; + //if(invert) + // this->rx_filters[this->num_filters].can_id|=CAN_INV_FILTER; // set up the new filters this->num_filters++; if(setsockopt(this->can_socket_fd,SOL_CAN_RAW,CAN_RAW_FILTER,this->rx_filters,sizeof(struct can_filter)*this->num_filters)==-1) @@ -319,9 +335,14 @@ void CCAN::add_id_filter(unsigned short int can_id,bool invert) /* handle exceptions */ throw CCommException(_HERE_,"Error while setting up the filters",this->comm_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 this->can_event_map[can_id]; } -void CCAN::add_id_filter(unsigned short int can_id,unsigned short int mask,bool invert) +std::string CCAN::add_id_filter(unsigned short int can_id,unsigned short int mask) { struct can_filter *new_filters; @@ -341,8 +362,8 @@ void CCAN::add_id_filter(unsigned short int can_id,unsigned short int mask,bool this->rx_filters[this->num_filters].can_id=can_id; this->rx_filters[this->num_filters].can_mask=mask; } - if(invert) - this->rx_filters[this->num_filters].can_id|=CAN_INV_FILTER; + //if(invert) + // this->rx_filters[this->num_filters].can_id|=CAN_INV_FILTER; // set up the new filters this->num_filters++; if(setsockopt(this->can_socket_fd,SOL_CAN_RAW,CAN_RAW_FILTER,this->rx_filters,sizeof(struct can_filter)*this->num_filters)==-1) @@ -350,10 +371,19 @@ void CCAN::add_id_filter(unsigned short int can_id,unsigned short int mask,bool /* handle exceptions */ throw CCommException(_HERE_,"Error while setting up the filters",this->comm_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 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; @@ -365,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) @@ -375,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(); @@ -404,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/CMakeLists.txt b/src/examples/CMakeLists.txt index 39a241c14d2eb533a4149dec7680359850d22857..dcdee1297e8711425bc3c9b4b996812738ca21e1 100644 --- a/src/examples/CMakeLists.txt +++ b/src/examples/CMakeLists.txt @@ -22,12 +22,6 @@ ADD_EXECUTABLE(test_virtual_can_rx test_virtual_can_rx.cpp) # edit the following line to add the necessary libraries TARGET_LINK_LIBRARIES(test_virtual_can_rx ${IRIUTILS_LIBRARY} comm pthread) -# edit the following line to add the source code for the example and the name of the executable -ADD_EXECUTABLE(test_candump test_candump.cpp) - -# edit the following line to add the necessary libraries -TARGET_LINK_LIBRARIES(test_candump ${IRIUTILS_LIBRARY} comm pthread) - IF(BUILD_FTDI) ADD_EXECUTABLE(test_ftdi test_ftdi.cpp) diff --git a/src/examples/test_can.cpp b/src/examples/test_can.cpp index 75e735682e48f1615296913482e314007c5bd01e..4cea86cfd123db3de9cb3f67fe71b401155720e5 100755 --- a/src/examples/test_can.cpp +++ b/src/examples/test_can.cpp @@ -36,7 +36,7 @@ int main(int argc,char *argv[]) data[9]=0x0A; for(i=0;i<10;i++) { - can_port.write_frame(0x1FB+i,data,10); + can_port.write(0x1FB+i,data,10); sleep(1); } }catch(CCommException &e){ diff --git a/src/examples/test_can_rx.cpp b/src/examples/test_can_rx.cpp index b067f5fc618d6c2af5f290d643cc45e308111fdf..049504e0a6754136ed3f4c7d356aeb41b97c540b 100755 --- a/src/examples/test_can_rx.cpp +++ b/src/examples/test_can_rx.cpp @@ -10,7 +10,7 @@ #include <linux/can/raw.h> #include <linux/can.h> -const std::string can_dev="can1"; +const std::string can_dev="can0"; /** * \example test_can.cpp @@ -22,18 +22,19 @@ int main(int argc,char *argv[]) std::list<std::string> events; CCAN can_port("can_port"); unsigned char data[8]; - unsigned int can_id; + unsigned int can_id=0x200; int i=0,len; events.push_back(can_port.get_new_frame_event_id()); try{ can_port.open(can_dev); - can_port.add_id_filter(0x200,0xFF0,false); + can_port.add_id_filter(can_id,0xFF0); while(1) { event_server->wait_all(events); std::cout << "[" << std::dec << can_port.get_last_rx_timestamp() << "]" << std::endl; - can_port.read_frame(&can_id,data,&len); + 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; std::cout << "data: "; diff --git a/src/examples/test_candump.cpp b/src/examples/test_candump.cpp deleted file mode 100755 index fb9771356d751196d9830707716fd7c5b192c1d8..0000000000000000000000000000000000000000 --- a/src/examples/test_candump.cpp +++ /dev/null @@ -1,60 +0,0 @@ -#include "eventserver.h" -#include "threadserver.h" -#include "commexceptions.h" -#include "can.h" -#include <stdio.h> -#include <unistd.h> -#include <string> -#include <iostream> -#include <iomanip> - -#include <linux/can/raw.h> -#include <linux/can.h> - -const std::string can_dev="slcan0"; - -int main(int argc,char *argv[]) -{ - CEventServer *event_server=CEventServer::instance(); - std::list<std::string> events; - CCAN can_port("can_port"); - CCAN can_port2("can_port2"); - unsigned char data[8]; - unsigned int can_id; - int i=0,len; - int event_id; - - events.push_back(can_port.get_new_frame_event_id()); - events.push_back(can_port2.get_new_frame_event_id()); - try{ - can_port.open(can_dev); - can_port2.open(can_dev); - unsigned short int can_filter = (unsigned short int)(7 << 4); - can_port.add_id_filter(can_filter, 0x0f0, false); - can_filter = (unsigned short int)(1 << 4); - can_port2.add_id_filter(can_filter, 0x0f0, false); - while(1) - { - event_id = event_server->wait_first(events); - if (event_id == 0) - { - can_port.read_frame(&can_id,data,&len); - std::cout << " " << can_dev << " " << std::uppercase << std::hex <<(int)can_id << std::dec << " [" <<(int)len << "] "; - for(i=0;i<len;i++) - std::cout << std::uppercase << std::setw(2) << std::setfill('0') << std::hex << (int)data[i] << " "; - std::cout << std::dec << std::endl; - - } - else if (event_id == 1) - { - can_port2.read_frame(&can_id,data,&len); - std::cout << " " << can_dev << " " << std::uppercase << std::hex <<(int)can_id << std::dec << " [" <<(int)len << "] "; - for(i=0;i<len;i++) - std::cout << std::uppercase << std::setw(2) << std::setfill('0') << std::hex << (int)data[i] << " "; - std::cout << std::dec << std::endl; - } - } - }catch(CCommException &e){ - std::cout << e.what() << std::endl; - } -}