(原创)android6.0系统Healthd深入分析


概述

Healthd是android4.4之后提出来的一种中介模型,该模型向下监听来自底层的电池事件,向上传递电池数据信息给Framework层的BatteryService用以计算电池电量相关状态信息,BatteryServcie通过传递来的数据来计算电池电量显示,剩余电量,电量级别等信息,如果收到过温报警或者严重低电报警等信息,系统会直接关机,保护硬件。

 

主模块处理流程

Healthd模块代码是在system/core/healthd/,其模块入口在healthd的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;

}

 

 

 

 

 

可以看出Main函数并不长,但是其作用确实巨大的,main函数起着一个统筹兼顾的作用,其他各个模块函数去做一些具体相应的工作,最后汇总到main函数中被调用。

代码中开始便是解析参数,healthd_mode_ops是一个关于充电状态结构体变量,结构体变量里的参数是函数指针,在初始化时指向各个不同的操作函数,当开机充电时变量赋值为&android_ops,关机充电时候变量赋值为&charger_ops。

在ret = healthd_init();中进行一些初始化工作。

static int healthd_init() {

epollfd = epoll_create(MAX_EPOLL_EVENTS);

    if (epollfd == -1) {

       KLOG_ERROR(LOG_TAG,

                   "epoll_create failed; errno=%d\n",

                   errno);

       return -1;

   }

 

    healthd_board_init(&healthd_config);

    healthd_mode_ops->init(&healthd_config);

    wakealarm_init();

    uevent_init();

    gBatteryMonitor = new BatteryMonitor();

    gBatteryMonitor->init(&healthd_config);

    return 0;

}

 

 

 

创建一个epoll的变量将其赋值给epollfd,在healthd_board_init中未作任何事便返回了。

healthd_mode_ops->init调用有两种情况:关机情况下调用charger_ops的init函数;开机情况下调用android_ops的init函数,这里就开机情况来分析。android_ops的init函数指针指向healthd_mode_android_init函数

代码如下:

 

void healthd_mode_android_init(struct healthd_config* /*config*/) {
    ProcessState::self()->setThreadPoolMaxThreadCount(0);//线程池里最大线程数
    IPCThreadState::self()->disableBackgroundScheduling(true);//禁用后台调度
    IPCThreadState::self()->setupPolling(&gBinderFd);//

    if (gBinderFd >= 0) {
        if (healthd_register_event(gBinderFd, binder_event))
            KLOG_ERROR(LOG_TAG,
                       "Register for binder events failed\n");
    }

    gBatteryPropertiesRegistrar = new BatteryPropertiesRegistrar();
    gBatteryPropertiesRegistrar->publish();
}

 

 

 

 

 

 

前面三条语句做初始化工作,设置线程池最大线程数,禁用后台调度,以及将gBinderfd加入到epoll中。healthd_register_event将binder_event事件注册到gBinderfd文件节点用以监听Binder事件。gBatteryPropertiesRegistrar->publish将"batteryproperties"这个Service注册到ServiceManager中

 

 

再来看看wakealarm_init函数:

 

static void wakealarm_init(void) {
    wakealarm_fd = timerfd_create(CLOCK_BOOTTIME_ALARM, TFD_NONBLOCK);
    if (wakealarm_fd == -1) {
        KLOG_ERROR(LOG_TAG, "wa
  • 5
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值