libhealthloop: Only wake up for power supply events

healthd receives power supply information as uevents and holds a
wakelock while receiving these uevents. Without uevent filter, suspend
is postponed indefinitely if a uevent is generated during suspend. Fix
this by attaching a BPF program to the uevent socket that filters out
all events that are not power supply events.

This CL replaces the following CLs:
* Lianwei Wang, healthd: Don't set all eventpoll wakeup-able,
  2015-07-09
  (https://android-review.googlesource.com/c/platform/system/core/+/158851).
* Stephane Lee, Add BPF filter to filter uevents for
  SUBSYSTEM=powersupply, 2022-05-06.

Multiple ideas and some code in this CL have been borrowed from
Stephane Lee's CL.

Bug: 139203596
Bug: 140330870
Bug: 203131934
Bug: 203229817
Bug: 203462310
Bug: 221725014
Test: Verified that a Pixel 2024 still wakes up if a USB cable is connected or disconnected.
Test: Verified that suspend and resume still works for a Pixel 2024 device.
Test: Verified that the following text appears in the Cuttlefish logcat output: "HealthLoop: Successfully attached BPF program to uevent socket"
Test: Verified as follows that recovery mode works fine with Cuttlefish: adb reboot recovery && adb root && adb shell dmesg | grep -E 'F DEBUG|HealthLoop'
Change-Id: I64446b103d660d220880461bdef7ef0f531e1734
Signed-off-by: Bart Van Assche <bvanassche@google.com>
diff --git a/health/utils/libhealthloop/Android.bp b/health/utils/libhealthloop/Android.bp
index b266f67..4ebc575 100644
--- a/health/utils/libhealthloop/Android.bp
+++ b/health/utils/libhealthloop/Android.bp
@@ -21,6 +21,17 @@
     default_applicable_licenses: ["hardware_interfaces_license"],
 }
 
+bpf {
+    name: "filterPowerSupplyEvents.o",
+    srcs: ["filterPowerSupplyEvents.c"],
+    // "vendor: true" because all binaries that use this BPF filter are vendor
+    // binaries.
+    vendor: true,
+}
+
+// Since "required" sections are ignored in static library definitions,
+// filterPowerSupplyEvents.o has been added in
+// build/make/target/product/base_vendor.mk.
 cc_library_static {
     name: "libhealthloop",
     vendor_available: true,
@@ -34,6 +45,7 @@
         "libcutils",
     ],
     header_libs: [
+        "bpf_headers",
         "libbatteryservice_headers",
         "libhealthd_headers",
         "libutils_headers",
@@ -42,3 +54,30 @@
         "include",
     ],
 }
+
+genrule {
+    name: "filterPowerSupplyEvents.h",
+    out: ["filterPowerSupplyEvents.h"],
+    srcs: [":filterPowerSupplyEvents.o"],
+    cmd: "cat $(in) | od -v -tx1 | cut -c9- | grep -v '^$$' | sed 's/^/0x/;s/ /, 0x/g;s/^, //;s/$$/,/' > $(out)",
+}
+
+cc_test_host {
+    name: "filterPowerSupplyEventsTest",
+    team: "trendy_team_pixel_system_sw_storage",
+    srcs: [
+        "filterPowerSupplyEventsTest.cpp",
+    ],
+    shared_libs: [
+        "libbase",
+        "libbpf",
+    ],
+    static_libs: [
+        "libgmock",
+    ],
+    generated_headers: [
+        "filterPowerSupplyEvents.h",
+        "libbpf_headers",
+    ],
+    compile_multilib: "64",
+}