apps/uorb: change nuttx/list to posix queue

Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
This commit is contained in:
dongjiuzhu1 2024-04-03 20:35:37 +08:00 committed by Lup Yuen Lee
parent 729e1cad6e
commit fe5acd6570

View File

@ -22,11 +22,10 @@
* Included Files * Included Files
****************************************************************************/ ****************************************************************************/
#include <nuttx/list.h>
#include <ctype.h> #include <ctype.h>
#include <errno.h> #include <errno.h>
#include <dirent.h> #include <dirent.h>
#include <inttypes.h>
#include <poll.h> #include <poll.h>
#include <signal.h> #include <signal.h>
#include <stdio.h> #include <stdio.h>
@ -34,6 +33,7 @@
#include <string.h> #include <string.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <sys/queue.h>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
@ -53,31 +53,35 @@
struct listen_object_s struct listen_object_s
{ {
struct list_node node; /* Node of object info list */ SLIST_ENTRY(listen_object_s) node; /* Node of object info list */
struct orb_object object; /* Object id */
orb_abstime timestamp; /* Time of lastest generation */ struct orb_object object; /* Object id */
unsigned long generation; /* Latest generation */ orb_abstime timestamp; /* Time of lastest generation */
FAR FILE *file; unsigned long generation; /* Latest generation */
FAR FILE *file;
}; };
SLIST_HEAD(listen_list_s, listen_object_s);
/**************************************************************************** /****************************************************************************
* Private Function Prototypes * Private Function Prototypes
****************************************************************************/ ****************************************************************************/
static int listener_get_state(FAR struct orb_object *object, static int listener_get_state(FAR struct orb_object *object,
FAR struct orb_state *state); 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); FAR struct orb_object *object);
static void listener_delete_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 list_node *objlist, static int listener_generate_object_list(FAR struct listen_list_s *objlist,
FAR const char *filter); FAR const char *filter);
static int listener_print(FAR const struct orb_metadata *meta, int fd); static int listener_print(FAR const struct orb_metadata *meta, int fd);
static void listener_monitor(FAR struct list_node *objlist, int nb_objects, static void listener_monitor(FAR struct listen_list_s *objlist,
float topic_rate, int topic_latency, int nb_objects, float topic_rate,
int nb_msgs, int timeout, bool record); int topic_latency, int nb_msgs,
static int listener_update(FAR struct list_node *objlist, int timeout, bool record);
static int listener_update(FAR struct listen_list_s *objlist,
FAR struct orb_object *object); 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, FAR const char *filter,
bool only_once); bool only_once);
static int listener_create_dir(FAR char *dir, size_t size); 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. * 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 orb_object *object)
{ {
FAR struct listen_object_s *tmp; 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->timestamp = orb_absolute_time();
tmp->generation = ret < 0 ? 0 : state.generation; tmp->generation = ret < 0 ? 0 : state.generation;
tmp->file = NULL; tmp->file = NULL;
list_add_tail(objlist, &tmp->node); SLIST_INSERT_HEAD(objlist, tmp, node);
return 0; return 0;
} }
@ -252,7 +256,7 @@ static int listener_add_object(FAR struct list_node *objlist,
* 0 on success. * 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 orb_object *object)
{ {
FAR struct listen_object_s *old = NULL; 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 */ /* 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 && if (tmp->object.meta == object->meta &&
tmp->object.instance == object->instance) tmp->object.instance == object->instance)
@ -335,18 +339,18 @@ static int listener_update(FAR struct list_node *objlist,
* None. * 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 *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); 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. * 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 const char *filter)
{ {
FAR struct dirent *entry; FAR struct dirent *entry;
@ -570,9 +574,10 @@ static int listener_record(FAR const struct orb_metadata *meta, int fd,
* None * None
****************************************************************************/ ****************************************************************************/
static void listener_monitor(FAR struct list_node *objlist, int nb_objects, static void listener_monitor(FAR struct listen_list_s *objlist,
float topic_rate, int topic_latency, int nb_objects, float topic_rate,
int nb_msgs, int timeout, bool record) int topic_latency, int nb_msgs,
int timeout, bool record)
{ {
FAR struct pollfd *fds; FAR struct pollfd *fds;
char path[PATH_MAX]; 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 */ /* Prepare pollfd for all objects */
list_for_every_entry(objlist, tmp, struct listen_object_s, node) SLIST_FOREACH(tmp, objlist, node)
{ {
int fd; int fd;
@ -634,7 +639,7 @@ static void listener_monitor(FAR struct list_node *objlist, int nb_objects,
listener_create_dir(path, sizeof(path)); listener_create_dir(path, sizeof(path));
dir = path + strlen(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, sprintf(dir, "%s%d.csv", tmp->object.meta->o_name,
tmp->object.instance); 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) if (poll(&fds[0], nb_objects, timeout * 1000) > 0)
{ {
i = 0; i = 0;
list_for_every_entry(objlist, tmp, struct listen_object_s, node) SLIST_FOREACH(tmp, objlist, node)
{ {
if (fds[i].revents & POLLIN) if (fds[i].revents & POLLIN)
{ {
@ -706,7 +711,7 @@ static void listener_monitor(FAR struct list_node *objlist, int nb_objects,
} }
i = 0; i = 0;
list_for_every_entry(objlist, tmp, struct listen_object_s, node) SLIST_FOREACH(tmp, objlist, node)
{ {
if (fds[i].fd < 0) if (fds[i].fd < 0)
{ {
@ -757,7 +762,20 @@ static void listener_monitor(FAR struct list_node *objlist, int nb_objects,
* None. * 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, FAR const char *filter,
bool only_once) 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[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", uorbinfo_raw("\033[K" "%-*s INST #SUB RATE #Q SIZE",
ORB_MAX_PRINT_NAME - 2, "NAME"); ORB_MAX_PRINT_NAME - 2, "NAME");
@ -820,8 +838,8 @@ static void exit_handler(int signo)
int main(int argc, FAR char *argv[]) int main(int argc, FAR char *argv[])
{ {
struct list_node objlist;
FAR struct listen_object_s *tmp; FAR struct listen_object_s *tmp;
struct listen_list_s objlist;
float topic_rate = 0; float topic_rate = 0;
int topic_latency = 0; int topic_latency = 0;
int nb_msgs = 0; int nb_msgs = 0;
@ -904,7 +922,7 @@ int main(int argc, FAR char *argv[])
/* Alloc list and exec command */ /* Alloc list and exec command */
list_initialize(&objlist); SLIST_INIT(&objlist);
ret = listener_generate_object_list(&objlist, filter); ret = listener_generate_object_list(&objlist, filter);
if (ret <= 0) if (ret <= 0)
{ {
@ -918,7 +936,7 @@ int main(int argc, FAR char *argv[])
else else
{ {
uorbinfo_raw("\nMointor objects num:%d", ret); 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", uorbinfo_raw("object_name:%s, object_instance:%d",
tmp->object.meta->o_name, tmp->object.meta->o_name,