From 992747cef386e829a5addff8e1e3074a6dddf934 Mon Sep 17 00:00:00 2001 From: Xiang Xiao Date: Fri, 29 Jul 2022 02:03:50 +0800 Subject: [PATCH] drivers/battery: Handle the early changed event correctly since it may happen before battery_xxx_register sometime Signed-off-by: Xiang Xiao --- drivers/power/battery_charger.c | 10 ++++++++++ drivers/power/battery_gauge.c | 10 ++++++++++ drivers/power/battery_monitor.c | 10 ++++++++++ include/nuttx/list.h | 5 +++++ 4 files changed, 35 insertions(+) diff --git a/drivers/power/battery_charger.c b/drivers/power/battery_charger.c index f72b79513b..5fce337571 100644 --- a/drivers/power/battery_charger.c +++ b/drivers/power/battery_charger.c @@ -467,6 +467,16 @@ int battery_charger_changed(FAR struct battery_charger_dev_s *dev, FAR struct battery_charger_priv_s *priv; int ret; + /* Event happen too early? */ + + if (list_is_clear(&dev->flist)) + { + /* Yes, record it and return directly */ + + dev->mask |= mask; + return 0; + } + ret = nxsem_wait_uninterruptible(&dev->batsem); if (ret < 0) { diff --git a/drivers/power/battery_gauge.c b/drivers/power/battery_gauge.c index 2a8c38955a..0e36aab74f 100644 --- a/drivers/power/battery_gauge.c +++ b/drivers/power/battery_gauge.c @@ -435,6 +435,16 @@ int battery_gauge_changed(FAR struct battery_gauge_dev_s *dev, FAR struct battery_gauge_priv_s *priv; int ret; + /* Event happen too early? */ + + if (list_is_clear(&dev->flist)) + { + /* Yes, record it and return directly */ + + dev->mask |= mask; + return 0; + } + ret = nxsem_wait_uninterruptible(&dev->batsem); if (ret < 0) { diff --git a/drivers/power/battery_monitor.c b/drivers/power/battery_monitor.c index 49ea4a34a2..8090fc9a83 100644 --- a/drivers/power/battery_monitor.c +++ b/drivers/power/battery_monitor.c @@ -510,6 +510,16 @@ int battery_monitor_changed(FAR struct battery_monitor_dev_s *dev, FAR struct battery_monitor_priv_s *priv; int ret; + /* Event happen too early? */ + + if (list_is_clear(&dev->flist)) + { + /* Yes, record it and return directly */ + + dev->mask |= mask; + return 0; + } + ret = nxsem_wait_uninterruptible(&dev->batsem); if (ret < 0) { diff --git a/include/nuttx/list.h b/include/nuttx/list.h index 4361e7ee16..bbeb16e541 100644 --- a/include/nuttx/list.h +++ b/include/nuttx/list.h @@ -390,6 +390,11 @@ static inline bool list_is_empty(FAR struct list_node *list) return (list->next == list) ? true : false; } +static inline bool list_is_clear(FAR struct list_node *list) +{ + return (list->next == NULL) ? true : false; +} + static inline size_t list_length(FAR struct list_node *list) { FAR struct list_node *node = list;