无法读取库伦值文件节点解决方案

读取库伦值的目的是为了换算成电流,量化场景功耗用途

1.报错日志

/power_log/debuglogger$ adb shell dmesg | grep -Ei "avc.+.system_server"[   79.942272] logd.auditd: type=1400 audit(1744279324.832:7149): avc:  denied  { read } for  comm="binder:1548_6" name="charge_counter" dev="sysfs" ino=55475 scontext=u:r:system_server:s0 tcontext=u:object_r:sysfs_batteryinfo:s0 tclass=file permissive=0[   79.942359] logd.auditd: type=1400 audit(1744279324.832:7150): avc:  denied  { read } for  comm="binder:1548_6" name="temp" dev="sysfs" ino=55479 scontext=u:r:system_server:s0 tcontext=u:object_r:sysfs_batteryinfo:s0 tclass=file permissive=0

由于库伦值和电池温度节点属于都是 sysfs_batteryinfo默认不允许访问的,故我们需要配置节点域

0

因为上述配置的节点,如果直接按avc配置规则,一般会报如下 neverallow 异常,因为上述配置的节点都是 sysfs_batteryinfo默认不允许访问的,故我们需要配置节点域。

错误配置案例:allow system_server sysfs_batteryinfo:file { read open getattr };,直接无法编译成功,会提示neverallow

​​​​​​​

neverallow check failed at out_sys/soong/.intermediates/system/sepolicy/plat_sepolicy.cil/android_common/plat_sepolicy.cil:14894 from system/sepolicy/private/domain.te:1557  (neverallow base_typeattr_451 sysfs_batteryinfo (file (read open)))        allow at out_sys/soong/.intermediates/system/sepolicy/system_ext_sepolicy.cil/android_common/mssi_64_cn_armv82/system_ext_sepolicy.cil:2885      (allow system_server sysfs_batteryinfo (file (read getattr open)))Failed to generate binaryFailed to build policydb

2.解决方案system/sepolicy/private/domain.te 去掉neverallow配置

哪里不允许修改为允许,例如新增 -system_server​​​​​​​

# Platform must not have access to sysfs_batteryinfo, but should do it via health HALfull_treble_only(`  neverallow {    coredomain    -shell    # For access to block device information under /sys/class/block.    -apexd    # Read sysfs block device information.    -init    # Generate uevents for health info    -ueventd    # Recovery uses health HAL passthrough implementation.    -recovery    # Charger uses health HAL passthrough implementation.    -charger    -incidentd    -system_app    -system_server  } sysfs_batteryinfo:file { open read };')

system_server.te 添加读写权限​​​​​​​

allow system_server sysfs_batteryinfo:dir { search };allow system_server sysfs_batteryinfo:file { read open getattr };

3.代码读取库伦值示例​​​​​​

/** * Get battery charge counter value */private static final String BATTERY_CAPACITY_PATH        = "/sys/class/power_supply/battery/charge_counter";public static int getBatteryCapacity() {    return getBatteryFileNode(BATTERY_CAPACITY_PATH) / 1000;}public static int getBatteryCapacity(Context context) {    BatteryManager batteryManager = (BatteryManager)            context.getSystemService(Context.BATTERY_SERVICE);    if (batteryManager == null) {        return -1;    }    return (int) batteryManager.getLongProperty(            BatteryManager.BATTERY_PROPERTY_CHARGE_COUNTER);}/** * Get cpu temparature */public static String getBatteryTemperature() {    String line = null;    String temp = null;    line = FileUtils.readFileNode("/sys/class/power_supply/battery/temp");    if (line != null) {        temp = line.trim();    } else {        temp = "-1";    }    return temp;}public static int getBatteryTemperature(Context context) {    Intent batteryIntent = context.registerReceiver(null,            new IntentFilter(Intent.ACTION_BATTERY_CHANGED));    return batteryIntent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1);}

4.刷机验证 system_server 进程正常读取charge_counter和temp​​​​​​​

/codes/MTK_A15/alps$ adb shell dumpsys EngineService getBatteryCapacitygetBatteryCapacity=2946,getBatteryTemperature=290,getBatteryApiCapacity=2946000,getBatteryApiTemperature=290

你可能感兴趣的:(功耗)