【鸿蒙南向开发】OpenHarmony源码解析之开关机流程分析

1 简介

电源管理服务组件是OpenHarmony电源管理子系统的一个组件,主要负责如下功能:

  1. 系统关机
  2. 重启设备
  3. 检测当前设备的亮灭屏状态
  4. 查询系统是否支持该类型的锁
  5. 创建RunningLock锁
  6. 锁定和持有RunningLock锁
  7. 释放RunningLock锁
  8. 查询当前RunningLock锁是持有状态,还是释放状态
  9. 系统休眠和唤醒以及屏幕开关

本文主要分析开关机功能,包括NAPI接口以及功能实现的整个流程。

图1 电源管理子系统架构图

[图片上传失败…(image-52a8e7-1723020113526)]

图2 电源管理服务组件架构图

image.png

2 电源管理服务组件代码目录

/base/powermgr/power_manager
├── figures                  	# 架构图
├── frameworks                  # FrameWork层
│   ├── dialog                  # Dialog 资源文件
│   ├── include                 # 头文件
│   ├── napi                  	# NAPI
│   └── native                  # Native层
├── interfaces                  # 接口层
│   ├── innerkits               # 内部接口
│   └── kits                    # 外置接口
├── sa_profile                  # SA 配置文件
├── services                    # 服务层
│   ├── native                  # Native 层
│   └── zidl                    # zidl 接口层
├── test                        # 测试用例
└── utils                       # 工具和通用层

3 开关机整体流程

image.png

流程描述:

  1. JavaScript应用调用shutdownDevice执行系统关机功能;
  2. shutdownDevice调用power.cpp注册的NAPI接口ShutdownDevice,ShutdownDevice接口进一步调用NAPI封装的RebootOrShutdown方法,RebootOrShutdown会根据true或false参数,决定是调用PowerMgrClient的系统关机ShutDownDevice方法还是重启设备RebootDevice方法;
  3. ShutDownDevice首先会调用Connect方法,Connect会通过GetSystemAbilityManager方法获取SystemAbilityManager实例,然后通过CheckSystemAbility方法检查对应的SA是否存在,并返回remoteObject对象,最后通过iface_cast构造proxy对象,此时创建了PowerMgrProxy实例,最后ShutDownDevice调用PowerMgrProxy的ShutDownDevice方法;
  4. 电源管理服务代理请求端PowerMgrProxy的ShutDownDevice方法调用SendRequest方法向电源管理服务提供端PowerMgrStub发送系统关机请求;
  5. 电源管理服务提供端PowerMgrStub接收到系统关机请求后,调用ShutDownDeviceStub方法;
  6. PowerMgrService继承PowerMgrStub的ShutDownDeviceStub方法,并进一步调用PowerMgrService的ShutDownDevice方法;
  7. ShutDownDevice首先判断是否持有系统APP的uid权限,以及是否持有"ohos.permission.REBOOT"的权限,随后执行ShutdownService的Shutdown方法;
  8. Shutdown方法进一步执行ShutdownService的RebootOrShutdown方法,根据true或false决定是执行Reboot或Shutdown;
  9. 在Shutdown方法中会通过系统调用方法DoReboot执行shutdown命令;

日志

ShutdownDevice
01-01 06:36:32.772  1200  1209 D 02907/PowerMgrJSNAPI: [power.cpp] RebootOrShutdown# RebootOrShutdown: enter, reboot
01-01 06:36:32.772  1200  1209 E 02900/PowerMgrClient: [power_mgr_client.cpp] RebootDevice# ymz PowerMgrClient::RebootDevice called.
01-01 06:36:32.772  1200  1209 E 02900/PowerMgrClient: [power_mgr_client.cpp] Connect# ymz PowerMgrClient::Connect called.
01-01 06:36:32.772  1200  1209 E 02900/PowerMgrClient: [power_mgr_proxy.cpp] RebootDevice# ymz PowerMgrProxy::RebootDevice called!
01-01 06:45:13.576   373   373 D 02901/PowerMgrService: [power_mgr_stub.cpp] RebootDeviceStub# ymz PowerMgrStub::RebootDeviceStub called!
01-01 06:45:13.576   373   373 I 02901/PowerMgrService: [power_mgr_service.cpp] RebootDevice# ymz PowerMgrService::RebootDevice called!
01-01 06:45:13.576   373   373 I 02901/PowerMgrService: [shutdown_service.cpp] Reboot# ymz ShutdownService::Reboot called!
01-01 06:45:13.576   373   373 I 02901/PowerMgrService: [shutdown_service.cpp] RebootOrShutdown# ymz ShutdownService::RebootOrShutdown called!

3.1 电源管理服务注册

  1. 调用System Ability的MakeAndRegisterAbility接口注册Power Manager Service实例
//OpenHarmony/base/powermgr/power_manager/services/native/src/power_mgr_service.cpp

auto pms = DelayedSpSingleton<PowerMgrService>::GetInstance();
const bool G_REGISTER_RESULT = SystemAbility::MakeAndRegisterAbility(pms.GetRefPtr());

  1. System Ability调用Power Manager Service的OnStart函数实现电源管理服务组件的启动
//OpenHarmony/base/powermgr/power_manager/services/native/src/power_mgr_service.cpp

void PowerMgrService::OnStart()
{
    POWER_HILOGI(MODULE_SERVICE, "OnStart enter.");
    //如果Power Manager Service已启动,则返回(单实例)
    if (//OpenHarmony/base/powermgr/power_manager/services/native/src/power_mgr_service.cppready_) {

        POWER_HILOGE(MODULE_SERVICE, "OnStart is ready, nothing to do.");
        return;
    }

    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值