• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Open Harmony 重启调用过程

武飞扬头像
浪迹天涯2018
帮助2

学新通

一.应用端调用

import power from '@ohos.power';

power.shutdownDevice("shutdown_test");

  1.  
    import power from '@ohos.power';
  2.  
    power.shutdownDevice("shutdown_test");

二.客户端native调用

static napi_module g_module 定义了模块的属性.

定义模块模块名:nm_modname,JS应用层通过这个模块名调用。

接口注册函数:nm_register_func,在此函数中注册本模块要导出的接口。

使用DECLARE_NAPI_FUNCTION(“js函数名”, c 实现函数名)定义接口函数

DECLARE_NAPI_FUNCTION("rebootDevice", RebootDevice)

  1.  
    base/powermgr/power_manager/frameworks/napi/power/power.cpp
  2.  
    EXTERN_C_START
  3.  
    /*
  4.  
    * function for module exports
  5.  
    */
  6.  
    static napi_value PowerInit(napi_env env, napi_value exports)
  7.  
    {
  8.  
    POWER_HILOGD(MODULE_JS_NAPI, "%{public}s: enter", __func__);
  9.  
    napi_property_descriptor desc[] = {
  10.  
    DECLARE_NAPI_FUNCTION("shutdownDevice", ShutdownDevice),
  11.  
    DECLARE_NAPI_FUNCTION("rebootDevice", RebootDevice),
  12.  
    DECLARE_NAPI_FUNCTION("isScreenOn", IsScreenOn),
  13.  
    };
  14.  
    NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
  15.  
    POWER_HILOGD(MODULE_JS_NAPI, "%{public}s: exit", __func__);
  16.  
     
  17.  
    return exports;
  18.  
    }
  19.  
    EXTERN_C_END
  20.  
     
  21.  
    /*
  22.  
    * Module definition
  23.  
    */
  24.  
     
  25.  
    定义模块
  26.  
    模块名:nm_modname,JS应用层通过这个模块名调用。
  27.  
    接口注册函数:nm_register_func,在此函数中注册本模块要导出的接口。
  28.  
    static napi_module g_module = {
  29.  
    .nm_version = 1,
  30.  
    .nm_flags = 0,
  31.  
    .nm_filename = "power",
  32.  
    .nm_register_func = PowerInit,
  33.  
    .nm_modname = "power",
  34.  
    .nm_priv = ((void *)0),
  35.  
    .reserved = {0}
  36.  
    };
  37.  
     
  38.  
    注册模块,加载动态库时自动调用注册。
  39.  
    /*
  40.  
    * Module registration
  41.  
    */
  42.  
    extern "C" __attribute__((constructor)) void RegisterPowerModule(void)
  43.  
    {
  44.  
    napi_module_register(&g_module);
  45.  
    }
学新通

对传入参数进行校验和转换成c/c 类型,具体代码位置见注释.

  1.  
    base/powermgr/power_manager/frameworks/napi/power/power.cpp
  2.  
    static napi_value RebootDevice(napi_env env, napi_callback_info info)
  3.  
    {
  4.  
    return RebootOrShutdown(env, info, true);
  5.  
    }
  6.  
     
  7.  
     
  8.  
    static napi_value RebootOrShutdown(napi_env env, napi_callback_info info, bool isReboot)
  9.  
    {
  10.  
    POWER_HILOGD(MODULE_JS_NAPI, "%{public}s: enter, %{public}s", __func__, isReboot ? "reboot" : "shutdown");
  11.  
    size_t argc = 1;
  12.  
    napi_value args[1] = { 0 };
  13.  
    napi_value jsthis;
  14.  
    void *data = nullptr;
  15.  
     
  16.  
    napi_status status = napi_get_cb_info(env, info, &argc, args, &jsthis, &data);
  17.  
    NAPI_ASSERT(env, (status == napi_ok) && (argc >= 1), "failed to get cb info"); //校验传入参数个数
  18.  
    napi_valuetype type = napi_undefined;
  19.  
    NAPI_CALL(env, napi_typeof(env, args[0], &type)); //获取js传入类型
  20.  
    NAPI_ASSERT(env, type == napi_string, "wrong argument type. string expected."); //校验传入参数类型
  21.  
     
  22.  
    char reason[REASON_MAX] = { 0 };
  23.  
    size_t reasonLen = 0;
  24.  
    status = napi_get_value_string_utf8(env, args[0], reason, REASON_MAX - 1, &reasonLen); //接受到参数转换成c/c 类型
  25.  
    if (status != napi_ok) {
  26.  
    POWER_HILOGE(MODULE_JS_NAPI, "%{public}s: get reason failed", __func__);
  27.  
    return nullptr;
  28.  
    }
  29.  
    if (isReboot) {
  30.  
    g_powerMgrClient.RebootDevice(std::string(reason));
  31.  
    } else {
  32.  
    g_powerMgrClient.ShutDownDevice(std::string(reason));
  33.  
    }
  34.  
    POWER_HILOGD(MODULE_JS_NAPI, "%{public}s: reason %{public}s, exit", __func__, reason);
  35.  
    return nullptr;
  36.  
    }
学新通

PowerMgrProxy对象会将重启的请求以IPC方式发送给电源管理服务提供端PowerMgrStub对象处理

  1.  
    base/powermgr/power_manager/frameworks/native/power_mgr_client.cpp
  2.  
    void PowerMgrClient::RebootDevice(const std::string& reason)
  3.  
    {
  4.  
    RETURN_IF(Connect() != ERR_OK);
  5.  
    POWER_HILOGE(MODULE_INNERKIT, "%{public}s called.", __func__);
  6.  
    proxy_->RebootDevice(reason);
  7.  
    }
  8.  
     
  9.  
    PowerMgrProxy给电源管理服务发送请求
  10.  
    base/powermgr/power_manager/services/zidl/src/power_mgr_proxy.cpp
  11.  
    void PowerMgrProxy::RebootDevice(const std::string& reason)
  12.  
    {
  13.  
    sptr<IRemoteObject> remote = Remote();
  14.  
    RETURN_IF(remote == nullptr);
  15.  
     
  16.  
    MessageParcel data;
  17.  
    MessageParcel reply;
  18.  
    MessageOption option;
  19.  
     
  20.  
    if (!data.WriteInterfaceToken(PowerMgrProxy::GetDescriptor())) {
  21.  
    POWER_HILOGE(MODULE_INNERKIT, "PowerMgrProxy::%{public}s write descriptor failed!", __func__);
  22.  
    return;
  23.  
    }
  24.  
     
  25.  
    WRITE_PARCEL_NO_RET(data, String16, Str8ToStr16(reason));
  26.  
     
  27.  
    int ret = remote->SendRequest(static_cast<int>(IPowerMgr::REBOOT_DEVICE), data, reply, option);
  28.  
    if (ret != ERR_OK) {
  29.  
    POWER_HILOGE(MODULE_INNERKIT, "PowerMgrProxy::%{public}s Transact is failed, error code: %d", __func__, ret);
  30.  
    }
  31.  
    }
学新通

三.电源服务内调用

电源管理服务是被加载到foundation的进程内

学新通

通过消息码匹配对应的业务函数,并调用,这里匹配的是RebootDeviceStub

  1.  
    base/powermgr/power_manager/services/zidl/src/power_mgr_stub.cpp
  2.  
    int PowerMgrStub::OnRemoteRequest(uint32_t code, MessageParcel &data,
  3.  
    MessageParcel &reply, MessageOption &option)
  4.  
    {
  5.  
    POWER_HILOGD(MODULE_SERVICE,
  6.  
    "PowerMgrStub::OnRemoteRequest, cmd = %{public}u, flags= %{public}d", code, option.GetFlags());
  7.  
    std::u16string descripter = PowerMgrStub::GetDescriptor();
  8.  
    std::u16string remoteDescripter = data.ReadInterfaceToken();
  9.  
    if (descripter != remoteDescripter) {
  10.  
    POWER_HILOGE(MODULE_SERVICE,
  11.  
    "PowerMgrStub::OnRemoteRequest failed, descriptor is not matched!");
  12.  
    return E_GET_POWER_SERVICE_FAILED;
  13.  
    }
  14.  
    const int DFX_DELAY_MS = 10000;
  15.  
    int id = HiviewDFX::XCollie::GetInstance().SetTimer("PowerMgrStub", DFX_DELAY_MS, nullptr, nullptr,
  16.  
    HiviewDFX::XCOLLIE_FLAG_NOOP);
  17.  
     
  18.  
    int ret = ERR_OK;
  19.  
    switch (code) {
  20.  
    case static_cast<int>(IPowerMgr::WAKEUP_DEVICE):
  21.  
    ret = WakeupDeviceStub(data);
  22.  
    break;
  23.  
    case static_cast<int>(IPowerMgr::SUSPEND_DEVICE):
  24.  
    ret = SuspendDeviceStub(data);
  25.  
    break;
  26.  
    case static_cast<int>(IPowerMgr::REFRESH_ACTIVITY):
  27.  
    ret = RefreshActivityStub(data);
  28.  
    break;
  29.  
    case static_cast<int>(IPowerMgr::REBOOT_DEVICE):
  30.  
    ret = RebootDeviceStub(data);
  31.  
    break;
  32.  
    case static_cast<int>(IPowerMgr::SHUTDOWN_DEVICE):
  33.  
    ret = ShutDownDeviceStub(data);
  34.  
    break;
  35.  
    ......
  36.  
    ......
  37.  
    default:
  38.  
    ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
  39.  
    }
  40.  
    HiviewDFX::XCollie::GetInstance().CancelTimer(id);
  41.  
    return ret;
  42.  
    }
  43.  
     
  44.  
     
  45.  
    int32_t PowerMgrStub::RebootDeviceStub(MessageParcel& data)
  46.  
    {
  47.  
    std::string reason = Str16ToStr8(data.ReadString16());
  48.  
    RebootDevice(reason);
  49.  
    return ERR_OK;
  50.  
    }
学新通

在RebootDevice方法中会判断相关的权限,以及调用ShutdownService对象的Reboot方法

  1.  
    base/powermgr/power_manager/services/native/src/power_mgr_service.cpp
  2.  
    void PowerMgrService::RebootDevice(const std::string& reason)
  3.  
    {
  4.  
    std::lock_guard lock(mutex_);
  5.  
    pid_t pid = IPCSkeleton::GetCallingPid();
  6.  
    auto uid = IPCSkeleton::GetCallingUid();
  7.  
    if (reason.find("updater") != std::string::npos) {
  8.  
    if (!Permission::CheckCallingPermission("ohos.permission.REBOOT_RECOVERY")) {
  9.  
    POWER_HILOGE(MODULE_SERVICE,
  10.  
    "%{public}s Request failed, %{public}d permission check fail",
  11.  
    __func__, pid);
  12.  
    return;
  13.  
    }
  14.  
    } else {
  15.  
    if (!Permission::CheckIsSystemAppByUid(uid)
  16.  
    && !Permission::CheckCallingPermission("ohos.permission.REBOOT")) {
  17.  
    POWER_HILOGE(MODULE_SERVICE,
  18.  
    "%{public}s Request failed, %{public}d permission check fail",
  19.  
    __func__, pid);
  20.  
    return;
  21.  
    }
  22.  
    }
  23.  
    POWER_HILOGI(MODULE_SERVICE, "Cancel auto sleep timer");
  24.  
    powerStateMachine_->CancelDelayTimer(
  25.  
    PowermsEventHandler::CHECK_USER_ACTIVITY_TIMEOUT_MSG);
  26.  
    powerStateMachine_->CancelDelayTimer(
  27.  
    PowermsEventHandler::CHECK_USER_ACTIVITY_OFF_TIMEOUT_MSG);
  28.  
    powerStateMachine_->CancelDelayTimer(
  29.  
    PowermsEventHandler::CHECK_USER_ACTIVITY_SLEEP_TIMEOUT_MSG);
  30.  
     
  31.  
    POWER_HILOGI(MODULE_SERVICE, "PID: %{public}d Call %{public}s !", pid, __func__);
  32.  
    shutdownService_.Reboot(reason);
  33.  
    }
学新通
  1.  
    base/powermgr/power_manager/services/native/src/shutdown_service.cpp
  2.  
    void ShutdownService::Reboot(const std::string& reason)
  3.  
    {
  4.  
    RebootOrShutdown(reason, true);
  5.  
    }
  6.  
     
  7.  
    void ShutdownService::RebootOrShutdown(const std::string& reason, bool isReboot)
  8.  
    {
  9.  
    if (started_) {
  10.  
    POWER_HILOGE(MODULE_SERVICE, "Shutdown is already running.");
  11.  
    return;
  12.  
    }
  13.  
    started_ = true;
  14.  
    make_unique<thread>([=] {
  15.  
    Prepare();
  16.  
    POWER_HILOGD(MODULE_SERVICE, "reason = %{public}s, reboot = %{public}d", reason.c_str(), isReboot);
  17.  
    if (devicePowerAction_ != nullptr) {
  18.  
    isReboot ? devicePowerAction_->Reboot(reason) : devicePowerAction_->Shutdown(reason);
  19.  
    }
  20.  
    started_ = false;
  21.  
    })->detach();
  22.  
    }
学新通

执行系统调用,进行Reboot操作

  1.  
    base/powermgr/power_manager/services/native/src/actions/default/device_power_action.cpp
  2.  
    void DevicePowerAction::Reboot(const std::string& reason)
  3.  
    {
  4.  
    std::string rebootReason = Updater(reason);
  5.  
    POWER_HILOGI(MODULE_SERVICE, "Reboot executing.");
  6.  
    DoReboot(rebootReason.c_str());
  7.  
    }

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhibbbie
系列文章
更多 icon
同类精品
更多 icon
继续加载