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

lupyuen pushed a commit to branch releases/12.7
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git

commit fe5acd65709d52c25cbe8b3e95ea5129d55a324a
Author: dongjiuzhu1 <dongjiuz...@xiaomi.com>
AuthorDate: Wed Apr 3 20:35:37 2024 +0800

    apps/uorb: change nuttx/list to posix queue
    
    Signed-off-by: dongjiuzhu1 <dongjiuz...@xiaomi.com>
---
 system/uorb/listener.c | 92 ++++++++++++++++++++++++++++++--------------------
 1 file changed, 55 insertions(+), 37 deletions(-)

diff --git a/system/uorb/listener.c b/system/uorb/listener.c
index 6f4af7808..2a2f77f88 100644
--- a/system/uorb/listener.c
+++ b/system/uorb/listener.c
@@ -22,11 +22,10 @@
  * Included Files
  ****************************************************************************/
 
-#include <nuttx/list.h>
-
 #include <ctype.h>
 #include <errno.h>
 #include <dirent.h>
+#include <inttypes.h>
 #include <poll.h>
 #include <signal.h>
 #include <stdio.h>
@@ -34,6 +33,7 @@
 #include <string.h>
 #include <sys/stat.h>
 #include <sys/ioctl.h>
+#include <sys/queue.h>
 #include <unistd.h>
 #include <fcntl.h>
 
@@ -53,31 +53,35 @@
 
 struct listen_object_s
 {
-  struct list_node  node;         /* Node of object info list */
-  struct orb_object object;       /* Object id */
-  orb_abstime       timestamp;    /* Time of lastest generation  */
-  unsigned long     generation;   /* Latest generation */
-  FAR FILE         *file;
+  SLIST_ENTRY(listen_object_s) node; /* Node of object info list */
+
+  struct orb_object object; /* Object id */
+  orb_abstime timestamp;    /* Time of lastest generation */
+  unsigned long generation; /* Latest generation */
+  FAR FILE *file;
 };
 
+SLIST_HEAD(listen_list_s, listen_object_s);
+
 /****************************************************************************
  * Private Function Prototypes
  ****************************************************************************/
 
 static int listener_get_state(FAR struct orb_object *object,
                               FAR struct orb_state *state);
-static int listener_add_object(FAR struct list_node *objlist,
+static int listener_add_object(FAR struct listen_list_s *objlist,
                                FAR struct orb_object *object);
-static void listener_delete_object_list(FAR struct list_node *objlist);
-static int listener_generate_object_list(FAR struct list_node *objlist,
+static void listener_delete_object_list(FAR struct listen_list_s *objlist);
+static int listener_generate_object_list(FAR struct listen_list_s *objlist,
                                          FAR const char *filter);
 static int listener_print(FAR const struct orb_metadata *meta, int fd);
-static void listener_monitor(FAR struct list_node *objlist, int nb_objects,
-                             float topic_rate, int topic_latency,
-                             int nb_msgs, int timeout, bool record);
-static int listener_update(FAR struct list_node *objlist,
+static void listener_monitor(FAR struct listen_list_s *objlist,
+                             int nb_objects, float topic_rate,
+                             int topic_latency, int nb_msgs,
+                             int timeout, bool record);
+static int listener_update(FAR struct listen_list_s *objlist,
                            FAR struct orb_object *object);
-static void listener_top(FAR struct list_node *objlist,
+static void listener_top(FAR struct listen_list_s *objlist,
                          FAR const char *filter,
                          bool only_once);
 static int listener_create_dir(FAR char *dir, size_t size);
@@ -215,7 +219,7 @@ static int listener_get_state(FAR struct orb_object *object,
  *   0 on success, otherwise return negative errno.
  ****************************************************************************/
 
-static int listener_add_object(FAR struct list_node *objlist,
+static int listener_add_object(FAR struct listen_list_s *objlist,
                                FAR struct orb_object *object)
 {
   FAR struct listen_object_s *tmp;
@@ -234,7 +238,7 @@ static int listener_add_object(FAR struct list_node 
*objlist,
   tmp->timestamp       = orb_absolute_time();
   tmp->generation      = ret < 0 ? 0 : state.generation;
   tmp->file            = NULL;
-  list_add_tail(objlist, &tmp->node);
+  SLIST_INSERT_HEAD(objlist, tmp, node);
   return 0;
 }
 
@@ -252,7 +256,7 @@ static int listener_add_object(FAR struct list_node 
*objlist,
  *   0 on success.
  ****************************************************************************/
 
-static int listener_update(FAR struct list_node *objlist,
+static int listener_update(FAR struct listen_list_s *objlist,
                            FAR struct orb_object *object)
 {
   FAR struct listen_object_s *old = NULL;
@@ -261,7 +265,7 @@ static int listener_update(FAR struct list_node *objlist,
 
   /* Check wether object already exist in old list */
 
-  list_for_every_entry(objlist, tmp, struct listen_object_s, node)
+  SLIST_FOREACH(tmp, objlist, node)
     {
       if (tmp->object.meta == object->meta &&
           tmp->object.instance == object->instance)
@@ -335,18 +339,18 @@ static int listener_update(FAR struct list_node *objlist,
  *   None.
  ****************************************************************************/
 
-static void listener_delete_object_list(FAR struct list_node *objlist)
+static void listener_delete_object_list(FAR struct listen_list_s *objlist)
 {
   FAR struct listen_object_s *tmp;
-  FAR struct listen_object_s *next;
 
-  list_for_every_entry_safe(objlist, tmp, next, struct listen_object_s, node)
+  while (!SLIST_EMPTY(objlist))
     {
-      list_delete(&tmp->node);
+      tmp = SLIST_FIRST(objlist);
+      SLIST_REMOVE_HEAD(objlist, node);
       free(tmp);
     }
 
-  list_initialize(objlist);
+  SLIST_INIT(objlist);
 }
 
 /****************************************************************************
@@ -364,7 +368,7 @@ static void listener_delete_object_list(FAR struct 
list_node *objlist)
  *   Negative errno on failure.
  ****************************************************************************/
 
-static int listener_generate_object_list(FAR struct list_node *objlist,
+static int listener_generate_object_list(FAR struct listen_list_s *objlist,
                                          FAR const char *filter)
 {
   FAR struct dirent *entry;
@@ -570,9 +574,10 @@ static int listener_record(FAR const struct orb_metadata 
*meta, int fd,
  *   None
  ****************************************************************************/
 
-static void listener_monitor(FAR struct list_node *objlist, int nb_objects,
-                             float topic_rate, int topic_latency,
-                             int nb_msgs, int timeout, bool record)
+static void listener_monitor(FAR struct listen_list_s *objlist,
+                             int nb_objects, float topic_rate,
+                             int topic_latency, int nb_msgs,
+                             int timeout, bool record)
 {
   FAR struct pollfd *fds;
   char path[PATH_MAX];
@@ -599,7 +604,7 @@ static void listener_monitor(FAR struct list_node *objlist, 
int nb_objects,
 
   /* Prepare pollfd for all objects */
 
-  list_for_every_entry(objlist, tmp, struct listen_object_s, node)
+  SLIST_FOREACH(tmp, objlist, node)
     {
       int fd;
 
@@ -634,7 +639,7 @@ static void listener_monitor(FAR struct list_node *objlist, 
int nb_objects,
       listener_create_dir(path, sizeof(path));
       dir = path + strlen(path);
 
-      list_for_every_entry(objlist, tmp, struct listen_object_s, node)
+      SLIST_FOREACH(tmp, objlist, node)
         {
           sprintf(dir, "%s%d.csv", tmp->object.meta->o_name,
                   tmp->object.instance);
@@ -664,7 +669,7 @@ static void listener_monitor(FAR struct list_node *objlist, 
int nb_objects,
       if (poll(&fds[0], nb_objects, timeout * 1000) > 0)
         {
           i = 0;
-          list_for_every_entry(objlist, tmp, struct listen_object_s, node)
+          SLIST_FOREACH(tmp, objlist, node)
             {
               if (fds[i].revents & POLLIN)
                 {
@@ -706,7 +711,7 @@ static void listener_monitor(FAR struct list_node *objlist, 
int nb_objects,
     }
 
   i = 0;
-  list_for_every_entry(objlist, tmp, struct listen_object_s, node)
+  SLIST_FOREACH(tmp, objlist, node)
     {
       if (fds[i].fd < 0)
         {
@@ -757,7 +762,20 @@ static void listener_monitor(FAR struct list_node 
*objlist, int nb_objects,
  *   None.
  ****************************************************************************/
 
-static void listener_top(FAR struct list_node *objlist,
+static size_t listen_length(FAR struct listen_list_s *objlist)
+{
+  struct listen_object_s *tmp;
+  size_t count = 0;
+
+  SLIST_FOREACH(tmp, objlist, node)
+    {
+      count++;
+    }
+
+  return count;
+}
+
+static void listener_top(FAR struct listen_list_s *objlist,
                          FAR const char *filter,
                          bool only_once)
 {
@@ -791,7 +809,7 @@ static void listener_top(FAR struct list_node *objlist,
           uorbinfo_raw("\033[H"); /* move cursor to top left corner */
         }
 
-      uorbinfo_raw("\033[K" "current objects: %zu", list_length(objlist));
+      uorbinfo_raw("\033[K" "current objects: %zu", listen_length(objlist));
       uorbinfo_raw("\033[K" "%-*s INST #SUB RATE #Q SIZE",
                    ORB_MAX_PRINT_NAME - 2, "NAME");
 
@@ -820,8 +838,8 @@ static void exit_handler(int signo)
 
 int main(int argc, FAR char *argv[])
 {
-  struct list_node objlist;
   FAR struct listen_object_s *tmp;
+  struct listen_list_s objlist;
   float topic_rate = 0;
   int topic_latency = 0;
   int nb_msgs       = 0;
@@ -904,7 +922,7 @@ int main(int argc, FAR char *argv[])
 
   /* Alloc list and exec command */
 
-  list_initialize(&objlist);
+  SLIST_INIT(&objlist);
   ret = listener_generate_object_list(&objlist, filter);
   if (ret <= 0)
     {
@@ -918,7 +936,7 @@ int main(int argc, FAR char *argv[])
   else
     {
       uorbinfo_raw("\nMointor objects num:%d", ret);
-      list_for_every_entry(&objlist, tmp, struct listen_object_s, node)
+      SLIST_FOREACH(tmp, &objlist, node)
         {
           uorbinfo_raw("object_name:%s, object_instance:%d",
                        tmp->object.meta->o_name,

Reply via email to