199552fe9Sopenharmony_ci/*
299552fe9Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
399552fe9Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
499552fe9Sopenharmony_ci * you may not use this file except in compliance with the License.
599552fe9Sopenharmony_ci * You may obtain a copy of the License at
699552fe9Sopenharmony_ci *
799552fe9Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
899552fe9Sopenharmony_ci *
999552fe9Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1099552fe9Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1199552fe9Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1299552fe9Sopenharmony_ci * See the License for the specific language governing permissions and
1399552fe9Sopenharmony_ci * limitations under the License.
1499552fe9Sopenharmony_ci */
1599552fe9Sopenharmony_ci
1699552fe9Sopenharmony_ci#include "standby_service_client.h"
1799552fe9Sopenharmony_ci
1899552fe9Sopenharmony_ci#include <message_parcel.h>
1999552fe9Sopenharmony_ci
2099552fe9Sopenharmony_ci#include "iservice_registry.h"
2199552fe9Sopenharmony_ci#include "system_ability_definition.h"
2299552fe9Sopenharmony_ci#include "standby_service_errors.h"
2399552fe9Sopenharmony_ci#include "standby_service_log.h"
2499552fe9Sopenharmony_ci#include "standby_service_proxy.h"
2599552fe9Sopenharmony_ci
2699552fe9Sopenharmony_cinamespace OHOS {
2799552fe9Sopenharmony_cinamespace DevStandbyMgr {
2899552fe9Sopenharmony_ciStandbyServiceClient::StandbyServiceClient() {}
2999552fe9Sopenharmony_ci
3099552fe9Sopenharmony_ciStandbyServiceClient::~StandbyServiceClient() {}
3199552fe9Sopenharmony_ci
3299552fe9Sopenharmony_ciStandbyServiceClient& StandbyServiceClient::GetInstance()
3399552fe9Sopenharmony_ci{
3499552fe9Sopenharmony_ci    static StandbyServiceClient StandbyServiceClient;
3599552fe9Sopenharmony_ci    return StandbyServiceClient;
3699552fe9Sopenharmony_ci}
3799552fe9Sopenharmony_ci
3899552fe9Sopenharmony_ciErrCode StandbyServiceClient::SubscribeStandbyCallback(const sptr<IStandbyServiceSubscriber>& subscriber)
3999552fe9Sopenharmony_ci{
4099552fe9Sopenharmony_ci    std::lock_guard<std::mutex> lock(mutex_);
4199552fe9Sopenharmony_ci    if (!GetStandbyServiceProxy()) {
4299552fe9Sopenharmony_ci        STANDBYSERVICE_LOGE("get standby service proxy failed");
4399552fe9Sopenharmony_ci        return ERR_STANDBY_SERVICE_NOT_CONNECTED;
4499552fe9Sopenharmony_ci    }
4599552fe9Sopenharmony_ci    if (subscriber == nullptr) {
4699552fe9Sopenharmony_ci        STANDBYSERVICE_LOGE("subscriber is nullptr");
4799552fe9Sopenharmony_ci        return ERR_STANDBY_INVALID_PARAM;
4899552fe9Sopenharmony_ci    }
4999552fe9Sopenharmony_ci    return standbyServiceProxy_->SubscribeStandbyCallback(subscriber);
5099552fe9Sopenharmony_ci}
5199552fe9Sopenharmony_ci
5299552fe9Sopenharmony_ciErrCode StandbyServiceClient::UnsubscribeStandbyCallback(const sptr<IStandbyServiceSubscriber>& subscriber)
5399552fe9Sopenharmony_ci{
5499552fe9Sopenharmony_ci    std::lock_guard<std::mutex> lock(mutex_);
5599552fe9Sopenharmony_ci    if (!GetStandbyServiceProxy()) {
5699552fe9Sopenharmony_ci        STANDBYSERVICE_LOGE("get standby service proxy failed");
5799552fe9Sopenharmony_ci        return ERR_STANDBY_SERVICE_NOT_CONNECTED;
5899552fe9Sopenharmony_ci    }
5999552fe9Sopenharmony_ci    if (subscriber == nullptr) {
6099552fe9Sopenharmony_ci        STANDBYSERVICE_LOGE("subscriber is nullptr");
6199552fe9Sopenharmony_ci        return ERR_STANDBY_INVALID_PARAM;
6299552fe9Sopenharmony_ci    }
6399552fe9Sopenharmony_ci    return standbyServiceProxy_->UnsubscribeStandbyCallback(subscriber);
6499552fe9Sopenharmony_ci}
6599552fe9Sopenharmony_ci
6699552fe9Sopenharmony_ciErrCode StandbyServiceClient::ApplyAllowResource(const sptr<ResourceRequest>& resourceRequest)
6799552fe9Sopenharmony_ci{
6899552fe9Sopenharmony_ci    std::lock_guard<std::mutex> lock(mutex_);
6999552fe9Sopenharmony_ci    if (!GetStandbyServiceProxy()) {
7099552fe9Sopenharmony_ci        STANDBYSERVICE_LOGE("get standby service proxy failed");
7199552fe9Sopenharmony_ci        return ERR_STANDBY_SERVICE_NOT_CONNECTED;
7299552fe9Sopenharmony_ci    }
7399552fe9Sopenharmony_ci    if (resourceRequest == nullptr) {
7499552fe9Sopenharmony_ci        STANDBYSERVICE_LOGE("resource request is nullptr");
7599552fe9Sopenharmony_ci        return ERR_STANDBY_INVALID_PARAM;
7699552fe9Sopenharmony_ci    }
7799552fe9Sopenharmony_ci    return standbyServiceProxy_->ApplyAllowResource(resourceRequest);
7899552fe9Sopenharmony_ci}
7999552fe9Sopenharmony_ci
8099552fe9Sopenharmony_ciErrCode StandbyServiceClient::UnapplyAllowResource(const sptr<ResourceRequest>& resourceRequest)
8199552fe9Sopenharmony_ci{
8299552fe9Sopenharmony_ci    std::lock_guard<std::mutex> lock(mutex_);
8399552fe9Sopenharmony_ci    if (!GetStandbyServiceProxy()) {
8499552fe9Sopenharmony_ci        STANDBYSERVICE_LOGE("get standby service proxy failed");
8599552fe9Sopenharmony_ci        return ERR_STANDBY_SERVICE_NOT_CONNECTED;
8699552fe9Sopenharmony_ci    }
8799552fe9Sopenharmony_ci    if (resourceRequest == nullptr) {
8899552fe9Sopenharmony_ci        STANDBYSERVICE_LOGE("resource request is nullptr");
8999552fe9Sopenharmony_ci        return ERR_STANDBY_INVALID_PARAM;
9099552fe9Sopenharmony_ci    }
9199552fe9Sopenharmony_ci    return standbyServiceProxy_->UnapplyAllowResource(resourceRequest);
9299552fe9Sopenharmony_ci}
9399552fe9Sopenharmony_ci
9499552fe9Sopenharmony_ciErrCode StandbyServiceClient::GetAllowList(uint32_t allowType, std::vector<AllowInfo>& allowInfoArray,
9599552fe9Sopenharmony_ci    uint32_t reasonCode)
9699552fe9Sopenharmony_ci{
9799552fe9Sopenharmony_ci    std::lock_guard<std::mutex> lock(mutex_);
9899552fe9Sopenharmony_ci    if (!GetStandbyServiceProxy()) {
9999552fe9Sopenharmony_ci        STANDBYSERVICE_LOGE("get standby service proxy failed");
10099552fe9Sopenharmony_ci        return ERR_STANDBY_SERVICE_NOT_CONNECTED;
10199552fe9Sopenharmony_ci    }
10299552fe9Sopenharmony_ci    if (!allowInfoArray.empty()) {
10399552fe9Sopenharmony_ci        STANDBYSERVICE_LOGW("allow info array is not empty");
10499552fe9Sopenharmony_ci        allowInfoArray.clear();
10599552fe9Sopenharmony_ci    }
10699552fe9Sopenharmony_ci    return standbyServiceProxy_->GetAllowList(allowType, allowInfoArray, reasonCode);
10799552fe9Sopenharmony_ci}
10899552fe9Sopenharmony_ci
10999552fe9Sopenharmony_ciErrCode StandbyServiceClient::IsDeviceInStandby(bool& isStandby)
11099552fe9Sopenharmony_ci{
11199552fe9Sopenharmony_ci    std::lock_guard<std::mutex> lock(mutex_);
11299552fe9Sopenharmony_ci    if (!GetStandbyServiceProxy()) {
11399552fe9Sopenharmony_ci        STANDBYSERVICE_LOGE("get standby service proxy failed");
11499552fe9Sopenharmony_ci        return ERR_STANDBY_SERVICE_NOT_CONNECTED;
11599552fe9Sopenharmony_ci    }
11699552fe9Sopenharmony_ci    return standbyServiceProxy_->IsDeviceInStandby(isStandby);
11799552fe9Sopenharmony_ci}
11899552fe9Sopenharmony_ci
11999552fe9Sopenharmony_ciErrCode StandbyServiceClient::SetNatInterval(uint32_t& type, bool& enable, uint32_t& interval)
12099552fe9Sopenharmony_ci{
12199552fe9Sopenharmony_ci    std::lock_guard<std::mutex> lock(mutex_);
12299552fe9Sopenharmony_ci    if (!GetStandbyServiceProxy()) {
12399552fe9Sopenharmony_ci        STANDBYSERVICE_LOGE("get standby service proxy failed");
12499552fe9Sopenharmony_ci        return ERR_STANDBY_SERVICE_NOT_CONNECTED;
12599552fe9Sopenharmony_ci    }
12699552fe9Sopenharmony_ci    return standbyServiceProxy_->SetNatInterval(type, enable, interval);
12799552fe9Sopenharmony_ci}
12899552fe9Sopenharmony_ci
12999552fe9Sopenharmony_ciErrCode StandbyServiceClient::HandleEvent(const std::shared_ptr<ResData> &resData)
13099552fe9Sopenharmony_ci{
13199552fe9Sopenharmony_ci    std::lock_guard<std::mutex> lock(mutex_);
13299552fe9Sopenharmony_ci    std::string sceneInfo = resData->payload.dump();
13399552fe9Sopenharmony_ci    if (!GetStandbyServiceProxy()) {
13499552fe9Sopenharmony_ci        STANDBYSERVICE_LOGE("get standby service proxy failed");
13599552fe9Sopenharmony_ci        return ERR_STANDBY_SERVICE_NOT_CONNECTED;
13699552fe9Sopenharmony_ci    }
13799552fe9Sopenharmony_ci    return standbyServiceProxy_->HandleEvent(resData->resType, resData->value, sceneInfo);
13899552fe9Sopenharmony_ci}
13999552fe9Sopenharmony_ci
14099552fe9Sopenharmony_ciErrCode StandbyServiceClient::ReportWorkSchedulerStatus(bool started, int32_t uid, const std::string& bundleName)
14199552fe9Sopenharmony_ci{
14299552fe9Sopenharmony_ci    std::lock_guard<std::mutex> lock(mutex_);
14399552fe9Sopenharmony_ci    if (!GetStandbyServiceProxy()) {
14499552fe9Sopenharmony_ci        STANDBYSERVICE_LOGE("get standby service proxy failed");
14599552fe9Sopenharmony_ci        return ERR_STANDBY_SERVICE_NOT_CONNECTED;
14699552fe9Sopenharmony_ci    }
14799552fe9Sopenharmony_ci    return standbyServiceProxy_->ReportWorkSchedulerStatus(started, uid, bundleName);
14899552fe9Sopenharmony_ci}
14999552fe9Sopenharmony_ci
15099552fe9Sopenharmony_ciErrCode StandbyServiceClient::GetRestrictList(uint32_t restrictType, std::vector<AllowInfo>& restrictInfoList,
15199552fe9Sopenharmony_ci    uint32_t reasonCode)
15299552fe9Sopenharmony_ci{
15399552fe9Sopenharmony_ci    std::lock_guard<std::mutex> lock(mutex_);
15499552fe9Sopenharmony_ci    if (!GetStandbyServiceProxy()) {
15599552fe9Sopenharmony_ci        STANDBYSERVICE_LOGE("get standby service proxy failed");
15699552fe9Sopenharmony_ci        return ERR_STANDBY_SERVICE_NOT_CONNECTED;
15799552fe9Sopenharmony_ci    }
15899552fe9Sopenharmony_ci    if (!restrictInfoList.empty()) {
15999552fe9Sopenharmony_ci        STANDBYSERVICE_LOGW("restrict info array is not empty");
16099552fe9Sopenharmony_ci        restrictInfoList.clear();
16199552fe9Sopenharmony_ci    }
16299552fe9Sopenharmony_ci    return standbyServiceProxy_->GetRestrictList(restrictType, restrictInfoList, reasonCode);
16399552fe9Sopenharmony_ci}
16499552fe9Sopenharmony_ci
16599552fe9Sopenharmony_ciErrCode StandbyServiceClient::IsStrategyEnabled(const std::string& strategyName, bool& isEnabled)
16699552fe9Sopenharmony_ci{
16799552fe9Sopenharmony_ci    std::lock_guard<std::mutex> lock(mutex_);
16899552fe9Sopenharmony_ci    if (!GetStandbyServiceProxy()) {
16999552fe9Sopenharmony_ci        STANDBYSERVICE_LOGE("get standby service proxy failed");
17099552fe9Sopenharmony_ci        return ERR_STANDBY_SERVICE_NOT_CONNECTED;
17199552fe9Sopenharmony_ci    }
17299552fe9Sopenharmony_ci    return standbyServiceProxy_->IsStrategyEnabled(strategyName, isEnabled);
17399552fe9Sopenharmony_ci}
17499552fe9Sopenharmony_ci
17599552fe9Sopenharmony_ciErrCode StandbyServiceClient::ReportPowerOverused(const std::string &module, uint32_t level)
17699552fe9Sopenharmony_ci{
17799552fe9Sopenharmony_ci    std::lock_guard<std::mutex> lock(mutex_);
17899552fe9Sopenharmony_ci    STANDBYSERVICE_LOGI("[PowerOverused] StandbyClient: power overused, module name: %{public}s, "
17999552fe9Sopenharmony_ci        "level: %{public}u.", module.c_str(), level);
18099552fe9Sopenharmony_ci
18199552fe9Sopenharmony_ci    if (!GetStandbyServiceProxy()) {
18299552fe9Sopenharmony_ci        STANDBYSERVICE_LOGE("get standby service proxy failed");
18399552fe9Sopenharmony_ci        return ERR_STANDBY_SERVICE_NOT_CONNECTED;
18499552fe9Sopenharmony_ci    }
18599552fe9Sopenharmony_ci    return standbyServiceProxy_->ReportPowerOverused(module, level);
18699552fe9Sopenharmony_ci}
18799552fe9Sopenharmony_ci
18899552fe9Sopenharmony_ciErrCode StandbyServiceClient::ReportDeviceStateChanged(DeviceStateType type, bool enabled)
18999552fe9Sopenharmony_ci{
19099552fe9Sopenharmony_ci    std::lock_guard<std::mutex> lock(mutex_);
19199552fe9Sopenharmony_ci    STANDBYSERVICE_LOGI("device state changed, state type: %{public}d, enabled: %{public}d",
19299552fe9Sopenharmony_ci        static_cast<int32_t>(type), enabled);
19399552fe9Sopenharmony_ci    if (!GetStandbyServiceProxy()) {
19499552fe9Sopenharmony_ci        STANDBYSERVICE_LOGE("get standby service proxy failed");
19599552fe9Sopenharmony_ci        return ERR_STANDBY_SERVICE_NOT_CONNECTED;
19699552fe9Sopenharmony_ci    }
19799552fe9Sopenharmony_ci    return standbyServiceProxy_->ReportDeviceStateChanged(type, enabled);
19899552fe9Sopenharmony_ci}
19999552fe9Sopenharmony_ci
20099552fe9Sopenharmony_cibool StandbyServiceClient::GetStandbyServiceProxy()
20199552fe9Sopenharmony_ci{
20299552fe9Sopenharmony_ci    if (standbyServiceProxy_ != nullptr) {
20399552fe9Sopenharmony_ci        return true;
20499552fe9Sopenharmony_ci    }
20599552fe9Sopenharmony_ci    sptr<ISystemAbilityManager> systemAbilityManager =
20699552fe9Sopenharmony_ci        SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
20799552fe9Sopenharmony_ci    if (systemAbilityManager == nullptr) {
20899552fe9Sopenharmony_ci        STANDBYSERVICE_LOGE("get standby service proxy failed");
20999552fe9Sopenharmony_ci        return false;
21099552fe9Sopenharmony_ci    }
21199552fe9Sopenharmony_ci
21299552fe9Sopenharmony_ci    sptr<IRemoteObject> remoteObject =
21399552fe9Sopenharmony_ci        systemAbilityManager->GetSystemAbility(DEVICE_STANDBY_SERVICE_SYSTEM_ABILITY_ID);
21499552fe9Sopenharmony_ci    if (remoteObject == nullptr) {
21599552fe9Sopenharmony_ci        STANDBYSERVICE_LOGE("get standby service system ability failed");
21699552fe9Sopenharmony_ci        return false;
21799552fe9Sopenharmony_ci    }
21899552fe9Sopenharmony_ci
21999552fe9Sopenharmony_ci    standbyServiceProxy_ = iface_cast<IStandbyService>(remoteObject);
22099552fe9Sopenharmony_ci    if ((standbyServiceProxy_ == nullptr) || (standbyServiceProxy_->AsObject() == nullptr)) {
22199552fe9Sopenharmony_ci        STANDBYSERVICE_LOGE("standby service proxy iface_cast from remote Onject failed");
22299552fe9Sopenharmony_ci        return false;
22399552fe9Sopenharmony_ci    }
22499552fe9Sopenharmony_ci
22599552fe9Sopenharmony_ci    deathRecipient_ = new (std::nothrow) StandbyServiceDeathRecipient(*this);
22699552fe9Sopenharmony_ci    if (deathRecipient_ == nullptr) {
22799552fe9Sopenharmony_ci        return false;
22899552fe9Sopenharmony_ci    }
22999552fe9Sopenharmony_ci
23099552fe9Sopenharmony_ci    standbyServiceProxy_->AsObject()->AddDeathRecipient(deathRecipient_);
23199552fe9Sopenharmony_ci    return true;
23299552fe9Sopenharmony_ci}
23399552fe9Sopenharmony_ci
23499552fe9Sopenharmony_civoid StandbyServiceClient::ResetStandbyServiceClient()
23599552fe9Sopenharmony_ci{
23699552fe9Sopenharmony_ci    std::lock_guard<std::mutex> lock(mutex_);
23799552fe9Sopenharmony_ci    if ((standbyServiceProxy_ != nullptr)&& (standbyServiceProxy_->AsObject() != nullptr)) {
23899552fe9Sopenharmony_ci        standbyServiceProxy_->AsObject()->RemoveDeathRecipient(deathRecipient_);
23999552fe9Sopenharmony_ci    }
24099552fe9Sopenharmony_ci    standbyServiceProxy_ = nullptr;
24199552fe9Sopenharmony_ci}
24299552fe9Sopenharmony_ci
24399552fe9Sopenharmony_ciStandbyServiceClient::StandbyServiceDeathRecipient::StandbyServiceDeathRecipient(
24499552fe9Sopenharmony_ci    StandbyServiceClient& standbyServiceClient)
24599552fe9Sopenharmony_ci    : standbyServiceClient_(standbyServiceClient) {}
24699552fe9Sopenharmony_ci
24799552fe9Sopenharmony_ciStandbyServiceClient::StandbyServiceDeathRecipient::~StandbyServiceDeathRecipient() {}
24899552fe9Sopenharmony_ci
24999552fe9Sopenharmony_civoid StandbyServiceClient::StandbyServiceDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& remote)
25099552fe9Sopenharmony_ci{
25199552fe9Sopenharmony_ci    standbyServiceClient_.ResetStandbyServiceClient();
25299552fe9Sopenharmony_ci}
25399552fe9Sopenharmony_ci}  // namespace DevStandbyMgr
25499552fe9Sopenharmony_ci}  // namespace OHOS