1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "standby_service_subscriber_proxy.h"
17 
18 #include <message_parcel.h>
19 
20 #include "standby_service_errors.h"
21 #include "standby_service_log.h"
22 #include "standby_ipc_interface_code.h"
23 
24 namespace OHOS {
25 namespace DevStandbyMgr {
StandbyServiceSubscriberProxy(const sptr<IRemoteObject> & impl)26 StandbyServiceSubscriberProxy::StandbyServiceSubscriberProxy(const sptr<IRemoteObject>& impl)
27     : IRemoteProxy<IStandbyServiceSubscriber>(impl) {}
~StandbyServiceSubscriberProxy()28 StandbyServiceSubscriberProxy::~StandbyServiceSubscriberProxy() {}
29 
OnDeviceIdleMode(bool napped,bool sleeping)30 void StandbyServiceSubscriberProxy::OnDeviceIdleMode(bool napped, bool sleeping)
31 {
32     sptr<IRemoteObject> remote = Remote();
33     if (remote == nullptr) {
34         STANDBYSERVICE_LOGW("OnDeviceIdleMode remote is dead.");
35         return;
36     }
37     MessageParcel data;
38     if (!data.WriteInterfaceToken(StandbyServiceSubscriberProxy::GetDescriptor())) {
39         STANDBYSERVICE_LOGW("OnDeviceIdleMode write interface token failed.");
40         return;
41     }
42 
43     if (!data.WriteBool(napped) || !data.WriteBool(sleeping)) {
44         STANDBYSERVICE_LOGW("OnDeviceIdleMode write parameter failed.");
45         return;
46     }
47 
48     MessageParcel reply;
49     MessageOption option = {MessageOption::TF_ASYNC};
50     int32_t ret = remote->SendRequest(
51         static_cast<uint32_t>(StandbySubscriberInterfaceCode::ON_DEVICE_IDLE_MODE), data, reply, option);
52     if (ret!= ERR_OK) {
53         STANDBYSERVICE_LOGE("OnDeviceIdleMode SendRequest failed, error code: %d", ret);
54     }
55 }
56 
OnAllowListChanged(int32_t uid,const std::string & name,uint32_t allowType,bool added)57 void StandbyServiceSubscriberProxy::OnAllowListChanged(int32_t uid, const std::string& name, uint32_t allowType,
58     bool added)
59 {
60     sptr<IRemoteObject> remote = Remote();
61     if (remote == nullptr) {
62         STANDBYSERVICE_LOGW("OnAllowListChanged remote is dead.");
63         return;
64     }
65     MessageParcel data;
66     if (!data.WriteInterfaceToken(StandbyServiceSubscriberProxy::GetDescriptor())) {
67         STANDBYSERVICE_LOGW("OnAllowListChanged write interface token failed.");
68         return;
69     }
70 
71     if (!data.WriteInt32(uid) || !data.WriteString(name) ||
72         !data.WriteUint32(allowType) || !data.WriteBool(added)) {
73         STANDBYSERVICE_LOGW("OnAllowListChanged write notification failed.");
74         return;
75     }
76 
77     MessageParcel reply;
78     MessageOption option = {MessageOption::TF_ASYNC};
79     int32_t ret = remote->SendRequest(
80         static_cast<uint32_t>(StandbySubscriberInterfaceCode::ON_ALLOW_LIST_CHANGED), data, reply, option);
81     if (ret!= ERR_OK) {
82         STANDBYSERVICE_LOGE("OnAllowListChanged SendRequest failed, error code: %d", ret);
83     }
84 }
85 
OnPowerOverused(const std::string & module,uint32_t level)86 void StandbyServiceSubscriberProxy::OnPowerOverused(const std::string& module, uint32_t level)
87 {
88     sptr<IRemoteObject> remote = Remote();
89     if (remote == nullptr) {
90         STANDBYSERVICE_LOGW("OnPowerOverused remote is dead.");
91         return;
92     }
93     MessageParcel data;
94     if (!data.WriteInterfaceToken(StandbyServiceSubscriberProxy::GetDescriptor())) {
95         STANDBYSERVICE_LOGW("OnPowerOverused write interface token failed.");
96         return;
97     }
98 
99     if (!data.WriteString(module) || !data.WriteUint32(level)) {
100         STANDBYSERVICE_LOGW("OnPowerOverused write notification failed.");
101         return;
102     }
103 
104     MessageParcel reply;
105     MessageOption option = {MessageOption::TF_ASYNC};
106     int32_t ret = remote->SendRequest(
107         static_cast<uint32_t>(StandbySubscriberInterfaceCode::ON_POWER_OVERUSED), data, reply, option);
108     if (ret!= ERR_OK) {
109         STANDBYSERVICE_LOGE("OnPowerOverused SendRequest failed, error code: %d", ret);
110     }
111 }
112 
113 }  // namespace DevStandbyMgr
114 }  // namespace OHOS