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
OnRestrictListChanged(int32_t uid,const std::string & name,uint32_t allowType,bool added)86 void StandbyServiceSubscriberProxy::OnRestrictListChanged(int32_t uid, const std::string& name, uint32_t allowType,
87 bool added)
88 {
89 sptr<IRemoteObject> remote = Remote();
90 if (remote == nullptr) {
91 STANDBYSERVICE_LOGW("OnRestrictListChanged remote is dead.");
92 return;
93 }
94 MessageParcel data;
95 if (!data.WriteInterfaceToken(StandbyServiceSubscriberProxy::GetDescriptor())) {
96 STANDBYSERVICE_LOGW("OnRestrictListChanged write interface token failed.");
97 return;
98 }
99
100 if (!data.WriteInt32(uid) || !data.WriteString(name) ||
101 !data.WriteUint32(allowType) || !data.WriteBool(added)) {
102 STANDBYSERVICE_LOGW("OnRestrictListChanged write notification failed.");
103 return;
104 }
105
106 MessageParcel reply;
107 MessageOption option = {MessageOption::TF_ASYNC};
108 int32_t ret = remote->SendRequest(
109 static_cast<uint32_t>(StandbySubscriberInterfaceCode::ON_RESTRICT_LIST_CHANGED), data, reply, option);
110 if (ret!= ERR_OK) {
111 STANDBYSERVICE_LOGE("OnRestrictListChanged SendRequest failed, error code: %d", ret);
112 }
113 }
114
OnPowerOverused(const std::string & module,uint32_t level)115 void StandbyServiceSubscriberProxy::OnPowerOverused(const std::string& module, uint32_t level)
116 {
117 sptr<IRemoteObject> remote = Remote();
118 if (remote == nullptr) {
119 STANDBYSERVICE_LOGW("OnPowerOverused remote is dead.");
120 return;
121 }
122 MessageParcel data;
123 if (!data.WriteInterfaceToken(StandbyServiceSubscriberProxy::GetDescriptor())) {
124 STANDBYSERVICE_LOGW("OnPowerOverused write interface token failed.");
125 return;
126 }
127
128 if (!data.WriteString(module) || !data.WriteUint32(level)) {
129 STANDBYSERVICE_LOGW("OnPowerOverused write notification failed.");
130 return;
131 }
132
133 MessageParcel reply;
134 MessageOption option = {MessageOption::TF_ASYNC};
135 int32_t ret = remote->SendRequest(
136 static_cast<uint32_t>(StandbySubscriberInterfaceCode::ON_POWER_OVERUSED), data, reply, option);
137 if (ret!= ERR_OK) {
138 STANDBYSERVICE_LOGE("OnPowerOverused SendRequest failed, error code: %d", ret);
139 }
140 }
141
142 } // namespace DevStandbyMgr
143 } // namespace OHOS