Skip to contentSkip to conversation

panel: added sysfs support

PATCH Superseded Dixit Parmar 2 participants 3 messages Nov 7, 2024 – Jun 23, 2025

v4 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 panel: added sysfs support

battery applet supports reading battery and supply information from sysfs entries, /sys/class/power_supply/.

[YOCTO #12904]
https://bugzilla.yoctoproject.org/show_bug.cgi?id=12904

---
 applets/battery/Makefile.am     |   4 +
 applets/battery/battery-sysfs.c | 142 ++++++++++++++++++++++++++++++++
 applets/battery/battery.h       |   3 +-
 configure.ac                    |   4 +
 4 files changed, 152 insertions(+), 1 deletion(-)
 create mode 100644 applets/battery/battery-sysfs.c
applets/battery/Makefile.am +4 -0
index 9b02240..1b93a0b 100644
@@ -5,6 +5,10 @@ applet_LTLIBRARIES = libbattery.la
 libbattery_la_SOURCES = battery.c battery.h
 libbattery_la_CPPFLAGS = $(AM_CPPFLAGS) -DDATADIR=\"$(pkgdatadir)/battery/\"
 
+if HAVE_SYSFS
+libbattery_la_SOURCES += battery-sysfs.c
+endif
+
 if HAVE_LIBAPM
 libbattery_la_LIBADD = -lapm
 libbattery_la_SOURCES += battery-apm.c
applets/battery/battery-sysfs.c new file +142 -0

142 lines not on this page · raw message

applets/battery/battery.h +2 -1
index cf23431..a941aec 100644
@@ -6,8 +6,9 @@
 
 #ifndef MB_APPLET_BATTERY_H
 #define MB_APPLET_BATTERY_H
-
+#include <stdio.h>
 #include <string.h>
+#include <stdlib.h>
 
 int pm_support(void);
 const char* pm_battery_icon(void);
configure.ac +4 -0
index d6c57d8..7038212 100644
@@ -61,6 +61,9 @@ AC_ARG_WITH(
 )
 
 case "$with_battery" in
+     "sysfs") enable_linux_sysfs=yes
+     enable_battery=yes
+     ;;
      "acpi") AC_CHECK_HEADERS(libacpi.h, enable_linux_acpi=yes, AC_MSG_FAILURE([You need to install libacpi]))
      enable_battery=yes
      ;;
@@ -75,6 +78,7 @@ esac
 AM_CONDITIONAL(HAVE_BATTERY, test x$enable_battery = xyes)
 AM_CONDITIONAL(HAVE_LIBAPM, test x$enable_linux_apm = xyes)
 AM_CONDITIONAL(HAVE_LIBACPI, test x$enable_linux_acpi = xyes)
+AM_CONDITIONAL(HAVE_SYSFS, test x$enable_linux_sysfs = xyes)
 
 # glib-genmarshal
 GLIB_GENMARSHAL=`$PKG_CONFIG --variable=glib_genmarshal glib-2.0`  
Signature · 1 line
2.43.0
#1 lore raw

Hi Ross,
Can you please review if everything looks good.

#2 lore raw
In reply to Dixit Parmar
Quoted 2 lines · Dixit Parmar via lists.yoctoproject.org

battery applet supports reading battery and supply information from sysfs entries, /sys/class/power_supply/.

Yay!

Review

Quoted 5 lines

+++ b/applets/battery/battery-sysfs.c

(quoted diff) +2 -0
@@ -0,0 +1,142 @@
 
+static char batt_dev[128] = "";
+static char ac_dev[128] = “";

Hardcoded and relatively-small buffer sizes make me anxious. These strings are dynamically allocated, so don’t use fixed-size buffers and just use heap allocations everywhere.

Quoted 1 line

+int read_file(const char *dev, const char *f, char *buff, size_t len)

This should be a static function.

applets/battery/battery-sysfs.c line 69

Quoted 12 lines
applets/battery/battery-sysfs.c new file +12 -0
+{
+ if(dev != NULL && f != NULL && buff != NULL) {
+ gchar *file_path = g_build_filename(PROCFS_PS_DIR, dev, f, NULL);
+ gchar *file_content = NULL;
+                gsize length;
+ if (g_file_get_contents(file_path, &file_content, &length, NULL)) {
+ if(length <= len) {
+ memset(buff, 0, len);
+ memcpy(buff, file_content, length);
+ } else {
+ length = -1;
+ }

Why do we need to copy the newly allocated buffer and then free it? I think this function should basically just be g_build_filename;g_file_get_contents();g_free(filename);

Quoted 4 lines

+ /* Check battery presence */
+ ret = read_file(batt_dev, "present", buff, sizeof(buff));
+ if(ret > 0) {
+ batt_present = g_ascii_strtoll(buff, &endptr, 10);

This pattern of read_file/stroll() happens a few times, pull that out to a little function.

applets/battery/battery.h line 11

Quoted 10 lines
applets/battery/battery.h +2 -1
@@ -6,8 +6,9 @@
 
 #ifndef MB_APPLET_BATTERY_H
 #define MB_APPLET_BATTERY_H
-
+#include <stdio.h>
 #include <string.h>
+#include <stdlib.h>

The header should only include what is needed for the header: string.h shouldn’t be in there, and stdio/stdlib should be included in the .c if needed (I can’t see any stdio references though).

Sorry again for the incredibly late review!

Thanks,
Ross
IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.