package ohos.hdi.power.v1_0;import ohos.hdi.power.v1_0.IPowerHdiCallback;import ohos.hdi.power.v1_0.PowerTypes;interface IPowerInterface {
RegisterCallback([in] IPowerHdiCallback ipowerHdiCallback);
StartSuspend();
StopSuspend();
ForceSuspend();
SuspendBlock([in] String name);
SuspendUnblock([in] String name);
PowerDump([out] String info);
}
package ohos.hdi.power.v1_0;
[callback] interface IPowerHdiCallback {
OnSuspend();
OnWakeup();
}
package ohos.hdi.power.v1_0;enum PowerHdfCmd { CMD_REGISTER_CALLBCK = 0,
CMD_START_SUSPEND,
CMD_STOP_SUSPEND,
CMD_FORCE_SUSPEND,
CMD_SUSPEND_BLOCK,
CMD_SUSPEND_UNBLOCK,
CMD_DUMP,
};enum PowerHdfCallbackCmd { CMD_ON_SUSPEND = 0,
CMD_ON_WAKEUP,
};enum PowerHdfState {
AWAKE = 0,
INACTIVE,
SLEEP,
};
import("//drivers/adapter/uhdf2/hdi.gni")if (defined(ohos_lite)) {
group("libpower_proxy_1.0") {
deps = []
public_configs = []
}
} else {
hdi("power") {
module_name = "power_interface_service"
sources = [ "IPowerHdiCallback.idl",
"IPowerInterface.idl",
"PowerTypes.idl",
]
language = "cpp" subsystem_name = "hdf" part_name = "power_device_driver" }
}
namespace OHOS {namespace HDI {namespace Power {namespace V1_0 {using namespace OHOS;
enum {
CMD_POWER_INTERFACE_REGISTER_CALLBACK,
CMD_POWER_INTERFACE_START_SUSPEND,
CMD_POWER_INTERFACE_STOP_SUSPEND,
CMD_POWER_INTERFACE_FORCE_SUSPEND,
CMD_POWER_INTERFACE_SUSPEND_BLOCK,
CMD_POWER_INTERFACE_SUSPEND_UNBLOCK,
CMD_POWER_INTERFACE_POWER_DUMP,
CMD_POWER_INTERFACE_GET_VERSION,
};
class IPowerInterface : public IRemoteBroker {public:
DECLARE_INTERFACE_DESCRIPTOR(u"ohos.hdi.power.v1_0.IPowerInterface");
virtual ~IPowerInterface() = default;
static sptr<IPowerInterface> Get();
static sptr<IPowerInterface> GetInstance(const std::string& serviceName);
virtual int32_t RegisterCallback(const sptr<IPowerHdiCallback>& ipowerHdiCallback) = 0;
virtual int32_t StartSuspend() = 0;
virtual int32_t StopSuspend() = 0;
virtual int32_t ForceSuspend() = 0;
virtual int32_t SuspendBlock(const std::string& name) = 0;
virtual int32_t SuspendUnblock(const std::string& name) = 0;
virtual int32_t PowerDump(std::string& info) = 0;
virtual int32_t GetVersion(uint32_t& majorVer, uint32_t& minorVer) = 0;
};
} // V1_0} // Power} // HDI} // OHOS
static int32_t PowerInterfaceDriverDispatch(struct HdfDeviceIoClient *client, int cmdId, struct HdfSBuf *data,
struct HdfSBuf *reply)
{
......
return hdfPowerInterfaceHost->service->OnRemoteRequest(cmdId, *dataParcel, *replyParcel, option); // 将接口调用转发到stub实现}static int HdfPowerInterfaceDriverBind(struct HdfDeviceObject *deviceObject)
{
......
hdfPowerInterfaceHost->ioservice.Dispatch = PowerInterfaceDriverDispatch;
hdfPowerInterfaceHost->ioservice.Open = NULL;
hdfPowerInterfaceHost->ioservice.Release = NULL;
hdfPowerInterfaceHost->service = new PowerInterfaceImpl();
deviceObject->service = &hdfPowerInterfaceHost->ioservice;
return HDF_SUCCESS;
}
......
struct HdfDriverEntry g_powerinterfaceDriverEntry = {
.moduleVersion = 1,
.moduleName = "power_interface_service",
.Bind = HdfPowerInterfaceDriverBind,
.Init = HdfPowerInterfaceDriverInit,
.Release = HdfPowerInterfaceDriverRelease,
};
#include "v1_0/power_interface_stub.h"/* 继承PowerInterfaceStub并实现IPowerInterface 中的接口*/class PowerInterfaceImpl : public PowerInterfaceStub {public: virtual ~PowerInterfaceImpl() {}
int32_t RegisterCallback(const sptr<IPowerHdiCallback>& ipowerHdiCallback) override;
int32_t StartSuspend() override;
int32_t StopSuspend() override;
int32_t ForceSuspend() override;
int32_t SuspendBlock(const std::string& name) override;
int32_t SuspendUnblock(const std::string& name) override;
int32_t PowerDump(std::string& info) override;
};// 在cpp中对相关接口进行实现,其中调用了内核相关接口,实现了具体功能int32_t PowerInterfaceImpl::StopSuspend()
{
suspendRetry_ = false;
return HDF_SUCCESS;
}int32_t PowerInterfaceImpl::ForceSuspend()
{
suspendRetry_ = false;
NotifyCallback(CMD_ON_SUSPEND);
DoSuspend();
NotifyCallback(CMD_ON_WAKEUP);
return HDF_SUCCESS;
}int32_t PowerInterfaceImpl::SuspendBlock(const std::string& name)
{ std::lock_guard<std::mutex> lock(mutex_);
if (name.empty()) {
return HDF_ERR_INVALID_PARAM;
}
UniqueFd fd(TEMP_FAILURE_RETRY(open(LOCK_PATH, O_RDWR | O_CLOEXEC)));
bool ret = SaveStringToFd(fd, name);
if (!ret) {
return HDF_FAILURE;
}
return HDF_SUCCESS;
}
ohos_executable("call_foo_hdi") {
sources = ["src/call_foo_hdi.cpp",
]
deps = ["//drivers/interface/foo/v1.0:libfoo_proxy_1.0",
]
external_deps = ["hiviewdfx_hilog_native:libhilog","ipc:ipc_core","utils_base:utils",
]
part_name = "bar"subsystem_name = "bar_subsystem"}
#include "v1_0/power_interface.h" //包含Power HDI接口头文件
using namespace OHOS::HDI::Power::V1_0; //使用HDI接口命名空间
namespace OHOS {
namespace PowerMgr {
sptr<IPowerInterface> powerInterface = nullptr;
SystemSuspendController::SystemSuspendController()
{
sptr<IPowerHdiCallback> g_callback = new PowerHdiCallbackImpl();
powerInterface = IPowerInterface::Get(); //调用接口实例化接口获取客户端实例
if (powerInterface == nullptr) {
POWER_HILOGE(COMP_SVC, "The hdf interface is null");
return;
}
powerInterface->RegisterCallback(g_callback); // 调用HDI接口注册电源事件回调}
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70011554/viewspace-2899237/ ,如需转载,请注明出处,否则将追究法律责任。