充电学习—5、healthed 电池服务

1、healthed服务监听接收内核kernel的电池事件,然后上传数据给framware层的batterysevice,BatteryService计算电池的电量,显示,绘制动画等

android电池系统框架:
在这里插入图片描述

2、healthd服务入口:android/system/core/healthd/healthd.cpp 中main函数

healthd_mode_ops是一个关于充电状态的结构体变量,main里面做healthed初始化,并做一个主循环持续

main函数中,首先根据传入的参数不同区分:开机充电、recovery、关机充电。这三种情况,然后指定不同的healthd_mode_ops回调函数

int main(int argc, char **argv) {
。。。。。。。。。。。。。。。
	//healthd_mode_ops是一个关于充电状态的结构体变量,
    healthd_mode_ops = &android_ops;//开机充电时,指向android_ops
。。。。。。。。。。。。。。。。。。。
    ret = healthd_init(); //healthed初始化
。。。。。。。。。。。。。。。。。。。
    healthd_mainloop(); //主循环
}

3、healthed_init函数:

static int healthd_init() {
    epollfd = epoll_create(MAX_EPOLL_EVENTS);//创建一个epoll变量
    if (epollfd == -1) {
        KLOG_ERROR(LOG_TAG,
                   "epoll_create failed; errno=%d\n",
                   errno);
        return -1;
    }

   //和板子级别的初始化,里面其实是一个空函数,什么也没做
    healthd_board_init(&healthd_config);
	//根据系统所处的模式,有三种情况的init,开机充电,关机充电,recovery
    healthd_mode_ops->init(&healthd_config);
	//wakealarm定时器初始化
    wakealarm_init();
	//uevent事件初始化,用以监听电池的uevent事件。
    uevent_init();
	//BatteryMonitor初始化。
    gBatteryMonitor = new BatteryMonitor();//创建batteryMonitor对象
    gBatteryMonitor->init(&healthd_config);//初始化batteryMonitor,打开/sys/class/power_supply,
										   //遍历该节点下的电池参数初始化healthd的config参数
    return 0;
}

4、wakeupalarm_init函数:

static void wakealarm_init(void) {
	//创建一个wakealarm对应的定时器描述符
    wakealarm_fd = timerfd_create(CLOCK_BOOTTIME_ALARM, TFD_NONBLOCK);
    if (wakealarm_fd == -1) {
        KLOG_ERROR(LOG_TAG, "wakealarm_init: timerfd_create failed\n");
        return;
    }
	//将wakealarm事件注册到wakealarm_fd文件节点上以监听wakealarm事件。
    if (healthd_register_event(wakealarm_fd, wakealarm_event))
        KLOG_ERROR(LOG_TAG,
                   "Registration of wakealarm event failed\n");
    //设置alarm唤醒间隔
    wakealarm_set_interval(healthd_config.periodic_chores_interval_fast);
}

5、BatteryService电池监听广播服务:

frameworks/base/services/core/java/com/android/server/BatteryService.java

//将电池监听注册到底层

public void onStart() {
	IBinder b = ServiceManager.getService("batteryproperties");
	final IBatteryPropertiesRegistrar batteryPropertiesRegistrar =
			IBatteryPropertiesRegistrar.Stub.asInterface(b);
	try {
		//注册电池监听,当底层电池电量发生变化调用此监听,并调用update。
		batteryPropertiesRegistrar.registerListener(new BatteryListener());
	} catch (RemoteException e) {
		// Should never happen.
	}
 
	publishBinderService("battery", new BinderService());
	publishLocalService(BatteryManagerInternal.class, new LocalService());
}

//当底层有信息时,会调用update更新BatteryService中相关值。

    private void update(BatteryProperties props) {
        synchronized (mLock) {
            if (!mUpdatesStopped) {
                mBatteryProps = props;
                // Process the new values.
                processValuesLocked(false);
            } else {
                mLastBatteryProps.set(props);
            }
        }
    }

processValuesLocked函数更新上报的电池信息:
电池电量、充电状态、低温关机、低电关机、电池状态、适配器插拔等
在这里:
//发送电池状态变换广播

  sendIntentLocked();

相关推荐

  1. 充电学习—2、开关电源基本原理

    2024-06-17 20:58:02       5 阅读
  2. k8s学习-Health Check

    2024-06-17 20:58:02       30 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-17 20:58:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-17 20:58:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-17 20:58:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-17 20:58:02       18 阅读

热门阅读

  1. 基于开源语音识别项目ChatTTS的推荐

    2024-06-17 20:58:02       7 阅读
  2. 代码随想录打卡第十二天补

    2024-06-17 20:58:02       6 阅读
  3. cocosCreator获取手机剪切板内容

    2024-06-17 20:58:02       7 阅读
  4. python 多线程

    2024-06-17 20:58:02       9 阅读
  5. C++之std::lock_guard和std::unique_lock

    2024-06-17 20:58:02       8 阅读
  6. 异常类型处理 3.0

    2024-06-17 20:58:02       7 阅读