From b8cb081cfca2ac2117c7a6980e62b03f5725cd46 Mon Sep 17 00:00:00 2001 From: zhang Date: Wed, 20 Oct 2021 16:47:47 +0800 Subject: [PATCH 1/3] add module code Signed-off-by: zhang --- displaymgr.gni | 2 + frameworks/napi/brightness.cpp | 116 +++++++++++ .../native/display_power_mgr_client.cpp | 120 +++++++++++ interfaces/innerkits/BUILD.gn | 5 +- .../innerkits/native/include/display_info.h | 16 +- .../native/include/display_power_mgr_client.h | 60 ++++++ .../native/include/idisplay_power_mgr.h | 45 +++++ interfaces/kits/js/napi/BUILD.gn | 7 +- ohos.build | 2 +- service/BUILD.gn | 9 +- .../include/display_power_mgr_service.h | 71 +++++++ .../native/include/display_system_ability.h | 40 ++++ service/native/include/gradual_animator.h | 69 +++++++ service/native/include/screen_action.h | 16 +- service/native/include/screen_controller.h | 45 +++-- .../native/src/display_power_mgr_service.cpp | 87 ++++++++ service/native/src/display_system_ability.cpp | 38 ++++ service/native/src/gradual_animator.cpp | 137 +++++++++++++ service/native/src/screen_action.cpp | 108 +++++++++- service/native/src/screen_controller.cpp | 108 ++++++++-- .../zidl/include/display_power_mgr_proxy.h | 42 ++++ service/zidl/include/display_power_mgr_stub.h | 38 ++++ service/zidl/src/display_power_mgr_proxy.cpp | 190 ++++++++++++++++++ service/zidl/src/display_power_mgr_stub.cpp | 135 +++++++++++++ test/native/unittest/BUILD.gn | 3 +- .../include/display_power_mgr_service_test.h | 28 +++ .../src/display_power_mgr_service_test.cpp | 129 ++++++++++++ utils/native/include/display_mgr_errors.h | 4 +- 28 files changed, 1612 insertions(+), 58 deletions(-) create mode 100644 frameworks/napi/brightness.cpp create mode 100644 frameworks/native/display_power_mgr_client.cpp create mode 100644 interfaces/innerkits/native/include/display_power_mgr_client.h create mode 100644 interfaces/innerkits/native/include/idisplay_power_mgr.h create mode 100644 service/native/include/display_power_mgr_service.h create mode 100644 service/native/include/display_system_ability.h create mode 100644 service/native/include/gradual_animator.h create mode 100644 service/native/src/display_power_mgr_service.cpp create mode 100644 service/native/src/display_system_ability.cpp create mode 100644 service/native/src/gradual_animator.cpp create mode 100644 service/zidl/include/display_power_mgr_proxy.h create mode 100644 service/zidl/include/display_power_mgr_stub.h create mode 100644 service/zidl/src/display_power_mgr_proxy.cpp create mode 100644 service/zidl/src/display_power_mgr_stub.cpp create mode 100644 test/native/unittest/include/display_power_mgr_service_test.h create mode 100644 test/native/unittest/src/display_power_mgr_service_test.cpp diff --git a/displaymgr.gni b/displaymgr.gni index be86402..af29775 100644 --- a/displaymgr.gni +++ b/displaymgr.gni @@ -17,6 +17,8 @@ displaymgr_native_part_name = "display_manager_native" displaymgr_root_path = "//base/powermgr/display_manager" +displaymgr_framework_path = "${displaymgr_root_path}/frameworks" + displaymgr_service_path = "${displaymgr_root_path}/service" displaymgr_interfaces_path = "${displaymgr_root_path}/interfaces" diff --git a/frameworks/napi/brightness.cpp b/frameworks/napi/brightness.cpp new file mode 100644 index 0000000..b4bc8ca --- /dev/null +++ b/frameworks/napi/brightness.cpp @@ -0,0 +1,116 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include + +#include +#include + +#include "display_power_mgr_client.h" +#include "hilog_wrapper.h" + +using namespace OHOS::DisplayPowerMgr; + +struct BrightnessAsyncCallbackInfo { + napi_env env; + napi_async_work asyncWork; + int32_t value; +}; + +static napi_value SetValue(napi_env env, napi_callback_info info) +{ + DISPLAY_HILOGD(MODULE_JS_NAPI, "enter"); + size_t argc = 1; + napi_value args[1] = { 0 }; + napi_value jsthis; + void *data = nullptr; + + napi_status status = napi_get_cb_info(env, info, &argc, args, &jsthis, &data); + NAPI_ASSERT(env, (status == napi_ok) && (argc >= 1), "Failed to get cb info"); + + napi_valuetype type; + NAPI_CALL(env, napi_typeof(env, args[0], &type)); + + NAPI_ASSERT(env, type == napi_number, "Wrong argument type. Numbers expected."); + + int32_t value = 0; + NAPI_CALL(env, napi_get_value_int32(env, args[0], &value)); + + BrightnessAsyncCallbackInfo* asyncCallbackInfo = new BrightnessAsyncCallbackInfo { + .env = env, + .asyncWork = nullptr, + }; + + asyncCallbackInfo->value = value; + + napi_value resourceName; + napi_create_string_latin1(env, "setValue", NAPI_AUTO_LENGTH, &resourceName); + + napi_create_async_work( + env, + nullptr, + resourceName, + [](napi_env env, void *data) { + BrightnessAsyncCallbackInfo* asyncCallbackInfo = (BrightnessAsyncCallbackInfo *)data; + if (!DisplayPowerMgrClient::GetInstance().SetBrightness(asyncCallbackInfo->value)) { + DISPLAY_HILOGE(MODULE_JS_NAPI, "Failed to set brightness"); + } else { + DISPLAY_HILOGD(MODULE_JS_NAPI, "Succeed to set brightness"); + } + }, + [](napi_env env, napi_status status, void *data) { + BrightnessAsyncCallbackInfo* asyncCallbackInfo = (BrightnessAsyncCallbackInfo *)data; + napi_delete_async_work(env, asyncCallbackInfo->asyncWork); + delete asyncCallbackInfo; + }, + (void *)asyncCallbackInfo, + &asyncCallbackInfo->asyncWork); + + NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackInfo->asyncWork)); + + DISPLAY_HILOGD(MODULE_JS_NAPI, "return"); + return nullptr; +} + +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = { + DECLARE_NAPI_FUNCTION("setValue", SetValue), + }; + NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); + + DISPLAY_HILOGD(MODULE_JS_NAPI, "return"); + + return exports; +} +EXTERN_C_END + +static napi_module g_module = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = "brightness", + .nm_register_func = Init, + .nm_modname = "brightness", + .nm_priv = ((void *)0), + .reserved = { 0 } +}; + +extern "C" __attribute__((constructor)) void RegisterModule(void) +{ + napi_module_register(&g_module); +} diff --git a/frameworks/native/display_power_mgr_client.cpp b/frameworks/native/display_power_mgr_client.cpp new file mode 100644 index 0000000..9d52497 --- /dev/null +++ b/frameworks/native/display_power_mgr_client.cpp @@ -0,0 +1,120 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "display_power_mgr_client.h" + +#include +#include +#include + +#include "display_common.h" + +namespace OHOS { +namespace DisplayPowerMgr { +DisplayPowerMgrClient::DisplayPowerMgrClient() = default; +DisplayPowerMgrClient::~DisplayPowerMgrClient() = default; + +sptr DisplayPowerMgrClient::GetProxy() +{ + std::lock_guard lock(mutex_); + if (proxy_ != nullptr) { + return proxy_; + } + + sptr sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (sam == nullptr) { + DISPLAY_HILOGE(MODULE_INNERKIT, "Failed to get system ability manager"); + return nullptr; + } + sptr obj = sam->CheckSystemAbility(DISPLAY_MANAGER_SERVICE_ID); + if (obj == nullptr) { + DISPLAY_HILOGE(MODULE_INNERKIT, "Failed to get display manager service"); + return nullptr; + } + sptr dr = new DisplayDeathRecipient(*this); + if ((obj->IsProxyObject()) && (!obj->AddDeathRecipient(dr))) { + DISPLAY_HILOGE(MODULE_INNERKIT, "Failed to add death recipient"); + return nullptr; + } + + proxy_ = iface_cast(obj); + deathRecipient_ = dr; + DISPLAY_HILOGI(MODULE_INNERKIT, "Succeed to connect display manager service"); + return proxy_; +} + +void DisplayPowerMgrClient::OnRemoteDied(const wptr& remote) +{ + if (remote == nullptr) { + DISPLAY_HILOGE(MODULE_INNERKIT, "OnRemoteDied failed, remote is nullptr"); + return; + } + + std::lock_guard lock(mutex_); + RETURN_IF(proxy_ == nullptr); + + auto serviceRemote = proxy_->AsObject(); + if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) { + serviceRemote->RemoveDeathRecipient(deathRecipient_); + proxy_ = nullptr; + } +} + +bool DisplayPowerMgrClient::SetDisplayState(DisplayState state, uint32_t id) +{ + auto proxy = GetProxy(); + if (proxy == nullptr) { + return true; + } + return proxy->SetDisplayState(id, state); +} + +DisplayState DisplayPowerMgrClient::GetDisplayState(uint32_t id) +{ + auto proxy = GetProxy(); + if (proxy == nullptr) { + return DisplayState::DISPLAY_UNKNOWN; + } + return proxy->GetDisplayState(id); +} + +bool DisplayPowerMgrClient::SetBrightness(uint32_t value, uint32_t id) +{ + auto proxy = GetProxy(); + if (proxy == nullptr) { + return true; + } + return proxy->SetBrightness(id, value); +} + +bool DisplayPowerMgrClient::AdjustBrightness(uint32_t value, uint32_t duration, uint32_t id) +{ + auto proxy = GetProxy(); + if (proxy == nullptr) { + return true; + } + return proxy->AdjustBrightness(id, value, duration); +} + +bool DisplayPowerMgrClient::SetStateConfig(DisplayState state, uint32_t value, uint32_t id) +{ + auto proxy = GetProxy(); + if (proxy == nullptr) { + return true; + } + return proxy->SetStateConfig(id, state, value); +} +} // namespace DisplayPowerMgr +} // namespace OHOS diff --git a/interfaces/innerkits/BUILD.gn b/interfaces/innerkits/BUILD.gn index b236aad..92cf726 100644 --- a/interfaces/innerkits/BUILD.gn +++ b/interfaces/innerkits/BUILD.gn @@ -26,9 +26,8 @@ config("displaymgr_public_config") { ohos_shared_library("displaymgr") { sources = [ - "${displaymgr_service_path}/zidl/src/display_mgr_proxy.cpp", - "native/src/display_manager.cpp", - "native/src/display_mgr_client.cpp", + "${displaymgr_framework_path}/native/display_power_mgr_client.cpp", + "${displaymgr_service_path}/zidl/src/display_power_mgr_proxy.cpp", ] configs = [ diff --git a/interfaces/innerkits/native/include/display_info.h b/interfaces/innerkits/native/include/display_info.h index c578038..5117781 100644 --- a/interfaces/innerkits/native/include/display_info.h +++ b/interfaces/innerkits/native/include/display_info.h @@ -17,11 +17,17 @@ #define DISPLAYMGR_DISPLAY_INFO_H namespace OHOS { -namespace DisplayMgr { -enum class ScreenState : uint32_t { - SCREEN_STATE_OFF = 0, - SCREEN_STATE_ON = 1, +namespace DisplayPowerMgr { +/** + * Display State + */ +enum class DisplayState : uint32_t { + DISPLAY_OFF = 0, + DISPLAY_DIM = 1, + DISPLAY_ON = 2, + DISPLAY_SUSPEND = 3, + DISPLAY_UNKNOWN = 4, }; -} // namespace DisplayMgr +} // namespace DisplayPowerMgr } // namespace OHOS #endif // DISPLAYMGR_DISPLAY_INFO_H diff --git a/interfaces/innerkits/native/include/display_power_mgr_client.h b/interfaces/innerkits/native/include/display_power_mgr_client.h new file mode 100644 index 0000000..3478eb3 --- /dev/null +++ b/interfaces/innerkits/native/include/display_power_mgr_client.h @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef DISPLAYMGR_DISPLAY_MGR_CLIENT_H +#define DISPLAYMGR_DISPLAY_MGR_CLIENT_H + +#include +#include + +#include "display_info.h" +#include "idisplay_power_mgr.h" + +namespace OHOS { +namespace DisplayPowerMgr { +class DisplayPowerMgrClient : public DelayedRefSingleton { + DECLARE_DELAYED_REF_SINGLETON(DisplayPowerMgrClient); + +public: + bool SetDisplayState(DisplayState state, uint32_t id = 0); + DisplayState GetDisplayState(uint32_t id = 0); + bool SetBrightness(uint32_t value, uint32_t id = 0); + bool AdjustBrightness(uint32_t value, uint32_t duration, uint32_t id = 0); + bool SetStateConfig(DisplayState state, uint32_t value, uint32_t id = 0); + +private: + class DisplayDeathRecipient : public IRemoteObject::DeathRecipient { + public: + explicit DisplayDeathRecipient(DisplayPowerMgrClient& client) : client_(client) {} + ~DisplayDeathRecipient() override = default; + void OnRemoteDied(const wptr& remote) override + { + client_.OnRemoteDied(remote); + } + + private: + DisplayPowerMgrClient& client_; + }; + + sptr GetProxy(); + void OnRemoteDied(const wptr& remote); + + std::mutex mutex_; + sptr proxy_ {nullptr}; + sptr deathRecipient_ {nullptr}; +}; +} // namespace DisplayPowerMgr +} // namespace OHOS +#endif // DISPLAYMGR_GRADUAL_ANIMATOR_H diff --git a/interfaces/innerkits/native/include/idisplay_power_mgr.h b/interfaces/innerkits/native/include/idisplay_power_mgr.h new file mode 100644 index 0000000..cf2f5e9 --- /dev/null +++ b/interfaces/innerkits/native/include/idisplay_power_mgr.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef DISPLAYMGR_IDISPLAY_MGR_H +#define DISPLAYMGR_IDISPLAY_MGR_H + +#include + +#include "display_info.h" + +namespace OHOS { +namespace DisplayPowerMgr { +class IDisplayPowerMgr : public IRemoteBroker { +public: + enum { + SET_DISPLAY_STATE = 0, + GET_DISPLAY_STATE, + SET_BRIGHTNESS, + ADJUST_BRIGHTNESS, + SET_STATE_CONFIG, + }; + + virtual bool SetDisplayState(uint32_t id, DisplayState state) = 0; + virtual DisplayState GetDisplayState(uint32_t id) = 0; + virtual bool SetBrightness(uint32_t id, int32_t value) = 0; + virtual bool AdjustBrightness(uint32_t id, int32_t value, uint32_t duration) = 0; + virtual bool SetStateConfig(uint32_t id, DisplayState state, int32_t value) = 0; + + DECLARE_INTERFACE_DESCRIPTOR(u"ohos.displaypowermgr.IDisplayPowerMgr"); +}; +} // namespace DisplayPowerMgr +} // namespace OHOS +#endif // DISPLAYMGR_IDISPLAY_MGR_H diff --git a/interfaces/kits/js/napi/BUILD.gn b/interfaces/kits/js/napi/BUILD.gn index febc174..2636e26 100644 --- a/interfaces/kits/js/napi/BUILD.gn +++ b/interfaces/kits/js/napi/BUILD.gn @@ -14,7 +14,7 @@ import("//base/powermgr/display_manager/displaymgr.gni") ohos_shared_library("brightness") { - sources = [ "brightness.cpp" ] + sources = [ "${displaymgr_framework_path}/napi/brightness.cpp" ] include_dirs = [ "//foundation/ace/napi/native_engine", @@ -29,7 +29,10 @@ ohos_shared_library("brightness") { "//foundation/ace/napi:ace_napi", ] - external_deps = [ "hiviewdfx_hilog_native:libhilog" ] + external_deps = [ + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + ] relative_install_dir = "module" diff --git a/ohos.build b/ohos.build index 932c203..8a59632 100644 --- a/ohos.build +++ b/ohos.build @@ -14,7 +14,7 @@ "header": { "header_files": [ "display_info.h", - "display_manager.h" + "display_power_mgr_client.h" ], "header_base": "//base/powermgr/display_manager/interfaces/innerkits/native/include" } diff --git a/service/BUILD.gn b/service/BUILD.gn index 87282f7..f9250ee 100644 --- a/service/BUILD.gn +++ b/service/BUILD.gn @@ -26,10 +26,12 @@ config("displaymgr_public_config") { ohos_shared_library("displaymgrservice") { sources = [ - "native/src/display_mgr_service.cpp", + "native/src/display_power_mgr_service.cpp", + "native/src/display_system_ability.cpp", + "native/src/gradual_animator.cpp", "native/src/screen_action.cpp", "native/src/screen_controller.cpp", - "zidl/src/display_mgr_stub.cpp", + "zidl/src/display_power_mgr_stub.cpp", ] configs = [ @@ -42,10 +44,13 @@ ohos_shared_library("displaymgrservice") { deps = [ "${displaymgr_native_innerkits_path}:displaymgr", "//drivers/peripheral/display/hal:hdi_display_device", + "//foundation/graphic/standard:libwmservice", "//utils/native/base:utils", ] external_deps = [ + "appexecfwk_standard:appexecfwk_base", + "appexecfwk_standard:libeventhandler", "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", "safwk:system_ability_fwk", diff --git a/service/native/include/display_power_mgr_service.h b/service/native/include/display_power_mgr_service.h new file mode 100644 index 0000000..96a5fc5 --- /dev/null +++ b/service/native/include/display_power_mgr_service.h @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef DISPLAYMGR_DISPLAY_MGR_SERVICE_H +#define DISPLAYMGR_DISPLAY_MGR_SERVICE_H + +#include +#include +#include + +#include "display_power_mgr_stub.h" +#include "screen_controller.h" +#include "display_common.h" + +namespace OHOS { +namespace DisplayPowerMgr { +class DisplayPowerMgrService : public DisplayPowerMgrStub { +public: + DisplayPowerMgrService(); + ~DisplayPowerMgrService() = default; + virtual bool SetDisplayState(uint32_t id, DisplayState state) override; + virtual DisplayState GetDisplayState(uint32_t id) override; + virtual bool SetBrightness(uint32_t id, int32_t value) override; + virtual bool AdjustBrightness(uint32_t id, int32_t value, uint32_t duration) override; + virtual bool SetStateConfig(uint32_t id, DisplayState state, int32_t value) override; + virtual int32_t Dump(int32_t fd, const std::vector& args) override; + + class DisplaySystemAbility : public SystemAbility { + DECLARE_SYSTEM_ABILITY(DisplaySystemAbility); + + public: + DisplaySystemAbility(int32_t id, bool runOnCreate) : SystemAbility(id, runOnCreate) {} + ~DisplaySystemAbility() override = default; + + void OnStart() override + { + DISPLAY_HILOGI(MODULE_SERVICE, "Start service"); + service_ = new DisplayPowerMgrService(); + if (!Publish(service_)) { + DISPLAY_HILOGE(MODULE_SERVICE, "Failed to publish service"); + } + } + + void OnStop() override + { + DISPLAY_HILOGI(MODULE_SERVICE, "Stop service"); + } + + private: + sptr service_; + }; + REGISTER_SYSTEM_ABILITY_BY_ID(DisplaySystemAbility, DISPLAY_MANAGER_SERVICE_ID, true); + +private: + std::map> controllerMap_; +}; +} // namespace DisplayPowerMgr +} // namespace OHOS +#endif // DISPLAYMGR_DISPLAY_MGR_SERVICE_H diff --git a/service/native/include/display_system_ability.h b/service/native/include/display_system_ability.h new file mode 100644 index 0000000..ec1b3af --- /dev/null +++ b/service/native/include/display_system_ability.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef DISPLAY_SYSTEM_ABILITY_H +#define DISPLAY_SYSTEM_ABILITY_H + +#include +#include +#include "display_power_mgr_service.h" + +namespace OHOS { +namespace DisplayPowerMgr { +class DisplaySystemAbility : public SystemAbility { + DECLARE_SYSTEM_ABILITY(DisplaySystemAbility); + +public: + DisplaySystemAbility(int32_t id, bool runOnCreate) : SystemAbility(id, runOnCreate) {} + ~DisplaySystemAbility() override = default; + + void OnStart() override; + void OnStop() override; + +private: + sptr service_; +}; +} +} +#endif \ No newline at end of file diff --git a/service/native/include/gradual_animator.h b/service/native/include/gradual_animator.h new file mode 100644 index 0000000..f592fac --- /dev/null +++ b/service/native/include/gradual_animator.h @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef DISPLAYMGR_GRADUAL_ANIMATOR_H +#define DISPLAYMGR_GRADUAL_ANIMATOR_H + +#include +#include +#include +#include + +namespace OHOS { +namespace DisplayPowerMgr { +class AnimateCallback { +public: + virtual void onStart() = 0; + virtual void onChanged(int32_t currentValue) = 0; + virtual void onEnd() = 0; +}; + +class GradualAnimator : public std::enable_shared_from_this { +public: + GradualAnimator(const std::string& name, std::shared_ptr callback); + ~GradualAnimator() = default; + void StartAnimation(int32_t from, int32_t to, uint32_t duration); + void StopAnimation(); + bool IsAnimating(); +private: + static const uint32_t DEFAULT_UPDATE_TIME = 200; + static const uint32_t EVENT_STEP = 1; + class AnimatorHandler : public AppExecFwk::EventHandler { + public: + AnimatorHandler(const std::shared_ptr& runner, + std::shared_ptr owner); + ~AnimatorHandler() = default; + void ProcessEvent(const AppExecFwk::InnerEvent::Pointer& event) override; + private: + std::weak_ptr owner_; + }; + void NextStep(); + std::string name_; + std::shared_ptr callback_; + std::shared_ptr eventRunner_; + std::shared_ptr handler_; + bool animating_ = false; + int32_t from_; + int32_t to_; + uint32_t duration_; + uint32_t updateTime_; + uint32_t steps_; + int32_t stride_; + int32_t current_; + uint32_t currentStep_; +}; +} // namespace DisplayPowerMgr +} // namespace OHOS +#endif // DISPLAYMGR_GRADUAL_ANIMATOR_H \ No newline at end of file diff --git a/service/native/include/screen_action.h b/service/native/include/screen_action.h index fbe3fa3..e9682be 100644 --- a/service/native/include/screen_action.h +++ b/service/native/include/screen_action.h @@ -18,18 +18,23 @@ #include #include +#include #include #include "display_info.h" namespace OHOS { -namespace DisplayMgr { +namespace DisplayPowerMgr { class ScreenAction { public: - void Init(); - bool SetPowerState(ScreenState state); - bool SetBrightness(int32_t value); + ScreenAction(); + ~ScreenAction() = default; + std::vector GetDisplayIds(); + DisplayState GetPowerState(uint32_t devId); + bool SetPowerState(uint32_t devId, DisplayState state); + uint32_t GetBrightness(uint32_t devId); + bool SetBrightness(uint32_t devId, uint32_t value); private: struct DeviceFuncCloser { @@ -48,8 +53,9 @@ private: static constexpr int32_t MAX_BRIGHTNESS = 255; static constexpr int32_t MIN_BRIGHTNESS = 6; + std::vector devIds_; DeviceFuncPtr hdiFuncs_; }; -} // namespace DisplayMgr +} // namespace DisplayPowerMgr } // namespace OHOS #endif // DISPLAYMGR_SCREEN_ACTION_H diff --git a/service/native/include/screen_controller.h b/service/native/include/screen_controller.h index 1675150..3ad2c07 100644 --- a/service/native/include/screen_controller.h +++ b/service/native/include/screen_controller.h @@ -16,33 +16,44 @@ #ifndef DISPLAYMGR_SCREEN_CONTROLLER_H #define DISPLAYMGR_SCREEN_CONTROLLER_H +#include #include #include "display_info.h" +#include "gradual_animator.h" #include "screen_action.h" namespace OHOS { -namespace DisplayMgr { -class ScreenController { +namespace DisplayPowerMgr { +class ScreenController : + public AnimateCallback, + public std::enable_shared_from_this { public: - ScreenController(); - ~ScreenController() = default; + ScreenController(uint32_t devId, std::shared_ptr action); + virtual ~ScreenController() = default; - bool UpdateState(ScreenState state); - bool UpdateBrightness(int32_t value); + DisplayState GetState() + { + return state_; + }; + bool UpdateState(DisplayState state); + bool UpdateStateConfig(DisplayState state, uint32_t value); + bool UpdateBrightness(uint32_t value, uint32_t duration = 0); bool IsScreenOn(); - + virtual void onStart() override; + virtual void onChanged(int32_t currentValue) override; + virtual void onEnd() override; private: - inline bool IsScreenStateLocked(ScreenState state) - { - return state_ == state; - } - + static const uint32_t SCREEN_BRIGHTNESS_UPDATE_DURATION = 200; std::mutex mutex_; - ScreenState state_{ScreenState::SCREEN_STATE_ON}; - int32_t brightness_{0}; - ScreenAction action_; + const uint32_t devId_; + DisplayState state_; + std::map stateValues_; + + uint32_t brightness_ {0}; + std::shared_ptr action_; + std::shared_ptr animator_; }; -} // namespace DisplayMgr +} // namespace DisplayPowerMgr } // namespace OHOS -#endif // DISPLAYMGR_SCREEN_CONTROLLER_H +#endif // DISPLAYMGR_SCREEN_CONTROLLER_H \ No newline at end of file diff --git a/service/native/src/display_power_mgr_service.cpp b/service/native/src/display_power_mgr_service.cpp new file mode 100644 index 0000000..ef3c967 --- /dev/null +++ b/service/native/src/display_power_mgr_service.cpp @@ -0,0 +1,87 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "display_power_mgr_service.h" + +#include +#include + +namespace OHOS { +namespace DisplayPowerMgr { +DisplayPowerMgrService::DisplayPowerMgrService() +{ + std::shared_ptr screenAction = std::make_shared(); + std::vector devIds = screenAction->GetDisplayIds(); + int count = devIds.size(); + for (int i = 0; i < count; i++) { + controllerMap_.emplace(devIds[i], std::make_shared(devIds[i], screenAction)); + } +} + +bool DisplayPowerMgrService::SetDisplayState(uint32_t id, DisplayState state) +{ + auto iterater = controllerMap_.find(id); + if (iterater == controllerMap_.end()) { + return false; + } + return iterater->second->UpdateState(state); +} + +DisplayState DisplayPowerMgrService::GetDisplayState(uint32_t id) +{ + auto iterater = controllerMap_.find(id); + if (iterater == controllerMap_.end()) { + return DisplayState::DISPLAY_UNKNOWN; + } + return iterater->second->GetState(); +} + +bool DisplayPowerMgrService::SetBrightness(uint32_t id, int32_t value) +{ + auto iterater = controllerMap_.find(id); + if (iterater == controllerMap_.end()) { + return false; + } + return iterater->second->UpdateBrightness(value); +} + +bool DisplayPowerMgrService::AdjustBrightness(uint32_t id, int32_t value, uint32_t duration) +{ + auto iterater = controllerMap_.find(id); + if (iterater == controllerMap_.end()) { + return false; + } + return iterater->second->UpdateBrightness(value, duration); +} + +bool DisplayPowerMgrService::SetStateConfig(uint32_t id, DisplayState state, int32_t value) +{ + auto iterater = controllerMap_.find(id); + if (iterater == controllerMap_.end()) { + return false; + } + return iterater->second->UpdateStateConfig(state, value); +} + +int32_t DisplayPowerMgrService::Dump(int32_t fd, const std::vector& args) +{ + std::string result("Empty dump info"); + if (!SaveStringToFd(fd, result)) { + DISPLAY_HILOGE(MODULE_SERVICE, "Failed to save dump info to fd"); + } + return ERR_OK; +} +} // namespace DisplayPowerMgr +} // namespace OHOS diff --git a/service/native/src/display_system_ability.cpp b/service/native/src/display_system_ability.cpp new file mode 100644 index 0000000..496b357 --- /dev/null +++ b/service/native/src/display_system_ability.cpp @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "display_system_ability.h" +#include "display_common.h" + +namespace OHOS { +namespace DisplayPowerMgr { +namespace { +REGISTER_SYSTEM_ABILITY_BY_ID(DisplaySystemAbility, DISPLAY_MANAGER_SERVICE_ID, true); +} +void DisplaySystemAbility::OnStart() +{ + DISPLAY_HILOGI(MODULE_SERVICE, "Start service"); + service_ = new DisplayPowerMgrService(); + if (!Publish(service_)) { + DISPLAY_HILOGE(MODULE_SERVICE, "Failed to publish service"); + } +} + +void DisplaySystemAbility::OnStop() +{ + DISPLAY_HILOGI(MODULE_SERVICE, "Stop service"); +} +} +} \ No newline at end of file diff --git a/service/native/src/gradual_animator.cpp b/service/native/src/gradual_animator.cpp new file mode 100644 index 0000000..84b88ce --- /dev/null +++ b/service/native/src/gradual_animator.cpp @@ -0,0 +1,137 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "gradual_animator.h" + +#include "display_common.h" + +namespace OHOS { +namespace DisplayPowerMgr { +GradualAnimator::GradualAnimator(const std::string& name, + std::shared_ptr callback) +{ + DISPLAY_HILOGD(MODULE_SERVICE, "GradualAnimator construct start"); + name_ = name; + callback_ = callback; + eventRunner_ = AppExecFwk::EventRunner::Create(name_); + if (eventRunner_ == nullptr) { + DISPLAY_HILOGW(MODULE_SERVICE, "GradualAnimator failed due to create EventRunner"); + } + from_ = 0; + to_ = 0; + current_ = 0; + duration_ = 0; + steps_ = 0; + stride_ = 0; + updateTime_ = DEFAULT_UPDATE_TIME; + handler_ = nullptr; + DISPLAY_HILOGD(MODULE_SERVICE, "GradualAnimator construct end"); +} + +void GradualAnimator::StartAnimation(int32_t from, int32_t to, uint32_t duration) +{ + DISPLAY_HILOGD(MODULE_SERVICE, + "StartAnimation from=%{public}d, to=%{public}d, duration=%{public}d", + from, to, duration); + if (callback_ == nullptr) { + DISPLAY_HILOGW(MODULE_SERVICE, "Callback is NULL"); + return; + } + from_ = from; + to_ = to; + current_ = from_; + duration_ = duration; + steps_ = duration_ / updateTime_; + if (steps_ < 1) { + steps_ = 1; + } + stride_ = (to_ - from_) / steps_; + currentStep_ = 0; + if (handler_ == nullptr) { + handler_ = std::make_shared(eventRunner_, shared_from_this()); + } + animating_ = true; + handler_->SendEvent(EVENT_STEP, 0, updateTime_); + DISPLAY_HILOGD(MODULE_SERVICE, "StartAnimation end"); +} + +void GradualAnimator::StopAnimation() +{ + DISPLAY_HILOGD(MODULE_SERVICE, "GradualAnimator StopAnimation start"); + animating_ = false; + handler_->RemoveEvent(EVENT_STEP); + if (callback_ == nullptr) { + DISPLAY_HILOGW(MODULE_SERVICE, "Callback is NULL"); + return; + } + callback_->onEnd(); + DISPLAY_HILOGD(MODULE_SERVICE, "GradualAnimator StopAnimation end"); +} + +bool GradualAnimator::IsAnimating() +{ + return animating_; +} + +void GradualAnimator::NextStep() +{ + if (!animating_) { + DISPLAY_HILOGW(MODULE_SERVICE, "NextStep, not animating"); + return; + } + if (callback_ == nullptr) { + DISPLAY_HILOGW(MODULE_SERVICE, "Callback is NULL"); + return; + } + currentStep_++; + if (currentStep_ == 1) { + callback_->onStart(); + } + if (currentStep_ < steps_) { + current_ = current_ + stride_; + callback_->onChanged(current_); + handler_->SendEvent(EVENT_STEP, 0, updateTime_); + } else { + current_ = to_; + callback_->onChanged(current_); + callback_->onEnd(); + animating_ = false; + } +} + +GradualAnimator::AnimatorHandler::AnimatorHandler( + const std::shared_ptr& runner, + std::shared_ptr owner) + : AppExecFwk::EventHandler(runner), owner_(owner) +{ + DISPLAY_HILOGD(MODULE_SERVICE, "AnimatorHandler is created"); +} + +void GradualAnimator::AnimatorHandler::ProcessEvent(const AppExecFwk::InnerEvent::Pointer& event) +{ + DISPLAY_HILOGD(MODULE_SERVICE, "AnimatorHandler::%{public}s ,eventid = %d", __func__, + event->GetInnerEventId()); + std::shared_ptr animator = owner_.lock(); + switch (event->GetInnerEventId()) { + case EVENT_STEP: { + animator->NextStep(); + break; + } + default: + break; + } +} +} // namespace DisplayPowerMgr +} // namespace OHOS diff --git a/service/native/src/screen_action.cpp b/service/native/src/screen_action.cpp index 163a4c2..a37fb2b 100644 --- a/service/native/src/screen_action.cpp +++ b/service/native/src/screen_action.cpp @@ -16,11 +16,13 @@ #include "screen_action.h" #include "display_common.h" +#include "display_type.h" #include "hilog_wrapper.h" +#include "window_manager_service_client.h" namespace OHOS { -namespace DisplayMgr { -void ScreenAction::Init() +namespace DisplayPowerMgr { +ScreenAction::ScreenAction() { DeviceFuncs *f = NULL; @@ -29,22 +31,114 @@ void ScreenAction::Init() DISPLAY_HILOGE(MODULE_SERVICE, "Failed to init device"); return; } + devIds_.push_back(0); hdiFuncs_ = DeviceFuncPtr(f); DISPLAY_HILOGI(MODULE_SERVICE, "Succeed to init"); } -bool ScreenAction::SetPowerState(ScreenState state __attribute__((__unused__))) +std::vector ScreenAction::GetDisplayIds() { + return devIds_; +} + +DisplayState ScreenAction::GetPowerState(uint32_t devId) +{ + DisplayState ret = DisplayState::DISPLAY_UNKNOWN; + + auto wmsc = WindowManagerServiceClient::GetInstance(); + wmsc->Init(); + sptr wms = wmsc->GetService(); + if (wms == nullptr) { + DISPLAY_HILOGE(MODULE_SERVICE, "FAILED to get service from WindowManager Client"); + return DisplayState::DISPLAY_UNKNOWN; + } + auto promise = wms->GetDisplayPower(devId)->Await(); + if (promise.wret != WM_OK) { + DISPLAY_HILOGE(MODULE_SERVICE, "GetPowerState failed: %{public}d", promise.wret); + return ret; + } + + switch (promise.status) { + case POWER_STATUS_ON: + ret = DisplayState::DISPLAY_ON; + break; + case POWER_STATUS_STANDBY: + ret = DisplayState::DISPLAY_DIM; + break; + case POWER_STATUS_SUSPEND: + ret = DisplayState::DISPLAY_SUSPEND; + break; + case POWER_STATUS_OFF: + ret = DisplayState::DISPLAY_OFF; + break; + default: + break; + } + + return ret; +} + +bool ScreenAction::SetPowerState(uint32_t devId, DisplayState state) +{ + DISPLAY_HILOGI(MODULE_SERVICE, "SetDisplayPower: devId=%{public}d, state=%{public}d", + devId, static_cast(state)); + auto wmsc = WindowManagerServiceClient::GetInstance(); + wmsc->Init(); + sptr wms = wmsc->GetService(); + if (wms == nullptr) { + DISPLAY_HILOGE(MODULE_SERVICE, "FAILED to get service from WindowManager Client"); + return false; + } + + DispPowerStatus status = POWER_STATUS_BUTT; + switch (state) { + case DisplayState::DISPLAY_ON: + status = POWER_STATUS_ON; + break; + case DisplayState::DISPLAY_DIM: + status = POWER_STATUS_STANDBY; + break; + case DisplayState::DISPLAY_SUSPEND: + status = POWER_STATUS_SUSPEND; + break; + case DisplayState::DISPLAY_OFF: + status = POWER_STATUS_OFF; + break; + default: + break; + } + + auto wret = wms->SetDisplayPower(devId, status)->Await(); + if (wret != WM_OK) { + DISPLAY_HILOGE(MODULE_SERVICE, "SetDisplayPower failed: %{public}d", wret); + return false; + } + return true; } -bool ScreenAction::SetBrightness(int32_t value) +uint32_t ScreenAction::GetBrightness(uint32_t devId) +{ + uint32_t level = 0; + if (!hdiFuncs_) { + DISPLAY_HILOGE(MODULE_SERVICE, "GetBrightness:Invalid device functions"); + return 0; + } + int32_t hdiRet = hdiFuncs_->GetDisplayBacklight(devId, &level); + if (hdiRet != DISPLAY_SUCCESS) { + DISPLAY_HILOGE(MODULE_SERVICE, "GetBrightness failed:%d", level); + return 0; + } + return level; +} + +bool ScreenAction::SetBrightness(uint32_t devId, uint32_t value) { if (!hdiFuncs_) { - DISPLAY_HILOGE(MODULE_SERVICE, "Invalid device functions"); + DISPLAY_HILOGE(MODULE_SERVICE, "SetBrightness: Invalid device functions"); return false; } - return hdiFuncs_->SetDisplayBacklight(0, GetValidBrightness(value)) == DISPLAY_SUCCESS; + return hdiFuncs_->SetDisplayBacklight(devId, GetValidBrightness(value)) == DISPLAY_SUCCESS; } -} // namespace DisplayMgr +} // namespace DisplayPowerMgr } // namespace OHOS diff --git a/service/native/src/screen_controller.cpp b/service/native/src/screen_controller.cpp index 44c4987..93f34fe 100644 --- a/service/native/src/screen_controller.cpp +++ b/service/native/src/screen_controller.cpp @@ -19,37 +19,119 @@ #include "hilog_wrapper.h" namespace OHOS { -namespace DisplayMgr { -ScreenController::ScreenController() +namespace DisplayPowerMgr { +const int DISPLAY_FULL_BRIGHTNESS = 100; +const int DISPLAY_DIM_BRIGHTNESS = 50; +const int DISPLAY_OFF_BRIGHTNESS = 0; +const int DISPLAY_SUSPEND_BRIGHTNESS = 50; + +ScreenController::ScreenController(uint32_t devId, std::shared_ptr action) + : devId_(devId), state_(DisplayState::DISPLAY_UNKNOWN), action_(action) { - action_.Init(); + DISPLAY_HILOGI(MODULE_SERVICE, "ScreenController created: %{public}d", devId_); + stateValues_.emplace(DisplayState::DISPLAY_ON, DISPLAY_FULL_BRIGHTNESS); + stateValues_.emplace(DisplayState::DISPLAY_DIM, DISPLAY_DIM_BRIGHTNESS); + stateValues_.emplace(DisplayState::DISPLAY_OFF, DISPLAY_OFF_BRIGHTNESS); + stateValues_.emplace(DisplayState::DISPLAY_SUSPEND, DISPLAY_SUSPEND_BRIGHTNESS); + animator_ = nullptr; } -bool ScreenController::UpdateState(ScreenState state) +bool ScreenController::UpdateState(DisplayState state) { std::lock_guard lock(mutex_); - if (IsScreenStateLocked(state)) { + DISPLAY_HILOGI(MODULE_SERVICE, "ScreenController UpdateState: %{public}d, %{public}d", + devId_, static_cast(state)); + if (state == state_) { return true; } + + bool ret = action_->SetPowerState(devId_, state); + if (!ret) { + DISPLAY_HILOGW(MODULE_SERVICE, "SetPowerState failed state=%{public}d", state); + } state_ = state; - action_.SetPowerState(state); - DISPLAY_HILOGI(MODULE_SERVICE, "Update screen state to %{public}u", ToUnderlying(state)); + auto iterator = stateValues_.find(state); + if (iterator != stateValues_.end()) { + ret = action_->SetBrightness(devId_, iterator->second); + if (ret) { + brightness_ = iterator->second; + } else { + DISPLAY_HILOGI(MODULE_SERVICE, "set brightness falied! %{public}d", iterator->second); + } + } + DISPLAY_HILOGI(MODULE_SERVICE, "Update screen state to %{public}u", state); return true; } -bool ScreenController::UpdateBrightness(int32_t value) +bool ScreenController::UpdateBrightness(uint32_t value, uint32_t duraion) +{ + std::lock_guard lock(mutex_); + DISPLAY_HILOGI(MODULE_SERVICE, "ScreenController UpdateState: %{public}d, %{public}d", + devId_, value); + if (animator_ == nullptr) { + std::string name = "ScreenController_" + std::to_string(devId_); + std::shared_ptr callback = shared_from_this(); + animator_ = std::make_shared(name, callback); + } + if (animator_->IsAnimating()) { + animator_->StopAnimation(); + } + if (duraion > 0) { + DISPLAY_HILOGI(MODULE_SERVICE, "UpdateState gradually"); + animator_->StartAnimation(brightness_, value, SCREEN_BRIGHTNESS_UPDATE_DURATION); + return true; + } + bool ret = action_->SetBrightness(devId_, value); + if (ret) { + brightness_ = value; + DISPLAY_HILOGI(MODULE_SERVICE, "Update brightness to %{public}d", value); + } else { + DISPLAY_HILOGI(MODULE_SERVICE, "Update brightness falied! %{public}d", value); + } + + return ret; +} + +bool ScreenController::UpdateStateConfig(DisplayState state, uint32_t value) { std::lock_guard lock(mutex_); - brightness_ = value; - action_.SetBrightness(value); - DISPLAY_HILOGI(MODULE_SERVICE, "Update brightness to %{public}d", value); + DISPLAY_HILOGI(MODULE_SERVICE, + "ScreenController UpdateStateConfig: Id=%{public}d, State=%{public}d, Value=%{public}d", + devId_, static_cast(state), value); + auto iterator = stateValues_.find(state); + if (iterator == stateValues_.end()) { + DISPLAY_HILOGI(MODULE_SERVICE, "UpdateStateConfig No such state"); + return false; + } + iterator->second = value; return true; } bool ScreenController::IsScreenOn() { std::lock_guard lock(mutex_); - return IsScreenStateLocked(ScreenState::SCREEN_STATE_ON); + return (state_ == DisplayState::DISPLAY_ON || state_ == DisplayState::DISPLAY_DIM); +} + +void ScreenController::onStart() +{ + DISPLAY_HILOGD(MODULE_SERVICE, "ScreenAnimatorCallback onStart"); +} + +void ScreenController::onChanged(int32_t currentValue) +{ + brightness_ = currentValue; + bool ret = action_->SetBrightness(devId_, currentValue); + if (ret) { + DISPLAY_HILOGD(MODULE_SERVICE, "Update brightness to %{public}d", currentValue); + } else { + DISPLAY_HILOGD(MODULE_SERVICE, "Update brightness falied! %{public}d", currentValue); + } +} + +void ScreenController::onEnd() +{ + DISPLAY_HILOGD(MODULE_SERVICE, "ScreenAnimatorCallback OnEnd"); } -} // namespace DisplayMgr +} // namespace DisplayPowerMgr } // namespace OHOS diff --git a/service/zidl/include/display_power_mgr_proxy.h b/service/zidl/include/display_power_mgr_proxy.h new file mode 100644 index 0000000..e27b0d0 --- /dev/null +++ b/service/zidl/include/display_power_mgr_proxy.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef DISPLAYMGR_DISPLAY_MGR_PROXY_H +#define DISPLAYMGR_DISPLAY_MGR_PROXY_H + +#include + +#include "idisplay_power_mgr.h" + +namespace OHOS { +namespace DisplayPowerMgr { +class DisplayPowerMgrProxy : public IRemoteProxy { +public: + explicit DisplayPowerMgrProxy(const sptr& impl) + : IRemoteProxy(impl) {} + ~DisplayPowerMgrProxy() override = default; + + virtual bool SetDisplayState(uint32_t id, DisplayState state) override; + virtual DisplayState GetDisplayState(uint32_t id) override; + virtual bool SetBrightness(uint32_t id, int32_t value) override; + virtual bool AdjustBrightness(uint32_t id, int32_t value, uint32_t duration) override; + virtual bool SetStateConfig(uint32_t id, DisplayState state, int32_t value) override; + +private: + static inline BrokerDelegator delegator_; +}; +} // namespace DisplayPowerMgr +} // namespace OHOS +#endif // DISPLAYMGR_DISPLAY_MGR_PROXY_H diff --git a/service/zidl/include/display_power_mgr_stub.h b/service/zidl/include/display_power_mgr_stub.h new file mode 100644 index 0000000..2a35edd --- /dev/null +++ b/service/zidl/include/display_power_mgr_stub.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef DISPLAYMGR_DISPLAY_MGR_STUB_H +#define DISPLAYMGR_DISPLAY_MGR_STUB_H + +#include + +#include "idisplay_power_mgr.h" + +namespace OHOS { +namespace DisplayPowerMgr { +class DisplayPowerMgrStub : public IRemoteStub { +public: + int32_t OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; + +private: + int32_t SetDisplayStateStub(MessageParcel& data, MessageParcel& reply); + int32_t GetDisplayStateStub(MessageParcel& data, MessageParcel& reply); + int32_t SetBrightnessStub(MessageParcel& data, MessageParcel& reply); + int32_t AdjustBrightnessStub(MessageParcel& data, MessageParcel& reply); + int32_t SetStateConfigStub(MessageParcel& data, MessageParcel& reply); +}; +} // namespace DisplayPowerMgr +} // namespace OHOS +#endif // DISPLAYMGR_DISPLAY_MGR_STUB_H diff --git a/service/zidl/src/display_power_mgr_proxy.cpp b/service/zidl/src/display_power_mgr_proxy.cpp new file mode 100644 index 0000000..f0538f7 --- /dev/null +++ b/service/zidl/src/display_power_mgr_proxy.cpp @@ -0,0 +1,190 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "display_power_mgr_proxy.h" + +#include "display_common.h" + +namespace OHOS { +namespace DisplayPowerMgr { +bool DisplayPowerMgrProxy::SetDisplayState(uint32_t id, DisplayState state) +{ + sptr remote = Remote(); + RETURN_IF_WITH_RET(remote == nullptr, false); + + bool result = false; + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(DisplayPowerMgrProxy::GetDescriptor())) { + DISPLAY_HILOGE(MODULE_INNERKIT, "DisplayPowerMgrClient::%{public}s write descriptor failed!", __func__); + return result; + } + + WRITE_PARCEL_WITH_RET(data, Uint32, id, false); + WRITE_PARCEL_WITH_RET(data, Uint32, static_cast(state), false); + + int ret = remote->SendRequest(static_cast(IDisplayPowerMgr::SET_DISPLAY_STATE), + data, reply, option); + if (ret != ERR_OK) { + DISPLAY_HILOGE(MODULE_INNERKIT, "DisplayPowerMgrProxy::%{public}s SendRequest is failed, error code: %d", + __func__, ret); + return result; + } + + if (!reply.ReadBool(result)) { + DISPLAY_HILOGE(MODULE_INNERKIT, "Readback fail!"); + return result; + } + + return result; +} + +DisplayState DisplayPowerMgrProxy::GetDisplayState(uint32_t id) +{ + sptr remote = Remote(); + RETURN_IF_WITH_RET(remote == nullptr, DisplayState::DISPLAY_UNKNOWN); + + uint32_t result = 0; + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(DisplayPowerMgrProxy::GetDescriptor())) { + DISPLAY_HILOGE(MODULE_INNERKIT, "DisplayPowerMgrClient::%{public}s write descriptor failed!", __func__); + return DisplayState::DISPLAY_UNKNOWN; + } + + WRITE_PARCEL_WITH_RET(data, Uint32, id, DisplayState::DISPLAY_UNKNOWN); + + int ret = remote->SendRequest(static_cast(IDisplayPowerMgr::GET_DISPLAY_STATE), + data, reply, option); + if (ret != ERR_OK) { + DISPLAY_HILOGE(MODULE_INNERKIT, "DisplayPowerMgrProxy::%{public}s SendRequest is failed,%d", __func__, ret); + return DisplayState::DISPLAY_UNKNOWN; + } + + if (!reply.ReadUint32(result)) { + DISPLAY_HILOGE(MODULE_INNERKIT, "Readback fail!"); + return DisplayState::DISPLAY_UNKNOWN; + } + + return static_cast(result); +} + +bool DisplayPowerMgrProxy::SetBrightness(uint32_t id, int32_t value) +{ + sptr remote = Remote(); + RETURN_IF_WITH_RET(remote == nullptr, false); + + bool result = false; + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(DisplayPowerMgrProxy::GetDescriptor())) { + DISPLAY_HILOGE(MODULE_INNERKIT, "DisplayPowerMgrProxy::%{public}s write descriptor failed!", __func__); + return result; + } + + WRITE_PARCEL_WITH_RET(data, Uint32, id, false); + WRITE_PARCEL_WITH_RET(data, Int32, value, false); + + int ret = remote->SendRequest(static_cast(IDisplayPowerMgr::SET_BRIGHTNESS), + data, reply, option); + if (ret != ERR_OK) { + DISPLAY_HILOGE(MODULE_INNERKIT, "DisplayPowerMgrProxy::%{public}s SendRequest is failed: %d", __func__, ret); + return result; + } + + if (!reply.ReadBool(result)) { + DISPLAY_HILOGE(MODULE_INNERKIT, "Readback fail!"); + return result; + } + + return result; +} + +bool DisplayPowerMgrProxy::AdjustBrightness(uint32_t id, int32_t value, uint32_t duration) +{ + sptr remote = Remote(); + RETURN_IF_WITH_RET(remote == nullptr, false); + + bool result = false; + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(DisplayPowerMgrProxy::GetDescriptor())) { + DISPLAY_HILOGE(MODULE_INNERKIT, "DisplayPowerMgrProxy::%{public}s write descriptor failed!", __func__); + return result; + } + + WRITE_PARCEL_WITH_RET(data, Uint32, id, false); + WRITE_PARCEL_WITH_RET(data, Int32, value, false); + WRITE_PARCEL_WITH_RET(data, Int32, duration, false); + + int ret = remote->SendRequest(static_cast(IDisplayPowerMgr::ADJUST_BRIGHTNESS), + data, reply, option); + if (ret != ERR_OK) { + DISPLAY_HILOGE(MODULE_INNERKIT, "DisplayPowerMgrProxy::%{public}s SendRequest is failed: %d", __func__, ret); + return result; + } + + if (!reply.ReadBool(result)) { + DISPLAY_HILOGE(MODULE_INNERKIT, "Readback fail!"); + return result; + } + + return result; +} + +bool DisplayPowerMgrProxy::SetStateConfig(uint32_t id, DisplayState state, int32_t value) +{ + sptr remote = Remote(); + RETURN_IF_WITH_RET(remote == nullptr, false); + + bool result = false; + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(DisplayPowerMgrProxy::GetDescriptor())) { + DISPLAY_HILOGE(MODULE_INNERKIT, "DisplayPowerMgrProxy::%{public}s write descriptor failed!", __func__); + return result; + } + + WRITE_PARCEL_WITH_RET(data, Uint32, id, false); + WRITE_PARCEL_WITH_RET(data, Uint32, static_cast(state), false); + WRITE_PARCEL_WITH_RET(data, Int32, value, false); + + int ret = remote->SendRequest(static_cast(IDisplayPowerMgr::SET_STATE_CONFIG), + data, reply, option); + if (ret != ERR_OK) { + DISPLAY_HILOGE(MODULE_INNERKIT, "PowerMgrProxy::%{public}s SendRequest is failed, error code: %d", + __func__, ret); + return result; + } + + if (!reply.ReadBool(result)) { + DISPLAY_HILOGE(MODULE_INNERKIT, "Readback fail!"); + return result; + } + + return result; +} +} // namespace DisplayPowerMgr +} // namespace OHOS diff --git a/service/zidl/src/display_power_mgr_stub.cpp b/service/zidl/src/display_power_mgr_stub.cpp new file mode 100644 index 0000000..8fe9b53 --- /dev/null +++ b/service/zidl/src/display_power_mgr_stub.cpp @@ -0,0 +1,135 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "display_power_mgr_stub.h" + +#include + +#include "display_common.h" + +namespace OHOS { +namespace DisplayPowerMgr { +int32_t DisplayPowerMgrStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, + MessageOption &option) +{ + DISPLAY_HILOGD(MODULE_SERVICE, "DisplayPowerMgrStub::OnRemoteRequest, cmd = %d, flags= %d", + code, option.GetFlags()); + std::u16string descripter = DisplayPowerMgrStub::GetDescriptor(); + std::u16string remoteDescripter = data.ReadInterfaceToken(); + if (descripter != remoteDescripter) { + DISPLAY_HILOGE(MODULE_SERVICE, "DisplayPowerMgrStub::OnRemoteRequest failed, descriptor is not matched!"); + return E_GET_POWER_SERVICE_FAILED; + } + + switch (code) { + case static_cast(IDisplayPowerMgr::SET_DISPLAY_STATE): + return SetDisplayStateStub(data, reply); + case static_cast(IDisplayPowerMgr::GET_DISPLAY_STATE): + return GetDisplayStateStub(data, reply); + case static_cast(IDisplayPowerMgr::SET_BRIGHTNESS): + return SetBrightnessStub(data, reply); + case static_cast(IDisplayPowerMgr::ADJUST_BRIGHTNESS): + return AdjustBrightnessStub(data, reply); + case static_cast(IDisplayPowerMgr::SET_STATE_CONFIG): + return SetStateConfigStub(data, reply); + default: + return IPCObjectStub::OnRemoteRequest(code, data, reply, option); + } +} + +int32_t DisplayPowerMgrStub::SetDisplayStateStub(MessageParcel& data, MessageParcel& reply) +{ + uint32_t id = 0; + uint32_t state = 0; + + READ_PARCEL_WITH_RET(data, Uint32, id, E_READ_PARCEL_ERROR); + READ_PARCEL_WITH_RET(data, Uint32, state, E_READ_PARCEL_ERROR); + + bool ret = SetDisplayState(id, static_cast(state)); + if (!reply.WriteBool(ret)) { + DISPLAY_HILOGE(MODULE_SERVICE, "Failed to write SetScreenState return value"); + return E_WRITE_PARCEL_ERROR; + } + return ERR_OK; +} + +int32_t DisplayPowerMgrStub::GetDisplayStateStub(MessageParcel& data, MessageParcel& reply) +{ + uint32_t id = 0; + + READ_PARCEL_WITH_RET(data, Uint32, id, E_READ_PARCEL_ERROR); + + DisplayState ret = GetDisplayState(id); + if (!reply.WriteUint32(static_cast(ret))) { + DISPLAY_HILOGE(MODULE_SERVICE, "Failed to write SetScreenState return value"); + return E_WRITE_PARCEL_ERROR; + } + return ERR_OK; +} + + +int32_t DisplayPowerMgrStub::SetBrightnessStub(MessageParcel& data, MessageParcel& reply) +{ + uint32_t id = 0; + int32_t value = 0; + + READ_PARCEL_WITH_RET(data, Uint32, id, E_READ_PARCEL_ERROR); + READ_PARCEL_WITH_RET(data, Int32, value, E_READ_PARCEL_ERROR); + + bool ret = SetBrightness(id, value); + if (!reply.WriteBool(ret)) { + DISPLAY_HILOGE(MODULE_SERVICE, "Failed to write SetBrightness return value"); + return E_WRITE_PARCEL_ERROR; + } + return ERR_OK; +} + +int32_t DisplayPowerMgrStub::AdjustBrightnessStub(MessageParcel& data, MessageParcel& reply) +{ + uint32_t id = 0; + int32_t value = 0; + uint32_t duration = 0; + + READ_PARCEL_WITH_RET(data, Uint32, id, E_READ_PARCEL_ERROR); + READ_PARCEL_WITH_RET(data, Int32, value, E_READ_PARCEL_ERROR); + READ_PARCEL_WITH_RET(data, Uint32, duration, E_READ_PARCEL_ERROR); + + bool ret = AdjustBrightness(id, value, duration); + if (!reply.WriteBool(ret)) { + DISPLAY_HILOGE(MODULE_SERVICE, "Failed to write SetBrightness return value"); + return E_WRITE_PARCEL_ERROR; + } + return ERR_OK; +} + +int32_t DisplayPowerMgrStub::SetStateConfigStub(MessageParcel& data, MessageParcel& reply) +{ + uint32_t id = 0; + uint32_t state = 0; + int32_t value = 0; + + READ_PARCEL_WITH_RET(data, Uint32, id, E_READ_PARCEL_ERROR); + READ_PARCEL_WITH_RET(data, Uint32, state, E_READ_PARCEL_ERROR); + READ_PARCEL_WITH_RET(data, Int32, value, E_READ_PARCEL_ERROR); + + bool ret = SetStateConfig(id, static_cast(state), value); + if (!reply.WriteBool(ret)) { + DISPLAY_HILOGE(MODULE_SERVICE, "Failed to write SetBrightness return value"); + return E_WRITE_PARCEL_ERROR; + } + return ERR_OK; +} +} // namespace DisplayPowerMgr +} // namespace OHOS diff --git a/test/native/unittest/BUILD.gn b/test/native/unittest/BUILD.gn index ca5edf5..3803f56 100644 --- a/test/native/unittest/BUILD.gn +++ b/test/native/unittest/BUILD.gn @@ -23,6 +23,7 @@ config("module_private_config") { include_dirs = [ "include", "//utils/system/safwk/native/include", + "//foundation/appexecfwk/standard/interfaces/innerkits/libeventhandler/include", ] } @@ -30,7 +31,7 @@ config("module_private_config") { ohos_unittest("unittest_display_mgr_service") { module_out_path = module_output_path - sources = [ "src/display_mgr_service_test.cpp" ] + sources = [ "src/display_power_mgr_service_test.cpp" ] configs = [ "${displaymgr_utils_path}:utils_config", diff --git a/test/native/unittest/include/display_power_mgr_service_test.h b/test/native/unittest/include/display_power_mgr_service_test.h new file mode 100644 index 0000000..8b32860 --- /dev/null +++ b/test/native/unittest/include/display_power_mgr_service_test.h @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef DISPLAYMGR_DISPLAY_MGR_SERVICE_TEST_H +#define DISPLAYMGR_DISPLAY_MGR_SERVICE_TEST_H + +#include + +class DisplayPowerMgrServiceTest : public testing::Test { +public: + static void SetUpTestCase(void) {} + static void TearDownTestCase(void) {} + void SetUp() {} + void TearDown() {} +}; +#endif // DISPLAYMGR_DISPLAY_MGR_SERVICE_TEST_H diff --git a/test/native/unittest/src/display_power_mgr_service_test.cpp b/test/native/unittest/src/display_power_mgr_service_test.cpp new file mode 100644 index 0000000..6cf34b2 --- /dev/null +++ b/test/native/unittest/src/display_power_mgr_service_test.cpp @@ -0,0 +1,129 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "display_power_mgr_service_test.h" + +#include +#include + +#include "display_power_mgr_client.h" +#include "display_power_mgr_service.h" + +using namespace testing::ext; +using namespace OHOS; +using namespace OHOS::DisplayPowerMgr; + +namespace { +/** + * @tc.name: DisplayPowerMgrService01 + * @tc.desc: Test DisplayPowerMgrService service ready. + * @tc.type: FUNC + */ +HWTEST_F(DisplayPowerMgrServiceTest, DisplayPowerMgrService01, TestSize.Level0) +{ + sptr sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + EXPECT_TRUE(sam != nullptr) << "DisplayPowerMgrService01 fail to get GetSystemAbilityManager"; + sptr remoteObject_ = sam->CheckSystemAbility(DISPLAY_MANAGER_SERVICE_ID); + EXPECT_TRUE(remoteObject_ != nullptr) << "GetSystemAbility failed."; +} + +/** + * @tc.name: DisplayPowerMgrService002 + * @tc.desc: Test set screen state off + * @tc.type: FUNC + */ +HWTEST_F(DisplayPowerMgrServiceTest, DisplayPowerMgrService002, TestSize.Level0) +{ + auto ret = DisplayPowerMgrClient::GetInstance().SetDisplayState(DisplayState::DISPLAY_OFF); + sleep(5); + EXPECT_TRUE(ret); +} + +/** + * @tc.name: DisplayPowerMgrService003 + * @tc.desc: Test set screen state on + * @tc.type: FUNC + */ +HWTEST_F(DisplayPowerMgrServiceTest, DisplayPowerMgrService003, TestSize.Level0) +{ + auto ret = DisplayPowerMgrClient::GetInstance().SetDisplayState(DisplayState::DISPLAY_ON); + sleep(5); + EXPECT_TRUE(ret); +} + +/** + * @tc.name: DisplayPowerMgrService004 + * @tc.desc: Test set brightness + * @tc.type: FUNC + */ +HWTEST_F(DisplayPowerMgrServiceTest, DisplayPowerMgrService004, TestSize.Level0) +{ + auto ret = DisplayPowerMgrClient::GetInstance().SetBrightness(5); + sleep(5); + EXPECT_TRUE(ret); + ret = DisplayPowerMgrClient::GetInstance().SetBrightness(255); + sleep(5); + EXPECT_TRUE(ret); +} + + +/** + * @tc.name: DisplayPowerMgrService005 + * @tc.desc: Test set screen state on + * @tc.type: FUNC + */ +HWTEST_F(DisplayPowerMgrServiceTest, DisplayPowerMgrService005, TestSize.Level0) +{ + auto ret = DisplayPowerMgrClient::GetInstance().SetDisplayState(DisplayState::DISPLAY_ON); + sleep(5); + EXPECT_TRUE(ret); +} + +/** + * @tc.name: DisplayPowerMgrService006 + * @tc.desc: Test set screen state dim + * @tc.type: FUNC + */ +HWTEST_F(DisplayPowerMgrServiceTest, DisplayPowerMgrService006, TestSize.Level0) +{ + auto ret = DisplayPowerMgrClient::GetInstance().SetDisplayState(DisplayState::DISPLAY_DIM); + sleep(5); + EXPECT_TRUE(ret); +} + +/** + * @tc.name: DisplayPowerMgrService007 + * @tc.desc: Test set screen state off + * @tc.type: FUNC + */ +HWTEST_F(DisplayPowerMgrServiceTest, DisplayPowerMgrService007, TestSize.Level0) +{ + auto ret = DisplayPowerMgrClient::GetInstance().SetDisplayState(DisplayState::DISPLAY_OFF); + sleep(5); + EXPECT_TRUE(ret); +} + +/** + * @tc.name: DisplayPowerMgrService008 + * @tc.desc: Test set screen state suspend + * @tc.type: FUNC + */ +HWTEST_F(DisplayPowerMgrServiceTest, DisplayPowerMgrService008, TestSize.Level0) +{ + auto ret = DisplayPowerMgrClient::GetInstance().SetDisplayState(DisplayState::DISPLAY_SUSPEND); + sleep(5); + EXPECT_TRUE(ret); +} +} \ No newline at end of file diff --git a/utils/native/include/display_mgr_errors.h b/utils/native/include/display_mgr_errors.h index 90ca486..33b7762 100644 --- a/utils/native/include/display_mgr_errors.h +++ b/utils/native/include/display_mgr_errors.h @@ -19,7 +19,7 @@ #include namespace OHOS { -namespace DisplayMgr { +namespace DisplayPowerMgr { enum { /** * Module type: Display Manager Service @@ -42,7 +42,7 @@ enum { E_ADD_DEATH_RECIPIENT_FAILED, E_INNER_ERR }; -} // namespace DisplayMgr +} // namespace DisplayPowerMgr } // namespace OHOS #endif // DISPLAYMGR_DISPLAY_MGR_ERRORS_H -- Gitee From f83862ff22977af15aed77168d3434ab9f21a7fd Mon Sep 17 00:00:00 2001 From: zhang Date: Wed, 20 Oct 2021 17:44:27 +0800 Subject: [PATCH 2/3] add module code Signed-off-by: zhang --- .../native/include/display_manager.h | 39 ------ .../innerkits/native/include/idisplay_mgr.h | 39 ------ .../innerkits/native/src/display_manager.cpp | 32 ----- .../native/src/display_mgr_client.cpp | 93 -------------- .../innerkits/native/src/display_mgr_client.h | 57 --------- interfaces/kits/js/napi/brightness.cpp | 116 ------------------ service/native/include/display_mgr_service.h | 37 ------ service/native/src/display_mgr_service.cpp | 74 ----------- service/zidl/include/display_mgr_proxy.h | 39 ------ service/zidl/include/display_mgr_stub.h | 35 ------ service/zidl/src/display_mgr_proxy.cpp | 80 ------------ service/zidl/src/display_mgr_stub.cpp | 72 ----------- .../include/display_mgr_service_test.h | 28 ----- .../unittest/src/display_mgr_service_test.cpp | 78 ------------ utils/native/include/hilog_wrapper.h | 46 +++---- 15 files changed, 23 insertions(+), 842 deletions(-) delete mode 100644 interfaces/innerkits/native/include/display_manager.h delete mode 100644 interfaces/innerkits/native/include/idisplay_mgr.h delete mode 100644 interfaces/innerkits/native/src/display_manager.cpp delete mode 100644 interfaces/innerkits/native/src/display_mgr_client.cpp delete mode 100644 interfaces/innerkits/native/src/display_mgr_client.h delete mode 100644 interfaces/kits/js/napi/brightness.cpp delete mode 100644 service/native/include/display_mgr_service.h delete mode 100644 service/native/src/display_mgr_service.cpp delete mode 100644 service/zidl/include/display_mgr_proxy.h delete mode 100644 service/zidl/include/display_mgr_stub.h delete mode 100644 service/zidl/src/display_mgr_proxy.cpp delete mode 100644 service/zidl/src/display_mgr_stub.cpp delete mode 100644 test/native/unittest/include/display_mgr_service_test.h delete mode 100644 test/native/unittest/src/display_mgr_service_test.cpp diff --git a/interfaces/innerkits/native/include/display_manager.h b/interfaces/innerkits/native/include/display_manager.h deleted file mode 100644 index e23a44a..0000000 --- a/interfaces/innerkits/native/include/display_manager.h +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef DISPLAYMGR_DISPLAY_MANAGER_H -#define DISPLAYMGR_DISPLAY_MANAGER_H - -#include - -#include "display_info.h" - -namespace OHOS { -namespace DisplayMgr { -class DisplayManager { -public: - /** - * Set screen state. - */ - static bool SetScreenState(ScreenState state); - - /** - * Set brightness. - */ - static bool SetBrightness(int32_t value); -}; -} // namespace DisplayMgr -} // namespace OHOS -#endif // DISPLAYMGR_DISPLAY_MANAGER_H diff --git a/interfaces/innerkits/native/include/idisplay_mgr.h b/interfaces/innerkits/native/include/idisplay_mgr.h deleted file mode 100644 index f16a7eb..0000000 --- a/interfaces/innerkits/native/include/idisplay_mgr.h +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef DISPLAYMGR_IDISPLAY_MGR_H -#define DISPLAYMGR_IDISPLAY_MGR_H - -#include - -#include "display_info.h" - -namespace OHOS { -namespace DisplayMgr { -class IDisplayMgr : public IRemoteBroker { -public: - enum { - SET_POWER_STATE = 0, - SET_BRIGHTNESS, - }; - - virtual bool SetScreenState(ScreenState state) = 0; - virtual bool SetBrightness(int32_t value) = 0; - - DECLARE_INTERFACE_DESCRIPTOR(u"ohos.displaymgr.IDisplayMgr"); -}; -} // namespace DisplayMgr -} // namespace OHOS -#endif // DISPLAYMGR_IDISPLAY_MGR_H diff --git a/interfaces/innerkits/native/src/display_manager.cpp b/interfaces/innerkits/native/src/display_manager.cpp deleted file mode 100644 index 8c003e4..0000000 --- a/interfaces/innerkits/native/src/display_manager.cpp +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "display_manager.h" - -#include "display_mgr_client.h" - -namespace OHOS { -namespace DisplayMgr { -bool DisplayManager::SetScreenState(ScreenState state) -{ - return DisplayMgrClient::GetInstance().SetScreenState(state); -} - -bool DisplayManager::SetBrightness(int32_t value) -{ - return DisplayMgrClient::GetInstance().SetBrightness(value); -} -} // namespace DisplayMgr -} // namespace OHOS diff --git a/interfaces/innerkits/native/src/display_mgr_client.cpp b/interfaces/innerkits/native/src/display_mgr_client.cpp deleted file mode 100644 index e3ee056..0000000 --- a/interfaces/innerkits/native/src/display_mgr_client.cpp +++ /dev/null @@ -1,93 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "display_mgr_client.h" - -#include -#include -#include - -#include "display_common.h" - -namespace OHOS { -namespace DisplayMgr { -DisplayMgrClient::DisplayMgrClient() = default; -DisplayMgrClient::~DisplayMgrClient() = default; - -sptr DisplayMgrClient::GetProxy() -{ - std::lock_guard lock(mutex_); - if (proxy_ != nullptr) { - return proxy_; - } - - sptr sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - if (sam == nullptr) { - DISPLAY_HILOGE(MODULE_INNERKIT, "Failed to get system ability manager"); - return nullptr; - } - sptr obj = sam->CheckSystemAbility(DISPLAY_MANAGER_SERVICE_ID); - if (obj == nullptr) { - DISPLAY_HILOGE(MODULE_INNERKIT, "Failed to get display manager service"); - return nullptr; - } - sptr dr = new DisplayDeathRecipient(*this); - if ((obj->IsProxyObject()) && (!obj->AddDeathRecipient(dr))) { - DISPLAY_HILOGE(MODULE_INNERKIT, "Failed to add death recipient"); - return nullptr; - } - - proxy_ = iface_cast(obj); - deathRecipient_ = dr; - DISPLAY_HILOGI(MODULE_INNERKIT, "Succeed to connect display manager service"); - return proxy_; -} - -void DisplayMgrClient::OnRemoteDied(const wptr& remote) -{ - if (remote == nullptr) { - DISPLAY_HILOGE(MODULE_INNERKIT, "OnRemoteDied failed, remote is nullptr"); - return; - } - - std::lock_guard lock(mutex_); - RETURN_IF(proxy_ == nullptr); - - auto serviceRemote = proxy_->AsObject(); - if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) { - serviceRemote->RemoveDeathRecipient(deathRecipient_); - proxy_ = nullptr; - } -} - -bool DisplayMgrClient::SetScreenState(ScreenState state) -{ - auto proxy = GetProxy(); - if (proxy == nullptr) { - return false; - } - return proxy->SetScreenState(state); -} - -bool DisplayMgrClient::SetBrightness(int32_t value) -{ - auto proxy = GetProxy(); - if (proxy == nullptr) { - return false; - } - return proxy->SetBrightness(value); -} -} // namespace DisplayMgr -} // namespace OHOS diff --git a/interfaces/innerkits/native/src/display_mgr_client.h b/interfaces/innerkits/native/src/display_mgr_client.h deleted file mode 100644 index 5f24e7a..0000000 --- a/interfaces/innerkits/native/src/display_mgr_client.h +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef DISPLAYMGR_DISPLAY_MGR_CLIENT_H -#define DISPLAYMGR_DISPLAY_MGR_CLIENT_H - -#include -#include - -#include "display_info.h" -#include "idisplay_mgr.h" - -namespace OHOS { -namespace DisplayMgr { -class DisplayMgrClient : public DelayedRefSingleton { - DECLARE_DELAYED_REF_SINGLETON(DisplayMgrClient); - -public: - bool SetScreenState(ScreenState state); - bool SetBrightness(int32_t value); - -private: - class DisplayDeathRecipient : public IRemoteObject::DeathRecipient { - public: - explicit DisplayDeathRecipient(DisplayMgrClient& client) : client_(client) {} - ~DisplayDeathRecipient() override = default; - void OnRemoteDied(const wptr& remote) override - { - client_.OnRemoteDied(remote); - } - - private: - DisplayMgrClient& client_; - }; - - sptr GetProxy(); - void OnRemoteDied(const wptr& remote); - - std::mutex mutex_; - sptr proxy_{nullptr}; - sptr deathRecipient_{nullptr}; -}; -} // namespace DisplayMgr -} // namespace OHOS -#endif // DISPLAYMGR_DISPLAY_MGR_CLIENT_H diff --git a/interfaces/kits/js/napi/brightness.cpp b/interfaces/kits/js/napi/brightness.cpp deleted file mode 100644 index fa1bb0b..0000000 --- a/interfaces/kits/js/napi/brightness.cpp +++ /dev/null @@ -1,116 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include -#include - -#include -#include - -#include "display_manager.h" -#include "hilog_wrapper.h" - -using namespace OHOS::DisplayMgr; - -struct BrightnessAsyncCallbackInfo { - napi_env env; - napi_async_work asyncWork; - int32_t value; -}; - -static napi_value SetValue(napi_env env, napi_callback_info info) -{ - DISPLAY_HILOGD(MODULE_JS_NAPI, "enter"); - size_t argc = 1; - napi_value args[1] = { 0 }; - napi_value jsthis; - void *data = nullptr; - - napi_status status = napi_get_cb_info(env, info, &argc, args, &jsthis, &data); - NAPI_ASSERT(env, (status == napi_ok) && (argc >= 1), "Failed to get cb info"); - - napi_valuetype type; - NAPI_CALL(env, napi_typeof(env, args[0], &type)); - - NAPI_ASSERT(env, type == napi_number, "Wrong argument type. Numbers expected."); - - int32_t value = 0; - NAPI_CALL(env, napi_get_value_int32(env, args[0], &value)); - - BrightnessAsyncCallbackInfo* asyncCallbackInfo = new BrightnessAsyncCallbackInfo { - .env = env, - .asyncWork = nullptr, - }; - - asyncCallbackInfo->value = value; - - napi_value resourceName; - napi_create_string_latin1(env, "setValue", NAPI_AUTO_LENGTH, &resourceName); - - napi_create_async_work( - env, - nullptr, - resourceName, - [](napi_env env, void *data) { - BrightnessAsyncCallbackInfo* asyncCallbackInfo = (BrightnessAsyncCallbackInfo *)data; - if (!DisplayManager::SetBrightness(asyncCallbackInfo->value)) { - DISPLAY_HILOGE(MODULE_JS_NAPI, "Failed to set brightness"); - } else { - DISPLAY_HILOGD(MODULE_JS_NAPI, "Succeed to set brightness"); - } - }, - [](napi_env env, napi_status status, void *data) { - BrightnessAsyncCallbackInfo* asyncCallbackInfo = (BrightnessAsyncCallbackInfo *)data; - napi_delete_async_work(env, asyncCallbackInfo->asyncWork); - delete asyncCallbackInfo; - }, - (void *)asyncCallbackInfo, - &asyncCallbackInfo->asyncWork); - - NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackInfo->asyncWork)); - - DISPLAY_HILOGD(MODULE_JS_NAPI, "return"); - return nullptr; -} - -EXTERN_C_START -static napi_value Init(napi_env env, napi_value exports) -{ - napi_property_descriptor desc[] = { - DECLARE_NAPI_FUNCTION("setValue", SetValue), - }; - NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); - - DISPLAY_HILOGD(MODULE_JS_NAPI, "return"); - - return exports; -} -EXTERN_C_END - -static napi_module g_module = { - .nm_version = 1, - .nm_flags = 0, - .nm_filename = "brightness", - .nm_register_func = Init, - .nm_modname = "brightness", - .nm_priv = ((void *)0), - .reserved = { 0 } -}; - -extern "C" __attribute__((constructor)) void RegisterModule(void) -{ - napi_module_register(&g_module); -} diff --git a/service/native/include/display_mgr_service.h b/service/native/include/display_mgr_service.h deleted file mode 100644 index b20688f..0000000 --- a/service/native/include/display_mgr_service.h +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef DISPLAYMGR_DISPLAY_MGR_SERVICE_H -#define DISPLAYMGR_DISPLAY_MGR_SERVICE_H - -#include - -#include "display_mgr_stub.h" -#include "screen_controller.h" - -namespace OHOS { -namespace DisplayMgr { -class DisplayMgrService : public DisplayMgrStub { -public: - bool SetScreenState(ScreenState state) override; - bool SetBrightness(int32_t value) override; - int32_t Dump(int32_t fd, const std::vector& args) override; - -private: - ScreenController screenController_; -}; -} // namespace DisplayMgr -} // namespace OHOS -#endif // DISPLAYMGR_DISPLAY_MGR_SERVICE_H diff --git a/service/native/src/display_mgr_service.cpp b/service/native/src/display_mgr_service.cpp deleted file mode 100644 index ace83dc..0000000 --- a/service/native/src/display_mgr_service.cpp +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "display_mgr_service.h" - -#include -#include -#include -#include - -#include "display_common.h" - -namespace OHOS { -namespace DisplayMgr { -namespace { -class DisplaySystemAbility : public SystemAbility { - DECLARE_SYSTEM_ABILITY(DisplaySystemAbility); - -public: - DisplaySystemAbility(int32_t id, bool runOnCreate) : SystemAbility(id, runOnCreate) {} - ~DisplaySystemAbility() override = default; - - void OnStart() override - { - DISPLAY_HILOGI(MODULE_SERVICE, "Start service"); - service_ = new DisplayMgrService(); - if (!Publish(service_)) { - DISPLAY_HILOGE(MODULE_SERVICE, "Failed to publish service"); - } - } - - void OnStop() override - { - DISPLAY_HILOGI(MODULE_SERVICE, "Stop service"); - } - -private: - sptr service_; -}; -REGISTER_SYSTEM_ABILITY_BY_ID(DisplaySystemAbility, DISPLAY_MANAGER_SERVICE_ID, true); -} - -bool DisplayMgrService::SetScreenState(ScreenState state) -{ - return screenController_.UpdateState(state); -} - -bool DisplayMgrService::SetBrightness(int32_t value) -{ - return screenController_.UpdateBrightness(value); -} - -int32_t DisplayMgrService::Dump(int32_t fd, const std::vector& args) -{ - std::string result("Empty dump info"); - if (!SaveStringToFd(fd, result)) { - DISPLAY_HILOGE(MODULE_SERVICE, "Failed to save dump info to fd"); - } - return ERR_OK; -} -} // namespace DisplayMgr -} // namespace OHOS diff --git a/service/zidl/include/display_mgr_proxy.h b/service/zidl/include/display_mgr_proxy.h deleted file mode 100644 index eb2e542..0000000 --- a/service/zidl/include/display_mgr_proxy.h +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef DISPLAYMGR_DISPLAY_MGR_PROXY_H -#define DISPLAYMGR_DISPLAY_MGR_PROXY_H - -#include - -#include "idisplay_mgr.h" - -namespace OHOS { -namespace DisplayMgr { -class DisplayMgrProxy : public IRemoteProxy { -public: - explicit DisplayMgrProxy(const sptr& impl) - : IRemoteProxy(impl) {} - ~DisplayMgrProxy() override = default; - - bool SetScreenState(ScreenState state) override; - bool SetBrightness(int32_t value) override; - -private: - static inline BrokerDelegator delegator_; -}; -} // namespace DisplayMgr -} // namespace OHOS -#endif // DISPLAYMGR_DISPLAY_MGR_PROXY_H diff --git a/service/zidl/include/display_mgr_stub.h b/service/zidl/include/display_mgr_stub.h deleted file mode 100644 index 50d3ba6..0000000 --- a/service/zidl/include/display_mgr_stub.h +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef DISPLAYMGR_DISPLAY_MGR_STUB_H -#define DISPLAYMGR_DISPLAY_MGR_STUB_H - -#include - -#include "idisplay_mgr.h" - -namespace OHOS { -namespace DisplayMgr { -class DisplayMgrStub : public IRemoteStub { -public: - int32_t OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; - -private: - int32_t SetScreenStateStub(MessageParcel& data, MessageParcel& reply); - int32_t SetBrightnessStub(MessageParcel& data, MessageParcel& reply); -}; -} // namespace DisplayMgr -} // namespace OHOS -#endif // DISPLAYMGR_DISPLAY_MGR_STUB_H diff --git a/service/zidl/src/display_mgr_proxy.cpp b/service/zidl/src/display_mgr_proxy.cpp deleted file mode 100644 index d54fd0d..0000000 --- a/service/zidl/src/display_mgr_proxy.cpp +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "display_mgr_proxy.h" - -#include "display_common.h" - -namespace OHOS { -namespace DisplayMgr { -bool DisplayMgrProxy::SetScreenState(ScreenState state) -{ - sptr remote = Remote(); - RETURN_IF_WITH_RET(remote == nullptr, false); - - bool result = false; - MessageParcel data; - MessageParcel reply; - MessageOption option; - - if (!data.WriteInterfaceToken(DisplayMgrProxy::GetDescriptor())) { - DISPLAY_HILOGE(MODULE_INNERKIT, "write descriptor failed!"); - return result; - } - - WRITE_PARCEL_WITH_RET(data, Uint32, static_cast(state), false); - - int ret = remote->SendRequest(static_cast(IDisplayMgr::SET_POWER_STATE), data, reply, option); - if (ret != ERR_OK) { - DISPLAY_HILOGE(MODULE_INNERKIT, "SendRequest is failed, error code: %d", ret); - return result; - } - if (!reply.ReadBool(result)) { - DISPLAY_HILOGE(MODULE_INNERKIT, "Readback fail!"); - } - - return result; -} - -bool DisplayMgrProxy::SetBrightness(int32_t value) -{ - sptr remote = Remote(); - RETURN_IF_WITH_RET(remote == nullptr, false); - - bool result = false; - MessageParcel data; - MessageParcel reply; - MessageOption option; - - if (!data.WriteInterfaceToken(DisplayMgrProxy::GetDescriptor())) { - DISPLAY_HILOGE(MODULE_INNERKIT, "write descriptor failed!"); - return result; - } - - WRITE_PARCEL_WITH_RET(data, Int32, value, false); - - int ret = remote->SendRequest(static_cast(IDisplayMgr::SET_BRIGHTNESS), data, reply, option); - if (ret != ERR_OK) { - DISPLAY_HILOGE(MODULE_INNERKIT, "SendRequest is failed, error code: %d", ret); - return result; - } - if (!reply.ReadBool(result)) { - DISPLAY_HILOGE(MODULE_INNERKIT, "Readback fail!"); - } - - return result; -} -} // namespace DisplayMgr -} // namespace OHOS diff --git a/service/zidl/src/display_mgr_stub.cpp b/service/zidl/src/display_mgr_stub.cpp deleted file mode 100644 index c4e6198..0000000 --- a/service/zidl/src/display_mgr_stub.cpp +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "display_mgr_stub.h" - -#include - -#include "display_common.h" - -namespace OHOS { -namespace DisplayMgr { -int32_t DisplayMgrStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) -{ - DISPLAY_HILOGD(MODULE_SERVICE, "DisplayMgrStub::OnRemoteRequest, cmd = %d, flags= %d", code, option.GetFlags()); - std::u16string descripter = DisplayMgrStub::GetDescriptor(); - std::u16string remoteDescripter = data.ReadInterfaceToken(); - if (descripter != remoteDescripter) { - DISPLAY_HILOGE(MODULE_SERVICE, "DisplayMgrStub::OnRemoteRequest failed, descriptor is not matched!"); - return E_GET_POWER_SERVICE_FAILED; - } - - switch (code) { - case static_cast(IDisplayMgr::SET_POWER_STATE): - return SetScreenStateStub(data, reply); - case static_cast(IDisplayMgr::SET_BRIGHTNESS): - return SetBrightnessStub(data, reply); - default: - return IPCObjectStub::OnRemoteRequest(code, data, reply, option); - } -} - -int32_t DisplayMgrStub::SetScreenStateStub(MessageParcel& data, MessageParcel& reply) -{ - uint32_t state = 0; - - READ_PARCEL_WITH_RET(data, Uint32, state, E_READ_PARCEL_ERROR); - - bool ret = SetScreenState(static_cast(state)); - if (!reply.WriteBool(ret)) { - DISPLAY_HILOGE(MODULE_SERVICE, "Failed to write SetScreenState return value"); - return E_WRITE_PARCEL_ERROR; - } - return ERR_OK; -} - -int32_t DisplayMgrStub::SetBrightnessStub(MessageParcel& data, MessageParcel& reply) -{ - int32_t value = 0; - - READ_PARCEL_WITH_RET(data, Int32, value, E_READ_PARCEL_ERROR); - - bool ret = SetBrightness(value); - if (!reply.WriteBool(ret)) { - DISPLAY_HILOGE(MODULE_SERVICE, "Failed to write SetBrightness return value"); - return E_WRITE_PARCEL_ERROR; - } - return ERR_OK; -} -} // namespace DisplayMgr -} // namespace OHOS diff --git a/test/native/unittest/include/display_mgr_service_test.h b/test/native/unittest/include/display_mgr_service_test.h deleted file mode 100644 index 21d05a3..0000000 --- a/test/native/unittest/include/display_mgr_service_test.h +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef DISPLAYMGR_DISPLAY_MGR_SERVICE_TEST_H -#define DISPLAYMGR_DISPLAY_MGR_SERVICE_TEST_H - -#include - -class DisplayMgrServiceTest : public testing::Test { -public: - static void SetUpTestCase(void) {} - static void TearDownTestCase(void) {} - void SetUp() {} - void TearDown() {} -}; -#endif // DISPLAYMGR_DISPLAY_MGR_SERVICE_TEST_H diff --git a/test/native/unittest/src/display_mgr_service_test.cpp b/test/native/unittest/src/display_mgr_service_test.cpp deleted file mode 100644 index 5cc893a..0000000 --- a/test/native/unittest/src/display_mgr_service_test.cpp +++ /dev/null @@ -1,78 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "display_mgr_service_test.h" - -#include -#include - -#include "display_manager.h" -#include "display_mgr_service.h" - -using namespace testing::ext; -using namespace OHOS; -using namespace OHOS::DisplayMgr; - -/** - * @tc.name: DisplayMgrService01 - * @tc.desc: Test DisplayMgrService service ready. - * @tc.type: FUNC - */ -HWTEST_F(DisplayMgrServiceTest, DisplayMgrService01, TestSize.Level0) -{ - sptr sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - EXPECT_TRUE(sam != nullptr) << "DisplayMgrService01 fail to get GetSystemAbilityManager"; - sptr remoteObject_ = sam->CheckSystemAbility(DISPLAY_MANAGER_SERVICE_ID); - EXPECT_TRUE(remoteObject_ != nullptr) << "GetSystemAbility failed."; -} - -/** - * @tc.name: DisplayMgrService002 - * @tc.desc: Test set screen state off - * @tc.type: FUNC - */ -HWTEST_F(DisplayMgrServiceTest, DisplayMgrService002, TestSize.Level0) -{ - auto ret = DisplayManager::SetScreenState(ScreenState::SCREEN_STATE_OFF); - sleep(5); - EXPECT_TRUE(ret); -} - -/** - * @tc.name: DisplayMgrService003 - * @tc.desc: Test set screen state on - * @tc.type: FUNC - */ -HWTEST_F(DisplayMgrServiceTest, DisplayMgrService003, TestSize.Level0) -{ - auto ret = DisplayManager::SetScreenState(ScreenState::SCREEN_STATE_ON); - sleep(5); - EXPECT_TRUE(ret); -} - -/** - * @tc.name: DisplayMgrService004 - * @tc.desc: Test set brightness - * @tc.type: FUNC - */ -HWTEST_F(DisplayMgrServiceTest, DisplayMgrService004, TestSize.Level0) -{ - auto ret = DisplayManager::SetBrightness(5); - sleep(5); - EXPECT_TRUE(ret); - ret = DisplayManager::SetBrightness(255); - sleep(5); - EXPECT_TRUE(ret); -} diff --git a/utils/native/include/hilog_wrapper.h b/utils/native/include/hilog_wrapper.h index 8a95164..e19a986 100644 --- a/utils/native/include/hilog_wrapper.h +++ b/utils/native/include/hilog_wrapper.h @@ -13,14 +13,14 @@ * limitations under the License. */ -#ifndef DISPLAYMGR_HILOG_WRAPPER_H -#define DISPLAYMGR_HILOG_WRAPPER_H +#ifndef DISPLAY_POWER_MGR_HILOG_WRAPPER_H +#define DISPLAY_POWER_MGR_HILOG_WRAPPER_H #define CONFIG_HILOG #ifdef CONFIG_HILOG #include "hilog/log.h" namespace OHOS { -namespace DisplayMgr { +namespace DisplayPowerMgr { #define __FILENAME__ (__builtin_strrchr(__FILE__, '/') ? __builtin_strrchr(__FILE__, '/') + 1 : __FILE__) #define __FORMATED(fmt, ...) "[%{public}s] %{public}s# " fmt, __FILENAME__, __FUNCTION__, ##__VA_ARGS__ @@ -45,45 +45,45 @@ namespace DisplayMgr { #endif // param of log interface, such as DISPLAY_HILOGF. -enum DisplayMgrSubModule { +enum DisplayPowerMgrSubModule { MODULE_INNERKIT = 0, MODULE_SERVICE, MODULE_JAVAKIT, // java kit, defined to avoid repeated use of domain. MODULE_JNI, MODULE_COMMON, MODULE_JS_NAPI, - DISPLAYMGR_MODULE_BUTT, + DISPLAY_POWER_MGR_MODULE_BUTT, }; -// 0xD002900: subsystem:PowerMgr module:DisplayMgr, 8 bits reserved. -static constexpr unsigned int BASE_DISPLAYMGR_DOMAIN_ID = 0xD002910; +// 0xD002900: subsystem:PowerMgr module:DisplayPowerMgr, 8 bits reserved. +static constexpr unsigned int BASE_DisplayPowerMgr_DOMAIN_ID = 0xD002910; -enum DisplayMgrDomainId { - DISPLAYMGR_INNERKIT_DOMAIN = BASE_DISPLAYMGR_DOMAIN_ID + MODULE_INNERKIT, - DISPLAYMGR_SERVICE_DOMAIN, - DISPLAYMGR_JAVAKIT_DOMAIN, +enum DisplayPowerMgrDomainId { + DISPLAY_POWER_MGR_INNERKIT_DOMAIN = BASE_DisplayPowerMgr_DOMAIN_ID + MODULE_INNERKIT, + DISPLAY_POWER_MGR_SERVICE_DOMAIN, + DISPLAY_POWER_MGR_JAVAKIT_DOMAIN, COMMON_DOMAIN, - DISPLAYMGR_JS_NAPI, - DISPLAYMGR_BUTT, + DISPLAY_POWER_MGR_JS_NAPI, + DISPLAY_POWER_MGR_BUTT, }; -static constexpr OHOS::HiviewDFX::HiLogLabel DISPLAY_MGR_LABEL[DISPLAYMGR_MODULE_BUTT] = { - {LOG_CORE, DISPLAYMGR_INNERKIT_DOMAIN, "DisplayMgrClient"}, - {LOG_CORE, DISPLAYMGR_SERVICE_DOMAIN, "DisplayMgrService"}, - {LOG_CORE, DISPLAYMGR_JAVAKIT_DOMAIN, "DisplayMgrJavaService"}, - {LOG_CORE, DISPLAYMGR_INNERKIT_DOMAIN, "DisplayMgrJni"}, - {LOG_CORE, COMMON_DOMAIN, "DisplayMgrCommon"}, - {LOG_CORE, DISPLAYMGR_JS_NAPI, "DisplayMgrJSNAPI"}, +static constexpr OHOS::HiviewDFX::HiLogLabel DISPLAY_MGR_LABEL[DISPLAY_POWER_MGR_MODULE_BUTT] = { + {LOG_CORE, DISPLAY_POWER_MGR_INNERKIT_DOMAIN, "DisplayPowerMgrClient"}, + {LOG_CORE, DISPLAY_POWER_MGR_SERVICE_DOMAIN, "DisplayPowerMgrService"}, + {LOG_CORE, DISPLAY_POWER_MGR_JAVAKIT_DOMAIN, "DisplayPowerMgrJavaService"}, + {LOG_CORE, DISPLAY_POWER_MGR_INNERKIT_DOMAIN, "DisplayPowerMgrJni"}, + {LOG_CORE, COMMON_DOMAIN, "DisplayPowerMgrCommon"}, + {LOG_CORE, DISPLAY_POWER_MGR_JS_NAPI, "DisplayPowerMgrJSNAPI"}, }; // In order to improve performance, do not check the module range. -// Besides, make sure module is less than DISPLAYMGR_MODULE_BUTT. +// Besides, make sure module is less than DisplayPowerMgr_MODULE_BUTT. #define DISPLAY_HILOGF(m, ...) (void)OHOS::HiviewDFX::HiLog::Fatal(DISPLAY_MGR_LABEL[m], __FORMATED(__VA_ARGS__)) #define DISPLAY_HILOGE(m, ...) (void)OHOS::HiviewDFX::HiLog::Error(DISPLAY_MGR_LABEL[m], __FORMATED(__VA_ARGS__)) #define DISPLAY_HILOGW(m, ...) (void)OHOS::HiviewDFX::HiLog::Warn(DISPLAY_MGR_LABEL[m], __FORMATED(__VA_ARGS__)) #define DISPLAY_HILOGI(m, ...) (void)OHOS::HiviewDFX::HiLog::Info(DISPLAY_MGR_LABEL[m], __FORMATED(__VA_ARGS__)) #define DISPLAY_HILOGD(m, ...) (void)OHOS::HiviewDFX::HiLog::Debug(DISPLAY_MGR_LABEL[m], __FORMATED(__VA_ARGS__)) -} // namespace DisplayMgr +} // namespace DisplayPowerMgr } // namespace OHOS #else @@ -96,4 +96,4 @@ static constexpr OHOS::HiviewDFX::HiLogLabel DISPLAY_MGR_LABEL[DISPLAYMGR_MODULE #endif // CONFIG_HILOG -#endif // DISPLAYMGR_HILOG_WRAPPER_H +#endif // DISPLAY_POWER_MGR_HILOG_WRAPPER_H -- Gitee From b060b1b1e418ce3c8bcb60d4d490386ceebf9687 Mon Sep 17 00:00:00 2001 From: zhang Date: Thu, 21 Oct 2021 09:10:09 +0800 Subject: [PATCH 3/3] add module code Signed-off-by: zhang --- frameworks/native/display_power_mgr_client.cpp | 8 ++++---- .../unittest/src/display_power_mgr_service_test.cpp | 10 ++++++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/frameworks/native/display_power_mgr_client.cpp b/frameworks/native/display_power_mgr_client.cpp index 9d52497..94e59c8 100644 --- a/frameworks/native/display_power_mgr_client.cpp +++ b/frameworks/native/display_power_mgr_client.cpp @@ -76,7 +76,7 @@ bool DisplayPowerMgrClient::SetDisplayState(DisplayState state, uint32_t id) { auto proxy = GetProxy(); if (proxy == nullptr) { - return true; + return false; } return proxy->SetDisplayState(id, state); } @@ -94,7 +94,7 @@ bool DisplayPowerMgrClient::SetBrightness(uint32_t value, uint32_t id) { auto proxy = GetProxy(); if (proxy == nullptr) { - return true; + return false; } return proxy->SetBrightness(id, value); } @@ -103,7 +103,7 @@ bool DisplayPowerMgrClient::AdjustBrightness(uint32_t value, uint32_t duration, { auto proxy = GetProxy(); if (proxy == nullptr) { - return true; + return false; } return proxy->AdjustBrightness(id, value, duration); } @@ -112,7 +112,7 @@ bool DisplayPowerMgrClient::SetStateConfig(DisplayState state, uint32_t value, u { auto proxy = GetProxy(); if (proxy == nullptr) { - return true; + return false; } return proxy->SetStateConfig(id, state, value); } diff --git a/test/native/unittest/src/display_power_mgr_service_test.cpp b/test/native/unittest/src/display_power_mgr_service_test.cpp index 6cf34b2..fc0e17c 100644 --- a/test/native/unittest/src/display_power_mgr_service_test.cpp +++ b/test/native/unittest/src/display_power_mgr_service_test.cpp @@ -26,6 +26,7 @@ using namespace OHOS; using namespace OHOS::DisplayPowerMgr; namespace { +#ifdef IPC_AVAILABLE /** * @tc.name: DisplayPowerMgrService01 * @tc.desc: Test DisplayPowerMgrService service ready. @@ -89,6 +90,8 @@ HWTEST_F(DisplayPowerMgrServiceTest, DisplayPowerMgrService005, TestSize.Level0) auto ret = DisplayPowerMgrClient::GetInstance().SetDisplayState(DisplayState::DISPLAY_ON); sleep(5); EXPECT_TRUE(ret); + DisplayState state = DisplayPowerMgrClient::GetInstance().GetDisplayState(); + EXPECT_TRUE(state == DisplayState::DISPLAY_ON); } /** @@ -101,6 +104,8 @@ HWTEST_F(DisplayPowerMgrServiceTest, DisplayPowerMgrService006, TestSize.Level0) auto ret = DisplayPowerMgrClient::GetInstance().SetDisplayState(DisplayState::DISPLAY_DIM); sleep(5); EXPECT_TRUE(ret); + DisplayState state = DisplayPowerMgrClient::GetInstance().GetDisplayState(); + EXPECT_TRUE(state == DisplayState::DISPLAY_DIM); } /** @@ -113,6 +118,8 @@ HWTEST_F(DisplayPowerMgrServiceTest, DisplayPowerMgrService007, TestSize.Level0) auto ret = DisplayPowerMgrClient::GetInstance().SetDisplayState(DisplayState::DISPLAY_OFF); sleep(5); EXPECT_TRUE(ret); + DisplayState state = DisplayPowerMgrClient::GetInstance().GetDisplayState(); + EXPECT_TRUE(state == DisplayState::DISPLAY_OFF); } /** @@ -125,5 +132,8 @@ HWTEST_F(DisplayPowerMgrServiceTest, DisplayPowerMgrService008, TestSize.Level0) auto ret = DisplayPowerMgrClient::GetInstance().SetDisplayState(DisplayState::DISPLAY_SUSPEND); sleep(5); EXPECT_TRUE(ret); + DisplayState state = DisplayPowerMgrClient::GetInstance().GetDisplayState(); + EXPECT_TRUE(state == DisplayState::DISPLAY_SUSPEND); } +#endif // IPC_AVAILABLE } \ No newline at end of file -- Gitee