Skip to contentSkip to conversation

panel: added sysfs support

PATCH Superseded Dixit Parmar 2 participants 3 messages Oct 24, 2024 – Nov 7, 2024

v3 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 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 | 155 ++++++++++++++++++++++++++++++++
 applets/battery/battery.h       |   5 +-
 configure.ac                    |   4 +
 4 files changed, 167 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 +155 -0

155 lines not on this page · raw message

applets/battery/battery.h +4 -1
index cf23431..5c60744 100644
@@ -6,8 +6,11 @@
 
 #ifndef MB_APPLET_BATTERY_H
 #define MB_APPLET_BATTERY_H
-
+#include <stdio.h>
 #include <string.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <dirent.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
Quoted 2 lines · Dixit Parmar via lists.yoctoproject.org

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

First, many thanks for this, it’s been on my todo list for far too long! However, some comments to make the code more idiomatically glib.

Quoted 1 line

+int read_file_content(const char *file_path, char *buff, size_t len)

Instead of reinventing the wheel, just use g_file_get_contents() (https://docs.gtk.org/glib/func.file_get_contents.html).

Quoted 1 line

+ snprintf(file_path, sizeof(file_path), "%s/%s/%s", PROCFS_PS_DIR, dev, f);

GLib also provides g_build_filename().

Quoted 1 line

+ dir = opendir(PROCFS_PS_DIR);

And g_dir_open().

Quoted 1 line

+ ac_sts = atoi(buff);

As presumably the files in sysfs are defined as being ASCII digits, it’s probably safer to use g_ascii_strtoll() from glib. The standard C conversion functions are locale sensitive which can cause “interesting” behaviour when parsing.

Thanks Dixit!

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.

#2 lore raw

Thank you for taking out time reviewing this and sharing your feedback. I have covered all your suggestions in v4 patchset and shared. Kindly review. ~Dixit