1 /*
2  * Copyright (c) 2021-2022 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 "net_policy_callback_proxy.h"
17 
18 #include "net_mgr_log_wrapper.h"
19 #include "net_quota_policy.h"
20 
21 namespace OHOS {
22 namespace NetManagerStandard {
NetPolicyCallbackProxy(const sptr<IRemoteObject> & impl)23 NetPolicyCallbackProxy::NetPolicyCallbackProxy(const sptr<IRemoteObject> &impl) : IRemoteProxy<INetPolicyCallback>(impl)
24 {
25 }
26 
27 NetPolicyCallbackProxy::~NetPolicyCallbackProxy() = default;
28 
NetUidPolicyChange(uint32_t uid,uint32_t policy)29 int32_t NetPolicyCallbackProxy::NetUidPolicyChange(uint32_t uid, uint32_t policy)
30 {
31     MessageParcel data;
32     if (!WriteInterfaceToken(data)) {
33         NETMGR_LOG_E("WriteInterfaceToken failed");
34         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
35     }
36 
37     if (!data.WriteUint32(uid)) {
38         NETMGR_LOG_E("Write uint32 data failed");
39         return NETMANAGER_ERR_WRITE_DATA_FAIL;
40     }
41 
42     if (!data.WriteUint32(policy)) {
43         NETMGR_LOG_E("Write uint32 data failed");
44         return NETMANAGER_ERR_WRITE_DATA_FAIL;
45     }
46 
47     return SendRequest(data, static_cast<uint32_t>(PolicyCallbackInterfaceCode::NOTIFY_NET_UID_POLICY_CHANGE));
48 }
49 
NetUidRuleChange(uint32_t uid,uint32_t rule)50 int32_t NetPolicyCallbackProxy::NetUidRuleChange(uint32_t uid, uint32_t rule)
51 {
52     MessageParcel data;
53     if (!WriteInterfaceToken(data)) {
54         NETMGR_LOG_E("WriteInterfaceToken failed");
55         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
56     }
57 
58     if (!data.WriteUint32(uid)) {
59         NETMGR_LOG_E("Write uint32 data failed");
60         return NETMANAGER_ERR_WRITE_DATA_FAIL;
61     }
62 
63     if (!data.WriteUint32(rule)) {
64         NETMGR_LOG_E("Write uint32 data failed");
65         return NETMANAGER_ERR_WRITE_DATA_FAIL;
66     }
67 
68     return SendRequest(data, static_cast<uint32_t>(PolicyCallbackInterfaceCode::NOTIFY_NET_UID_RULE_CHANGE));
69 }
70 
NetBackgroundPolicyChange(bool isBackgroundPolicyAllow)71 int32_t NetPolicyCallbackProxy::NetBackgroundPolicyChange(bool isBackgroundPolicyAllow)
72 {
73     MessageParcel data;
74     if (!WriteInterfaceToken(data)) {
75         NETMGR_LOG_E("WriteInterfaceToken failed");
76         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
77     }
78 
79     if (!data.WriteBool(isBackgroundPolicyAllow)) {
80         NETMGR_LOG_E("Write Bool data failed");
81         return NETMANAGER_ERR_WRITE_DATA_FAIL;
82     }
83 
84     return SendRequest(data, static_cast<uint32_t>(PolicyCallbackInterfaceCode::NOTIFY_BACKGROUND_POLICY_CHANGE));
85 }
86 
NetQuotaPolicyChange(const std::vector<NetQuotaPolicy> & quotaPolicies)87 int32_t NetPolicyCallbackProxy::NetQuotaPolicyChange(const std::vector<NetQuotaPolicy> &quotaPolicies)
88 {
89     if (quotaPolicies.empty()) {
90         NETMGR_LOG_E("NetQuotaPolicyChange proxy quotaPolicies empty");
91         return POLICY_ERR_QUOTA_POLICY_NOT_EXIST;
92     }
93 
94     MessageParcel data;
95     if (!WriteInterfaceToken(data)) {
96         NETMGR_LOG_E("WriteInterfaceToken failed");
97         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
98     }
99 
100     if (!NetQuotaPolicy::Marshalling(data, quotaPolicies)) {
101         NETMGR_LOG_E("Marshalling failed.");
102         return NETMANAGER_ERR_WRITE_DATA_FAIL;
103     }
104 
105     return SendRequest(data, static_cast<uint32_t>(PolicyCallbackInterfaceCode::NOTIFY_NET_QUOTA_POLICY_CHANGE));
106 }
107 
NetStrategySwitch(const std::string & simId,bool enable)108 int32_t NetPolicyCallbackProxy::NetStrategySwitch(const std::string &simId, bool enable)
109 {
110     MessageParcel data;
111     if (!WriteInterfaceToken(data)) {
112         NETMGR_LOG_E("WriteInterfaceToken failed");
113         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
114     }
115 
116     if (!data.WriteString(simId)) {
117         NETMGR_LOG_E("WriteString String data failed");
118         return NETMANAGER_ERR_WRITE_DATA_FAIL;
119     }
120 
121     if (!data.WriteBool(enable)) {
122         NETMGR_LOG_E("WriteBool Bool data failed");
123         return NETMANAGER_ERR_WRITE_DATA_FAIL;
124     }
125 
126     return SendRequest(data, static_cast<uint32_t>(PolicyCallbackInterfaceCode::NET_POLICY_STRATEGYSWITCH_CHANGE));
127 }
128 
NetMeteredIfacesChange(std::vector<std::string> & ifaces)129 int32_t NetPolicyCallbackProxy::NetMeteredIfacesChange(std::vector<std::string> &ifaces)
130 {
131     MessageParcel data;
132     if (!WriteInterfaceToken(data)) {
133         NETMGR_LOG_E("WriteInterfaceToken failed");
134         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
135     }
136     uint32_t size = static_cast<uint32_t>(ifaces.size());
137     if (!data.WriteUint32(size)) {
138         NETMGR_LOG_E("Write uint32 data failed");
139         return NETMANAGER_ERR_WRITE_DATA_FAIL;
140     }
141 
142     for (uint32_t i = 0; i < ifaces.size(); ++i) {
143         if (!data.WriteString(ifaces[i])) {
144             NETMGR_LOG_E("Write String data failed");
145             return NETMANAGER_ERR_WRITE_DATA_FAIL;
146         }
147     }
148 
149     return SendRequest(data, static_cast<uint32_t>(PolicyCallbackInterfaceCode::NOTIFY_NET_METERED_IFACES_CHANGE));
150 }
151 
WriteInterfaceToken(MessageParcel & data)152 bool NetPolicyCallbackProxy::WriteInterfaceToken(MessageParcel &data)
153 {
154     if (!data.WriteInterfaceToken(NetPolicyCallbackProxy::GetDescriptor())) {
155         NETMGR_LOG_E("WriteInterfaceToken failed");
156         return false;
157     }
158     return true;
159 }
160 
SendRequest(MessageParcel & data,uint32_t code)161 int32_t NetPolicyCallbackProxy::SendRequest(MessageParcel &data, uint32_t code)
162 {
163     sptr<IRemoteObject> remote = Remote();
164     if (remote == nullptr) {
165         NETMGR_LOG_E("Remote is null");
166         return NETMANAGER_ERR_LOCAL_PTR_NULL;
167     }
168 
169     MessageParcel reply;
170     MessageOption option;
171     int32_t ret = remote->SendRequest(code, data, reply, option);
172     if (ret != 0) {
173         NETMGR_LOG_E("Proxy SendRequest failed, ret code:[%{public}d]", ret);
174         return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
175     }
176 
177     return ret;
178 }
179 } // namespace NetManagerStandard
180 } // namespace OHOS
181