在前面的文章中我们提到了Android电池模块中的驱动层,该层主要通过充电芯片驱动去读取电池电量,电池状态等信息。在我们在驱动中拿到电池电量信息的时候是怎么一步步传到Android层的,在这里就需要提到health模块。health模块是Android4.4以后推出的,主要用于向下读取电池驱动中的电量信息,然后向上传递给Framwork中的
BatteryService.
该篇文章主要就Android health模块进行分析(下面关键代码会有注释):
Health模块的代码位置位于/system/core/healthd/,其入口在Healthd.cpp中的main函数中,
int main(int argc, char **argv) {
int ch;
int ret;
klog_set_level(KLOG_LEVEL);
healthd_mode_ops = &android_ops; //开机充电时初始化结构体
if (!strcmp(basename(argv[0]), "charger")) {
healthd_mode_ops = &charger_ops; //关机充电时初始化结构体
} else {
while ((ch = getopt(argc, argv, "cr")) != -1) {
switch (ch) {
case 'c':
healthd_mode_ops = &charger_ops;
break;
case 'r':
healthd_mode_ops = &recovery_ops;
break;
case '?':
default:
KLOG_ERROR(LOG_TAG, "Unrecognized healthd option: %c\n",
optopt);
exit(1);
}
}
}
ret = healthd_init(); //主要做初始化的工作
if (ret) {
KLOG_ERROR("Initialization failed, exiting\n");
exit(2);
}
healthd_mainloop();
KLOG_ERROR("Main loop terminated, exiting\n");
return 3;
}
healthd_mode_ops是一个充电状态的结构体,
正常开机情况下会 将android_ops结构体赋值给healthd_mode_ops ,如果在关机情况下会将 charget_ops结构体赋值给healthd_mode_opos,就是关机充电的使用。
然后在healthd_init中,主要做一些初始化工作:
static int healthd_init() {
epollfd = epoll_create(MAX_EPOLL_EVENTS); //创建epoll用于
if (epollfd == -1) {
KLOG_ERROR(LOG_TAG,