/* * Copyright (c) 2021-2022 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 "errors.h" #include "message_option.h" #include "message_parcel.h" #include "display_log.h" #include "display_common.h" namespace OHOS { namespace DisplayPowerMgr { bool DisplayPowerMgrProxy::SetDisplayState(uint32_t id, DisplayState state, uint32_t reason) { 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(COMP_FWK, "write descriptor failed!"); 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, Uint32, reason, false); int ret = remote->SendRequest(static_cast(IDisplayPowerMgr::SET_DISPLAY_STATE), data, reply, option); if (ret != ERR_OK) { DISPLAY_HILOGE(COMP_FWK, "SendRequest is failed, error code: %d", ret); return result; } if (!reply.ReadBool(result)) { DISPLAY_HILOGE(COMP_FWK, "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(COMP_FWK, "write descriptor failed!"); 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(COMP_FWK, "SendRequest is failed, error code: %d", ret); return DisplayState::DISPLAY_UNKNOWN; } if (!reply.ReadUint32(result)) { DISPLAY_HILOGE(COMP_FWK, "Readback fail!"); return DisplayState::DISPLAY_UNKNOWN; } return static_cast(result); } std::vector DisplayPowerMgrProxy::GetDisplayIds() { sptr remote = Remote(); std::vector result; RETURN_IF_WITH_RET(remote == nullptr, result); uint32_t count = 0; MessageParcel data; MessageParcel reply; MessageOption option; if (!data.WriteInterfaceToken(DisplayPowerMgrProxy::GetDescriptor())) { DISPLAY_HILOGE(COMP_FWK, "write descriptor failed!"); return result; } int ret = remote->SendRequest(static_cast(IDisplayPowerMgr::GET_DISPLAY_IDS), data, reply, option); if (ret != ERR_OK) { DISPLAY_HILOGE(COMP_FWK, "SendRequest is failed, error code: %d", ret); return result; } if (!reply.ReadUint32(count)) { DISPLAY_HILOGE(COMP_FWK, "Readback fail!"); return result; } for (uint32_t i = 0; i < count; i++) { uint32_t value; if (reply.ReadUint32(value)) { result.push_back(value); } else { DISPLAY_HILOGE(COMP_FWK, "read value fail: %{public}d", i); } } return result; } uint32_t DisplayPowerMgrProxy::GetMainDisplayId() { sptr remote = Remote(); uint32_t result = 0; RETURN_IF_WITH_RET(remote == nullptr, result); MessageParcel data; MessageParcel reply; MessageOption option; if (!data.WriteInterfaceToken(DisplayPowerMgrProxy::GetDescriptor())) { DISPLAY_HILOGE(COMP_FWK, "write descriptor failed!"); return result; } int ret = remote->SendRequest(static_cast(IDisplayPowerMgr::GET_MAIN_DISPLAY_ID), data, reply, option); if (ret != ERR_OK) { DISPLAY_HILOGE(COMP_FWK, "SendRequest is failed, error code: %d", ret); return result; } if (!reply.ReadUint32(result)) { DISPLAY_HILOGE(COMP_FWK, "Readback fail!"); return result; } return result; } bool DisplayPowerMgrProxy::SetBrightness(uint32_t value, uint32_t displayId) { 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(COMP_FWK, "write descriptor failed!"); return result; } WRITE_PARCEL_WITH_RET(data, Uint32, value, false); WRITE_PARCEL_WITH_RET(data, Uint32, displayId, false); int ret = remote->SendRequest(static_cast(IDisplayPowerMgr::SET_BRIGHTNESS), data, reply, option); if (ret != ERR_OK) { DISPLAY_HILOGE(COMP_FWK, "SendRequest is failed, error code: %d", ret); return result; } if (!reply.ReadBool(result)) { DISPLAY_HILOGE(COMP_FWK, "Readback fail!"); return result; } int32_t error; READ_PARCEL_WITH_RET(reply, Int32, error, result); lastError_ = static_cast(error); return result; } bool DisplayPowerMgrProxy::DiscountBrightness(double discount, uint32_t displayId) { 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(COMP_FWK, "write descriptor failed!"); return result; } WRITE_PARCEL_WITH_RET(data, Double, discount, false); WRITE_PARCEL_WITH_RET(data, Uint32, displayId, false); int ret = remote->SendRequest(static_cast(IDisplayPowerMgr::DISCOUNT_BRIGHTNESS), data, reply, option); if (ret != ERR_OK) { DISPLAY_HILOGE(COMP_FWK, "SendRequest is failed, error code: %d", ret); return false; } if (!reply.ReadBool(result)) { DISPLAY_HILOGE(COMP_FWK, "Readback fail!"); return false; } return result; } bool DisplayPowerMgrProxy::OverrideBrightness(uint32_t value, uint32_t displayId) { 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(COMP_FWK, "write descriptor failed!"); return result; } WRITE_PARCEL_WITH_RET(data, Uint32, value, false); WRITE_PARCEL_WITH_RET(data, Uint32, displayId, false); int ret = remote->SendRequest(static_cast(IDisplayPowerMgr::OVERRIDE_BRIGHTNESS), data, reply, option); if (ret != ERR_OK) { DISPLAY_HILOGE(COMP_FWK, "SendRequest is failed, error code: %d", ret); return result; } if (!reply.ReadBool(result)) { DISPLAY_HILOGE(COMP_FWK, "Readback fail!"); return result; } return result; } bool DisplayPowerMgrProxy::RestoreBrightness(uint32_t displayId) { sptr remote = Remote(); uint32_t result = 0; RETURN_IF_WITH_RET(remote == nullptr, result); MessageParcel data; MessageParcel reply; MessageOption option; if (!data.WriteInterfaceToken(DisplayPowerMgrProxy::GetDescriptor())) { DISPLAY_HILOGE(COMP_FWK, "write descriptor failed!"); return result; } WRITE_PARCEL_WITH_RET(data, Uint32, displayId, false); int ret = remote->SendRequest(static_cast(IDisplayPowerMgr::RESTORE_BRIGHTNESS), data, reply, option); if (ret != ERR_OK) { DISPLAY_HILOGE(COMP_FWK, "SendRequest is failed, error code: %d", ret); return result; } if (!reply.ReadUint32(result)) { DISPLAY_HILOGE(COMP_FWK, "Readback fail!"); return result; } return result; } uint32_t DisplayPowerMgrProxy::GetBrightness(uint32_t displayId) { sptr remote = Remote(); uint32_t result = 0; RETURN_IF_WITH_RET(remote == nullptr, result); MessageParcel data; MessageParcel reply; MessageOption option; if (!data.WriteInterfaceToken(DisplayPowerMgrProxy::GetDescriptor())) { DISPLAY_HILOGE(COMP_FWK, "write descriptor failed!"); return result; } WRITE_PARCEL_WITH_RET(data, Uint32, displayId, false); int ret = remote->SendRequest(static_cast(IDisplayPowerMgr::GET_BRIGHTNESS), data, reply, option); if (ret != ERR_OK) { DISPLAY_HILOGE(COMP_FWK, "SendRequest is failed, error code: %d", ret); return result; } if (!reply.ReadUint32(result)) { DISPLAY_HILOGE(COMP_FWK, "Readback fail!"); return result; } return result; } uint32_t DisplayPowerMgrProxy::GetDefaultBrightness() { sptr remote = Remote(); uint32_t result = 0; RETURN_IF_WITH_RET(remote == nullptr, result); MessageParcel data; MessageParcel reply; MessageOption option; if (!data.WriteInterfaceToken(DisplayPowerMgrProxy::GetDescriptor())) { DISPLAY_HILOGE(COMP_FWK, "write descriptor failed!"); return result; } int ret = remote->SendRequest(static_cast(IDisplayPowerMgr::GET_DEFAULT_BRIGHTNESS), data, reply, option); if (ret != ERR_OK) { DISPLAY_HILOGE(COMP_FWK, "SendRequest is failed, error code: %d", ret); return result; } if (!reply.ReadUint32(result)) { DISPLAY_HILOGE(COMP_FWK, "Readback fail!"); return result; } return result; } uint32_t DisplayPowerMgrProxy::GetMaxBrightness() { sptr remote = Remote(); uint32_t result = 0; RETURN_IF_WITH_RET(remote == nullptr, result); MessageParcel data; MessageParcel reply; MessageOption option; if (!data.WriteInterfaceToken(DisplayPowerMgrProxy::GetDescriptor())) { DISPLAY_HILOGE(COMP_FWK, "write descriptor failed!"); return result; } int ret = remote->SendRequest(static_cast(IDisplayPowerMgr::GET_MAX_BRIGHTNESS), data, reply, option); if (ret != ERR_OK) { DISPLAY_HILOGE(COMP_FWK, "SendRequest is failed, error code: %d", ret); return result; } if (!reply.ReadUint32(result)) { DISPLAY_HILOGE(COMP_FWK, "Readback fail!"); return result; } return result; } uint32_t DisplayPowerMgrProxy::GetMinBrightness() { sptr remote = Remote(); uint32_t result = 0; RETURN_IF_WITH_RET(remote == nullptr, result); MessageParcel data; MessageParcel reply; MessageOption option; if (!data.WriteInterfaceToken(DisplayPowerMgrProxy::GetDescriptor())) { DISPLAY_HILOGE(COMP_FWK, "write descriptor failed!"); return result; } int ret = remote->SendRequest(static_cast(IDisplayPowerMgr::GET_MIN_BRIGHTNESS), data, reply, option); if (ret != ERR_OK) { DISPLAY_HILOGE(COMP_FWK, "SendRequest is failed, error code: %d", ret); return result; } if (!reply.ReadUint32(result)) { DISPLAY_HILOGE(COMP_FWK, "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(COMP_FWK, "write descriptor failed!"); 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(COMP_FWK, "SendRequest is failed, error code: %d", ret); return result; } if (!reply.ReadBool(result)) { DISPLAY_HILOGE(COMP_FWK, "Readback fail!"); return result; } return result; } bool DisplayPowerMgrProxy::AutoAdjustBrightness(bool enable) { 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(COMP_FWK, "write descriptor failed!"); return result; } WRITE_PARCEL_WITH_RET(data, Bool, enable, false); int ret = remote->SendRequest(static_cast(IDisplayPowerMgr::AUTO_ADJUST_BRIGHTNESS), data, reply, option); if (ret != ERR_OK) { DISPLAY_HILOGE(COMP_FWK, "SendRequest is failed, error code: %d", ret); return result; } if (!reply.ReadBool(result)) { DISPLAY_HILOGE(COMP_FWK, "Readback fail!"); return result; } return result; } bool DisplayPowerMgrProxy::IsAutoAdjustBrightness() { 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(COMP_FWK, "write descriptor failed!"); return result; } int ret = remote->SendRequest(static_cast(IDisplayPowerMgr::IS_AUTO_ADJUST_BRIGHTNESS), data, reply, option); if (ret != ERR_OK) { DISPLAY_HILOGE(COMP_FWK, "SendRequest is failed, error code: %d", ret); return result; } if (!reply.ReadBool(result)) { DISPLAY_HILOGE(COMP_FWK, "Readback fail!"); return result; } return result; } bool DisplayPowerMgrProxy::RegisterCallback(sptr callback) { 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(COMP_FWK, "write descriptor failed!"); return result; } WRITE_PARCEL_WITH_RET(data, RemoteObject, callback->AsObject(), false); int ret = remote->SendRequest(static_cast(IDisplayPowerMgr::REGISTER_CALLBACK), data, reply, option); if (ret != ERR_OK) { DISPLAY_HILOGE(COMP_FWK, "SendRequest is failed, error code: %d", ret); return result; } if (!reply.ReadBool(result)) { DISPLAY_HILOGE(COMP_FWK, "Readback fail!"); return result; } return result; } bool DisplayPowerMgrProxy::BoostBrightness(int32_t timeoutMs, uint32_t displayId) { 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(COMP_FWK, "write descriptor failed!"); return result; } WRITE_PARCEL_WITH_RET(data, Int32, timeoutMs, false); WRITE_PARCEL_WITH_RET(data, Uint32, displayId, false); int ret = remote->SendRequest(static_cast(IDisplayPowerMgr::BOOST_BRIGHTNESS), data, reply, option); if (ret != ERR_OK) { DISPLAY_HILOGE(COMP_FWK, "SendRequest is failed, error code: %d", ret); return result; } if (!reply.ReadBool(result)) { DISPLAY_HILOGE(COMP_FWK, "Readback fail!"); return result; } return result; } bool DisplayPowerMgrProxy::CancelBoostBrightness(uint32_t displayId) { 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(COMP_FWK, "write descriptor failed!"); return result; } WRITE_PARCEL_WITH_RET(data, Uint32, displayId, false); int ret = remote->SendRequest(static_cast(IDisplayPowerMgr::CANCEL_BOOST_BRIGHTNESS), data, reply, option); if (ret != ERR_OK) { DISPLAY_HILOGE(COMP_FWK, "SendRequest is failed, error code: %d", ret); return result; } if (!reply.ReadBool(result)) { DISPLAY_HILOGE(COMP_FWK, "Readback fail!"); return result; } return result; } uint32_t DisplayPowerMgrProxy::GetDeviceBrightness(uint32_t displayId) { sptr remote = Remote(); uint32_t result = 0; RETURN_IF_WITH_RET(remote == nullptr, result); MessageParcel data; MessageParcel reply; MessageOption option; if (!data.WriteInterfaceToken(DisplayPowerMgrProxy::GetDescriptor())) { DISPLAY_HILOGE(COMP_FWK, "write descriptor failed!"); return result; } WRITE_PARCEL_WITH_RET(data, Uint32, displayId, false); int ret = remote->SendRequest(static_cast(IDisplayPowerMgr::GET_DEVICE_BRIGHTNESS), data, reply, option); if (ret != ERR_OK) { DISPLAY_HILOGE(COMP_FWK, "SendRequest is failed, error code: %d", ret); return result; } if (!reply.ReadUint32(result)) { DISPLAY_HILOGE(COMP_FWK, "Readback fail!"); return result; } return result; } DisplayErrors DisplayPowerMgrProxy::GetError() { return lastError_; } } // namespace DisplayPowerMgr } // namespace OHOS