You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by gn...@apache.org on 2020/01/17 15:38:50 UTC

[incubator-nuttx] branch pr118 updated: drivers/can/can.c: Fix errors with pointers when using internal OS interface (#118)

This is an automated email from the ASF dual-hosted git repository.

gnutt pushed a commit to branch pr118
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git


The following commit(s) were added to refs/heads/pr118 by this push:
     new 3c32b9d  drivers/can/can.c:  Fix errors with pointers when using internal OS interface (#118)
3c32b9d is described below

commit 3c32b9d3a1bec757be9423a20e04e4499b48f327
Author: Oleg <ev...@gmail.com>
AuthorDate: Fri Jan 17 18:38:43 2020 +0300

    drivers/can/can.c:  Fix errors with pointers when using internal OS interface (#118)
    
    * Fix CAN driver to work with internal OS interfaces.  Store internal file pointer in f_priv and use it then to distinct readers (See issue #111)
    * Store reader pointer in f_priv instead of filep
    * Don't need in filep in can_reader_s
---
 drivers/can/can.c       | 32 ++++++++------------------------
 include/nuttx/can/can.h |  1 -
 2 files changed, 8 insertions(+), 25 deletions(-)

diff --git a/drivers/can/can.c b/drivers/can/can.c
index 4556d56..bfb9961 100644
--- a/drivers/can/can.c
+++ b/drivers/can/can.c
@@ -390,7 +390,7 @@ static FAR struct can_reader_s *init_can_reader(FAR struct file *filep)
   reader->fifo.rx_tail  = 0;
 
   nxsem_init(&reader->fifo.rx_sem, 0, 1);
-  reader->filep = filep;
+  filep->f_priv = reader;
 
   return reader;
 }
@@ -509,7 +509,7 @@ static int can_close(FAR struct file *filep)
 
   list_for_every_safe(&dev->cd_readers, node, tmp)
     {
-      if (((FAR struct can_reader_s *)node)->filep == filep)
+      if (((FAR struct can_reader_s *)node) == ((FAR struct can_reader_s *)filep->f_priv))
         {
           list_delete(node);
           kmm_free(node);
@@ -517,6 +517,8 @@ static int can_close(FAR struct file *filep)
         }
     }
 
+  filep->f_priv = NULL;
+
   /* Decrement the references to the driver.  If the reference count will
    * decrement to 0, then uninitialize the driver.
    */
@@ -574,7 +576,6 @@ static ssize_t can_read(FAR struct file *filep, FAR char *buffer,
   FAR struct inode         *inode = filep->f_inode;
   FAR struct can_dev_s     *dev = inode->i_private;
   FAR struct can_reader_s  *reader = NULL;
-  FAR struct list_node     *node;
   FAR struct can_rxfifo_s  *fifo;
   size_t                    nread;
   irqstate_t                flags;
@@ -630,16 +631,8 @@ static ssize_t can_read(FAR struct file *filep, FAR char *buffer,
         }
 #endif /* CONFIG_CAN_ERRORS */
 
-      list_for_every(&dev->cd_readers, node)
-        {
-          if (((FAR struct can_reader_s *) node)->filep == filep)
-            {
-              reader = (FAR struct can_reader_s *)node;
-              break;
-            }
-        }
-
-      DEBUGASSERT(reader != NULL);
+      DEBUGASSERT(filep->f_priv != NULL);
+      reader = (FAR struct can_reader_s *)filep->f_priv;
 
       fifo = &reader->fifo;
 
@@ -1027,7 +1020,6 @@ static int can_poll(FAR struct file *filep, FAR struct pollfd *fds,
   FAR struct inode *inode = (FAR struct inode *)filep->f_inode;
   FAR struct can_dev_s *dev = (FAR struct can_dev_s *)inode->i_private;
   FAR struct can_reader_s *reader = NULL;
-  FAR struct list_node *node;
   pollevent_t eventset;
   int ndx;
   int ret;
@@ -1042,16 +1034,8 @@ static int can_poll(FAR struct file *filep, FAR struct pollfd *fds,
     }
 #endif
 
-  list_for_every(&dev->cd_readers, node)
-    {
-      if (((FAR struct can_reader_s *)node)->filep == filep)
-        {
-          reader = (FAR struct can_reader_s *)node;
-          break;
-        }
-    }
-
-  DEBUGASSERT(reader != NULL);
+  DEBUGASSERT(filep->f_priv != NULL);
+  reader = (FAR struct can_reader_s *)filep->f_priv;
 
   /* Get exclusive access to the poll structures */
 
diff --git a/include/nuttx/can/can.h b/include/nuttx/can/can.h
index 69f3747..3be9d19 100644
--- a/include/nuttx/can/can.h
+++ b/include/nuttx/can/can.h
@@ -561,7 +561,6 @@ struct can_reader_s
 {
   struct list_node     list;
   sem_t                read_sem;
-  FAR struct file     *filep;
   struct can_rxfifo_s  fifo;             /* Describes receive FIFO */
 };