1 /*
2 * Copyright (c) 2021 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 #include "net_policy_callback_proxy.h"
16
17 #include "net_policy_cellular_policy.h"
18 #include "net_mgr_log_wrapper.h"
19
20 namespace OHOS {
21 namespace NetManagerStandard {
NetPolicyCallbackProxy(const sptr<IRemoteObject> & impl)22 NetPolicyCallbackProxy::NetPolicyCallbackProxy(const sptr<IRemoteObject> &impl)
23 : IRemoteProxy<INetPolicyCallback>(impl)
24 {}
25
~NetPolicyCallbackProxy()26 NetPolicyCallbackProxy::~NetPolicyCallbackProxy() {}
27
NetUidPolicyChanged(uint32_t uid,NetUidPolicy policy)28 int32_t NetPolicyCallbackProxy::NetUidPolicyChanged(uint32_t uid, NetUidPolicy policy)
29 {
30 MessageParcel data;
31 if (!WriteInterfaceToken(data)) {
32 NETMGR_LOG_E("WriteInterfaceToken failed");
33 return ERR_FLATTEN_OBJECT;
34 }
35
36 if (!data.WriteUint32(uid)) {
37 return ERR_NULL_OBJECT;
38 }
39
40 if (!data.WriteUint32(static_cast<uint32_t>(policy))) {
41 return ERR_NULL_OBJECT;
42 }
43
44 sptr<IRemoteObject> remote = Remote();
45 if (remote == nullptr) {
46 NETMGR_LOG_E("Remote is null");
47 return ERR_NULL_OBJECT;
48 }
49
50 MessageParcel reply;
51 MessageOption option;
52 int32_t ret = remote->SendRequest(NET_POLICY_UIDPOLICY_CHANGED, data, reply, option);
53 if (ret != ERR_NONE) {
54 NETMGR_LOG_E("Proxy SendRequest failed, ret code:[%{public}d]", ret);
55 }
56 return ret;
57 }
58
NetBackgroundPolicyChanged(bool isBackgroundPolicyAllow)59 int32_t NetPolicyCallbackProxy::NetBackgroundPolicyChanged(bool isBackgroundPolicyAllow)
60 {
61 MessageParcel data;
62 if (!WriteInterfaceToken(data)) {
63 NETMGR_LOG_E("WriteInterfaceToken failed");
64 return ERR_FLATTEN_OBJECT;
65 }
66
67 if (!data.WriteBool(isBackgroundPolicyAllow)) {
68 return ERR_NULL_OBJECT;
69 }
70
71 sptr<IRemoteObject> remote = Remote();
72 if (remote == nullptr) {
73 NETMGR_LOG_E("Remote is null");
74 return ERR_NULL_OBJECT;
75 }
76
77 MessageParcel reply;
78 MessageOption option;
79 int32_t ret = remote->SendRequest(NET_POLICY_BACKGROUNDPOLICY_CHANGED, data, reply, option);
80 if (ret != ERR_NONE) {
81 NETMGR_LOG_E("Proxy SendRequest failed, ret code:[%{public}d]", ret);
82 }
83 return ret;
84 }
85
NetCellularPolicyChanged(const std::vector<NetPolicyCellularPolicy> & cellularPolicies)86 int32_t NetPolicyCallbackProxy::NetCellularPolicyChanged(const std::vector<NetPolicyCellularPolicy> &cellularPolicies)
87 {
88 if (cellularPolicies.empty()) {
89 NETMGR_LOG_E("NetCellularPolicyChanged proxy cellularPolicies empty");
90 return ERR_FLATTEN_OBJECT;
91 }
92
93 MessageParcel data;
94 if (!WriteInterfaceToken(data)) {
95 NETMGR_LOG_E("WriteInterfaceToken failed");
96 return ERR_FLATTEN_OBJECT;
97 }
98
99 if (!NetPolicyCellularPolicy::Marshalling(data, cellularPolicies)) {
100 NETMGR_LOG_E("Marshalling failed.");
101 return ERR_FLATTEN_OBJECT;
102 }
103
104 sptr<IRemoteObject> remote = Remote();
105 if (remote == nullptr) {
106 NETMGR_LOG_E("Remote is null");
107 return ERR_NULL_OBJECT;
108 }
109
110 MessageParcel reply;
111 MessageOption option;
112 int32_t ret = remote->SendRequest(NET_POLICY_CELLULARPOLICY_CHANGED, data, reply, option);
113 if (ret != ERR_NONE) {
114 NETMGR_LOG_E("Proxy SendRequest failed, ret code:[%{public}d]", ret);
115 }
116 return ret;
117 }
118
NetStrategySwitch(const std::string & simId,bool enable)119 int32_t NetPolicyCallbackProxy::NetStrategySwitch(const std::string &simId, bool enable)
120 {
121 MessageParcel data;
122 if (!WriteInterfaceToken(data)) {
123 NETMGR_LOG_E("WriteInterfaceToken failed");
124 return ERR_FLATTEN_OBJECT;
125 }
126
127 if (!data.WriteString(simId)) {
128 return ERR_NULL_OBJECT;
129 }
130
131 if (!data.WriteBool(enable)) {
132 return ERR_NULL_OBJECT;
133 }
134
135 sptr<IRemoteObject> remote = Remote();
136 if (remote == nullptr) {
137 NETMGR_LOG_E("Remote is null");
138 return ERR_NULL_OBJECT;
139 }
140
141 MessageParcel reply;
142 MessageOption option;
143 int32_t ret = remote->SendRequest(NET_POLICY_STRATEGYSWITCH_CHANGED, data, reply, option);
144 if (ret != ERR_NONE) {
145 NETMGR_LOG_E("Proxy SendRequest failed, ret code:[%{public}d]", ret);
146 }
147 return ret;
148 }
149
WriteInterfaceToken(MessageParcel & data)150 bool NetPolicyCallbackProxy::WriteInterfaceToken(MessageParcel &data)
151 {
152 if (!data.WriteInterfaceToken(NetPolicyCallbackProxy::GetDescriptor())) {
153 NETMGR_LOG_E("WriteInterfaceToken failed");
154 return false;
155 }
156 return true;
157 }
158 } // namespace NetManagerStandard
159 } // namespace OHOS
160