当前位置: 首页 > news >正文

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

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

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)))    <root>    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

相关文章:

  • 项目周期过长,如何拆分里程碑
  • 基于STM32 的实时FFT处理(Matlab+MDK5)
  • UE5 Lyra Experience控件模式详解
  • 主流时序数据库深度对比:TDengine、InfluxDB与IoTDB的技术特性、性能及选型考量
  • 【黑客帝国连接虚拟与现实:数据采集系统(DAQ)硬核技术深度解析】
  • 静态链接part1
  • 图像融合(Image Fusion)是什么
  • Linux网络之局域网yum仓库与apt的实现
  • 算法中的数论基础
  • 客运从业资格证适用哪些岗位
  • Linux 多线程编程实战指南
  • 【DeepSeek前沿科技】DeepSeek与Notion知识库搭建指南
  • 多组学空转数据如何进行整合分析?(SpatialGlue库)
  • 驱动-内核空间和用户空间数据交换
  • 生命篇---心肺复苏、AED除颤仪使用、海姆立克急救法、常见情况急救简介
  • 算法学习C++需注意的基本知识
  • CentOS7更换国内YUM源和Docker简单应用
  • 【创建一个YOLO免环境训练包】
  • 入侵检测snort功能概述
  • Java基础 - 泛型(基本概念)
  • 天迈装饰网站建设项目/上海百度seo公司
  • 小说网站编辑怎么做/免费推广的网站
  • 衡水做淘宝网站建设/找个网站
  • 重庆公司核名在哪个网站/东莞网站设计排行榜
  • 做公司网站怎么推广/微信销售平台
  • 大众汽车网站建设/长春网站建设开发