From d961fc9fb8ba2784dbf5099ace7f773d706746d7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Sergi=20Hern=C3=A0ndez=20Juan?= <shernand@iri.upc.edu>
Date: Thu, 9 Jul 2015 12:54:39 +0000
Subject: [PATCH] Implemented the clear_id_filters() function and added a
 public close() function.

---
 src/can/can.cpp | 22 ++++++++++++++++++++--
 src/can/can.h   |  4 ++++
 2 files changed, 24 insertions(+), 2 deletions(-)

diff --git a/src/can/can.cpp b/src/can/can.cpp
index e65a287..7294035 100755
--- a/src/can/can.cpp
+++ b/src/can/can.cpp
@@ -323,7 +323,8 @@ void CCAN::add_id_filter(unsigned short int can_id,bool invert)
 
   new_filters=new struct can_filter[this->num_filters+1];
   memcpy(new_filters,this->rx_filters,sizeof(struct can_filter)*this->num_filters);
-  delete[] this->rx_filters;
+  if(this->rx_filters!=NULL)
+    delete[] this->rx_filters;
   this->rx_filters=new_filters;
   // update the new filter
   if((can_id&(~CAN_SFF_MASK))!=0)// it's an extended identifier
@@ -353,7 +354,8 @@ void CCAN::add_id_filter(unsigned short int can_id,unsigned short int mask,bool
 
   new_filters=new struct can_filter[this->num_filters+1];
   memcpy(new_filters,this->rx_filters,sizeof(struct can_filter)*this->num_filters);
-  delete[] this->rx_filters;
+  if(this->rx_filters!=NULL)
+    delete[] this->rx_filters;
   this->rx_filters=new_filters;
   // update the new filter
   if((can_id&(~CAN_SFF_MASK))!=0)// it's an extended identifier
@@ -379,7 +381,23 @@ void CCAN::add_id_filter(unsigned short int can_id,unsigned short int mask,bool
 
 void CCAN::clear_id_filters(void)
 {
+  if(this->rx_filters!=NULL)
+  {
+    delete this->rx_filters;
+    this->rx_filters=NULL;
+    this->num_filters=0;
+  }
+  if(setsockopt(this->can_socket_fd,SOL_CAN_RAW,CAN_RAW_FILTER,this->rx_filters,sizeof(struct can_filter)*this->num_filters)==-1)
+  {
+    /* handle exceptions */
+    throw CCommException(_HERE_,"Error while clearing the existing filters",this->comm_id);
+  }
+}
 
+void CCAN::close(void)
+{
+  if(this->can_socket_fd!=-1)
+    this->close();
 }
 
 CCAN::~CCAN()
diff --git a/src/can/can.h b/src/can/can.h
index b695ae4..97f6361 100755
--- a/src/can/can.h
+++ b/src/can/can.h
@@ -243,6 +243,10 @@ class CCAN : protected CComm
      * \brief
      */
     void clear_id_filters(void); 
+    /** 
+     * \brief
+     */
+    void close(void);
     /**
      * \brief Destructor
      *
-- 
GitLab